1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xalan" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, Lotus
  53. * Development Corporation., http://www.lotus.com. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package org.apache.xpath.operations;
  58. import java.util.Vector;
  59. import javax.xml.transform.TransformerException;
  60. import org.apache.xalan.res.XSLMessages;
  61. import org.apache.xalan.templates.ElemTemplateElement;
  62. import org.apache.xalan.templates.ElemVariable;
  63. import org.apache.xalan.templates.Stylesheet;
  64. import org.apache.xml.utils.PrefixResolver;
  65. import org.apache.xml.utils.QName;
  66. import org.apache.xpath.Expression;
  67. import org.apache.xpath.ExpressionNode;
  68. import org.apache.xpath.ExpressionOwner;
  69. import org.apache.xpath.XPath;
  70. import org.apache.xpath.XPathContext;
  71. import org.apache.xpath.XPathVisitor;
  72. import org.apache.xpath.axes.PathComponent;
  73. import org.apache.xpath.axes.WalkerFactory;
  74. import org.apache.xpath.objects.XNodeSet;
  75. import org.apache.xpath.objects.XObject;
  76. import org.apache.xpath.res.XPATHErrorResources;
  77. import org.xml.sax.ContentHandler;
  78. import org.xml.sax.SAXException;
  79. /**
  80. * The variable reference expression executer.
  81. */
  82. public class Variable extends Expression implements PathComponent
  83. {
  84. /** Tell if fixupVariables was called.
  85. * @serial */
  86. private boolean m_fixUpWasCalled = false;
  87. /** The qualified name of the variable.
  88. * @serial */
  89. protected QName m_qname;
  90. /**
  91. * The index of the variable, which is either an absolute index to a
  92. * global, or, if higher than the globals area, must be adjusted by adding
  93. * the offset to the current stack frame.
  94. */
  95. protected int m_index;
  96. /**
  97. * Set the index for the variable into the stack. For advanced use only. You
  98. * must know what you are doing to use this.
  99. *
  100. * @param index a global or local index.
  101. */
  102. public void setIndex(int index)
  103. {
  104. m_index = index;
  105. }
  106. /**
  107. * Set the index for the variable into the stack. For advanced use only.
  108. *
  109. * @return index a global or local index.
  110. */
  111. public int getIndex()
  112. {
  113. return m_index;
  114. }
  115. /**
  116. * Set whether or not this is a global reference. For advanced use only.
  117. *
  118. * @param isGlobal true if this should be a global variable reference.
  119. */
  120. public void setIsGlobal(boolean isGlobal)
  121. {
  122. m_isGlobal = isGlobal;
  123. }
  124. /**
  125. * Set the index for the variable into the stack. For advanced use only.
  126. *
  127. * @return true if this should be a global variable reference.
  128. */
  129. public boolean getGlobal()
  130. {
  131. return m_isGlobal;
  132. }
  133. protected boolean m_isGlobal = false;
  134. /**
  135. * This function is used to fixup variables from QNames to stack frame
  136. * indexes at stylesheet build time.
  137. * @param vars List of QNames that correspond to variables. This list
  138. * should be searched backwards for the first qualified name that
  139. * corresponds to the variable reference qname. The position of the
  140. * QName in the vector from the start of the vector will be its position
  141. * in the stack frame (but variables above the globalsTop value will need
  142. * to be offset to the current stack frame).
  143. */
  144. public void fixupVariables(java.util.Vector vars, int globalsSize)
  145. {
  146. m_fixUpWasCalled = true;
  147. int sz = vars.size();
  148. for (int i = vars.size()-1; i >= 0; i--)
  149. {
  150. QName qn = (QName)vars.elementAt(i);
  151. // System.out.println("qn: "+qn);
  152. if(qn.equals(m_qname))
  153. {
  154. if(i < globalsSize)
  155. {
  156. m_isGlobal = true;
  157. m_index = i;
  158. }
  159. else
  160. {
  161. m_index = i-globalsSize;
  162. }
  163. return;
  164. }
  165. }
  166. java.lang.String msg = XSLMessages.createXPATHMessage(XPATHErrorResources.ER_COULD_NOT_FIND_VAR,
  167. new Object[]{m_qname.toString()});
  168. TransformerException te = new TransformerException(msg, this);
  169. throw new org.apache.xml.utils.WrappedRuntimeException(te);
  170. }
  171. /**
  172. * Set the qualified name of the variable.
  173. *
  174. * @param qname Must be a non-null reference to a qualified name.
  175. */
  176. public void setQName(QName qname)
  177. {
  178. m_qname = qname;
  179. }
  180. /**
  181. * Get the qualified name of the variable.
  182. *
  183. * @return A non-null reference to a qualified name.
  184. */
  185. public QName getQName()
  186. {
  187. return m_qname;
  188. }
  189. /**
  190. * Execute an expression in the XPath runtime context, and return the
  191. * result of the expression.
  192. *
  193. *
  194. * @param xctxt The XPath runtime context.
  195. *
  196. * @return The result of the expression in the form of a <code>XObject</code>.
  197. *
  198. * @throws javax.xml.transform.TransformerException if a runtime exception
  199. * occurs.
  200. */
  201. public XObject execute(XPathContext xctxt)
  202. throws javax.xml.transform.TransformerException
  203. {
  204. return execute(xctxt, false);
  205. }
  206. /**
  207. * Dereference the variable, and return the reference value. Note that lazy
  208. * evaluation will occur. If a variable within scope is not found, a warning
  209. * will be sent to the error listener, and an empty nodeset will be returned.
  210. *
  211. *
  212. * @param xctxt The runtime execution context.
  213. *
  214. * @return The evaluated variable, or an empty nodeset if not found.
  215. *
  216. * @throws javax.xml.transform.TransformerException
  217. */
  218. public XObject execute(XPathContext xctxt, boolean destructiveOK) throws javax.xml.transform.TransformerException
  219. {
  220. org.apache.xml.utils.PrefixResolver xprefixResolver = xctxt.getNamespaceContext();
  221. // Is the variable fetched always the same?
  222. // XObject result = xctxt.getVariable(m_qname);
  223. if(m_fixUpWasCalled)
  224. {
  225. XObject result;
  226. if(m_isGlobal)
  227. result = xctxt.getVarStack().getGlobalVariable(xctxt, m_index, destructiveOK);
  228. else
  229. result = xctxt.getVarStack().getLocalVariable(xctxt, m_index, destructiveOK);
  230. if (null == result)
  231. {
  232. // This should now never happen...
  233. warn(xctxt, XPATHErrorResources.WG_ILLEGAL_VARIABLE_REFERENCE,
  234. new Object[]{ m_qname.getLocalPart() }); //"VariableReference given for variable out "+
  235. // (new RuntimeException()).printStackTrace();
  236. // error(xctxt, XPATHErrorResources.ER_COULDNOT_GET_VAR_NAMED,
  237. // new Object[]{ m_qname.getLocalPart() }); //"Could not get variable named "+varName);
  238. result = new XNodeSet(xctxt.getDTMManager());
  239. }
  240. return result;
  241. }
  242. else
  243. {
  244. // Hack city... big time. This is needed to evaluate xpaths from extensions,
  245. // pending some bright light going off in my head. Some sort of callback?
  246. synchronized(this)
  247. {
  248. org.apache.xalan.templates.ElemVariable vvar= getElemVariable();
  249. if(null != vvar)
  250. {
  251. m_index = vvar.getIndex();
  252. m_isGlobal = vvar.getIsTopLevel();
  253. m_fixUpWasCalled = true;
  254. return execute(xctxt);
  255. }
  256. }
  257. throw new javax.xml.transform.TransformerException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_VAR_NOT_RESOLVABLE, new Object[]{m_qname.toString()})); //"Variable not resolvable: "+m_qname);
  258. }
  259. }
  260. /**
  261. * Get the XSLT ElemVariable that this sub-expression references. In order for
  262. * this to work, the SourceLocator must be the owning ElemTemplateElement.
  263. * @return The dereference to the ElemVariable, or null if not found.
  264. */
  265. public org.apache.xalan.templates.ElemVariable getElemVariable()
  266. {
  267. // Get the current ElemTemplateElement, and then walk backwards in
  268. // document order, searching
  269. // for an xsl:param element or xsl:variable element that matches our
  270. // qname. If we reach the top level, use the StylesheetRoot's composed
  271. // list of top level variables and parameters.
  272. org.apache.xpath.ExpressionNode owner = getExpressionOwner();
  273. if (null != owner && owner instanceof org.apache.xalan.templates.ElemTemplateElement)
  274. {
  275. org.apache.xalan.templates.ElemVariable vvar;
  276. org.apache.xalan.templates.ElemTemplateElement prev =
  277. (org.apache.xalan.templates.ElemTemplateElement) owner;
  278. if (!(prev instanceof org.apache.xalan.templates.Stylesheet))
  279. {
  280. while ( !(prev.getParentNode() instanceof org.apache.xalan.templates.Stylesheet) )
  281. {
  282. org.apache.xalan.templates.ElemTemplateElement savedprev = prev;
  283. while (null != (prev = prev.getPreviousSiblingElem()))
  284. {
  285. if(prev instanceof org.apache.xalan.templates.ElemVariable)
  286. {
  287. vvar = (org.apache.xalan.templates.ElemVariable) prev;
  288. if (vvar.getName().equals(m_qname))
  289. {
  290. return vvar;
  291. }
  292. }
  293. }
  294. prev = savedprev.getParentElem();
  295. }
  296. }
  297. vvar = prev.getStylesheetRoot().getVariableOrParamComposed(m_qname);
  298. if (null != vvar)
  299. {
  300. return vvar;
  301. }
  302. }
  303. return null;
  304. }
  305. /**
  306. * Tell if this expression returns a stable number that will not change during
  307. * iterations within the expression. This is used to determine if a proximity
  308. * position predicate can indicate that no more searching has to occur.
  309. *
  310. *
  311. * @return true if the expression represents a stable number.
  312. */
  313. public boolean isStableNumber()
  314. {
  315. return true;
  316. }
  317. /**
  318. * Get the analysis bits for this walker, as defined in the WalkerFactory.
  319. * @return One of WalkerFactory#BIT_DESCENDANT, etc.
  320. */
  321. public int getAnalysisBits()
  322. {
  323. org.apache.xalan.templates.ElemVariable vvar = getElemVariable();
  324. if(null != vvar)
  325. {
  326. XPath xpath = vvar.getSelect();
  327. if(null != xpath)
  328. {
  329. Expression expr = xpath.getExpression();
  330. if(null != expr && expr instanceof PathComponent)
  331. {
  332. return ((PathComponent)expr).getAnalysisBits();
  333. }
  334. }
  335. }
  336. return WalkerFactory.BIT_FILTER;
  337. }
  338. /**
  339. * @see XPathVisitable#callVisitors(ExpressionOwner, XPathVisitor)
  340. */
  341. public void callVisitors(ExpressionOwner owner, XPathVisitor visitor)
  342. {
  343. visitor.visitVariableRef(owner, this);
  344. }
  345. /**
  346. * @see Expression#deepEquals(Expression)
  347. */
  348. public boolean deepEquals(Expression expr)
  349. {
  350. if(!isSameClass(expr))
  351. return false;
  352. if(!m_qname.equals(((Variable)expr).m_qname))
  353. return false;
  354. // We have to make sure that the qname really references
  355. // the same variable element.
  356. if(getElemVariable() != ((Variable)expr).getElemVariable())
  357. return false;
  358. return true;
  359. }
  360. static final java.lang.String PSUEDOVARNAMESPACE = "http://xml.apache.org/xalan/psuedovar";
  361. /**
  362. * Tell if this is a psuedo variable reference, declared by Xalan instead
  363. * of by the user.
  364. */
  365. public boolean isPsuedoVarRef()
  366. {
  367. java.lang.String ns = m_qname.getNamespaceURI();
  368. if((null != ns) && ns.equals(PSUEDOVARNAMESPACE))
  369. {
  370. if(m_qname.getLocalName().startsWith("#"))
  371. return true;
  372. }
  373. return false;
  374. }
  375. }