2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 * $Id: Variable.java 468655 2006-10-28 07:12:06Z minchau $
21 package org.apache.xpath.operations;
23 import javax.xml.transform.TransformerException;
25 import org.apache.xalan.res.XSLMessages;
26 import org.apache.xml.utils.QName;
27 import org.apache.xpath.Expression;
28 import org.apache.xpath.ExpressionOwner;
29 import org.apache.xpath.XPath;
30 import org.apache.xpath.XPathContext;
31 import org.apache.xpath.XPathVisitor;
32 import org.apache.xpath.axes.PathComponent;
33 import org.apache.xpath.axes.WalkerFactory;
34 import org.apache.xpath.objects.XNodeSet;
35 import org.apache.xpath.objects.XObject;
36 import org.apache.xpath.res.XPATHErrorResources;
40 * The variable reference expression executer.
42 public class Variable extends Expression implements PathComponent
44 static final long serialVersionUID = -4334975375609297049L;
45 /** Tell if fixupVariables was called.
47 private boolean m_fixUpWasCalled = false;
49 /** The qualified name of the variable.
51 protected QName m_qname;
54 * The index of the variable, which is either an absolute index to a
55 * global, or, if higher than the globals area, must be adjusted by adding
56 * the offset to the current stack frame.
58 protected int m_index;
61 * Set the index for the variable into the stack. For advanced use only. You
62 * must know what you are doing to use this.
64 * @param index a global or local index.
66 public void setIndex(int index)
72 * Set the index for the variable into the stack. For advanced use only.
74 * @return index a global or local index.
82 * Set whether or not this is a global reference. For advanced use only.
84 * @param isGlobal true if this should be a global variable reference.
86 public void setIsGlobal(boolean isGlobal)
88 m_isGlobal = isGlobal;
92 * Set the index for the variable into the stack. For advanced use only.
94 * @return true if this should be a global variable reference.
96 public boolean getGlobal()
105 protected boolean m_isGlobal = false;
108 * This function is used to fixup variables from QNames to stack frame
109 * indexes at stylesheet build time.
110 * @param vars List of QNames that correspond to variables. This list
111 * should be searched backwards for the first qualified name that
112 * corresponds to the variable reference qname. The position of the
113 * QName in the vector from the start of the vector will be its position
114 * in the stack frame (but variables above the globalsTop value will need
115 * to be offset to the current stack frame).
117 public void fixupVariables(java.util.Vector vars, int globalsSize)
119 m_fixUpWasCalled = true;
120 int sz = vars.size();
122 for (int i = vars.size()-1; i >= 0; i--)
124 QName qn = (QName)vars.elementAt(i);
125 // System.out.println("qn: "+qn);
126 if(qn.equals(m_qname))
136 m_index = i-globalsSize;
143 java.lang.String msg = XSLMessages.createXPATHMessage(XPATHErrorResources.ER_COULD_NOT_FIND_VAR,
144 new Object[]{m_qname.toString()});
146 TransformerException te = new TransformerException(msg, this);
148 throw new org.apache.xml.utils.WrappedRuntimeException(te);
154 * Set the qualified name of the variable.
156 * @param qname Must be a non-null reference to a qualified name.
158 public void setQName(QName qname)
164 * Get the qualified name of the variable.
166 * @return A non-null reference to a qualified name.
168 public QName getQName()
174 * Execute an expression in the XPath runtime context, and return the
175 * result of the expression.
178 * @param xctxt The XPath runtime context.
180 * @return The result of the expression in the form of a <code>XObject</code>.
182 * @throws javax.xml.transform.TransformerException if a runtime exception
185 public XObject execute(XPathContext xctxt)
186 throws javax.xml.transform.TransformerException
188 return execute(xctxt, false);
193 * Dereference the variable, and return the reference value. Note that lazy
194 * evaluation will occur. If a variable within scope is not found, a warning
195 * will be sent to the error listener, and an empty nodeset will be returned.
198 * @param xctxt The runtime execution context.
200 * @return The evaluated variable, or an empty nodeset if not found.
202 * @throws javax.xml.transform.TransformerException
204 public XObject execute(XPathContext xctxt, boolean destructiveOK) throws javax.xml.transform.TransformerException
206 org.apache.xml.utils.PrefixResolver xprefixResolver = xctxt.getNamespaceContext();
209 // Is the variable fetched always the same?
210 // XObject result = xctxt.getVariable(m_qname);
214 result = xctxt.getVarStack().getGlobalVariable(xctxt, m_index, destructiveOK);
216 result = xctxt.getVarStack().getLocalVariable(xctxt, m_index, destructiveOK);
219 result = xctxt.getVarStack().getVariableOrParam(xctxt,m_qname);
224 // This should now never happen...
225 warn(xctxt, XPATHErrorResources.WG_ILLEGAL_VARIABLE_REFERENCE,
226 new Object[]{ m_qname.getLocalPart() }); //"VariableReference given for variable out "+
227 // (new RuntimeException()).printStackTrace();
228 // error(xctxt, XPATHErrorResources.ER_COULDNOT_GET_VAR_NAMED,
229 // new Object[]{ m_qname.getLocalPart() }); //"Could not get variable named "+varName);
231 result = new XNodeSet(xctxt.getDTMManager());
238 // // Hack city... big time. This is needed to evaluate xpaths from extensions,
239 // // pending some bright light going off in my head. Some sort of callback?
240 // synchronized(this)
242 // org.apache.xalan.templates.ElemVariable vvar= getElemVariable();
245 // m_index = vvar.getIndex();
246 // m_isGlobal = vvar.getIsTopLevel();
247 // m_fixUpWasCalled = true;
248 // return execute(xctxt);
251 // throw new javax.xml.transform.TransformerException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_VAR_NOT_RESOLVABLE, new Object[]{m_qname.toString()})); //"Variable not resolvable: "+m_qname);
256 * Get the XSLT ElemVariable that this sub-expression references. In order for
257 * this to work, the SourceLocator must be the owning ElemTemplateElement.
258 * @return The dereference to the ElemVariable, or null if not found.
260 public org.apache.xalan.templates.ElemVariable getElemVariable()
263 // Get the current ElemTemplateElement, and then walk backwards in
264 // document order, searching
265 // for an xsl:param element or xsl:variable element that matches our
266 // qname. If we reach the top level, use the StylesheetRoot's composed
267 // list of top level variables and parameters.
269 org.apache.xalan.templates.ElemVariable vvar = null;
270 org.apache.xpath.ExpressionNode owner = getExpressionOwner();
272 if (null != owner && owner instanceof org.apache.xalan.templates.ElemTemplateElement)
275 org.apache.xalan.templates.ElemTemplateElement prev =
276 (org.apache.xalan.templates.ElemTemplateElement) owner;
278 if (!(prev instanceof org.apache.xalan.templates.Stylesheet))
280 while ( prev != null && !(prev.getParentNode() instanceof org.apache.xalan.templates.Stylesheet) )
282 org.apache.xalan.templates.ElemTemplateElement savedprev = prev;
284 while (null != (prev = prev.getPreviousSiblingElem()))
286 if(prev instanceof org.apache.xalan.templates.ElemVariable)
288 vvar = (org.apache.xalan.templates.ElemVariable) prev;
290 if (vvar.getName().equals(m_qname))
297 prev = savedprev.getParentElem();
301 vvar = prev.getStylesheetRoot().getVariableOrParamComposed(m_qname);
308 * Tell if this expression returns a stable number that will not change during
309 * iterations within the expression. This is used to determine if a proximity
310 * position predicate can indicate that no more searching has to occur.
313 * @return true if the expression represents a stable number.
315 public boolean isStableNumber()
321 * Get the analysis bits for this walker, as defined in the WalkerFactory.
322 * @return One of WalkerFactory#BIT_DESCENDANT, etc.
324 public int getAnalysisBits()
326 org.apache.xalan.templates.ElemVariable vvar = getElemVariable();
329 XPath xpath = vvar.getSelect();
332 Expression expr = xpath.getExpression();
333 if(null != expr && expr instanceof PathComponent)
335 return ((PathComponent)expr).getAnalysisBits();
339 return WalkerFactory.BIT_FILTER;
344 * @see org.apache.xpath.XPathVisitable#callVisitors(ExpressionOwner, XPathVisitor)
346 public void callVisitors(ExpressionOwner owner, XPathVisitor visitor)
348 visitor.visitVariableRef(owner, this);
351 * @see Expression#deepEquals(Expression)
353 public boolean deepEquals(Expression expr)
355 if(!isSameClass(expr))
358 if(!m_qname.equals(((Variable)expr).m_qname))
361 // We have to make sure that the qname really references
362 // the same variable element.
363 if(getElemVariable() != ((Variable)expr).getElemVariable())
369 static final java.lang.String PSUEDOVARNAMESPACE = "http://xml.apache.org/xalan/psuedovar";
372 * Tell if this is a psuedo variable reference, declared by Xalan instead
375 public boolean isPsuedoVarRef()
377 java.lang.String ns = m_qname.getNamespaceURI();
378 if((null != ns) && ns.equals(PSUEDOVARNAMESPACE))
380 if(m_qname.getLocalName().startsWith("#"))