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.xalan.templates;
  58. import org.apache.xpath.XPath;
  59. import org.apache.xpath.XPathContext;
  60. import org.apache.xpath.VariableStack;
  61. import org.apache.xpath.objects.XObject;
  62. import org.apache.xpath.objects.XRTreeFrag;
  63. import org.apache.xpath.objects.XString;
  64. import org.apache.xalan.transformer.TransformerImpl;
  65. import org.apache.xalan.res.XSLMessages;
  66. import org.apache.xalan.res.XSLTErrorResources;
  67. //import org.w3c.dom.DocumentFragment;
  68. //import org.w3c.dom.Node;
  69. import org.apache.xml.dtm.DTM;
  70. /**
  71. * An instance of this class holds unto a variable until
  72. * it is executed. It is used at this time for global
  73. * variables which must (we think) forward reference.
  74. */
  75. public class XUnresolvedVariable extends XObject
  76. {
  77. /** The node context for execution. */
  78. transient private int m_context;
  79. /** The transformer context for execution. */
  80. transient private TransformerImpl m_transformer;
  81. /** An index to the point in the variable stack where we should
  82. * begin variable searches for evaluation of expressions.
  83. * This is -1 if m_isTopLevel is false.
  84. **/
  85. transient private int m_varStackPos = -1;
  86. /** An index into the variable stack where the variable context
  87. * ends, i.e. at the point we should terminate the search.
  88. **/
  89. transient private int m_varStackContext;
  90. /** true if this variable or parameter is a global.
  91. * @serial */
  92. private boolean m_isGlobal;
  93. /** true if this variable or parameter is not currently being evaluated. */
  94. transient private boolean m_doneEval = true;
  95. /**
  96. * Create an XUnresolvedVariable, that may be executed at a later time.
  97. * This is primarily used so that forward referencing works with
  98. * global variables. An XUnresolvedVariable is initially pushed
  99. * into the global variable stack, and then replaced with the real
  100. * thing when it is accessed.
  101. *
  102. * @param obj Must be a non-null reference to an ElemVariable.
  103. * @param sourceNode The node context for execution.
  104. * @param transformer The transformer execution context.
  105. * @param varStackPos An index to the point in the variable stack where we should
  106. * begin variable searches for evaluation of expressions.
  107. * @param varStackContext An index into the variable stack where the variable context
  108. * ends, i.e. at the point we should terminate the search.
  109. * @param isGlobal true if this is a global variable.
  110. */
  111. public XUnresolvedVariable(ElemVariable obj, int sourceNode,
  112. TransformerImpl transformer,
  113. int varStackPos, int varStackContext,
  114. boolean isGlobal)
  115. {
  116. super(obj);
  117. m_context = sourceNode;
  118. m_transformer = transformer;
  119. // For globals, this value will have to be updated once we
  120. // have determined how many global variables have been pushed.
  121. m_varStackPos = varStackPos;
  122. // For globals, this should zero.
  123. m_varStackContext = varStackContext;
  124. m_isGlobal = isGlobal;
  125. }
  126. /**
  127. * For support of literal objects in xpaths.
  128. *
  129. * @param xctxt The XPath execution context.
  130. *
  131. * @return This object.
  132. *
  133. * @throws javax.xml.transform.TransformerException
  134. */
  135. public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
  136. {
  137. if (!m_doneEval)
  138. {
  139. this.m_transformer.getMsgMgr().error
  140. (xctxt.getSAXLocator(), XSLTErrorResources.ER_REFERENCING_ITSELF,
  141. new Object[]{((ElemVariable)this.object()).getName().getLocalName()});
  142. }
  143. VariableStack vars = xctxt.getVarStack();
  144. // These three statements need to be combined into one operation.
  145. int currentFrame = vars.getStackFrame();
  146. //// vars.setStackFrame(m_varStackPos);
  147. ElemVariable velem = (ElemVariable)m_obj;
  148. try
  149. {
  150. m_doneEval = false;
  151. if(-1 != velem.m_frameSize)
  152. vars.link(velem.m_frameSize);
  153. XObject var = velem.getValue(m_transformer, m_context);
  154. m_doneEval = true;
  155. return var;
  156. }
  157. finally
  158. {
  159. // These two statements need to be combined into one operation.
  160. // vars.setStackFrame(currentFrame);
  161. if(-1 != velem.m_frameSize)
  162. vars.unlink(currentFrame);
  163. }
  164. }
  165. /**
  166. * Set an index to the point in the variable stack where we should
  167. * begin variable searches for evaluation of expressions.
  168. * This is -1 if m_isTopLevel is false.
  169. *
  170. * @param top A valid value that specifies where in the variable
  171. * stack the search should begin.
  172. */
  173. public void setVarStackPos(int top)
  174. {
  175. m_varStackPos = top;
  176. }
  177. /**
  178. * Set an index into the variable stack where the variable context
  179. * ends, i.e. at the point we should terminate the search.
  180. *
  181. * @param The point at which the search should terminate, normally
  182. * zero for global variables.
  183. */
  184. public void setVarStackContext(int bottom)
  185. {
  186. m_varStackContext = bottom;
  187. }
  188. /**
  189. * Tell what kind of class this is.
  190. *
  191. * @return CLASS_UNRESOLVEDVARIABLE
  192. */
  193. public int getType()
  194. {
  195. return CLASS_UNRESOLVEDVARIABLE;
  196. }
  197. /**
  198. * Given a request type, return the equivalent string.
  199. * For diagnostic purposes.
  200. *
  201. * @return An informational string.
  202. */
  203. public String getTypeString()
  204. {
  205. return "XUnresolvedVariable (" + object().getClass().getName() + ")";
  206. }
  207. }