1 16 19 package org.apache.xalan.transformer; 20 21 import javax.xml.transform.TransformerException ; 22 23 import org.apache.xalan.res.XSLMessages; 24 import org.apache.xalan.templates.Constants; 25 import org.apache.xalan.templates.ElemTemplate; 26 import org.apache.xalan.templates.ElemTemplateElement; 27 import org.apache.xml.utils.ObjectStack; 28 29 32 public class StackGuard 33 { 34 35 44 public static int m_recursionLimit = -1; 45 46 TransformerImpl m_transformer; 47 48 60 public int getRecursionLimit() 61 { 62 return m_recursionLimit; 63 } 64 65 77 public void setRecursionLimit(int limit) 78 { 79 m_recursionLimit = limit; 80 } 81 82 89 public StackGuard(TransformerImpl transformerImpl) 90 { 91 m_transformer = transformerImpl; 92 } 93 94 102 public int countLikeTemplates(ElemTemplate templ, int pos) 103 { 104 ObjectStack elems = m_transformer.getCurrentTemplateElements(); 105 int count = 1; 106 for (int i = pos-1; i >= 0; i--) 107 { 108 if((ElemTemplateElement)elems.elementAt(i) == templ) 109 count++; 110 } 111 112 return count; 113 } 114 115 116 123 private ElemTemplate getNextMatchOrNamedTemplate(int pos) 124 { 125 ObjectStack elems = m_transformer.getCurrentTemplateElements(); 126 for (int i = pos; i >= 0; i--) 127 { 128 ElemTemplateElement elem = (ElemTemplateElement) elems.elementAt(i); 129 if(null != elem) 130 { 131 if(elem.getXSLToken() == Constants.ELEMNAME_TEMPLATE) 132 { 133 return (ElemTemplate)elem; 134 } 135 } 136 } 137 return null; 138 } 139 140 148 public void checkForInfinateLoop() throws TransformerException 149 { 150 int nTemplates = m_transformer.getCurrentTemplateElementsCount(); 151 if(nTemplates < m_recursionLimit) 152 return; 153 154 if(m_recursionLimit <= 0) 155 return; 157 for (int i = (nTemplates - 1); i >= m_recursionLimit; i--) 160 { 161 ElemTemplate template = getNextMatchOrNamedTemplate(i); 162 163 if(null == template) 164 break; 165 166 int loopCount = countLikeTemplates(template, i); 167 168 if (loopCount >= m_recursionLimit) 169 { 170 175 String idIs = XSLMessages.createMessage(((null != template.getName()) ? "nameIs" : "matchPatternIs"), null); 176 Object [] msgArgs = new Object []{ new Integer (loopCount), idIs, 177 ((null != template.getName()) ? template.getName().toString() 178 : template.getMatch().getPatternString()) }; 179 String msg = XSLMessages.createMessage("recursionTooDeep", msgArgs); 180 181 throw new TransformerException (msg); 182 } 183 } 184 } 185 186 } 187 | Popular Tags |