| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.pattern; |
| 9 |
|
| 10 |
import com.saxonica.ee.stream.Posture; |
| 11 |
import com.saxonica.ee.stream.Streamability; |
| 12 |
import com.saxonica.ee.stream.Sweep; |
| 13 |
import com.saxonica.ee.trans.ContextItemStaticInfoEE; |
| 14 |
import net.sf.saxon.expr.*; |
| 15 |
import net.sf.saxon.expr.instruct.SlotManager; |
| 16 |
import net.sf.saxon.expr.parser.*; |
| 17 |
import net.sf.saxon.om.Item; |
| 18 |
import net.sf.saxon.om.NodeInfo; |
| 19 |
import net.sf.saxon.om.SequenceIterator; |
| 20 |
import net.sf.saxon.om.TreeInfo; |
| 21 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 22 |
import net.sf.saxon.trans.XPathException; |
| 23 |
import net.sf.saxon.tree.iter.ManualIterator; |
| 24 |
import net.sf.saxon.type.ItemType; |
| 25 |
import net.sf.saxon.type.UType; |
| 26 |
import net.sf.saxon.value.SequenceType; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class NodeSetPattern extends Pattern { |
| 39 |
|
| 40 |
protected Operand selectionOp; |
| 41 |
protected ItemType itemType; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public NodeSetPattern(Expression exp) { |
| 51 |
selectionOp = new Operand(this, exp, OperandRole.NAVIGATE); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
@Override |
| 65 |
public Iterable<Operand> operands() { |
| 66 |
return selectionOp; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public Expression getSelectionExpression() { |
| 75 |
return selectionOp.getChildExpression(); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public Pattern typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException { |
| 87 |
selectionOp.setChildExpression(getSelectionExpression().typeCheck(visitor, contextItemType)); |
| 88 |
RoleDiagnostic role = new RoleDiagnostic(RoleDiagnostic.MATCH_PATTERN, getSelectionExpression().toString(), 0); |
| 89 |
TypeChecker tc = visitor.getConfiguration().getTypeChecker(false); |
| 90 |
Expression checked = null; |
| 91 |
try { |
| 92 |
checked = tc.staticTypeCheck( |
| 93 |
getSelectionExpression(), SequenceType.NODE_SEQUENCE, role, visitor); |
| 94 |
} catch (XPathException e) { |
| 95 |
visitor.issueWarning("Pattern will never match anything. " + e.getMessage(), getLocation()); |
| 96 |
checked = Literal.makeEmptySequence(); |
| 97 |
} |
| 98 |
selectionOp.setChildExpression(checked); |
| 99 |
itemType = getSelectionExpression().getItemType(); |
| 100 |
return this; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public void setItemType(ItemType type) { |
| 109 |
this.itemType = type; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public int getDependencies() { |
| 118 |
return getSelectionExpression().getDependencies(); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
@Override |
| 132 |
public boolean isMotionless() { |
| 133 |
Expression select = getSelectionExpression(); |
| 134 |
Streamability.getStreamability(select, new ContextItemStaticInfoEE(getItemType(), false, Posture.STRIDING), null); |
| 135 |
return Streamability.getSweep(select) == Sweep.MOTIONLESS; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public int allocateSlots(SlotManager slotManager, int nextFree) { |
| 147 |
return ExpressionTool.allocateSlots(getSelectionExpression(), nextFree, slotManager); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public SequenceIterator<? extends NodeInfo> selectNodes(TreeInfo doc, XPathContext context) throws XPathException { |
| 159 |
XPathContext c2 = context.newMinorContext(); |
| 160 |
ManualIterator<NodeInfo> mi = new ManualIterator<>(doc.getRootNode()); |
| 161 |
c2.setCurrentIterator(mi); |
| 162 |
return (SequenceIterator<? extends NodeInfo>)getSelectionExpression().iterate(c2); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public boolean matches(Item<?> item, XPathContext context) throws XPathException { |
| 173 |
if (item instanceof NodeInfo) { |
| 174 |
SequenceIterator iter = getSelectionExpression().iterate(context); |
| 175 |
return SingletonIntersectExpression.containsNode(iter, (NodeInfo)item); |
| 176 |
} else { |
| 177 |
return false; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
@Override |
| 187 |
public UType getUType() { |
| 188 |
return getItemType().getUType(); |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
public ItemType getItemType() { |
| 196 |
if (itemType == null) { |
| 197 |
itemType = getSelectionExpression().getItemType(); |
| 198 |
} |
| 199 |
if (itemType instanceof NodeTest) { |
| 200 |
return itemType; |
| 201 |
} else { |
| 202 |
return AnyNodeTest.getInstance(); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
public boolean equals(Object other) { |
| 213 |
return (other instanceof NodeSetPattern) && |
| 214 |
((NodeSetPattern) other).getSelectionExpression().isEqual(getSelectionExpression()); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
public int computeHashCode() { |
| 222 |
return 0x73108728 ^ getSelectionExpression().hashCode(); |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
public Pattern copy(RebindingMap rebindings) { |
| 234 |
NodeSetPattern n = new NodeSetPattern(getSelectionExpression().copy(rebindings)); |
| 235 |
ExpressionTool.copyLocationInfo(this, n); |
| 236 |
return n; |
| 237 |
} |
| 238 |
|
| 239 |
public void export(ExpressionPresenter presenter) throws XPathException { |
| 240 |
presenter.startElement("p.nodeSet"); |
| 241 |
if (itemType != null) { |
| 242 |
presenter.emitAttribute("type", itemType.toExportString()); |
| 243 |
} |
| 244 |
getSelectionExpression().export(presenter); |
| 245 |
presenter.endElement(); |
| 246 |
} |
| 247 |
|
| 248 |
} |
| 249 |
|