KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.icl.saxon.expr;
2 import com.icl.saxon.Context;
3 import com.icl.saxon.Binding;
4 import com.icl.saxon.Bindery;
5 import com.icl.saxon.trace.*; // e.g.
6
import com.icl.saxon.style.XSLGeneralVariable;
7 import javax.xml.transform.TransformerException JavaDoc;
8
9 /**
10 * Variable reference: a reference to an XSL variable
11 */

12
13 public class VariableReference extends Expression {
14
15     int fingerprint;
16     Binding binding;
17     
18     /**
19     * Constructor
20     * @param name the variable name (as a Name object)
21     */

22
23     public VariableReference(int fingerprint, StaticContext staticContext) throws XPathException {
24         this.fingerprint = fingerprint;
25         binding = staticContext.bindVariable(fingerprint);
26     }
27
28     /**
29     * Determine which aspects of the context the expression depends on. The result is
30     * a bitwise-or'ed value composed from constants such as Context.VARIABLES and
31     * Context.CURRENT_NODE
32     */

33
34     public int getDependencies() {
35         return Context.VARIABLES;
36     }
37
38     /**
39     * Perform a partial evaluation of the expression, by eliminating specified dependencies
40     * on the context.
41     * @param dependencies The dependencies to be removed
42     * @param context The context to be used for the partial evaluation
43     * @return a new expression that does not have any of the specified
44     * dependencies
45     */

46
47     public Expression reduce(int dependencies, Context context) throws XPathException {
48         if ((dependencies & Context.VARIABLES) != 0) {
49             return evaluate(context);
50         } else {
51             return this;
52         }
53     }
54
55     /**
56     * Get the value of this variable in a given context.
57     * @param context the Context which contains the relevant variable bindings
58     * @return the value of the variable, if it is defined
59     * @throw XPathException if the variable is undefined
60     */

61
62
63     public Value evaluate(Context c) throws XPathException {
64
65         Bindery b = c.getBindery();
66         Value v = b.getValue(binding);
67         
68         if (v==null) {
69
70             if (!binding.isGlobal()) {
71                 throw new XPathException("Variable " + binding.getVariableName() + " is undefined");
72             }
73             
74             // it must be a forwards reference; try to evaluate it now.
75
// but first set a flag to stop looping. This flag is set in the Bindery because
76
// the VariableReference itself can be used by multiple threads simultaneously
77

78             try {
79
80                 b.setExecuting(binding, true);
81     
82                 if (binding instanceof XSLGeneralVariable) {
83                     if (c.getController().isTracing()) { // e.g.
84
TraceListener listener = c.getController().getTraceListener();
85                 
86                         listener.enter((XSLGeneralVariable)binding, c);
87                         ((XSLGeneralVariable)binding).process(c);
88                         listener.leave((XSLGeneralVariable)binding, c);
89     
90                     } else {
91                         ((XSLGeneralVariable)binding).process(c);
92                     }
93                 }
94     
95                 b.setExecuting(binding, false);
96                 
97                 v = b.getValue(binding);
98
99             } catch (TransformerException JavaDoc err) {
100                 if (err instanceof XPathException) {
101                     throw (XPathException)err;
102                 } else {
103                     throw new XPathException(err);
104                 }
105             }
106             
107             if (v==null) {
108                 throw new XPathException("Variable " + binding.getVariableName() + " is undefined");
109             }
110         }
111         return v;
112     }
113        
114
115     /**
116     * Get the object bound to the variable
117     */

118
119     public Binding getBinding() {
120         return binding;
121     }
122
123     /**
124     * Determine the data type of the expression, if possible
125     * @return the type of the variable, if this can be determined statically;
126     * otherwise Value.ANY (meaning not known in advance)
127     */

128
129     public int getDataType() {
130         return binding.getDataType();
131     }
132
133     /**
134     * Simplify the expression. If the variable has a fixed value, the variable reference
135     * will be replaced with that value.
136     */

137
138     public Expression simplify() {
139         Value v = binding.constantValue();
140         if (v==null) {
141             return this;
142         } else {
143             return v;
144         }
145     }
146
147     /**
148     * Diagnostic print of expression structure
149     */

150     
151     public void display(int level) {
152         System.err.println(indent(level) + "$" + binding.getVariableName());
153     }
154 }
155
156 //
157
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
158
// you may not use this file except in compliance with the License. You may obtain a copy of the
159
// License at http://www.mozilla.org/MPL/
160
//
161
// Software distributed under the License is distributed on an "AS IS" basis,
162
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
163
// See the License for the specific language governing rights and limitations under the License.
164
//
165
// The Original Code is: all this file.
166
//
167
// The Initial Developer of the Original Code is
168
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
169
//
170
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
171
//
172
// Contributor(s):
173
// Portions marked "e.g." are from Edwin Glaser (edwin@pannenleiter.de)
174
//
175
Popular Tags