mmidy 00/10/16 15:56:05
Modified: java/src/org/apache/xalan/templates TemplateList.java
java/src/org/apache/xalan/transformer TransformerImpl.java
java/src/org/apache/xpath/patterns NodeTest.java
StepPattern.java UnionPattern.java
Log:
Build template list by priority then document order. Misc changes to be able
to get a template score for both step and union patterns.
Revision Changes Path
1.11 +97 -40
xml-xalan/java/src/org/apache/xalan/templates/TemplateList.java
Index: TemplateList.java
===================================================================
RCS file:
/home/cvs/xml-xalan/java/src/org/apache/xalan/templates/TemplateList.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- TemplateList.java 2000/10/16 16:14:51 1.10
+++ TemplateList.java 2000/10/16 22:56:03 1.11
@@ -65,8 +65,12 @@
import org.apache.xalan.utils.QName;
import org.apache.xpath.XPath;
import org.apache.xpath.compiler.PsuedoNames;
+import org.apache.xpath.patterns.NodeTest;
+import org.apache.xpath.Expression;
import org.apache.xalan.res.XSLTErrorResources;
import org.apache.xpath.XPathContext;
+import org.apache.xpath.patterns.StepPattern;
+import org.apache.xpath.patterns.UnionPattern;
/**
* <meta name="usage" content="advanced"/>
@@ -121,54 +125,97 @@
template.error(XSLTErrorResources.ER_DUPLICATE_NAMED_TEMPLATE, new
Object[]{template.getName()});
}
- if(null != template.getMatch())
+ XPath matchXPath = template.getMatch();
+ if(null != matchXPath)
{
- Vector strings = template.getMatch().getTargetElementStrings();
- if(null != strings)
+ Expression matchExpr = matchXPath.getExpression();
+ if(matchExpr instanceof StepPattern)
{
- int nTargets = strings.size();
- for(int stringIndex = 0; stringIndex < nTargets; stringIndex++)
+ insertPatternInTable((StepPattern)matchExpr, template, pos);
+ }
+ if(matchExpr instanceof UnionPattern)
+ {
+ UnionPattern upat = (UnionPattern)matchExpr;
+ StepPattern[] pats = upat.getPatterns();
+ int n = pats.length;
+ for(int i = 0; i < n; i++)
{
- String target = (String)strings.elementAt(stringIndex);
-
- Object newMatchPat = new
MatchPattern2(template.getMatch().getPatternString(),
- template.getMatch(),
- template, pos,
- target, m_stylesheet);
-
- // See if there's already one there
- Object val = m_patternTable.get(target);
- if(null == val)
- {
- // System.out.println("putting: "+target);
- m_patternTable.put(target, newMatchPat);
- }
- else
- {
- // find the tail of the list
- MatchPattern2 matchPat = (MatchPattern2)val;
- //((MatchPattern2)newMatchPat).setNext(matchPat);
- //m_patternTable.put(target, newMatchPat);
- //*
- // Sort by priority first, then by document order.
- double priority =
((MatchPattern2)newMatchPat).getTemplate().getPriority();
- MatchPattern2 next;
- while( ((next = matchPat.getNext()) != null) &&
- (matchPat.getTemplate().getPriority() >= priority) )
-
- {
- matchPat = next;
- }
- // System.out.println("appending: "+target+" to
"+matchPat.getPattern());
- matchPat.setNext((MatchPattern2)newMatchPat);
- //*/
- }
+ insertPatternInTable(pats[i], template, pos);
}
}
+ else
+ {
+ // TODO: assert error
+ }
}
}
/**
+ * Add a template to the template list.
+ */
+ public void insertPatternInTable(StepPattern pattern, ElemTemplate
template, int pos)
+ {
+ String target = pattern.getTargetString();
+ if(null != target)
+ {
+ Object newMatchPat = new
MatchPattern2(template.getMatch().getPatternString(),
+ template.getMatch(),
+ template, pos,
+ target, m_stylesheet, pattern);
+
+ // See if there's already one there
+ Object val = m_patternTable.get(target);
+ if(null == val)
+ {
+ // System.out.println("putting: "+target);
+ m_patternTable.put(target, newMatchPat);
+ }
+ else
+ {
+ MatchPattern2 matchPat = (MatchPattern2)val;
+ //((MatchPattern2)newMatchPat).setNext(matchPat);
+ //m_patternTable.put(target, newMatchPat);
+ //*
+ // Sort by priority first, then by document order.
+ double priority = getPriorityOrScore((MatchPattern2)newMatchPat);
+ MatchPattern2 next;
+ while( ((next = matchPat.getNext()) != null) &&
+ (getPriorityOrScore(next) > priority) )
+
+ {
+ matchPat = next;
+ }
+ // System.out.println("appending: "+target+" to
"+matchPat.getPattern());
+
+ // This check is just to catch the first template in the list
+ // It's priority was not checked against the new template
+ if ( (getPriorityOrScore(matchPat) <= priority))
+ {
+ ((MatchPattern2)newMatchPat).setNext(matchPat);
+ m_patternTable.put(target, newMatchPat);
+ }
+ else
+ {
+ ((MatchPattern2)newMatchPat).setNext(next);
+ matchPat.setNext((MatchPattern2)newMatchPat);
+ }
+ }
+ }
+ }
+
+ private double getPriorityOrScore(MatchPattern2 matchPat)
+ {
+ double priority = matchPat.getTemplate().getPriority();
+ if(priority == XPath.MATCH_SCORE_NONE)
+ {
+ Expression ex = matchPat.getStepPattern();
+ if (ex instanceof NodeTest)
+ return ((NodeTest)ex).getDefaultScore();
+ }
+ return priority;
+ }
+
+ /**
* Locate a macro via the "name" attribute.
* @exception XSLProcessorException thrown if the active ProblemListener
and XPathContext decide
* the error condition is severe enough to halt processing.
@@ -350,6 +397,12 @@
bestMatchedRule = rule;
bestMatchedPattern = matchPat;
}
+ // We have found a match. If not issueing conflict warnings,
+ // stop right here. This should be the best match because
+ // the template list was set up with the highest priority
+ // (including document order priority) template at the top.
+ if (quietConflictWarnings)
+ break;
}
// Date date2 = new Date();
// m_totalTimePatternMatching+=(date2.getTime() -
date1.getTime());
@@ -625,7 +678,7 @@
* patterns (for compatibility with old syntax).
*/
MatchPattern2(String pat, XPath exp, ElemTemplate template, int
posInStylesheet,
- String targetString, Stylesheet stylesheet)
+ String targetString, Stylesheet stylesheet, StepPattern
pattern )
{
m_pattern = pat;
m_template = template;
@@ -633,6 +686,7 @@
m_targetString = targetString;
m_stylesheet = stylesheet;
m_expression = exp;
+ m_stepPattern = pattern;
}
Stylesheet m_stylesheet;
@@ -641,6 +695,9 @@
XPath m_expression;
public XPath getExpression() { return m_expression; }
+
+ StepPattern m_stepPattern;
+ public StepPattern getStepPattern() { return m_stepPattern; }
int m_posInStylesheet;
1.35 +1 -1
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.34
retrieving revision 1.35
diff -u -r1.34 -r1.35
--- TransformerImpl.java 2000/10/16 16:13:41 1.34
+++ TransformerImpl.java 2000/10/16 22:56:04 1.35
@@ -1663,7 +1663,7 @@
* If this is set to true, do not warn about pattern
* match conflicts.
*/
- private boolean m_quietConflictWarnings = false;
+ private boolean m_quietConflictWarnings = true;
/**
* Get quietConflictWarnings property.
1.7 +5 -0
xml-xalan/java/src/org/apache/xpath/patterns/NodeTest.java
Index: NodeTest.java
===================================================================
RCS file:
/home/cvs/xml-xalan/java/src/org/apache/xpath/patterns/NodeTest.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- NodeTest.java 2000/09/29 16:25:24 1.6
+++ NodeTest.java 2000/10/16 22:56:04 1.7
@@ -116,6 +116,11 @@
m_score = SCORE_QNAME;
}
+ public double getDefaultScore()
+ {
+ return m_score.num();
+ }
+
public static void debugWhatToShow(int whatToShow)
{
java.util.Vector v = new java.util.Vector();
1.8 +39 -1
xml-xalan/java/src/org/apache/xpath/patterns/StepPattern.java
Index: StepPattern.java
===================================================================
RCS file:
/home/cvs/xml-xalan/java/src/org/apache/xpath/patterns/StepPattern.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- StepPattern.java 2000/10/06 14:22:47 1.7
+++ StepPattern.java 2000/10/16 22:56:04 1.8
@@ -5,6 +5,7 @@
import org.apache.xpath.XPathContext;
import org.apache.xalan.utils.PrefixResolver;
import org.apache.xpath.axes.SubContextList;
+import org.apache.xpath.compiler.PsuedoNames;
import org.w3c.dom.traversal.NodeFilter;
import org.w3c.dom.Node;
@@ -22,6 +23,42 @@
super(whatToShow);
}
+ String m_targetString; // only calculate on head
+
+ public void calcTargetString()
+ {
+ int whatToShow = getWhatToShow();
+ switch(whatToShow)
+ {
+ case NodeFilter.SHOW_COMMENT:
+ m_targetString = PsuedoNames.PSEUDONAME_COMMENT;
+ break;
+ case NodeFilter.SHOW_TEXT:
+ m_targetString = PsuedoNames.PSEUDONAME_TEXT;
+ break;
+ case NodeFilter.SHOW_ALL:
+ m_targetString = PsuedoNames.PSEUDONAME_ANY;
+ break;
+ case NodeFilter.SHOW_DOCUMENT:
+ m_targetString = PsuedoNames.PSEUDONAME_ROOT;
+ break;
+ case NodeFilter.SHOW_ELEMENT:
+ if(this.WILD == m_name)
+ m_targetString = PsuedoNames.PSEUDONAME_ANY;
+ else
+ m_targetString = m_name;
+ break;
+ default:
+ m_targetString = PsuedoNames.PSEUDONAME_ANY;
+ break;
+ }
+ }
+
+ public String getTargetString()
+ {
+ return m_targetString;
+ }
+
/**
* Reference to nodetest and predicate for
* parent or ancestor.
@@ -56,10 +93,11 @@
*/
protected final void calcScore()
{
- if(getPredicateCount() > 0)
+ if((getPredicateCount() > 0) || (null != m_relativePathPattern))
m_score = SCORE_OTHER;
else
super.calcScore();
+ calcTargetString();
}
1.2 +5 -0
xml-xalan/java/src/org/apache/xpath/patterns/UnionPattern.java
Index: UnionPattern.java
===================================================================
RCS file:
/home/cvs/xml-xalan/java/src/org/apache/xpath/patterns/UnionPattern.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- UnionPattern.java 2000/07/05 14:48:55 1.1
+++ UnionPattern.java 2000/10/16 22:56:04 1.2
@@ -13,6 +13,11 @@
m_patterns = patterns;
}
+ public StepPattern[] getPatterns()
+ {
+ return m_patterns;
+ }
+
/**
* Test a node to see if it matches the given node test.
* @param xpath The xpath that is executing.