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.w3c.dom.Node;
  59. //import org.w3c.dom.Text;
  60. //import org.w3c.dom.Element;
  61. import javax.xml.transform.TransformerException;
  62. import org.apache.xalan.res.XSLMessages;
  63. import org.apache.xalan.templates.Constants;
  64. import org.apache.xalan.templates.ElemTemplate;
  65. import org.apache.xalan.templates.ElemTemplateElement;
  66. import org.apache.xml.utils.ObjectStack;
  67. /**
  68. * Class to guard against recursion getting too deep.
  69. */
  70. public class StackGuard
  71. {
  72. /**
  73. * Used for infinite loop check. If the value is -1, do not
  74. * check for infinite loops. Anyone who wants to enable that
  75. * check should change the value of this variable to be the
  76. * level of recursion that they want to check. Be careful setting
  77. * this variable, if the number is too low, it may report an
  78. * infinite loop situation, when there is none.
  79. * Post version 1.0.0, we'll make this a runtime feature.
  80. */
  81. public static int m_recursionLimit = -1;
  82. TransformerImpl m_transformer;
  83. /**
  84. * Get the recursion limit.
  85. * Used for infinite loop check. If the value is -1, do not
  86. * check for infinite loops. Anyone who wants to enable that
  87. * check should change the value of this variable to be the
  88. * level of recursion that they want to check. Be careful setting
  89. * this variable, if the number is too low, it may report an
  90. * infinite loop situation, when there is none.
  91. * Post version 1.0.0, we'll make this a runtime feature.
  92. *
  93. * @return The recursion limit.
  94. */
  95. public int getRecursionLimit()
  96. {
  97. return m_recursionLimit;
  98. }
  99. /**
  100. * Set the recursion limit.
  101. * Used for infinite loop check. If the value is -1, do not
  102. * check for infinite loops. Anyone who wants to enable that
  103. * check should change the value of this variable to be the
  104. * level of recursion that they want to check. Be careful setting
  105. * this variable, if the number is too low, it may report an
  106. * infinite loop situation, when there is none.
  107. * Post version 1.0.0, we'll make this a runtime feature.
  108. *
  109. * @param limit The recursion limit.
  110. */
  111. public void setRecursionLimit(int limit)
  112. {
  113. m_recursionLimit = limit;
  114. }
  115. /**
  116. * Constructor StackGuard
  117. *
  118. *
  119. * @param xslTemplate Current template node
  120. * @param sourceXML Source Node
  121. */
  122. public StackGuard(TransformerImpl transformerImpl)
  123. {
  124. m_transformer = transformerImpl;
  125. }
  126. /**
  127. * Overide equal method for StackGuard objects
  128. *
  129. *
  130. * @param obj StackGuard object to compare
  131. *
  132. * @return True if the given object matches this StackGuard object
  133. */
  134. public int countLikeTemplates(ElemTemplate templ, int pos)
  135. {
  136. ObjectStack elems = m_transformer.getCurrentTemplateElements();
  137. int count = 1;
  138. for (int i = pos-1; i >= 0; i--)
  139. {
  140. if((ElemTemplateElement)elems.elementAt(i) == templ)
  141. count++;
  142. }
  143. return count;
  144. }
  145. /**
  146. * Get the next named or match template down from and including
  147. * the given position.
  148. * @param pos the current index position in the stack.
  149. * @return null if no matched or named template found, otherwise
  150. * the next named or matched template at or below the position.
  151. */
  152. private ElemTemplate getNextMatchOrNamedTemplate(int pos)
  153. {
  154. ObjectStack elems = m_transformer.getCurrentTemplateElements();
  155. for (int i = pos; i >= 0; i--)
  156. {
  157. ElemTemplateElement elem = (ElemTemplateElement) elems.elementAt(i);
  158. if(null != elem)
  159. {
  160. if(elem.getXSLToken() == Constants.ELEMNAME_TEMPLATE)
  161. {
  162. return (ElemTemplate)elem;
  163. }
  164. }
  165. }
  166. return null;
  167. }
  168. /**
  169. * Check if we are in an infinite loop
  170. *
  171. *
  172. * @param guard Current StackGuard object (matching current template)
  173. *
  174. * @throws TransformerException
  175. */
  176. public void checkForInfinateLoop() throws TransformerException
  177. {
  178. int nTemplates = m_transformer.getCurrentTemplateElementsCount();
  179. if(nTemplates < m_recursionLimit)
  180. return;
  181. if(m_recursionLimit <= 0)
  182. return; // Safety check.
  183. // loop from the top index down to the recursion limit (I don't think
  184. // there's any need to go below that).
  185. for (int i = (nTemplates - 1); i >= m_recursionLimit; i--)
  186. {
  187. ElemTemplate template = getNextMatchOrNamedTemplate(i);
  188. if(null == template)
  189. break;
  190. int loopCount = countLikeTemplates(template, i);
  191. if (loopCount >= m_recursionLimit)
  192. {
  193. // throw new TransformerException("Template nesting too deep. nesting = "+loopCount+
  194. // ", template "+((null == template.getName()) ? "name = " : "match = ")+
  195. // ((null != template.getName()) ? template.getName().toString()
  196. // : template.getMatch().getPatternString()));
  197. String idIs = XSLMessages.createMessage(((null != template.getName()) ? "nameIs" : "matchPatternIs"), null);
  198. Object[] msgArgs = new Object[]{ new Integer(loopCount), idIs,
  199. ((null != template.getName()) ? template.getName().toString()
  200. : template.getMatch().getPatternString()) };
  201. String msg = XSLMessages.createMessage("recursionTooDeep", msgArgs);
  202. throw new TransformerException(msg);
  203. }
  204. }
  205. }
  206. }