KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > apps > svgbrowser > XMLPreferenceManager


1 /*
2
3    Copyright 1999-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17 */

18
19 package org.apache.batik.apps.svgbrowser;
20
21 import java.io.BufferedReader JavaDoc;
22 import java.io.BufferedWriter JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.InputStreamReader JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.io.OutputStreamWriter JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Properties JavaDoc;
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 JavaDoc;
40 import org.w3c.dom.Element JavaDoc;
41 import org.w3c.dom.Node JavaDoc;
42
43 /**
44  * An extension of {@link PreferenceManager} which store the preference
45  * in XML.
46  *
47  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
48  * @version $Id: XMLPreferenceManager.java,v 1.6 2004/10/30 18:38:04 deweese Exp $
49  */

50 public class XMLPreferenceManager extends PreferenceManager {
51     
52     /**
53      * The XML parser
54      */

55     protected String JavaDoc xmlParserClassName;
56
57     /**
58      * The XML encoding used to store properties
59      */

60     public static final String JavaDoc PREFERENCE_ENCODING = "8859_1";
61
62     /**
63      * Creates a preference manager.
64      * @param prefFileName the name of the preference file.
65      */

66     public XMLPreferenceManager(String JavaDoc prefFileName){
67         this(prefFileName, null,
68              XMLResourceDescriptor.getXMLParserClassName());
69     }
70
71     /**
72      * Creates a preference manager.
73      * @param prefFileName the name of the preference file.
74      * @param defaults where to get defaults value if the value is
75      * not specified in the file.
76      */

77     public XMLPreferenceManager(String JavaDoc prefFileName,
78                                 Map JavaDoc defaults){
79         this(prefFileName, defaults,
80              XMLResourceDescriptor.getXMLParserClassName());
81     }
82
83     /**
84      * Creates a preference manager.
85      * @param prefFileName the name of the preference file.
86      * @param parser The XML parser class name.
87      */

88     public XMLPreferenceManager(String JavaDoc prefFileName, String JavaDoc parser) {
89         this(prefFileName, null, parser);
90     }
91
92     /**
93      * Creates a preference manager with a default values
94      * initialization map.
95      * @param prefFileName the name of the preference file.
96      * @param defaults where to get defaults value if the value is
97      * not specified in the file.
98      * @param parser The XML parser class name.
99      */

100     public XMLPreferenceManager(String JavaDoc prefFileName, Map JavaDoc defaults, String JavaDoc parser) {
101         super(prefFileName, defaults);
102         internal = new XMLProperties();
103         xmlParserClassName = parser;
104     }
105
106     /**
107      * To store the preferences.
108      */

109     protected class XMLProperties extends Properties JavaDoc {
110
111         /**
112          * Reads a property list (key and element pairs) from the input stream.
113          * The stream is assumed to be using the ISO 8859-1 character encoding.
114          */

115         public synchronized void load(InputStream JavaDoc is) throws IOException JavaDoc {
116             BufferedReader JavaDoc r;
117             r = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is, PREFERENCE_ENCODING));
118             DocumentFactory df = new SAXDocumentFactory
119                 (GenericDOMImplementation.getDOMImplementation(),
120                  xmlParserClassName);
121             Document JavaDoc doc = df.createDocument("http://xml.apache.org/batik/preferences",
122                                              "preferences",
123                                              null,
124                                              r);
125             Element JavaDoc elt = doc.getDocumentElement();
126             for (Node JavaDoc n = elt.getFirstChild(); n != null; n = n.getNextSibling()) {
127                 if (n.getNodeType() == Node.ELEMENT_NODE) {
128                     if (n.getNodeName().equals("property")) {
129                         String JavaDoc name = ((Element JavaDoc)n).getAttributeNS(null, "name");
130                         
131                         StringBuffer JavaDoc cont = new StringBuffer JavaDoc();
132                         for (Node JavaDoc 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 JavaDoc val = cont.toString();
142                         put(name, val);
143                     }
144                 }
145             }
146         }
147
148         /**
149          * Writes this property list (key and element pairs) in this
150          * <code>Properties</code> table to the output stream in a format suitable
151          * for loading into a <code>Properties</code> table using the
152          * <code>load</code> method.
153          * The stream is written using the ISO 8859-1 character encoding.
154          */

155         public synchronized void store(OutputStream JavaDoc os, String JavaDoc header)
156             throws IOException JavaDoc {
157             BufferedWriter JavaDoc w;
158             w = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(os, PREFERENCE_ENCODING));
159
160             Map JavaDoc m = new HashMap JavaDoc();
161             enumerate(m);
162
163             w.write("<preferences xmlns=\"http://xml.apache.org/batik/preferences\">\n");
164
165             Iterator JavaDoc it = m.keySet().iterator();
166             while (it.hasNext()) {
167                 String JavaDoc n = (String JavaDoc)it.next();
168                 String JavaDoc v = (String JavaDoc)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         /**
180          * Enumerates all key/value pairs in the specified m.
181          * @param m the map
182          */

183         private synchronized void enumerate(Map JavaDoc m) {
184             if (defaults != null) {
185                 Iterator JavaDoc it = m.keySet().iterator();
186                 while (it.hasNext()) {
187                     Object JavaDoc k = it.next();
188                     m.put(k, defaults.get(k));
189                 }
190             }
191             Iterator JavaDoc it = keySet().iterator();
192             while (it.hasNext()) {
193                 Object JavaDoc k = it.next();
194                 m.put(k, get(k));
195             }
196         }
197         
198     }
199 }
200
Popular Tags