KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > xml > XPathFind


1 /*
2  * Evaluate XPath expressions on an XML file
3  * Copyright (C) 2004, University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs.xml;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.dom4j.Attribute;
26 import org.dom4j.Document;
27 import org.dom4j.Element;
28 import org.dom4j.Node;
29 import org.dom4j.io.SAXReader;
30
31 /**
32  * Find nodes in a dom4j tree that match a particular
33  * XPath expression. The main() driver prints out information
34  * about matching nodes in an XML document.
35  *
36  * <p> For example, to find the list of non-disabled detectors
37  * in a FindBugs plugin descriptor, you can use the expression
38  * <blockquote>
39  * <code>/FindbugsPlugin/Detector[boolean(@disabled)=false()]/@class</code>
40  * </blockquote>
41  *
42  * @author David Hovemeyer
43  */

44 public abstract class XPathFind {
45     private Document document;
46
47     public XPathFind(Document document) {
48         this.document = document;
49     }
50
51     public void find(String JavaDoc xpath) {
52         for (Node node : (List JavaDoc<Node>) document.selectNodes(xpath)) {
53             match(node);
54         }
55     }
56
57     protected abstract void match(Node node);
58
59     public static void main(String JavaDoc[] argv) throws Exception JavaDoc {
60         if (argv.length != 2) {
61             System.err.println("Usage: " + XPathFind.class.getName() +
62                 ": <filename> <xpath expression>");
63             System.exit(1);
64         }
65
66         String JavaDoc fileName = argv[0];
67         String JavaDoc xpath = argv[1];
68
69         SAXReader reader = new SAXReader();
70         Document document = reader.read(fileName);
71
72         XPathFind finder = new XPathFind(document) {
73             @Override JavaDoc
74             protected void match(Node node) {
75                 //System.out.println(node.toString());
76
if (node instanceof Element) {
77                     Element element = (Element) node;
78                     System.out.println("Element: " + element.getQualifiedName());
79                     System.out.println("\tText: " + element.getText());
80                     System.out.println("\tAttributes:");
81                     for (Iterator JavaDoc<Attribute> i = element.attributeIterator(); i.hasNext(); ) {
82                         Attribute attribute = i.next();
83                         System.out.println("\t\t" + attribute.getName() + "=" + attribute.getValue());
84                     }
85                 } else if (node instanceof Attribute) {
86                     Attribute attribute = (Attribute) node;
87                     System.out.println("Attribute: " + attribute.getName() + "=" + attribute.getValue());
88                 }
89             }
90         };
91
92         finder.find(xpath);
93     }
94 }
95
96 // vim:ts=4
97
Popular Tags