KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > xpath > XPathFunctionCall


1 package net.sf.saxon.xpath;
2 import net.sf.saxon.expr.*;
3 import net.sf.saxon.om.SequenceIterator;
4 import net.sf.saxon.om.ValueRepresentation;
5 import net.sf.saxon.trans.DynamicError;
6 import net.sf.saxon.trans.XPathException;
7 import net.sf.saxon.type.ItemType;
8 import net.sf.saxon.type.Type;
9 import net.sf.saxon.value.SequenceType;
10 import net.sf.saxon.value.Value;
11 import net.sf.saxon.Configuration;
12
13 import javax.xml.xpath.XPathFunction JavaDoc;
14 import javax.xml.xpath.XPathFunctionException JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.List JavaDoc;
17
18
19
20 /**
21 * This class is an expression that calls an external function supplied using the
22  * JAXP XPathFunction interface
23 */

24
25 public class XPathFunctionCall extends FunctionCall {
26
27     private XPathFunction JavaDoc function;
28     /**
29      * Default constructor
30      */

31
32     public XPathFunctionCall(XPathFunction JavaDoc function) {
33         this.function = function;
34     }
35
36     /**
37     * preEvaluate: this method suppresses compile-time evaluation by doing nothing
38     * (because the external function might have side-effects and might use the context)
39     */

40
41     public Expression preEvaluate(StaticContext env) {
42         return this;
43     }
44
45
46     /**
47     * Method called by the expression parser when all arguments have been supplied
48     */

49
50     public void checkArguments(StaticContext env) throws XPathException {
51     }
52
53
54     /**
55      * Determine which aspects of the context the expression depends on. XPath external
56      * functions are given no access to context information so they cannot have any
57      * dependencies on it.
58     */

59
60     public int getIntrinsicDependencies() {
61         return 0;
62     }
63
64
65     /**
66     * Evaluate the function. <br>
67     * @param context The context in which the function is to be evaluated
68     * @return a Value representing the result of the function.
69     * @throws XPathException if the function cannot be evaluated.
70     */

71
72     public SequenceIterator iterate(XPathContext context) throws XPathException {
73         ValueRepresentation[] argValues = new ValueRepresentation[argument.length];
74         for (int i=0; i<argValues.length; i++) {
75             argValues[i] = ExpressionTool.lazyEvaluate(argument[i], context, 10);
76         }
77         return call(argValues, context);
78     }
79
80
81     /**
82      * Call an extension function previously identified using the bind() method. A subclass
83      * can override this method.
84      * @param argValues The values of the arguments
85      * @return The value returned by the extension function
86      */

87
88     public SequenceIterator call(ValueRepresentation[] argValues, XPathContext context) throws XPathException {
89         List JavaDoc convertedArgs = new ArrayList JavaDoc(argValues.length);
90         for (int i=0; i<argValues.length; i++) {
91             convertedArgs.add(Value.asValue(argValues[i]).convertToJava(Object JavaDoc.class, context));
92         }
93         try {
94             Object JavaDoc result = function.evaluate(convertedArgs);
95             Configuration config = context.getController().getConfiguration();
96             return Value.convertJavaObjectToXPath(result, SequenceType.ANY_SEQUENCE, config).iterate(context);
97         } catch (XPathFunctionException JavaDoc e) {
98             throw new DynamicError(e);
99         }
100     }
101
102     /**
103      * Determine the data type of the expression, if possible. All expressions return
104      * sequences, in general; this method determines the type of the items within the
105      * sequence, assuming that (a) this is known in advance, and (b) it is the same for
106      * all items in the sequence.
107      *
108      * <p>This method will always return a result, though it may be the best approximation
109      * that is available at the time.</p>
110      *
111      * @return the item type
112      */

113
114     public ItemType getItemType() {
115         return Type.ITEM_TYPE;
116     }
117
118     /**
119      * Determine the cardinality of the result
120      * @return ZERO_OR_MORE (we don't know)
121      */

122     public int computeCardinality() {
123         return StaticProperty.ALLOWS_ZERO_OR_MORE;
124     }
125
126
127 }
128
129 //
130
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
131
// you may not use this file except in compliance with the License. You may obtain a copy of the
132
// License at http://www.mozilla.org/MPL/
133
//
134
// Software distributed under the License is distributed on an "AS IS" basis,
135
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
136
// See the License for the specific language governing rights and limitations under the License.
137
//
138
// The Original Code is: all this file.
139
//
140
// The Initial Developer of the Original Code is Michael H. Kay.
141
//
142
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
143
//
144
// Contributor(s): Gunther Schadow (changes to allow access to public fields; also wrapping
145
// of extensions and mapping of null to empty sequence).
146
//
147
Popular Tags