Debian Codegraph @

net/sf/saxon/expr/instruct/Block.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.expr.instruct;
9
10 import net.sf.saxon.expr.*;
11 import net.sf.saxon.expr.parser.*;
12 import net.sf.saxon.om.*;
13 import net.sf.saxon.pattern.NodeKindTest;
14 import net.sf.saxon.trace.ExpressionPresenter;
15 import net.sf.saxon.trans.XPathException;
16 import net.sf.saxon.tree.iter.EmptyIterator;
17 import net.sf.saxon.tree.iter.ListIterator;
18 import net.sf.saxon.type.*;
19 import net.sf.saxon.value.*;
20
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.List;
24
25
26 /**
27 * An expression that delivers the concatenation of the results of its subexpressions. This may
28 * represent an XSLT sequence constructor, or an XPath/XQuery expression of the form (a,b,c,d).
29 */
30
31 public class Block extends Instruction {
32
33 // TODO: allow the last expression in a Block to be a tail-call of a function, at least in push mode
34
35 private final Operand[] operanda;
36 private boolean allNodesUntyped;
37
38 /**
39 * Create a block, supplying its child expressions
40 * @param children the child expressions in order
41 */
42
43 public Block(Expression[] children) {
44 operanda = new Operand[children.length];
45 for (int i=0; i<children.length; i++) {
46 operanda[i] = new Operand(this, children[i], OperandRole.SAME_FOCUS_ACTION);
47 }
48 for (Expression e : children) {
49 adoptChildExpression(e);
50 }
51 }
52
53 @Override
54 public boolean isInstruction() {
55 return false;
56 }
57
58 /**
59 * Get the n'th child expression
60 * @param n the position of the child expression required (zero-based)
61 * @return the child expression at that position
62 */
63
64 private Expression child(int n) {
65 return operanda[n].getChildExpression();
66 }
67
68 /**
69 * Set the n'th child expression
70 * @param n the position of the child expression to be modified (zero-based)
71 * @param child the child expression at that position
72 */
73
74 private void setChild(int n, Expression child) {
75 operanda[n].setChildExpression(child);
76 }
77
78 /**
79 * Get the number of children
80 * @return the number of child subexpressions
81 */
82
83 private int size() {
84 return operanda.length;
85 }
86
87 @Override
88 public Iterable<Operand> operands() {
89 return Arrays.asList(operanda);
90 }
91
92 /**
93 * Ask whether this expression is, or contains, the binding of a given variable
94 *
95 * @param binding the variable binding
96 * @return true if this expression is the variable binding (for example a ForExpression
97 * or LetExpression) or if it is a FLWOR expression that binds the variable in one of its
98 * clauses.
99 */
100 @Override
101 public boolean hasVariableBinding(Binding binding) {
102 if (binding instanceof LocalParam) {
103 for (Operand o : operanda) {
104 if (o.getChildExpression() == binding) {
105 return true;
106 }
107 }
108 }
109 return false;
110 }
111
112 /**
113 * Static factory method to create a block. If one of the arguments is already a block,
114 * the contents will be merged into a new composite block
115 *
116 * @param e1 the first subexpression (child) of the block
117 * @param e2 the second subexpression (child) of the block
118 * @return a Block containing the two subexpressions, and if either of them is a block, it will
119 * have been collapsed to create a flattened sequence
120 */
121
122 public static Expression makeBlock(/*@Nullable*/ Expression e1, Expression e2) {
123 if (e1 == null || Literal.isEmptySequence(e1)) {
124 return e2;
125 }
126 if (e2 == null || Literal.isEmptySequence(e2)) {
127 return e1;
128 }
129 if (e1 instanceof Block || e2 instanceof Block) {
130 List<Expression> list = new ArrayList<>(10);
131 if (e1 instanceof Block) {
132 for (Operand o : e1.operands()) {
133 list.add(o.getChildExpression());
134 }
135 } else {
136 list.add(e1);
137 }
138 if (e2 instanceof Block) {
139 for (Operand o : e2.operands()) {
140 list.add(o.getChildExpression());
141 }
142 } else {
143 list.add(e2);
144 }
145
146 Expression[] exps = new Expression[list.size()];
147 exps = list.toArray(exps);
148 return new Block(exps);
149
150 } else {
151 Expression[] exps = {e1, e2};
152 return new Block(exps);
153
154 }
155 }
156
157 /**
158 * Static factory method to create a block from a list of expressions
159 *
160 * @param list the list of expressions making up this block. The members of the List must
161 * be instances of Expression. The list is effectively copied; subsequent changes
162 * to the contents of the list have no effect.
163 * @return a Block containing the two subexpressions, and if either of them is a block, it will
164 * have been collapsed to create a flattened sequence
165 */
166
167 public static Expression makeBlock(List<Expression> list) {
168 if (list.isEmpty()) {
169 return Literal.makeEmptySequence();
170 } else if (list.size() == 1) {
171 return list.get(0);
172 } else {
173 Expression[] exps = new Expression[list.size()];
174 exps = list.toArray(exps);
175 return new Block(exps);
176 }
177 }
178
179 public String getExpressionName() {
180 return "sequence";
181 }
182
183 /**
184 * Get the children of this instruction
185 *
186 * @return the children of this instruction, as an array of Operand objects. May return
187 * a zero-length array if there are no children.
188 */
189
190 public Operand[] getOperanda() {
191 return operanda;
192 }
193
194
195 public int computeSpecialProperties() {
196 if (size() == 0) {
197 // An empty sequence has all special properties except "has side effects".
198 return StaticProperty.SPECIAL_PROPERTY_MASK & ~StaticProperty.HAS_SIDE_EFFECTS;
199 }
200 int p = super.computeSpecialProperties();
201 if (allNodesUntyped) {
202 p |= StaticProperty.ALL_NODES_UNTYPED;
203 }
204 // if all the expressions are axis expressions, we have a same-document node-set
205 boolean allAxisExpressions = true;
206 boolean allChildAxis = true;
207 boolean allSubtreeAxis = true;
208 for (Operand o : operands()) {
209 Expression child = o.getChildExpression();
210 if (!(child instanceof AxisExpression)) {
211 allAxisExpressions = false;
212 allChildAxis = false;
213 allSubtreeAxis = false;
214 break;
215 }
216 byte axis = ((AxisExpression) child).getAxis();
217 if (axis != AxisInfo.CHILD) {
218 allChildAxis = false;
219 }
220 if (!AxisInfo.isSubtreeAxis[axis]) {
221 allSubtreeAxis = false;
222 }
223 }
224 if (allAxisExpressions) {
225 p |= StaticProperty.CONTEXT_DOCUMENT_NODESET |
226 StaticProperty.SINGLE_DOCUMENT_NODESET |
227 StaticProperty.NO_NODES_NEWLY_CREATED;
228 // if they all use the child axis, then we have a peer node-set
229 if (allChildAxis) {
230 p |= StaticProperty.PEER_NODESET;
231 }
232 if (allSubtreeAxis) {
233 p |= StaticProperty.SUBTREE_NODESET;
234 }
235 // special case: selecting attributes then children, node-set is sorted
236 if (size() == 2 &&
237 ((AxisExpression) child(0)).getAxis() == AxisInfo.ATTRIBUTE &&
238 ((AxisExpression) child(1)).getAxis() == AxisInfo.CHILD) {
239 p |= StaticProperty.ORDERED_NODESET;
240 }
241 }
242 return p;
243 }
244
245 public boolean implementsStaticTypeCheck() {
246 return true;
247 }
248
249 /**
250 * Static type checking for let expressions is delegated to the child expressions,
251 * to allow further delegation to the branches of a conditional
252 *
253 * @param req the required type
254 * @param backwardsCompatible true if backwards compatibility mode applies
255 * @param role the role of the expression in relation to the required type
256 * @param visitor an expression visitor
257 * @return the expression after type checking (perhaps augmented with dynamic type checking code)
258 * @throws XPathException if failures occur, for example if the static type of one branch of the conditional
259 * is incompatible with the required type
260 */
261
262 public Expression staticTypeCheck(SequenceType req,
263 boolean backwardsCompatible,
264 RoleDiagnostic role, ExpressionVisitor visitor)
265 throws XPathException {
266
267 TypeChecker tc = visitor.getConfiguration().getTypeChecker(backwardsCompatible);
268 if (backwardsCompatible && !Cardinality.allowsMany(req.getCardinality())) {
269 Expression first = FirstItemExpression.makeFirstItemExpression(this);
270 return tc.staticTypeCheck(first, req, role, visitor);
271 }
272 Expression[] checked = new Expression[operanda.length];
273 SequenceType subReq = req;
274 if (req.getCardinality() != StaticProperty.ALLOWS_ZERO_OR_MORE) {
275 subReq = SequenceType.makeSequenceType(req.getPrimaryType(), StaticProperty.ALLOWS_ZERO_OR_MORE);
276 }
277 for (int i=0; i<operanda.length; i++) {
278 checked[i] = tc.staticTypeCheck(operanda[i].getChildExpression(), subReq, role, visitor);
279 }
280 Block b2 = new Block(checked);
281 ExpressionTool.copyLocationInfo(this, b2);
282 b2.allNodesUntyped = allNodesUntyped;
283
284 int reqCard = req.getCardinality();
285 int suppliedCard = b2.getCardinality();
286 if (!Cardinality.subsumes(req.getCardinality(), suppliedCard)) {
287 if ((reqCard & suppliedCard) == 0) {
288 XPathException err = new XPathException(
289 "The required cardinality of the " + role.getMessage() +
290 " is " + Cardinality.toString(reqCard) +
291 ", but the supplied cardinality is " + Cardinality.toString(suppliedCard), role.getErrorCode(), getLocation());
292 err.setIsTypeError(true);
293 err.setFailingExpression(this);
294 throw err;
295 } else {
296 return CardinalityChecker.makeCardinalityChecker(b2, reqCard, role);
297 }
298 }
299 return b2;
300 }
301
302
303 /**
304 * Determine whether the block includes any instructions that might return nodes with a type annotation
305 * @param insn the instruction (for example this block)
306 * @param th the type hierarchy cache
307 * @return true if any expression in the block can return type-annotated nodes
308 */
309
310 public static boolean neverReturnsTypedNodes(Instruction insn, TypeHierarchy th) {
311 for (Operand o : insn.operands()) {
312 Expression exp = o.getChildExpression();
313 if (!exp.hasSpecialProperty(StaticProperty.ALL_NODES_UNTYPED)) {
314 ItemType it = exp.getItemType();
315 if (th.relationship(it, NodeKindTest.ELEMENT) != TypeHierarchy.DISJOINT ||
316 th.relationship(it, NodeKindTest.ATTRIBUTE) != TypeHierarchy.DISJOINT) {
317 return false;
318 }
319 }
320 }
321 return true;
322 }
323
324 /**
325 * Merge any adjacent instructions that create literal text nodes
326 *
327 * @return the expression after merging literal text instructions
328 */
329
330 public Expression mergeAdjacentTextInstructions() {
331 boolean[] isLiteralText = new boolean[size()];
332 boolean hasAdjacentTextNodes = false;
333 for (int i = 0; i < size(); i++) {
334 isLiteralText[i] = child(i) instanceof ValueOf &&
335 ((ValueOf) child(i)).getSelect() instanceof StringLiteral &&
336 !((ValueOf) child(i)).isDisableOutputEscaping();
337
338 if (i > 0 && isLiteralText[i] && isLiteralText[i - 1]) {
339 hasAdjacentTextNodes = true;
340 }
341 }
342 if (hasAdjacentTextNodes) {
343 List<Expression> content = new ArrayList<>(size());
344 String pendingText = null;
345 for (int i = 0; i < size(); i++) {
346 if (isLiteralText[i]) {
347 pendingText = (pendingText == null ? "" : pendingText) +
348 ((StringLiteral) ((ValueOf) child(i)).getSelect()).getStringValue();
349 } else {
350 if (pendingText != null) {
351 ValueOf inst = new ValueOf(new StringLiteral(pendingText), false, false);
352 content.add(inst);
353 pendingText = null;
354 }
355 content.add(child(i));
356 }
357 }
358 if (pendingText != null) {
359 ValueOf inst = new ValueOf(new StringLiteral(pendingText), false, false);
360 content.add(inst);
361 }
362 return makeBlock(content);
363 } else {
364 return this;
365 }
366 }
367
368
369 /**
370 * Copy an expression. This makes a deep copy.
371 *
372 * @return the copy of the original expression
373 * @param rebindings Variables that need to be re-bound
374 */
375
376 /*@NotNull*/
377 public Expression copy(RebindingMap rebindings) {
378 Expression[] c2 = new Expression[size()];
379 for (int c = 0; c < size(); c++) {
380 c2[c] = child(c).copy(rebindings);
381 }
382 Block b2 = new Block(c2);
383 for (int c = 0; c < size(); c++) {
384 b2.adoptChildExpression(c2[c]);
385 }
386 b2.allNodesUntyped = allNodesUntyped;
387 ExpressionTool.copyLocationInfo(this, b2);
388 return b2;
389 }
390
391 /**
392 * Determine the data type of the items returned by this expression
393 *
394 * @return the data type
395 */
396
397 /*@NotNull*/
398 public final ItemType getItemType() {
399 if (size() == 0) {
400 return ErrorType.getInstance();
401 }
402 ItemType t1 = null;
403 TypeHierarchy th = getConfiguration().getTypeHierarchy();
404 for (int i = 0; i < size(); i++) {
405 Expression child = child(i);
406 if (!(child instanceof Message)) {
407 ItemType t = child.getItemType();
408 t1 = t1 == null ? t : Type.getCommonSuperType(t1, t, th);
409 if (t1 instanceof AnyItemType) {
410 return t1; // no point going any further
411 }
412 }
413 }
414 return t1 == null ? ErrorType.getInstance() : t1;
415 }
416
417 /**
418 * Get the static type of the expression as a UType, following precisely the type
419 * inference rules defined in the XSLT 3.0 specification.
420 *
421 * @return the static item type of the expression according to the XSLT 3.0 defined rules
422 * @param contextItemType the static type of the context item
423 */
424 @Override
425 public UType getStaticUType(UType contextItemType) {
426 if (isInstruction()) {
427 return super.getStaticUType(contextItemType);
428 } else {
429 if (size() == 0) {
430 return UType.VOID;
431 }
432 UType t1 = child(0).getStaticUType(contextItemType);
433 for (int i = 1; i < size(); i++) {
434 t1 = t1.union(child(i).getStaticUType(contextItemType));
435 if (t1 == UType.ANY) {
436 return t1; // no point going any further
437 }
438 }
439 return t1;
440 }
441 }
442
443 /**
444 * Determine the cardinality of the expression
445 */
446
447 public int computeCardinality() {
448 if (size() == 0) {
449 return StaticProperty.EMPTY;
450 }
451 int c1 = child(0).getCardinality();
452 for (int i = 1; i < size(); i++) {
453 c1 = Cardinality.sum(c1, child(i).getCardinality());
454 if (c1 == StaticProperty.ALLOWS_MANY) {
455 break;
456 }
457 }
458 return c1;
459 }
460
461 /**
462 * Determine whether this instruction creates new nodes.
463 * This implementation returns true if any child instruction
464 * returns true.
465 */
466
467 public final boolean mayCreateNewNodes() {
468 return someOperandCreatesNewNodes();
469 }
470
471
472 /**
473 * Check to ensure that this expression does not contain any updating subexpressions.
474 * This check is overridden for those expressions that permit updating subexpressions.
475 *
476 * @throws net.sf.saxon.trans.XPathException
477 * if the expression has a non-permitted updateing subexpression
478 */
479
480 public void checkForUpdatingSubexpressions() throws XPathException {
481 if (size() < 2) {
482 return;
483 }
484 boolean updating = false;
485 boolean nonUpdating = false;
486 for (Operand o : operands()) {
487 Expression child = o.getChildExpression();
488 if (ExpressionTool.isNotAllowedInUpdatingContext(child)) {
489 if (updating) {
490 XPathException err = new XPathException(
491 "If any subexpression is updating, then all must be updating", "XUST0001");
492 err.setLocation(child.getLocation());
493 throw err;
494 }
495 nonUpdating = true;
496 }
497 if (child.isUpdatingExpression()) {
498 if (nonUpdating) {
499 XPathException err = new XPathException(
500 "If any subexpression is updating, then all must be updating", "XUST0001");
501 err.setLocation(child.getLocation());
502 throw err;
503 }
504 updating = true;
505 }
506 }
507 }
508
509 /**
510 * Determine whether this is a vacuous expression as defined in the XQuery update specification
511 *
512 * @return true if this expression is vacuous
513 */
514
515 public boolean isVacuousExpression() {
516 // true if all subexpressions are vacuous
517 for (Operand o : operands()) {
518 Expression child = o.getChildExpression();
519 if (!child.isVacuousExpression()) {
520 return false;
521 }
522 }
523 return true;
524 }
525
526 /**
527 * Simplify an expression. This performs any static optimization (by rewriting the expression
528 * as a different expression). The default implementation does nothing.
529 *
530 *
531 *
532 * @throws XPathException if an error is discovered during expression
533 * rewriting
534 */
535
536 /*@NotNull*/
537 public Expression simplify() throws XPathException {
538 boolean allAtomic = true;
539 boolean nested = false;
540
541 for (int c = 0; c < size(); c++) {
542 setChild(c, child(c).simplify());
543 if (!Literal.isAtomic(child(c))) {
544 allAtomic = false;
545 }
546 if (child(c) instanceof Block) {
547 nested = true;
548 } else if (Literal.isEmptySequence(child(c))) {
549 nested = true;
550 }
551 }
552 if (size() == 1) {
553 Expression e = getOperanda()[0].getChildExpression();
554 e.setParentExpression(getParentExpression());
555 return e;
556 } else if (size() == 0) {
557 Expression result = Literal.makeEmptySequence();
558 ExpressionTool.copyLocationInfo(this, result);
559 result.setParentExpression(getParentExpression());
560 return result;
561 } else if (nested) {
562 List<Expression> list = new ArrayList<>(size() * 2);
563 flatten(list);
564 Expression[] children = new Expression[list.size()];
565 for (int i = 0; i < list.size(); i++) {
566 children[i] = list.get(i);
567 }
568 Block newBlock = new Block(children);
569 ExpressionTool.copyLocationInfo(this, newBlock);
570 return newBlock.simplify();
571 } else if (allAtomic) {
572 AtomicValue[] values = new AtomicValue[size()];
573 for (int c = 0; c < size(); c++) {
574 values[c] = (AtomicValue) ((Literal) child(c)).getValue();
575 }
576 Expression result = Literal.makeLiteral(new SequenceExtent<>(values), this);
577 result.setParentExpression(getParentExpression());
578 return result;
579 } else {
580 return this;
581 }
582 }
583
584 /**
585 * Simplify the contents of a Block by merging any nested blocks, merging adjacent
586 * literals, and eliminating any empty sequences.
587 *
588 * @param targetList the new list of expressions comprising the contents of the block
589 * after simplification
590 * @throws XPathException should not happen
591 */
592
593 private void flatten(List<Expression> targetList) throws XPathException {
594 List<Item<?>> currentLiteralList = null;
595 for (Operand o : operands()) {
596 Expression child = o.getChildExpression();
597 if (Literal.isEmptySequence(child)) {
598 // do nothing, omit it from the output
599 } else if (child instanceof Block) {
600 flushCurrentLiteralList(currentLiteralList, targetList);
601 currentLiteralList = null;
602 ((Block) child).flatten(targetList);
603 } else if (child instanceof Literal && !(((Literal) child).getValue() instanceof IntegerRange)) {
604 SequenceIterator<?> iterator = ((Literal) child).getValue().iterate();
605 if (currentLiteralList == null) {
606 currentLiteralList = new ArrayList<>(10);
607 }
608 Item item;
609 while ((item = iterator.next()) != null) {
610 currentLiteralList.add(item);
611 }
612 // no-op
613 } else {
614 flushCurrentLiteralList(currentLiteralList, targetList);
615 currentLiteralList = null;
616 targetList.add(child);
617 }
618 }
619 flushCurrentLiteralList(currentLiteralList, targetList);
620 }
621
622 private void flushCurrentLiteralList(List<Item<?>> currentLiteralList, List<Expression> list) {
623 if (currentLiteralList != null) {
624 ListIterator<Item<?>> iter = new ListIterator<>(currentLiteralList);
625 Literal lit = Literal.makeLiteral(iter.materialize(), this);
626 list.add(lit);
627 }
628 }
629
630 /**
631 * Determine whether the block is a candidate for evaluation using a "shared append expression"
632 * where the result of the evaluation is a sequence implemented as a list of subsequences
633 * @return true if the block is a candidate for "shared append" processing
634 */
635
636 public boolean isCandidateForSharedAppend() {
637 for (Operand o : operands()) {
638 Expression exp = o.getChildExpression();
639 if (exp instanceof VariableReference || exp instanceof Literal) {
640 return true;
641 }
642 }
643 return false;
644 }
645
646 /*@NotNull*/
647 public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
648 typeCheckChildren(visitor, contextInfo);
649 if (neverReturnsTypedNodes(this, visitor.getConfiguration().getTypeHierarchy())) {
650 resetLocalStaticProperties();
651 allNodesUntyped = true;
652 }
653 return this;
654 }
655
656 /*@NotNull*/
657 public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
658 optimizeChildren(visitor, contextInfo);
659 boolean canSimplify = false;
660 boolean prevLiteral = false;
661 // Simplify the expression by collapsing nested blocks and merging adjacent literals
662 for (Operand o : operands()) {
663 Expression child = o.getChildExpression();
664 if (child instanceof Block) {
665 canSimplify = true;
666 break;
667 }
668 if (child instanceof Literal && !(((Literal) child).getValue() instanceof IntegerRange)) {
669 if (prevLiteral || Literal.isEmptySequence(child)) {
670 canSimplify = true;
671 break;
672 }
673 prevLiteral = true;
674 } else {
675 prevLiteral = false;
676 }
677 }
678 if (canSimplify) {
679 List<Expression> list = new ArrayList<>(size() * 2);
680 flatten(list);
681 Expression result = Block.makeBlock(list);
682 result.setRetainedStaticContext(getRetainedStaticContext());
683 return result;
684 }
685 if (size() == 0) {
686 return Literal.makeEmptySequence();
687 } else if (size() == 1) {
688 return child(0);
689 } else {
690 return this;
691 }
692 }
693
694
695 /**
696 * Check that any elements and attributes constructed or returned by this expression are acceptable
697 * in the content model of a given complex type. It's always OK to say yes, since the check will be
698 * repeated at run-time. The process of checking element and attribute constructors against the content
699 * model of a complex type also registers the type of content expected of those constructors, so the
700 * static validation can continue recursively.
701 */
702
703 public void checkPermittedContents(SchemaType parentType, boolean whole) throws XPathException {
704 for (Operand o : operands()) {
705 Expression child = o.getChildExpression();
706 child.checkPermittedContents(parentType, false);
707 }
708 }
709
710 /**
711 * Diagnostic print of expression structure. The abstract expression tree
712 * is written to the supplied output destination.
713 */
714
715 public void export(ExpressionPresenter out) throws XPathException {
716 out.startElement("sequence", this);
717 for (Operand o : operands()) {
718 Expression child = o.getChildExpression();
719 child.export(out);
720 }
721 out.endElement();
722 }
723
724 @Override
725 public String toShortString() {
726 return "(" + child(0).toShortString() + ", ...)";
727 }
728
729 public TailCall processLeavingTail(XPathContext context) throws XPathException {
730 TailCall tc = null;
731 for (Operand o : operands()) {
732 Expression child = o.getChildExpression();
733 try {
734 if (child instanceof TailCallReturner) {
735 tc = ((TailCallReturner) child).processLeavingTail(context);
736 } else {
737 child.process(context);
738 tc = null;
739 }
740 } catch (XPathException e) {
741 e.maybeSetLocation(child.getLocation());
742 e.maybeSetContext(context);
743 throw e;
744 }
745 }
746 return tc;
747 }
748
749 /**
750 * An implementation of Expression must provide at least one of the methods evaluateItem(), iterate(), or process().
751 * This method indicates which of these methods is provided. This implementation provides both iterate() and
752 * process() methods natively.
753 */
754
755 public int getImplementationMethod() {
756 return ITERATE_METHOD | PROCESS_METHOD;
757 }
758
759 /**
760 * Iterate over the results of all the child expressions
761 */
762
763 /*@NotNull*/
764 public SequenceIterator<?> iterate(XPathContext context) throws XPathException {
765 if (size() == 0) {
766 return EmptyIterator.emptyIterator();
767 } else if (size() == 1) {
768 return child(0).iterate(context);
769 } else {
770 return new BlockIterator(operanda, context);
771 }
772 }
773
774
775 /**
776 * Evaluate an updating expression, adding the results to a Pending Update List.
777 * The default implementation of this method, which is used for non-updating expressions,
778 * throws an UnsupportedOperationException
779 *
780 * @param context the XPath dynamic evaluation context
781 * @param pul the pending update list to which the results should be written
782 */
783
784 public void evaluatePendingUpdates(XPathContext context, PendingUpdateList pul) throws XPathException {
785 for (Operand o : operands()) {
786 Expression child = o.getChildExpression();
787 child.evaluatePendingUpdates(context, pul);
788 }
789 }
790
791 @Override
792 public String getStreamerName() {
793 return "Block";
794 }
795
796 }