KickJava   Java API By Example, From Geeks To Geeks.

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


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: LargeDocumentDemo2.java,v 1.4 2005/01/29 14:52:57 maartenc Exp $
8  */

9
10 package org.dom4j.samples;
11
12 import org.dom4j.Document;
13 import org.dom4j.Element;
14 import org.dom4j.ElementHandler;
15 import org.dom4j.ElementPath;
16 import org.dom4j.io.SAXReader;
17
18 /**
19  * A test harness to test the content API in DOM4J
20  *
21  * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan </a>
22  * @version $Revision: 1.4 $
23  */

24 public class LargeDocumentDemo2 extends SAXDemo {
25
26     public static void main(String JavaDoc[] args) {
27         run(new LargeDocumentDemo2(), args);
28     }
29
30     public LargeDocumentDemo2() {
31     }
32
33     public void run(String JavaDoc[] args) throws Exception JavaDoc {
34         if (args.length < 1) {
35             printUsage("<XML document URL>");
36             return;
37         }
38
39         String JavaDoc xmlFile = args[0];
40
41         Document document = parse(xmlFile);
42         process(document);
43     }
44
45     protected Document parse(String JavaDoc url) throws Exception JavaDoc {
46         SAXReader reader = new SAXReader();
47
48         println("Parsing document: " + url);
49
50         // enable pruning to call me back as each Element is complete
51
reader.addHandler("/PLAY/ACT", new playActHandler());
52
53         println("##### starting parse");
54         Document document = reader.read(url);
55         println("##### finished parse");
56
57         // the document will be complete but have the prunePath elements pruned
58
println("Now lets dump what is left of the document after pruning...");
59
60         return document;
61     }
62
63     class playActHandler implements ElementHandler {
64         public void onStart(ElementPath path) {
65             Element element = path.getCurrent();
66             path.addHandler("SCENE/SPEECH", new actSceneSpeechHandler());
67         }
68
69         public void onEnd(ElementPath path) {
70             Element element = path.getCurrent();
71             println("Found Act: " + element.element("TITLE").getText());
72             path.removeHandler("SCENE/SPEECH");
73             element.detach();
74         }
75     }
76
77     class actSceneSpeechHandler implements ElementHandler {
78         public void onStart(ElementPath path) {
79             Element element = path.getCurrent();
80             println("Found Start of Speech");
81         }
82
83         public void onEnd(ElementPath path) {
84             Element element = path.getCurrent();
85             println("Found End of Speech by Speaker: "
86                     + element.element("SPEAKER").getText());
87             element.detach();
88         }
89     }
90 }
91
92 /*
93  * Redistribution and use of this software and associated documentation
94  * ("Software"), with or without modification, are permitted provided that the
95  * following conditions are met:
96  *
97  * 1. Redistributions of source code must retain copyright statements and
98  * notices. Redistributions must also contain a copy of this document.
99  *
100  * 2. Redistributions in binary form must reproduce the above copyright notice,
101  * this list of conditions and the following disclaimer in the documentation
102  * and/or other materials provided with the distribution.
103  *
104  * 3. The name "DOM4J" must not be used to endorse or promote products derived
105  * from this Software without prior written permission of MetaStuff, Ltd. For
106  * written permission, please contact dom4j-info@metastuff.com.
107  *
108  * 4. Products derived from this Software may not be called "DOM4J" nor may
109  * "DOM4J" appear in their names without prior written permission of MetaStuff,
110  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
111  *
112  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
113  *
114  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
115  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
116  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
117  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
118  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
119  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
120  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
121  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
122  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
123  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
124  * POSSIBILITY OF SUCH DAMAGE.
125  *
126  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
127  *
128  * $Id: LargeDocumentDemo2.java,v 1.4 2005/01/29 14:52:57 maartenc Exp $
129  */

130
Popular Tags