| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.trans; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.expr.ComponentBinding; |
| 12 |
import net.sf.saxon.expr.XPathContext; |
| 13 |
import net.sf.saxon.expr.XPathContextMajor; |
| 14 |
import net.sf.saxon.expr.instruct.SlotManager; |
| 15 |
import net.sf.saxon.expr.instruct.TemplateRule; |
| 16 |
import net.sf.saxon.expr.sort.GenericSorter; |
| 17 |
import net.sf.saxon.expr.sort.Sortable; |
| 18 |
import net.sf.saxon.om.*; |
| 19 |
import net.sf.saxon.pattern.*; |
| 20 |
import net.sf.saxon.style.StylesheetModule; |
| 21 |
import net.sf.saxon.style.StylesheetPackage; |
| 22 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 23 |
import net.sf.saxon.trans.rules.*; |
| 24 |
import net.sf.saxon.tree.util.Navigator; |
| 25 |
import net.sf.saxon.type.*; |
| 26 |
import net.sf.saxon.value.AtomicValue; |
| 27 |
import net.sf.saxon.value.Whitespace; |
| 28 |
import net.sf.saxon.z.IntHashMap; |
| 29 |
import net.sf.saxon.z.IntIterator; |
| 30 |
|
| 31 |
import java.util.*; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class SimpleMode extends Mode { |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
protected final RuleChain genericRuleChain = new RuleChain(); |
| 44 |
protected RuleChain atomicValueRuleChain = new RuleChain(); |
| 45 |
protected RuleChain functionItemRuleChain = new RuleChain(); |
| 46 |
protected RuleChain documentRuleChain = new RuleChain(); |
| 47 |
protected RuleChain textRuleChain = new RuleChain(); |
| 48 |
protected RuleChain commentRuleChain = new RuleChain(); |
| 49 |
protected RuleChain processingInstructionRuleChain = new RuleChain(); |
| 50 |
protected RuleChain namespaceRuleChain = new RuleChain(); |
| 51 |
protected RuleChain unnamedElementRuleChain = new RuleChain(); |
| 52 |
protected RuleChain unnamedAttributeRuleChain = new RuleChain(); |
| 53 |
protected IntHashMap<RuleChain> namedElementRuleChains = new IntHashMap<>(32); |
| 54 |
protected IntHashMap<RuleChain> namedAttributeRuleChains = new IntHashMap<>(8); |
| 55 |
protected Map<StructuredQName, RuleChain> qNamedElementRuleChains; |
| 56 |
protected Map<StructuredQName, RuleChain> qNamedAttributeRuleChains; |
| 57 |
|
| 58 |
private BuiltInRuleSet builtInRuleSet = TextOnlyCopyRuleSet.getInstance(); |
| 59 |
private Rule mostRecentRule; |
| 60 |
private int mostRecentModuleHash; |
| 61 |
private int stackFrameSlotsNeeded = 0; |
| 62 |
private int highestRank; |
| 63 |
|
| 64 |
|
| 65 |
private Map<String, Integer> explicitPropertyPrecedences = new HashMap<>(); |
| 66 |
private Map<String, String> explicitPropertyValues = new HashMap<>(); |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public SimpleMode(StructuredQName modeName) { |
| 77 |
super(modeName); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public void setBuiltInRuleSet(BuiltInRuleSet defaultRules) { |
| 88 |
this.builtInRuleSet = defaultRules; |
| 89 |
hasRules = true; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public BuiltInRuleSet getBuiltInRuleSet() { |
| 101 |
return this.builtInRuleSet; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
@Override |
| 109 |
public SimpleMode getActivePart() { |
| 110 |
return this; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public void checkForConflictingProperties() throws XPathException { |
| 120 |
for (Map.Entry<String, String> entry : getActivePart().explicitPropertyValues.entrySet()) { |
| 121 |
String prop = entry.getKey(); |
| 122 |
String value = entry.getValue(); |
| 123 |
if (value.equals("##conflict##")) { |
| 124 |
throw new XPathException( |
| 125 |
"For " + getLabel() + |
| 126 |
", there are conflicting values for xsl:mode/@" + |
| 127 |
prop + |
| 128 |
" at the same import precedence", "XTSE0545"); |
| 129 |
} |
| 130 |
if (prop.equals("typed")) { |
| 131 |
mustBeTyped = "yes".equals(value) || "strict".equals(value) || "lax".equals(value); |
| 132 |
mustBeUntyped = "no".equals(value); |
| 133 |
} else if (prop.equals("on-no-match")) { |
| 134 |
BuiltInRuleSet base = null; |
| 135 |
switch (value) { |
| 136 |
case "text-only-copy": |
| 137 |
base = TextOnlyCopyRuleSet.getInstance(); |
| 138 |
break; |
| 139 |
case "shallow-copy": |
| 140 |
base = ShallowCopyRuleSet.getInstance(); |
| 141 |
break; |
| 142 |
case "deep-copy": |
| 143 |
base = DeepCopyRuleSet.getInstance(); |
| 144 |
break; |
| 145 |
case "shallow-skip": |
| 146 |
base = ShallowSkipRuleSet.getInstance(); |
| 147 |
break; |
| 148 |
case "deep-skip": |
| 149 |
base = DeepSkipRuleSet.getInstance(); |
| 150 |
break; |
| 151 |
case "fail": |
| 152 |
base = FailRuleSet.getInstance(); |
| 153 |
break; |
| 154 |
default: |
| 155 |
|
| 156 |
break; |
| 157 |
} |
| 158 |
if ("yes".equals(explicitPropertyValues.get("warning-on-no-match"))) { |
| 159 |
base = new RuleSetWithWarnings(base); |
| 160 |
} |
| 161 |
setBuiltInRuleSet(base); |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public String getLabel() { |
| 172 |
return isUnnamedMode() ? |
| 173 |
"the unnamed mode" : |
| 174 |
"mode " + modeName.getDisplayName(); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
public static void copyRules(final SimpleMode from, final SimpleMode to) { |
| 185 |
try { |
| 186 |
from.processRules(r -> { |
| 187 |
Rule r2 = r.copy(false); |
| 188 |
to.addRule(r2.getPattern(), r2); |
| 189 |
}); |
| 190 |
} catch (XPathException e) { |
| 191 |
throw new AssertionError(e); |
| 192 |
} |
| 193 |
|
| 194 |
to.mostRecentRule = from.mostRecentRule; |
| 195 |
to.mostRecentModuleHash = from.mostRecentModuleHash; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
protected RuleSearchState makeRuleSearchState(RuleChain chain, XPathContext context) { |
| 204 |
return new RuleSearchState(); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
@Override |
| 215 |
public boolean isEmpty() { |
| 216 |
return !hasRules; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
public void setExplicitProperty(String name, String value, int precedence) { |
| 228 |
Integer p = explicitPropertyPrecedences.get(name); |
| 229 |
if (p != null) { |
| 230 |
if (p < precedence) { |
| 231 |
explicitPropertyPrecedences.put(name, precedence); |
| 232 |
explicitPropertyValues.put(name, value); |
| 233 |
} else if (p == precedence) { |
| 234 |
String v = explicitPropertyValues.get(name); |
| 235 |
if (v != null & !v.equals(value)) { |
| 236 |
|
| 237 |
|
| 238 |
explicitPropertyValues.put(name, "##conflict##"); |
| 239 |
} |
| 240 |
} else { |
| 241 |
|
| 242 |
} |
| 243 |
} else { |
| 244 |
explicitPropertyPrecedences.put(name, precedence); |
| 245 |
explicitPropertyValues.put(name, value); |
| 246 |
} |
| 247 |
|
| 248 |
final String typed = explicitPropertyValues.get("typed"); |
| 249 |
mustBeTyped = "yes".equals(typed) || "strict".equals(typed) || "lax".equals(typed); |
| 250 |
mustBeUntyped = "no".equals(typed); |
| 251 |
|
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
public String getPropertyValue(String name) { |
| 262 |
return explicitPropertyValues.get(name); |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
@Override |
| 275 |
public Set<String> getExplicitNamespaces(NamePool pool) { |
| 276 |
Set<String> namespaces = new HashSet<>(); |
| 277 |
IntIterator ii = namedElementRuleChains.keyIterator(); |
| 278 |
while (ii.hasNext()) { |
| 279 |
int fp = ii.next(); |
| 280 |
namespaces.add(pool.getURI(fp)); |
| 281 |
} |
| 282 |
return namespaces; |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
public void addRule(Pattern pattern, RuleTarget action, |
| 303 |
StylesheetModule module, int precedence, double priority, int position, int part) { |
| 304 |
|
| 305 |
hasRules = true; |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
if (pattern.getItemType() instanceof ErrorType) { |
| 310 |
return; |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
int moduleHash = module.hashCode(); |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
int minImportPrecedence = module.getMinImportPrecedence(); |
| 331 |
|
| 332 |
Rule newRule = makeRule(pattern, action, precedence, minImportPrecedence, priority, position, part); |
| 333 |
|
| 334 |
if (pattern instanceof NodeTestPattern) { |
| 335 |
ItemType test = pattern.getItemType(); |
| 336 |
if (test instanceof AnyNodeTest) { |
| 337 |
newRule.setAlwaysMatches(true); |
| 338 |
} else if (test instanceof NodeKindTest) { |
| 339 |
newRule.setAlwaysMatches(true); |
| 340 |
} else if (test instanceof NameTest) { |
| 341 |
int kind = test.getPrimitiveType(); |
| 342 |
if (kind == Type.ELEMENT || kind == Type.ATTRIBUTE) { |
| 343 |
newRule.setAlwaysMatches(true); |
| 344 |
} |
| 345 |
} |
| 346 |
} |
| 347 |
mostRecentRule = newRule; |
| 348 |
mostRecentModuleHash = moduleHash; |
| 349 |
|
| 350 |
addRule(pattern, newRule); |
| 351 |
|
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
public Rule makeRule( Pattern pattern, RuleTarget action, |
| 367 |
int precedence, int minImportPrecedence, double priority, int sequence, int part) { |
| 368 |
return new Rule(pattern, action, precedence, minImportPrecedence, priority, sequence, part); |
| 369 |
} |
| 370 |
|
| 371 |
public void addRule(Pattern pattern, Rule newRule) { |
| 372 |
UType uType = pattern.getUType(); |
| 373 |
if (uType.equals(UType.ELEMENT)) { |
| 374 |
int fp = pattern.getFingerprint(); |
| 375 |
addRuleToNamedOrUnnamedChain( |
| 376 |
newRule, fp, unnamedElementRuleChain, namedElementRuleChains); |
| 377 |
} else if (uType.equals(UType.ATTRIBUTE)) { |
| 378 |
int fp = pattern.getFingerprint(); |
| 379 |
addRuleToNamedOrUnnamedChain( |
| 380 |
newRule, fp, unnamedAttributeRuleChain, namedAttributeRuleChains); |
| 381 |
} else if (uType.equals(UType.DOCUMENT)) { |
| 382 |
addRuleToList(newRule, documentRuleChain); |
| 383 |
} else if (uType.equals(UType.TEXT)) { |
| 384 |
addRuleToList(newRule, textRuleChain); |
| 385 |
} else if (uType.equals(UType.COMMENT)) { |
| 386 |
addRuleToList(newRule, commentRuleChain); |
| 387 |
} else if (uType.equals(UType.PI)) { |
| 388 |
addRuleToList(newRule, processingInstructionRuleChain); |
| 389 |
} else if (uType.equals(UType.NAMESPACE)) { |
| 390 |
addRuleToList(newRule, namespaceRuleChain); |
| 391 |
} else if (UType.ANY_ATOMIC.subsumes(uType)) { |
| 392 |
addRuleToList(newRule, atomicValueRuleChain); |
| 393 |
} else if (UType.FUNCTION.subsumes(uType)) { |
| 394 |
addRuleToList(newRule, functionItemRuleChain); |
| 395 |
} else { |
| 396 |
addRuleToList(newRule, genericRuleChain); |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
protected void addRuleToNamedOrUnnamedChain( |
| 401 |
Rule newRule, int fp, RuleChain unnamedRuleChain, IntHashMap<RuleChain> namedRuleChains) { |
| 402 |
if (fp == -1) { |
| 403 |
addRuleToList(newRule, unnamedRuleChain); |
| 404 |
} else { |
| 405 |
RuleChain chain = namedRuleChains.get(fp); |
| 406 |
if (chain == null) { |
| 407 |
chain = new RuleChain(newRule); |
| 408 |
namedRuleChains.put(fp, chain); |
| 409 |
} else { |
| 410 |
addRuleToList(newRule, chain); |
| 411 |
} |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
private void addRuleToList(Rule newRule, RuleChain list) { |
| 424 |
if (list.head() == null) { |
| 425 |
list.setHead(newRule); |
| 426 |
} else { |
| 427 |
int precedence = newRule.getPrecedence(); |
| 428 |
double priority = newRule.getPriority(); |
| 429 |
Rule rule = list.head(); |
| 430 |
Rule prev = null; |
| 431 |
while (rule != null) { |
| 432 |
if ((rule.getPrecedence() < precedence) || |
| 433 |
(rule.getPrecedence() == precedence && rule.getPriority() <= priority)) { |
| 434 |
newRule.setNext(rule); |
| 435 |
if (prev == null) { |
| 436 |
list.setHead(newRule); |
| 437 |
} else { |
| 438 |
prev.setNext(newRule); |
| 439 |
} |
| 440 |
break; |
| 441 |
} else { |
| 442 |
prev = rule; |
| 443 |
rule = rule.getNext(); |
| 444 |
} |
| 445 |
} |
| 446 |
if (rule == null) { |
| 447 |
assert prev != null; |
| 448 |
prev.setNext(newRule); |
| 449 |
newRule.setNext(null); |
| 450 |
} |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
public void allocatePatternSlots(int slots) { |
| 461 |
stackFrameSlotsNeeded = Math.max(stackFrameSlotsNeeded, slots); |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
@Override |
| 474 |
public Rule getRule(Item item, XPathContext context) throws XPathException { |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
if (stackFrameSlotsNeeded > 0) { |
| 487 |
context = makeNewContext(context); |
| 488 |
} |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
RuleChain unnamedNodeChain; |
| 493 |
Rule bestRule = null; |
| 494 |
|
| 495 |
if (item instanceof NodeInfo) { |
| 496 |
NodeInfo node = (NodeInfo) item; |
| 497 |
switch (node.getNodeKind()) { |
| 498 |
case Type.DOCUMENT: |
| 499 |
unnamedNodeChain = documentRuleChain; |
| 500 |
break; |
| 501 |
|
| 502 |
case Type.ELEMENT: { |
| 503 |
unnamedNodeChain = unnamedElementRuleChain; |
| 504 |
RuleChain namedNodeChain; |
| 505 |
if (node.hasFingerprint()) { |
| 506 |
namedNodeChain = namedElementRuleChains.get(node.getFingerprint()); |
| 507 |
} else { |
| 508 |
namedNodeChain = getNamedRuleChain(context, Type.ELEMENT, node.getURI(), node.getLocalPart()); |
| 509 |
} |
| 510 |
if (namedNodeChain != null) { |
| 511 |
bestRule = searchRuleChain(node, context, null, namedNodeChain); |
| 512 |
} |
| 513 |
break; |
| 514 |
} |
| 515 |
case Type.ATTRIBUTE: { |
| 516 |
unnamedNodeChain = unnamedAttributeRuleChain; |
| 517 |
RuleChain namedNodeChain; |
| 518 |
if (node.hasFingerprint()) { |
| 519 |
namedNodeChain = namedAttributeRuleChains.get(node.getFingerprint()); |
| 520 |
} else { |
| 521 |
namedNodeChain = getNamedRuleChain(context, Type.ATTRIBUTE, node.getURI(), node.getLocalPart()); |
| 522 |
} |
| 523 |
if (namedNodeChain != null) { |
| 524 |
bestRule = searchRuleChain(node, context, null, namedNodeChain); |
| 525 |
} |
| 526 |
break; |
| 527 |
} |
| 528 |
case Type.TEXT: |
| 529 |
unnamedNodeChain = textRuleChain; |
| 530 |
break; |
| 531 |
case Type.COMMENT: |
| 532 |
unnamedNodeChain = commentRuleChain; |
| 533 |
break; |
| 534 |
case Type.PROCESSING_INSTRUCTION: |
| 535 |
unnamedNodeChain = processingInstructionRuleChain; |
| 536 |
break; |
| 537 |
case Type.NAMESPACE: |
| 538 |
unnamedNodeChain = namespaceRuleChain; |
| 539 |
break; |
| 540 |
default: |
| 541 |
throw new AssertionError("Unknown node kind"); |
| 542 |
} |
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
if (unnamedNodeChain != null) { |
| 547 |
bestRule = searchRuleChain(node, context, bestRule, unnamedNodeChain); |
| 548 |
} |
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
bestRule = searchRuleChain(node, context, bestRule, genericRuleChain); |
| 553 |
|
| 554 |
} else if (item instanceof AtomicValue) { |
| 555 |
if (atomicValueRuleChain != null) { |
| 556 |
bestRule = searchRuleChain(item, context, bestRule, atomicValueRuleChain); |
| 557 |
} |
| 558 |
bestRule = searchRuleChain(item, context, bestRule, genericRuleChain); |
| 559 |
|
| 560 |
} else if (item instanceof Function) { |
| 561 |
if (functionItemRuleChain != null) { |
| 562 |
bestRule = searchRuleChain(item, context, bestRule, functionItemRuleChain); |
| 563 |
} |
| 564 |
bestRule = searchRuleChain(item, context, bestRule, genericRuleChain); |
| 565 |
} |
| 566 |
|
| 567 |
return bestRule; |
| 568 |
} |
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
protected RuleChain getNamedRuleChain(XPathContext c, int kind, String uri, String local) { |
| 582 |
|
| 583 |
|
| 584 |
synchronized(this) { |
| 585 |
if (qNamedElementRuleChains == null) { |
| 586 |
qNamedElementRuleChains = new HashMap<>(namedElementRuleChains.size()); |
| 587 |
qNamedAttributeRuleChains = new HashMap<>(namedAttributeRuleChains.size()); |
| 588 |
NamePool pool = c.getNamePool(); |
| 589 |
indexByQName(pool, namedElementRuleChains, qNamedElementRuleChains); |
| 590 |
indexByQName(pool, namedAttributeRuleChains, qNamedAttributeRuleChains); |
| 591 |
} |
| 592 |
} |
| 593 |
return (kind == Type.ELEMENT ? qNamedElementRuleChains : qNamedAttributeRuleChains) |
| 594 |
.get(new StructuredQName("", uri, local)); |
| 595 |
} |
| 596 |
|
| 597 |
private static void indexByQName(NamePool pool, IntHashMap<RuleChain> indexByFP, Map<StructuredQName, RuleChain> indexByQN) { |
| 598 |
IntIterator ii = indexByFP.keyIterator(); |
| 599 |
while (ii.hasNext()) { |
| 600 |
int fp = ii.next(); |
| 601 |
RuleChain eChain = indexByFP.get(fp); |
| 602 |
StructuredQName name = pool.getStructuredQName(fp); |
| 603 |
indexByQN.put(name, eChain); |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
protected Rule searchRuleChain(Item item, XPathContext context, Rule bestRule, RuleChain chain) throws XPathException { |
| 619 |
|
| 620 |
while (!(context instanceof XPathContextMajor)) { |
| 621 |
context = context.getCaller(); |
| 622 |
} |
| 623 |
|
| 624 |
|
| 625 |
RuleSearchState ruleSearchState = makeRuleSearchState(chain, context); |
| 626 |
|
| 627 |
Rule head = chain == null ? null : chain.head(); |
| 628 |
while (head != null) { |
| 629 |
if (bestRule != null) { |
| 630 |
int rank = head.compareRank(bestRule); |
| 631 |
if (rank < 0) { |
| 632 |
|
| 633 |
|
| 634 |
break; |
| 635 |
} else if (rank == 0) { |
| 636 |
|
| 637 |
if (ruleMatches(head, item, (XPathContextMajor) context, ruleSearchState)) { |
| 638 |
if (head.getSequence() != bestRule.getSequence()) { |
| 639 |
reportAmbiguity(item, bestRule, head, context); |
| 640 |
} |
| 641 |
|
| 642 |
int seqComp = Integer.compare(bestRule.getSequence(), head.getSequence()); |
| 643 |
if (seqComp > 0) { |
| 644 |
return bestRule; |
| 645 |
} else if (seqComp < 0) { |
| 646 |
return head; |
| 647 |
} else { |
| 648 |
|
| 649 |
bestRule = bestRule.getPartNumber() > head.getPartNumber() ? bestRule : head; |
| 650 |
} |
| 651 |
break; |
| 652 |
} else { |
| 653 |
|
| 654 |
} |
| 655 |
} else { |
| 656 |
|
| 657 |
if (ruleMatches(head, item, (XPathContextMajor) context, ruleSearchState)) { |
| 658 |
bestRule = head; |
| 659 |
} |
| 660 |
} |
| 661 |
} else if (ruleMatches(head, item, (XPathContextMajor) context, ruleSearchState)) { |
| 662 |
bestRule = head; |
| 663 |
if (getRecoveryPolicy() == Configuration.RECOVER_SILENTLY) { |
| 664 |
|
| 665 |
break; |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
head = head.getNext(); |
| 670 |
} |
| 671 |
|
| 672 |
return bestRule; |
| 673 |
} |
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
protected boolean ruleMatches(Rule r, Item item, XPathContextMajor context, RuleSearchState pre) throws XPathException { |
| 687 |
return r.isAlwaysMatches() || r.matches(item, context); |
| 688 |
} |
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
@Override |
| 703 |
public Rule getRule(Item item, XPathContext context, RuleFilter filter) throws XPathException { |
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
if (stackFrameSlotsNeeded > 0) { |
| 712 |
context = makeNewContext(context); |
| 713 |
} |
| 714 |
|
| 715 |
|
| 716 |
RuleSearchState ruleSearchState; |
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
Rule bestRule = null; |
| 721 |
RuleChain unnamedNodeChain; |
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
if (item instanceof NodeInfo) { |
| 727 |
NodeInfo node = (NodeInfo) item; |
| 728 |
switch (node.getNodeKind()) { |
| 729 |
case Type.DOCUMENT: |
| 730 |
unnamedNodeChain = documentRuleChain; |
| 731 |
break; |
| 732 |
case Type.ELEMENT: { |
| 733 |
unnamedNodeChain = unnamedElementRuleChain; |
| 734 |
RuleChain namedNodeChain; |
| 735 |
if (node.hasFingerprint()) { |
| 736 |
namedNodeChain = namedElementRuleChains.get(node.getFingerprint()); |
| 737 |
} else { |
| 738 |
namedNodeChain = getNamedRuleChain(context, Type.ELEMENT, node.getURI(), node.getLocalPart()); |
| 739 |
} |
| 740 |
if (namedNodeChain != null) { |
| 741 |
ruleSearchState = makeRuleSearchState(namedNodeChain, context); |
| 742 |
bestRule = searchRuleChain(item, context, null, namedNodeChain, ruleSearchState, filter); |
| 743 |
} |
| 744 |
break; |
| 745 |
} |
| 746 |
case Type.ATTRIBUTE: { |
| 747 |
unnamedNodeChain = unnamedAttributeRuleChain; |
| 748 |
RuleChain namedNodeChain; |
| 749 |
if (node.hasFingerprint()) { |
| 750 |
namedNodeChain = namedAttributeRuleChains.get(node.getFingerprint()); |
| 751 |
} else { |
| 752 |
namedNodeChain = getNamedRuleChain(context, Type.ATTRIBUTE, node.getURI(), node.getLocalPart()); |
| 753 |
} |
| 754 |
if (namedNodeChain != null) { |
| 755 |
ruleSearchState = makeRuleSearchState(namedNodeChain, context); |
| 756 |
bestRule = searchRuleChain(item, context, null, namedNodeChain, ruleSearchState, filter); |
| 757 |
} |
| 758 |
break; |
| 759 |
} |
| 760 |
case Type.TEXT: |
| 761 |
unnamedNodeChain = textRuleChain; |
| 762 |
break; |
| 763 |
case Type.COMMENT: |
| 764 |
unnamedNodeChain = commentRuleChain; |
| 765 |
break; |
| 766 |
case Type.PROCESSING_INSTRUCTION: |
| 767 |
unnamedNodeChain = processingInstructionRuleChain; |
| 768 |
break; |
| 769 |
case Type.NAMESPACE: |
| 770 |
unnamedNodeChain = namespaceRuleChain; |
| 771 |
break; |
| 772 |
default: |
| 773 |
throw new AssertionError("Unknown node kind"); |
| 774 |
} |
| 775 |
|
| 776 |
ruleSearchState = makeRuleSearchState(unnamedNodeChain, context); |
| 777 |
bestRule = searchRuleChain(item, context, bestRule, unnamedNodeChain, ruleSearchState, filter); |
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
ruleSearchState = makeRuleSearchState(genericRuleChain, context); |
| 782 |
return searchRuleChain(item, context, bestRule, genericRuleChain, ruleSearchState, filter); |
| 783 |
|
| 784 |
} else if (item instanceof AtomicValue) { |
| 785 |
if (atomicValueRuleChain != null) { |
| 786 |
ruleSearchState = makeRuleSearchState(atomicValueRuleChain, context); |
| 787 |
bestRule = searchRuleChain(item, context, bestRule, atomicValueRuleChain, ruleSearchState, filter); |
| 788 |
} |
| 789 |
ruleSearchState = makeRuleSearchState(genericRuleChain, context); |
| 790 |
bestRule = searchRuleChain(item, context, bestRule, genericRuleChain, ruleSearchState, filter); |
| 791 |
return bestRule; |
| 792 |
|
| 793 |
} else if (item instanceof Function) { |
| 794 |
if (functionItemRuleChain != null) { |
| 795 |
ruleSearchState = makeRuleSearchState(functionItemRuleChain, context); |
| 796 |
bestRule = searchRuleChain(item, context, bestRule, functionItemRuleChain, ruleSearchState, filter); |
| 797 |
} |
| 798 |
ruleSearchState = makeRuleSearchState(genericRuleChain, context); |
| 799 |
bestRule = searchRuleChain(item, context, bestRule, genericRuleChain, ruleSearchState, filter); |
| 800 |
return bestRule; |
| 801 |
|
| 802 |
} else { |
| 803 |
return null; |
| 804 |
} |
| 805 |
|
| 806 |
} |
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
protected Rule searchRuleChain(Item item, XPathContext context, |
| 822 |
Rule bestRule, RuleChain chain, RuleSearchState ruleSearchState, RuleFilter filter) throws XPathException { |
| 823 |
Rule head = chain == null ? null : chain.head(); |
| 824 |
while (!(context instanceof XPathContextMajor)) { |
| 825 |
context = context.getCaller(); |
| 826 |
} |
| 827 |
while (head != null) { |
| 828 |
if (filter == null || filter.testRule(head)) { |
| 829 |
if (bestRule != null) { |
| 830 |
int rank = head.compareRank(bestRule); |
| 831 |
if (rank < 0) { |
| 832 |
|
| 833 |
|
| 834 |
break; |
| 835 |
} else if (rank == 0) { |
| 836 |
|
| 837 |
if (ruleMatches(head, item, (XPathContextMajor) context, ruleSearchState)) { |
| 838 |
reportAmbiguity(item, bestRule, head, context); |
| 839 |
|
| 840 |
bestRule = bestRule.getSequence() > head.getSequence() ? bestRule : head; |
| 841 |
break; |
| 842 |
} else { |
| 843 |
|
| 844 |
} |
| 845 |
} else { |
| 846 |
|
| 847 |
if (ruleMatches(head, item, (XPathContextMajor) context, ruleSearchState)) { |
| 848 |
bestRule = head; |
| 849 |
} |
| 850 |
} |
| 851 |
} else if (ruleMatches(head, item, (XPathContextMajor) context, ruleSearchState)) { |
| 852 |
bestRule = head; |
| 853 |
if (getRecoveryPolicy() == Configuration.RECOVER_SILENTLY) { |
| 854 |
break; |
| 855 |
} |
| 856 |
} |
| 857 |
} |
| 858 |
head = head.getNext(); |
| 859 |
} |
| 860 |
return bestRule; |
| 861 |
} |
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
protected void reportAmbiguity(Item item, Rule r1, Rule r2, XPathContext c) |
| 877 |
throws XPathException { |
| 878 |
|
| 879 |
if (r1.getAction() == r2.getAction() && r1.getSequence() == r2.getSequence()) { |
| 880 |
return; |
| 881 |
} |
| 882 |
String path; |
| 883 |
String errorCode = "XTDE0540"; |
| 884 |
|
| 885 |
if (item instanceof NodeInfo) { |
| 886 |
path = Navigator.getPath((NodeInfo) item); |
| 887 |
} else { |
| 888 |
path = item.toShortString(); |
| 889 |
} |
| 890 |
|
| 891 |
Pattern pat1 = r1.getPattern(); |
| 892 |
Pattern pat2 = r2.getPattern(); |
| 893 |
|
| 894 |
String message; |
| 895 |
if (r1.getAction() == r2.getAction()) { |
| 896 |
message = "Ambiguous rule match for " + path + ". " + |
| 897 |
"Matches \"" + showPattern(pat1) + "\" on line " + pat1.getLocation().getLineNumber() + |
| 898 |
" of " + pat1.getLocation().getSystemId() + |
| 899 |
", a rule which appears in the stylesheet more than once, because the containing module was included more than once"; |
| 900 |
} else { |
| 901 |
message = "Ambiguous rule match for " + path + '\n' + |
| 902 |
"Matches both \"" + showPattern(pat1) + "\" on line " + pat1.getLocation().getLineNumber() + |
| 903 |
" of " + pat1.getLocation().getSystemId() + |
| 904 |
"\nand \"" + showPattern(pat2) + "\" on line " + pat2.getLocation().getLineNumber() + |
| 905 |
" of " + pat2.getLocation().getSystemId(); |
| 906 |
} |
| 907 |
|
| 908 |
XPathException err = new XPathException(message, errorCode); |
| 909 |
if (getRecoveryPolicy() == Configuration.DO_NOT_RECOVER) { |
| 910 |
throw err; |
| 911 |
} else { |
| 912 |
c.getController().recoverableError(err); |
| 913 |
} |
| 914 |
} |
| 915 |
|
| 916 |
private static String showPattern(Pattern p) { |
| 917 |
|
| 918 |
return Whitespace.collapseWhitespace(p.toString()).toString(); |
| 919 |
} |
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
public void prepareStreamability() throws XPathException { |
| 927 |
} |
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
public void allocateAllBindingSlots(final StylesheetPackage pack) { |
| 937 |
if (getDeclaringComponent().getDeclaringPackage() == pack && !bindingSlotsAllocated) { |
| 938 |
forceAllocateAllBindingSlots(pack, this, getDeclaringComponent().getComponentBindings()); |
| 939 |
bindingSlotsAllocated = true; |
| 940 |
} |
| 941 |
} |
| 942 |
|
| 943 |
public static void forceAllocateAllBindingSlots( |
| 944 |
final StylesheetPackage pack, final SimpleMode mode, final List<ComponentBinding> bindings) { |
| 945 |
final Set<TemplateRule> rulesProcessed = new HashSet<>(); |
| 946 |
final IdentityHashMap<Pattern, Boolean> patternsProcessed = new IdentityHashMap<>(); |
| 947 |
try { |
| 948 |
mode.processRules(r -> { |
| 949 |
|
| 950 |
|
| 951 |
Pattern pattern = r.getPattern(); |
| 952 |
if (!patternsProcessed.containsKey(pattern)) { |
| 953 |
allocateBindingSlotsRecursive(pack, mode, pattern, bindings); |
| 954 |
patternsProcessed.put(pattern, true); |
| 955 |
} |
| 956 |
TemplateRule tr = (TemplateRule) r.getAction(); |
| 957 |
if (tr.getBody() != null && !rulesProcessed.contains(tr)) { |
| 958 |
allocateBindingSlotsRecursive(pack, mode, tr.getBody(), bindings); |
| 959 |
rulesProcessed.add(tr); |
| 960 |
} |
| 961 |
}); |
| 962 |
} catch (XPathException e) { |
| 963 |
throw new AssertionError(e); |
| 964 |
} |
| 965 |
} |
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
public void computeStreamability() throws XPathException { |
| 972 |
|
| 973 |
} |
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
public void invertStreamableTemplates() throws XPathException { |
| 983 |
|
| 984 |
} |
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
public void explainTemplateRules(final ExpressionPresenter out) throws XPathException { |
| 997 |
RuleAction action = r -> r.export(out, isDeclaredStreamable()); |
| 998 |
RuleGroupAction group = new RuleGroupAction() { |
| 999 |
String type; |
| 1000 |
|
| 1001 |
public void start() { |
| 1002 |
out.startElement("ruleSet"); |
| 1003 |
out.emitAttribute("type", type); |
| 1004 |
} |
| 1005 |
|
| 1006 |
public void setString(String type) { |
| 1007 |
this.type = type; |
| 1008 |
} |
| 1009 |
|
| 1010 |
public void start(int i) { |
| 1011 |
out.startElement("ruleChain"); |
| 1012 |
out.emitAttribute("key", out.getNamePool().getClarkName(i)); |
| 1013 |
} |
| 1014 |
|
| 1015 |
public void end() { |
| 1016 |
out.endElement(); |
| 1017 |
} |
| 1018 |
}; |
| 1019 |
try { |
| 1020 |
processRules(action, group); |
| 1021 |
} catch (XPathException err) { |
| 1022 |
|
| 1023 |
} |
| 1024 |
|
| 1025 |
} |
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
@Override |
| 1036 |
public void exportTemplateRules(final ExpressionPresenter out) throws XPathException { |
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
RuleAction action = r -> { |
| 1041 |
|
| 1042 |
r.export(out, isDeclaredStreamable()); |
| 1043 |
|
| 1044 |
}; |
| 1045 |
|
| 1046 |
processRules(action); |
| 1047 |
|
| 1048 |
} |
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
public void processRules(RuleAction action) throws XPathException { |
| 1057 |
processRules(action, null); |
| 1058 |
} |
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
public void processRules(RuleAction action, RuleGroupAction group) throws XPathException { |
| 1068 |
processRuleChain(documentRuleChain, action, setGroup(group, "document-node()")); |
| 1069 |
processRuleChain(unnamedElementRuleChain, action, setGroup(group, "element()")); |
| 1070 |
processRuleChains(namedElementRuleChains, action, setGroup(group, "namedElements")); |
| 1071 |
processRuleChain(unnamedAttributeRuleChain, action, setGroup(group, "attribute()")); |
| 1072 |
processRuleChains(namedAttributeRuleChains, action, setGroup(group, "namedAttributes")); |
| 1073 |
processRuleChain(textRuleChain, action, setGroup(group, "text()")); |
| 1074 |
processRuleChain(commentRuleChain, action, setGroup(group, "comment()")); |
| 1075 |
processRuleChain(processingInstructionRuleChain, action, setGroup(group, "processing-instruction()")); |
| 1076 |
processRuleChain(namespaceRuleChain, action, setGroup(group, "namespace()")); |
| 1077 |
processRuleChain(genericRuleChain, action, setGroup(group, "node()")); |
| 1078 |
processRuleChain(atomicValueRuleChain, action, setGroup(group, "atomicValue")); |
| 1079 |
processRuleChain(functionItemRuleChain, action, setGroup(group, "function()")); |
| 1080 |
} |
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
protected RuleGroupAction setGroup(RuleGroupAction group, String type) { |
| 1090 |
if (group != null) { |
| 1091 |
group.setString(type); |
| 1092 |
} |
| 1093 |
return group; |
| 1094 |
} |
| 1095 |
|
| 1096 |
|
| 1097 |
public void processRuleChains(IntHashMap<RuleChain> chains, RuleAction action, RuleGroupAction group) throws XPathException { |
| 1098 |
if (chains.size() > 0) { |
| 1099 |
if (group != null) { |
| 1100 |
group.start(); |
| 1101 |
} |
| 1102 |
IntIterator ii = chains.keyIterator(); |
| 1103 |
while (ii.hasNext()) { |
| 1104 |
int i = ii.next(); |
| 1105 |
if (group != null) { |
| 1106 |
group.start(i); |
| 1107 |
} |
| 1108 |
RuleChain r = chains.get(i); |
| 1109 |
processRuleChain(r, action, null); |
| 1110 |
if (group != null) { |
| 1111 |
group.end(); |
| 1112 |
} |
| 1113 |
} |
| 1114 |
if (group != null) { |
| 1115 |
group.end(); |
| 1116 |
} |
| 1117 |
} |
| 1118 |
} |
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
public void processRuleChain(RuleChain chain, RuleAction action) throws XPathException { |
| 1127 |
Rule r = chain == null ? null : chain.head(); |
| 1128 |
while (r != null) { |
| 1129 |
action.processRule(r); |
| 1130 |
r = r.getNext(); |
| 1131 |
} |
| 1132 |
} |
| 1133 |
|
| 1134 |
|
| 1135 |
public void processRuleChain(RuleChain chain, RuleAction action, RuleGroupAction group) throws XPathException { |
| 1136 |
Rule r = chain == null ? null : chain.head(); |
| 1137 |
if (r != null) { |
| 1138 |
if (group != null) { |
| 1139 |
group.start(); |
| 1140 |
} |
| 1141 |
while (r != null) { |
| 1142 |
action.processRule(r); |
| 1143 |
r = r.getNext(); |
| 1144 |
} |
| 1145 |
if (group != null) { |
| 1146 |
group.end(); |
| 1147 |
} |
| 1148 |
} |
| 1149 |
} |
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
public void optimizeRules() { |
| 1157 |
} |
| 1158 |
|
| 1159 |
public int getMaxPrecedence() { |
| 1160 |
try { |
| 1161 |
MaxPrecedenceAction action = new MaxPrecedenceAction(); |
| 1162 |
processRules(action); |
| 1163 |
return action.max; |
| 1164 |
} catch (XPathException e) { |
| 1165 |
throw new AssertionError(e); |
| 1166 |
} |
| 1167 |
} |
| 1168 |
|
| 1169 |
private static class MaxPrecedenceAction implements RuleAction { |
| 1170 |
public int max = 0; |
| 1171 |
|
| 1172 |
public void processRule(Rule r) { |
| 1173 |
if (r.getPrecedence() > max) { |
| 1174 |
max = r.getPrecedence(); |
| 1175 |
} |
| 1176 |
} |
| 1177 |
} |
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
public void computeRankings(int start) throws XPathException { |
| 1189 |
|
| 1190 |
final RuleSorter sorter = new RuleSorter(start); |
| 1191 |
|
| 1192 |
processRules(sorter::addRule); |
| 1193 |
|
| 1194 |
sorter.allocateRanks(); |
| 1195 |
highestRank = start + sorter.getNumberOfRules(); |
| 1196 |
} |
| 1197 |
|
| 1198 |
public int getMaxRank() { |
| 1199 |
return highestRank; |
| 1200 |
} |
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
private static class RuleSorter implements Sortable { |
| 1209 |
public ArrayList<Rule> rules = new ArrayList<>(100); |
| 1210 |
private int start; |
| 1211 |
|
| 1212 |
public RuleSorter(int start) { |
| 1213 |
this.start = start; |
| 1214 |
} |
| 1215 |
|
| 1216 |
public void addRule(Rule rule) { |
| 1217 |
rules.add(rule); |
| 1218 |
} |
| 1219 |
|
| 1220 |
public int compare(int a, int b) { |
| 1221 |
return rules.get(a).compareComputedRank(rules.get(b)); |
| 1222 |
} |
| 1223 |
|
| 1224 |
public void swap(int a, int b) { |
| 1225 |
Rule temp = rules.get(a); |
| 1226 |
rules.set(a, rules.get(b)); |
| 1227 |
rules.set(b, temp); |
| 1228 |
} |
| 1229 |
|
| 1230 |
public void allocateRanks() { |
| 1231 |
GenericSorter.quickSort(0, rules.size(), this); |
| 1232 |
int rank = start; |
| 1233 |
for (int i = 0; i < rules.size(); i++) { |
| 1234 |
if (i > 0 && rules.get(i - 1).compareComputedRank(rules.get(i)) != 0) { |
| 1235 |
rank++; |
| 1236 |
} |
| 1237 |
rules.get(i).setRank(rank); |
| 1238 |
} |
| 1239 |
} |
| 1240 |
|
| 1241 |
public int getNumberOfRules() { |
| 1242 |
return rules.size(); |
| 1243 |
} |
| 1244 |
} |
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
public interface RuleGroupAction { |
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
void setString(String s); |
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
void start(); |
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
void start(int i); |
| 1269 |
|
| 1270 |
|
| 1271 |
|
| 1272 |
|
| 1273 |
void end(); |
| 1274 |
|
| 1275 |
} |
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
public void allocateAllPatternSlots() { |
| 1283 |
final List<Integer> count = new ArrayList<>(1); |
| 1284 |
count.add(0); |
| 1285 |
final SlotManager slotManager = new SlotManager(); |
| 1286 |
final RuleAction slotAllocator = r -> { |
| 1287 |
int slots = r.getPattern().allocateSlots(slotManager, 0); |
| 1288 |
int max = Math.max(count.get(0), slots); |
| 1289 |
count.set(0, max); |
| 1290 |
}; |
| 1291 |
try { |
| 1292 |
processRules(slotAllocator); |
| 1293 |
} catch (XPathException e) { |
| 1294 |
throw new AssertionError(e); |
| 1295 |
} |
| 1296 |
stackFrameSlotsNeeded = count.get(0); |
| 1297 |
} |
| 1298 |
|
| 1299 |
@Override |
| 1300 |
public int getStackFrameSlotsNeeded() { |
| 1301 |
return stackFrameSlotsNeeded; |
| 1302 |
} |
| 1303 |
|
| 1304 |
public void setStackFrameSlotsNeeded(int slots) { |
| 1305 |
this.stackFrameSlotsNeeded = slots; |
| 1306 |
} |
| 1307 |
|
| 1308 |
|
| 1309 |
} |
| 1310 |
|