KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > style > XSLVariable


1 package net.sf.saxon.style;
2 import net.sf.saxon.expr.ComputedExpression;
3 import net.sf.saxon.expr.Expression;
4 import net.sf.saxon.expr.ExpressionTool;
5 import net.sf.saxon.expr.StaticProperty;
6 import net.sf.saxon.instruct.Executable;
7 import net.sf.saxon.instruct.GeneralVariable;
8 import net.sf.saxon.instruct.GlobalVariable;
9 import net.sf.saxon.instruct.LocalVariable;
10 import net.sf.saxon.pattern.NodeKindTest;
11 import net.sf.saxon.trans.XPathException;
12 import net.sf.saxon.value.EmptySequence;
13 import net.sf.saxon.value.SequenceType;
14
15 /**
16 * Handler for xsl:variable elements in stylesheet. <br>
17 * The xsl:variable element has mandatory attribute name and optional attribute select
18 */

19
20 public class XSLVariable extends XSLVariableDeclaration {
21
22     private int state = 0;
23             // 0 = before prepareAttributes()
24
// 1 = during prepareAttributes()
25
// 2 = after prepareAttributes()
26

27     public void prepareAttributes() throws XPathException {
28         if (state==2) return;
29         if (state==1) {
30             compileError("Circular reference to variable", "XTDE0640");
31         }
32         state = 1;
33         //System.err.println("Prepare attributes of $" + getVariableName());
34
super.prepareAttributes();
35         state = 2;
36     }
37
38     /**
39     * Determine whether this node is an instruction.
40     * @return true - it is an instruction (well, it can be, anyway)
41     */

42
43     public boolean isInstruction() {
44         return true;
45     }
46
47     /**
48     * Get the static type of the variable. This is the declared type, unless the value
49     * is statically known and constant, in which case it is the actual type of the value.
50     */

51
52     public SequenceType getRequiredType() {
53         // System.err.println("Get required type of $" + getVariableName());
54
SequenceType defaultType = (requiredType==null ? SequenceType.ANY_SEQUENCE : requiredType);
55         if (assignable) {
56             return defaultType;
57         } else if (requiredType != null) {
58             return requiredType;
59         } else if (select!=null) {
60             if (select instanceof EmptySequence) {
61                 // returning Type.EMPTY gives problems with static type checking
62
return defaultType;
63             } else {
64                 try {
65                     // try to infer the type from the select expression
66
return SequenceType.makeSequenceType(select.getItemType(), select.getCardinality());
67                 } catch (Exception JavaDoc err) {
68                     // this may fail because the select expression references a variable or function
69
// whose type is not yet known, because of forwards (perhaps recursive) references.
70
return defaultType;
71                 }
72             }
73         } else if (hasChildNodes()) {
74             return SequenceType.makeSequenceType(NodeKindTest.DOCUMENT, StaticProperty.EXACTLY_ONE);
75         } else {
76             // no select attribute or content: value is an empty string
77
return SequenceType.SINGLE_STRING;
78         }
79     }
80
81     /**
82      * Compile: used only for global variables.
83      * This method ensures space is available for local variables declared within
84      * this global variable
85      */

86
87     public Expression compile(Executable exec) throws XPathException {
88
89         if (references.size()==0 && !assignable) {
90             redundant = true;
91         }
92
93         if (!redundant) {
94             GeneralVariable inst;
95             if (global) {
96                 inst = new GlobalVariable();
97                 ((GlobalVariable)inst).setExecutable(getExecutable());
98                 if (select instanceof ComputedExpression) {
99                     ((ComputedExpression)select).setParentExpression(inst);
100                 }
101                 initializeInstruction(exec, inst);
102                 inst.setVariableName(getVariableName());
103                 inst.setSlotNumber(getSlotNumber());
104                 inst.setRequiredType(getRequiredType());
105                 ExpressionTool.makeParentReferences(inst);
106                 fixupBinding(inst);
107                 return inst;
108             } else {
109                 throw new AssertionError JavaDoc("Local variable found when compiling a global variable");
110 // inst = new LocalVariable();
111
// if (select instanceof ComputedExpression) {
112
// ((ComputedExpression)select).setParentExpression(inst);
113
// }
114
// initializeInstruction(exec, inst);
115
// inst.setVariableName(getVariableName());
116
// inst.setSlotNumber(getSlotNumber());
117
// inst.setRequiredType(getRequiredType());
118
// ExpressionTool.makeParentReferences(inst);
119
// fixupBinding(inst);
120
// return inst;
121
}
122         }
123
124         return null;
125     }
126
127     public Expression compileLocalVariable(Executable exec) throws XPathException {
128
129         if (references.size()==0 && !assignable) {
130             redundant = true;
131         }
132
133         if (!redundant) {
134             GeneralVariable inst;
135             if (global) {
136                 throw new AssertionError JavaDoc("Global variable found when compiling local variable");
137 // inst = new GlobalVariable();
138
// ((GlobalVariable)inst).setExecutable(getExecutable());
139
// if (select instanceof ComputedExpression) {
140
// ((ComputedExpression)select).setParentExpression(inst);
141
// }
142
// initializeInstruction(exec, inst);
143
// inst.setVariableName(getVariableName());
144
// inst.setSlotNumber(getSlotNumber());
145
// inst.setRequiredType(getRequiredType());
146
// ExpressionTool.makeParentReferences(inst);
147
// fixupBinding(inst);
148
// return inst;
149
} else {
150                 inst = new LocalVariable();
151                 if (select instanceof ComputedExpression) {
152                     ((ComputedExpression)select).setParentExpression(inst);
153                 }
154                 initializeInstruction(exec, inst);
155                 inst.setVariableName(getVariableName());
156                 inst.setRequiredType(getRequiredType());
157                 ExpressionTool.makeParentReferences(inst);
158                 return inst;
159             }
160         }
161
162         return null;
163     }
164
165
166 }
167
168 //
169
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
170
// you may not use this file except in compliance with the License. You may obtain a copy of the
171
// License at http://www.mozilla.org/MPL/
172
//
173
// Software distributed under the License is distributed on an "AS IS" basis,
174
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
175
// See the License for the specific language governing rights and limitations under the License.
176
//
177
// The Original Code is: all this file.
178
//
179
// The Initial Developer of the Original Code is Michael H. Kay.
180
//
181
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
182
//
183
// Contributor(s): none.
184
//
185
Popular Tags