You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xalan.apache.org by sb...@apache.org on 2002/06/08 22:41:00 UTC
cvs commit: xml-xalan/java/src/org/apache/xml/utils URI.java
sboag 2002/06/08 13:41:00
Modified: java/src/org/apache/xalan/templates ElemApplyTemplates.java
ElemTemplate.java
java/src/org/apache/xalan/transformer StackGuard.java
TransformerImpl.java
java/src/org/apache/xml/utils URI.java
Log:
Add back a certain amount of StackGuard functionality. Instead of checking
counts where both the node and the xsl:template are the same... (we would
really need also to check that parameters are the same), just check for recursion
of templates. We can make it fancier over time to also check for same params,
but checking of the current node may be harder. I was only able to set the
recursion limit to about 475 before I would get stack overflow errors anyway.
I also did some stuff where the error messages should be more consistent by
making sure some of the arrays are reset in XPathContex, which may address
some of the inconsistency problems that Christina reported.
may address part of the problem that Christina was having.
Revision Changes Path
1.24 +28 -28 xml-xalan/java/src/org/apache/xalan/templates/ElemApplyTemplates.java
Index: ElemApplyTemplates.java
===================================================================
RCS file: /home/cvs/xml-xalan/java/src/org/apache/xalan/templates/ElemApplyTemplates.java,v
retrieving revision 1.23
retrieving revision 1.24
diff -u -r1.23 -r1.24
--- ElemApplyTemplates.java 10 Apr 2002 20:33:58 -0000 1.23
+++ ElemApplyTemplates.java 8 Jun 2002 20:41:00 -0000 1.24
@@ -57,32 +57,21 @@
package org.apache.xalan.templates;
//import org.w3c.dom.*;
-import org.xml.sax.*;
-
-import org.apache.xpath.*;
-import org.apache.xpath.objects.XObject;
-
import java.util.Vector;
-import org.apache.xalan.trace.TracerEvent;
-import org.apache.xml.utils.QName;
-import org.apache.xalan.res.XSLTErrorResources;
-import org.apache.xpath.VariableStack;
-import org.apache.xalan.transformer.TransformerImpl;
+import javax.xml.transform.TransformerException;
import org.apache.xalan.transformer.ResultTreeHandler;
-import org.apache.xalan.transformer.ClonerToResultTree;
-import org.apache.xml.dtm.DTMAxisTraverser;
-import org.apache.xml.dtm.DTMIterator;
+import org.apache.xalan.transformer.StackGuard;
+import org.apache.xalan.transformer.TransformerImpl;
import org.apache.xml.dtm.DTM;
-import org.apache.xml.dtm.Axis;
-
-import javax.xml.transform.TransformerException;
-import javax.xml.transform.SourceLocator;
-
-import org.apache.xml.dtm.DTMManager;
-
-// Experemental
-import org.apache.xml.dtm.ref.ExpandedNameTable;
+import org.apache.xml.dtm.DTMIterator;
+import org.apache.xml.utils.QName;
+import org.apache.xpath.VariableStack;
+import org.apache.xpath.XPath;
+import org.apache.xpath.XPathContext;
+import org.apache.xpath.objects.XObject;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.SAXException;
/**
* <meta name="usage" content="advanced"/>
@@ -257,6 +246,8 @@
VariableStack vars = xctxt.getVarStack();
int nParams = getParamElemCount();
int thisframe = vars.getStackFrame();
+ StackGuard guard = transformer.getStackGuard();
+ boolean check = (guard.getRecursionLimit() > -1) ? true : false;
try
{
@@ -367,8 +358,13 @@
continue;
}
}
-
+ else
+ {
+ transformer.setCurrentElement(template);
+ }
+
transformer.pushPairCurrentMatched(template, child);
+ transformer.getStackGuard().checkForInfinateLoop();
int currentFrameBottom; // See comment with unlink, below
if(template.m_frameSize > 0)
@@ -412,9 +408,6 @@
else
currentFrameBottom = 0;
- // if (check)
- // guard.push(this, child);
-
// Fire a trace event for the template.
if (TransformerImpl.S_DEBUG)
transformer.getTraceManager().fireTraceEvent(template);
@@ -426,8 +419,15 @@
t != null; t = t.m_nextSibling)
{
xctxt.setSAXLocator(t);
- transformer.setCurrentElement(t);
- t.execute(transformer);
+ try
+ {
+ transformer.pushElemTemplateElement(t);
+ t.execute(transformer);
+ }
+ finally
+ {
+ transformer.popElemTemplateElement();
+ }
}
if (TransformerImpl.S_DEBUG)
1.16 +4 -1 xml-xalan/java/src/org/apache/xalan/templates/ElemTemplate.java
Index: ElemTemplate.java
===================================================================
RCS file: /home/cvs/xml-xalan/java/src/org/apache/xalan/templates/ElemTemplate.java,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -r1.15 -r1.16
--- ElemTemplate.java 24 Mar 2002 00:57:53 -0000 1.15
+++ ElemTemplate.java 8 Jun 2002 20:41:00 -0000 1.16
@@ -420,7 +420,10 @@
TransformerImpl transformer)
throws TransformerException
{
- XPathContext xctxt = transformer.getXPathContext();
+ XPathContext xctxt = transformer.getXPathContext();
+
+ transformer.getStackGuard().checkForInfinateLoop();
+
xctxt.pushRTFContext();
if (TransformerImpl.S_DEBUG)
1.7 +59 -112 xml-xalan/java/src/org/apache/xalan/transformer/StackGuard.java
Index: StackGuard.java
===================================================================
RCS file: /home/cvs/xml-xalan/java/src/org/apache/xalan/transformer/StackGuard.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- StackGuard.java 12 Jun 2001 19:15:27 -0000 1.6
+++ StackGuard.java 8 Jun 2002 20:41:00 -0000 1.7
@@ -59,13 +59,12 @@
//import org.w3c.dom.Node;
//import org.w3c.dom.Text;
//import org.w3c.dom.Element;
-import org.apache.xml.dtm.DTM;
-
import java.io.PrintWriter;
import java.io.StringWriter;
import javax.xml.transform.TransformerException;
-
+import org.apache.xalan.templates.Constants;
+import org.apache.xalan.templates.ElemTemplate;
import org.apache.xalan.templates.ElemTemplateElement;
/**
@@ -84,6 +83,8 @@
* Post version 1.0.0, we'll make this a runtime feature.
*/
public static int m_recursionLimit = -1;
+
+ TransformerImpl m_transformer;
/**
* Get the recursion limit.
@@ -119,21 +120,6 @@
m_recursionLimit = limit;
}
- /** Stylesheet Template node */
- ElemTemplateElement m_xslRule;
-
- /** Source node */
- int m_sourceXML;
-
- /** Stack where ElemTempalteElements will be pushed */
- java.util.Stack stack = new java.util.Stack();
-
- /**
- * Constructor StackGuard
- *
- */
- public StackGuard(){}
-
/**
* Constructor StackGuard
*
@@ -141,10 +127,9 @@
* @param xslTemplate Current template node
* @param sourceXML Source Node
*/
- public StackGuard(ElemTemplateElement xslTemplate, int sourceXML)
+ public StackGuard(TransformerImpl transformerImpl)
{
- m_xslRule = xslTemplate;
- m_sourceXML = sourceXML;
+ m_transformer = transformerImpl;
}
/**
@@ -155,41 +140,42 @@
*
* @return True if the given object matches this StackGuard object
*/
- public boolean equals(Object obj)
+ public int countLikeTemplates(ElemTemplate templ, int pos)
{
-
- if (((StackGuard) obj).m_xslRule.equals(m_xslRule)
- && ((StackGuard) obj).m_sourceXML == m_sourceXML)
+ ElemTemplateElement[] elems = m_transformer.getCurrentTemplateElements();
+ int count = 1;
+ for (int i = pos-1; i >= 0; i--)
{
- return true;
+ if(elems[i] == templ)
+ count++;
}
-
- return false;
+
+ return count;
}
+
/**
- * Output diagnostics if in an infinite loop
- *
- *
- * @param pw Non-null PrintWriter instance to use
+ * Get the next named or match template down from and including
+ * the given position.
+ * @param pos the current index position in the stack.
+ * @return null if no matched or named template found, otherwise
+ * the next named or matched template at or below the position.
*/
- public void print(PrintWriter pw)
+ private ElemTemplate getNextMatchOrNamedTemplate(int pos)
{
-
- // for the moment, these diagnostics are really bad...
- // %DTBD% We need an execution context.
-// if (m_sourceXML instanceof Text)
-// {
-// Text tx = (Text) m_sourceXML;
-//
-// pw.println(tx.getData());
-// }
-// else if (m_sourceXML instanceof Element)
-// {
-// Element elem = (Element) m_sourceXML;
-//
-// pw.println(elem.getNodeName());
-// }
+ ElemTemplateElement[] elems = m_transformer.getCurrentTemplateElements();
+ for (int i = pos; i >= 0; i--)
+ {
+ ElemTemplateElement elem = elems[i];
+ if(null != elem)
+ {
+ if(elem.getXSLToken() == Constants.ELEMNAME_TEMPLATE)
+ {
+ return (ElemTemplate)elem;
+ }
+ }
+ }
+ return null;
}
/**
@@ -200,73 +186,34 @@
*
* @throws TransformerException
*/
- public void checkForInfinateLoop(StackGuard guard) throws TransformerException
+ public void checkForInfinateLoop() throws TransformerException
{
-
- int nRules = stack.size();
- int loopCount = 0;
-
- for (int i = (nRules - 1); i >= 0; i--)
+ int nTemplates = m_transformer.getCurrentTemplateElementsCount();
+ if(nTemplates < m_recursionLimit)
+ return;
+
+ if(m_recursionLimit <= 0)
+ return; // Safety check.
+
+ // loop from the top index down to the recursion limit (I don't think
+ // there's any need to go below that).
+ for (int i = (nTemplates - 1); i >= m_recursionLimit; i--)
{
- if (stack.elementAt(i).equals(guard))
- {
- loopCount++;
- }
-
- if (loopCount >= m_recursionLimit)
- {
-
- // Print out really bad diagnostics.
- StringWriter sw = new StringWriter();
- PrintWriter pw = new PrintWriter(sw);
-
- pw.println("Infinite loop diagnosed! Stack trace:");
-
- int k;
-
- for (k = 0; k < nRules; k++)
- {
- pw.println("Source Elem #" + k + " ");
-
- StackGuard guardOnStack = (StackGuard) stack.elementAt(i);
-
- guardOnStack.print(pw);
- }
-
- pw.println("Source Elem #" + k + " ");
- guard.print(pw);
- pw.println("End of infinite loop diagnosis.");
-
- throw new TransformerException(sw.getBuffer().toString());
- }
+ ElemTemplate template = getNextMatchOrNamedTemplate(i);
+
+ if(null == template)
+ break;
+
+ int loopCount = countLikeTemplates(template, i);
+
+ if (loopCount >= m_recursionLimit)
+ {
+ throw new TransformerException("Template nesting too deep. nesting = "+loopCount+
+ ", template "+((null == template.getName()) ? "name = " : "match = ")+
+ ((null != template.getName()) ? template.getName().toString()
+ : template.getMatch().getPatternString()));
+ }
}
}
- /**
- * Push in a StackGuard object mathing given template
- *
- *
- * @param xslTemplate Current template being processed
- * @param sourceXML Current Source Node
- *
- * @throws TransformerException
- */
- public void push(ElemTemplateElement xslTemplate, int sourceXML)
- throws TransformerException
- {
-
- StackGuard guard = new StackGuard(xslTemplate, sourceXML);
-
- checkForInfinateLoop(guard);
- stack.push(guard);
- }
-
- /**
- * Pop out Stack of StackGuard objects
- *
- */
- public void pop()
- {
- stack.pop();
- }
}
1.132 +26 -3 xml-xalan/java/src/org/apache/xalan/transformer/TransformerImpl.java
Index: TransformerImpl.java
===================================================================
RCS file: /home/cvs/xml-xalan/java/src/org/apache/xalan/transformer/TransformerImpl.java,v
retrieving revision 1.131
retrieving revision 1.132
diff -u -r1.131 -r1.132
--- TransformerImpl.java 5 Apr 2002 19:59:12 -0000 1.131
+++ TransformerImpl.java 8 Jun 2002 20:41:00 -0000 1.132
@@ -281,7 +281,7 @@
* Object to guard agains infinite recursion when
* doing queries.
*/
- private StackGuard m_stackGuard = new StackGuard();
+ private StackGuard m_stackGuard;
/**
* Output handler to bottleneck SAX events.
@@ -396,6 +396,7 @@
setStylesheet(stylesheet);
setXPathContext(new XPathContext(this));
getXPathContext().setNamespaceContext(stylesheet);
+ m_stackGuard = new StackGuard(this);
}
/**
@@ -423,7 +424,6 @@
// I need to look more carefully at which of these really
// needs to be reset.
m_countersTable = null;
- m_stackGuard = new StackGuard();
m_xcontext.reset();
@@ -440,7 +440,7 @@
m_currentMatchTemplates.removeAllElements();
m_currentMatchedNodes.removeAllElements();
-
+
m_resultTreeHandler = null;
m_outputTarget = null;
m_keyManager = new KeyManager();
@@ -2396,6 +2396,29 @@
}
}
return elems;
+ }
+
+ /**
+ * Get the count of how many elements are
+ * active.
+ * @return The number of active elements on
+ * the currentTemplateElements stack.
+ */
+ public int getCurrentTemplateElementsCount()
+ {
+ return m_currentTemplateElementsTop;
+ }
+
+
+ /**
+ * Get the count of how many elements are
+ * active.
+ * @return The number of active elements on
+ * the currentTemplateElements stack.
+ */
+ public ElemTemplateElement[] getCurrentTemplateElements()
+ {
+ return m_currentTemplateElements;
}
/**
1.7 +0 -1 xml-xalan/java/src/org/apache/xml/utils/URI.java
Index: URI.java
===================================================================
RCS file: /home/cvs/xml-xalan/java/src/org/apache/xml/utils/URI.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- URI.java 28 Jul 2001 00:26:00 -0000 1.6
+++ URI.java 8 Jun 2002 20:41:00 -0000 1.7
@@ -91,7 +91,6 @@
* default port for a specific scheme). Rather, it only knows the
* grammar and basic set of operations that can be applied to a URI.
*
- * @version $Id: URI.java,v 1.6 2001/07/28 00:26:00 mmidy Exp $
*
*/
public class URI implements Serializable
---------------------------------------------------------------------
To unsubscribe, e-mail: xalan-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xalan-cvs-help@xml.apache.org