Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / gnu / java / util / prefs / NodeWriter.java
blobc3cf8e8188a89198fb3bb8dcaed7b38d9c5bd5da
1 /* NodeWriter - Writes and exports preferences nodes to files
2 Copyright (C) 2001, 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package gnu.java.util.prefs;
40 import java.io.BufferedWriter;
41 import java.io.IOException;
42 import java.io.OutputStream;
43 import java.io.OutputStreamWriter;
44 import java.io.UnsupportedEncodingException;
45 import java.io.Writer;
47 import java.util.StringTokenizer;
49 import java.util.prefs.*;
51 /**
52 * Writes and exports preferences nodes to files
54 * @author Mark Wielaard (mark@klomp.org)
56 public class NodeWriter {
58 /** The Preferences node to write. */
59 private final Preferences prefs;
61 /** The bufferedWriter to write the node to. */
62 private final BufferedWriter bw;
64 /**
65 * True if the complete sub tree should be written,
66 * false if only the node should be written.
68 private boolean subtree;
70 /**
71 * Creates a new NodeWriter for the given preferences node and
72 * outputstream. Creates a new OutputStreamWriter.
74 public NodeWriter(Preferences prefs, OutputStream os) {
75 this.prefs = prefs;
76 Writer w;
77 try
79 w = new OutputStreamWriter(os, "UTF-8");
81 catch (UnsupportedEncodingException uee)
83 // Shouldn't happen, since we always have UTF-8 available.
84 InternalError ie = new InternalError("UTF-8 encoding missing");
85 ie.initCause(uee);
86 throw ie;
88 this.bw = new BufferedWriter(w);
91 /**
92 * Writes the preference node plus the complete subtree.
94 public void writePrefsTree() throws BackingStoreException, IOException {
95 subtree = true;
96 writeHeader();
97 writePreferences();
98 bw.flush();
102 * Writes only the preference node.
104 public void writePrefs() throws BackingStoreException, IOException {
105 subtree = false;
106 writeHeader();
107 writePreferences();
108 bw.flush();
112 * Writes the standard header.
114 private void writeHeader() throws BackingStoreException, IOException {
115 bw.write("<?xml version=\"1.0\"?>");
116 bw.newLine();
117 bw.write("<!DOCTYPE preferences SYSTEM "
118 + "\"http://java.sun.com/dtd/preferences.dtd\">");
119 bw.newLine();
120 bw.newLine();
121 bw.write("<!-- GNU Classpath java.util.prefs Preferences ");
123 if (prefs.isUserNode()) {
124 bw.write("user");
125 } else {
126 bw.write("system");
129 // root node?
130 if (prefs.parent() == null) {
131 bw.write(" root");
134 if (subtree) {
135 bw.write(" tree");
136 } else {
137 bw.write(" node");
140 // no root?
141 if (prefs.parent() != null) {
142 bw.newLine();
143 bw.write(" '");
144 bw.write(prefs.absolutePath());
145 bw.write('\'');
146 bw.newLine();
148 bw.write(" -->");
149 bw.newLine();
150 bw.newLine();
154 * Write the preferences tag and the root.
156 private void writePreferences() throws BackingStoreException, IOException {
157 bw.write("<preferences>");
158 bw.newLine();
159 writeRoot();
160 bw.write("</preferences>");
161 bw.newLine();
164 private void writeRoot() throws BackingStoreException, IOException {
165 bw.write(" <root type=\"");
166 if (prefs.isUserNode()) {
167 bw.write("user");
168 } else {
169 bw.write("system");
171 bw.write("\"/>");
173 writeRootMap();
174 writeNode();
176 bw.write(" </root>");
177 bw.newLine();
180 private void writeRootMap() throws BackingStoreException, IOException {
181 // Is it a root node?
182 if(prefs.parent() == null && prefs.keys().length > 0) {
183 bw.newLine();
184 writeMap(prefs, 2);
185 } else {
186 bw.write("<map/>");
187 bw.newLine();
192 * Writes all the parents of the preferences node without any entries.
193 * Returns the number of parents written, which has to be used as
194 * argument to <code>writeCloseParents()</code> after writing the node
195 * itself.
197 private int writeParents() throws IOException {
198 int parents;
199 String path = prefs.absolutePath();
200 int lastslash = path.lastIndexOf("/");
201 if (lastslash > 0) {
202 path = path.substring(1, lastslash);
203 StringTokenizer st = new StringTokenizer(path);
204 parents = st.countTokens();
206 System.out.println("path: " + path);
207 System.out.println("parents: " + parents);
209 for (int i=0; i<parents; i++) {
210 String name = st.nextToken();
211 indent(i+2);
212 bw.write("<node name=\"" + name + "\">");
213 bw.write("<map/>");
214 bw.write("</node>");
215 bw.newLine();
217 } else {
218 parents = 0;
221 return parents;
224 private void writeCloseParents(int parents) throws IOException {
225 while(parents > 0) {
226 indent(parents+1);
227 bw.write("</node>");
228 bw.newLine();
229 parents--;
233 private void writeNode() throws BackingStoreException, IOException {
234 int parents = writeParents();
235 // root?
236 int indent;
237 if (prefs.parent() == null) {
238 indent = parents+1;
239 } else {
240 indent = parents+2;
242 writeNode(prefs, indent);
243 writeCloseParents(parents);
246 private void writeNode(Preferences node, int indent)
247 throws BackingStoreException, IOException
249 // not root?
250 if (node.parent() != null) {
251 indent(indent);
252 bw.write("<node name=\"" + node.name() + "\">");
253 if (node.keys().length > 0) {
254 bw.newLine();
256 writeMap(node, indent+1);
259 if (subtree) {
260 String[] children = node.childrenNames();
261 for (int i=0; i<children.length; i++) {
262 Preferences child = node.node(children[i]);
263 writeNode(child, indent+1);
267 // not root?
268 if (node.parent() != null) {
269 indent(indent);
270 bw.write("</node>");
271 bw.newLine();
275 private void writeMap(Preferences node, int indent)
276 throws BackingStoreException, IOException
278 // construct String used for indentation
279 StringBuffer indentBuffer = new StringBuffer(2*indent);
280 for (int i=0; i < indent; i++)
281 indentBuffer.append(" ");
282 String indentString = indentBuffer.toString();
284 if (node.keys().length > 0) {
285 bw.write(indentString);
286 bw.write("<map>");
287 bw.newLine();
288 writeEntries(node, indentString + " ");
289 bw.write(indentString);
290 bw.write("</map>");
291 } else {
292 bw.write("<map/>");
294 bw.newLine();
297 private void writeEntries(Preferences node, String indent)
298 throws BackingStoreException, IOException
300 String[] keys = node.keys();
301 for(int i = 0; i < keys.length; i++) {
302 String value = node.get(keys[i], null);
303 if (value == null) {
304 throw new BackingStoreException("null value for key '"
305 + keys[i] + "'");
308 bw.write(indent);
309 bw.write("<entry key=\"" + keys[i] + "\""
310 + " value=\"" + value + "\"/>");
311 bw.newLine();
315 private void indent(int x) throws IOException {
316 for (int i=0; i<x; i++) {
317 bw.write(" ");