KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dom > DOMCount


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 1999, 2000 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 1999, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package dom;
59
60 import org.enhydra.apache.xerces.dom.TextImpl;
61 import org.w3c.dom.Document JavaDoc;
62 import org.w3c.dom.NamedNodeMap JavaDoc;
63 import org.w3c.dom.Node JavaDoc;
64 import org.w3c.dom.NodeList JavaDoc;
65
66 import util.Arguments;
67
68 /**
69  * A sample DOM counter. This sample program illustrates how to
70  * traverse a DOM tree in order to information about the document.
71  *
72  * @version $id$
73  */

74 public class DOMCount {
75
76     //
77
// Constants
78
//
79

80     /** Default parser name. */
81     private static final String JavaDoc
82     DEFAULT_PARSER_NAME = "dom.wrappers.DOMParser";
83
84     private static boolean setValidation = false; //defaults
85
private static boolean setNameSpaces = true;
86     private static boolean setSchemaSupport = true;
87     private static boolean setSchemaFullSupport = false;
88     private static boolean setDeferredDOM = true;
89
90
91
92     //
93
// Data
94
//
95

96     /** Elements. */
97     private long elements;
98
99     /** Attributes. */
100     private long attributes;
101
102     /** Characters. */
103     private long characters;
104
105     /** Ignorable whitespace. */
106     private long ignorableWhitespace;
107
108
109     //
110
// Public static methods
111
//
112

113     /** Counts the resulting document tree. */
114     public static void count(String JavaDoc parserWrapperName, String JavaDoc uri) {
115
116         try {
117             DOMParserWrapper parser =
118             (DOMParserWrapper)Class.forName(parserWrapperName).newInstance();
119             DOMCount counter = new DOMCount();
120             long before = System.currentTimeMillis();
121             parser.setFeature( "http://apache.org/xml/features/dom/defer-node-expansion",
122
123                                setDeferredDOM );
124             parser.setFeature( "http://xml.org/sax/features/validation",
125                                setValidation );
126             parser.setFeature( "http://xml.org/sax/features/namespaces",
127                                setNameSpaces );
128             parser.setFeature( "http://apache.org/xml/features/validation/schema",
129                                setSchemaSupport );
130             parser.setFeature( "http://apache.org/xml/features/validation/schema-full-checking",
131                                setSchemaFullSupport );
132
133             Document JavaDoc document = parser.parse(uri);
134             counter.traverse(document);
135             long after = System.currentTimeMillis();
136             counter.printResults(uri, after - before);
137         } catch (org.xml.sax.SAXParseException JavaDoc spe) {
138         } catch (org.xml.sax.SAXNotRecognizedException JavaDoc ex ){
139         } catch (org.xml.sax.SAXNotSupportedException JavaDoc ex ){
140         } catch (org.xml.sax.SAXException JavaDoc se) {
141             if (se.getException() != null)
142                 se.getException().printStackTrace(System.err);
143             else
144                 se.printStackTrace(System.err);
145         } catch (Exception JavaDoc e) {
146             e.printStackTrace(System.err);
147         }
148
149     } // print(String,String,boolean)
150

151     //
152
// Public methods
153
//
154

155     /** Traverses the specified node, recursively. */
156     public void traverse(Node JavaDoc node) {
157
158         // is there anything to do?
159
if (node == null) {
160             return;
161         }
162
163         int type = node.getNodeType();
164         switch (type) {
165         // print document
166
case Node.DOCUMENT_NODE: {
167                 elements = 0;
168                 attributes = 0;
169                 characters = 0;
170                 ignorableWhitespace = 0;
171                 traverse(((Document JavaDoc)node).getDocumentElement());
172                 break;
173             }
174
175             // print element with attributes
176
case Node.ELEMENT_NODE: {
177                 elements++;
178                 NamedNodeMap JavaDoc attrs = node.getAttributes();
179                 if (attrs != null) {
180                     attributes += attrs.getLength();
181                 }
182                 NodeList JavaDoc children = node.getChildNodes();
183                 if (children != null) {
184                     int len = children.getLength();
185                     for (int i = 0; i < len; i++) {
186                         traverse(children.item(i));
187                     }
188                 }
189                 break;
190             }
191
192             // handle entity reference nodes
193
case Node.ENTITY_REFERENCE_NODE: {
194                 NodeList JavaDoc children = node.getChildNodes();
195                 if (children != null) {
196                     int len = children.getLength();
197                     for (int i = 0; i < len; i++) {
198                         traverse(children.item(i));
199                     }
200                 }
201                 break;
202             }
203
204             // print text
205
case Node.CDATA_SECTION_NODE: {
206                 characters += node.getNodeValue().length();
207                 break;
208             }
209         case Node.TEXT_NODE: {
210                 if (node instanceof TextImpl) {
211                     if (((TextImpl)node).isIgnorableWhitespace())
212                         ignorableWhitespace += node.getNodeValue().length();
213                     else
214                         characters += node.getNodeValue().length();
215                 } else
216                     characters += node.getNodeValue().length();
217                 break;
218             }
219         }
220
221     } // traverse(Node)
222

223     /** Prints the results. */
224     public void printResults(String JavaDoc uri, long time) {
225
226         // filename.xml: 631 ms (4 elems, 0 attrs, 78 spaces, 0 chars)
227
System.out.print(uri);
228         System.out.print(": ");
229         System.out.print(time);
230         System.out.print(" ms (");
231         System.out.print(elements);
232         System.out.print(" elems, ");
233         System.out.print(attributes);
234         System.out.print(" attrs, ");
235         System.out.print(ignorableWhitespace);
236         System.out.print(" spaces, ");
237         System.out.print(characters);
238         System.out.print(" chars)");
239         System.out.println();
240
241     } // printResults(String,long)
242

243     //
244
// Main
245
//
246

247     /** Main program entry point. */
248     public static void main(String JavaDoc argv[]) {
249
250         Arguments argopt = new Arguments();
251         argopt.setUsage( new String JavaDoc[] {
252                              "usage: java dom.DOMCount (options) uri ...",
253                              "",
254                              "options:",
255                              " -p name Specify DOM parser wrapper by name.",
256                              " -n | -N Turn on/off namespace [default=on]",
257                              " -v | -V Turn on/off validation [default=off]",
258                              " -s | -S Turn on/off Schema support [default=on]",
259                              " -f | -F Turn on/off Schema full consraint checking [default=off]",
260                              " -d | -D Turn on/off deferred DOM [default=on]",
261                              " -h This help screen."} );
262
263
264         // is there anything to do?
265
if (argv.length == 0) {
266             argopt.printUsage();
267             System.exit(1);
268         }
269
270         // vars
271
String JavaDoc parserName = DEFAULT_PARSER_NAME;
272
273         argopt.parseArgumentTokens(argv , new char[] { 'p'} );
274
275         int c;
276         String JavaDoc arg = null;
277         while ( ( arg = argopt.getlistFiles() ) != null ) {
278 outer:
279             while ( (c = argopt.getArguments()) != -1 ){
280                 switch (c) {
281                 case 'v':
282                     setValidation = true;
283                     //System.out.println( "v" );
284
break;
285                 case 'V':
286                     setValidation = false;
287                     //System.out.println( "V" );
288
break;
289                 case 'N':
290                     setNameSpaces = false;
291                     break;
292                 case 'n':
293                     setNameSpaces = true;
294                     break;
295                 case 'p':
296                     //System.out.println('p');
297
parserName = argopt.getStringParameter();
298                     //System.out.println( "parserName = " + parserName );
299
break;
300                 case 'd':
301                     setDeferredDOM = true;
302                     break;
303                 case 'D':
304                     setDeferredDOM = false;
305                     break;
306                 case 's':
307                     //System.out.println("s" );
308
setSchemaSupport = true;
309                     break;
310                 case 'S':
311                     //System.out.println("S" );
312
setSchemaSupport = false;
313                     break;
314                 case 'f':
315                     setSchemaFullSupport = true;
316                     break;
317                 case 'F':
318                     setSchemaFullSupport = false;
319                     break;
320                 case '?':
321                 case 'h':
322                 case '-':
323                     argopt.printUsage();
324                     System.exit(1);
325                     break;
326                 case -1:
327                     //System.out.println( "-1" );
328
break outer;
329                 default:
330                     
331                     break;
332                 }
333             }
334
335             count(parserName, arg ); //count uri
336
}
337
338     } // main(String[])
339

340 } // class DOMCount
341
Popular Tags