View Javadoc
1 // $Id: Dictionary.java,v 1.1.1.1 2002/09/29 17:26:06 powerpete Exp $ 2 package de.jface.util; 3 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.io.OutputStream; 7 8 import java.util.Iterator; 9 import java.util.StringTokenizer; 10 11 import org.jdom.Document; 12 import org.jdom.Element; 13 import org.jdom.JDOMException; 14 import org.jdom.input.SAXBuilder; 15 import org.jdom.output.XMLOutputter; 16 17 /*** 18 * Class <code>Dictionary</code> 19 * of project RemotePostIt. 20 * 21 * @author Moritz Petersen 22 * @version $Revision: 1.1.1.1 $ 23 */ 24 public class Dictionary 25 { 26 private final Key root; 27 28 public Dictionary(String name) 29 { 30 root = new Key(name); 31 } 32 33 public static Dictionary create(InputStream in) 34 throws JDOMException 35 { 36 Document doc = new SAXBuilder().build(in); 37 Element rootElement = doc.getRootElement(); 38 Dictionary dictionary = new Dictionary(rootElement.getName()); 39 40 dictionary.createFromElement(dictionary.getRootKey(), rootElement); 41 42 return dictionary; 43 } 44 45 /*** 46 * Recursivly fills the dictionary with values, taken from the 47 * element and the root elements. The key structure will be created 48 * - using this method - on the fly. 49 */ 50 private void createFromElement(Key key, Element element) 51 { 52 put(key, element.getText()); 53 54 for (Iterator i = element.getChildren().iterator(); i.hasNext();) 55 { 56 Element e = (Element) i.next(); 57 Key k = key.getKey(e.getName()); 58 59 createFromElement(k, e); 60 } 61 } 62 63 public void write(OutputStream out) 64 throws JDOMException, IOException 65 { 66 Element rootElement = new Element(root.getName()); 67 68 createFromKey(rootElement, root); 69 70 Document doc = new Document(rootElement); 71 XMLOutputter outputter = new XMLOutputter(); 72 73 outputter.output(doc, out); 74 } 75 76 /*** 77 * This is the reverse method for {@link createFromElement}. 78 */ 79 private void createFromKey(Element element, Key key) 80 { 81 element.setText(get(key)); 82 83 for (Iterator i = key.getChildren().iterator(); i.hasNext();) 84 { 85 Key k = (Key) i.next(); 86 Element e = new Element(k.getName()); 87 88 element.addContent(e); 89 createFromKey(e, k); 90 } 91 } 92 93 public String get(String path) 94 { 95 return get(getKey(path)); 96 } 97 98 private Key getKey(String path) 99 { 100 Key key = root; 101 102 // this could be more efficiently if cached... 103 // TODO: evaluate efficiency 104 for (StringTokenizer stok = new StringTokenizer(path, "."); 105 stok.hasMoreTokens();) 106 { 107 key = key.getKey(stok.nextToken()); 108 } 109 110 return key; 111 } 112 113 public String get(Key key) 114 { 115 return (String) key.getValue(); 116 } 117 118 public void put(String path, String value) 119 { 120 put(getKey(path), value); 121 } 122 123 public void put(Key key, String value) 124 { 125 key.setValue(value); 126 } 127 128 public boolean remove(String path) 129 { 130 return remove(getKey(path)); 131 } 132 133 public boolean remove(Key key) 134 { 135 return key.remove(); 136 } 137 138 public boolean contains(Key key) 139 { 140 return root.contains(key); 141 } 142 143 public Key getRootKey() 144 { 145 return root; 146 } 147 } 148 149 // ------1---------2---------3---------4---------5---------6---------7---------8

This page was automatically generated by Maven