Debian Codegraph @

net/sf/saxon/expr/FilterExpression.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;
9
10
11 import net.sf.saxon.Configuration;
12 import net.sf.saxon.expr.instruct.Choose;
13 import net.sf.saxon.expr.parser.*;
14 import net.sf.saxon.functions.PositionAndLast;
15 import net.sf.saxon.functions.SystemFunction;
16 import net.sf.saxon.functions.registry.VendorFunctionSetHE;
17 import net.sf.saxon.lib.Feature;
18 import net.sf.saxon.lib.NamespaceConstant;
19 import net.sf.saxon.om.*;
20 import net.sf.saxon.pattern.*;
21 import net.sf.saxon.trace.ExpressionPresenter;
22 import net.sf.saxon.trans.XPathException;
23 import net.sf.saxon.tree.iter.EmptyIterator;
24 import net.sf.saxon.type.*;
25 import net.sf.saxon.value.*;
26
27 import java.math.BigInteger;
28
29
30 /**
31 * A FilterExpression contains a base expression and a filter predicate, which may be an
32 * integer expression (positional filter), or a boolean expression (qualifier)
33 */
34
35 public final class FilterExpression extends BinaryExpression implements ContextSwitchingExpression {
36
37 private boolean filterIsPositional; // true if the value of the filter might depend on
38 // the context position
39 private boolean filterIsSingletonBoolean; // true if the filter expression always returns a single boolean
40 private boolean filterIsIndependent; // true if the filter expression does not
41 // depend on the context item or position. (It may depend on last()).
42 public static final int FILTERED = 10000;
43
44 public final static OperandRole FILTER_PREDICATE =
45 new OperandRole(OperandRole.USES_NEW_FOCUS | OperandRole.HIGHER_ORDER, OperandUsage.INSPECTION, SequenceType.ANY_SEQUENCE);
46
47
48 /**
49 * Constructor
50 *
51 * @param base The base expression to be filtered.
52 * @param filter An expression defining the filter predicate
53 */
54
55 public FilterExpression(Expression base, Expression filter) {
56 super(base, Token.LSQB, filter);
57 base.setFiltered(true);
58 }
59
60 @Override
61 protected OperandRole getOperandRole(int arg) {
62 return arg == 0 ? OperandRole.SAME_FOCUS_ACTION : FILTER_PREDICATE;
63 }
64
65 public Expression getBase() {
66 return getLhsExpression();
67 }
68
69 public void setBase(Expression base) {
70 setLhsExpression(base);
71 }
72
73 /**
74 * Get the filter expression
75 *
76 * @return the expression acting as the filter predicate
77 */
78
79 public Expression getFilter() {
80 return getRhsExpression();
81 }
82
83
84 public void setFilter(Expression filter) {
85 setRhsExpression(filter);
86 }
87
88 /**
89 * Get a name identifying the kind of expression, in terms meaningful to a user.
90 *
91 * @return a name identifying the kind of expression, in terms meaningful to a user.
92 * The name will always be in the form of a lexical XML QName, and should match the name used
93 * in explain() output displaying the expression.
94 */
95
96 public String getExpressionName() {
97 return "filter";
98 }
99
100 /**
101 * Get the data type of the items returned
102 *
103 * @return an integer representing the data type
104 */
105
106 /*@NotNull*/
107 public ItemType getItemType() {
108 // special case the expression B[. instance of x]
109 if (getFilter() instanceof InstanceOfExpression &&
110 ((InstanceOfExpression) getFilter()).getBaseExpression() instanceof ContextItemExpression) {
111 return ((InstanceOfExpression) getFilter()).getRequiredItemType();
112 }
113 return getBase().getItemType();
114 }
115
116
117 /**
118 * Get the static type of the expression as a UType, following precisely the type
119 * inference rules defined in the XSLT 3.0 specification.
120 *
121 * @param contextItemType the static type of the context item
122 * @return the static item type of the expression according to the XSLT 3.0 defined rules
123 */
124 @Override
125 public UType getStaticUType(UType contextItemType) {
126 return getBase().getStaticUType(contextItemType);
127 }
128
129
130 /**
131 * Get the base expression
132 *
133 * @return the base expression being filtered
134 */
135
136 public Expression getSelectExpression() {
137 return getBase();
138 }
139
140 /**
141 * @return the boolean of filter is positional
142 * @see com.saxonica.ee.bytecode.FilterExpressionCompiler
143 */
144 public boolean isFilterIsPositional() {
145 return filterIsPositional;
146 }
147
148 /**
149 * Get the subexpression that is evaluated in the new context
150 *
151 * @return the subexpression evaluated in the context set by the controlling expression
152 */
153
154 public Expression getActionExpression() {
155 return getFilter();
156 }
157
158
159 /**
160 * Determine if the filter is positional
161 *
162 * @param th the Type Hierarchy (for cached access to type information)
163 * @return true if the value of the filter depends on the position of the item against
164 * which it is evaluated
165 */
166
167 public boolean isPositional(TypeHierarchy th) {
168 return isPositionalFilter(getFilter(), th);
169 }
170
171 /**
172 * Test if the filter always returns a singleton boolean.
173 * <p>This information is available only after typeCheck() has been called.</p>
174 *
175 * @return true if the filter is a simple boolean expression
176 */
177
178 public boolean isSimpleBooleanFilter() {
179 return filterIsSingletonBoolean;
180 }
181
182 /**
183 * Determine whether the filter is independent of the context item and position
184 * <p>This information is available only after typeCheck() has been called.</p>
185 *
186 * @return true if the filter is a numeric value that does not depend on the context item or position
187 */
188
189 public boolean isIndependentFilter() {
190 return filterIsIndependent;
191 }
192
193 /**
194 * Simplify an expression
195 *
196 * @throws XPathException if any failure occurs
197 */
198
199 /*@NotNull*/
200 public Expression simplify() throws XPathException {
201
202 setBase(getBase().simplify());
203 setFilter(getFilter().simplify());
204
205 // ignore the filter if the base expression is an empty sequence
206 if (Literal.isEmptySequence(getBase())) {
207 return getBase();
208 }
209
210 // check whether the filter is a constant true() or false()
211 if (getFilter() instanceof Literal && !(((Literal) getFilter()).getValue() instanceof NumericValue)) {
212 try {
213 if (getFilter().effectiveBooleanValue(new EarlyEvaluationContext(getConfiguration()))) {
214 return getBase();
215 } else {
216 return Literal.makeEmptySequence();
217 }
218 } catch (XPathException e) {
219 e.maybeSetLocation(getLocation());
220 throw e;
221 }
222 }
223
224 // check whether the filter is [last()] (note, [position()=last()] is handled elsewhere)
225
226 if (getFilter().isCallOn(PositionAndLast.Last.class)) {
227 setFilter(new IsLastExpression(true));
228 adoptChildExpression(getFilter());
229 }
230
231 return this;
232
233 }
234
235 /**
236 * Type-check the expression
237 *
238 * @param visitor the expression visitor
239 * @param contextInfo return the expression after type-checking (potentially modified to add run-time
240 * checks and/or conversions)
241 */
242
243 /*@NotNull*/
244 public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
245 Configuration config = visitor.getConfiguration();
246 final TypeHierarchy th = config.getTypeHierarchy();
247 getLhs().typeCheck(visitor, contextInfo);
248 getBase().setFiltered(true);
249 if (Literal.isEmptySequence(getBase())) {
250 return getBase();
251 }
252
253 ContextItemStaticInfo baseItemType = config.makeContextItemStaticInfo(getSelectExpression().getItemType(), false);
254 baseItemType.setContextSettingExpression(getBase());
255 getRhs().typeCheck(visitor, baseItemType);
256
257 // The filter expression usually doesn't need to be sorted
258
259 Expression filter2 = ExpressionTool.unsortedIfHomogeneous(getFilter(), visitor.isOptimizeForStreaming());
260 if (filter2 != getFilter()) {
261 setFilter(filter2);
262 }
263
264 // detect head expressions (E[1]) and treat them specially
265
266 if (Literal.isConstantOne(getFilter())) {
267 Expression fie = FirstItemExpression.makeFirstItemExpression(getBase());
268 ExpressionTool.copyLocationInfo(this, fie);
269 return fie;
270 }
271
272 // determine whether the filter might depend on position
273 filterIsPositional = isPositionalFilter(getFilter(), th);
274
275 // determine whether the filter always evaluates to a single boolean
276 filterIsSingletonBoolean =
277 getFilter().getCardinality() == StaticProperty.EXACTLY_ONE &&
278 getFilter().getItemType().equals(BuiltInAtomicType.BOOLEAN);
279
280 // determine whether the filter expression is independent of the focus
281
282 filterIsIndependent = (getFilter().getDependencies() & StaticProperty.DEPENDS_ON_FOCUS) == 0;
283
284 ExpressionTool.resetStaticProperties(this);
285 return this;
286 }
287
288 /**
289 * Perform optimisation of an expression and its subexpressions.
290 * <p>This method is called after all references to functions and variables have been resolved
291 * to the declaration of the function or variable, and after all type checking has been done.</p>
292 *
293 * @param visitor an expression visitor
294 * @param contextItemType the static type of "." at the point where this expression is invoked.
295 * The parameter is set to null if it is known statically that the context item will be undefined.
296 * If the type of the context item is not known statically, the argument is set to
297 * {@link net.sf.saxon.type.Type#ITEM_TYPE}
298 * @return the original expression, rewritten if appropriate to optimize execution
299 * @throws XPathException if an error is discovered during this phase
300 * (typically a type error)
301 */
302
303 /*@NotNull*/
304 public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException {
305 final Configuration config = visitor.getConfiguration();
306 final Optimizer opt = visitor.obtainOptimizer();
307 final boolean tracing = config.getBooleanProperty(Feature.TRACE_OPTIMIZER_DECISIONS);
308 final TypeHierarchy th = config.getTypeHierarchy();
309
310 getLhs().optimize(visitor, contextItemType);
311 getBase().setFiltered(true);
312
313 ContextItemStaticInfo baseItemType = config.makeContextItemStaticInfo(getSelectExpression().getItemType(), false);
314 baseItemType.setContextSettingExpression(getBase());
315 getRhs().optimize(visitor, baseItemType);
316
317 // The filter expression usually doesn't need to be sorted
318
319 Expression filter2 = ExpressionTool.unsortedIfHomogeneous(getFilter(), visitor.isOptimizeForStreaming());
320 if (filter2 != getFilter()) {
321 setFilter(filter2);
322 }
323
324 // Rewrite child::X[last()] as child::X[empty(following-sibling::X)] - especially useful for patterns
325
326 if (getFilter() instanceof IsLastExpression &&
327 ((IsLastExpression) getFilter()).getCondition() &&
328 getBase() instanceof AxisExpression &&
329 ((AxisExpression) getBase()).getAxis() == AxisInfo.CHILD) {
330 NodeTest test = ((AxisExpression) getBase()).getNodeTest();
331 AxisExpression fs = new AxisExpression(AxisInfo.FOLLOWING_SIBLING, test);
332 setFilter(SystemFunction.makeCall("empty", getRetainedStaticContext(), fs));
333 if (tracing) {
334 Optimizer.trace(config, "Replaced [last()] predicate by test for following-sibling", this);
335 }
336 }
337
338 // if the result of evaluating the filter cannot include numeric values, then we can use
339 // its effective boolean value
340
341 ItemType filterType = getFilter().getItemType();
342 if (!th.isSubType(filterType, BuiltInAtomicType.BOOLEAN)
343 && th.relationship(filterType, NumericType.getInstance()) == TypeHierarchy.DISJOINT) {
344 Expression f = SystemFunction.makeCall("boolean", getRetainedStaticContext(), getFilter());
345 setFilter(f.optimize(visitor, baseItemType));
346 }
347
348 // the filter expression may have been reduced to a constant boolean by previous optimizations
349 if (getFilter() instanceof Literal && ((Literal) getFilter()).getValue() instanceof BooleanValue) {
350 if (((BooleanValue) ((Literal) getFilter()).getValue()).getBooleanValue()) {
351 if (tracing) {
352 opt.trace("Redundant filter removed", getBase());
353 }
354 return getBase();
355 } else {
356 Expression result = Literal.makeEmptySequence();
357 ExpressionTool.copyLocationInfo(this, result);
358 if (tracing) {
359 opt.trace("Filter expression eliminated because predicate is always false", result);
360 }
361 return result;
362 }
363 }
364
365 // determine whether the filter might depend on position
366 filterIsPositional = isPositionalFilter(getFilter(), th);
367 filterIsSingletonBoolean =
368 getFilter().getCardinality() == StaticProperty.EXACTLY_ONE &&
369 getFilter().getItemType().equals(BuiltInAtomicType.BOOLEAN);
370
371 // determine whether the filter is indexable
372 if (!filterIsPositional && !visitor.isOptimizeForStreaming()) {
373 int isIndexable = opt.isIndexableFilter(getFilter());
374
375 // If the filter is indexable consider creating a key, or an indexed filter expression
376 // (This happens in Saxon-EE only)
377 if (isIndexable != 0) {
378 boolean contextIsDoc = contextItemType != null && contextItemType.getItemType() != ErrorType.getInstance() &&
379 th.isSubType(contextItemType.getItemType(), NodeKindTest.DOCUMENT);
380 Expression f = opt.tryIndexedFilter(this, visitor, isIndexable > 0, contextIsDoc);
381 if (f != this) {
382 return f.typeCheck(visitor, contextItemType).optimize(visitor, contextItemType);
383 }
384 }
385 }
386
387 // if the filter is positional, try changing f[a and b] to f[a][b] to increase
388 // the chances of finishing early.
389
390 if (filterIsPositional &&
391 getFilter() instanceof BooleanExpression &&
392 ((BooleanExpression) getFilter()).operator == Token.AND) {
393 BooleanExpression bf = (BooleanExpression) getFilter();
394 if (isExplicitlyPositional(bf.getLhsExpression()) &&
395 !isExplicitlyPositional(bf.getRhsExpression())) {
396 Expression p0 = forceToBoolean(bf.getLhsExpression());
397 Expression p1 = forceToBoolean(bf.getRhsExpression());
398 FilterExpression f1 = new FilterExpression(getBase(), p0);
399 ExpressionTool.copyLocationInfo(this, f1);
400 FilterExpression f2 = new FilterExpression(f1, p1);
401 ExpressionTool.copyLocationInfo(this, f2);
402 if (tracing) {
403 opt.trace("Composite filter replaced by nested filter expressions", f2);
404 }
405 return f2.optimize(visitor, contextItemType);
406 }
407 if (isExplicitlyPositional(bf.getRhsExpression()) &&
408 !isExplicitlyPositional(bf.getLhsExpression())) {
409 Expression p0 = forceToBoolean(bf.getLhsExpression());
410 Expression p1 = forceToBoolean(bf.getRhsExpression());
411 FilterExpression f1 = new FilterExpression(getBase(), p1);
412 ExpressionTool.copyLocationInfo(this, f1);
413 FilterExpression f2 = new FilterExpression(f1, p0);
414 ExpressionTool.copyLocationInfo(this, f2);
415 if (tracing) {
416 opt.trace("Composite filter replaced by nested filter expressions", f2);
417 }
418 return f2.optimize(visitor, contextItemType);
419 }
420 }
421
422 if (getFilter() instanceof IsLastExpression &&
423 ((IsLastExpression) getFilter()).getCondition()) {
424
425 if (getBase() instanceof Literal) {
426 setFilter(Literal.makeLiteral(new Int64Value(((Literal) getBase()).getValue().getLength()), this));
427 } else {
428 return new LastItemExpression(getBase());
429 }
430 }
431
432 Expression subsequence = tryToRewritePositionalFilter(visitor, tracing);
433 if (subsequence != null) {
434 if (tracing) {
435 subsequence.setRetainedStaticContext(getRetainedStaticContext()); // Avoids errors in debug explain
436 opt.trace("Rewrote Filter Expression as:", subsequence);
437 }
438 ExpressionTool.copyLocationInfo(this, subsequence);
439 return subsequence.simplify()
440 .typeCheck(visitor, contextItemType)
441 .optimize(visitor, contextItemType);
442 }
443
444 // If there are two non-positional filters, consider changing their order based on the estimated cost
445 // of evaluation, so we evaluate the cheapest predicates first
446
447 if (!filterIsPositional && getLhsExpression() instanceof FilterExpression &&
448 !((FilterExpression) getLhsExpression()).isFilterIsPositional()) {
449 Expression f2 = opt.reorderPredicates(this, visitor, contextItemType);
450 if (f2 != this) {
451 return f2;
452 }
453 }
454
455 final Sequence<?> sequence = tryEarlyEvaluation(visitor);
456 if (sequence != null) {
457 GroundedValue<?> value = sequence.materialize();
458 return Literal.makeLiteral(value, this);
459 }
460
461 return this;
462
463 }
464
465 /**
466 * Return the estimated cost of evaluating an expression. This is a very crude measure based
467 * on the syntactic form of the expression (we have no knowledge of data values). We take
468 * the cost of evaluating a simple scalar comparison or arithmetic expression as 1 (one),
469 * and we assume that a sequence has length 5. The resulting estimates may be used, for
470 * example, to reorder the predicates in a filter expression so cheaper predicates are
471 * evaluated first.
472 */
473 @Override
474 public int getCost() {
475 return getLhsExpression().getCost() + 5 * getRhsExpression().getCost();
476 }
477
478 /**
479 * An implementation of Expression must provide at least one of the methods evaluateItem(), iterate(), or process().
480 * This method indicates which of these methods is provided directly. The other methods will always be available
481 * indirectly, using an implementation that relies on one of the other methods.
482 *
483 * @return the implementation method, for example {@link #ITERATE_METHOD} or {@link #EVALUATE_METHOD} or
484 * {@link #PROCESS_METHOD}
485 */
486 @Override
487 public int getImplementationMethod() {
488 return ITERATE_METHOD;
489 }
490
491 /**
492 * For an expression that returns an integer or a sequence of integers, get
493 * a lower and upper bound on the values of the integers that may be returned, from
494 * static analysis. The default implementation returns null, meaning "unknown" or
495 * "not applicable". Other implementations return an array of two IntegerValue objects,
496 * representing the lower and upper bounds respectively. The values
497 * UNBOUNDED_LOWER and UNBOUNDED_UPPER are used by convention to indicate that
498 * the value may be arbitrarily large. The values MAX_STRING_LENGTH and MAX_SEQUENCE_LENGTH
499 * are used to indicate values limited by the size of a string or the size of a sequence.
500 *
501 * @return the lower and upper bounds of integer values in the result, or null to indicate
502 * unknown or not applicable.
503 */
504 @Override
505 public IntegerValue[] getIntegerBounds() {
506 return getBase().getIntegerBounds();
507 }
508
509
510 private Sequence<?> tryEarlyEvaluation(ExpressionVisitor visitor) {
511 // Attempt early evaluation of a filter expression if the base sequence is constant and the
512 // filter depends only on the context. (This can't be done if, for example, the predicate uses
513 // local variables, even variables declared within the predicate)
514 try {
515 if (getBase() instanceof Literal &&
516 !ExpressionTool.refersToVariableOrFunction(getFilter()) &&
517 (getFilter().getDependencies() & ~StaticProperty.DEPENDS_ON_FOCUS) == 0) {
518 XPathContext context = visitor.getStaticContext().makeEarlyEvaluationContext();
519 return iterate(context).materialize();
520 }
521 } catch (XPathException e) {
522 // can happen for a variety of reasons, for example the filter references a global parameter,
523 // references the doc() function, etc.
524 return null;
525 }
526 return null;
527 }
528
529 /**
530 * Add a representation of this expression to a PathMap. The PathMap captures a map of the nodes visited
531 * by an expression in a source tree.
532 *
533 * @param pathMap the PathMap to which the expression should be added
534 * @param pathMapNodeSet the PathMapNodeSet to which the paths embodied in this expression should be added
535 * @return the pathMapNode representing the focus established by this expression, in the case where this
536 * expression is the first operand of a path expression or filter expression
537 */
538
539 public PathMap.PathMapNodeSet addToPathMap(PathMap pathMap, PathMap.PathMapNodeSet pathMapNodeSet) {
540 PathMap.PathMapNodeSet target = getBase().addToPathMap(pathMap, pathMapNodeSet);
541 getFilter().addToPathMap(pathMap, target);
542 return target;
543 }
544
545 /**
546 * Construct an expression that obtains the effective boolean value of a given expression,
547 * by wrapping it in a call of the boolean() function
548 *
549 * @param in the given expression
550 * @return an expression that wraps the given expression in a call to the fn:boolean() function
551 */
552
553 private static Expression forceToBoolean(Expression in) {
554 if (in.getItemType().getPrimitiveType() == StandardNames.XS_BOOLEAN) {
555 return in;
556 }
557 return SystemFunction.makeCall("boolean", in.getRetainedStaticContext(), in);
558 }
559
560 /**
561 * Attempt to rewrite a filter expression whose predicate is a test of the form
562 * [position() op expr] as a call on functions such as subsequence, remove, or saxon:itemAt
563 *
564 * @param visitor the current expression visitor
565 * @return the rewritten expression if a rewrite was possible, or null otherwise
566 * @throws XPathException if an error occurs
567 */
568
569 private Expression tryToRewritePositionalFilter(ExpressionVisitor visitor, boolean tracing) throws XPathException {
570 Configuration config = visitor.getConfiguration();
571 TypeHierarchy th = config.getTypeHierarchy();
572 if (getFilter() instanceof Literal) {
573 GroundedValue val = ((Literal) getFilter()).getValue();
574 if (val instanceof NumericValue) {
575 Expression result;
576 int lvalue = ((NumericValue)val).asSubscript();
577 if (lvalue != -1) {
578 if (lvalue == 1) {
579 result = FirstItemExpression.makeFirstItemExpression(getBase());
580 } else {
581 result = new SubscriptExpression(getBase(), getFilter());
582 }
583 } else {
584 result = Literal.makeEmptySequence();
585 }
586 if (tracing) {
587 Optimizer.trace(config, "Rewriting numeric filter expression with constant subscript", result);
588 }
589 return result;
590 } else {
591 Expression result = ExpressionTool.effectiveBooleanValue(val.iterate()) ? getBase() : Literal.makeEmptySequence();
592 if (tracing) {
593 Optimizer.trace(config, "Rewriting boolean filter expression with constant subscript", result);
594 }
595 return result;
596 }
597 }
598 if (th.isSubType(getFilter().getItemType(), NumericType.getInstance()) &&
599 !Cardinality.allowsMany(getFilter().getCardinality()) &&
600 (getFilter().getDependencies() & StaticProperty.DEPENDS_ON_FOCUS) == 0) {
601 Expression result = new SubscriptExpression(getBase(), getFilter());
602 if (tracing) {
603 Optimizer.trace(config, "Rewriting numeric filter expression with focus-independent subscript", result);
604 }
605 return result;
606 }
607 if (getFilter() instanceof ComparisonExpression) {
608 //VendorFunctionLibrary lib = getConfiguration().getVendorFunctionLibrary();
609 Expression lhs = ((ComparisonExpression) getFilter()).getLhsExpression();
610 Expression rhs = ((ComparisonExpression) getFilter()).getRhsExpression();
611 int operator = ((ComparisonExpression) getFilter()).getSingletonOperator();
612 Expression comparand;
613 if (lhs.isCallOn(PositionAndLast.Position.class)
614 && th.isSubType(rhs.getItemType(), NumericType.getInstance())) {
615 comparand = rhs;
616 } else if (rhs.isCallOn(PositionAndLast.Position.class)
617 && th.isSubType(lhs.getItemType(), NumericType.getInstance())) {
618 comparand = lhs;
619 operator = Token.inverse(operator);
620 } else {
621 return null;
622 }
623
624 if (ExpressionTool.dependsOnFocus(comparand)) {
625 return null;
626 }
627
628 int card = comparand.getCardinality();
629 if (Cardinality.allowsMany(card)) {
630 return null;
631 }
632
633 // If the comparand might be an empty sequence, do the base rewrite and then wrap the
634 // rewritten expression EXP in "let $n := comparand if exists($n) then EXP else ()
635 if (Cardinality.allowsZero(card)) {
636 LetExpression let = new LetExpression();
637 let.setRequiredType(SequenceType.makeSequenceType(comparand.getItemType(), card));
638 let.setVariableQName(new StructuredQName("pp", NamespaceConstant.SAXON, "pp" + let.hashCode()));
639 let.setSequence(comparand);
640 comparand = new LocalVariableReference(let);
641 LocalVariableReference existsArg = new LocalVariableReference(let);
642 Expression exists = SystemFunction.makeCall("exists", getRetainedStaticContext(), existsArg);
643 Expression rewrite = tryToRewritePositionalFilterSupport(getBase(), comparand, operator, th);
644 if (rewrite == null) {
645 return this;
646 }
647 Expression choice = Choose.makeConditional(exists, rewrite);
648 let.setAction(choice);
649 return let;
650 } else {
651 return tryToRewritePositionalFilterSupport(getBase(), comparand, operator, th);
652 }
653 } else if (getFilter() instanceof IntegerRangeTest) {
654 // rewrite SEQ[position() = N to M]
655 // => let $n := N return subsequence(SEQ, $n, (M - ($n - 1))
656 // (precise form is optimized for the case where $n is a literal, especially N = 1)
657 Expression val = ((IntegerRangeTest) getFilter()).getValue();
658 if (!val.isCallOn(PositionAndLast.class)) {
659 return null;
660 }
661 Expression min = ((IntegerRangeTest) getFilter()).getMin();
662 Expression max = ((IntegerRangeTest) getFilter()).getMax();
663
664 if (ExpressionTool.dependsOnFocus(min)) {
665 return null;
666 }
667 if (ExpressionTool.dependsOnFocus(max)) {
668 if (max.isCallOn(PositionAndLast.Last.class)) {
669 Expression result = SystemFunction.makeCall("subsequence", getRetainedStaticContext(), getBase(), min);
670 if (tracing) {
671 Optimizer.trace(config, "Rewriting numeric range filter expression using subsequence()", result);
672 }
673 return result;
674 } else {
675 return null;
676 }
677 }
678
679 LetExpression let = new LetExpression();
680 let.setRequiredType(SequenceType.SINGLE_INTEGER);
681 let.setVariableQName(new StructuredQName("nn", NamespaceConstant.SAXON, "nn" + let.hashCode()));
682 let.setSequence(min);
683 min = new LocalVariableReference(let);
684 LocalVariableReference min2 = new LocalVariableReference(let);
685 Expression minMinusOne = new ArithmeticExpression(
686 min2, Token.MINUS, Literal.makeLiteral(Int64Value.makeIntegerValue(1), this));
687 Expression length = new ArithmeticExpression(max, Token.MINUS, minMinusOne);
688 Expression subs = SystemFunction.makeCall("subsequence", getRetainedStaticContext(), getBase(), min, length);
689 let.setAction(subs);
690 if (tracing) {
691 Optimizer.trace(config, "Rewriting numeric range filter expression using subsequence()", subs);
692 }
693 return let;
694
695 } else {
696 return null;
697 }
698 }
699
700 private static Expression tryToRewritePositionalFilterSupport(
701 Expression start, Expression comparand, int operator,
702 TypeHierarchy th)
703 throws XPathException {
704 if (th.isSubType(comparand.getItemType(), BuiltInAtomicType.INTEGER)) {
705 switch (operator) {
706 case Token.FEQ: {
707 if (Literal.isConstantOne(comparand)) {
708 return FirstItemExpression.makeFirstItemExpression(start);
709 } else if (comparand instanceof Literal && ((IntegerValue) ((Literal) comparand).getValue()).asBigInteger().compareTo(BigInteger.ZERO) <= 0) {
710 return Literal.makeEmptySequence();
711 } else {
712 return new SubscriptExpression(start, comparand);
713 }
714 }
715 case Token.FLT: {
716
717 Expression[] args = new Expression[3];
718 args[0] = start;
719 args[1] = Literal.makeLiteral(Int64Value.makeIntegerValue(1), start);
720 if (Literal.isAtomic(comparand)) {
721 long n = ((NumericValue) ((Literal) comparand).getValue()).longValue();
722 args[2] = Literal.makeLiteral(Int64Value.makeIntegerValue(n - 1), start);
723 } else {
724 ArithmeticExpression decrement = new ArithmeticExpression(
725 comparand, Token.MINUS, Literal.makeLiteral(Int64Value.makeIntegerValue(1), start));
726 decrement.setCalculator(Calculator.getCalculator( // bug 2704
727 StandardNames.XS_INTEGER, StandardNames.XS_INTEGER, Calculator.MINUS, true));
728 args[2] = decrement;
729 }
730 return SystemFunction.makeCall("subsequence", start.getRetainedStaticContext(), args);
731 }
732 case Token.FLE: {
733 Expression[] args = new Expression[3];
734 args[0] = start;
735 args[1] = Literal.makeLiteral(Int64Value.makeIntegerValue(1), start);
736 args[2] = comparand;
737 return SystemFunction.makeCall("subsequence", start.getRetainedStaticContext(), args);
738 }
739 case Token.FNE: {
740 return SystemFunction.makeCall("remove", start.getRetainedStaticContext(), start, comparand);
741 }
742 case Token.FGT: {
743 Expression[] args = new Expression[2];
744 args[0] = start;
745 if (Literal.isAtomic(comparand)) {
746 long n = ((NumericValue) ((Literal) comparand).getValue()).longValue();
747 args[1] = Literal.makeLiteral(Int64Value.makeIntegerValue(n + 1), start);
748 } else {
749 args[1] = new ArithmeticExpression(
750 comparand, Token.PLUS, Literal.makeLiteral(Int64Value.makeIntegerValue(1), start));
751 }
752 return SystemFunction.makeCall("subsequence", start.getRetainedStaticContext(), args);
753 }
754 case Token.FGE: {
755 return SystemFunction.makeCall("subsequence", start.getRetainedStaticContext(), start, comparand);
756 }
757 default:
758 throw new IllegalArgumentException("operator");
759 }
760
761 } else {
762 // the comparand is not known statically to be an integer
763 switch (operator) {
764 case Token.FEQ: {
765 return new SubscriptExpression(start, comparand);
766 }
767 case Token.FLT: {
768 // rewrite SEQ[position() lt V] as
769 // let $N := V return subsequence(SEQ, 1, if (is-whole-number($N)) then $N-1 else floor($N)))
770 LetExpression let = new LetExpression();
771 let.setRequiredType(SequenceType.makeSequenceType(
772 comparand.getItemType(), StaticProperty.ALLOWS_ONE));
773 let.setVariableQName(new StructuredQName("pp", NamespaceConstant.SAXON, "pp" + let.hashCode()));
774 let.setSequence(comparand);
775 LocalVariableReference isWholeArg = new LocalVariableReference(let);
776 LocalVariableReference arithArg = new LocalVariableReference(let);
777 LocalVariableReference floorArg = new LocalVariableReference(let);
778 Expression isWhole = VendorFunctionSetHE.getInstance().makeFunction("is-whole-number", 1).makeFunctionCall(isWholeArg);
779 Expression minusOne = new ArithmeticExpression(
780 arithArg, Token.MINUS, Literal.makeLiteral(Int64Value.makeIntegerValue(1), start));
781 Expression floor = SystemFunction.makeCall("floor", start.getRetainedStaticContext(), floorArg);
782 Expression choice = Choose.makeConditional(isWhole, minusOne, floor);
783 Expression subs = SystemFunction.makeCall(
784 "subsequence", start.getRetainedStaticContext(), start, Literal.makeLiteral(Int64Value.makeIntegerValue(1), start), choice);
785 let.setAction(subs);
786 //decl.fixupReferences(let);
787 return let;
788 }
789 case Token.FLE: {
790 Expression floor = SystemFunction.makeCall("floor", start.getRetainedStaticContext(), comparand);
791 return SystemFunction.makeCall(
792 "subsequence", start.getRetainedStaticContext(), start, Literal.makeLiteral(Int64Value.makeIntegerValue(1), start), floor);
793 }
794 case Token.FNE: {
795 // rewrite SEQ[position() ne V] as
796 // let $N := V return remove(SEQ, if (is-whole-number($N)) then xs:integer($N) else 0)
797 LetExpression let = new LetExpression();
798 ExpressionTool.copyLocationInfo(start, let);
799 let.setRequiredType(SequenceType.makeSequenceType(
800 comparand.getItemType(), StaticProperty.ALLOWS_ONE));
801 let.setVariableQName(new StructuredQName("pp", NamespaceConstant.SAXON, "pp" + let.hashCode()));
802 let.setSequence(comparand);
803 LocalVariableReference isWholeArg = new LocalVariableReference(let);
804 LocalVariableReference castArg = new LocalVariableReference(let);
805 Expression isWhole = VendorFunctionSetHE.getInstance().makeFunction("is-whole-number", 1).makeFunctionCall(isWholeArg);
806 ExpressionTool.copyLocationInfo(start, isWhole);
807 Expression cast = new CastExpression(castArg, BuiltInAtomicType.INTEGER, false);
808 ExpressionTool.copyLocationInfo(start, cast);
809 Expression choice = Choose.makeConditional(
810 isWhole, cast, Literal.makeLiteral(Int64Value.makeIntegerValue(0), start));
811 Expression rem = SystemFunction.makeCall("remove", start.getRetainedStaticContext(), start, choice);
812 let.setAction(rem);
813 return let;
814 }
815 case Token.FGT: {
816 // rewrite SEQ[position() gt V] as
817 // let $N := V return subsequence(SEQ, if (is-whole-number($N)) then $N+1 else ceiling($N)))
818 LetExpression let = new LetExpression();
819 let.setRequiredType(SequenceType.makeSequenceType(
820 comparand.getItemType(), StaticProperty.ALLOWS_ONE));
821 let.setVariableQName(new StructuredQName("pp", NamespaceConstant.SAXON, "pp" + let.hashCode()));
822 let.setSequence(comparand);
823 LocalVariableReference isWholeArg = new LocalVariableReference(let);
824 LocalVariableReference arithArg = new LocalVariableReference(let);
825 LocalVariableReference ceilingArg = new LocalVariableReference(let);
826 Expression isWhole = VendorFunctionSetHE.getInstance().makeFunction("is-whole-number", 1).makeFunctionCall(isWholeArg);
827 Expression plusOne = new ArithmeticExpression(
828 arithArg, Token.PLUS, Literal.makeLiteral(Int64Value.makeIntegerValue(1), start));
829 Expression ceiling = SystemFunction.makeCall("ceiling", start.getRetainedStaticContext(), ceilingArg);
830 Expression choice = Choose.makeConditional(isWhole, plusOne, ceiling);
831 Expression subs = SystemFunction.makeCall("subsequence", start.getRetainedStaticContext(), start, choice);
832 let.setAction(subs);
833 return let;
834 }
835 case Token.FGE: {
836 // rewrite SEQ[position() ge V] => subsequence(SEQ, ceiling(V))
837 Expression ceiling = SystemFunction.makeCall("ceiling", start.getRetainedStaticContext(), comparand);
838 return SystemFunction.makeCall("subsequence", start.getRetainedStaticContext(), start, ceiling);
839 }
840 default:
841 throw new IllegalArgumentException("operator");
842 }
843 }
844 }
845
846 /**
847 * Replace this expression by a simpler expression that delivers the results without regard
848 * to order.
849 *
850 * @param retainAllNodes set to true if the result must contain exactly the same nodes as the
851 * original; set to false if the result can eliminate (or introduce) duplicates.
852 * @param forStreaming set to true if optimizing for streaming
853 */
854 @Override
855 public Expression unordered(boolean retainAllNodes, boolean forStreaming) throws XPathException {
856 if (!filterIsPositional) {
857 setBase(getBase().unordered(retainAllNodes, forStreaming));
858 }
859 return this;
860 }
861
862 /**
863 * Rearrange a filter expression so that predicates that are independent of a given
864 * set of range variables come first, allowing them to be promoted along with the base
865 * expression
866 *
867 * @param bindings the given set of range variables
868 * @param opt the Optimizer
869 * @param th the type hierarchy cache
870 * @return the expression after promoting independent predicates
871 */
872
873 private FilterExpression promoteIndependentPredicates(Binding[] bindings, Optimizer opt, TypeHierarchy th) {
874 if (!ExpressionTool.dependsOnVariable(getBase(), bindings)) {
875 return this;
876 }
877 if (isPositional(th)) {
878 return this;
879 }
880 if (getBase() instanceof FilterExpression) {
881 FilterExpression fe = (FilterExpression) getBase();
882 if (fe.isPositional(th)) {
883 return this;
884 }
885 if (!ExpressionTool.dependsOnVariable(fe.getFilter(), bindings)) {
886 return this;
887 }
888 if (!ExpressionTool.dependsOnVariable(getFilter(), bindings)) {
889 FilterExpression result = new FilterExpression(
890 new FilterExpression(fe.getBase(), getFilter()).promoteIndependentPredicates(bindings, opt, th),
891 fe.getFilter());
892 opt.trace("Reordered filter predicates:", result);
893 return result;
894 }
895 }
896 return this;
897 }
898
899 /**
900 * Determine whether an expression, when used as a filter, is potentially positional;
901 * that is, where it either contains a call on position() or last(), or where it is capable of returning
902 * a numeric result.
903 *
904 * @param exp the expression to be examined
905 * @param th the type hierarchy cache
906 * @return true if the expression depends on position() or last() explicitly or implicitly
907 */
908
909 public static boolean isPositionalFilter(Expression exp, TypeHierarchy th) {
910 ItemType type = exp.getItemType();
911 if (type.equals(BuiltInAtomicType.BOOLEAN)) {
912 // common case, get it out of the way quickly
913 return isExplicitlyPositional(exp);
914 }
915 return type.equals(BuiltInAtomicType.ANY_ATOMIC) ||
916 type instanceof AnyItemType ||
917 type.equals(BuiltInAtomicType.INTEGER) ||
918 type.equals(NumericType.getInstance()) ||
919 th.isSubType(type, NumericType.getInstance()) ||
920 isExplicitlyPositional(exp);
921 }
922
923 /**
924 * Determine whether an expression, when used as a filter, has an explicit dependency on position() or last()
925 *
926 * @param exp the expression being tested
927 * @return true if the expression is explicitly positional, that is, if it contains an explicit call on
928 * position() or last()
929 */
930
931 private static boolean isExplicitlyPositional(Expression exp) {
932 return (exp.getDependencies() & (StaticProperty.DEPENDS_ON_POSITION | StaticProperty.DEPENDS_ON_LAST)) != 0;
933 }
934
935
936 /**
937 * Get the static cardinality of this expression
938 *
939 * @return the cardinality. The method attempts to determine the case where the
940 * filter predicate is guaranteed to select at most one item from the sequence being filtered
941 */
942
943 public int computeCardinality() {
944 if (getFilter() instanceof Literal && ((Literal) getFilter()).getValue() instanceof NumericValue) {
945 if (((NumericValue) ((Literal) getFilter()).getValue()).compareTo(1) == 0 &&
946 !Cardinality.allowsZero(getBase().getCardinality())) {
947 return StaticProperty.ALLOWS_ONE;
948 } else {
949 return StaticProperty.ALLOWS_ZERO_OR_ONE;
950 }
951 }
952 if (filterIsIndependent) {
953 ItemType filterType = getFilter().getItemType().getPrimitiveItemType();
954 if (filterType == BuiltInAtomicType.INTEGER || filterType == BuiltInAtomicType.DOUBLE ||
955 filterType == BuiltInAtomicType.DECIMAL || filterType == BuiltInAtomicType.FLOAT) {
956 return StaticProperty.ALLOWS_ZERO_OR_ONE;
957 }
958 if (getFilter() instanceof ArithmeticExpression) {
959 return StaticProperty.ALLOWS_ZERO_OR_ONE;
960 }
961 }
962 if (getFilter() instanceof IsLastExpression && ((IsLastExpression) getFilter()).getCondition()) {
963 return StaticProperty.ALLOWS_ZERO_OR_ONE;
964 }
965 if (!Cardinality.allowsMany(getBase().getCardinality())) {
966 return StaticProperty.ALLOWS_ZERO_OR_ONE;
967 }
968
969 return StaticProperty.ALLOWS_ZERO_OR_MORE;
970 }
971
972 /**
973 * Get the static properties of this expression (other than its type). The result is
974 * bit-significant. These properties are used for optimizations. In general, if
975 * property bit is set, it is true, but if it is unset, the value is unknown.
976 *
977 * @return the static properties of the expression, as a bit-significant value
978 */
979
980 public int computeSpecialProperties() {
981 return getBase().getSpecialProperties();
982 }
983
984 /**
985 * Is this expression the same as another expression?
986 *
987 * @param other the expression to be compared with this one
988 * @return true if the two expressions are statically equivalent
989 */
990
991 public boolean equals(Object other) {
992 if (other instanceof FilterExpression) {
993 FilterExpression f = (FilterExpression) other;
994 return getBase().isEqual(f.getBase()) &&
995 getFilter().isEqual(f.getFilter());
996 }
997 return false;
998 }
999
1000 /**
1001 * get HashCode for comparing two expressions
1002 *
1003 * @return the hash code
1004 */
1005
1006 public int computeHashCode() {
1007 return "FilterExpression".hashCode() + getBase().hashCode() + getFilter().hashCode();
1008 }
1009
1010 /**
1011 * Convert this expression to an equivalent XSLT pattern
1012 *
1013 * @param config the Saxon configuration
1014 * @return the equivalent pattern
1015 * @throws net.sf.saxon.trans.XPathException if conversion is not possible
1016 */
1017 @Override
1018 public Pattern toPattern(Configuration config) throws XPathException {
1019 Expression base = getSelectExpression();
1020 Expression filter = getFilter();
1021 TypeHierarchy th = config.getTypeHierarchy();
1022 Pattern basePattern = base.toPattern(config);
1023 if (!isPositional(th)) {
1024 return new BasePatternWithPredicate(basePattern, filter);
1025 } else if (basePattern instanceof NodeTestPattern &&
1026 basePattern.getItemType() instanceof NodeTest &&
1027 filterIsPositional &&
1028 base instanceof AxisExpression &&
1029 ((AxisExpression) base).getAxis() == AxisInfo.CHILD &&
1030 (filter.getDependencies() & StaticProperty.DEPENDS_ON_LAST) == 0) {
1031 if (filter instanceof Literal && ((Literal) filter).getValue() instanceof IntegerValue) {
1032 return new SimplePositionalPattern((NodeTest) basePattern.getItemType(), (int) ((IntegerValue) ((Literal) filter).getValue()).longValue());
1033 } else {
1034 return new GeneralPositionalPattern((NodeTest) basePattern.getItemType(), filter);
1035 }
1036 }
1037 if (base.getItemType() instanceof NodeTest) {
1038 return new GeneralNodePattern(this, (NodeTest) base.getItemType());
1039 } else {
1040 throw new XPathException("The filtered expression in an XSLT 2.0 pattern must be a simple step");
1041 }
1042 }
1043
1044 /**
1045 * Iterate over the results, returning them in the correct order
1046 *
1047 * @param context the dynamic context for the evaluation
1048 * @return an iterator over the expression results
1049 * @throws XPathException if any dynamic error occurs
1050 */
1051
1052 /*@NotNull*/
1053 public SequenceIterator<?> iterate(XPathContext context) throws XPathException {
1054
1055 // Fast path where the filter value is independent of the focus
1056
1057 if (filterIsIndependent) {
1058 try {
1059 SequenceIterator it = getFilter().iterate(context);
1060 Item first = it.next();
1061 if (first == null) {
1062 return EmptyIterator.emptyIterator();
1063 }
1064 if (first instanceof NumericValue) {
1065 if (it.next() != null) {
1066 ExpressionTool.ebvError("sequence of two or more items starting with a numeric value", getFilter());
1067 } else {
1068 // Filter is a constant number
1069 int pos = ((NumericValue)first).asSubscript();
1070 if (pos != -1) {
1071 if (getBase() instanceof VariableReference) {
1072 Sequence<?> baseVal = ((VariableReference) getBase()).evaluateVariable(context);
1073 if (baseVal instanceof MemoClosure) {
1074 Item<?> m = ((MemoClosure) baseVal).itemAt(pos - 1);
1075 return m == null ? EmptyIterator.emptyIterator() : m.iterate();
1076 } else {
1077 Item<?> m = baseVal.materialize().itemAt(pos - 1);
1078 return m == null ? EmptyIterator.emptyIterator() : m.iterate();
1079 }
1080 } else if (getBase() instanceof Literal) {
1081 Item<?> i = ((Literal) getBase()).getValue().itemAt(pos - 1);
1082 return i == null ? EmptyIterator.emptyIterator() : i.iterate();
1083 } else {
1084 SequenceIterator<?> baseIter = getBase().iterate(context);
1085 return SubsequenceIterator.make(baseIter, pos, pos);
1086 }
1087 }
1088 // a non-integer value or non-positive number will never be equal to position()
1089 return EmptyIterator.emptyIterator();
1090 }
1091 } else {
1092 // Filter is focus-independent, but not numeric: need to use the effective boolean value
1093 boolean ebv = false;
1094 if (first instanceof NodeInfo) {
1095 ebv = true;
1096 } else if (first instanceof BooleanValue) {
1097 ebv = ((BooleanValue) first).getBooleanValue();
1098 if (it.next() != null) {
1099 ExpressionTool.ebvError("sequence of two or more items starting with a boolean value", getFilter());
1100 }
1101 } else if (first instanceof StringValue) {
1102 ebv = !((StringValue) first).isZeroLength();
1103 if (it.next() != null) {
1104 ExpressionTool.ebvError("sequence of two or more items starting with a boolean value", getFilter());
1105 }
1106 } else {
1107 ExpressionTool.ebvError("sequence starting with an atomic value other than a boolean, number, or string", getFilter());
1108 }
1109 if (ebv) {
1110 return getBase().iterate(context);
1111 } else {
1112 return EmptyIterator.emptyIterator();
1113 }
1114 }
1115 } catch (XPathException e) {
1116 e.maybeSetLocation(getLocation());
1117 throw e;
1118 }
1119 }
1120
1121 // get an iterator over the base nodes
1122
1123 SequenceIterator<?> baseIter = getBase().iterate(context);
1124
1125 // quick exit for an empty sequence
1126
1127 if (baseIter instanceof EmptyIterator) {
1128 return baseIter;
1129 }
1130
1131 if (filterIsPositional && !filterIsSingletonBoolean) {
1132 return new FilterIterator<>(baseIter, getFilter(), context);
1133 } else {
1134 return new FilterIterator.NonNumeric<>(baseIter, getFilter(), context);
1135 }
1136
1137 }
1138
1139 /**
1140 * Copy an expression. This makes a deep copy.
1141 *
1142 * @param rebindings a mutable list of (old binding, new binding) pairs
1143 * that is used to update the bindings held in any
1144 * local variable references that are copied.
1145 * @return the copy of the original expression
1146 */
1147
1148 /*@NotNull*/
1149 public Expression copy(RebindingMap rebindings) {
1150 FilterExpression fe = new FilterExpression(getBase().copy(rebindings), getFilter().copy(rebindings));
1151 ExpressionTool.copyLocationInfo(this, fe);
1152 fe.filterIsIndependent = filterIsIndependent;
1153 fe.filterIsPositional = filterIsPositional;
1154 fe.filterIsSingletonBoolean = filterIsSingletonBoolean;
1155 return fe;
1156 }
1157
1158
1159 /**
1160 * Get the (partial) name of a class that supports streaming of this kind of expression
1161 *
1162 * @return the partial name of a class that can be instantiated to provide streaming support in Saxon-EE,
1163 * or null if there is no such class
1164 */
1165 @Override
1166 public String getStreamerName() {
1167 return "FilterExpression";
1168 }
1169
1170 /**
1171 * The toString() method for an expression attempts to give a representation of the expression
1172 * in an XPath-like form, but there is no guarantee that the syntax will actually be true XPath.
1173 * In the case of XSLT instructions, the toString() method gives an abstracted view of the syntax
1174 */
1175
1176 public String toString() {
1177 return ExpressionTool.parenthesize(getBase()) + "[" + getFilter() + "]";
1178 }
1179
1180 @Override
1181 public String toShortString() {
1182 return getBase().toShortString() + "[" + getFilter().toShortString() + "]";
1183 }
1184
1185 /**
1186 * Diagnostic print of expression structure. The abstract expression tree
1187 * is written to the supplied output destination.
1188 *
1189 * @param out the ExpressionPresenter to be used
1190 */
1191
1192 public void export(ExpressionPresenter out) throws XPathException {
1193 out.startElement("filter", this);
1194 String flags = "";
1195 if (filterIsIndependent) {
1196 flags += "i";
1197 }
1198 if (filterIsPositional) {
1199 flags += "p";
1200 }
1201 if (filterIsSingletonBoolean) {
1202 flags += "b";
1203 }
1204 out.emitAttribute("flags", flags);
1205 getBase().export(out);
1206 getFilter().export(out);
1207 out.endElement();
1208 }
1209
1210 public void setFlags(String flags) {
1211 filterIsIndependent = flags.contains("i");
1212 filterIsPositional = flags.contains("p");
1213 filterIsSingletonBoolean = flags.contains("b");
1214 }
1215
1216
1217 }
1218