001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019:
020: package org.apache.batik.apps.svgbrowser;
021:
022: import java.io.BufferedReader;
023: import java.io.BufferedWriter;
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.io.InputStreamReader;
027: import java.io.OutputStream;
028: import java.io.OutputStreamWriter;
029: import java.util.HashMap;
030: import java.util.Iterator;
031: import java.util.Map;
032: import java.util.Properties;
033:
034: import org.apache.batik.dom.GenericDOMImplementation;
035: import org.apache.batik.dom.util.DOMUtilities;
036: import org.apache.batik.dom.util.DocumentFactory;
037: import org.apache.batik.dom.util.SAXDocumentFactory;
038: import org.apache.batik.util.PreferenceManager;
039: import org.apache.batik.util.XMLResourceDescriptor;
040: import org.w3c.dom.Document;
041: import org.w3c.dom.Element;
042: import org.w3c.dom.Node;
043:
044: /**
045: * An extension of {@link PreferenceManager} which store the preference
046: * in XML.
047: *
048: * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
049: * @version $Id: XMLPreferenceManager.java 475477 2006-11-15 22:44:28Z cam $
050: */
051: public class XMLPreferenceManager extends PreferenceManager {
052:
053: /**
054: * The XML parser
055: */
056: protected String xmlParserClassName;
057:
058: /**
059: * The XML encoding used to store properties
060: */
061: public static final String PREFERENCE_ENCODING = "8859_1";
062:
063: /**
064: * Creates a preference manager.
065: * @param prefFileName the name of the preference file.
066: */
067: public XMLPreferenceManager(String prefFileName) {
068: this (prefFileName, null, XMLResourceDescriptor
069: .getXMLParserClassName());
070: }
071:
072: /**
073: * Creates a preference manager.
074: * @param prefFileName the name of the preference file.
075: * @param defaults where to get defaults value if the value is
076: * not specified in the file.
077: */
078: public XMLPreferenceManager(String prefFileName, Map defaults) {
079: this (prefFileName, defaults, XMLResourceDescriptor
080: .getXMLParserClassName());
081: }
082:
083: /**
084: * Creates a preference manager.
085: * @param prefFileName the name of the preference file.
086: * @param parser The XML parser class name.
087: */
088: public XMLPreferenceManager(String prefFileName, String parser) {
089: this (prefFileName, null, parser);
090: }
091:
092: /**
093: * Creates a preference manager with a default values
094: * initialization map.
095: * @param prefFileName the name of the preference file.
096: * @param defaults where to get defaults value if the value is
097: * not specified in the file.
098: * @param parser The XML parser class name.
099: */
100: public XMLPreferenceManager(String prefFileName, Map defaults,
101: String parser) {
102: super (prefFileName, defaults);
103: internal = new XMLProperties();
104: xmlParserClassName = parser;
105: }
106:
107: /**
108: * To store the preferences.
109: */
110: protected class XMLProperties extends Properties {
111:
112: /**
113: * Reads a property list (key and element pairs) from the input stream.
114: * The stream is assumed to be using the ISO 8859-1 character encoding.
115: */
116: public synchronized void load(InputStream is)
117: throws IOException {
118: BufferedReader r;
119: r = new BufferedReader(new InputStreamReader(is,
120: PREFERENCE_ENCODING));
121: DocumentFactory df = new SAXDocumentFactory(
122: GenericDOMImplementation.getDOMImplementation(),
123: xmlParserClassName);
124: Document doc = df.createDocument(
125: "http://xml.apache.org/batik/preferences",
126: "preferences", null, r);
127: Element elt = doc.getDocumentElement();
128: for (Node n = elt.getFirstChild(); n != null; n = n
129: .getNextSibling()) {
130: if (n.getNodeType() == Node.ELEMENT_NODE) {
131: if (n.getNodeName().equals("property")) {
132: String name = ((Element) n).getAttributeNS(
133: null, "name");
134:
135: StringBuffer cont = new StringBuffer();
136: for (Node c = n.getFirstChild(); c != null; c = c
137: .getNextSibling()) {
138: if (c.getNodeType() == Node.TEXT_NODE) {
139: cont.append(c.getNodeValue());
140: } else {
141: break;
142: }
143: }
144: String val = cont.toString();
145: put(name, val);
146: }
147: }
148: }
149: }
150:
151: /**
152: * Writes this property list (key and element pairs) in this
153: * <code>Properties</code> table to the output stream in a format suitable
154: * for loading into a <code>Properties</code> table using the
155: * <code>load</code> method.
156: * The stream is written using the ISO 8859-1 character encoding.
157: */
158: public synchronized void store(OutputStream os, String header)
159: throws IOException {
160: BufferedWriter w;
161: w = new BufferedWriter(new OutputStreamWriter(os,
162: PREFERENCE_ENCODING));
163:
164: Map m = new HashMap();
165: enumerate(m);
166:
167: w
168: .write("<preferences xmlns=\"http://xml.apache.org/batik/preferences\">\n");
169:
170: Iterator it = m.keySet().iterator();
171: while (it.hasNext()) {
172: String n = (String) it.next();
173: String v = (String) m.get(n);
174:
175: w.write("<property name=\"" + n + "\">");
176: w.write(DOMUtilities.contentToString(v));
177: w.write("</property>\n");
178: }
179:
180: w.write("</preferences>\n");
181: w.flush();
182: }
183:
184: /**
185: * Enumerates all key/value pairs in the specified m.
186: * @param m the map
187: */
188: private synchronized void enumerate(Map m) {
189: if (defaults != null) {
190: Iterator it = m.keySet().iterator();
191: while (it.hasNext()) {
192: Object k = it.next();
193: m.put(k, defaults.get(k));
194: }
195: }
196: Iterator it = keySet().iterator();
197: while (it.hasNext()) {
198: Object k = it.next();
199: m.put(k, get(k));
200: }
201: }
202:
203: }
204: }
|