KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > sxpath > XPathEvaluator


1 package net.sf.saxon.sxpath;
2 import net.sf.saxon.Configuration;
3 import net.sf.saxon.event.Builder;
4 import net.sf.saxon.event.Stripper;
5 import net.sf.saxon.expr.Expression;
6 import net.sf.saxon.expr.ExpressionTool;
7 import net.sf.saxon.instruct.SlotManager;
8 import net.sf.saxon.om.AllElementStripper;
9 import net.sf.saxon.om.NamePool;
10 import net.sf.saxon.om.NamespaceResolver;
11 import net.sf.saxon.om.NodeInfo;
12 import net.sf.saxon.trans.IndependentContext;
13 import net.sf.saxon.trans.XPathException;
14 import net.sf.saxon.type.Type;
15
16 import javax.xml.transform.Source JavaDoc;
17 import javax.xml.transform.stream.StreamSource JavaDoc;
18 import java.io.File JavaDoc;
19 import java.util.List JavaDoc;
20
21 /**
22  * This is a cut-down version of the XPathEvaluator in the net.sf.saxon.xpath package. It provides
23  * much of the same functionality, but without any dependencies on the JAXP 1.3 interfaces, which
24  * are not available in JDK 1.4. The main restrictions are that it does not support mechanisms for
25  * defining variables or functions.
26   *
27   * @author Michael H. Kay
28   */

29
30 public class XPathEvaluator {
31
32     private IndependentContext staticContext;
33     private boolean stripSpace = false;
34
35     /**
36     * Default constructor. Creates an XPathEvaluator with a default configuration and name pool.
37     */

38
39     public XPathEvaluator() {
40         this(new Configuration());
41     }
42
43     /**
44      * Construct an XPathEvaluator with a specified configuration.
45      * @param config the configuration to be used
46      */

47     public XPathEvaluator(Configuration config) {
48         staticContext = new IndependentContext(config);
49     }
50
51     /**
52      * Get the Configuration in use
53      */

54
55     public Configuration getConfiguration() {
56         return staticContext.getConfiguration();
57     }
58
59     /**
60     * Indicate whether all whitespace text nodes in the source document are to be
61     * removed.
62     * @param strip True if all whitespace text nodes are to be stripped from the source document,
63     * false otherwise. The default if the method is not called is false.
64     */

65
66     public void setStripSpace(boolean strip) {
67         stripSpace = strip;
68     }
69
70     /**
71     * Build a source document.
72      * @param source a JAXP Source object. This may be any implementation of Source that Saxon recognizes:
73      * not only the standard kinds of source such as StreamSource, SAXSource, and DOMSource, but also for
74      * example a JDOM or XOM DocumentWrapper.
75      * @return the NodeInfo representing the root of the constructed tree.
76      * @throws XPathException if, for example, XML parsing fails.
77     */

78
79     public NodeInfo build(Source JavaDoc source) throws XPathException {
80         NamePool pool;
81         if (source instanceof NodeInfo) {
82             pool = ((NodeInfo)source).getNamePool();
83         } else {
84             pool = NamePool.getDefaultNamePool();
85         }
86         Stripper stripper = null;
87         if (stripSpace) {
88             stripper = AllElementStripper.getInstance();
89         }
90         Configuration config = new Configuration();
91         config.setNamePool(pool);
92         return Builder.build(source, stripper, config);
93     }
94
95     /**
96     * Set the static context for compiling XPath expressions. This provides control over the
97     * environment in which the expression is compiled, for example it allows namespace prefixes to
98     * be declared, variables to be bound and functions to be defined. For most purposes, the static
99     * context can be defined by providing and tailoring an instance of the IndependentContext class.
100     * Until this method is called, a default static context is used, in which no namespaces are defined
101     * other than the standard ones (xml, xslt, and saxon), and no variables or functions (other than the
102     * core XPath functions) are available.
103     */

104
105     public void setStaticContext(IndependentContext context) {
106         staticContext = context;
107     }
108
109     /**
110     * Get the current static context. This will always return a value; if no static context has been
111      * supplied by the user, the system creates its own.
112     */

113
114     public IndependentContext getStaticContext() {
115         return staticContext;
116     }
117
118     /**
119     * Prepare an XPath expression for subsequent evaluation.
120     * @param expression The XPath expression to be evaluated, supplied as a string.
121     * @return an XPathExpression object representing the prepared expression
122     * @throws XPathException if the syntax of the expression is wrong, or if it references namespaces,
123     * variables, or functions that have not been declared.
124     */

125
126     public XPathExpression createExpression(String JavaDoc expression) throws XPathException {
127         Expression exp = ExpressionTool.make(expression, staticContext,0,-1,1);
128         exp = exp.typeCheck(staticContext, Type.ITEM_TYPE);
129         SlotManager map = staticContext.getConfiguration().makeSlotManager();
130         ExpressionTool.allocateSlots(exp, 0, map);
131         XPathExpression xpe = new XPathExpression(this, exp);
132         xpe.setStackFrameMap(map);
133         return xpe;
134     }
135
136     /**
137      * Set the external namespace resolver to be used. This overrides any namespaces declared directly
138      * using declareNamespace on the staticContext object
139      * @param namespaceContext The namespace context
140      */

141
142     public void setNamespaceResolver(NamespaceResolver namespaceContext) {
143         staticContext.setNamespaceResolver(namespaceContext);
144     }
145
146     /**
147      * Get the external namespace resolver, if one has been set using {@link #setNamespaceResolver}
148      * @return the namespace context if set, or null otherwise
149      */

150
151     public NamespaceResolver getNamespaceResolver() {
152         return staticContext.getNamespaceResolver();
153     }
154
155     /**
156      * A simple command-line interface for the XPathEvaluator (not documented).
157      * First parameter is the filename containing the source document, second
158      * parameter is the XPath expression.
159      */

160
161     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
162         if (args.length != 2) {
163             System.err.println("format: java XPathEvaluator source.xml \"expression\"");
164             return;
165         }
166         XPathEvaluator xpe = new XPathEvaluator();
167         XPathExpression exp = xpe.createExpression(args[1]);
168         List JavaDoc results = exp.evaluate(new StreamSource JavaDoc(new File JavaDoc(args[0])));
169         for (int i = 0; i < results.size(); i++) {
170             Object JavaDoc o = results.get(i);
171             System.err.println(o);
172         }
173     }
174
175 }
176
177 //
178
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
179
// you may not use this file except in compliance with the License. You may obtain a copy of the
180
// License at http://www.mozilla.org/MPL/
181
//
182
// Software distributed under the License is distributed on an "AS IS" basis,
183
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
184
// See the License for the specific language governing rights and limitations under the License.
185
//
186
// The Original Code is: all this file.
187
//
188
// The Initial Developer of the Original Code is Michael H. Kay
189
//
190
// Contributor(s):
191
//
192
Popular Tags