| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.style; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.Expression; |
| 11 |
import net.sf.saxon.lib.NamespaceConstant; |
| 12 |
import net.sf.saxon.om.AttributeCollection; |
| 13 |
import net.sf.saxon.om.AxisInfo; |
| 14 |
import net.sf.saxon.om.StructuredQName; |
| 15 |
import net.sf.saxon.pattern.Pattern; |
| 16 |
import net.sf.saxon.trans.SaxonErrorCode; |
| 17 |
import net.sf.saxon.trans.XPathException; |
| 18 |
import net.sf.saxon.value.Whitespace; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public class XSLAccumulatorRule extends StyleElement { |
| 25 |
|
| 26 |
private Pattern match; |
| 27 |
private boolean postDescent; |
| 28 |
private Expression select; |
| 29 |
private boolean capture; |
| 30 |
|
| 31 |
public void prepareAttributes() throws XPathException { |
| 32 |
|
| 33 |
String matchAtt = null; |
| 34 |
String newValueAtt = null; |
| 35 |
|
| 36 |
AttributeCollection atts = getAttributeList(); |
| 37 |
for (int a = 0; a < atts.getLength(); a++) { |
| 38 |
if (atts.getURI(a).isEmpty()) { |
| 39 |
String f = atts.getQName(a); |
| 40 |
if (f.equals("match")) { |
| 41 |
matchAtt = atts.getValue(a); |
| 42 |
} else if (f.equals("select")) { |
| 43 |
newValueAtt = atts.getValue(a); |
| 44 |
select = makeExpression(newValueAtt, a); |
| 45 |
} else if (f.equals("phase")) { |
| 46 |
String phaseAtt = Whitespace.trim(atts.getValue(a)); |
| 47 |
if ("start".equals(phaseAtt)) { |
| 48 |
postDescent = false; |
| 49 |
} else if ("end".equals(phaseAtt)) { |
| 50 |
postDescent = true; |
| 51 |
} else { |
| 52 |
postDescent = true; |
| 53 |
compileError("phase must be 'start' or 'end'", "XTSE0020"); |
| 54 |
} |
| 55 |
} else { |
| 56 |
checkUnknownAttribute(atts.getNodeName(a)); |
| 57 |
} |
| 58 |
} else if (atts.getURI(a).equals(NamespaceConstant.SAXON)) { |
| 59 |
if (atts.getLocalName(a).equals("capture")) { |
| 60 |
capture = processBooleanAttribute("saxon:capture", atts.getValue(a)); |
| 61 |
} else { |
| 62 |
checkUnknownAttribute(atts.getNodeName(a)); |
| 63 |
} |
| 64 |
} else { |
| 65 |
checkUnknownAttribute(atts.getNodeName(a)); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
if (matchAtt == null) { |
| 70 |
reportAbsence("match"); |
| 71 |
matchAtt = "non-existent-element"; |
| 72 |
} |
| 73 |
match = makePattern(matchAtt, "match"); |
| 74 |
|
| 75 |
if (capture && !postDescent) { |
| 76 |
compileWarning("saxon:capture has no effect on a pre-descent accumulator rule", |
| 77 |
SaxonErrorCode.SXWN9000); |
| 78 |
} |
| 79 |
|
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
public void validate(ComponentDeclaration decl) throws XPathException { |
| 84 |
select = typeCheck("select", select); |
| 85 |
match = typeCheck("match", match); |
| 86 |
if (select != null && iterateAxis(AxisInfo.CHILD).next() != null) { |
| 87 |
compileError("If the xsl:accumulator-rule element has a select attribute then it must have no children"); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
public Expression getNewValueExpression(Compilation compilation, ComponentDeclaration decl) throws XPathException { |
| 92 |
if (select == null) { |
| 93 |
select = compileSequenceConstructor(compilation, decl, true); |
| 94 |
} |
| 95 |
return select; |
| 96 |
} |
| 97 |
|
| 98 |
public Pattern getMatch() { |
| 99 |
return match; |
| 100 |
} |
| 101 |
|
| 102 |
public void setMatch(Pattern match) { |
| 103 |
this.match = match; |
| 104 |
} |
| 105 |
|
| 106 |
public boolean isPostDescent() { |
| 107 |
return postDescent; |
| 108 |
} |
| 109 |
|
| 110 |
public void setPostDescent(boolean postDescent) { |
| 111 |
this.postDescent = postDescent; |
| 112 |
} |
| 113 |
|
| 114 |
public boolean isCapture() { |
| 115 |
return capture; |
| 116 |
} |
| 117 |
|
| 118 |
public Expression getSelect() { |
| 119 |
return select; |
| 120 |
} |
| 121 |
|
| 122 |
public void setSelect(Expression select) { |
| 123 |
this.select = select; |
| 124 |
} |
| 125 |
|
| 126 |
public SourceBinding hasImplicitBinding(StructuredQName name) { |
| 127 |
if (name.getLocalPart().equals("value") && name.hasURI("")) { |
| 128 |
SourceBinding sb = new SourceBinding(this); |
| 129 |
sb.setVariableQName(new StructuredQName("", "", "value")); |
| 130 |
assert ((XSLAccumulator)getParent()) != null; |
| 131 |
sb.setDeclaredType(((XSLAccumulator)getParent()).getResultType()); |
| 132 |
sb.setProperty(SourceBinding.IMPLICITLY_DECLARED, true); |
| 133 |
return sb; |
| 134 |
} else { |
| 135 |
return null; |
| 136 |
} |
| 137 |
} |
| 138 |
} |