KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nu > xom > samples > PropertyPrinter


1 /* Copyright 2002, 2003 Elliotte Rusty Harold
2    
3    This library is free software; you can redistribute it and/or modify
4    it under the terms of version 2.1 of the GNU Lesser General Public
5    License as published by the Free Software Foundation.
6    
7    This library is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10    GNU Lesser General Public License for more details.
11    
12    You should have received a copy of the GNU Lesser General Public
13    License along with this library; if not, write to the
14    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15    Boston, MA 02111-1307 USA
16    
17    You can contact Elliotte Rusty Harold by sending e-mail to
18    elharo@metalab.unc.edu. Please include the word "XOM" in the
19    subject line. The XOM home page is located at http://www.xom.nu/
20 */

21
22 package nu.xom.samples;
23
24 import java.io.IOException JavaDoc;
25 import java.io.OutputStreamWriter JavaDoc;
26 import java.io.Writer JavaDoc;
27
28 import nu.xom.Attribute;
29 import nu.xom.Document;
30 import nu.xom.Element;
31 import nu.xom.Node;
32
33
34 /**
35  * <p>
36  * Demonstrates recursive descent through an XML document,
37  * and the getter methods of the <code>Element</code>
38  * and <code>Attribute</code> classes.
39  * </p>
40  *
41  * @author Elliotte Rusty Harold
42  * @version 1.0
43  *
44  */

45 public class PropertyPrinter {
46
47     private Writer JavaDoc out;
48     
49     public PropertyPrinter(Writer JavaDoc out) {
50       if (out == null) {
51       throw new NullPointerException JavaDoc("Writer must be non-null.");
52       }
53       this.out = out;
54     }
55     
56     public PropertyPrinter() {
57       this(new OutputStreamWriter JavaDoc(System.out));
58     }
59     
60     private int nodeCount = 0;
61     
62     public void writeNode(Node node) throws IOException JavaDoc {
63       
64         if (node == null) {
65             throw new NullPointerException JavaDoc("Node must be non-null.");
66         }
67         if (node instanceof Document) {
68             // starting a new document, reset the node count
69
nodeCount = 1;
70         }
71       
72         String JavaDoc type = node.getClass().getName(); // never null
73
String JavaDoc value = node.getValue();
74         
75         String JavaDoc name = null;
76         String JavaDoc localName = null;
77         String JavaDoc uri = null;
78         String JavaDoc prefix = null;
79         /* This is the one example I found where
80          * it seemed like it might make sense to
81          * move the namespace methods into Node or another
82          * common interface. However, this is a pretty
83          * artificial example, so I decided not to.
84          */

85         if (node instanceof Element) {
86             Element element = (Element) node;
87             name = element.getQualifiedName();
88             localName = element.getLocalName();
89             uri = element.getNamespaceURI();
90             prefix = element.getNamespacePrefix();
91         }
92         else if (node instanceof Attribute) {
93             Element element = (Element) node;
94             name = element.getQualifiedName();
95             localName = element.getLocalName();
96             uri = element.getNamespaceURI();
97             prefix = element.getNamespacePrefix();
98         }
99
100       
101         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
102         result.append("Node " + nodeCount + ":\r\n");
103         result.append(" Type: " + type + "\r\n");
104         if (name != null) {
105             result.append(" Name: " + name + "\r\n");
106         }
107         if (localName != null) {
108             result.append(" Local Name: " + localName + "\r\n");
109         }
110         if (prefix != null) {
111             result.append(" Prefix: " + prefix + "\r\n");
112         }
113         if (uri != null) {
114             result.append(" Namespace URI: " + uri + "\r\n");
115         }
116         if (value != null) {
117             result.append(" Value: " + value + "\r\n");
118         }
119       
120         out.write(result.toString());
121         out.write("\r\n");
122         out.flush();
123       
124         nodeCount++;
125       
126     }
127
128 }
129
Popular Tags