KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* Copyright 2002-2004 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
26 import nu.xom.Builder;
27 import nu.xom.Element;
28 import nu.xom.Attribute;
29 import nu.xom.Nodes;
30 import nu.xom.NodeFactory;
31 import nu.xom.ParsingException;
32
33
34 /**
35  * <p>
36  * Demonstrates walking the element hierarchy of
37  * an XML document in a streaming fashion while
38  * storing state in the node factory.
39  * </p>
40  *
41  * @author Elliotte Rusty Harold
42  * @version 1.0
43  *
44  */

45 public class StreamingTypeCounter extends NodeFactory{
46
47     private int CDATA = 0;
48     private int ID = 0;
49     private int IDREF = 0;
50     private int IDREFS = 0;
51     private int NMTOKEN = 0;
52     private int NMTOKENS = 0;
53     private int ENTITY = 0;
54     private int ENTITIES = 0;
55     private int NOTATION = 0;
56     
57     private Nodes empty = new Nodes();
58
59     public void printCount() {
60         System.out.println("CDATA type attributes: " + CDATA);
61         System.out.println("ID type attributes: " + ID );
62         System.out.println("IDREF type attributes: " + IDREF );
63         System.out.println("IDREFS type attributes: " + IDREFS );
64         System.out.println("NMTOKEN type attributes: " + NMTOKEN );
65         System.out.println("NMTOKENS type attributes: " + NMTOKENS );
66         System.out.println("ENTITY type attributes: " + ENTITY );
67         System.out.println("ENTITIES type attributes: " + ENTITIES );
68         System.out.println("NOTATION type attributes: " + NOTATION );
69     }
70
71     public static void main(String JavaDoc[] args) {
72   
73         if (args.length == 0) {
74             System.out.println(
75               "Usage: java nu.xom.samples.StreamingTypeCounter URL"
76             );
77             return;
78         }
79       
80         StreamingTypeCounter counter = new StreamingTypeCounter();
81         Builder builder = new Builder(counter);
82      
83         try {
84             builder.build(args[0]);
85             counter.printCount();
86         }
87         // indicates a well-formedness error
88
catch (ParsingException ex) {
89             System.out.println(args[0] + " is not well-formed.");
90             System.out.println(ex.getMessage());
91         }
92         catch (IOException JavaDoc ex) {
93             System.out.println(ex);
94         }
95   
96     }
97
98     // We don't need the comments.
99
public Nodes makeComment(String JavaDoc data) {
100         return empty;
101     }
102
103     // We don't need text nodes at all
104
public Nodes makeText(String JavaDoc data) {
105         return empty;
106     }
107
108     public Element startMakingElement(String JavaDoc name, String JavaDoc namespace) {
109         // We only need to create the root element
110
return null;
111     }
112     
113     public Nodes makeAttribute(String JavaDoc name, String JavaDoc URI,
114       String JavaDoc value, Attribute.Type type) {
115         if (type.equals(Attribute.Type.CDATA)) CDATA++;
116         else if (type.equals(Attribute.Type.UNDECLARED)) CDATA++;
117         else if (type.equals(Attribute.Type.ID)) ID++;
118         else if (type.equals(Attribute.Type.IDREF)) IDREF++;
119         else if (type.equals(Attribute.Type.IDREFS)) IDREFS++;
120         else if (type.equals(Attribute.Type.NMTOKEN)) NMTOKEN++;
121         else if (type.equals(Attribute.Type.NMTOKENS)) NMTOKENS++;
122         else if (type.equals(Attribute.Type.ENTITY)) ENTITY++;
123         else if (type.equals(Attribute.Type.ENTITIES)) ENTITIES++;
124         else if (type.equals(Attribute.Type.NOTATION)) NOTATION++;
125         return empty;
126     }
127
128     public Nodes makeDocType(String JavaDoc rootElementName,
129       String JavaDoc publicID, String JavaDoc systemID) {
130         return empty;
131     }
132
133     public Nodes makeProcessingInstruction(
134       String JavaDoc target, String JavaDoc data) {
135         return empty;
136     }
137
138 }
139
Popular Tags