View Javadoc

1   /*
2    * Copyright (c) 2002 Peter Antman, Teknik i Media  <peter.antman@tim.se>
3    *
4    * $Id: XPathUtil.java,v 1.1.1.1 2004/05/19 12:07:31 pra Exp $
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version
10   * 
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */
20  package org.backsource.utils.xml;
21  import java.io.File;
22  import java.util.List;
23  import java.util.Iterator;
24  
25  import org.w3c.dom.Node;
26  import org.w3c.dom.Document;
27  import org.jaxen.XPath;
28  import org.jaxen.JaxenException;
29  import org.jaxen.dom.DOMXPath;
30  /***
31   * Util to make it easy to get nodes from a document with the help of an XPath expression.
32   *
33   *
34   * @author <a href="mailto:pra@tim.se">Peter Antman</a>
35   * @version $Revision: 1.1.1.1 $
36   */
37  
38  public class XPathUtil{
39     private XPathUtil (){
40        
41     }
42  
43     /***
44      * Get the DOM specifix XPath engine.
45      */
46     protected static XPath getDOMXPath(String xpath)throws XmlException {
47        // Make cahcable
48        try {
49           return new DOMXPath(xpath);     
50        } catch (JaxenException e) {
51           throw new XmlException("Could not construct DOM XPath:" + e,e);
52        } // end of try-catch
53     }
54     
55  
56     /***
57      * <p>Select a single node from node based on xpath.
58      *
59      * @returns a single node even if the xpath hits more than one node. Returns null if no node was hitted.
60      */
61     public static Node selectSingleNode(Node xml, String xpath) throws XmlException {
62        try {
63           return (Node)getDOMXPath(xpath).selectSingleNode(xml);     
64        } catch (JaxenException e) {
65           throw new XmlException("Could not select single node:" + e,e);
66        } // end of try-catch
67     }
68     
69     
70     /***
71      * <p>Select all nodes from node based on xpath.
72      *
73      * @returns the number of nodes the xpath hits.
74      */
75     public static List selectNodes(Node xml, String xpath)throws XmlException {
76        try {
77           return getDOMXPath(xpath).selectNodes(xml);     
78        } catch (JaxenException e) {
79           throw new XmlException("Could not select nodes:" + e,e);
80        } // end of try-catch
81     }
82  
83  
84     public static void main(String[] args){
85        if ( args.length < 2) {
86           System.out.println("XPathUtil file.xml XPATHEXPR");
87           System.exit(0);
88        } // end of if ()
89        try {
90           String file = args[0];
91           String xpath = args[1];
92           
93           Document doc = DocumentUtil.getDocument( new File(file) );
94           /*
95           XPath x = new DOMXPath(xpath);
96           System.out.println(x.booleanValueOf(doc));
97           */
98           Iterator nodes = selectNodes(doc,xpath).iterator();
99           while ( nodes.hasNext()) {
100             Object o = nodes.next();
101             // System.out.println(o);
102             if ( o instanceof Node) {
103                System.out.println("Found node:" + DocumentUtil.getXml( (Node)o, true,null,true));
104             } else {
105                System.out.println("Found " + o +" ( " + o.getClass() +")");
106 
107             } // end of else
108             
109          } // end of while ()
110          
111       } catch (Exception e) {
112          System.out.println("Could not run xpath " + e);
113          e.printStackTrace();
114       } // end of try-catch
115       
116    }
117 } // XpathUtil