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)
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
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
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
.*;
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
;
65 * True if the complete sub tree should be written,
66 * false if only the node should be written.
68 private boolean subtree
;
71 * Creates a new NodeWriter for the given preferences node and
72 * outputstream. Creates a new OutputStreamWriter.
74 public NodeWriter(Preferences prefs
, OutputStream os
) {
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");
88 this.bw
= new BufferedWriter(w
);
92 * Writes the preference node plus the complete subtree.
94 public void writePrefsTree() throws BackingStoreException
, IOException
{
102 * Writes only the preference node.
104 public void writePrefs() throws BackingStoreException
, IOException
{
112 * Writes the standard header.
114 private void writeHeader() throws BackingStoreException
, IOException
{
115 bw
.write("<?xml version=\"1.0\"?>");
117 bw
.write("<!DOCTYPE preferences SYSTEM "
118 + "\"http://java.sun.com/dtd/preferences.dtd\">");
121 bw
.write("<!-- GNU Classpath java.util.prefs Preferences ");
123 if (prefs
.isUserNode()) {
130 if (prefs
.parent() == null) {
141 if (prefs
.parent() != null) {
144 bw
.write(prefs
.absolutePath());
154 * Write the preferences tag and the root.
156 private void writePreferences() throws BackingStoreException
, IOException
{
157 bw
.write("<preferences>");
160 bw
.write("</preferences>");
164 private void writeRoot() throws BackingStoreException
, IOException
{
165 bw
.write(" <root type=\"");
166 if (prefs
.isUserNode()) {
176 bw
.write(" </root>");
180 private void writeRootMap() throws BackingStoreException
, IOException
{
181 // Is it a root node?
182 if(prefs
.parent() == null && prefs
.keys().length
> 0) {
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
197 private int writeParents() throws IOException
{
199 String path
= prefs
.absolutePath();
200 int lastslash
= path
.lastIndexOf("/");
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();
212 bw
.write("<node name=\"" + name
+ "\">");
224 private void writeCloseParents(int parents
) throws IOException
{
233 private void writeNode() throws BackingStoreException
, IOException
{
234 int parents
= writeParents();
237 if (prefs
.parent() == null) {
242 writeNode(prefs
, indent
);
243 writeCloseParents(parents
);
246 private void writeNode(Preferences node
, int indent
)
247 throws BackingStoreException
, IOException
250 if (node
.parent() != null) {
252 bw
.write("<node name=\"" + node
.name() + "\">");
253 if (node
.keys().length
> 0) {
256 writeMap(node
, indent
+1);
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);
268 if (node
.parent() != null) {
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
);
288 writeEntries(node
, indentString
+ " ");
289 bw
.write(indentString
);
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);
304 throw new BackingStoreException("null value for key '"
309 bw
.write("<entry key=\"" + keys
[i
] + "\""
310 + " value=\"" + value
+ "\"/>");
315 private void indent(int x
) throws IOException
{
316 for (int i
=0; i
<x
; i
++) {