KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > query > GlobalVariableDefinition


1 package net.sf.saxon.query;
2
3 import net.sf.saxon.expr.*;
4 import net.sf.saxon.instruct.GeneralVariable;
5 import net.sf.saxon.instruct.GlobalParam;
6 import net.sf.saxon.instruct.GlobalVariable;
7 import net.sf.saxon.instruct.SlotManager;
8 import net.sf.saxon.om.NamePool;
9 import net.sf.saxon.trans.XPathException;
10 import net.sf.saxon.type.ItemType;
11 import net.sf.saxon.type.Type;
12 import net.sf.saxon.value.SequenceType;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 /**
19  * Class to hold compile-time information about an XQuery global variable
20  * or parameter
21  */

22
23 public class GlobalVariableDefinition implements VariableDeclaration, Declaration {
24
25     protected List JavaDoc references = new ArrayList JavaDoc(10);
26     private SequenceType requiredType;
27     private Expression value;
28     private int nameCode;
29     private boolean isParameter;
30     private String JavaDoc variableName;
31     private String JavaDoc systemId; // identifies the module where the variable declaration occurred
32
private int lineNumber; // identifies the line number of the variable declaration
33

34     /**
35      * Set the required type of the variable
36      * @param type the declared type, from the "as" clause if present
37      */

38
39     public void setRequiredType(SequenceType type) {
40         requiredType = type;
41     }
42
43     /**
44      * Get the required type of the variable
45      * @return the declared type, from the "as" clause if present
46      */

47
48     public SequenceType getRequiredType() {
49         return requiredType;
50     }
51
52     /**
53      * Set the variable name
54      * @param nameCode the variable name, expressed as a NamePool name code
55      */

56     public void setNameCode(int nameCode) {
57         this.nameCode = nameCode;
58     }
59
60     /**
61      * Get the variable name
62      * @return the variable name, expressed as a NamePool name code
63      */

64     public int getNameCode() {
65         return nameCode;
66     }
67     /**
68      * Set the line number where the variable declaration appears in the source
69      * @param lineNumber the line number
70      */

71     public void setLineNumber(int lineNumber) {
72         this.lineNumber = lineNumber;
73     }
74
75     /**
76      * Get the line number where the declaration appears
77      */

78
79     public int getLineNumber() {
80         return lineNumber;
81     }
82
83     /**
84      * Get column number
85      * @return -1 always
86      */

87
88     public int getColumnNumber() {
89         return -1;
90     }
91
92     /**
93      * Get public identifier
94      * @return null always
95      */

96
97     public String JavaDoc getPublicId() {
98         return null;
99     }
100
101     /**
102      * Set the system ID of the module where the variable declaration appears
103      */

104     public void setSystemId(String JavaDoc systemId) {
105         this.systemId = systemId;
106     }
107
108     /**
109      * Get the system ID of the module containing the variable declaration
110      */

111
112     public String JavaDoc getSystemId() {
113         return systemId;
114     }
115
116     /**
117      * Get the name of the variable
118      * @return the variable name, as a lexical QName
119      */

120     public String JavaDoc getVariableName() {
121         return variableName;
122     }
123
124     /**
125      * Set the variable name
126      * @param variableName the variable name, as a lexical QName
127      */

128     public void setVariableName(String JavaDoc variableName) {
129         this.variableName = variableName;
130     }
131
132     /**
133      * Set the expression used to define the value of the variable
134      * @param val the initializing expression
135      */

136     public void setValueExpression(Expression val) {
137         this.value = val;
138     }
139
140     /**
141      * Indicate whether this global variable is a "parameter" (an external variable, in XQuery terminology)
142      * @param b true if this variable is external
143      */

144     public void setIsParameter(boolean b) {
145         isParameter = b;
146     }
147
148     /**
149      * Register a variable reference that refers to this global variable
150      * @param ref the variable reference
151      */

152     public void registerReference(BindingReference ref) {
153         references.add(ref);
154     }
155
156     /**
157      * Iterate over the references to this variable
158      */

159
160     public Iterator JavaDoc iterateReferences() {
161         return references.iterator();
162     }
163
164
165     /**
166      * Create a compiled representation of this global variable
167      * @param env the static context for the query module
168      * @param slot the slot number allocated to this variable
169      * @return the compiled representation
170      * @throws XPathException if compile-time errors are found.
171      */

172
173     public GlobalVariable compile(StaticQueryContext env, int slot) throws XPathException {
174
175         GlobalVariable var;
176         if (isParameter) {
177             var = new GlobalParam();
178             var.setExecutable(env.getExecutable());
179             var.setRequiredParam(value==null);
180         } else {
181             var = new GlobalVariable();
182             var.setExecutable(env.getExecutable());
183         }
184
185         var.setSelectExpression(value);
186         var.setNameCode(nameCode);
187         var.setRequiredType(requiredType);
188         var.setVariableName(variableName);
189         var.setSlotNumber(slot);
190
191         int loc = var.getExecutable().getLocationMap().allocateLocationId(systemId, lineNumber);
192         var.setLocationId(loc);
193
194         Iterator JavaDoc iter = references.iterator();
195         while (iter.hasNext()) {
196             BindingReference binding = (BindingReference)iter.next();
197             binding.setStaticType(requiredType, null, 0);
198             binding.fixup(var);
199         }
200         env.getExecutable().registerGlobalVariable(var);
201         return var;
202     }
203
204     /**
205      * Type check the compiled representation of this global variable
206      * @param env the static context for the query module
207      * @throws XPathException if compile-time errors are found.
208      */

209
210     public static void typeCheck(StaticQueryContext env, GeneralVariable var) throws XPathException {
211
212         Expression value = var.getSelectExpression();
213         if (value != null) {
214             if (value instanceof ComputedExpression) {
215                 ((ComputedExpression)value).setParentExpression(var);
216             }
217             RoleLocator role = new RoleLocator(
218                     RoleLocator.VARIABLE, env.getNamePool().getDisplayName(var.getNameCode()), 0, null);
219             Expression value2 = TypeChecker.strictTypeCheck(
220                                     value.simplify(env).typeCheck(env, Type.ITEM_TYPE),
221                                     var.getRequiredType(),
222                                     role, env);
223             value2 = value2.optimize(env.getConfiguration().getOptimizer(), env, Type.ITEM_TYPE);
224             var.setSelectExpression(value2);
225             if (value2 instanceof ComputedExpression) {
226                 ((ComputedExpression)value2).setParentExpression(var);
227             }
228             // the value expression may declare local variables
229
SlotManager map = env.getConfiguration().makeSlotManager();
230             int slots = ExpressionTool.allocateSlots(value2, 0, map);
231             if (slots > 0) {
232                 ((GlobalVariable)var).setContainsLocals(map);
233             }
234         }
235
236         if (var.getRequiredType() == SequenceType.ANY_SEQUENCE && !(var instanceof GlobalParam)) {
237             // no type was declared; try to deduce a type from the value
238
try {
239                 ItemType itemType = value.getItemType();
240                 int cardinality = value.getCardinality();
241                 var.setRequiredType(SequenceType.makeSequenceType(itemType, cardinality));
242             } catch (Exception JavaDoc err) {
243                 // exceptions can happen because references to variables and functions are still unbound
244
}
245         }
246     }
247
248
249     /**
250      * Produce diagnostic output showing the compiled and optimized expression tree for a function
251      * @param pool the namepool to be used
252      */

253     public void explain(NamePool pool) {
254         System.err.println("declare variable " + pool.getDisplayName(nameCode) + " := ");
255         if (value != null) {
256             value.display(4, pool, System.err);
257         }
258         System.err.println(";");
259     }
260 }
261
262
263 //
264
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
265
// you may not use this file except in compliance with the License. You may obtain a copy of the
266
// License at http://www.mozilla.org/MPL/
267
//
268
// Software distributed under the License is distributed on an "AS IS" basis,
269
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
270
// See the License for the specific language governing rights and limitations under the License.
271
//
272
// The Original Code is: all this file.
273
//
274
// The Initial Developer of the Original Code is Michael H. Kay
275
//
276
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
277
//
278
// Contributor(s): none
279
//
Popular Tags