Debian Codegraph @

net/sf/saxon/expr/FilterIterator.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 import net.sf.saxon.expr.parser.ExpressionTool;
11 import net.sf.saxon.om.FocusIterator;
12 import net.sf.saxon.om.Item;
13 import net.sf.saxon.om.NodeInfo;
14 import net.sf.saxon.om.SequenceIterator;
15 import net.sf.saxon.trans.XPathException;
16 import net.sf.saxon.value.*;
17
18 /**
19 * A FilterIterator filters an input sequence using a filter expression. Note that a FilterIterator
20 * is not used where the filter is a constant number (PositionFilter is used for this purpose instead),
21 * so this class does no optimizations for numeric predicates.
22 */
23
24 public class FilterIterator<T extends Item<?>> implements SequenceIterator<T> {
25
26 protected FocusIterator<T> base;
27 protected Expression filter;
28 protected XPathContext filterContext;
29
30 /**
31 * Constructor
32 *
33 * @param base An iteration of the items to be filtered
34 * @param filter The expression defining the filter predicate
35 * @param context The context in which the expression is being evaluated
36 */
37
38 public FilterIterator(SequenceIterator<T> base, Expression filter,
39 XPathContext context) {
40 this.filter = filter;
41 filterContext = context.newMinorContext();
42 this.base = filterContext.trackFocus(base);
43 }
44
45 /**
46 * Set the base iterator
47 *
48 * @param base the iterator over the sequence to be filtered
49 * @param context the context in which the (outer) filter expression is evaluated
50 */
51
52 public void setSequence(SequenceIterator<?> base, XPathContext context) {
53 filterContext = context.newMinorContext();
54 this.base = (FocusIterator<T>)filterContext.trackFocus(base);
55 }
56
57 /**
58 * Get the next item if there is one
59 */
60
61 public T next() throws XPathException {
62 return getNextMatchingItem();
63 }
64
65 /**
66 * Get the next item in the base sequence that matches the filter predicate
67 * if there is such an item, or null if not.
68 *
69 * @return the next item that matches the predicate
70 * @throws XPathException if a dynamic error occurs
71 */
72
73 protected T getNextMatchingItem() throws XPathException {
74 T next;
75 while ((next = base.next()) != null) {
76 if (matches()) {
77 return next;
78 }
79 }
80 return null;
81 }
82
83 /**
84 * Determine whether the context item matches the filter predicate
85 *
86 * @return true if the context item matches
87 * @throws XPathException if an error occurs evaluating the match
88 */
89
90 protected boolean matches() throws XPathException {
91
92 // This code is carefully designed to avoid reading more items from the
93 // iteration of the filter expression than are absolutely essential.
94
95 // The code is almost identical to the code in ExpressionTool#effectiveBooleanValue
96 // except for the handling of a numeric result
97
98 SequenceIterator iterator = filter.iterate(filterContext);
99 return testPredicateValue(iterator, base.position(), filter);
100 }
101
102 public static boolean testPredicateValue(SequenceIterator iterator, long position, Expression filter) throws XPathException {
103 Item first = iterator.next();
104 if (first == null) {
105 return false;
106 }
107 if (first instanceof NodeInfo) {
108 return true;
109 } else {
110 if (first instanceof BooleanValue) {
111 if (iterator.next() != null) {
112 ExpressionTool.ebvError("a sequence of two or more items starting with a boolean", filter);
113 }
114 return ((BooleanValue) first).getBooleanValue();
115 } else if (first instanceof StringValue) {
116 if (iterator.next() != null) {
117 ExpressionTool.ebvError("a sequence of two or more items starting with a string", filter);
118 }
119 return first.getStringValueCS().length() != 0;
120 } else if (first instanceof Int64Value) {
121 if (iterator.next() != null) {
122 ExpressionTool.ebvError("a sequence of two or more items starting with a numeric value", filter);
123 }
124 return ((Int64Value) first).longValue() == position;
125
126 } else if (first instanceof NumericValue) {
127 if (iterator.next() != null) {
128 ExpressionTool.ebvError("a sequence of two or more items starting with a numeric value", filter);
129 }
130 return ((NumericValue) first).compareTo(position) == 0;
131 } else if (first instanceof AtomicValue) {
132 ExpressionTool.ebvError("a sequence starting with an atomic value of type "
133 + ((AtomicValue) first).getPrimitiveType().getDisplayName()
134 + " (" + first.toShortString() + ")",
135 filter);
136 return false;
137 } else {
138 ExpressionTool.ebvError("a sequence starting with " + first.getGenre().getDescription() + " (" + first.toShortString() + ")", filter);
139 return false;
140 }
141 }
142 }
143
144 public void close() {
145 base.close();
146 }
147
148 /**
149 * Get properties of this iterator, as a bit-significant integer.
150 *
151 * @return the properties of this iterator. This will be some combination of
152 * properties such as {@link SequenceIterator#GROUNDED}, {@link SequenceIterator#LAST_POSITION_FINDER},
153 * and {@link SequenceIterator#LOOKAHEAD}. It is always
154 * acceptable to return the value zero, indicating that there are no known special properties.
155 * It is acceptable for the properties of the iterator to change depending on its state.
156 */
157
158 public int getProperties() {
159 return 0;
160 }
161
162 /**
163 * Subclass to handle the common special case where it is statically known
164 * that the filter cannot return a numeric value
165 */
166
167 public static final class NonNumeric<T extends Item<?>> extends FilterIterator<T> {
168
169 /**
170 * Create a CompiledFilterIterator for the situation where it is known that the filter
171 * expression will never evaluate to a number value. For this case we can simply
172 * use the effective boolean value of the predicate
173 *
174 * @param base iterator over the sequence to be filtered
175 * @param filter the filter expression
176 * @param context the current context (for evaluating the filter expression as a whole).
177 * A new context will be created to evaluate the predicate.
178 */
179
180 public NonNumeric(SequenceIterator<T> base, Expression filter,
181 XPathContext context) {
182 super(base, filter, context);
183 }
184
185 /**
186 * Determine whether the context item matches the filter predicate
187 */
188
189 protected boolean matches() throws XPathException {
190 return filter.effectiveBooleanValue(filterContext);
191 }
192
193 }
194
195 }
196