KickJava   Java API By Example, From Geeks To Geeks.

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


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

9
10 package org.dom4j.samples;
11
12 import java.util.Iterator JavaDoc;
13 import java.util.List JavaDoc;
14
15 import org.dom4j.Document;
16 import org.dom4j.DocumentHelper;
17 import org.dom4j.XPath;
18
19 /**
20  * A utility program which performs XPath expressions on one or more XML files
21  * and outputs the matches. It is similar to the <code>grep</code> command on
22  * Unix but uses XPath expressions for matching
23  *
24  * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan </a>
25  * @version $Revision: 1.5 $
26  */

27 public class XPathGrep extends SAXDemo {
28
29     protected XPath xpath;
30
31     protected boolean verbose;
32
33     public static void main(String JavaDoc[] args) {
34         run(new XPathGrep(), args);
35     }
36
37     public XPathGrep() {
38     }
39
40     public void run(String JavaDoc[] args) throws Exception JavaDoc {
41         if (args.length < 2) {
42             printUsage("{options} <XPath expression> <xml file(s)>");
43             return;
44         }
45
46         for (int i = 0, size = args.length; i < size; i++) {
47             String JavaDoc arg = args[i];
48             if (arg.startsWith("-")) {
49                 readOptions(arg);
50             } else {
51                 if (xpath == null) {
52                     setXPath(arg);
53                 } else {
54                     Document document = parse(arg);
55                     process(document);
56                 }
57             }
58         }
59     }
60
61     public void setXPath(String JavaDoc xpathExpression) {
62         xpath = DocumentHelper.createXPath(xpathExpression);
63     }
64
65     protected void process(Document document) throws Exception JavaDoc {
66         // perform XPath
67
if (verbose) {
68             println("About to evalute: " + xpath);
69             println("Results:");
70         }
71
72         Object JavaDoc object = xpath.evaluate(document);
73
74         if (object instanceof List JavaDoc) {
75             List JavaDoc list = (List JavaDoc) object;
76             for (Iterator JavaDoc iter = list.iterator(); iter.hasNext();) {
77                 getXMLWriter().write(iter.next());
78                 getXMLWriter().println();
79             }
80             getXMLWriter().flush();
81         } else {
82             println((object != null) ? object.toString() : "null");
83         }
84     }
85
86     protected void readOptions(String JavaDoc arg) {
87         if (arg.indexOf('v') >= 0) {
88             verbose = true;
89         }
90     }
91 }
92
93 /*
94  * Redistribution and use of this software and associated documentation
95  * ("Software"), with or without modification, are permitted provided that the
96  * following conditions are met:
97  *
98  * 1. Redistributions of source code must retain copyright statements and
99  * notices. Redistributions must also contain a copy of this document.
100  *
101  * 2. Redistributions in binary form must reproduce the above copyright notice,
102  * this list of conditions and the following disclaimer in the documentation
103  * and/or other materials provided with the distribution.
104  *
105  * 3. The name "DOM4J" must not be used to endorse or promote products derived
106  * from this Software without prior written permission of MetaStuff, Ltd. For
107  * written permission, please contact dom4j-info@metastuff.com.
108  *
109  * 4. Products derived from this Software may not be called "DOM4J" nor may
110  * "DOM4J" appear in their names without prior written permission of MetaStuff,
111  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
112  *
113  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
114  *
115  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
116  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
117  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
118  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
119  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
120  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
121  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
122  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
123  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
124  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
125  * POSSIBILITY OF SUCH DAMAGE.
126  *
127  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
128  *
129  * $Id: XPathGrep.java,v 1.5 2005/01/29 14:52:57 maartenc Exp $
130  */

131
Popular Tags