KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > expr > Expression


1 package com.icl.saxon.expr;
2 import com.icl.saxon.*;
3 import com.icl.saxon.om.NodeEnumeration;
4 import com.icl.saxon.functions.*;
5 import com.icl.saxon.output.Outputter;
6 import javax.xml.transform.TransformerException JavaDoc;
7
8
9 /**
10 * This class serves two purposes: it is an abstract superclass for different kinds of XPath expression,
11 * and it contains a static method to invoke the expression parser
12 */

13
14 public abstract class Expression {
15
16     protected StaticContext staticContext;
17
18     /**
19     * Parse an expression
20     * @param expression The expression (as a character string)
21     * @param env An object giving information about the compile-time context of the expression
22     * @return an object of type Expression
23     */

24
25     public static Expression make(String JavaDoc expression, StaticContext env) throws XPathException {
26         try {
27             Expression exp = (new ExpressionParser()).parse(expression, env).simplify();
28             exp.staticContext = env;
29             return exp;
30         } catch (XPathException err) {
31             if (env.forwardsCompatibleModeIsEnabled()) {
32                 return new ErrorExpression(err);
33             } else {
34                 throw err;
35             }
36         }
37     }
38
39     /**
40     * Simplify an expression. Default implementation does nothing.
41     * @return the simplified expression
42     */

43
44     public Expression simplify() throws XPathException {
45         return this;
46     };
47
48     /**
49     * Set the static context used when the expression was parsed
50     */

51
52     public final void setStaticContext(StaticContext sc) {
53         staticContext = sc;
54     }
55
56     /**
57     * Determine the static context used when the expression was parsed
58     */

59
60     public final StaticContext getStaticContext() {
61         return staticContext;
62     }
63
64     /**
65     * Determine whether the expression contains any references to variables
66     * @return true if so
67     */

68
69     public boolean containsReferences() throws XPathException {
70         return (getDependencies() & Context.VARIABLES) != 0;
71     }
72
73     /**
74     * Evaluate an expression.
75     * @param context The context in which the expression is to be evaluated
76     * @return the value of the expression, evaluated in the current context
77     */

78
79     public abstract Value evaluate(Context context) throws XPathException;
80
81     /**
82     * Evaluate an expression as a Boolean.<br>
83     * The result of x.evaluateAsBoolean(c) must be equivalent to x.evaluate(c).asBoolean();
84     * but optimisations are possible when it is known that a boolean result is required,
85     * especially in the case of a NodeSet.
86     * @param context The context in which the expression is to be evaluated
87     * @return the value of the expression, evaluated in the current context
88     */

89
90     public boolean evaluateAsBoolean(Context context) throws XPathException {
91         return evaluate(context).asBoolean();
92     }
93
94     /**
95     * Evaluate an expression as a Number.<br>
96     * The result of x.evaluateAsNumber(c) must be equivalent to x.evaluate(c).asNumber();
97     * but optimisations are possible when it is known that a numeric result is required,
98     * especially in the case of a NodeSet.
99     * @param context The context in which the expression is to be evaluated
100     * @return the value of the expression, evaluated in the current context
101     */

102
103     public double evaluateAsNumber(Context context) throws XPathException {
104         return evaluate(context).asNumber();
105     }
106
107     /**
108     * Evaluate an expression as a String.<br>
109     * The result of x.evaluateAsString(c) must be equivalent to x.evaluate(c).asString();
110     * but optimisations are possible when it is known that a string result is required,
111     * especially in the case of a NodeSet.
112     * @param context The context in which the expression is to be evaluated
113     * @return the value of the expression, evaluated in the current context
114     */

115
116     public String JavaDoc evaluateAsString(Context context) throws XPathException {
117         return evaluate(context).asString();
118     }
119
120     /**
121     * Evaluate an expression as a String and write the result to the
122     * specified outputter.<br>
123     * @param out The required outputter
124     * @param context The context in which the expression is to be evaluated
125     */

126
127     public void outputStringValue(Outputter out, Context context) throws TransformerException JavaDoc {
128         out.writeContent(evaluateAsString(context));
129     }
130
131     /**
132     * Evaluate an expression as a NodeSet.<br>
133     * @param context The context in which the expression is to be evaluated
134     * @return the value of the expression, evaluated in the current context. Note that
135     * the result is not necessarily in document order; to get it into document order,
136     * call sort() on the result.
137     * @throws XPathException when the expression does not return a nodeset.
138     */

139
140     public NodeSetValue evaluateAsNodeSet(Context context) throws XPathException {
141         // Default implementation: see also NodeSetExpression
142
Value val = evaluate(context);
143         if (val instanceof NodeSetValue)
144             return ((NodeSetValue)val);
145         throw new XPathException("The value is not a node-set");
146     }
147
148     /**
149     * Return an enumeration of nodes in a nodeset.
150     * @param context The context in which the expression is to be evaluated
151     * @param sorted Indicates whether the nodes are required in document order. If
152     * this is false, they may come in any order, but there will be no duplicates.
153     * @throws XPathException when the expression does not return a nodeset.
154     */

155
156     public NodeEnumeration enumerate(Context context, boolean sorted) throws XPathException {
157         // default implementation: see also NodeSetExpression
158
Value val = evaluate(context);
159         if (val instanceof NodeSetValue) {
160             if (sorted) {
161                 ((NodeSetValue)val).sort();
162             }
163             NodeEnumeration z = ((NodeSetValue)val).enumerate();
164             return z;
165         }
166         throw new XPathException("The value is not a node-set");
167     }
168
169     /**
170     * Determine the data type of the expression, if possible
171     * @return one of the values Value.STRING, Value.BOOLEAN, Value.NUMBER, Value.NODESET,
172     * Value.FRAGMENT, or Value.ANY (meaning not known in advance)
173     */

174     
175     public abstract int getDataType();
176
177     /**
178     * Determine, in the case of an expression whose data type is Value.NODESET,
179     * whether all the nodes in the node-set are guaranteed to come from the same
180     * document as the context node. Used for optimization.
181     */

182     
183     public boolean isContextDocumentNodeSet() {
184         return false;
185     }
186
187     /**
188     * Determine whether the expression uses the current() function. This is an error if the
189     * expression is within a pattern
190     */

191
192     public boolean usesCurrent() {
193         return (getDependencies() & Context.CURRENT_NODE) != 0;
194     }
195
196     /**
197     * Determine which aspects of the context the expression depends on. The result is
198     * a bitwise-or'ed value composed from constants such as Context.VARIABLES and
199     * Context.CURRENT_NODE
200     */

201
202     public abstract int getDependencies();
203
204     /**
205     * Perform a partial evaluation of the expression, by eliminating specified dependencies
206     * on the context.
207     * @param dependencies The dependencies to be removed, e.g. Context.VARIABLES
208     * @param context The context to be used for the partial evaluation
209     * @return a new expression (or Value) that does not have any of the specified dependencies
210     */

211
212     public abstract Expression reduce(int dependencies, Context context) throws XPathException;
213
214     /**
215     * Diagnostic print of expression structure
216     */

217     
218     public abstract void display(int level);
219     
220     /**
221     * Construct indent string, for diagnostic output
222     */

223     
224     protected static String JavaDoc indent(int level) {
225         String JavaDoc s = "";
226         for (int i=0; i<level; i++) {
227             s += " ";
228         }
229         return s;
230     }
231 }
232
233 //
234
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
235
// you may not use this file except in compliance with the License. You may obtain a copy of the
236
// License at http://www.mozilla.org/MPL/
237
//
238
// Software distributed under the License is distributed on an "AS IS" basis,
239
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
240
// See the License for the specific language governing rights and limitations under the License.
241
//
242
// The Original Code is: all this file.
243
//
244
// The Initial Developer of the Original Code is
245
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
246
//
247
// The line marked PB-SYNC is by Peter Bryant (pbryant@bigfoot.com). All Rights Reserved.
248
//
249
// Contributor(s): Michael Kay, Peter Bryant
250
//
251
Popular Tags