| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.style; |
| 9 |
|
| 10 |
import com.saxonica.ee.stream.PostureAndSweep; |
| 11 |
import com.saxonica.ee.stream.Streamability; |
| 12 |
import com.saxonica.ee.stream.Sweep; |
| 13 |
import com.saxonica.ee.trans.ContextItemStaticInfoEE; |
| 14 |
import net.sf.saxon.Configuration; |
| 15 |
import net.sf.saxon.expr.Component; |
| 16 |
import net.sf.saxon.expr.Expression; |
| 17 |
import net.sf.saxon.expr.StringLiteral; |
| 18 |
import net.sf.saxon.expr.accum.Accumulator; |
| 19 |
import net.sf.saxon.expr.accum.AccumulatorRegistry; |
| 20 |
import net.sf.saxon.expr.accum.AccumulatorRule; |
| 21 |
import net.sf.saxon.expr.instruct.Actor; |
| 22 |
import net.sf.saxon.expr.instruct.SlotManager; |
| 23 |
import net.sf.saxon.expr.parser.ExpressionTool; |
| 24 |
import net.sf.saxon.expr.parser.ExpressionVisitor; |
| 25 |
import net.sf.saxon.expr.parser.Optimizer; |
| 26 |
import net.sf.saxon.expr.parser.RoleDiagnostic; |
| 27 |
import net.sf.saxon.functions.AccumulatorFn; |
| 28 |
import net.sf.saxon.lib.Feature; |
| 29 |
import net.sf.saxon.lib.NamespaceConstant; |
| 30 |
import net.sf.saxon.om.*; |
| 31 |
import net.sf.saxon.pattern.NodeTest; |
| 32 |
import net.sf.saxon.pattern.Pattern; |
| 33 |
import net.sf.saxon.trace.LocationKind; |
| 34 |
import net.sf.saxon.trans.*; |
| 35 |
import net.sf.saxon.tree.iter.AxisIterator; |
| 36 |
import net.sf.saxon.type.AtomicType; |
| 37 |
import net.sf.saxon.type.ItemType; |
| 38 |
import net.sf.saxon.type.UType; |
| 39 |
import net.sf.saxon.value.SequenceType; |
| 40 |
import net.sf.saxon.value.StringValue; |
| 41 |
import net.sf.saxon.value.Whitespace; |
| 42 |
|
| 43 |
import java.util.ArrayList; |
| 44 |
import java.util.List; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public class XSLAccumulator extends StyleElement implements StylesheetComponent { |
| 51 |
|
| 52 |
private Accumulator accumulator = new Accumulator(); |
| 53 |
private boolean streamable; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public Actor getActor() { |
| 60 |
if (accumulator.getDeclaringComponent() == null) { |
| 61 |
accumulator.makeDeclaringComponent(Visibility.PRIVATE, getContainingPackage()); |
| 62 |
} |
| 63 |
return accumulator; |
| 64 |
} |
| 65 |
|
| 66 |
public SymbolicName getSymbolicName() { |
| 67 |
StructuredQName qname = accumulator.getAccumulatorName(); |
| 68 |
return qname==null ? null : new SymbolicName(StandardNames.XSL_ACCUMULATOR, null); |
| 69 |
} |
| 70 |
|
| 71 |
public void checkCompatibility(Component component) throws XPathException { |
| 72 |
|
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
@Override |
| 83 |
public boolean isDeclaration() { |
| 84 |
return true; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public void prepareSimpleAttributes() throws XPathException { |
| 95 |
|
| 96 |
AttributeCollection atts = getAttributeList(); |
| 97 |
for (int a = 0; a < atts.getLength(); a++) { |
| 98 |
String f = atts.getQName(a); |
| 99 |
if (f.equals("name")) { |
| 100 |
String name = Whitespace.trim(atts.getValue(a)); |
| 101 |
try { |
| 102 |
accumulator.setAccumulatorName(makeQName(name)); |
| 103 |
} catch (XPathException err) { |
| 104 |
compileError("Accumulator name " + Err.wrap(name) + " is not a valid QName", |
| 105 |
err.getErrorCodeQName()); |
| 106 |
accumulator.setAccumulatorName(new StructuredQName("saxon", NamespaceConstant.SAXON, "error-name")); |
| 107 |
} |
| 108 |
|
| 109 |
} else if (f.equals("streamable")) { |
| 110 |
accumulator.setDeclaredStreamable(false); |
| 111 |
streamable = processStreamableAtt(atts.getValue(a)); |
| 112 |
accumulator.setDeclaredStreamable(streamable); |
| 113 |
} else if (atts.getURI(a).equals(NamespaceConstant.SAXON) && atts.getLocalName(a).equals("trace")) { |
| 114 |
accumulator.setTracing(processBooleanAttribute("saxon:trace", atts.getValue(a))); |
| 115 |
} else { |
| 116 |
|
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
if (accumulator.getAccumulatorName() == null) { |
| 121 |
reportAbsence("name"); |
| 122 |
} |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
public void prepareAttributes() throws XPathException { |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
AttributeCollection atts = getAttributeList(); |
| 131 |
for (int a = 0; a < atts.getLength(); a++) { |
| 132 |
String f = atts.getQName(a); |
| 133 |
if (f.equals("name")) { |
| 134 |
|
| 135 |
} else if (f.equals("streamable")) { |
| 136 |
|
| 137 |
} else if (f.equals("initial-value")) { |
| 138 |
String exp = atts.getValue(a); |
| 139 |
accumulator.setInitialValueExpression(makeExpression(exp, a)); |
| 140 |
} else if (f.equals("as")) { |
| 141 |
String asAtt = atts.getValue(a); |
| 142 |
try { |
| 143 |
SequenceType requiredType = makeSequenceType(asAtt); |
| 144 |
accumulator.setType(requiredType); |
| 145 |
} catch (XPathException e) { |
| 146 |
compileErrorInAttribute(e.getMessage(), e.getErrorCodeLocalPart(), "as"); |
| 147 |
} |
| 148 |
} else if (atts.getURI(a).equals(NamespaceConstant.SAXON) && atts.getLocalName(a).equals("trace")) { |
| 149 |
accumulator.setTracing(processBooleanAttribute("saxon:trace", atts.getValue(a))); |
| 150 |
} else { |
| 151 |
checkUnknownAttribute(atts.getNodeName(a)); |
| 152 |
} |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
if (accumulator.getType() == null) { |
| 157 |
accumulator.setType(SequenceType.ANY_SEQUENCE); |
| 158 |
} |
| 159 |
|
| 160 |
if (accumulator.getInitialValueExpression() == null) { |
| 161 |
reportAbsence("initial-value"); |
| 162 |
StringLiteral zls = new StringLiteral(StringValue.EMPTY_STRING); |
| 163 |
zls.setRetainedStaticContext(makeRetainedStaticContext()); |
| 164 |
accumulator.setInitialValueExpression(zls); |
| 165 |
} |
| 166 |
|
| 167 |
} |
| 168 |
|
| 169 |
@Override |
| 170 |
public void compileDeclaration(Compilation compilation, ComponentDeclaration decl) throws XPathException { |
| 171 |
|
| 172 |
Configuration config = compilation.getConfiguration(); |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
{ |
| 177 |
accumulator.setPackageData(compilation.getPackageData()); |
| 178 |
accumulator.obtainDeclaringComponent(decl.getSourceElement()); |
| 179 |
Expression init = accumulator.getInitialValueExpression(); |
| 180 |
ExpressionVisitor visitor = ExpressionVisitor.make(getStaticContext()); |
| 181 |
init = init.typeCheck(visitor, config.getDefaultContextItemStaticInfo()); |
| 182 |
RoleDiagnostic role = new RoleDiagnostic(RoleDiagnostic.INSTRUCTION, "xsl:accumulator-rule/select", 0); |
| 183 |
init = config.getTypeChecker(false).staticTypeCheck(init, accumulator.getType(), role, visitor); |
| 184 |
init = init.optimize(visitor, config.getDefaultContextItemStaticInfo()); |
| 185 |
SlotManager stackFrameMap = config.makeSlotManager(); |
| 186 |
ExpressionTool.allocateSlots(init, 0, stackFrameMap); |
| 187 |
accumulator.setSlotManagerForInitialValueExpression(stackFrameMap); |
| 188 |
checkInitialStreamability(init); |
| 189 |
accumulator.addChildExpression(init); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
AxisIterator kids = iterateAxis(AxisInfo.CHILD); |
| 195 |
NodeInfo curr; |
| 196 |
while ((curr = kids.next()) != null) { |
| 197 |
if (curr instanceof XSLAccumulatorRule) { |
| 198 |
XSLAccumulatorRule rule = (XSLAccumulatorRule) curr; |
| 199 |
Pattern pattern = rule.getMatch(); |
| 200 |
Expression newValueExp = rule.getNewValueExpression(compilation, decl); |
| 201 |
ExpressionVisitor visitor = ExpressionVisitor.make(getStaticContext()); |
| 202 |
newValueExp = newValueExp.typeCheck(visitor, config.makeContextItemStaticInfo(pattern.getItemType(), false)); |
| 203 |
RoleDiagnostic role = new RoleDiagnostic(RoleDiagnostic.INSTRUCTION, "xsl:accumulator-rule/select", 0); |
| 204 |
newValueExp = config.getTypeChecker(false).staticTypeCheck(newValueExp, accumulator.getType(), role, visitor); |
| 205 |
newValueExp = newValueExp.optimize(visitor, getConfiguration().makeContextItemStaticInfo(pattern.getItemType(), false)); |
| 206 |
SlotManager stackFrameMap = getConfiguration().makeSlotManager(); |
| 207 |
stackFrameMap.allocateSlotNumber(new StructuredQName("", "", "value")); |
| 208 |
ExpressionTool.allocateSlots(newValueExp, 1, stackFrameMap); |
| 209 |
boolean isPreDescent = !rule.isPostDescent(); |
| 210 |
SimpleMode mode = isPreDescent ? accumulator.getPreDescentRules() : accumulator.getPostDescentRules(); |
| 211 |
AccumulatorRule action = new AccumulatorRule(newValueExp, stackFrameMap, rule.isPostDescent()); |
| 212 |
mode.addRule(pattern, action, decl.getModule(), decl.getModule().getPrecedence(), 1, 0, 0); |
| 213 |
|
| 214 |
checkRuleStreamability(rule, pattern, newValueExp); |
| 215 |
|
| 216 |
if (accumulator.isDeclaredStreamable() && rule.isPostDescent() && rule.isCapture()) { |
| 217 |
action.setCapturing(true); |
| 218 |
} |
| 219 |
|
| 220 |
ItemType itemType = pattern.getItemType(); |
| 221 |
if (itemType instanceof NodeTest) { |
| 222 |
if (!itemType.getUType().overlaps(UType.DOCUMENT.union(UType.CHILD_NODE_KINDS))) { |
| 223 |
rule.compileWarning("An accumulator rule that matches attribute or namespace nodes has no effect", "SXWN9999"); |
| 224 |
} |
| 225 |
} else if (itemType instanceof AtomicType) { |
| 226 |
rule.compileWarning("An accumulator rule that matches atomic values has no effect", "SXWN9999"); |
| 227 |
} |
| 228 |
|
| 229 |
accumulator.addChildExpression(newValueExp); |
| 230 |
accumulator.addChildExpression(pattern); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
accumulator.getPreDescentRules().allocateAllPatternSlots(); |
| 235 |
accumulator.getPostDescentRules().allocateAllPatternSlots(); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
public StructuredQName getObjectName() { |
| 246 |
StructuredQName qn = super.getObjectName(); |
| 247 |
if (qn == null) { |
| 248 |
String nameAtt = Whitespace.trim(getAttributeValue("", "name")); |
| 249 |
if (nameAtt == null) { |
| 250 |
return new StructuredQName("saxon", NamespaceConstant.SAXON, "badly-named-accumulator" + generateId()); |
| 251 |
} |
| 252 |
try { |
| 253 |
qn = makeQName(nameAtt); |
| 254 |
setObjectName(qn); |
| 255 |
} catch (XPathException err) { |
| 256 |
return new StructuredQName("saxon", NamespaceConstant.SAXON, "badly-named-accumulator" + generateId()); |
| 257 |
} |
| 258 |
} |
| 259 |
return qn; |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
public void index(ComponentDeclaration decl, PrincipalStylesheetModule top) throws XPathException { |
| 264 |
if (accumulator.getAccumulatorName() == null) { |
| 265 |
prepareSimpleAttributes(); |
| 266 |
} |
| 267 |
accumulator.setImportPrecedence(decl.getPrecedence()); |
| 268 |
if (top.getAccumulatorManager() == null) { |
| 269 |
StyleNodeFactory styleNodeFactory = getCompilation().getStyleNodeFactory(true); |
| 270 |
AccumulatorRegistry manager = styleNodeFactory.makeAccumulatorManager(); |
| 271 |
top.setAccumulatorManager(manager); |
| 272 |
getCompilation().getPackageData().setAccumulatorRegistry(manager); |
| 273 |
} |
| 274 |
AccumulatorRegistry mgr = top.getAccumulatorManager(); |
| 275 |
Accumulator existing = mgr.getAccumulator(accumulator.getAccumulatorName()); |
| 276 |
if (existing != null) { |
| 277 |
int existingPrec = existing.getImportPrecedence(); |
| 278 |
if (existingPrec == decl.getPrecedence()) { |
| 279 |
compileError("There are two accumulators with the same name (" + |
| 280 |
accumulator.getAccumulatorName().getDisplayName() + ") and the same import precedence", "XTSE3350"); |
| 281 |
} |
| 282 |
if (existingPrec > decl.getPrecedence()) { |
| 283 |
return; |
| 284 |
} |
| 285 |
} |
| 286 |
mgr.addAccumulator(accumulator); |
| 287 |
} |
| 288 |
|
| 289 |
public void validate(ComponentDeclaration decl) throws XPathException { |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
checkTopLevel("XTSE0010", true); |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
boolean foundRule = false; |
| 300 |
AxisIterator kids = iterateAxis(AxisInfo.CHILD); |
| 301 |
NodeInfo curr; |
| 302 |
while ((curr = kids.next()) != null) { |
| 303 |
if (curr instanceof XSLAccumulatorRule) { |
| 304 |
foundRule = true; |
| 305 |
} else { |
| 306 |
compileError("Only xsl:accumulator-rule is allowed here", "XTSE0010"); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
if (!foundRule) { |
| 311 |
compileError("xsl:accumulator must contain at least one xsl:accumulator-rule", "XTSE0010"); |
| 312 |
} |
| 313 |
|
| 314 |
} |
| 315 |
|
| 316 |
public SlotManager getSlotManager() { |
| 317 |
return null; |
| 318 |
} |
| 319 |
|
| 320 |
public void optimize(ComponentDeclaration declaration) throws XPathException { |
| 321 |
|
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
public SequenceType getResultType() { |
| 332 |
return accumulator.getType(); |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
public int getConstructType() { |
| 343 |
return StandardNames.XSL_ACCUMULATOR; |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
public void generateByteCode(Optimizer opt) { |
| 351 |
|
| 352 |
} |
| 353 |
|
| 354 |
private void checkInitialStreamability(Expression init) throws XPathException { |
| 355 |
|
| 356 |
|
| 357 |
if (accumulator.isDeclaredStreamable()) { |
| 358 |
PostureAndSweep ps = Streamability.getStreamability(init, ContextItemStaticInfoEE.DEFAULT, null); |
| 359 |
if (ps.getSweep() != Sweep.MOTIONLESS) { |
| 360 |
notStreamable(this,"The initial value expression for a streaming accumulator must be motionless"); |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
} |
| 366 |
|
| 367 |
private void checkRuleStreamability(XSLAccumulatorRule rule, Pattern pattern, Expression newValueExp) throws XPathException { |
| 368 |
|
| 369 |
|
| 370 |
if (accumulator.isDeclaredStreamable()) { |
| 371 |
boolean fallback = getConfiguration().getBooleanProperty(Feature.STREAMING_FALLBACK); |
| 372 |
if (!pattern.isMotionless()) { |
| 373 |
String message = "The patterns for the accumulator rules in a streaming accumulator must be motionless"; |
| 374 |
notStreamable(rule, message); |
| 375 |
} |
| 376 |
ContextItemStaticInfoEE csi = (ContextItemStaticInfoEE)getConfiguration().makeContextItemStaticInfo(pattern.getItemType(), false); |
| 377 |
csi.setContextPostureStriding(); |
| 378 |
csi.setAccumulatorPhase(rule.isPostDescent() ? AccumulatorFn.Phase.AFTER : AccumulatorFn.Phase.BEFORE); |
| 379 |
List<String> reasons = new ArrayList<>(4); |
| 380 |
PostureAndSweep ps = Streamability.getStreamability(newValueExp, csi, reasons); |
| 381 |
if (!ps.equals(PostureAndSweep.GROUNDED_AND_MOTIONLESS) && !rule.isCapture()) { |
| 382 |
|
| 383 |
|
| 384 |
StringBuilder message = new StringBuilder("The xsl:accumulator-rule/@select expression (or contained sequence constructor) " + |
| 385 |
"for a streaming accumulator must be grounded and motionless"); |
| 386 |
for (String reason : reasons) { |
| 387 |
message.append(". ").append(reason); |
| 388 |
} |
| 389 |
notStreamable(rule, message.toString()); |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
} |
| 394 |
|
| 395 |
private void notStreamable(StyleElement rule, String message) throws XPathException { |
| 396 |
boolean fallback = getConfiguration().getBooleanProperty(Feature.STREAMING_FALLBACK); |
| 397 |
if (fallback) { |
| 398 |
message += ". Falling back to non-streaming implementation"; |
| 399 |
rule.compileWarning(message, "XTSE3430"); |
| 400 |
rule.getCompilation().setFallbackToNonStreaming(true); |
| 401 |
} else { |
| 402 |
rule.compileError(message, "XTSE3430"); |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
} |