| 1 4 package gnu.text; 5 import java.util.Hashtable ; 6 import java.util.Vector ; 7 import java.util.Enumeration ; 8 9 11 12 public class Options 13 { 14 15 public static final int BOOLEAN_OPTION = 1; 16 17 public static final int STRING_OPTION = 2; 18 19 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 35 Hashtable valueTable; 36 37 38 Hashtable infoTable; 39 40 45 public void add(String key, int kind, String documentation) 46 { 47 if (infoTable == null) 48 infoTable = new Hashtable (); 49 else if (infoTable.get(key) != null) 50 throw new RuntimeException ("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 valueOf (OptionInfo info, String 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 message, SourceMessages messages) 84 { 85 if (messages == null) 86 throw new RuntimeException (message); 87 else 88 messages.error('e', message); 89 } 90 91 92 public void set (String key, Object value) 93 { 94 set(key, value, null); 95 } 96 97 98 public void set (String key, Object 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 ) 109 value = valueOf(info, (String ) value); 110 if (! (value instanceof Boolean )) 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 (); 122 valueTable.put(key, value); 123 } 124 125 126 public void reset (String key, Object oldValue) 127 { 128 if (valueTable == null) 129 valueTable = new Hashtable (); 130 if (oldValue == null) 131 valueTable.remove(key); 132 else 133 valueTable.put(key, oldValue); 134 } 135 136 public static final String UNKNOWN = "unknown option name"; 137 138 141 public String set (String key, String argument) 142 { 143 OptionInfo info = getInfo(key); 144 if (info == null) 145 return UNKNOWN; 146 Object 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 (); 154 valueTable.put(key, value); 155 return null; 156 } 157 158 public OptionInfo getInfo (String key) 159 { 160 Object 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 170 public Object get (String key, Object defaultValue) 171 { 172 Object 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 ("invalid option key: "+key); 180 return defaultValue; 181 } 182 183 188 public Object getLocal (String key) 189 { 190 return valueTable == null ? null : valueTable.get(key); 191 } 192 193 public boolean getBoolean (String key) 194 { 195 return ((Boolean ) get (key, Boolean.FALSE)).booleanValue(); 196 } 197 198 public boolean getBoolean (String key, boolean defaultValue) 199 { 200 Boolean defaultObject = defaultValue ? Boolean.TRUE : Boolean.FALSE; 201 return ((Boolean ) get (key, defaultObject)).booleanValue(); 202 } 203 204 210 public void pushOptionValues (Vector options) 211 { 212 int len = options.size(); 213 for (int i = 0; i < len; ) 214 { 215 String key = (String ) options.elementAt(i++); 216 Object newValue = options.elementAt(i); 217 options.setElementAt(newValue, i++); 218 set(key, options.elementAt(i++)); 219 } 220 } 221 222 224 public void popOptionValues (Vector options) 225 { 226 for (int i = options.size(); (i -= 3) >= 0; ) 227 { 228 String key = (String ) options.elementAt(i); 229 Object oldValue = options.elementAt(i+1); 230 options.setElementAt(null, i+1); 231 reset(key, oldValue); 232 } 233 } 234 235 237 public Vector keys () 238 { 239 Vector allKeys = new Vector (); 240 for (Options options = this; options != null; options = options.previous) 241 { 242 if (options.infoTable != null) 243 { 244 Enumeration e = options.infoTable.keys(); 245 while (e.hasMoreElements()) 246 { 247 Object k = e.nextElement(); 248 if (! allKeys.contains(k)) 249 allKeys.add(k); 250 } 251 } 252 } 253 return allKeys; 254 } 255 256 public String getDoc(String 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 key; 270 int kind; 271 String documentation; 272 } 273 | Popular Tags |