KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > anakia > XPathTool


1 package org.apache.velocity.anakia;
2
3 /*
4  * Copyright 2001,2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.util.List JavaDoc;
20
21 import org.jdom.Document;
22 import org.jdom.Element;
23
24 /**
25  * This class adds an entrypoint into XPath functionality,
26  * for Anakia.
27  * <p>
28  * All methods take a string XPath specification, along with
29  * a context, and produces a resulting java.util.List.
30  * <p>
31  * The W3C XPath Specification (http://www.w3.org/TR/xpath) refers
32  * to NodeSets repeatedly, but this implementation simply uses
33  * java.util.List to hold all Nodes. A 'Node' is any object in
34  * a JDOM object tree, such as an org.jdom.Element, org.jdom.Document,
35  * or org.jdom.Attribute.
36  * <p>
37  * To use it in Velocity, do this:
38  * <p>
39  * <pre>
40  * #set $authors = $xpath.applyTo("document/author", $root)
41  * #foreach ($author in $authors)
42  * $author.getValue()
43  * #end
44  * #set $chapterTitles = $xpath.applyTo("document/chapter/@title", $root)
45  * #foreach ($title in $chapterTitles)
46  * $title.getValue()
47  * #end
48  * </pre>
49  * <p>
50  * In newer Anakia builds, this class is obsoleted in favor of calling
51  * <code>selectNodes()</code> on the element directly:
52  * <pre>
53  * #set $authors = $root.selectNodes("document/author")
54  * #foreach ($author in $authors)
55  * $author.getValue()
56  * #end
57  * #set $chapterTitles = $root.selectNodes("document/chapter/@title")
58  * #foreach ($title in $chapterTitles)
59  * $title.getValue()
60  * #end
61  * </pre>
62  * <p>
63  *
64  * @author <a HREF="mailto:bob@werken.com">bob mcwhirter</a>
65  * @author <a HREF="mailto:jon@latchkey.com">Jon S. Stevens</a>
66  * @author <a HREF="mailto:szegedia@freemail.hu">Attila Szegedi</a>
67  * @version $Id: XPathTool.java,v 1.13.4.1 2004/03/03 23:22:04 geirm Exp $
68  */

69 public class XPathTool
70 {
71     /**
72      * Constructor does nothing, as this is mostly
73      * just objectified static methods
74      */

75     public XPathTool()
76     {
77         // RuntimeSingleton.info("XPathTool::XPathTool()");
78
// intentionally left blank
79
}
80
81     /**
82      * Apply an XPath to a JDOM Document
83      *
84      * @param xpathSpec The XPath to apply
85      * @param doc The Document context
86      *
87      * @return A list of selected nodes
88      */

89     public NodeList applyTo(String JavaDoc xpathSpec,
90                         Document doc)
91     {
92         //RuntimeSingleton.info("XPathTool::applyTo(String, Document)");
93
return new NodeList(XPathCache.getXPath(xpathSpec).applyTo( doc ), false);
94     }
95
96     /**
97      * Apply an XPath to a JDOM Element
98      *
99      * @param xpathSpec The XPath to apply
100      * @param doc The Element context
101      *
102      * @return A list of selected nodes
103      */

104     public NodeList applyTo(String JavaDoc xpathSpec,
105                         Element elem)
106     {
107         //RuntimeSingleton.info("XPathTool::applyTo(String, Element)");
108
return new NodeList(XPathCache.getXPath(xpathSpec).applyTo( elem ), false);
109     }
110
111     /**
112      * Apply an XPath to a nodeset
113      *
114      * @param xpathSpec The XPath to apply
115      * @param doc The nodeset context
116      *
117      * @return A list of selected nodes
118      */

119     public NodeList applyTo(String JavaDoc xpathSpec,
120                         List JavaDoc nodeSet)
121     {
122         //RuntimeSingleton.info("XPathTool::applyTo(String, List)");
123
return new NodeList(XPathCache.getXPath(xpathSpec).applyTo( nodeSet ), false);
124     }
125 }
126
127
128
129
Popular Tags