KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.icl.saxon.expr;
2 import com.icl.saxon.*;
3 import com.icl.saxon.style.SAXONFunction;
4 import com.icl.saxon.om.NodeInfo;
5 import javax.xml.transform.TransformerException JavaDoc;
6
7
8 import java.util.*;
9
10
11 /**
12 * This class represents a call to a function defined in the stylesheet
13 */

14
15 public class StyleSheetFunctionCall extends Function {
16
17     private SAXONFunction function;
18     private Controller boundController = null;
19     private NodeInfo boundContextNode = null;
20     private int boundContextPosition = -1;
21     private int boundContextSize = -1;
22
23     /**
24     * Create the reference to the saxon:function element
25     */

26
27     public void setFunction(SAXONFunction f) {
28         function = f;
29     }
30
31     /**
32     * Get the name of the function.
33     * @return the name of the function, as used in XSL expressions, but excluding
34     * its namespace prefix
35     */

36
37     public String JavaDoc getName() {
38         return function.getAttribute("name"); // not quite as specified
39
}
40
41     /**
42     * Determine the data type of the expression, if possible
43     * @return Value.ANY (meaning not known in advance)
44     */

45
46     public int getDataType() {
47         return Value.ANY;
48     }
49
50
51     /**
52     * Simplify the function call
53     */

54
55     public Expression simplify() throws XPathException {
56         for (int i=0; i<getNumberOfArguments(); i++) {
57             argument[i] = argument[i].simplify();
58         }
59         return this;
60     }
61         
62
63     /**
64     * Determine which aspects of the context the expression depends on. The result is
65     * a bitwise-or'ed value composed from constants such as Context.VARIABLES and
66     * Context.CURRENT_NODE
67     */

68
69     public int getDependencies() {
70         
71         // we could do better than this by examining the XSLT code
72

73         int dep = Context.NO_DEPENDENCIES;
74         if (boundController==null) dep |= Context.CONTROLLER;
75         if (boundContextNode==null) dep |= Context.CONTEXT_NODE;
76         if (boundContextPosition==-1) dep |= Context.POSITION;
77         if (boundContextSize==-1) dep |= Context.LAST;
78         
79         for (int i=0; i<getNumberOfArguments(); i++) {
80             dep |= argument[i].getDependencies();
81         }
82         return dep;
83
84     }
85
86     /**
87     * Remove dependencies.
88     */

89
90     public Expression reduce(int dependencies, Context context)
91             throws XPathException {
92
93         StyleSheetFunctionCall nf = new StyleSheetFunctionCall();
94         nf.setFunction(function);
95         nf.setStaticContext(getStaticContext());
96         nf.boundController = boundController;
97         nf.boundContextNode = boundContextNode;
98         nf.boundContextPosition = boundContextPosition;
99         nf.boundContextSize = boundContextSize;
100
101         for (int a=0; a<getNumberOfArguments(); a++) {
102             nf.addArgument(argument[a].reduce(dependencies, context));
103         }
104        
105         if (boundController==null && (dependencies & Context.CONTROLLER) != 0) {
106             nf.boundController = context.getController();
107         }
108         if (boundContextNode==null && (dependencies & Context.CONTEXT_NODE) != 0) {
109             nf.boundContextNode = context.getContextNodeInfo();
110         }
111         if (boundContextPosition==-1 && (dependencies & Context.POSITION) != 0) {
112             nf.boundContextPosition = context.getContextPosition();
113         }
114         if (boundContextSize==-1 && (dependencies & Context.LAST) != 0) {
115             nf.boundContextSize = context.getLast();
116         }
117
118         return nf.simplify();
119     }
120
121     /**
122     * Evaluate the function
123     * @param context The context in which the function is to be evaluated
124     * @return a Value representing the result of the function. This must be of the data type
125     * corresponding to the result of getType().
126     * @throws XPathException if the function cannot be evaluated.
127     */

128
129     public Value evaluate(Context c) throws XPathException {
130         Context context = c.newContext();
131         if (boundController!=null) {
132             context.setController(boundController);
133         }
134         if (boundContextNode!=null) {
135             context.setCurrentNode(boundContextNode);
136             context.setContextNode(boundContextNode);
137         }
138         if (boundContextPosition!=-1) {
139             context.setPosition(boundContextPosition);
140         }
141         if (boundContextSize!=-1) {
142             context.setLast(boundContextSize);
143         }
144
145         ParameterSet ps = new ParameterSet();
146         for (int i=0; i<getNumberOfArguments(); i++) {
147             int param = function.getNthParameter(i);
148             if (param==-1) {
149                 throw new XPathException("Too many arguments");
150             }
151             ps.put(param, argument[i].evaluate(c));
152         }
153         try {
154             return function.call(ps, context);
155         } catch (TransformerException JavaDoc err) {
156             throw new XPathException(err);
157         }
158     }
159     
160 }
161
162 //
163
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
164
// you may not use this file except in compliance with the License. You may obtain a copy of the
165
// License at http://www.mozilla.org/MPL/
166
//
167
// Software distributed under the License is distributed on an "AS IS" basis,
168
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
169
// See the License for the specific language governing rights and limitations under the License.
170
//
171
// The Original Code is: all this file.
172
//
173
// The Initial Developer of the Original Code is
174
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
175
//
176
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
177
//
178
// Contributor(s): none.
179
//
180
Popular Tags