| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr.sort; |
| 9 |
|
| 10 |
import com.saxonica.ee.stream.adjunct.MergeInstrAdjunct; |
| 11 |
import net.sf.saxon.Configuration; |
| 12 |
import net.sf.saxon.event.Receiver; |
| 13 |
import net.sf.saxon.event.ReceiverOptions; |
| 14 |
import net.sf.saxon.expr.*; |
| 15 |
import net.sf.saxon.expr.accum.Accumulator; |
| 16 |
import net.sf.saxon.expr.accum.AccumulatorManager; |
| 17 |
import net.sf.saxon.expr.instruct.Instruction; |
| 18 |
import net.sf.saxon.expr.instruct.TailCall; |
| 19 |
import net.sf.saxon.expr.parser.*; |
| 20 |
import net.sf.saxon.functions.*; |
| 21 |
import net.sf.saxon.lib.Feature; |
| 22 |
import net.sf.saxon.lib.ParseOptions; |
| 23 |
import net.sf.saxon.lib.Validation; |
| 24 |
import net.sf.saxon.om.*; |
| 25 |
import net.sf.saxon.pattern.NodeKindTest; |
| 26 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 27 |
import net.sf.saxon.trans.NoDynamicContextException; |
| 28 |
import net.sf.saxon.trans.XPathException; |
| 29 |
import net.sf.saxon.trans.XsltController; |
| 30 |
import net.sf.saxon.tree.iter.EmptyIterator; |
| 31 |
import net.sf.saxon.tree.iter.ManualIterator; |
| 32 |
import net.sf.saxon.tree.iter.SingletonIterator; |
| 33 |
import net.sf.saxon.tree.util.FastStringBuffer; |
| 34 |
import net.sf.saxon.type.ItemType; |
| 35 |
import net.sf.saxon.type.SchemaType; |
| 36 |
import net.sf.saxon.type.Type; |
| 37 |
import net.sf.saxon.type.TypeHierarchy; |
| 38 |
import net.sf.saxon.value.ExternalObject; |
| 39 |
import net.sf.saxon.value.ObjectValue; |
| 40 |
import net.sf.saxon.value.SequenceType; |
| 41 |
|
| 42 |
import java.util.ArrayList; |
| 43 |
import java.util.List; |
| 44 |
import java.util.Set; |
| 45 |
|
| 46 |
public class MergeInstr extends Instruction { |
| 47 |
|
| 48 |
protected MergeSource[] mergeSources; |
| 49 |
private Operand actionOp; |
| 50 |
protected AtomicComparer[] comparators; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public static class MergeSource { |
| 58 |
|
| 59 |
private MergeInstr instruction; |
| 60 |
public Location location; |
| 61 |
private Operand forEachItemOp = null; |
| 62 |
private Operand forEachStreamOp = null; |
| 63 |
private Operand rowSelectOp = null; |
| 64 |
public String sourceName = null; |
| 65 |
public SortKeyDefinitionList mergeKeyDefinitions = null; |
| 66 |
public String baseURI = null; |
| 67 |
public int validation; |
| 68 |
public SchemaType schemaType; |
| 69 |
public boolean streamable; |
| 70 |
public Set<Accumulator> accumulators; |
| 71 |
public Object invertedAction; |
| 72 |
|
| 73 |
public MergeSource(MergeInstr mi) { |
| 74 |
this.instruction = mi; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public MergeSource(MergeInstr instruction, |
| 89 |
Expression forEachItem, Expression forEachStream, Expression rSelect, String name, SortKeyDefinitionList sKeys, String baseURI) { |
| 90 |
this.instruction = instruction; |
| 91 |
if (forEachItem != null) { |
| 92 |
initForEachItem(instruction, forEachItem); |
| 93 |
} |
| 94 |
if (forEachStream != null) { |
| 95 |
initForEachStream(instruction, forEachStream); |
| 96 |
} |
| 97 |
if (rSelect != null) { |
| 98 |
initRowSelect(instruction, rSelect); |
| 99 |
} |
| 100 |
this.sourceName = name; |
| 101 |
this.mergeKeyDefinitions = sKeys; |
| 102 |
this.baseURI = baseURI; |
| 103 |
} |
| 104 |
|
| 105 |
public void initForEachItem(MergeInstr instruction, Expression forEachItem) { |
| 106 |
forEachItemOp = new Operand(instruction, forEachItem, OperandRole.INSPECT); |
| 107 |
} |
| 108 |
|
| 109 |
public void initForEachStream(MergeInstr instruction, Expression forEachStream) { |
| 110 |
forEachStreamOp = new Operand(instruction, forEachStream, OperandRole.INSPECT); |
| 111 |
} |
| 112 |
|
| 113 |
public void initRowSelect(MergeInstr instruction, Expression rowSelect) { |
| 114 |
rowSelectOp = new Operand(instruction, rowSelect, ROW_SELECT); |
| 115 |
} |
| 116 |
|
| 117 |
public void setStreamable(boolean streamable) { |
| 118 |
this.streamable = streamable; |
| 119 |
if (streamable && instruction.getConfiguration().getBooleanProperty(Feature.STREAMING_FALLBACK)) { |
| 120 |
this.streamable = false; |
| 121 |
Expression select = rowSelectOp.getChildExpression(); |
| 122 |
rowSelectOp.setChildExpression( |
| 123 |
SystemFunction.makeCall("snapshot", select.getRetainedStaticContext(), select)); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
public MergeSource copyMergeSource(MergeInstr newInstr, RebindingMap rebindings) { |
| 128 |
SortKeyDefinition[] newKeyDef = new SortKeyDefinition[mergeKeyDefinitions.size()]; |
| 129 |
|
| 130 |
for (int i = 0; i < mergeKeyDefinitions.size(); i++) { |
| 131 |
newKeyDef[i] = mergeKeyDefinitions.getSortKeyDefinition(i).copy(rebindings); |
| 132 |
|
| 133 |
} |
| 134 |
|
| 135 |
MergeSource ms = new MergeSource(newInstr, |
| 136 |
copy(getForEachItem(), rebindings), copy(getForEachSource(), rebindings), |
| 137 |
copy(getRowSelect(), rebindings), sourceName, new SortKeyDefinitionList(newKeyDef), baseURI); |
| 138 |
ms.validation = validation; |
| 139 |
ms.schemaType = schemaType; |
| 140 |
ms.streamable = streamable; |
| 141 |
ms.location = location; |
| 142 |
return ms; |
| 143 |
} |
| 144 |
|
| 145 |
private static Expression copy(Expression exp, RebindingMap rebindings) { |
| 146 |
return exp == null ? null : exp.copy(rebindings); |
| 147 |
} |
| 148 |
|
| 149 |
public Expression getForEachItem() { |
| 150 |
return forEachItemOp == null ? null : forEachItemOp.getChildExpression(); |
| 151 |
} |
| 152 |
|
| 153 |
public void setForEachItem(Expression forEachItem) { |
| 154 |
if (forEachItem != null) { |
| 155 |
forEachItemOp.setChildExpression(forEachItem); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
public Expression getForEachSource() { |
| 160 |
return forEachStreamOp == null ? null : forEachStreamOp.getChildExpression(); |
| 161 |
} |
| 162 |
|
| 163 |
public void setForEachStream(Expression forEachStream) { |
| 164 |
if (forEachStream != null) { |
| 165 |
forEachStreamOp.setChildExpression(forEachStream); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
public Expression getRowSelect() { |
| 170 |
return rowSelectOp.getChildExpression(); |
| 171 |
} |
| 172 |
|
| 173 |
public void setRowSelect(Expression rowSelect) { |
| 174 |
rowSelectOp.setChildExpression(rowSelect); |
| 175 |
} |
| 176 |
|
| 177 |
public SortKeyDefinitionList getMergeKeyDefinitionSet() { |
| 178 |
return mergeKeyDefinitions; |
| 179 |
} |
| 180 |
|
| 181 |
public void setMergeKeyDefinitionSet(SortKeyDefinitionList keys) { |
| 182 |
mergeKeyDefinitions = keys; |
| 183 |
} |
| 184 |
|
| 185 |
public void prepareForStreaming() throws XPathException { |
| 186 |
|
| 187 |
if (streamable) { |
| 188 |
MergeInstrAdjunct.checkStreamability(this, instruction.getConfiguration()); |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
public MergeInstr() {} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
public MergeInstr init(MergeSource[] mSources, Expression action) { |
| 206 |
|
| 207 |
actionOp = new Operand(this, action, OperandRole.FOCUS_CONTROLLED_ACTION); |
| 208 |
this.mergeSources = mSources; |
| 209 |
for (MergeSource mSource : mSources) { |
| 210 |
adoptChildExpression(mSource.getForEachItem()); |
| 211 |
adoptChildExpression(mSource.getForEachSource()); |
| 212 |
adoptChildExpression(mSource.getRowSelect()); |
| 213 |
|
| 214 |
} |
| 215 |
adoptChildExpression(action); |
| 216 |
|
| 217 |
return this; |
| 218 |
} |
| 219 |
|
| 220 |
public MergeSource[] getMergeSources() { |
| 221 |
return mergeSources; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
public void setAction(Expression action) { |
| 226 |
actionOp.setChildExpression(action); |
| 227 |
} |
| 228 |
|
| 229 |
public Expression getAction() { |
| 230 |
return actionOp.getChildExpression(); |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
@Override |
| 241 |
public int getInstructionNameCode() { |
| 242 |
return StandardNames.XSL_MERGE; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
public void checkPermittedContents(SchemaType parentType, boolean whole) throws XPathException { |
| 254 |
getAction().checkPermittedContents(parentType, false); |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
@Override |
| 266 |
public boolean allowExtractingCommonSubexpressions() { |
| 267 |
return false; |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
public ItemType getItemType() { |
| 278 |
return getAction().getItemType(); |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 284 |
|
| 285 |
final Configuration config = visitor.getConfiguration(); |
| 286 |
final TypeHierarchy th = config.getTypeHierarchy(); |
| 287 |
final TypeChecker tc = config.getTypeChecker(false); |
| 288 |
|
| 289 |
|
| 290 |
ItemType inputType = null; |
| 291 |
|
| 292 |
|
| 293 |
for (MergeSource mergeSource : mergeSources) { |
| 294 |
ContextItemStaticInfo rowContextItemType = contextInfo; |
| 295 |
if (mergeSource.getForEachItem() != null) { |
| 296 |
mergeSource.forEachItemOp.typeCheck(visitor, contextInfo); |
| 297 |
rowContextItemType = config.makeContextItemStaticInfo(mergeSource.getForEachItem().getItemType(), false); |
| 298 |
} else if (mergeSource.getForEachSource() != null) { |
| 299 |
mergeSource.forEachStreamOp.typeCheck(visitor, contextInfo); |
| 300 |
|
| 301 |
RoleDiagnostic role = new RoleDiagnostic(RoleDiagnostic.INSTRUCTION, "xsl:merge/for-each-source", 0); |
| 302 |
mergeSource.setForEachStream(tc.staticTypeCheck( |
| 303 |
mergeSource.getForEachSource(), SequenceType.STRING_SEQUENCE, role, visitor)); |
| 304 |
|
| 305 |
rowContextItemType = config.makeContextItemStaticInfo(NodeKindTest.DOCUMENT, false); |
| 306 |
} |
| 307 |
mergeSource.rowSelectOp.typeCheck(visitor, rowContextItemType); |
| 308 |
ItemType rowItemType = mergeSource.getRowSelect().getItemType(); |
| 309 |
if (inputType == null) { |
| 310 |
inputType = rowItemType; |
| 311 |
} else { |
| 312 |
inputType = Type.getCommonSuperType(inputType, rowItemType, th); |
| 313 |
} |
| 314 |
ContextItemStaticInfo cit = config.makeContextItemStaticInfo(inputType, false); |
| 315 |
if (mergeSource.mergeKeyDefinitions != null) { |
| 316 |
for (SortKeyDefinition skd : mergeSource.mergeKeyDefinitions) { |
| 317 |
Expression sortKey = skd.getSortKey(); |
| 318 |
sortKey = sortKey.typeCheck(visitor, cit); |
| 319 |
if (sortKey != null) { |
| 320 |
RoleDiagnostic role = |
| 321 |
new RoleDiagnostic(RoleDiagnostic.INSTRUCTION, "xsl:merge-key/select", 0); |
| 322 |
role.setErrorCode("XTTE1020"); |
| 323 |
sortKey = CardinalityChecker.makeCardinalityChecker(sortKey, StaticProperty.ALLOWS_ZERO_OR_ONE, role); |
| 324 |
|
| 325 |
skd.setSortKey(sortKey, true); |
| 326 |
} |
| 327 |
Expression exp = skd.getLanguage().typeCheck(visitor, config.makeContextItemStaticInfo(inputType, false)); |
| 328 |
skd.setLanguage(exp); |
| 329 |
|
| 330 |
exp = skd.getOrder().typeCheck(visitor, cit); |
| 331 |
skd.setOrder(exp); |
| 332 |
|
| 333 |
exp = skd.getCollationNameExpression(); |
| 334 |
if (exp != null) { |
| 335 |
exp = exp.typeCheck(visitor, cit); |
| 336 |
skd.setCollationNameExpression(exp); |
| 337 |
} |
| 338 |
|
| 339 |
exp = skd.getCaseOrder().typeCheck(visitor, cit); |
| 340 |
skd.setCaseOrder(exp); |
| 341 |
|
| 342 |
exp = skd.getDataTypeExpression(); |
| 343 |
if (exp != null) { |
| 344 |
exp = exp.typeCheck(visitor, cit); |
| 345 |
skd.setDataTypeExpression(exp); |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
} |
| 350 |
|
| 351 |
} |
| 352 |
|
| 353 |
actionOp.typeCheck(visitor, config.makeContextItemStaticInfo(inputType, false)); |
| 354 |
|
| 355 |
if (Literal.isEmptySequence(getAction())) { |
| 356 |
return getAction(); |
| 357 |
} |
| 358 |
if (mergeSources.length == 1 && Literal.isEmptySequence(mergeSources[0].getRowSelect())) { |
| 359 |
return mergeSources[0].getRowSelect(); |
| 360 |
} |
| 361 |
|
| 362 |
fixupGroupReferences(); |
| 363 |
|
| 364 |
return this; |
| 365 |
} |
| 366 |
|
| 367 |
public void fixupGroupReferences() { |
| 368 |
fixupGroupReferences(this, this, false); |
| 369 |
} |
| 370 |
|
| 371 |
private static void fixupGroupReferences(Expression exp, MergeInstr instr, boolean isInLoop) { |
| 372 |
if (exp == null) { |
| 373 |
|
| 374 |
} else if (exp.isCallOn(CurrentMergeGroup.class)) { |
| 375 |
CurrentMergeGroup fn = (CurrentMergeGroup)((SystemFunctionCall)exp).getTargetFunction(); |
| 376 |
fn.setControllingInstruction(instr, isInLoop); |
| 377 |
} else if (exp.isCallOn(CurrentMergeKey.class)) { |
| 378 |
CurrentMergeKey fn = (CurrentMergeKey) ((SystemFunctionCall) exp).getTargetFunction(); |
| 379 |
fn.setControllingInstruction(instr); |
| 380 |
} else if (exp instanceof MergeInstr) { |
| 381 |
|
| 382 |
|
| 383 |
MergeInstr instr2 = (MergeInstr) exp; |
| 384 |
if (instr2 == instr) { |
| 385 |
fixupGroupReferences(instr2.getAction(), instr, false); |
| 386 |
} else { |
| 387 |
for (MergeSource m : instr2.getMergeSources()) { |
| 388 |
for (SortKeyDefinition skd : m.mergeKeyDefinitions) { |
| 389 |
fixupGroupReferences(skd.getOrder(), instr, isInLoop); |
| 390 |
fixupGroupReferences(skd.getCaseOrder(), instr, isInLoop); |
| 391 |
fixupGroupReferences(skd.getDataTypeExpression(), instr, isInLoop); |
| 392 |
fixupGroupReferences(skd.getLanguage(), instr, isInLoop); |
| 393 |
fixupGroupReferences(skd.getCollationNameExpression(), instr, isInLoop); |
| 394 |
fixupGroupReferences(skd.getOrder(), instr, isInLoop); |
| 395 |
} |
| 396 |
if (m.forEachItemOp != null) { |
| 397 |
fixupGroupReferences(m.getForEachItem(), instr, isInLoop); |
| 398 |
} |
| 399 |
if (m.forEachStreamOp != null) { |
| 400 |
fixupGroupReferences(m.getForEachSource(), instr, isInLoop); |
| 401 |
} |
| 402 |
if (m.rowSelectOp != null) { |
| 403 |
fixupGroupReferences(m.getRowSelect(), instr, isInLoop); |
| 404 |
} |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
} else { |
| 409 |
for (Operand o : exp.operands()) { |
| 410 |
fixupGroupReferences(o.getChildExpression(), instr, isInLoop || o.isEvaluatedRepeatedly()); |
| 411 |
} |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
public final boolean mayCreateNewNodes() { |
| 423 |
int props = getAction().getSpecialProperties(); |
| 424 |
return (props & StaticProperty.NO_NODES_NEWLY_CREATED) == 0; |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 430 |
final Configuration config = visitor.getConfiguration(); |
| 431 |
final TypeHierarchy th = config.getTypeHierarchy(); |
| 432 |
ItemType inputType = null; |
| 433 |
|
| 434 |
for (MergeSource mergeSource : mergeSources) { |
| 435 |
ContextItemStaticInfo rowContextItemType = contextInfo; |
| 436 |
if (mergeSource.getForEachItem() != null) { |
| 437 |
mergeSource.forEachItemOp.optimize(visitor, contextInfo); |
| 438 |
rowContextItemType = config.makeContextItemStaticInfo(mergeSource.getForEachItem().getItemType(), false); |
| 439 |
} else if (mergeSource.getForEachSource() != null) { |
| 440 |
mergeSource.forEachStreamOp.optimize(visitor, contextInfo); |
| 441 |
rowContextItemType = config.makeContextItemStaticInfo(NodeKindTest.DOCUMENT, false); |
| 442 |
} |
| 443 |
mergeSource.rowSelectOp.optimize(visitor, rowContextItemType); |
| 444 |
ItemType rowItemType = mergeSource.getRowSelect().getItemType(); |
| 445 |
if (inputType == null) { |
| 446 |
inputType = rowItemType; |
| 447 |
} else { |
| 448 |
inputType = Type.getCommonSuperType(inputType, rowItemType, th); |
| 449 |
} |
| 450 |
|
| 451 |
} |
| 452 |
|
| 453 |
ContextItemStaticInfo cit = config.makeContextItemStaticInfo(inputType, false); |
| 454 |
setAction(getAction().optimize(visitor, cit)); |
| 455 |
|
| 456 |
if (Literal.isEmptySequence(getAction())) { |
| 457 |
return getAction(); |
| 458 |
} |
| 459 |
if (mergeSources.length == 1 && Literal.isEmptySequence(mergeSources[0].getRowSelect())) { |
| 460 |
return mergeSources[0].getRowSelect(); |
| 461 |
} |
| 462 |
|
| 463 |
return this; |
| 464 |
} |
| 465 |
|
| 466 |
@Override |
| 467 |
public void prepareForStreaming() throws XPathException { |
| 468 |
for (MergeSource mergeSource : mergeSources) { |
| 469 |
mergeSource.prepareForStreaming(); |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
private void checkMergeAtt(SortKeyDefinition[] sortKeyDefs) throws XPathException { |
| 481 |
for (int i = 1; i < sortKeyDefs.length; i++) { |
| 482 |
if (!sortKeyDefs[0].isEqual(sortKeyDefs[i])) { |
| 483 |
throw new XPathException("Corresponding xsl:merge-key attributes in different xsl:merge-source elements " + |
| 484 |
"do not have the same effective values", "XTDE2210"); |
| 485 |
} |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
private LastPositionFinder getLastPositionFinder(final XPathContext context) { |
| 500 |
return new LastPositionFinder() { |
| 501 |
private int last = -1; |
| 502 |
|
| 503 |
@Override |
| 504 |
public int getLength() throws XPathException { |
| 505 |
if (last >= 0) { |
| 506 |
return last; |
| 507 |
} else { |
| 508 |
AtomicComparer[] comps = getComparators(context); |
| 509 |
|
| 510 |
GroupIterator mgi = context.getCurrentMergeGroupIterator(); |
| 511 |
final XPathContextMajor c1 = context.newContext(); |
| 512 |
c1.setCurrentMergeGroupIterator(mgi); |
| 513 |
SequenceIterator inputIterator = getMergedInputIterator(context, comps, c1); |
| 514 |
|
| 515 |
|
| 516 |
inputIterator = new MergeGroupingIterator(inputIterator, getComparer(mergeSources[0].mergeKeyDefinitions, comps), null); |
| 517 |
|
| 518 |
return last = Count.steppingCount(inputIterator); |
| 519 |
} |
| 520 |
} |
| 521 |
}; |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
public SequenceIterator<?> iterate(XPathContext context) throws XPathException { |
| 527 |
|
| 528 |
try { |
| 529 |
AtomicComparer[] comps = getComparators(context); |
| 530 |
|
| 531 |
GroupIterator mgi = context.getCurrentMergeGroupIterator(); |
| 532 |
final XPathContextMajor c1 = context.newContext(); |
| 533 |
c1.setCurrentMergeGroupIterator(mgi); |
| 534 |
SequenceIterator<?> inputIterator = getMergedInputIterator(context, comps, c1); |
| 535 |
|
| 536 |
|
| 537 |
inputIterator = new MergeGroupingIterator(inputIterator, getComparer(mergeSources[0].mergeKeyDefinitions, comps), getLastPositionFinder(context)); |
| 538 |
|
| 539 |
|
| 540 |
c1.setCurrentMergeGroupIterator((GroupIterator) inputIterator); |
| 541 |
XPathContext c3 = c1.newMinorContext(); |
| 542 |
c3.trackFocus(inputIterator); |
| 543 |
return new ContextMappingIterator<>(cxt -> getAction().iterate(cxt), c3); |
| 544 |
} catch (XPathException e) { |
| 545 |
e.maybeSetLocation(getLocation()); |
| 546 |
throw e; |
| 547 |
} |
| 548 |
} |
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
private SequenceIterator<?> getMergedInputIterator(XPathContext context, AtomicComparer[] comps, final XPathContextMajor c1) throws XPathException { |
| 561 |
|
| 562 |
|
| 563 |
SequenceIterator<?> inputIterator = EmptyIterator.getInstance(); |
| 564 |
for (final MergeSource ms : mergeSources) { |
| 565 |
|
| 566 |
SequenceIterator<?> anchorsIter = null; |
| 567 |
|
| 568 |
if (ms.streamable && ms.getForEachSource() != null) { |
| 569 |
|
| 570 |
List<ContextMappingIterator<ExternalObject<ItemWithMergeKeys>>> msIters = |
| 571 |
MergeInstrAdjunct.makeSourceIterators(ms, getPackageData(), c1); |
| 572 |
for (ContextMappingIterator<ExternalObject<ItemWithMergeKeys>> iter : msIters) { |
| 573 |
inputIterator = makeMergeIterator(inputIterator, comps, ms, iter); |
| 574 |
} |
| 575 |
|
| 576 |
} else if (ms.getForEachSource() != null) { |
| 577 |
final ParseOptions options = new ParseOptions(context.getConfiguration().getParseOptions()); |
| 578 |
options.setSchemaValidationMode(ms.validation); |
| 579 |
options.setTopLevelType(ms.schemaType); |
| 580 |
options.setApplicableAccumulators(ms.accumulators); |
| 581 |
SequenceIterator<?> uriIter = ms.getForEachSource().iterate(c1); |
| 582 |
XsltController controller = (XsltController)context.getController(); |
| 583 |
final AccumulatorManager accumulatorManager = controller.getAccumulatorManager(); |
| 584 |
anchorsIter = new ItemMappingIterator<>(uriIter, baseItem -> { |
| 585 |
String uri = baseItem.getStringValue(); |
| 586 |
NodeInfo node = DocumentFn.makeDoc(uri, getRetainedStaticContext().getStaticBaseUriString(), |
| 587 |
getPackageData(), options, c1, getLocation(), true); |
| 588 |
accumulatorManager.setApplicableAccumulators(node.getTreeInfo(), ms.accumulators); |
| 589 |
return node; |
| 590 |
}); |
| 591 |
XPathContext c2 = c1.newMinorContext(); |
| 592 |
FocusIterator anchorsIterFocus = c2.trackFocus(anchorsIter); |
| 593 |
while (anchorsIterFocus.next() != null) { |
| 594 |
XPathContext c4 = c2.newMinorContext(); |
| 595 |
FocusIterator rowIntr = c4.trackFocus(ms.getRowSelect().iterate(c2)); |
| 596 |
MergeKeyMappingFunction addMergeKeys = new MergeKeyMappingFunction(c4, ms); |
| 597 |
ContextMappingIterator<ExternalObject<ItemWithMergeKeys>> contextMapKeysItr = |
| 598 |
new ContextMappingIterator<>(addMergeKeys, c4); |
| 599 |
inputIterator = makeMergeIterator(inputIterator, comps, ms, contextMapKeysItr); |
| 600 |
} |
| 601 |
} else if (ms.getForEachItem() != null) { |
| 602 |
anchorsIter = ms.getForEachItem().iterate(c1); |
| 603 |
XPathContext c2 = c1.newMinorContext(); |
| 604 |
FocusIterator anchorsIterFocus = c2.trackFocus(anchorsIter); |
| 605 |
while (anchorsIterFocus.next() != null) { |
| 606 |
inputIterator = getInputIterator(comps, inputIterator, ms, c2); |
| 607 |
} |
| 608 |
} else { |
| 609 |
inputIterator = getInputIterator(comps, inputIterator, ms, c1); |
| 610 |
|
| 611 |
} |
| 612 |
|
| 613 |
} |
| 614 |
return inputIterator; |
| 615 |
} |
| 616 |
|
| 617 |
private SequenceIterator getInputIterator(AtomicComparer[] comps, SequenceIterator inputIterator, MergeSource ms, XPathContext c2) throws XPathException { |
| 618 |
XPathContext c4 = c2.newMinorContext(); |
| 619 |
c4.setTemporaryOutputState(StandardNames.XSL_MERGE_KEY); |
| 620 |
FocusIterator<?> rowIntr = c4.trackFocus(ms.getRowSelect().iterate(c2)); |
| 621 |
MergeKeyMappingFunction addMergeKeys = new MergeKeyMappingFunction(c4, ms); |
| 622 |
ContextMappingIterator<ExternalObject<ItemWithMergeKeys>> contextMapKeysItr = |
| 623 |
new ContextMappingIterator<>(addMergeKeys, c4); |
| 624 |
inputIterator = makeMergeIterator(inputIterator, comps, ms, contextMapKeysItr); |
| 625 |
return inputIterator; |
| 626 |
} |
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
private AtomicComparer[] getComparators(XPathContext context) throws XPathException { |
| 638 |
|
| 639 |
|
| 640 |
AtomicComparer[] comps = comparators; |
| 641 |
if (comparators == null) { |
| 642 |
SortKeyDefinition[] tempSKeys = new SortKeyDefinition[mergeSources.length]; |
| 643 |
|
| 644 |
for (int i = 0; i < mergeSources[0].mergeKeyDefinitions.size(); i++) { |
| 645 |
for (int j = 0; j < mergeSources.length; j++) { |
| 646 |
tempSKeys[j] = mergeSources[j].mergeKeyDefinitions.getSortKeyDefinition(i).fix(context); |
| 647 |
} |
| 648 |
checkMergeAtt(tempSKeys); |
| 649 |
} |
| 650 |
|
| 651 |
comps = new AtomicComparer[mergeSources[0].mergeKeyDefinitions.size()]; |
| 652 |
for (int s = 0; s < mergeSources[0].mergeKeyDefinitions.size(); s++) { |
| 653 |
AtomicComparer comp = mergeSources[0].mergeKeyDefinitions.getSortKeyDefinition(s).getFinalComparator(); |
| 654 |
if (comp == null) { |
| 655 |
comp = mergeSources[0].mergeKeyDefinitions.getSortKeyDefinition(s).makeComparator(context); |
| 656 |
} |
| 657 |
comps[s] = comp; |
| 658 |
} |
| 659 |
} |
| 660 |
return comps; |
| 661 |
} |
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
private SequenceIterator makeMergeIterator( |
| 674 |
SequenceIterator result, AtomicComparer[] comps, MergeSource ms, |
| 675 |
ContextMappingIterator<ExternalObject<ItemWithMergeKeys>> contextMapKeysItr) throws XPathException { |
| 676 |
if (result == null || result instanceof EmptyIterator) { |
| 677 |
result = contextMapKeysItr; |
| 678 |
} else { |
| 679 |
result = new MergeIterator(result, contextMapKeysItr, getComparer(ms.mergeKeyDefinitions, comps)); |
| 680 |
} |
| 681 |
return result; |
| 682 |
} |
| 683 |
|
| 684 |
|
| 685 |
private final static OperandRole ROW_SELECT = |
| 686 |
new OperandRole(OperandRole.USES_NEW_FOCUS | OperandRole.HIGHER_ORDER, OperandUsage.INSPECTION, SequenceType.ANY_SEQUENCE); |
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
@Override |
| 697 |
public Iterable<Operand> operands() { |
| 698 |
List<Operand> list = new ArrayList<>(6); |
| 699 |
list.add(actionOp); |
| 700 |
if (mergeSources != null) { |
| 701 |
for (final MergeSource ms : mergeSources) { |
| 702 |
if (ms.forEachItemOp != null) { |
| 703 |
list.add(ms.forEachItemOp); |
| 704 |
} |
| 705 |
if (ms.forEachStreamOp != null) { |
| 706 |
list.add(ms.forEachStreamOp); |
| 707 |
} |
| 708 |
if (ms.rowSelectOp != null) { |
| 709 |
list.add(ms.rowSelectOp); |
| 710 |
} |
| 711 |
list.add(new Operand(this, ms.mergeKeyDefinitions, OperandRole.SINGLE_ATOMIC)); |
| 712 |
} |
| 713 |
} |
| 714 |
return list; |
| 715 |
} |
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
public Expression getGroupingKey() { |
| 725 |
return mergeSources[0].mergeKeyDefinitions.getSortKeyDefinition(0).getSortKey(); |
| 726 |
} |
| 727 |
|
| 728 |
public ItemOrderComparer getComparer(final SortKeyDefinitionList sKeys, final AtomicComparer[] comps) { |
| 729 |
return (a, b) -> { |
| 730 |
ObjectValue aObj = (ObjectValue) a; |
| 731 |
ObjectValue bObj = (ObjectValue) b; |
| 732 |
ItemWithMergeKeys aItem = (ItemWithMergeKeys) aObj.getObject(); |
| 733 |
ItemWithMergeKeys bItem = (ItemWithMergeKeys) bObj.getObject(); |
| 734 |
|
| 735 |
for (int i = 0; i < sKeys.size(); i++) { |
| 736 |
int val; |
| 737 |
try { |
| 738 |
val = comps[i].compareAtomicValues(aItem.sortKeyValues.get(i), bItem.sortKeyValues.get(i)); |
| 739 |
} catch (NoDynamicContextException e) { |
| 740 |
throw new IllegalStateException(e); |
| 741 |
} |
| 742 |
|
| 743 |
if (val != 0) { |
| 744 |
return val; |
| 745 |
} |
| 746 |
} |
| 747 |
return 0; |
| 748 |
}; |
| 749 |
} |
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
public Expression copy(RebindingMap rebindings) { |
| 760 |
MergeInstr newMerge = new MergeInstr(); |
| 761 |
MergeSource[] c2 = new MergeSource[mergeSources.length]; |
| 762 |
Expression a2 = getAction().copy(rebindings); |
| 763 |
for (int c = 0; c < mergeSources.length; c++) { |
| 764 |
c2[c] = mergeSources[c].copyMergeSource(newMerge, rebindings); |
| 765 |
} |
| 766 |
return newMerge.init(c2, a2); |
| 767 |
} |
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
public void export(ExpressionPresenter out) throws XPathException { |
| 775 |
out.startElement("merge", this); |
| 776 |
for (MergeSource mergeSource : mergeSources) { |
| 777 |
out.startSubsidiaryElement("mergeSrc"); |
| 778 |
if (mergeSource.sourceName != null && !mergeSource.sourceName.startsWith("saxon-merge-source-")) { |
| 779 |
out.emitAttribute("name", mergeSource.sourceName); |
| 780 |
} |
| 781 |
if (mergeSource.validation != Validation.SKIP && mergeSource.validation != Validation.BY_TYPE) { |
| 782 |
out.emitAttribute("validation", Validation.toString(mergeSource.validation)); |
| 783 |
} |
| 784 |
if (mergeSource.validation == Validation.BY_TYPE) { |
| 785 |
SchemaType type = mergeSource.schemaType; |
| 786 |
if (type != null) { |
| 787 |
out.emitAttribute("type", type.getStructuredQName()); |
| 788 |
} |
| 789 |
} |
| 790 |
if (mergeSource.accumulators != null && !mergeSource.accumulators.isEmpty()) { |
| 791 |
FastStringBuffer fsb = new FastStringBuffer(256); |
| 792 |
for (Accumulator acc : mergeSource.accumulators) { |
| 793 |
if (!fsb.isEmpty()) { |
| 794 |
fsb.append(" "); |
| 795 |
} |
| 796 |
fsb.append(acc.getAccumulatorName().getEQName()); |
| 797 |
} |
| 798 |
out.emitAttribute("accum", fsb.toString()); |
| 799 |
} |
| 800 |
if (mergeSource.streamable) { |
| 801 |
out.emitAttribute("flags", "s"); |
| 802 |
} |
| 803 |
if (mergeSource.getForEachItem() != null) { |
| 804 |
out.setChildRole("forEachItem"); |
| 805 |
mergeSource.getForEachItem().export(out); |
| 806 |
} |
| 807 |
if (mergeSource.getForEachSource() != null) { |
| 808 |
out.setChildRole("forEachStream"); |
| 809 |
mergeSource.getForEachSource().export(out); |
| 810 |
} |
| 811 |
out.setChildRole("selectRows"); |
| 812 |
mergeSource.getRowSelect().export(out); |
| 813 |
mergeSource.getMergeKeyDefinitionSet().export(out); |
| 814 |
out.endSubsidiaryElement(); |
| 815 |
} |
| 816 |
out.setChildRole("action"); |
| 817 |
getAction().export(out); |
| 818 |
out.endElement(); |
| 819 |
} |
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
@Override |
| 824 |
public TailCall processLeavingTail(XPathContext context) |
| 825 |
throws XPathException { |
| 826 |
|
| 827 |
Receiver out = context.getReceiver(); |
| 828 |
try (SequenceIterator<?> iter = iterate(context)) { |
| 829 |
iter.forEachOrFail(it -> out.append(it, getLocation(), ReceiverOptions.ALL_NAMESPACES)); |
| 830 |
} catch (XPathException e) { |
| 831 |
e.maybeSetLocation(getLocation()); |
| 832 |
e.maybeSetContext(context); |
| 833 |
throw e; |
| 834 |
} |
| 835 |
return null; |
| 836 |
} |
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
@Override |
| 845 |
public String getStreamerName() { |
| 846 |
return "MergeInstr"; |
| 847 |
} |
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
public static class MergeKeyMappingFunction implements ContextMappingFunction<ExternalObject<ItemWithMergeKeys>> { |
| 855 |
private MergeSource ms; |
| 856 |
private XPathContext baseContext; |
| 857 |
private XPathContext keyContext; |
| 858 |
private ManualIterator<Item<?>> manualIterator; |
| 859 |
|
| 860 |
public MergeKeyMappingFunction(XPathContext baseContext, MergeSource ms) { |
| 861 |
this.baseContext = baseContext; |
| 862 |
this.ms = ms; |
| 863 |
keyContext = baseContext.newMinorContext(); |
| 864 |
keyContext.setTemporaryOutputState(StandardNames.XSL_MERGE_KEY); |
| 865 |
|
| 866 |
manualIterator = new ManualIterator<>(); |
| 867 |
manualIterator.setPosition(1); |
| 868 |
keyContext.setCurrentIterator(manualIterator); |
| 869 |
} |
| 870 |
public SequenceIterator<ExternalObject<ItemWithMergeKeys>> map(XPathContext context) |
| 871 |
throws XPathException { |
| 872 |
Item<?> currentItem = context.getContextItem(); |
| 873 |
manualIterator.setContextItem(currentItem); |
| 874 |
ItemWithMergeKeys newItem = new ItemWithMergeKeys(currentItem, ms.mergeKeyDefinitions, ms.sourceName, keyContext); |
| 875 |
return SingletonIterator.makeIterator(new ObjectValue<>(newItem)); |
| 876 |
|
| 877 |
}; |
| 878 |
} |
| 879 |
} |
| 880 |
|