KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dom4j > samples > CountDemo


1 /*
2  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
3  *
4  * This software is open source.
5  * See the bottom of this file for the licence.
6  *
7  * $Id: CountDemo.java,v 1.4 2005/01/29 14:52:57 maartenc Exp $
8  */

9
10 package org.dom4j.samples;
11
12 import org.dom4j.Attribute;
13 import org.dom4j.Comment;
14 import org.dom4j.Document;
15 import org.dom4j.Element;
16 import org.dom4j.ProcessingInstruction;
17 import org.dom4j.Text;
18 import org.dom4j.Visitor;
19 import org.dom4j.VisitorSupport;
20
21 /**
22  * A sample program to count the number of various kinds of DOM4J node types
23  *
24  * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan </a>
25  * @version $Revision: 1.4 $
26  */

27 public class CountDemo extends SAXDemo {
28
29     private int numCharacters;
30
31     private int numComments;
32
33     private int numElements;
34
35     private int numAttributes;
36
37     private int numProcessingInstructions;
38
39     public static void main(String JavaDoc[] args) {
40         run(new CountDemo(), args);
41     }
42
43     public CountDemo() {
44     }
45
46     protected void process(Document document) throws Exception JavaDoc {
47         numCharacters = 0;
48         numComments = 0;
49         numElements = 0;
50         numAttributes = 0;
51         numProcessingInstructions = 0;
52
53         Visitor visitor = new VisitorSupport() {
54
55             public void visit(Element node) {
56                 ++numElements;
57             }
58
59             public void visit(Attribute node) {
60                 ++numAttributes;
61             }
62
63             public void visit(Comment node) {
64                 ++numComments;
65             }
66
67             public void visit(ProcessingInstruction node) {
68                 ++numProcessingInstructions;
69             }
70
71             public void visit(Text node) {
72                 String JavaDoc text = node.getText();
73                 if (text != null) {
74                     numCharacters += text.length();
75                 }
76             }
77         };
78
79         println("Document: " + document.getName() + " has the following");
80         println("Elements\tAttributes\tComments\tPIs\tCharacters");
81
82         document.accept(visitor);
83
84         println(numElements + "\t\t" + numAttributes + "\t\t" + numComments
85                 + "\t\t" + numProcessingInstructions + "\t" + numCharacters);
86     }
87 }
88
89 /*
90  * Redistribution and use of this software and associated documentation
91  * ("Software"), with or without modification, are permitted provided that the
92  * following conditions are met:
93  *
94  * 1. Redistributions of source code must retain copyright statements and
95  * notices. Redistributions must also contain a copy of this document.
96  *
97  * 2. Redistributions in binary form must reproduce the above copyright notice,
98  * this list of conditions and the following disclaimer in the documentation
99  * and/or other materials provided with the distribution.
100  *
101  * 3. The name "DOM4J" must not be used to endorse or promote products derived
102  * from this Software without prior written permission of MetaStuff, Ltd. For
103  * written permission, please contact dom4j-info@metastuff.com.
104  *
105  * 4. Products derived from this Software may not be called "DOM4J" nor may
106  * "DOM4J" appear in their names without prior written permission of MetaStuff,
107  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
108  *
109  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
110  *
111  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
112  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
113  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
114  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
115  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
116  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
117  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
118  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
119  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
120  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
121  * POSSIBILITY OF SUCH DAMAGE.
122  *
123  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
124  *
125  * $Id: CountDemo.java,v 1.4 2005/01/29 14:52:57 maartenc Exp $
126  */

127
Popular Tags