KickJava   Java API By Example, From Geeks To Geeks.

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


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: XPathValueOf.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.DocumentHelper;
14 import org.dom4j.XPath;
15
16 /**
17  * A utility program which performs XPath expressions on one or more XML files
18  * and outputs the valueOf the XPath expression. It is similar to the
19  * <code>grep</code> command on Unix but uses XPath valueOf for matching
20  *
21  * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan </a>
22  * @version $Revision: 1.4 $
23  */

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

107
Popular Tags