| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr.instruct; |
| 9 |
|
| 10 |
import net.sf.saxon.Controller; |
| 11 |
import net.sf.saxon.event.PipelineConfiguration; |
| 12 |
import net.sf.saxon.expr.*; |
| 13 |
import net.sf.saxon.expr.parser.*; |
| 14 |
import net.sf.saxon.expr.sort.*; |
| 15 |
import net.sf.saxon.functions.CurrentGroupCall; |
| 16 |
import net.sf.saxon.lib.StringCollator; |
| 17 |
import net.sf.saxon.lib.TraceListener; |
| 18 |
import net.sf.saxon.om.*; |
| 19 |
import net.sf.saxon.pattern.Pattern; |
| 20 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 21 |
import net.sf.saxon.trans.Err; |
| 22 |
import net.sf.saxon.trans.XPathException; |
| 23 |
import net.sf.saxon.type.ErrorType; |
| 24 |
import net.sf.saxon.type.ItemType; |
| 25 |
import net.sf.saxon.type.SchemaType; |
| 26 |
import net.sf.saxon.value.AtomicValue; |
| 27 |
import net.sf.saxon.value.StringValue; |
| 28 |
|
| 29 |
import java.net.URI; |
| 30 |
import java.net.URISyntaxException; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class ForEachGroup extends Instruction |
| 39 |
implements SortKeyEvaluator, ContextMappingFunction<Item<?>>, ContextSwitchingExpression { |
| 40 |
|
| 41 |
public static final int GROUP_BY = 0; |
| 42 |
public static final int GROUP_ADJACENT = 1; |
| 43 |
public static final int GROUP_STARTING = 2; |
| 44 |
public static final int GROUP_ENDING = 3; |
| 45 |
|
| 46 |
private byte algorithm; |
| 47 |
private int keyItemType; |
| 48 |
private StringCollator collator = null; |
| 49 |
private transient AtomicComparer[] sortComparators = null; |
| 50 |
private boolean composite = false; |
| 51 |
private boolean isInFork = false; |
| 52 |
|
| 53 |
private Operand selectOp; |
| 54 |
private Operand actionOp; |
| 55 |
private Operand keyOp; |
| 56 |
private Operand collationOp; |
| 57 |
private Operand sortKeysOp; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public ForEachGroup(Expression select, |
| 72 |
Expression action, |
| 73 |
byte algorithm, |
| 74 |
Expression key, |
| 75 |
StringCollator collator, |
| 76 |
Expression collationNameExpression, |
| 77 |
SortKeyDefinitionList sortKeys) { |
| 78 |
selectOp = new Operand(this, select, OperandRole.FOCUS_CONTROLLING_SELECT); |
| 79 |
actionOp = new Operand(this, action, OperandRole.FOCUS_CONTROLLED_ACTION); |
| 80 |
OperandRole keyRole = (algorithm == GROUP_ENDING || algorithm == GROUP_STARTING) ? OperandRole.PATTERN : OperandRole.NEW_FOCUS_ATOMIC; |
| 81 |
keyOp = new Operand(this, key, keyRole); |
| 82 |
if (collationNameExpression != null) { |
| 83 |
collationOp = new Operand(this, collationNameExpression, OperandRole.SINGLE_ATOMIC); |
| 84 |
} |
| 85 |
if (sortKeys != null) { |
| 86 |
sortKeysOp = new Operand(this, sortKeys, OperandRole.SINGLE_ATOMIC); |
| 87 |
} |
| 88 |
this.algorithm = algorithm; |
| 89 |
this.collator = collator; |
| 90 |
for (Operand o : operands()) { |
| 91 |
adoptChildExpression(o.getChildExpression()); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public int getInstructionNameCode() { |
| 102 |
return StandardNames.XSL_FOR_EACH_GROUP; |
| 103 |
} |
| 104 |
|
| 105 |
@Override |
| 106 |
public Iterable<Operand> operands() { |
| 107 |
return operandSparseList(selectOp, actionOp, keyOp, collationOp, sortKeysOp); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public Expression getSelectExpression() { |
| 117 |
return selectOp.getChildExpression(); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public Expression getActionExpression() { |
| 127 |
return actionOp.getChildExpression(); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public byte getAlgorithm() { |
| 137 |
return algorithm; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public Expression getGroupingKey() { |
| 148 |
return keyOp.getChildExpression(); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
public int getKeyItemType() { |
| 158 |
return keyItemType; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
public SortKeyDefinitionList getSortKeyDefinitions() { |
| 169 |
return sortKeysOp == null ? null : (SortKeyDefinitionList)sortKeysOp.getChildExpression(); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public AtomicComparer[] getSortKeyComparators() { |
| 180 |
return sortComparators; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public StringCollator getCollation() { |
| 191 |
return collator; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
public URI getBaseURI() { |
| 202 |
try { |
| 203 |
return getRetainedStaticContext().getStaticBaseUri(); |
| 204 |
} catch (XPathException err) { |
| 205 |
return null; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
public boolean isComposite() { |
| 210 |
return composite; |
| 211 |
} |
| 212 |
|
| 213 |
public void setComposite(boolean composite) { |
| 214 |
this.composite = composite; |
| 215 |
} |
| 216 |
|
| 217 |
public boolean isInFork() { |
| 218 |
return isInFork; |
| 219 |
} |
| 220 |
|
| 221 |
public void setIsInFork(boolean inFork) { |
| 222 |
isInFork = inFork; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
@Override |
| 234 |
public boolean allowExtractingCommonSubexpressions() { |
| 235 |
return false; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 240 |
|
| 241 |
selectOp.typeCheck(visitor, contextInfo); |
| 242 |
if (collationOp != null) { |
| 243 |
collationOp.typeCheck(visitor, contextInfo); |
| 244 |
} |
| 245 |
|
| 246 |
ItemType selectedItemType = getSelectExpression().getItemType(); |
| 247 |
if (selectedItemType == ErrorType.getInstance()) { |
| 248 |
return Literal.makeEmptySequence(); |
| 249 |
} |
| 250 |
|
| 251 |
for (Operand o : operands()) { |
| 252 |
fixupGroupReferences(this, this, selectedItemType, false); |
| 253 |
} |
| 254 |
|
| 255 |
ContextItemStaticInfo cit = visitor.getConfiguration().makeContextItemStaticInfo(selectedItemType, false); |
| 256 |
cit.setContextSettingExpression(getSelectExpression()); |
| 257 |
|
| 258 |
actionOp.typeCheck(visitor, cit); |
| 259 |
keyOp.typeCheck(visitor, cit); |
| 260 |
|
| 261 |
if (Literal.isEmptySequence(getSelectExpression())) { |
| 262 |
return getSelectExpression(); |
| 263 |
} |
| 264 |
if (Literal.isEmptySequence(getActionExpression())) { |
| 265 |
return getActionExpression(); |
| 266 |
} |
| 267 |
if (getSortKeyDefinitions() != null) { |
| 268 |
|
| 269 |
boolean allFixed = true; |
| 270 |
for (SortKeyDefinition sk : getSortKeyDefinitions()) { |
| 271 |
Expression sortKey = sk.getSortKey(); |
| 272 |
sortKey = sortKey.typeCheck(visitor, cit); |
| 273 |
if (sk.isBackwardsCompatible()) { |
| 274 |
sortKey = FirstItemExpression.makeFirstItemExpression(sortKey); |
| 275 |
} else { |
| 276 |
RoleDiagnostic role = |
| 277 |
new RoleDiagnostic(RoleDiagnostic.INSTRUCTION, "xsl:sort/select", 0); |
| 278 |
role.setErrorCode("XTTE1020"); |
| 279 |
sortKey = CardinalityChecker.makeCardinalityChecker(sortKey, StaticProperty.ALLOWS_ZERO_OR_ONE, role); |
| 280 |
} |
| 281 |
sk.setSortKey(sortKey, true); |
| 282 |
sk.typeCheck(visitor, contextInfo); |
| 283 |
if (sk.isFixed()) { |
| 284 |
AtomicComparer comp = sk.makeComparator( |
| 285 |
visitor.getStaticContext().makeEarlyEvaluationContext()); |
| 286 |
sk.setFinalComparator(comp); |
| 287 |
} else { |
| 288 |
allFixed = false; |
| 289 |
} |
| 290 |
} |
| 291 |
if (allFixed) { |
| 292 |
sortComparators = new AtomicComparer[getSortKeyDefinitions().size()]; |
| 293 |
for (int i = 0; i < getSortKeyDefinitions().size(); i++) { |
| 294 |
sortComparators[i] = getSortKeyDefinitions().getSortKeyDefinition(i).getFinalComparator(); |
| 295 |
} |
| 296 |
} |
| 297 |
} |
| 298 |
keyItemType = getGroupingKey().getItemType().getPrimitiveType(); |
| 299 |
|
| 300 |
return this; |
| 301 |
} |
| 302 |
|
| 303 |
private static void fixupGroupReferences(Expression exp, ForEachGroup feg, ItemType selectedItemType, boolean isInLoop) { |
| 304 |
if (exp == null) { |
| 305 |
|
| 306 |
} else if (exp instanceof CurrentGroupCall) { |
| 307 |
((CurrentGroupCall) exp).setControllingInstruction(feg, selectedItemType, isInLoop); |
| 308 |
} else if (exp instanceof ForEachGroup) { |
| 309 |
|
| 310 |
|
| 311 |
ForEachGroup feg2 = (ForEachGroup)exp; |
| 312 |
if (feg2 == feg) { |
| 313 |
fixupGroupReferences(feg2.getActionExpression(), feg, selectedItemType, false); |
| 314 |
} else { |
| 315 |
fixupGroupReferences(feg2.getSelectExpression(), feg, selectedItemType, isInLoop); |
| 316 |
fixupGroupReferences(feg2.getGroupingKey(), feg, selectedItemType, isInLoop); |
| 317 |
if (feg2.getSortKeyDefinitions() != null) { |
| 318 |
for (SortKeyDefinition skd : feg2.getSortKeyDefinitions()) { |
| 319 |
fixupGroupReferences(skd.getOrder(), feg, selectedItemType, isInLoop); |
| 320 |
fixupGroupReferences(skd.getCaseOrder(), feg, selectedItemType, isInLoop); |
| 321 |
fixupGroupReferences(skd.getDataTypeExpression(), feg, selectedItemType, isInLoop); |
| 322 |
fixupGroupReferences(skd.getLanguage(), feg, selectedItemType, isInLoop); |
| 323 |
fixupGroupReferences(skd.getCollationNameExpression(), feg, selectedItemType, isInLoop); |
| 324 |
fixupGroupReferences(skd.getOrder(), feg, selectedItemType, isInLoop); |
| 325 |
} |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
} else { |
| 330 |
for (Operand o : exp.operands()) { |
| 331 |
fixupGroupReferences(o.getChildExpression(), feg, selectedItemType, isInLoop || o.isEvaluatedRepeatedly()); |
| 332 |
} |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException { |
| 338 |
selectOp.optimize(visitor, contextItemType); |
| 339 |
ItemType selectedItemType = getSelectExpression().getItemType(); |
| 340 |
ContextItemStaticInfo sit = visitor.getConfiguration().makeContextItemStaticInfo(selectedItemType, false); |
| 341 |
sit.setContextSettingExpression(getSelectExpression()); |
| 342 |
actionOp.optimize(visitor, sit); |
| 343 |
keyOp.optimize(visitor, sit); |
| 344 |
|
| 345 |
if (Literal.isEmptySequence(getSelectExpression())) { |
| 346 |
return getSelectExpression(); |
| 347 |
} |
| 348 |
if (Literal.isEmptySequence(getActionExpression())) { |
| 349 |
return getActionExpression(); |
| 350 |
} |
| 351 |
|
| 352 |
if (getSortKeyDefinitions() != null) { |
| 353 |
for (SortKeyDefinition skd : getSortKeyDefinitions()) { |
| 354 |
Expression sortKey = skd.getSortKey(); |
| 355 |
sortKey = sortKey.optimize(visitor, sit); |
| 356 |
skd.setSortKey(sortKey, true); |
| 357 |
} |
| 358 |
} |
| 359 |
if (collationOp != null) { |
| 360 |
collationOp.optimize(visitor, contextItemType); |
| 361 |
} |
| 362 |
if (collator == null && (getCollationNameExpression() instanceof StringLiteral)) { |
| 363 |
String collation = ((StringLiteral) getCollationNameExpression()).getStringValue(); |
| 364 |
URI collationURI; |
| 365 |
try { |
| 366 |
collationURI = new URI(collation); |
| 367 |
if (!collationURI.isAbsolute()) { |
| 368 |
collationURI = getStaticBaseURI().resolve(collationURI); |
| 369 |
final String collationNameString = collationURI.toString(); |
| 370 |
setCollationNameExpression(new StringLiteral(collationNameString)); |
| 371 |
collator = visitor.getConfiguration().getCollation(collationNameString); |
| 372 |
if (collator == null) { |
| 373 |
XPathException err = new XPathException("Unknown collation " + Err.wrap(collationURI.toString(), Err.URI)); |
| 374 |
err.setErrorCode("XTDE1110"); |
| 375 |
err.setLocation(getLocation()); |
| 376 |
throw err; |
| 377 |
} |
| 378 |
} |
| 379 |
} catch (URISyntaxException err) { |
| 380 |
XPathException e = new XPathException("Collation name '" + getCollationNameExpression() + "' is not a valid URI"); |
| 381 |
e.setErrorCode("XTDE1110"); |
| 382 |
e.setLocation(getLocation()); |
| 383 |
throw e; |
| 384 |
} |
| 385 |
} |
| 386 |
return this; |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
public Expression copy(RebindingMap rebindings) { |
| 400 |
SortKeyDefinition[] newKeyDef = null; |
| 401 |
if (getSortKeyDefinitions() != null) { |
| 402 |
newKeyDef = new SortKeyDefinition[getSortKeyDefinitions().size()]; |
| 403 |
for (int i = 0; i < getSortKeyDefinitions().size(); i++) { |
| 404 |
newKeyDef[i] = getSortKeyDefinitions().getSortKeyDefinition(i).copy(rebindings); |
| 405 |
} |
| 406 |
} |
| 407 |
ForEachGroup feg = new ForEachGroup( |
| 408 |
getSelectExpression().copy(rebindings), |
| 409 |
getActionExpression().copy(rebindings), |
| 410 |
algorithm, |
| 411 |
getGroupingKey().copy(rebindings), |
| 412 |
collator, |
| 413 |
getCollationNameExpression().copy(rebindings), |
| 414 |
newKeyDef == null ? null : new SortKeyDefinitionList(newKeyDef)); |
| 415 |
ExpressionTool.copyLocationInfo(this, feg); |
| 416 |
feg.setComposite(isComposite()); |
| 417 |
fixupGroupReferences(feg, feg, getSelectExpression().getItemType(), false); |
| 418 |
return feg; |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
public ItemType getItemType() { |
| 430 |
return getActionExpression().getItemType(); |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
public int computeDependencies() { |
| 444 |
|
| 445 |
|
| 446 |
int dependencies = 0; |
| 447 |
dependencies |= getSelectExpression().getDependencies(); |
| 448 |
dependencies |= getGroupingKey().getDependencies() & ~StaticProperty.DEPENDS_ON_FOCUS; |
| 449 |
dependencies |= getActionExpression().getDependencies() |
| 450 |
& ~(StaticProperty.DEPENDS_ON_FOCUS | StaticProperty.DEPENDS_ON_CURRENT_GROUP); |
| 451 |
if (getSortKeyDefinitions() != null) { |
| 452 |
for (SortKeyDefinition skd : getSortKeyDefinitions()) { |
| 453 |
dependencies |= skd.getSortKey().getDependencies() & ~StaticProperty.DEPENDS_ON_FOCUS; |
| 454 |
Expression e = skd.getCaseOrder(); |
| 455 |
if (e != null && !(e instanceof Literal)) { |
| 456 |
dependencies |= e.getDependencies(); |
| 457 |
} |
| 458 |
e = skd.getDataTypeExpression(); |
| 459 |
if (e != null && !(e instanceof Literal)) { |
| 460 |
dependencies |= e.getDependencies(); |
| 461 |
} |
| 462 |
e = skd.getLanguage(); |
| 463 |
if (e != null && !(e instanceof Literal)) { |
| 464 |
dependencies |= e.getDependencies(); |
| 465 |
} |
| 466 |
} |
| 467 |
} |
| 468 |
if (getCollationNameExpression() != null) { |
| 469 |
dependencies |= getCollationNameExpression().getDependencies(); |
| 470 |
} |
| 471 |
return dependencies; |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
@Override |
| 482 |
public int computeSpecialProperties() { |
| 483 |
int p = super.computeSpecialProperties(); |
| 484 |
p |= getActionExpression().getSpecialProperties() & StaticProperty.ALL_NODES_UNTYPED; |
| 485 |
return p; |
| 486 |
} |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
public final boolean mayCreateNewNodes() { |
| 495 |
int props = getActionExpression().getSpecialProperties(); |
| 496 |
return (props & StaticProperty.NO_NODES_NEWLY_CREATED) == 0; |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
@Override |
| 506 |
public String getStreamerName() { |
| 507 |
return "ForEachGroup"; |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
public PathMap.PathMapNodeSet addToPathMap(PathMap pathMap, PathMap.PathMapNodeSet pathMapNodeSet) { |
| 531 |
PathMap.PathMapNodeSet target = getSelectExpression().addToPathMap(pathMap, pathMapNodeSet); |
| 532 |
if (getCollationNameExpression() != null) { |
| 533 |
getCollationNameExpression().addToPathMap(pathMap, pathMapNodeSet); |
| 534 |
} |
| 535 |
if (getSortKeyDefinitions() != null) { |
| 536 |
for (SortKeyDefinition skd : getSortKeyDefinitions()) { |
| 537 |
skd.getSortKey().addToPathMap(pathMap, target); |
| 538 |
Expression e = skd.getOrder(); |
| 539 |
if (e != null) { |
| 540 |
e.addToPathMap(pathMap, pathMapNodeSet); |
| 541 |
} |
| 542 |
e = skd.getCaseOrder(); |
| 543 |
if (e != null) { |
| 544 |
e.addToPathMap(pathMap, pathMapNodeSet); |
| 545 |
} |
| 546 |
e = skd.getDataTypeExpression(); |
| 547 |
if (e != null) { |
| 548 |
e.addToPathMap(pathMap, pathMapNodeSet); |
| 549 |
} |
| 550 |
e = skd.getLanguage(); |
| 551 |
if (e != null) { |
| 552 |
e.addToPathMap(pathMap, pathMapNodeSet); |
| 553 |
} |
| 554 |
e = skd.getCollationNameExpression(); |
| 555 |
if (e != null) { |
| 556 |
e.addToPathMap(pathMap, pathMapNodeSet); |
| 557 |
} |
| 558 |
} |
| 559 |
} |
| 560 |
return getActionExpression().addToPathMap(pathMap, target); |
| 561 |
} |
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
public void checkPermittedContents(SchemaType parentType, boolean whole) throws XPathException { |
| 573 |
getActionExpression().checkPermittedContents(parentType, false); |
| 574 |
} |
| 575 |
|
| 576 |
public TailCall processLeavingTail(XPathContext context) throws XPathException { |
| 577 |
Controller controller = context.getController(); |
| 578 |
assert controller != null; |
| 579 |
PipelineConfiguration pipe = context.getReceiver().getPipelineConfiguration(); |
| 580 |
|
| 581 |
GroupIterator groupIterator = getGroupIterator(context); |
| 582 |
XPathContextMajor c2 = context.newContext(); |
| 583 |
c2.setOrigin(this); |
| 584 |
FocusIterator focusIterator = c2.trackFocus(groupIterator); |
| 585 |
c2.setCurrentGroupIterator(groupIterator); |
| 586 |
c2.setCurrentTemplateRule(null); |
| 587 |
pipe.setXPathContext(c2); |
| 588 |
|
| 589 |
if (controller.isTracing()) { |
| 590 |
TraceListener listener = controller.getTraceListener(); |
| 591 |
assert listener != null; |
| 592 |
Item item; |
| 593 |
while ((item = focusIterator.next()) != null) { |
| 594 |
listener.startCurrentItem(item); |
| 595 |
getActionExpression().process(c2); |
| 596 |
listener.endCurrentItem(item); |
| 597 |
} |
| 598 |
} else { |
| 599 |
while (focusIterator.next() != null) { |
| 600 |
getActionExpression().process(c2); |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
pipe.setXPathContext(context); |
| 605 |
return null; |
| 606 |
} |
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
public Expression getCollationNameExpression() { |
| 615 |
return collationOp == null ? null : collationOp.getChildExpression(); |
| 616 |
} |
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
private StringCollator getCollator(XPathContext context) throws XPathException { |
| 627 |
if (getCollationNameExpression() != null) { |
| 628 |
StringValue collationValue = (StringValue) getCollationNameExpression().evaluateItem(context); |
| 629 |
assert collationValue != null; |
| 630 |
String cname = collationValue.getStringValue(); |
| 631 |
try { |
| 632 |
return context.getConfiguration().getCollation(cname, getStaticBaseURIString(), "FOCH0002"); |
| 633 |
} catch (XPathException e) { |
| 634 |
e.setLocation(getLocation()); |
| 635 |
throw e; |
| 636 |
} |
| 637 |
|
| 638 |
} else { |
| 639 |
|
| 640 |
return CodepointCollator.getInstance(); |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
private GroupIterator getGroupIterator(XPathContext context) throws XPathException { |
| 645 |
return getGroupIterator(getSelectExpression(), context); |
| 646 |
} |
| 647 |
|
| 648 |
public GroupIterator getGroupIterator(Expression select, XPathContext context) throws XPathException { |
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
GroupIterator groupIterator; |
| 653 |
switch (algorithm) { |
| 654 |
case GROUP_BY: { |
| 655 |
StringCollator coll = collator; |
| 656 |
if (coll==null) { |
| 657 |
|
| 658 |
coll = getCollator(context); |
| 659 |
} |
| 660 |
XPathContext c2 = context.newMinorContext(); |
| 661 |
FocusIterator<?> population = c2.trackFocus(select.iterate(context)); |
| 662 |
groupIterator = new GroupByIterator(population, getGroupingKey(), c2, coll, composite); |
| 663 |
break; |
| 664 |
} |
| 665 |
case GROUP_ADJACENT: { |
| 666 |
StringCollator coll = collator; |
| 667 |
if (coll==null) { |
| 668 |
|
| 669 |
coll = getCollator(context); |
| 670 |
} |
| 671 |
groupIterator = new GroupAdjacentIterator(select, getGroupingKey(), context, coll, composite); |
| 672 |
break; |
| 673 |
} |
| 674 |
case GROUP_STARTING: |
| 675 |
groupIterator = new GroupStartingIterator(select, (Pattern) getGroupingKey(), context); |
| 676 |
break; |
| 677 |
case GROUP_ENDING: |
| 678 |
groupIterator = new GroupEndingIterator(select, (Pattern) getGroupingKey(), context); |
| 679 |
break; |
| 680 |
default: |
| 681 |
throw new AssertionError("Unknown grouping algorithm"); |
| 682 |
} |
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
if (getSortKeyDefinitions() != null) { |
| 687 |
AtomicComparer[] comps = sortComparators; |
| 688 |
XPathContext xpc = context.newMinorContext(); |
| 689 |
if (comps == null) { |
| 690 |
comps = new AtomicComparer[getSortKeyDefinitions().size()]; |
| 691 |
for (int s = 0; s < getSortKeyDefinitions().size(); s++) { |
| 692 |
comps[s] = getSortKeyDefinitions().getSortKeyDefinition(s).makeComparator(xpc); |
| 693 |
} |
| 694 |
} |
| 695 |
groupIterator = new SortedGroupIterator(xpc, groupIterator, this, comps); |
| 696 |
|
| 697 |
} |
| 698 |
|
| 699 |
return groupIterator; |
| 700 |
} |
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
public SequenceIterator<?> iterate(XPathContext context) throws XPathException { |
| 720 |
GroupIterator master = getGroupIterator(context); |
| 721 |
XPathContextMajor c2 = context.newContext(); |
| 722 |
c2.setOrigin(this); |
| 723 |
c2.trackFocus(master); |
| 724 |
c2.setCurrentGroupIterator(master); |
| 725 |
c2.setCurrentTemplateRule(null); |
| 726 |
return new ContextMappingIterator<>(this, c2); |
| 727 |
} |
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
public SequenceIterator<?> map(XPathContext context) throws XPathException { |
| 740 |
return getActionExpression().iterate(context); |
| 741 |
} |
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
public AtomicValue evaluateSortKey(int n, XPathContext c) throws XPathException { |
| 748 |
return (AtomicValue) getSortKeyDefinitions().getSortKeyDefinition(n).getSortKey().evaluateItem(c); |
| 749 |
} |
| 750 |
|
| 751 |
|
| 752 |
public SortKeyDefinitionList getSortKeyDefinitionList() { |
| 753 |
if (sortKeysOp == null) { |
| 754 |
return null; |
| 755 |
} |
| 756 |
return (SortKeyDefinitionList) sortKeysOp.getChildExpression(); |
| 757 |
} |
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
public void export(ExpressionPresenter out) throws XPathException { |
| 765 |
out.startElement("forEachGroup", this); |
| 766 |
out.emitAttribute("algorithm", getAlgorithmName(algorithm)); |
| 767 |
String flags = ""; |
| 768 |
if (composite) { |
| 769 |
flags = "c"; |
| 770 |
} |
| 771 |
if (isInFork()) { |
| 772 |
flags += "k"; |
| 773 |
} |
| 774 |
if (!flags.isEmpty()) { |
| 775 |
out.emitAttribute("flags", flags); |
| 776 |
} |
| 777 |
out.setChildRole("select"); |
| 778 |
getSelectExpression().export(out); |
| 779 |
if (algorithm == GROUP_BY || algorithm == GROUP_ADJACENT) { |
| 780 |
out.setChildRole("key"); |
| 781 |
getGroupingKey().export(out); |
| 782 |
} else { |
| 783 |
out.setChildRole("match"); |
| 784 |
getGroupingKey().export(out); |
| 785 |
} |
| 786 |
if (getSortKeyDefinitions() != null) { |
| 787 |
out.setChildRole("sort"); |
| 788 |
getSortKeyDefinitionList().export(out); |
| 789 |
} |
| 790 |
if (getCollationNameExpression() != null) { |
| 791 |
out.setChildRole("collation"); |
| 792 |
getCollationNameExpression().export(out); |
| 793 |
} |
| 794 |
out.setChildRole("content"); |
| 795 |
getActionExpression().export(out); |
| 796 |
out.endElement(); |
| 797 |
} |
| 798 |
|
| 799 |
private static String getAlgorithmName(byte algorithm) { |
| 800 |
switch (algorithm) { |
| 801 |
case GROUP_BY: |
| 802 |
return "by"; |
| 803 |
case GROUP_ADJACENT: |
| 804 |
return "adjacent"; |
| 805 |
case GROUP_STARTING: |
| 806 |
return "starting"; |
| 807 |
case GROUP_ENDING: |
| 808 |
return "ending"; |
| 809 |
default: |
| 810 |
return "** unknown algorithm **"; |
| 811 |
} |
| 812 |
} |
| 813 |
|
| 814 |
public void setSelect(Expression select) { |
| 815 |
selectOp.setChildExpression(select); |
| 816 |
} |
| 817 |
|
| 818 |
public void setAction(Expression action) { |
| 819 |
actionOp.setChildExpression(action); |
| 820 |
} |
| 821 |
|
| 822 |
public void setKey(Expression key) { |
| 823 |
keyOp.setChildExpression(key); |
| 824 |
} |
| 825 |
|
| 826 |
public void setCollationNameExpression(Expression collationNameExpression) { |
| 827 |
if (collationOp == null) { |
| 828 |
collationOp = new Operand(this, collationNameExpression, OperandRole.SINGLE_ATOMIC); |
| 829 |
} else { |
| 830 |
collationOp.setChildExpression(collationNameExpression); |
| 831 |
} |
| 832 |
} |
| 833 |
|
| 834 |
public void setSortKeyDefinitions(SortKeyDefinitionList sortKeyDefinitions) { |
| 835 |
if (sortKeysOp == null) { |
| 836 |
sortKeysOp = new Operand(this, sortKeyDefinitions, OperandRole.SINGLE_ATOMIC); |
| 837 |
} else { |
| 838 |
sortKeysOp.setChildExpression(sortKeyDefinitions); |
| 839 |
} |
| 840 |
} |
| 841 |
} |
| 842 |
|