KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > text > Options


1 // Copyright (c) 2003 Per M.A. Bothner.
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

4 package gnu.text;
5 import java.util.Hashtable JavaDoc;
6 import java.util.Vector JavaDoc;
7 import java.util.Enumeration JavaDoc;
8
9 /** Mananges a table of named options,
10  * Can inherit from another table of "default" options. */

11
12 public class Options
13 {
14   /** Bit indicating option value is a boolean. */
15   public static final int BOOLEAN_OPTION = 1;
16
17   public static final int STRING_OPTION = 2;
18
19   /** The option table contain defaults, that we "inherit" from. */
20   Options previous;
21
22   OptionInfo first;
23   OptionInfo last;
24
25   public Options ()
26   {
27   }
28
29   public Options (Options previous)
30   {
31     this.previous = previous;
32   }
33
34   /** Maps property keys to options values. */
35   Hashtable JavaDoc valueTable;
36
37   /** Maps property keys to OptionInfo. */
38   Hashtable JavaDoc infoTable;
39
40   /** Create a new option and enters it in this table.
41    * A duplicate option throws a RuntimeException.
42    * @param key the options name (key).
43    * @param kind type and other flag bits of the option.
44    * @param documentation a String describing what the option does. */

45   public void add(String JavaDoc key, int kind, String JavaDoc documentation)
46   {
47     if (infoTable == null)
48       infoTable = new Hashtable JavaDoc();
49     else if (infoTable.get(key) != null)
50       throw new RuntimeException JavaDoc("duplicate option key: "+key);
51     OptionInfo info = new OptionInfo();
52     info.key = key;
53     info.kind = kind;
54     info.documentation = documentation;
55     if (first == null)
56       first = info;
57     else
58       last.next = info;
59     last = info;
60     infoTable.put(key, info);
61   }
62
63   static Object JavaDoc valueOf (OptionInfo info, String JavaDoc argument)
64   {
65     if ((info.kind & BOOLEAN_OPTION) != 0)
66       {
67         if (argument == null
68         || argument.equals("1")
69         || argument.equals("on")
70         || argument.equals("yes")
71         || argument.equals("true"))
72       return Boolean.TRUE;
73     if (argument.equals("0")
74         || argument.equals("off")
75         || argument.equals("no")
76         || argument.equals("false"))
77       return Boolean.FALSE;
78     return null;
79       }
80     return argument;
81   }
82
83   private void error(String JavaDoc message, SourceMessages messages)
84   {
85     if (messages == null)
86       throw new RuntimeException JavaDoc(message);
87     else
88       messages.error('e', message);
89   }
90
91   /** Set the value of a named option. */
92   public void set (String JavaDoc key, Object JavaDoc value)
93   {
94     set(key, value, null);
95   }
96
97   /** Set the value of a named option. */
98   public void set (String JavaDoc key, Object JavaDoc value, SourceMessages messages)
99   {
100     OptionInfo info = getInfo(key);
101     if (info == null)
102       {
103     error("invalid option key: "+key, messages);
104     return;
105       }
106     if ((info.kind & BOOLEAN_OPTION) != 0)
107       {
108     if (value instanceof String JavaDoc)
109       value = valueOf(info, (String JavaDoc) value);
110     if (! (value instanceof Boolean JavaDoc))
111       {
112         error("value for option "+key
113           +" must be boolean or yes/no/true/false/on/off/1/0",
114           messages);
115         return;
116       }
117       }
118     else if (value == null)
119       value = "";
120     if (valueTable == null)
121       valueTable = new Hashtable JavaDoc();
122     valueTable.put(key, value);
123   }
124
125   /** Reset the value of a named option. */
126   public void reset (String JavaDoc key, Object JavaDoc oldValue)
127   {
128     if (valueTable == null)
129       valueTable = new Hashtable JavaDoc();
130     if (oldValue == null)
131       valueTable.remove(key);
132     else
133       valueTable.put(key, oldValue);
134   }
135
136   public static final String JavaDoc UNKNOWN = "unknown option name";
137
138   /** Set the value of the key to the argument, appropriate parsed.
139    * return null on success or a String error message.
140    * If the option key is invalid, return UNKNOWN. */

141   public String JavaDoc set (String JavaDoc key, String JavaDoc argument)
142   {
143     OptionInfo info = getInfo(key);
144     if (info == null)
145       return UNKNOWN;
146     Object JavaDoc value = valueOf(info, argument);
147     if (value == null)
148       {
149     if ((info.kind & BOOLEAN_OPTION) != 0)
150       return "value of option "+key+" must be yes/no/true/false/on/off/1/0";
151       }
152     if (valueTable == null)
153       valueTable = new Hashtable JavaDoc();
154     valueTable.put(key, value);
155     return null;
156   }
157
158   public OptionInfo getInfo (String JavaDoc key)
159   {
160     Object JavaDoc info = infoTable == null ? null : infoTable.get(key);
161     if (info == null && previous != null)
162       info = previous.getInfo(key);
163     return (OptionInfo) info;
164   }
165
166   /** Get the value for the option.
167    * Throws an except if there is no option by that name,
168    * Returns defaultValue if there is such an option, but it
169    * hasn't been set. */

170   public Object JavaDoc get (String JavaDoc key, Object JavaDoc defaultValue)
171   {
172     Object JavaDoc val = valueTable == null ? null : valueTable.get(key);
173     if (val != null)
174       return val;
175     if (previous != null)
176       return previous.get(key, defaultValue);
177     OptionInfo info = getInfo(key);
178     if (info == null)
179       throw new RuntimeException JavaDoc("invalid option key: "+key);
180     return defaultValue;
181   }
182
183   /** Get current option value.
184    * Only look in local table, not in inherited Options.
185    * Return null if there is no binding (even when get would
186    * throw an except on an unknonw option).
187    */

188   public Object JavaDoc getLocal (String JavaDoc key)
189   {
190     return valueTable == null ? null : valueTable.get(key);
191   }
192
193   public boolean getBoolean (String JavaDoc key)
194   {
195     return ((Boolean JavaDoc) get (key, Boolean.FALSE)).booleanValue();
196   }
197
198   public boolean getBoolean (String JavaDoc key, boolean defaultValue)
199   {
200     Boolean JavaDoc defaultObject = defaultValue ? Boolean.TRUE : Boolean.FALSE;
201     return ((Boolean JavaDoc) get (key, defaultObject)).booleanValue();
202   }
203
204   /** Set a list of options, remember the old value.
205    * @param options is vector of triples, echo of which is consisting of:
206    * a String option key;
207    * an entry whose valus is ignores and is used to store the old value; and
208    * a new value for the options.
209    */

210   public void pushOptionValues (Vector JavaDoc options)
211   {
212     int len = options.size();
213     for (int i = 0; i < len; )
214       {
215     String JavaDoc key = (String JavaDoc) options.elementAt(i++);
216     Object JavaDoc newValue = options.elementAt(i);
217     options.setElementAt(newValue, i++);
218     set(key, options.elementAt(i++));
219       }
220   }
221
222   /** Restore a list of options, as set by pushOptionValues
223    */

224   public void popOptionValues (Vector JavaDoc options)
225   {
226     for (int i = options.size(); (i -= 3) >= 0; )
227       {
228     String JavaDoc key = (String JavaDoc) options.elementAt(i);
229     Object JavaDoc oldValue = options.elementAt(i+1);
230     options.setElementAt(null, i+1);
231     reset(key, oldValue);
232       }
233   }
234
235   /** Return the list of option keys.
236    */

237   public Vector JavaDoc keys ()
238   {
239     Vector JavaDoc allKeys = new Vector JavaDoc();
240     for (Options options = this; options != null; options = options.previous)
241       {
242     if (options.infoTable != null)
243       {
244         Enumeration JavaDoc e = options.infoTable.keys();
245         while (e.hasMoreElements())
246           {
247         Object JavaDoc k = e.nextElement();
248         if (! allKeys.contains(k))
249           allKeys.add(k);
250           }
251       }
252       }
253     return allKeys;
254   }
255
256   public String JavaDoc getDoc(String JavaDoc key)
257   {
258     OptionInfo info = getInfo(key);
259     if (key == null)
260       return null;
261     return info.documentation;
262   }
263   
264 }
265
266 final class OptionInfo
267 {
268   OptionInfo next;
269   String JavaDoc key;
270   int kind;
271   String JavaDoc documentation;
272 }
273
Popular Tags