1 2 24 25 26 27 28 29 package com.lutris.util; 30 31 import java.lang.reflect.Array ; 32 import java.lang.reflect.Method ; 33 import java.util.Enumeration ; 34 import java.util.Hashtable ; 35 import java.util.Vector ; 36 37 52 public class KeywordValueTable implements java.io.Serializable { 53 private Hashtable hashTable; 55 58 public KeywordValueTable() { 59 hashTable = new Hashtable (); 60 } 61 62 63 76 private synchronized KeywordValueTable findSection(String [] keywordPath, 77 boolean create, 78 int pathIdx) 79 throws KeywordValueException { 80 83 if (pathIdx == keywordPath.length - 1) { 84 return this; 85 } 86 87 90 Object value = hashTable.get(keywordPath [pathIdx]); 91 if (value != null) { 92 96 if (!(value instanceof KeywordValueTable)) { 97 if (!create) { 98 String msg = "keyword specifies a non-leaf component " + 99 "that is not a KeywordValueTable: " + 100 KeywordParser.join (keywordPath) + 101 " (component #" + pathIdx + ")"; 102 throw new KeywordValueException(msg); 103 } 104 value = newSection(); 105 hashTable.put(keywordPath [pathIdx], value); 106 } 107 } else { 108 111 if (!create) { 112 return null; 113 } 114 value = newSection(); 115 hashTable.put(keywordPath [pathIdx], value); 116 } 117 return ((KeywordValueTable) value).findSection(keywordPath, 118 create, 119 pathIdx + 1); 120 } 121 122 131 protected KeywordValueTable newSection() { 132 return new KeywordValueTable(); 133 } 134 135 144 public synchronized Object get(String keyword) 145 throws KeywordValueException { 146 147 String [] keywordPath = KeywordParser.parse(keyword); 148 KeywordValueTable section = findSection(keywordPath, 149 false, 0); 151 if (section == null) { 152 return null; 153 } 154 return section.hashTable.get(keywordPath [keywordPath.length - 1]); 155 } 156 157 169 public synchronized Object get(String keyword, 170 Object defaultValue) 171 throws KeywordValueException { 172 173 Object value; 174 String [] keywordPath = KeywordParser.parse (keyword); 175 KeywordValueTable section = findSection(keywordPath, 176 false, 0); 178 if (section == null) { 179 value = defaultValue; 180 } else { 181 value = section.hashTable.get(keywordPath [keywordPath.length - 1]); 182 if (value == null) { 183 value = defaultValue; 184 } 185 } 186 return value; 187 } 188 189 199 public synchronized String getString(String keyword) 200 throws KeywordValueException { 201 202 Object value = get(keyword); 203 if (value == null) { 204 return null; 205 } 206 return value.toString(); 207 } 208 209 220 public synchronized String getString(String keyword, 221 String defaultValue) 222 throws KeywordValueException { 223 224 Object value = get(keyword); 225 if (value == null) { 226 return defaultValue; 227 } 228 return value.toString(); 229 } 230 231 243 public synchronized KeywordValueTable getSection(String keyword) 244 throws KeywordValueException { 245 246 Object value = get(keyword); 247 if (value == null) { 248 return null; 249 } 250 if (!(value instanceof KeywordValueTable)) { 251 String msg = "Value of field \"" + keyword + 252 " is not a KeywordValueTable; it is " + 253 value.getClass().getName (); 254 throw new KeywordValueException(msg); 255 } 256 return (KeywordValueTable)value; 257 } 258 259 272 public synchronized void set(String keyword, 273 Object value) 274 throws KeywordValueException { 275 276 String [] keywordPath = KeywordParser.parse(keyword); 277 KeywordValueTable section = findSection(keywordPath, 278 true, 0); 280 section.hashTable.put (keywordPath[keywordPath.length - 1], 281 value); 282 } 283 284 285 303 public synchronized void setDefault(String keyword, 304 Object defaultValue) 305 throws KeywordValueException 306 { 307 if (!containsKey(keyword)) 308 set(keyword, defaultValue); 309 } 310 311 321 public synchronized boolean containsKey (String keyword) 322 throws KeywordValueException { 323 324 String [] keywordPath = KeywordParser.parse (keyword); 325 KeywordValueTable section = findSection(keywordPath, 326 false, 0); 328 if (section == null) { 329 return false; 330 } 331 return section.hashTable.containsKey(keywordPath [keywordPath.length - 1]); 332 } 333 334 340 public synchronized String [] keys() { 341 Enumeration keyEnum = hashTable.keys(); 342 343 346 Vector keyList = new Vector (); 347 while (keyEnum.hasMoreElements()) { 348 keyList.addElement (keyEnum.nextElement()); 349 } 350 351 String [] keyStrings = new String [keyList.size()]; 352 for (int idx = 0; idx < keyList.size (); idx++) { 353 keyStrings [idx] = (String ) keyList.elementAt (idx); 354 } 355 return keyStrings; 356 } 357 358 364 public synchronized String [] leafKeys() { 365 Enumeration keyEnum = hashTable.keys (); 366 367 371 Vector keyList = new Vector (); 372 while (keyEnum.hasMoreElements()) { 373 String key = (String )keyEnum.nextElement(); 374 Object value = hashTable.get(key); 375 376 if (value instanceof KeywordValueTable) { 377 String subKeys [] = ((KeywordValueTable) value).leafKeys (); 378 for (int idx = 0; idx < subKeys.length; idx++) { 379 keyList.addElement(KeywordParser.concat(key, 380 subKeys[idx])); 381 } 382 } else { 383 keyList.addElement(key); 384 } 385 } 386 387 String [] keyStrings = new String [keyList.size()]; 388 for (int idx = 0; idx < keyList.size(); idx++) { 389 keyStrings [idx] = (String )keyList.elementAt(idx); 390 } 391 return keyStrings; 392 } 393 394 402 public synchronized void remove(String keyword) 403 throws KeywordValueException { 404 405 String [] keywordPath = KeywordParser.parse(keyword); 406 KeywordValueTable section = findSection(keywordPath, 407 false, 0); 409 if (section != null) { 410 section.hashTable.remove(keywordPath[keywordPath.length - 1]); 411 } 412 } 413 414 419 public synchronized String toString () { 420 return hashTable.toString(); 421 } 422 423 428 public synchronized String toHtml() { 429 430 StringBuffer html = new StringBuffer (); 431 Enumeration keyEnum = hashTable.keys(); 432 433 html.append ("<UL>\n"); 434 Vector keyList = new Vector (); 435 if (!keyEnum.hasMoreElements()) 436 return ""; while (keyEnum.hasMoreElements()) { 438 String key = (String )keyEnum.nextElement(); 439 Object value = hashTable.get(key); 440 441 html.append("<LI> <TT>"); 442 html.append(key); 443 html.append(": </TT>"); 444 html.append(formatFieldAsHtml(value)); 445 html.append ("\n"); 446 } 447 html.append ("</UL>\n"); 448 449 return html.toString(); 450 } 451 452 459 private String formatArrayAsHtml(Object arrayObj) { 460 461 StringBuffer html = new StringBuffer (); 462 463 html.append("<OL START=0>\n"); 464 for (int idx = 0; idx < Array.getLength(arrayObj); idx++) { 465 html.append("<LI>"); 466 html.append(formatFieldAsHtml(Array.get(arrayObj, idx))); 467 html.append("\n"); 468 } 469 html.append("</OL>\n"); 470 471 return html.toString(); 472 } 473 474 481 private String formatObjectAsHtml(Object obj) { 482 String html; 483 484 if (obj instanceof String ) { 485 html = obj.toString(); 486 } else if (obj instanceof Integer ) { 487 html = "<I><FONT SIZE=-1>(Integer)</FONT></I>" + obj.toString(); 488 } else if (obj instanceof Boolean ) { 489 html = "<I><FONT SIZE=-1>(Boolean)</FONT></I>" + obj.toString(); 490 } else if (obj instanceof Double ) { 491 html = "<I><FONT SIZE=-1>(Double)</FONT></I>" + obj.toString(); 492 } else if (obj instanceof Long ) { 493 html = "<I><FONT SIZE=-1>(Long)</FONT></I>" + obj.toString(); 494 } else if (obj instanceof Short ) { 495 html = "<I><FONT SIZE=-1>(Short)</FONT></I>" + obj.toString(); 496 } else if (obj instanceof Float ) { 497 html = "<I><FONT SIZE=-1>(Float)</FONT></I>" + obj.toString(); 498 } else if (obj instanceof Character ) { 499 html = "<I><FONT SIZE=-1>(Character)</FONT></I>" + obj.toString(); 500 } else { 501 try { 504 Class objClass = obj.getClass(); 505 Class [] params = null; 506 Method toHtmlMethod = objClass.getMethod("toHtml", params); 507 Object [] args = null; 508 html = new String ("<I><FONT SIZE=-1>(Object)" + 509 toHtmlMethod.invoke(obj, args) + 510 "</FONT></I>"); 511 } catch (Exception e) { 512 html = "<I><FONT SIZE=-1>(Object)</FONT></I>"; 513 } 514 } 515 516 return html; 517 } 518 519 526 private String formatFieldAsHtml(Object fieldObj) { 527 528 String html; 529 530 if (fieldObj instanceof KeywordValueTable) { 531 html = ((KeywordValueTable)fieldObj).toHtml(); 532 } else if (fieldObj.getClass().isArray()) { 533 html = formatArrayAsHtml(fieldObj); 534 } else { 535 html = formatObjectAsHtml(fieldObj); 536 } 537 538 return html; 539 } 540 } 541 | Popular Tags |