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.transformer;
  58. import org.apache.xpath.XPathContext;
  59. import org.apache.xpath.VariableStack;
  60. import org.apache.xpath.axes.ContextNodeList;
  61. import org.apache.xml.utils.NodeVector;
  62. import org.apache.xml.utils.IntStack;
  63. import org.apache.xml.utils.BoolStack;
  64. import org.apache.xml.dtm.DTMIterator;
  65. import org.apache.xalan.templates.ElemTemplateElement;
  66. import java.util.Stack;
  67. import org.xml.sax.helpers.NamespaceSupport;
  68. import org.apache.xml.utils.NamespaceSupport2;
  69. import org.apache.xml.utils.ObjectStack;
  70. import java.util.Enumeration;
  71. /**
  72. * This class holds a "snapshot" of it's current transformer state,
  73. * which can later be restored.
  74. *
  75. * This only saves state which can change over the course of the side-effect-free
  76. * (i.e. no extensions that call setURIResolver, etc.).
  77. */
  78. class TransformSnapshotImpl implements TransformSnapshot
  79. {
  80. /**
  81. * The stack of Variable stack frames.
  82. */
  83. private VariableStack m_variableStacks;
  84. /**
  85. * The stack of <a href="http://www.w3.org/TR/xslt#dt-current-node">current node</a> objects.
  86. * Not to be confused with the current node list.
  87. */
  88. private IntStack m_currentNodes;
  89. /** A stack of the current sub-expression nodes. */
  90. private IntStack m_currentExpressionNodes;
  91. /**
  92. * The current context node lists stack.
  93. */
  94. private Stack m_contextNodeLists;
  95. /**
  96. * The current context node list.
  97. */
  98. private DTMIterator m_contextNodeList;
  99. /**
  100. * Stack of AxesIterators.
  101. */
  102. private Stack m_axesIteratorStack;
  103. /**
  104. * Is > 0 when we're processing a for-each.
  105. */
  106. private BoolStack m_currentTemplateRuleIsNull;
  107. /**
  108. * A node vector used as a stack to track the current
  109. * ElemTemplateElement. Needed for the
  110. * org.apache.xalan.transformer.TransformState interface,
  111. * so a tool can discover the calling template.
  112. */
  113. private ObjectStack m_currentTemplateElements;
  114. /**
  115. * A node vector used as a stack to track the current
  116. * ElemTemplate that was matched, as well as the node that
  117. * was matched. Needed for the
  118. * org.apache.xalan.transformer.TransformState interface,
  119. * so a tool can discover the matched template, and matched
  120. * node.
  121. */
  122. private Stack m_currentMatchTemplates;
  123. /**
  124. * A node vector used as a stack to track the current
  125. * ElemTemplate that was matched, as well as the node that
  126. * was matched. Needed for the
  127. * org.apache.xalan.transformer.TransformState interface,
  128. * so a tool can discover the matched template, and matched
  129. * node.
  130. */
  131. private NodeVector m_currentMatchNodes;
  132. /**
  133. * The table of counters for xsl:number support.
  134. * @see ElemNumber
  135. */
  136. private CountersTable m_countersTable;
  137. /**
  138. * Stack for the purposes of flagging infinite recursion with
  139. * attribute sets.
  140. */
  141. private Stack m_attrSetStack;
  142. /** Indicate whether a namespace context was pushed */
  143. boolean m_nsContextPushed;
  144. /**
  145. * Use the SAX2 helper class to track result namespaces.
  146. */
  147. private NamespaceSupport m_nsSupport;
  148. /** The number of events queued */
  149. int m_eventCount;
  150. /**
  151. * Constructor TransformSnapshotImpl
  152. * Take a snapshot of the currently executing context.
  153. *
  154. * @param transformer Non null transformer instance
  155. */
  156. TransformSnapshotImpl(TransformerImpl transformer)
  157. {
  158. try
  159. {
  160. // Are all these clones deep enough?
  161. ResultTreeHandler rtf = transformer.getResultTreeHandler();
  162. m_eventCount = rtf.m_eventCount;
  163. // yuck. No clone. Hope this is good enough.
  164. m_nsSupport = new NamespaceSupport2();
  165. Enumeration prefixes = rtf.m_nsSupport.getPrefixes();
  166. while (prefixes.hasMoreElements())
  167. {
  168. String prefix = (String) prefixes.nextElement();
  169. String uri = rtf.m_nsSupport.getURI(prefix);
  170. m_nsSupport.declarePrefix(prefix, uri);
  171. }
  172. m_nsContextPushed = rtf.m_nsContextPushed;
  173. XPathContext xpc = transformer.getXPathContext();
  174. m_variableStacks = (VariableStack) xpc.getVarStack().clone();
  175. m_currentNodes = (IntStack) xpc.getCurrentNodeStack().clone();
  176. m_currentExpressionNodes =
  177. (IntStack) xpc.getCurrentExpressionNodeStack().clone();
  178. m_contextNodeLists = (Stack) xpc.getContextNodeListsStack().clone();
  179. if (!m_contextNodeLists.empty())
  180. m_contextNodeList =
  181. (DTMIterator) xpc.getContextNodeList().clone();
  182. m_axesIteratorStack = (Stack) xpc.getAxesIteratorStackStacks().clone();
  183. m_currentTemplateRuleIsNull =
  184. (BoolStack) transformer.m_currentTemplateRuleIsNull.clone();
  185. m_currentTemplateElements =
  186. (ObjectStack) transformer.m_currentTemplateElements.clone();
  187. m_currentMatchTemplates =
  188. (Stack) transformer.m_currentMatchTemplates.clone();
  189. m_currentMatchNodes =
  190. (NodeVector) transformer.m_currentMatchedNodes.clone();
  191. m_countersTable =
  192. (CountersTable) transformer.getCountersTable().clone();
  193. if (transformer.m_attrSetStack != null)
  194. m_attrSetStack = (Stack) transformer.m_attrSetStack.clone();
  195. }
  196. catch (CloneNotSupportedException cnse)
  197. {
  198. throw new org.apache.xml.utils.WrappedRuntimeException(cnse);
  199. }
  200. }
  201. /**
  202. * This will reset the stylesheet to a given execution context
  203. * based on some previously taken snapshot where we can then start execution
  204. *
  205. * @param transformer Non null transformer instance
  206. */
  207. void apply(TransformerImpl transformer)
  208. {
  209. try
  210. {
  211. // Are all these clones deep enough?
  212. ResultTreeHandler rtf = transformer.getResultTreeHandler();
  213. if (rtf != null)
  214. {
  215. rtf.m_eventCount = 1; //1 for start document event! m_eventCount;
  216. // yuck. No clone. Hope this is good enough.
  217. rtf.m_nsSupport = new NamespaceSupport();
  218. Enumeration prefixes = m_nsSupport.getPrefixes();
  219. while (prefixes.hasMoreElements())
  220. {
  221. String prefix = (String) prefixes.nextElement();
  222. String uri = m_nsSupport.getURI(prefix);
  223. rtf.m_nsSupport.declarePrefix(prefix, uri);
  224. }
  225. rtf.m_nsContextPushed = m_nsContextPushed;
  226. }
  227. XPathContext xpc = transformer.getXPathContext();
  228. xpc.setVarStack((VariableStack) m_variableStacks.clone());
  229. xpc.setCurrentNodeStack((IntStack) m_currentNodes.clone());
  230. xpc.setCurrentExpressionNodeStack(
  231. (IntStack) m_currentExpressionNodes.clone());
  232. xpc.setContextNodeListsStack((Stack) m_contextNodeLists.clone());
  233. if (m_contextNodeList != null)
  234. xpc.pushContextNodeList((DTMIterator) m_contextNodeList.clone());
  235. xpc.setAxesIteratorStackStacks((Stack) m_axesIteratorStack.clone());
  236. transformer.m_currentTemplateRuleIsNull =
  237. (BoolStack) m_currentTemplateRuleIsNull.clone();
  238. transformer.m_currentTemplateElements =
  239. (ObjectStack) m_currentTemplateElements.clone();
  240. transformer.m_currentMatchTemplates =
  241. (Stack) m_currentMatchTemplates.clone();
  242. transformer.m_currentMatchedNodes =
  243. (NodeVector) m_currentMatchNodes.clone();
  244. transformer.m_countersTable = (CountersTable) m_countersTable.clone();
  245. if (m_attrSetStack != null)
  246. transformer.m_attrSetStack = (Stack) m_attrSetStack.clone();
  247. }
  248. catch (CloneNotSupportedException cnse)
  249. {
  250. throw new org.apache.xml.utils.WrappedRuntimeException(cnse);
  251. }
  252. }
  253. }