1 18 19 package org.apache.batik.apps.svgbrowser; 20 21 import java.io.BufferedReader ; 22 import java.io.BufferedWriter ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.io.InputStreamReader ; 26 import java.io.OutputStream ; 27 import java.io.OutputStreamWriter ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.Map ; 31 import java.util.Properties ; 32 33 import org.apache.batik.dom.GenericDOMImplementation; 34 import org.apache.batik.dom.util.DOMUtilities; 35 import org.apache.batik.dom.util.DocumentFactory; 36 import org.apache.batik.dom.util.SAXDocumentFactory; 37 import org.apache.batik.util.PreferenceManager; 38 import org.apache.batik.util.XMLResourceDescriptor; 39 import org.w3c.dom.Document ; 40 import org.w3c.dom.Element ; 41 import org.w3c.dom.Node ; 42 43 50 public class XMLPreferenceManager extends PreferenceManager { 51 52 55 protected String xmlParserClassName; 56 57 60 public static final String PREFERENCE_ENCODING = "8859_1"; 61 62 66 public XMLPreferenceManager(String prefFileName){ 67 this(prefFileName, null, 68 XMLResourceDescriptor.getXMLParserClassName()); 69 } 70 71 77 public XMLPreferenceManager(String prefFileName, 78 Map defaults){ 79 this(prefFileName, defaults, 80 XMLResourceDescriptor.getXMLParserClassName()); 81 } 82 83 88 public XMLPreferenceManager(String prefFileName, String parser) { 89 this(prefFileName, null, parser); 90 } 91 92 100 public XMLPreferenceManager(String prefFileName, Map defaults, String parser) { 101 super(prefFileName, defaults); 102 internal = new XMLProperties(); 103 xmlParserClassName = parser; 104 } 105 106 109 protected class XMLProperties extends Properties { 110 111 115 public synchronized void load(InputStream is) throws IOException { 116 BufferedReader r; 117 r = new BufferedReader (new InputStreamReader (is, PREFERENCE_ENCODING)); 118 DocumentFactory df = new SAXDocumentFactory 119 (GenericDOMImplementation.getDOMImplementation(), 120 xmlParserClassName); 121 Document doc = df.createDocument("http://xml.apache.org/batik/preferences", 122 "preferences", 123 null, 124 r); 125 Element elt = doc.getDocumentElement(); 126 for (Node n = elt.getFirstChild(); n != null; n = n.getNextSibling()) { 127 if (n.getNodeType() == Node.ELEMENT_NODE) { 128 if (n.getNodeName().equals("property")) { 129 String name = ((Element )n).getAttributeNS(null, "name"); 130 131 StringBuffer cont = new StringBuffer (); 132 for (Node c = n.getFirstChild(); 133 c != null; 134 c = c.getNextSibling()) { 135 if (c.getNodeType() == Node.TEXT_NODE) { 136 cont.append(c.getNodeValue()); 137 } else { 138 break; 139 } 140 } 141 String val = cont.toString(); 142 put(name, val); 143 } 144 } 145 } 146 } 147 148 155 public synchronized void store(OutputStream os, String header) 156 throws IOException { 157 BufferedWriter w; 158 w = new BufferedWriter (new OutputStreamWriter (os, PREFERENCE_ENCODING)); 159 160 Map m = new HashMap (); 161 enumerate(m); 162 163 w.write("<preferences xmlns=\"http://xml.apache.org/batik/preferences\">\n"); 164 165 Iterator it = m.keySet().iterator(); 166 while (it.hasNext()) { 167 String n = (String )it.next(); 168 String v = (String )m.get(n); 169 170 w.write("<property name=\"" + n + "\">"); 171 w.write(DOMUtilities.contentToString(v)); 172 w.write("</property>\n"); 173 } 174 175 w.write("</preferences>\n"); 176 w.flush(); 177 } 178 179 183 private synchronized void enumerate(Map m) { 184 if (defaults != null) { 185 Iterator it = m.keySet().iterator(); 186 while (it.hasNext()) { 187 Object k = it.next(); 188 m.put(k, defaults.get(k)); 189 } 190 } 191 Iterator it = keySet().iterator(); 192 while (it.hasNext()) { 193 Object k = it.next(); 194 m.put(k, get(k)); 195 } 196 } 197 198 } 199 } 200 | Popular Tags |