KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > templates > XUnresolvedVariable


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: XUnresolvedVariable.java,v 1.10 2004/02/16 20:32:32 minchau Exp $
18  */

19 package org.apache.xalan.templates;
20
21 import org.apache.xalan.res.XSLTErrorResources;
22 import org.apache.xalan.transformer.TransformerImpl;
23 import org.apache.xpath.VariableStack;
24 import org.apache.xpath.XPathContext;
25 import org.apache.xpath.objects.XObject;
26
27 /**
28  * An instance of this class holds unto a variable until
29  * it is executed. It is used at this time for global
30  * variables which must (we think) forward reference.
31  */

32 public class XUnresolvedVariable extends XObject
33 {
34   /** The node context for execution. */
35   transient private int m_context;
36   
37   /** The transformer context for execution. */
38   transient private TransformerImpl m_transformer;
39   
40   /** An index to the point in the variable stack where we should
41    * begin variable searches for evaluation of expressions.
42    * This is -1 if m_isTopLevel is false.
43    **/

44   transient private int m_varStackPos = -1;
45
46   /** An index into the variable stack where the variable context
47    * ends, i.e. at the point we should terminate the search.
48    **/

49   transient private int m_varStackContext;
50   
51   /** true if this variable or parameter is a global.
52    * @serial */

53   private boolean m_isGlobal;
54   
55   /** true if this variable or parameter is not currently being evaluated. */
56   transient private boolean m_doneEval = true;
57   
58   /**
59    * Create an XUnresolvedVariable, that may be executed at a later time.
60    * This is primarily used so that forward referencing works with
61    * global variables. An XUnresolvedVariable is initially pushed
62    * into the global variable stack, and then replaced with the real
63    * thing when it is accessed.
64    *
65    * @param obj Must be a non-null reference to an ElemVariable.
66    * @param sourceNode The node context for execution.
67    * @param transformer The transformer execution context.
68    * @param varStackPos An index to the point in the variable stack where we should
69    * begin variable searches for evaluation of expressions.
70    * @param varStackContext An index into the variable stack where the variable context
71    * ends, i.e. at the point we should terminate the search.
72    * @param isGlobal true if this is a global variable.
73    */

74   public XUnresolvedVariable(ElemVariable obj, int sourceNode,
75                              TransformerImpl transformer,
76                              int varStackPos, int varStackContext,
77                              boolean isGlobal)
78   {
79     super(obj);
80     m_context = sourceNode;
81     m_transformer = transformer;
82     
83     // For globals, this value will have to be updated once we
84
// have determined how many global variables have been pushed.
85
m_varStackPos = varStackPos;
86     
87     // For globals, this should zero.
88
m_varStackContext = varStackContext;
89     
90     m_isGlobal = isGlobal;
91   }
92     
93   /**
94    * For support of literal objects in xpaths.
95    *
96    * @param xctxt The XPath execution context.
97    *
98    * @return This object.
99    *
100    * @throws javax.xml.transform.TransformerException
101    */

102   public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException JavaDoc
103   {
104     if (!m_doneEval)
105     {
106       this.m_transformer.getMsgMgr().error
107         (xctxt.getSAXLocator(), XSLTErrorResources.ER_REFERENCING_ITSELF,
108           new Object JavaDoc[]{((ElemVariable)this.object()).getName().getLocalName()});
109     }
110     VariableStack vars = xctxt.getVarStack();
111     
112     // These three statements need to be combined into one operation.
113
int currentFrame = vars.getStackFrame();
114     //// vars.setStackFrame(m_varStackPos);
115

116
117     ElemVariable velem = (ElemVariable)m_obj;
118     try
119     {
120       m_doneEval = false;
121       if(-1 != velem.m_frameSize)
122         vars.link(velem.m_frameSize);
123       XObject var = velem.getValue(m_transformer, m_context);
124       m_doneEval = true;
125       return var;
126     }
127     finally
128     {
129       // These two statements need to be combined into one operation.
130
// vars.setStackFrame(currentFrame);
131

132       if(-1 != velem.m_frameSize)
133         vars.unlink(currentFrame);
134     }
135   }
136   
137   /**
138    * Set an index to the point in the variable stack where we should
139    * begin variable searches for evaluation of expressions.
140    * This is -1 if m_isTopLevel is false.
141    *
142    * @param top A valid value that specifies where in the variable
143    * stack the search should begin.
144    */

145   public void setVarStackPos(int top)
146   {
147     m_varStackPos = top;
148   }
149
150   /**
151    * Set an index into the variable stack where the variable context
152    * ends, i.e. at the point we should terminate the search.
153    *
154    * @param The point at which the search should terminate, normally
155    * zero for global variables.
156    */

157   public void setVarStackContext(int bottom)
158   {
159     m_varStackContext = bottom;
160   }
161   
162   /**
163    * Tell what kind of class this is.
164    *
165    * @return CLASS_UNRESOLVEDVARIABLE
166    */

167   public int getType()
168   {
169     return CLASS_UNRESOLVEDVARIABLE;
170   }
171   
172   /**
173    * Given a request type, return the equivalent string.
174    * For diagnostic purposes.
175    *
176    * @return An informational string.
177    */

178   public String JavaDoc getTypeString()
179   {
180     return "XUnresolvedVariable (" + object().getClass().getName() + ")";
181   }
182
183
184 }
185
Popular Tags