| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.pattern; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.*; |
| 11 |
import net.sf.saxon.expr.instruct.SlotManager; |
| 12 |
import net.sf.saxon.expr.parser.*; |
| 13 |
import net.sf.saxon.om.AxisInfo; |
| 14 |
import net.sf.saxon.om.Item; |
| 15 |
import net.sf.saxon.om.NodeInfo; |
| 16 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 17 |
import net.sf.saxon.trans.XPathException; |
| 18 |
import net.sf.saxon.type.ItemType; |
| 19 |
import net.sf.saxon.type.Type; |
| 20 |
import net.sf.saxon.type.UType; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public final class AncestorQualifiedPattern extends Pattern { |
| 28 |
|
| 29 |
private Pattern basePattern; |
| 30 |
private Pattern upperPattern; |
| 31 |
private byte upwardsAxis = AxisInfo.PARENT; |
| 32 |
private ItemType refinedItemType; |
| 33 |
private boolean testUpperPatternFirst = false; |
| 34 |
|
| 35 |
public AncestorQualifiedPattern(Pattern base, Pattern upper, byte axis) { |
| 36 |
this.basePattern = base; |
| 37 |
this.upperPattern = upper; |
| 38 |
this.upwardsAxis = axis; |
| 39 |
adoptChildExpression(base); |
| 40 |
adoptChildExpression(upper); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
@Override |
| 54 |
public Iterable<Operand> operands() { |
| 55 |
return operandList( |
| 56 |
new Operand(this, upperPattern, OperandRole.SAME_FOCUS_ACTION), |
| 57 |
new Operand(this, basePattern, OperandRole.SAME_FOCUS_ACTION)); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
@Override |
| 64 |
public void bindCurrent(LocalBinding binding) { |
| 65 |
basePattern.bindCurrent(binding); |
| 66 |
upperPattern.bindCurrent(binding); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public Pattern getBasePattern() { |
| 75 |
return basePattern; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public Pattern getUpperPattern() { |
| 86 |
return upperPattern; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public byte getUpwardsAxis() { |
| 97 |
return upwardsAxis; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public boolean isMotionless() { |
| 110 |
return basePattern.isMotionless() && upperPattern.isMotionless(); |
| 111 |
} |
| 112 |
|
| 113 |
@Override |
| 114 |
public boolean matchesCurrentGroup() { |
| 115 |
return upperPattern.matchesCurrentGroup(); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public Pattern simplify() throws XPathException { |
| 124 |
upperPattern = upperPattern.simplify(); |
| 125 |
basePattern = basePattern.simplify(); |
| 126 |
return this; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public Pattern typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException { |
| 138 |
basePattern = basePattern.typeCheck(visitor, contextItemType); |
| 139 |
upperPattern = upperPattern.typeCheck(visitor, contextItemType); |
| 140 |
if (upwardsAxis == AxisInfo.PARENT) { |
| 141 |
ItemType type = basePattern.getItemType(); |
| 142 |
if (type instanceof NodeTest) { |
| 143 |
|
| 144 |
AxisExpression step; |
| 145 |
if (type.getPrimitiveType() == Type.ATTRIBUTE) { |
| 146 |
step = new AxisExpression(AxisInfo.ATTRIBUTE, (NodeTest) type); |
| 147 |
} else { |
| 148 |
step = new AxisExpression(AxisInfo.CHILD, (NodeTest) type); |
| 149 |
} |
| 150 |
ExpressionTool.copyLocationInfo(this, step); |
| 151 |
Expression exp = step.typeCheck(visitor, visitor.getConfiguration().makeContextItemStaticInfo(upperPattern.getItemType(), false)); |
| 152 |
refinedItemType = exp.getItemType(); |
| 153 |
} |
| 154 |
} |
| 155 |
testUpperPatternFirst = upperPattern.getCost() < basePattern.getCost(); |
| 156 |
return this; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public int getDependencies() { |
| 166 |
return basePattern.getDependencies() | upperPattern.getDependencies(); |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
public int allocateSlots(SlotManager slotManager, int nextFree) { |
| 177 |
|
| 178 |
|
| 179 |
nextFree = upperPattern.allocateSlots(slotManager, nextFree); |
| 180 |
nextFree = basePattern.allocateSlots(slotManager, nextFree); |
| 181 |
return nextFree; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
public boolean matches(Item<?> item, XPathContext context) throws XPathException { |
| 193 |
return item instanceof NodeInfo && matchesBeneathAnchor((NodeInfo) item, null, context); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
public boolean matchesBeneathAnchor(NodeInfo node, NodeInfo anchor, XPathContext context) throws XPathException { |
| 208 |
if (testUpperPatternFirst) { |
| 209 |
return matchesUpperPattern(node, anchor, context) && basePattern.matches(node, context); |
| 210 |
} else { |
| 211 |
return basePattern.matchesBeneathAnchor(node, anchor, context) && matchesUpperPattern(node, anchor, context); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
private boolean matchesUpperPattern(NodeInfo node, NodeInfo anchor, XPathContext context) throws XPathException { |
| 216 |
switch (upwardsAxis) { |
| 217 |
case AxisInfo.SELF: |
| 218 |
return upperPattern.matchesBeneathAnchor(node, anchor, context); |
| 219 |
|
| 220 |
case AxisInfo.PARENT: |
| 221 |
NodeInfo par = node.getParent(); |
| 222 |
return par != null && upperPattern.matchesBeneathAnchor(par, anchor, context); |
| 223 |
|
| 224 |
case AxisInfo.ANCESTOR: { |
| 225 |
NodeInfo anc = node.getParent(); |
| 226 |
return hasMatchingAncestor(anchor, anc, context); |
| 227 |
} |
| 228 |
case AxisInfo.ANCESTOR_OR_SELF: { |
| 229 |
return hasMatchingAncestor(anchor, node, context); |
| 230 |
} |
| 231 |
default: |
| 232 |
throw new XPathException("Unsupported axis " + AxisInfo.axisName[upwardsAxis] + " in pattern"); |
| 233 |
} |
| 234 |
|
| 235 |
} |
| 236 |
|
| 237 |
private boolean hasMatchingAncestor(NodeInfo anchor, NodeInfo anc, XPathContext context) throws XPathException { |
| 238 |
while (anc != null) { |
| 239 |
if (upperPattern.matchesBeneathAnchor(anc, anchor, context)) { |
| 240 |
return true; |
| 241 |
} |
| 242 |
if (anc.equals(anchor)) { |
| 243 |
return false; |
| 244 |
} |
| 245 |
anc = anc.getParent(); |
| 246 |
} |
| 247 |
return false; |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
@Override |
| 256 |
public UType getUType() { |
| 257 |
return basePattern.getUType(); |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
public int getFingerprint() { |
| 268 |
return basePattern.getFingerprint(); |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
public ItemType getItemType() { |
| 276 |
if (refinedItemType != null) { |
| 277 |
return refinedItemType; |
| 278 |
} |
| 279 |
return basePattern.getItemType(); |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
@Override |
| 292 |
public Pattern convertToTypedPattern(String val) throws XPathException { |
| 293 |
if (upperPattern.getUType().equals(UType.DOCUMENT)) { |
| 294 |
|
| 295 |
Pattern b2 = basePattern.convertToTypedPattern(val); |
| 296 |
if (b2 == basePattern) { |
| 297 |
return this; |
| 298 |
} else { |
| 299 |
return new AncestorQualifiedPattern(b2, upperPattern, upwardsAxis); |
| 300 |
} |
| 301 |
} else { |
| 302 |
Pattern u2 = upperPattern.convertToTypedPattern(val); |
| 303 |
if (u2 == upperPattern) { |
| 304 |
return this; |
| 305 |
} else { |
| 306 |
return new AncestorQualifiedPattern(basePattern, u2, upwardsAxis); |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
@Override |
| 315 |
public String toString() { |
| 316 |
return upperPattern + (upwardsAxis == AxisInfo.PARENT ? "/" : "//") + basePattern; |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
public boolean equals(Object other) { |
| 326 |
if (other instanceof AncestorQualifiedPattern) { |
| 327 |
AncestorQualifiedPattern aqp = (AncestorQualifiedPattern) other; |
| 328 |
return basePattern.isEqual(aqp.basePattern) && upperPattern.isEqual(aqp.upperPattern) && upwardsAxis == aqp.upwardsAxis; |
| 329 |
} else { |
| 330 |
return false; |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
public int computeHashCode() { |
| 339 |
return 88267 ^ basePattern.hashCode() ^ upperPattern.hashCode() ^ (upwardsAxis << 22); |
| 340 |
} |
| 341 |
|
| 342 |
public void export(ExpressionPresenter presenter) throws XPathException { |
| 343 |
presenter.startElement("p.withUpper"); |
| 344 |
presenter.emitAttribute("axis", AxisInfo.axisName[getUpwardsAxis()]); |
| 345 |
presenter.emitAttribute("upFirst", ""+ testUpperPatternFirst); |
| 346 |
basePattern.export(presenter); |
| 347 |
upperPattern.export(presenter); |
| 348 |
presenter.endElement(); |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
public Pattern copy(RebindingMap rebindings) { |
| 360 |
AncestorQualifiedPattern n = new AncestorQualifiedPattern(basePattern.copy(rebindings), |
| 361 |
upperPattern.copy(rebindings), upwardsAxis); |
| 362 |
ExpressionTool.copyLocationInfo(this, n); |
| 363 |
return n; |
| 364 |
} |
| 365 |
|
| 366 |
} |
| 367 |
|