1 11 package org.eclipse.jface.dialogs; 12 13 import java.io.BufferedReader ; 14 import java.io.FileInputStream ; 15 import java.io.FileOutputStream ; 16 import java.io.IOException ; 17 import java.io.InputStreamReader ; 18 import java.io.OutputStream ; 19 import java.io.OutputStreamWriter ; 20 import java.io.PrintWriter ; 21 import java.io.Reader ; 22 import java.io.UnsupportedEncodingException ; 23 import java.io.Writer ; 24 import java.util.ArrayList ; 25 import java.util.Collection ; 26 import java.util.Collections ; 27 import java.util.Enumeration ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Map ; 32 33 import javax.xml.parsers.DocumentBuilder ; 34 import javax.xml.parsers.DocumentBuilderFactory ; 35 import javax.xml.parsers.ParserConfigurationException ; 36 37 import org.w3c.dom.Document ; 38 import org.w3c.dom.Element ; 39 import org.w3c.dom.Node ; 40 import org.w3c.dom.NodeList ; 41 import org.xml.sax.InputSource ; 42 import org.xml.sax.SAXException ; 43 44 69 70 public class DialogSettings implements IDialogSettings { 71 private String name; 73 74 76 private Map sections; 77 78 80 private Map items; 81 82 private Map arrayItems; 84 85 private static final String TAG_SECTION = "section"; 87 private static final String TAG_NAME = "name"; 89 private static final String TAG_KEY = "key"; 91 private static final String TAG_VALUE = "value"; 93 private static final String TAG_LIST = "list"; 95 private static final String TAG_ITEM = "item"; 97 105 public DialogSettings(String sectionName) { 106 name = sectionName; 107 items = new HashMap (); 108 arrayItems = new HashMap (); 109 sections = new HashMap (); 110 } 111 112 115 public IDialogSettings addNewSection(String sectionName) { 116 DialogSettings section = new DialogSettings(sectionName); 117 addSection(section); 118 return section; 119 } 120 121 124 public void addSection(IDialogSettings section) { 125 sections.put(section.getName(), section); 126 } 127 128 131 public String get(String key) { 132 return (String ) items.get(key); 133 } 134 135 138 public String [] getArray(String key) { 139 return (String []) arrayItems.get(key); 140 } 141 142 145 public boolean getBoolean(String key) { 146 return Boolean.valueOf((String ) items.get(key)).booleanValue(); 147 } 148 149 152 public double getDouble(String key) throws NumberFormatException { 153 String setting = (String ) items.get(key); 154 if (setting == null) { 155 throw new NumberFormatException ( 156 "There is no setting associated with the key \"" + key + "\""); } 158 159 return new Double (setting).doubleValue(); 160 } 161 162 165 public float getFloat(String key) throws NumberFormatException { 166 String setting = (String ) items.get(key); 167 if (setting == null) { 168 throw new NumberFormatException ( 169 "There is no setting associated with the key \"" + key + "\""); } 171 172 return new Float (setting).floatValue(); 173 } 174 175 178 public int getInt(String key) throws NumberFormatException { 179 String setting = (String ) items.get(key); 180 if (setting == null) { 181 throw new NumberFormatException ( 184 "There is no setting associated with the key \"" + key + "\""); } 186 187 return new Integer (setting).intValue(); 188 } 189 190 193 public long getLong(String key) throws NumberFormatException { 194 String setting = (String ) items.get(key); 195 if (setting == null) { 196 throw new NumberFormatException ( 199 "There is no setting associated with the key \"" + key + "\""); } 201 202 return new Long (setting).longValue(); 203 } 204 205 208 public String getName() { 209 return name; 210 } 211 212 215 public IDialogSettings getSection(String sectionName) { 216 return (IDialogSettings) sections.get(sectionName); 217 } 218 219 222 public IDialogSettings[] getSections() { 223 Collection values = sections.values(); 224 DialogSettings[] result = new DialogSettings[values.size()]; 225 values.toArray(result); 226 return result; 227 } 228 229 232 public void load(Reader r) { 233 Document document = null; 234 try { 235 DocumentBuilder parser = DocumentBuilderFactory.newInstance() 236 .newDocumentBuilder(); 237 document = parser.parse(new InputSource (r)); 239 240 Node root = document.getFirstChild(); 242 while (root.getNodeType() == Node.COMMENT_NODE) { 243 document.removeChild(root); 244 root = document.getFirstChild(); 245 } 246 load(document, (Element ) root); 247 } catch (ParserConfigurationException e) { 248 } catch (IOException e) { 250 } catch (SAXException e) { 252 } 254 } 255 256 259 public void load(String fileName) throws IOException { 260 FileInputStream stream = new FileInputStream (fileName); 261 BufferedReader reader = new BufferedReader (new InputStreamReader ( 262 stream, "utf-8")); load(reader); 264 reader.close(); 265 } 266 267 270 private void load(Document document, Element root) { 271 name = root.getAttribute(TAG_NAME); 272 NodeList l = root.getElementsByTagName(TAG_ITEM); 273 for (int i = 0; i < l.getLength(); i++) { 274 Node n = l.item(i); 275 if (root == n.getParentNode()) { 276 String key = ((Element ) l.item(i)).getAttribute(TAG_KEY); 277 String value = ((Element ) l.item(i)).getAttribute(TAG_VALUE); 278 items.put(key, value); 279 } 280 } 281 l = root.getElementsByTagName(TAG_LIST); 282 for (int i = 0; i < l.getLength(); i++) { 283 Node n = l.item(i); 284 if (root == n.getParentNode()) { 285 Element child = (Element ) l.item(i); 286 String key = child.getAttribute(TAG_KEY); 287 NodeList list = child.getElementsByTagName(TAG_ITEM); 288 List valueList = new ArrayList (); 289 for (int j = 0; j < list.getLength(); j++) { 290 Element node = (Element ) list.item(j); 291 if (child == node.getParentNode()) { 292 valueList.add(node.getAttribute(TAG_VALUE)); 293 } 294 } 295 String [] value = new String [valueList.size()]; 296 valueList.toArray(value); 297 arrayItems.put(key, value); 298 } 299 } 300 l = root.getElementsByTagName(TAG_SECTION); 301 for (int i = 0; i < l.getLength(); i++) { 302 Node n = l.item(i); 303 if (root == n.getParentNode()) { 304 DialogSettings s = new DialogSettings("NoName"); s.load(document, (Element ) n); 306 addSection(s); 307 } 308 } 309 } 310 311 314 public void put(String key, String [] value) { 315 arrayItems.put(key, value); 316 } 317 318 321 public void put(String key, double value) { 322 put(key, String.valueOf(value)); 323 } 324 325 328 public void put(String key, float value) { 329 put(key, String.valueOf(value)); 330 } 331 332 335 public void put(String key, int value) { 336 put(key, String.valueOf(value)); 337 } 338 339 342 public void put(String key, long value) { 343 put(key, String.valueOf(value)); 344 } 345 346 349 public void put(String key, String value) { 350 items.put(key, value); 351 } 352 353 356 public void put(String key, boolean value) { 357 put(key, String.valueOf(value)); 358 } 359 360 363 public void save(Writer writer) throws IOException { 364 save(new XMLWriter(writer)); 365 } 366 367 370 public void save(String fileName) throws IOException { 371 FileOutputStream stream = new FileOutputStream (fileName); 372 XMLWriter writer = new XMLWriter(stream); 373 save(writer); 374 writer.close(); 375 } 376 377 380 private void save(XMLWriter out) { 381 HashMap attributes = new HashMap (2); 382 attributes.put(TAG_NAME, name == null ? "" : name); out.startTag(TAG_SECTION, attributes); 384 attributes.clear(); 385 386 for (Iterator i = items.keySet().iterator(); i.hasNext();) { 387 String key = (String ) i.next(); 388 attributes.put(TAG_KEY, key == null ? "" : key); String string = (String ) items.get(key); 390 attributes.put(TAG_VALUE, string == null ? "" : string); out.printTag(TAG_ITEM, attributes, true); 392 } 393 394 attributes.clear(); 395 for (Iterator i = arrayItems.keySet().iterator(); i.hasNext();) { 396 String key = (String ) i.next(); 397 attributes.put(TAG_KEY, key == null ? "" : key); out.startTag(TAG_LIST, attributes); 399 String [] value = (String []) arrayItems.get(key); 400 attributes.clear(); 401 if (value != null) { 402 for (int index = 0; index < value.length; index++) { 403 String string = value[index]; 404 attributes.put(TAG_VALUE, string == null ? "" : string); out.printTag(TAG_ITEM, attributes, true); 406 } 407 } 408 out.endTag(TAG_LIST); 409 attributes.clear(); 410 } 411 for (Iterator i = sections.values().iterator(); i.hasNext();) { 412 ((DialogSettings) i.next()).save(out); 413 } 414 out.endTag(TAG_SECTION); 415 } 416 417 421 private static class XMLWriter extends PrintWriter { 422 423 protected int tab; 424 425 426 protected static final String XML_VERSION = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; 428 433 public XMLWriter(OutputStream output) throws UnsupportedEncodingException { 434 super(new OutputStreamWriter (output, "UTF8")); tab = 0; 436 println(XML_VERSION); 437 } 438 439 443 public XMLWriter(Writer output) { 444 super(output); 445 tab = 0; 446 println(XML_VERSION); 447 } 448 449 453 public void endTag(String name) { 454 tab--; 455 printTag("/" + name, null, false); } 457 458 private void printTabulation() { 459 for (int i = 0; i < tab; i++) { 460 super.print('\t'); 461 } 462 } 463 464 470 public void printTag(String name, HashMap parameters, boolean close) { 471 printTag(name, parameters, true, true, close); 472 } 473 474 private void printTag(String name, HashMap parameters, boolean shouldTab, boolean newLine, boolean close) { 475 StringBuffer sb = new StringBuffer (); 476 sb.append('<'); 477 sb.append(name); 478 if (parameters != null) { 479 for (Enumeration e = Collections.enumeration(parameters.keySet()); e.hasMoreElements();) { 480 sb.append(" "); String key = (String ) e.nextElement(); 482 sb.append(key); 483 sb.append("=\""); sb.append(getEscaped(String.valueOf(parameters.get(key)))); 485 sb.append("\""); } 487 } 488 if (close) { 489 sb.append('/'); 490 } 491 sb.append('>'); 492 if (shouldTab) { 493 printTabulation(); 494 } 495 if (newLine) { 496 println(sb.toString()); 497 } else { 498 print(sb.toString()); 499 } 500 } 501 502 507 public void startTag(String name, HashMap parameters) { 508 startTag(name, parameters, true); 509 tab++; 510 } 511 512 private void startTag(String name, HashMap parameters, boolean newLine) { 513 printTag(name, parameters, true, newLine, false); 514 } 515 516 private static void appendEscapedChar(StringBuffer buffer, char c) { 517 String replacement = getReplacement(c); 518 if (replacement != null) { 519 buffer.append('&'); 520 buffer.append(replacement); 521 buffer.append(';'); 522 } else { 523 buffer.append(c); 524 } 525 } 526 527 private static String getEscaped(String s) { 528 StringBuffer result = new StringBuffer (s.length() + 10); 529 for (int i = 0; i < s.length(); ++i) { 530 appendEscapedChar(result, s.charAt(i)); 531 } 532 return result.toString(); 533 } 534 535 private static String getReplacement(char c) { 536 switch (c) { 541 case '<' : 542 return "lt"; case '>' : 544 return "gt"; case '"' : 546 return "quot"; case '\'' : 548 return "apos"; case '&' : 550 return "amp"; case '\r': 552 return "#x0D"; case '\n': 554 return "#x0A"; case '\u0009': 556 return "#x09"; } 558 return null; 559 } 560 } 561 } 562 | Popular Tags |