KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > xml > DOMWriter


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: DOMWriter.java 109334 2004-12-01 13:04:19Z andreas $ */
19
20 package org.apache.lenya.xml;
21
22 import java.io.BufferedWriter JavaDoc;
23 import java.io.FileNotFoundException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.io.OutputStreamWriter JavaDoc;
26 import java.io.PrintWriter JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 import org.apache.log4j.Category;
30 import org.w3c.dom.Document JavaDoc;
31 import org.w3c.dom.Element JavaDoc;
32 import org.w3c.dom.NamedNodeMap JavaDoc;
33 import org.w3c.dom.Node JavaDoc;
34 import org.w3c.dom.NodeList JavaDoc;
35
36
37 /**
38  * DOCUMENT ME!
39  * @deprecated replaced by DocumentHelper
40  */

41 public class DOMWriter {
42     static Category log = Category.getInstance(DOMWriter.class);
43     PrintWriter JavaDoc out = null;
44     String JavaDoc encoding = null;
45
46     /**
47      * Creates a new DOMWriter object.
48      *
49      * @param out DOCUMENT ME!
50      */

51     public DOMWriter(PrintWriter JavaDoc out) {
52         this.out = out;
53     }
54
55     /**
56      * Creates a new DOMWriter object.
57      *
58      * @param out DOCUMENT ME!
59      * @param encoding DOCUMENT ME!
60      */

61     public DOMWriter(PrintWriter JavaDoc out, String JavaDoc encoding) {
62         this(out);
63         this.encoding = encoding;
64     }
65
66     /**
67      * Creates a new DOMWriter object.
68      *
69      * @param os DOCUMENT ME!
70      *
71      * @throws Exception DOCUMENT ME!
72      */

73     public DOMWriter(OutputStream JavaDoc os) throws Exception JavaDoc {
74         this(os, "utf-8");
75     }
76
77     /**
78      * Creates a new DOMWriter object.
79      *
80      * @param os DOCUMENT ME!
81      * @param encoding DOCUMENT ME!
82      *
83      * @throws Exception DOCUMENT ME!
84      */

85     public DOMWriter(OutputStream JavaDoc os, String JavaDoc encoding)
86         throws Exception JavaDoc {
87         out = new PrintWriter JavaDoc(new BufferedWriter JavaDoc(
88                     new OutputStreamWriter JavaDoc(os, XMLEncToJavaEnc.getJava(encoding))));
89         this.encoding = encoding;
90     }
91
92     /**
93      * DOCUMENT ME!
94      *
95      * @param args DOCUMENT ME!
96      */

97     public static void main(String JavaDoc[] args) {
98         if (args.length != 1) {
99             System.err.println("Usage: java org.apache.lenya.xml.DOMWriter \"file.xml\"");
100             System.err.println("Description: Reads \"file.xml\" and writes it to standard output");
101
102             return;
103         }
104
105         DOMParserFactory dpf = new DOMParserFactory();
106         Document JavaDoc document = null;
107
108         try {
109             document = dpf.getDocument(args[0]);
110         } catch (FileNotFoundException JavaDoc e) {
111             System.err.println("No such file: " + e.getMessage());
112
113             return;
114         } catch (Exception JavaDoc e) {
115             System.err.println(e.getMessage());
116
117             return;
118         }
119
120         try {
121             new DOMWriter(System.out, "iso-8859-1").printWithoutFormatting(document);
122         } catch (Exception JavaDoc e) {
123             System.err.println(e.getMessage());
124
125             return;
126         }
127
128         log.fatal("\n");
129
130         log.fatal(".main(): System.exit(0)");
131         System.exit(0);
132
133         new DOMWriter(new PrintWriter JavaDoc(System.out)).print(document);
134         System.out.print("\n");
135
136         XPointerFactory xpf = new XPointerFactory();
137
138         try {
139             Vector JavaDoc nodes = xpf.select(document.getDocumentElement(),
140                     "xpointer(/Example/People/Person/City)");
141             String JavaDoc[] values = xpf.getNodeValues(nodes);
142
143             for (int i = 0; i < values.length; i++) {
144                 System.out.println(values[i]);
145             }
146
147             Document JavaDoc doc = dpf.getDocument();
148             Element JavaDoc root = dpf.newElementNode(doc, "Root");
149
150             //
151
for (int i = 0; i < values.length; i++) {
152                 root.appendChild(dpf.newTextNode(doc, values[i]));
153             }
154
155             doc.appendChild(root);
156             new DOMWriter(new PrintWriter JavaDoc(System.out)).print(doc);
157             System.out.print("\n");
158         } catch (Exception JavaDoc e) {
159             System.err.println(e);
160         }
161     }
162
163     /**
164      * DOCUMENT ME!
165      *
166      * @param node DOCUMENT ME!
167      */

168     public void print(Node JavaDoc node) {
169         if (node == null) {
170             return;
171         }
172
173         short type = node.getNodeType();
174
175         switch (type) {
176         case Node.DOCUMENT_NODE: {
177             out.print("<?xml version=\"1.0\"");
178
179             if (encoding != null) {
180                 out.print(" encoding=\"" + encoding + "\"");
181             }
182
183             out.print("?>\n\n");
184             print(((Document JavaDoc) node).getDocumentElement());
185             out.flush();
186
187             break;
188         }
189
190         case Node.ELEMENT_NODE: {
191             out.print("<" + node.getNodeName());
192
193             NamedNodeMap JavaDoc attributes = node.getAttributes();
194
195             for (int i = 0; i < attributes.getLength(); i++) {
196                 Node JavaDoc attribute = attributes.item(i);
197                 out.print(" " + attribute.getNodeName() + "=\"" +
198                     Normalize.normalize(attribute.getNodeValue()) + "\"");
199             }
200
201             if (node.hasChildNodes()) {
202                 out.print(">");
203
204                 NodeList JavaDoc children = node.getChildNodes();
205
206                 for (int i = 0; i < children.getLength(); i++) {
207                     print(children.item(i));
208                 }
209
210                 out.print("</" + node.getNodeName() + ">");
211             } else {
212                 out.print("/>");
213             }
214
215             break;
216         }
217
218         case Node.TEXT_NODE: {
219             out.print(Normalize.normalize(node.getNodeValue()));
220
221             break;
222         }
223
224         case Node.COMMENT_NODE: {
225             out.print("<!--" + node.getNodeValue() + "-->");
226
227             break;
228         }
229
230         default: {
231             System.err.println(this.getClass().getName() + ".print(): Node type not implemented: " +
232                 type);
233
234             break;
235         }
236         }
237     }
238
239     /**
240      * DOCUMENT ME!
241      *
242      * @param node DOCUMENT ME!
243      */

244     public void printWithoutFormatting(Node JavaDoc node) {
245         if (node == null) {
246             return;
247         }
248
249         short type = node.getNodeType();
250
251         switch (type) {
252         case Node.DOCUMENT_NODE: {
253             out.print("<?xml version=\"1.0\"");
254
255             if (encoding != null) {
256                 out.print(" encoding=\"" + encoding + "\"");
257             }
258
259             out.print("?>\n\n");
260
261             Element JavaDoc root = ((Document JavaDoc) node).getDocumentElement();
262             root.setAttribute("xmlns:xlink", XLink.XLINK_NAMESPACE);
263             printWithoutFormatting(root);
264             out.flush();
265
266             break;
267         }
268
269         case Node.ELEMENT_NODE: {
270             out.print("<" + node.getNodeName());
271
272             NamedNodeMap JavaDoc attributes = node.getAttributes();
273
274             for (int i = 0; i < attributes.getLength(); i++) {
275                 Node JavaDoc attribute = attributes.item(i);
276                 out.print(" " + attribute.getNodeName() + "=\"" +
277                     replaceSpecialCharacters(attribute.getNodeValue()) + "\"");
278             }
279
280             if (node.hasChildNodes()) {
281                 out.print(">");
282
283                 NodeList JavaDoc children = node.getChildNodes();
284
285                 for (int i = 0; i < children.getLength(); i++) {
286                     printWithoutFormatting(children.item(i));
287                 }
288
289                 out.print("</" + node.getNodeName() + ">");
290             } else {
291                 out.print("/>");
292             }
293
294             break;
295         }
296
297         case Node.TEXT_NODE: {
298             out.print(replaceSpecialCharacters(node.getNodeValue()));
299
300             break;
301         }
302
303         case Node.COMMENT_NODE: {
304             out.print("<!--" + node.getNodeValue() + "-->");
305
306             break;
307         }
308
309         default: {
310             System.err.println(this.getClass().getName() + ".print(): Node type not implemented: " +
311                 type);
312
313             break;
314         }
315         }
316     }
317
318     /**
319      * DOCUMENT ME!
320      *
321      * @param s DOCUMENT ME!
322      *
323      * @return DOCUMENT ME!
324      */

325     public String JavaDoc replaceSpecialCharacters(String JavaDoc s) {
326         StringBuffer JavaDoc str = new StringBuffer JavaDoc();
327
328         int len = (s != null) ? s.length() : 0;
329
330         for (int i = 0; i < len; i++) {
331             char ch = s.charAt(i);
332
333             switch (ch) {
334             case '<': {
335                 str.append("&#60;");
336
337                 break;
338             }
339
340             case '>': {
341                 str.append("&#62;");
342
343                 break;
344             }
345
346             case '&': {
347                 str.append("&#38;");
348
349                 break;
350             }
351
352             default:
353                 str.append(ch);
354             }
355         }
356
357         return (str.toString());
358     }
359 }
360
Popular Tags