1:
37:
38: package ;
39:
40: import ;
41:
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48:
49: import ;
50:
51: import ;
52:
53:
58: public class NodeWriter {
59:
60:
61: private final Preferences prefs;
62:
63:
64: private final BufferedWriter bw;
65:
66:
70: private boolean subtree;
71:
72:
76: public NodeWriter(Preferences prefs, OutputStream os) {
77: this.prefs = prefs;
78: Writer w;
79: try
80: {
81: w = new OutputStreamWriter(os, "UTF-8");
82: }
83: catch (UnsupportedEncodingException uee)
84: {
85:
86: InternalError ie = new InternalError("UTF-8 encoding missing");
87: ie.initCause(uee);
88: throw ie;
89: }
90: this.bw = new BufferedWriter(w);
91: }
92:
93:
96: public void writePrefsTree() throws BackingStoreException, IOException {
97: subtree = true;
98: writeHeader();
99: writePreferences();
100: bw.flush();
101: }
102:
103:
106: public void writePrefs() throws BackingStoreException, IOException {
107: subtree = false;
108: writeHeader();
109: writePreferences();
110: bw.flush();
111: }
112:
113:
116: private void writeHeader() throws BackingStoreException, IOException {
117: bw.write("<?xml version=\"1.0\"?>");
118: bw.newLine();
119: bw.write("<!DOCTYPE preferences SYSTEM "
120: + "\"http://java.sun.com/dtd/preferences.dtd\">");
121: bw.newLine();
122: bw.newLine();
123: bw.write("<!-- GNU Classpath java.util.prefs Preferences ");
124:
125: if (prefs.isUserNode()) {
126: bw.write("user");
127: } else {
128: bw.write("system");
129: }
130:
131:
132: if (prefs.parent() == null) {
133: bw.write(" root");
134: }
135:
136: if (subtree) {
137: bw.write(" tree");
138: } else {
139: bw.write(" node");
140: }
141:
142:
143: if (prefs.parent() != null) {
144: bw.newLine();
145: bw.write(" '");
146: bw.write(prefs.absolutePath());
147: bw.write('\'');
148: bw.newLine();
149: }
150: bw.write(" -->");
151: bw.newLine();
152: bw.newLine();
153: }
154:
155:
158: private void writePreferences() throws BackingStoreException, IOException {
159: bw.write("<preferences>");
160: bw.newLine();
161: writeRoot();
162: bw.write("</preferences>");
163: bw.newLine();
164: }
165:
166: private void writeRoot() throws BackingStoreException, IOException {
167: bw.write(" <root type=\"");
168: if (prefs.isUserNode()) {
169: bw.write("user");
170: } else {
171: bw.write("system");
172: }
173: bw.write("\">");
174:
175: writeRootMap();
176: writeNode();
177:
178: bw.write(" </root>");
179: bw.newLine();
180: }
181:
182: private void writeRootMap() throws BackingStoreException, IOException {
183:
184: if(prefs.parent() == null && prefs.keys().length > 0) {
185: bw.newLine();
186: writeMap(prefs, 2);
187: } else {
188: bw.write("<map/>");
189: bw.newLine();
190: }
191: }
192:
193:
199: private int writeParents() throws IOException {
200: int parents;
201: String path = prefs.absolutePath();
202: int lastslash = path.lastIndexOf("/");
203: if (lastslash > 0) {
204: path = path.substring(1, lastslash);
205: StringTokenizer st = new StringTokenizer(path);
206: parents = st.countTokens();
207:
208: for (int i=0; i<parents; i++) {
209: String name = st.nextToken();
210: indent(i+2);
211: bw.write("<node name=\"" + name + "\">");
212: bw.write("<map/>");
213: bw.write("</node>");
214: bw.newLine();
215: }
216: } else {
217: parents = 0;
218: }
219:
220: return parents;
221: }
222:
223: private void writeCloseParents(int parents) throws IOException {
224: while(parents > 0) {
225: indent(parents+1);
226: bw.write("</node>");
227: bw.newLine();
228: parents--;
229: }
230: }
231:
232: private void writeNode() throws BackingStoreException, IOException {
233: int parents = writeParents();
234:
235: int indent;
236: if (prefs.parent() == null) {
237: indent = parents+1;
238: } else {
239: indent = parents+2;
240: }
241: writeNode(prefs, indent);
242: writeCloseParents(parents);
243: }
244:
245: private void writeNode(Preferences node, int indent)
246: throws BackingStoreException, IOException
247: {
248:
249: if (node.parent() != null) {
250: indent(indent);
251: bw.write("<node name=\"" + node.name() + "\">");
252: if (node.keys().length > 0) {
253: bw.newLine();
254: }
255: writeMap(node, indent+1);
256: }
257:
258: if (subtree) {
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);
263: }
264: }
265:
266:
267: if (node.parent() != null) {
268: indent(indent);
269: bw.write("</node>");
270: bw.newLine();
271: }
272: }
273:
274: private void writeMap(Preferences node, int indent)
275: throws BackingStoreException, IOException
276: {
277:
278: CPStringBuilder indentBuffer = new CPStringBuilder(2*indent);
279: for (int i=0; i < indent; i++)
280: indentBuffer.append(" ");
281: String indentString = indentBuffer.toString();
282:
283: if (node.keys().length > 0) {
284: bw.write(indentString);
285: bw.write("<map>");
286: bw.newLine();
287: writeEntries(node, indentString + " ");
288: bw.write(indentString);
289: bw.write("</map>");
290: } else {
291: bw.write("<map/>");
292: }
293: bw.newLine();
294: }
295:
296: private void writeEntries(Preferences node, String indent)
297: throws BackingStoreException, IOException
298: {
299: String[] keys = node.keys();
300: for(int i = 0; i < keys.length; i++) {
301: String value = node.get(keys[i], null);
302: if (value == null) {
303: throw new BackingStoreException("null value for key '"
304: + keys[i] + "'");
305: }
306:
307: bw.write(indent);
308: bw.write("<entry key=\"" + keys[i] + "\""
309: + " value=\"" + value + "\"/>");
310: bw.newLine();
311: }
312: }
313:
314: private void indent(int x) throws IOException {
315: for (int i=0; i<x; i++) {
316: bw.write(" ");
317: }
318: }
319: }