| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 20 |
|
| 21 |
|
| 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 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 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 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 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 |
|
| 59 |
|
| 60 |
|
| 61 |
public T next() throws XPathException { |
| 62 |
return getNextMatchingItem(); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 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 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
protected boolean matches() throws XPathException { |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 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 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public int getProperties() { |
| 159 |
return 0; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public static final class NonNumeric<T extends Item<?>> extends FilterIterator<T> { |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
public NonNumeric(SequenceIterator<T> base, Expression filter, |
| 181 |
XPathContext context) { |
| 182 |
super(base, filter, context); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
protected boolean matches() throws XPathException { |
| 190 |
return filter.effectiveBooleanValue(filterContext); |
| 191 |
} |
| 192 |
|
| 193 |
} |
| 194 |
|
| 195 |
} |
| 196 |
|