OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [libjava/] [classpath/] [gnu/] [java/] [util/] [prefs/] [NodeWriter.java] - Blame information for rev 14

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 jlechner
/* NodeWriter - Writes and exports preferences nodes to files
2
   Copyright (C) 2001 Free Software Foundation, Inc.
3
 
4
This file is part of GNU Classpath.
5
 
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.
10
 
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.
15
 
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.
20
 
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.
25
 
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. */
37
 
38
package gnu.java.util.prefs;
39
 
40
import java.io.BufferedWriter;
41
import java.io.IOException;
42
import java.io.OutputStream;
43
import java.io.OutputStreamWriter;
44
import java.io.Writer;
45
 
46
import java.util.StringTokenizer;
47
 
48
import java.util.prefs.*;
49
 
50
/**
51
 * Writes and exports preferences nodes to files
52
 *
53
 * @author Mark Wielaard (mark@klomp.org)
54
 */
55
public class NodeWriter {
56
 
57
    /** The Preferences node to write. */
58
    private final Preferences prefs;
59
 
60
    /** The bufferedWriter to write the node to. */
61
    private final BufferedWriter bw;
62
 
63
    /**
64
     * True if the complete sub tree should be written,
65
     * false if only the node should be written.
66
     */
67
    private boolean subtree;
68
 
69
    /**
70
     * Creates a new NodeWriter for the given preferences node and writer.
71
     */
72
    public NodeWriter(Preferences prefs, Writer w) {
73
        this.prefs = prefs;
74
        if (w instanceof BufferedWriter) {
75
            this.bw = (BufferedWriter) w;
76
        } else {
77
            this.bw = new BufferedWriter(w);
78
        }
79
    }
80
 
81
    /**
82
     * Creates a new NodeWriter for the given preferences node and
83
     * outputstream. Creates a new OutputStreamWriter.
84
     */
85
    public NodeWriter(Preferences prefs, OutputStream os) {
86
        this(prefs, new OutputStreamWriter(os));
87
    }
88
 
89
    /**
90
     * Writes the preference node plus the complete subtree.
91
     */
92
    public void writePrefsTree() throws BackingStoreException, IOException {
93
        subtree = true;
94
        writeHeader();
95
        writePreferences();
96
        bw.flush();
97
    }
98
 
99
    /**
100
     * Writes only the preference node.
101
     */
102
    public void writePrefs() throws BackingStoreException, IOException {
103
        subtree = false;
104
        writeHeader();
105
        writePreferences();
106
        bw.flush();
107
    }
108
 
109
    /**
110
     * Writes the standard header.
111
     */
112
    private void writeHeader() throws BackingStoreException, IOException {
113
        bw.write("<?xml version=\"1.0\"?>");
114
        bw.newLine();
115
        bw.newLine();
116
        bw.write("<!-- GNU Classpath java.util.prefs Preferences ");
117
 
118
        if (prefs.isUserNode()) {
119
            bw.write("user");
120
        } else {
121
            bw.write("system");
122
        }
123
 
124
        // root node?
125
        if (prefs.parent() == null) {
126
            bw.write(" root");
127
        }
128
 
129
        if (subtree) {
130
            bw.write(" tree");
131
        } else {
132
            bw.write(" node");
133
        }
134
 
135
        // no root?
136
        if (prefs.parent() != null) {
137
            bw.newLine();
138
            bw.write("     '");
139
            bw.write(prefs.absolutePath());
140
            bw.write('\'');
141
            bw.newLine();
142
        }
143
        bw.write(" -->");
144
        bw.newLine();
145
        bw.newLine();
146
    }
147
 
148
    /**
149
     * Write the preferences tag and the root.
150
     */
151
    private void writePreferences() throws BackingStoreException, IOException {
152
        bw.write("<preferences>");
153
        bw.newLine();
154
        writeRoot();
155
        bw.write("</preferences>");
156
        bw.newLine();
157
    }
158
 
159
    private void writeRoot() throws BackingStoreException, IOException {
160
        bw.write("  <root type=\"");
161
        if (prefs.isUserNode()) {
162
            bw.write("user");
163
        } else {
164
            bw.write("system");
165
        }
166
        bw.write("\"/>");
167
 
168
        writeRootMap();
169
        writeNode();
170
 
171
        bw.write("  </root>");
172
        bw.newLine();
173
    }
174
 
175
    private void writeRootMap() throws BackingStoreException, IOException {
176
        // Is it a root node?
177
        if(prefs.parent() == null && prefs.keys().length > 0) {
178
            bw.newLine();
179
            writeMap(prefs, 2);
180
        } else {
181
            bw.write("<map/>");
182
            bw.newLine();
183
        }
184
    }
185
 
186
    /**
187
     * Writes all the parents of the preferences node without any entries.
188
     * Returns the number of parents written, which has to be used as
189
     * argument to <code>writeCloseParents()</code> after writing the node
190
     * itself.
191
     */
192
    private int writeParents() throws IOException {
193
        int parents;
194
        String path = prefs.absolutePath();
195
        int lastslash = path.lastIndexOf("/");
196
        if (lastslash > 0) {
197
            path = path.substring(1, lastslash);
198
            StringTokenizer st = new StringTokenizer(path);
199
            parents = st.countTokens();
200
 
201
            System.out.println("path: " + path);
202
            System.out.println("parents: " + parents);
203
 
204
            for (int i=0; i<parents; i++) {
205
                String name = st.nextToken();
206
                indent(i+2);
207
                bw.write("<node name=\"" + name + "\">");
208
                bw.write("<map/>");
209
                bw.write("</node>");
210
                bw.newLine();
211
            }
212
        } else {
213
            parents = 0;
214
        }
215
 
216
        return parents;
217
    }
218
 
219
    private void writeCloseParents(int parents) throws IOException {
220
        while(parents > 0) {
221
            indent(parents+1);
222
            bw.write("</node>");
223
            bw.newLine();
224
            parents--;
225
        }
226
    }
227
 
228
    private void writeNode() throws BackingStoreException, IOException {
229
        int parents = writeParents();
230
        // root?
231
        int indent;
232
        if (prefs.parent() == null) {
233
            indent = parents+1;
234
        } else {
235
            indent = parents+2;
236
        }
237
        writeNode(prefs, indent);
238
        writeCloseParents(parents);
239
    }
240
 
241
    private void writeNode(Preferences node, int indent)
242
                                    throws BackingStoreException, IOException
243
    {
244
        // not root?
245
        if (node.parent() != null) {
246
            indent(indent);
247
            bw.write("<node name=\"" + node.name() + "\">");
248
            if (node.keys().length > 0) {
249
                bw.newLine();
250
            }
251
            writeMap(node, indent+1);
252
        }
253
 
254
        if (subtree) {
255
            String[] children = node.childrenNames();
256
            for (int i=0; i<children.length; i++) {
257
                Preferences child = node.node(children[i]);
258
                writeNode(child, indent+1);
259
            }
260
        }
261
 
262
        // not root?
263
        if (node.parent() != null) {
264
            indent(indent);
265
            bw.write("</node>");
266
            bw.newLine();
267
        }
268
    }
269
 
270
    private void writeMap(Preferences node, int indent)
271
                                    throws BackingStoreException, IOException
272
    {
273
        // construct String used for indentation
274
        StringBuffer indentBuffer = new StringBuffer(2*indent);
275
        for (int i=0; i < indent; i++)
276
            indentBuffer.append("  ");
277
        String indentString = indentBuffer.toString();
278
 
279
        if (node.keys().length > 0) {
280
            bw.write(indentString);
281
            bw.write("<map>");
282
            bw.newLine();
283
            writeEntries(node, indentString + "  ");
284
            bw.write(indentString);
285
            bw.write("</map>");
286
        } else {
287
            bw.write("<map/>");
288
        }
289
        bw.newLine();
290
    }
291
 
292
    private void writeEntries(Preferences node, String indent)
293
                                    throws BackingStoreException, IOException
294
    {
295
        String[] keys = node.keys();
296
        for(int i = 0; i < keys.length; i++) {
297
            String value = node.get(keys[i], null);
298
            if (value == null) {
299
                throw new BackingStoreException("null value for key '"
300
                                                + keys[i] + "'");
301
            }
302
 
303
            bw.write(indent);
304
            bw.write("<entry key=\"" + keys[i] + "\""
305
                    + " value=\"" + value + "\"/>");
306
            bw.newLine();
307
        }
308
    }
309
 
310
    private void indent(int x) throws IOException {
311
        for (int i=0; i<x; i++) {
312
            bw.write("  ");
313
        }
314
    }
315
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.