KickJava   Java API By Example, From Geeks To Geeks.

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


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

9
10 package org.dom4j.samples;
11
12 import java.io.BufferedReader JavaDoc;
13 import java.io.InputStreamReader JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.dom4j.Document;
18 import org.dom4j.Element;
19 import org.dom4j.Node;
20 import org.dom4j.io.OutputFormat;
21 import org.dom4j.io.XMLWriter;
22
23 /**
24  * A simple program that parsers a document and allows XPath expressions to be
25  * evaluated on the document.
26  *
27  * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan </a>
28  * @version $Revision: 1.4 $
29  */

30 public class XPathTool extends SAXDemo {
31
32     protected Document document;
33
34     protected XMLWriter xmlWriter;
35
36     protected boolean verbose;
37
38     public static void main(String JavaDoc[] args) {
39         run(new XPathTool(), args);
40     }
41
42     public XPathTool() {
43     }
44
45     public void run(String JavaDoc[] args) throws Exception JavaDoc {
46         if (args.length < 1) {
47             printUsage("{options} <xml file>");
48             return;
49         }
50
51         for (int i = 0, size = args.length; i < size; i++) {
52             String JavaDoc arg = args[i];
53             if (arg.startsWith("-")) {
54                 readOptions(arg);
55             } else {
56                 println("Parsing: " + arg);
57                 document = parse(arg);
58                 break;
59             }
60         }
61
62         xmlWriter = new XMLWriter(System.out, new OutputFormat(" ", true));
63         userLoop();
64     }
65
66     protected void userLoop() throws Exception JavaDoc {
67         println("Enter XPath expressions to evaluate or 'quit' to stop");
68
69         BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(
70                 System.in));
71
72         while (true) {
73             print("XPath> ");
74             String JavaDoc line = reader.readLine();
75             if (line == null) {
76                 break;
77             }
78             line = line.trim();
79             if (line.equalsIgnoreCase("quit")) {
80                 break;
81             }
82             evaluateCommand(line);
83         }
84
85         println("Bye");
86     }
87
88     protected void evaluateCommand(String JavaDoc xpath) throws Exception JavaDoc {
89         println("Results...");
90         Object JavaDoc results = document.selectObject(xpath);
91         printResult(results);
92         xmlWriter.flush();
93     }
94
95     protected void printResult(Object JavaDoc results) throws Exception JavaDoc {
96         if (results instanceof Node) {
97             Node node = (Node) results;
98             if (node instanceof Document) {
99                 Document document = (Document) node;
100                 println("Document: " + document.getName());
101             } else if (node instanceof Element) {
102                 Element element = (Element) node;
103                 xmlWriter.writeOpen(element);
104                 xmlWriter.println();
105             } else {
106                 xmlWriter.write(node);
107                 xmlWriter.println();
108             }
109         } else if (results instanceof List JavaDoc) {
110             List JavaDoc list = (List JavaDoc) results;
111             println("List of " + list.size() + " item(s)");
112             for (Iterator JavaDoc iter = list.iterator(); iter.hasNext();) {
113                 printResult(iter.next());
114             }
115         } else {
116             if (results == null) {
117                 println("null");
118             } else {
119                 println(results + " (" + results.getClass().getName() + ")");
120             }
121         }
122     }
123
124     protected void readOptions(String JavaDoc arg) {
125         if (arg.indexOf('v') >= 0) {
126             verbose = true;
127         }
128     }
129 }
130
131 /*
132  * Redistribution and use of this software and associated documentation
133  * ("Software"), with or without modification, are permitted provided that the
134  * following conditions are met:
135  *
136  * 1. Redistributions of source code must retain copyright statements and
137  * notices. Redistributions must also contain a copy of this document.
138  *
139  * 2. Redistributions in binary form must reproduce the above copyright notice,
140  * this list of conditions and the following disclaimer in the documentation
141  * and/or other materials provided with the distribution.
142  *
143  * 3. The name "DOM4J" must not be used to endorse or promote products derived
144  * from this Software without prior written permission of MetaStuff, Ltd. For
145  * written permission, please contact dom4j-info@metastuff.com.
146  *
147  * 4. Products derived from this Software may not be called "DOM4J" nor may
148  * "DOM4J" appear in their names without prior written permission of MetaStuff,
149  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
150  *
151  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
152  *
153  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
154  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
155  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
156  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
157  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
158  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
159  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
160  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
161  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
162  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
163  * POSSIBILITY OF SUCH DAMAGE.
164  *
165  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
166  *
167  * $Id: XPathTool.java,v 1.4 2005/01/29 14:52:57 maartenc Exp $
168  */

169
Popular Tags