KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.style;
2 import net.sf.saxon.expr.Binding;
3 import net.sf.saxon.expr.BindingReference;
4 import net.sf.saxon.expr.VariableDeclaration;
5 import net.sf.saxon.instruct.SlotManager;
6 import net.sf.saxon.trans.XPathException;
7 import net.sf.saxon.type.Type;
8 import net.sf.saxon.value.SequenceType;
9 import net.sf.saxon.value.Value;
10
11 import java.util.ArrayList JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.List JavaDoc;
14
15
16 /**
17 * Generic class for xsl:variable and xsl:param elements. <br>
18 */

19
20 public abstract class XSLVariableDeclaration
21         extends XSLGeneralVariable
22         implements VariableDeclaration, StylesheetProcedure {
23
24     // The slot number for the variable is allocated at this level (a) for global variables, and
25
// (b) for local parameters. For local variables, slot numbers are allocated only after an entire
26
// template or function has been compiled.
27

28     private int slotNumber = -9876; // initial value designed solely to show up when debugging
29

30     // List of VariableReference objects that reference this XSLVariableDeclaration
31
List JavaDoc references = new ArrayList JavaDoc(10);
32
33     /**
34      * Get the SlotManager associated with this stylesheet construct. The SlotManager contains the
35      * information needed to manage the local stack frames used by run-time instances of the code.
36      * @return the associated SlotManager object
37      */

38
39     public SlotManager getSlotManager() {
40         return slotManager;
41     }
42
43
44     public int getSlotNumber() {
45         return slotNumber;
46     }
47
48     public void setSlotNumber(int slot) {
49         slotNumber = slot;
50     }
51
52     /**
53     * Get the static type of the variable.
54     */

55
56     public abstract SequenceType getRequiredType();
57
58     /**
59     * Method called by VariableReference to register the variable reference for
60     * subsequent fixup
61     */

62
63     public void registerReference(BindingReference ref) {
64         references.add(ref);
65     }
66
67     /**
68      * Get the list of references to this variable or parameter. The items in the list are
69      * of class BindingReference.
70      */

71
72     public List JavaDoc getReferences() {
73         return references;
74     }
75
76     /**
77     * Determine whether this node is an instruction.
78     * @return true - it is an instruction (well, it can be, anyway)
79     */

80
81     public boolean isInstruction() {
82         return true;
83     }
84
85     /**
86      * Get the list of references
87      */

88
89     public List JavaDoc getReferenceList() {
90         return references;
91     }
92
93     /**
94     * Notify all references to this variable of the data type
95     */

96
97     public void fixupReferences() throws XPathException {
98         SequenceType type = getRequiredType();
99         Iterator JavaDoc iter = references.iterator();
100         while (iter.hasNext()) {
101             Value constantValue = null;
102             int properties = 0;
103             if (this instanceof XSLVariable && !isAssignable()) {
104                 if (select instanceof Value) {
105                     // we can't rely on the constant value because it hasn't yet been type-checked,
106
// which could change it (eg by numeric promotion). Rather than attempt all the type-checking
107
// now, we do a quick check. See test bug64
108
int relation = Type.relationship(select.getItemType(), type.getPrimaryType());
109                     if (relation == Type.SAME_TYPE || relation == Type.SUBSUMED_BY) {
110                         constantValue = (Value)select;
111                     }
112                 }
113                 if (select != null) {
114                     properties = select.getSpecialProperties();
115                 }
116             }
117             ((BindingReference)iter.next()).setStaticType(type, constantValue, properties);
118         }
119         super.fixupReferences();
120     }
121
122     /**
123     * Check that the variable is not already declared, and allocate a slot number
124     */

125
126     public void validate() throws XPathException {
127         super.validate();
128         if (global) {
129             if (!redundant) {
130                 slotNumber = getPrincipalStylesheet().allocateGlobalSlot(getVariableFingerprint());
131             }
132         } else {
133             checkWithinTemplate();
134 // SlotManager p = getContainingSlotManager();
135
// if (p==null) {
136
// compileError("Local variable must be declared within a template or function", "XT0010");
137
// } else {
138
// slotNumber = p.allocateSlotNumber(getVariableFingerprint());
139
// }
140
}
141         // Check for duplication
142
// Global variables are checked at the XSLStylesheet level
143
// For local variables, duplicates are now allowed
144
// For parameters, XSLParam and XSLWithParam do their own checks
145
// checkDuplicateDeclaration();
146
}
147
148     /**
149     * Notify all variable references of the Binding instruction
150     */

151
152     protected void fixupBinding(Binding binding) {
153         Iterator JavaDoc iter = references.iterator();
154         while (iter.hasNext()) {
155             ((BindingReference)iter.next()).fixup(binding);
156         }
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 Michael H. Kay.
174
//
175
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
176
//
177
// Contributor(s): none.
178
//
179
Popular Tags