Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/pqt/autorib/rib/RIBParams.java


1   //AutoRIB
2   // Copyright © 1998 - 2002, P W Quint
3   //
4   // Contact: autorib00@aol.com
5   //
6   // This library is free software; you can redistribute it and/or
7   // modify it under the terms of the GNU General Public
8   // License as published by the Free Software Foundation; either
9   // version 2 of the License, or (at your option) any later version.
10  //
11  // This library is distributed in the hope that it will be useful,
12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  // General Public License for more details.
15  //
16  // You should have received a copy of the GNU General Public
17  // License along with this library; if not, write to the Free Software
18  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  
20  package org.pqt.autorib.rib;
21  import java.io.*;
22  import java.util.*;
23  
24  import org.pqt.autorib.tokenizer.*;
25  import org.pqt.autorib.globals.*;
26  import org.pqt.autorib.util.*;
27  
28  /**
29   * A class to store parameters for a shader or other object. Parameters are
30   * in the form of a string name followed by a value e.g "Ks" .4, or "shadow" "myfile".
31   * Each name in the parameter list is unique.
32   *
33   */
34  public class RIBParams extends Object
35    {
36  
37     Vector params; //Use a vector rather than Hashtable to save memory
38  
39     protected class ParamRec extends Object
40        {
41          String Key;
42          Token Value;
43  
44          public ParamRec()
45          { super(); }
46  
47          public ParamRec(String k, Token v)
48            {
49              super();
50              Key = k;
51              Value = (Token)(v.clone());
52            }
53  
54          public int compareTo(Object o)
55            {
56              return ((ParamRec)o).Key.compareTo(Key);
57            }
58  
59          public void write(Writer out) throws IOException
60            {
61              Token.write(out,"\"" + Key + "\" ");
62              Value.write(out);
63            }
64  
65          /* this is a not quite straight forward since for parameters
66          defined in line the Key string in the paramrec will contain not
67          just the parameter name but its type also */
68          public boolean equals(Object o)
69            {
70              boolean res = o instanceof ParamRec;
71              if (res)
72                {
73                  res = false;
74                   int pos = Key.lastIndexOf(((ParamRec)o).Key);
75                  if (pos == 0) //at the beginning and so the whole string
76                    res = Key.equals(((ParamRec)o).Key);
77                  else if (pos > 0)
78                      res = ((Character.isWhitespace(Key.charAt(pos - 1)))
79                        && (pos + ((ParamRec)o).Key.length() == Key.length()));
80                    //if there is not whitespace before it it is not the
81                    //paramname so ignore
82                }
83              return res;
84            }
85  
86  
87          public Token getValue()
88            {
89              return Value;
90            }
91  
92  
93        }
94  
95        /** The default constructor
96         */      
97       public RIBParams()
98        {
99          super();
100         params = new Vector(10);
101       }
102 
103       /** This creates a new set of parameters based on an
104        * existing set (which is cloned)
105        * @param r the RIBParams to base this instance on
106        */      
107      public RIBParams(RIBParams r)
108        {
109         super();
110         params = (Vector) r.params.clone();
111       }
112 
113       /** Read in a set of parameters of the form "name" value
114        * @param in the Tokeniser to read from
115        * @throws FormatException is thrown if the name value list has
116        * syntax errors
117        * @throws IOException is thrown if an IO error is found
118        */      
119      public void read(Tokenizer in) throws FormatException,
120       IOException
121       {
122         ParamRec p;
123         String s;
124         boolean Done = false;
125 
126         while (!Done)
127           {
128             in.getToken();
129             if (in.token.tokenType == Token.STRING)
130               {
131                 s = in.token.stringVal;
132                 in.getToken();
133                 if ((in.token.tokenType == Token.REQUEST) ||
134                   (in.token.tokenType == Token.EOF) ||
135                   (in.token.tokenType == Token.CLOSEBRACE) ||
136                   (in.token.tokenType == Token.OPENBRACE))
137                   throw new FormatException("Error in parameter list", in);
138                 p = new ParamRec(s, in.token);
139                 params.addElement(p);
140               }
141             else
142               {
143                 in.pushBack();
144                 Done = true;
145               }
146           }
147         params.trimToSize();
148       }
149 
150 
151       /** Write a set of parameters to the given output
152        * @param out where to senf the output
153        * @throws IOException
154        */      
155       public void write(Writer out) throws IOException
156         {
157           int i;
158           for (i = 0; i < params.size(); i++)
159             ((ParamRec)params.elementAt(i)).write(out);
160         }
161 
162         /** Returns a the value of a parameter with a
163          * given name
164          * @param key the name to look for
165          * @return the Token containing the value
166          */        
167       public Token get(String key)
168         {
169           int i;
170           ParamRec p = new ParamRec(key, new Token());
171           for (i = 0; i < params.size(); i++)
172             if (((ParamRec) params.elementAt(i)).equals(p))
173               break;
174           return i < params.size() ?
175             ((ParamRec)params.elementAt(i)).getValue() :
176             null;
177         }
178 
179         /** Replaces the value of parameter with the
180          * given name with a new one. If a parameter
181          * of the given name does not exist it is
182          * created, and appended to the list of parameters
183          * @param key the name of the parameter to change
184          * @param value the new value
185          * @return true if the named parameter was found
186          */
187       public boolean replace(String key, Token value) {
188           int i;
189           boolean found = false;
190           ParamRec p = new ParamRec(key, value);
191           for (i = 0; i < params.size(); i++)
192               if (((ParamRec) params.elementAt(i)).equals(p)) {
193                   found = true;
194                   break;
195               }
196           if (found) {
197               p.Key = ((ParamRec)params.elementAt(i)).Key;
198               params.remove(i);
199           }
200           params.addElement(p);
201           return found;
202       }
203 
204         /** Add a new parameter and value to the
205          * parameters list. If a parameter of the name
206          * already exists, its value is changed to match
207          * the new value
208          * @param key the name of the parameter to add
209          * @param value the value of the parameter to add
210          */        
211       public void add(String key, Token value)
212         {
213           replace(key,value);
214         }
215 
216         /** Remove the named parameter from the list. If the
217          * name does not exist, fail silently.
218          * @param key the name of the parameter to remove
219          */        
220       public void remove(String key)
221         {
222           ParamRec p = new ParamRec(key, new Token());
223           params.removeElement(p);
224         }
225 
226         /** Empty the parameter list
227          */        
228       public void clear()
229         {
230           params.setSize(0);
231         }
232 
233         /** Return the number of parameters in the list
234          * @return
235          */        
236       public int size()
237         {
238           return params.size();
239         }
240      }