| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.pattern; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.expr.*; |
| 12 |
import net.sf.saxon.expr.instruct.SlotManager; |
| 13 |
import net.sf.saxon.expr.parser.*; |
| 14 |
import net.sf.saxon.functions.Current; |
| 15 |
import net.sf.saxon.om.AxisInfo; |
| 16 |
import net.sf.saxon.om.Item; |
| 17 |
import net.sf.saxon.om.NodeInfo; |
| 18 |
import net.sf.saxon.om.SequenceIterator; |
| 19 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 20 |
import net.sf.saxon.trans.XPathException; |
| 21 |
import net.sf.saxon.tree.iter.AxisIterator; |
| 22 |
import net.sf.saxon.tree.iter.ManualIterator; |
| 23 |
import net.sf.saxon.type.ItemType; |
| 24 |
import net.sf.saxon.type.Type; |
| 25 |
import net.sf.saxon.type.TypeHierarchy; |
| 26 |
import net.sf.saxon.type.UType; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public final class GeneralNodePattern extends Pattern { |
| 35 |
|
| 36 |
private Expression equivalentExpr = null; |
| 37 |
private NodeTest itemType = null; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public GeneralNodePattern(Expression expr, NodeTest itemType) { |
| 47 |
equivalentExpr = expr; |
| 48 |
this.itemType = itemType; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
@Override |
| 62 |
public Iterable<Operand> operands() { |
| 63 |
return new Operand(this, equivalentExpr, OperandRole.SAME_FOCUS_ACTION); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public boolean isMotionless() { |
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public Pattern typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException { |
| 88 |
ContextItemStaticInfo cit = visitor.getConfiguration().getDefaultContextItemStaticInfo(); |
| 89 |
equivalentExpr = equivalentExpr.typeCheck(visitor, cit); |
| 90 |
return this; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
@Override |
| 109 |
public Pattern optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 110 |
Configuration config = visitor.getConfiguration(); |
| 111 |
ContextItemStaticInfo defaultInfo = config.getDefaultContextItemStaticInfo(); |
| 112 |
equivalentExpr = equivalentExpr.optimize(visitor, defaultInfo); |
| 113 |
|
| 114 |
if (equivalentExpr instanceof FilterExpression && !((FilterExpression) equivalentExpr).isFilterIsPositional()) { |
| 115 |
try { |
| 116 |
return PatternMaker.fromExpression(equivalentExpr, config, true) |
| 117 |
.typeCheck(visitor, defaultInfo); |
| 118 |
} catch (XPathException err) { |
| 119 |
|
| 120 |
} |
| 121 |
} |
| 122 |
return this; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
public int getDependencies() { |
| 134 |
return equivalentExpr.getDependencies() & |
| 135 |
(StaticProperty.DEPENDS_ON_LOCAL_VARIABLES | StaticProperty.DEPENDS_ON_USER_FUNCTIONS); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
@Override |
| 142 |
public void bindCurrent(LocalBinding binding) { |
| 143 |
if (ExpressionTool.callsFunction(equivalentExpr, Current.FN_CURRENT, false)) { |
| 144 |
if (equivalentExpr.isCallOn(Current.class)) { |
| 145 |
equivalentExpr = new LocalVariableReference(binding); |
| 146 |
} else { |
| 147 |
replaceCurrent(equivalentExpr, binding); |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public int allocateSlots(SlotManager slotManager, int nextFree) { |
| 160 |
return ExpressionTool.allocateSlots(equivalentExpr, nextFree, slotManager); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
public boolean matches(Item<?> item, XPathContext context) throws XPathException { |
| 171 |
TypeHierarchy th = context.getConfiguration().getTypeHierarchy(); |
| 172 |
if (!itemType.matches(item, th)) { |
| 173 |
return false; |
| 174 |
} |
| 175 |
AxisIterator anc = ((NodeInfo) item).iterateAxis(AxisInfo.ANCESTOR_OR_SELF); |
| 176 |
while (true) { |
| 177 |
NodeInfo a = anc.next(); |
| 178 |
if (a == null) { |
| 179 |
return false; |
| 180 |
} |
| 181 |
if (matchesBeneathAnchor((NodeInfo) item, a, context)) { |
| 182 |
return true; |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
public boolean matchesBeneathAnchor(NodeInfo node, NodeInfo anchor, XPathContext context) throws XPathException { |
| 199 |
if (!itemType.matchesNode(node)) { |
| 200 |
return false; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
if (anchor == null) { |
| 208 |
AxisIterator ancestors = node.iterateAxis(AxisInfo.ANCESTOR_OR_SELF); |
| 209 |
while (true) { |
| 210 |
NodeInfo ancestor = ancestors.next(); |
| 211 |
if (ancestor == null) { |
| 212 |
return false; |
| 213 |
} |
| 214 |
if (matchesBeneathAnchor(node, ancestor, context)) { |
| 215 |
return true; |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
XPathContext c2 = context.newMinorContext(); |
| 222 |
ManualIterator<? extends NodeInfo> iter = new ManualIterator<>(anchor); |
| 223 |
c2.setCurrentIterator(iter); |
| 224 |
try { |
| 225 |
SequenceIterator<? extends NodeInfo> nsv = (SequenceIterator<? extends NodeInfo>)equivalentExpr.iterate(c2); |
| 226 |
while (true) { |
| 227 |
NodeInfo n = nsv.next(); |
| 228 |
if (n == null) { |
| 229 |
return false; |
| 230 |
} |
| 231 |
if (n.equals(node)) { |
| 232 |
return true; |
| 233 |
} |
| 234 |
} |
| 235 |
} catch (XPathException.Circularity e) { |
| 236 |
throw e; |
| 237 |
} catch (XPathException e) { |
| 238 |
handleDynamicError(e, c2); |
| 239 |
return false; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
@Override |
| 249 |
public UType getUType() { |
| 250 |
return itemType.getUType(); |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
public int getFingerprint() { |
| 261 |
return itemType.getFingerprint(); |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
public ItemType getItemType() { |
| 269 |
return itemType; |
| 270 |
} |
| 271 |
|
| 272 |
public Expression getEquivalentExpr() { |
| 273 |
return equivalentExpr; |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
public boolean equals(Object other) { |
| 282 |
if (other instanceof GeneralNodePattern) { |
| 283 |
GeneralNodePattern lpp = (GeneralNodePattern) other; |
| 284 |
return equivalentExpr.isEqual(lpp.equivalentExpr); |
| 285 |
} else { |
| 286 |
return false; |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
public int computeHashCode() { |
| 295 |
return 83641 ^ equivalentExpr.hashCode(); |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
public Pattern copy(RebindingMap rebindings) { |
| 307 |
GeneralNodePattern n = new GeneralNodePattern(equivalentExpr.copy(rebindings), itemType); |
| 308 |
ExpressionTool.copyLocationInfo(this, n); |
| 309 |
return n; |
| 310 |
} |
| 311 |
|
| 312 |
public void export(ExpressionPresenter presenter) throws XPathException { |
| 313 |
presenter.startElement("p.genNode"); |
| 314 |
presenter.emitAttribute("type", itemType.toString()); |
| 315 |
equivalentExpr.export(presenter); |
| 316 |
presenter.endElement(); |
| 317 |
} |
| 318 |
|
| 319 |
} |
| 320 |
|