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 gnu
.java
.lang
.CPStringBuilder
;
42 import java
.io
.BufferedWriter
;
43 import java
.io
.IOException
;
44 import java
.io
.OutputStream
;
45 import java
.io
.OutputStreamWriter
;
46 import java
.io
.UnsupportedEncodingException
;
47 import java
.io
.Writer
;
49 import java
.util
.StringTokenizer
;
51 import java
.util
.prefs
.*;
54 * Writes and exports preferences nodes to files
56 * @author Mark Wielaard (mark@klomp.org)
58 public class NodeWriter
{
60 /** The Preferences node to write. */
61 private final Preferences prefs
;
63 /** The bufferedWriter to write the node to. */
64 private final BufferedWriter bw
;
67 * True if the complete sub tree should be written,
68 * false if only the node should be written.
70 private boolean subtree
;
73 * Creates a new NodeWriter for the given preferences node and
74 * outputstream. Creates a new OutputStreamWriter.
76 public NodeWriter(Preferences prefs
, OutputStream os
) {
81 w
= new OutputStreamWriter(os
, "UTF-8");
83 catch (UnsupportedEncodingException uee
)
85 // Shouldn't happen, since we always have UTF-8 available.
86 InternalError ie
= new InternalError("UTF-8 encoding missing");
90 this.bw
= new BufferedWriter(w
);
94 * Writes the preference node plus the complete subtree.
96 public void writePrefsTree() throws BackingStoreException
, IOException
{
104 * Writes only the preference node.
106 public void writePrefs() throws BackingStoreException
, IOException
{
114 * Writes the standard header.
116 private void writeHeader() throws BackingStoreException
, IOException
{
117 bw
.write("<?xml version=\"1.0\"?>");
119 bw
.write("<!DOCTYPE preferences SYSTEM "
120 + "\"http://java.sun.com/dtd/preferences.dtd\">");
123 bw
.write("<!-- GNU Classpath java.util.prefs Preferences ");
125 if (prefs
.isUserNode()) {
132 if (prefs
.parent() == null) {
143 if (prefs
.parent() != null) {
146 bw
.write(prefs
.absolutePath());
156 * Write the preferences tag and the root.
158 private void writePreferences() throws BackingStoreException
, IOException
{
159 bw
.write("<preferences>");
162 bw
.write("</preferences>");
166 private void writeRoot() throws BackingStoreException
, IOException
{
167 bw
.write(" <root type=\"");
168 if (prefs
.isUserNode()) {
178 bw
.write(" </root>");
182 private void writeRootMap() throws BackingStoreException
, IOException
{
183 // Is it a root node?
184 if(prefs
.parent() == null && prefs
.keys().length
> 0) {
194 * Writes all the parents of the preferences node without any entries.
195 * Returns the number of parents written, which has to be used as
196 * argument to <code>writeCloseParents()</code> after writing the node
199 private int writeParents() throws IOException
{
201 String path
= prefs
.absolutePath();
202 int lastslash
= path
.lastIndexOf("/");
204 path
= path
.substring(1, lastslash
);
205 StringTokenizer st
= new StringTokenizer(path
);
206 parents
= st
.countTokens();
208 for (int i
=0; i
<parents
; i
++) {
209 String name
= st
.nextToken();
211 bw
.write("<node name=\"" + name
+ "\">");
223 private void writeCloseParents(int parents
) throws IOException
{
232 private void writeNode() throws BackingStoreException
, IOException
{
233 int parents
= writeParents();
236 if (prefs
.parent() == null) {
241 writeNode(prefs
, indent
);
242 writeCloseParents(parents
);
245 private void writeNode(Preferences node
, int indent
)
246 throws BackingStoreException
, IOException
249 if (node
.parent() != null) {
251 bw
.write("<node name=\"" + node
.name() + "\">");
252 if (node
.keys().length
> 0) {
255 writeMap(node
, indent
+1);
259 String
[] children
= node
.childrenNames();
260 for (int i
=0; i
<children
.length
; i
++) {
261 Preferences child
= node
.node(children
[i
]);
262 writeNode(child
, indent
+1);
267 if (node
.parent() != null) {
274 private void writeMap(Preferences node
, int indent
)
275 throws BackingStoreException
, IOException
277 // construct String used for indentation
278 CPStringBuilder indentBuffer
= new CPStringBuilder(2*indent
);
279 for (int i
=0; i
< indent
; i
++)
280 indentBuffer
.append(" ");
281 String indentString
= indentBuffer
.toString();
283 if (node
.keys().length
> 0) {
284 bw
.write(indentString
);
287 writeEntries(node
, indentString
+ " ");
288 bw
.write(indentString
);
296 private void writeEntries(Preferences node
, String indent
)
297 throws BackingStoreException
, IOException
299 String
[] keys
= node
.keys();
300 for(int i
= 0; i
< keys
.length
; i
++) {
301 String value
= node
.get(keys
[i
], null);
303 throw new BackingStoreException("null value for key '"
308 bw
.write("<entry key=\"" + keys
[i
] + "\""
309 + " value=\"" + value
+ "\"/>");
314 private void indent(int x
) throws IOException
{
315 for (int i
=0; i
<x
; i
++) {