| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.pattern; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.parser.Token; |
| 11 |
import net.sf.saxon.ma.map.DictionaryMap; |
| 12 |
import net.sf.saxon.om.Item; |
| 13 |
import net.sf.saxon.om.NodeInfo; |
| 14 |
import net.sf.saxon.om.NodeName; |
| 15 |
import net.sf.saxon.om.StructuredQName; |
| 16 |
import net.sf.saxon.trans.XPathException; |
| 17 |
import net.sf.saxon.tree.tiny.NodeVectorTree; |
| 18 |
import net.sf.saxon.tree.util.FastStringBuffer; |
| 19 |
import net.sf.saxon.type.*; |
| 20 |
import net.sf.saxon.value.StringValue; |
| 21 |
import net.sf.saxon.z.IntExceptPredicate; |
| 22 |
import net.sf.saxon.z.IntSet; |
| 23 |
|
| 24 |
import java.util.Optional; |
| 25 |
import java.util.function.IntPredicate; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class CombinedNodeTest extends NodeTest { |
| 38 |
|
| 39 |
private NodeTest nodetest1; |
| 40 |
private NodeTest nodetest2; |
| 41 |
private int operator; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public CombinedNodeTest(NodeTest nt1, int operator, NodeTest nt2) { |
| 53 |
nodetest1 = nt1; |
| 54 |
this.operator = operator; |
| 55 |
nodetest2 = nt2; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public UType getUType() { |
| 65 |
UType u1 = nodetest1.getUType(); |
| 66 |
UType u2 = nodetest2.getUType(); |
| 67 |
switch (operator) { |
| 68 |
case Token.UNION: |
| 69 |
return u1.union(u2); |
| 70 |
case Token.INTERSECT: |
| 71 |
return u1.intersection(u2); |
| 72 |
case Token.EXCEPT: |
| 73 |
return u1; |
| 74 |
default: |
| 75 |
throw new IllegalArgumentException("Unknown operator in Combined Node Test"); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
@Override |
| 95 |
public boolean matches(int nodeKind, NodeName name, SchemaType annotation) { |
| 96 |
switch (operator) { |
| 97 |
case Token.UNION: |
| 98 |
return nodetest1 == null || |
| 99 |
nodetest2 == null || |
| 100 |
nodetest1.matches(nodeKind, name, annotation) || |
| 101 |
nodetest2.matches(nodeKind, name, annotation); |
| 102 |
case Token.INTERSECT: |
| 103 |
return (nodetest1 == null || nodetest1.matches(nodeKind, name, annotation)) && |
| 104 |
(nodetest2 == null || nodetest2.matches(nodeKind, name, annotation)); |
| 105 |
case Token.EXCEPT: |
| 106 |
return (nodetest1 == null || nodetest1.matches(nodeKind, name, annotation)) && |
| 107 |
!(nodetest2 == null || nodetest2.matches(nodeKind, name, annotation)); |
| 108 |
default: |
| 109 |
throw new IllegalArgumentException("Unknown operator in Combined Node Test"); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
@Override |
| 114 |
public IntPredicate getMatcher(NodeVectorTree tree) { |
| 115 |
switch (operator) { |
| 116 |
case Token.UNION: |
| 117 |
return nodetest1.getMatcher(tree).or(nodetest2.getMatcher(tree)); |
| 118 |
case Token.INTERSECT: |
| 119 |
return nodetest1.getMatcher(tree).and(nodetest2.getMatcher(tree)); |
| 120 |
case Token.EXCEPT: |
| 121 |
return new IntExceptPredicate(nodetest1.getMatcher(tree), nodetest2.getMatcher(tree)); |
| 122 |
default: |
| 123 |
throw new IllegalArgumentException("Unknown operator in Combined Node Test"); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public boolean matchesNode( NodeInfo node) { |
| 136 |
switch (operator) { |
| 137 |
case Token.UNION: |
| 138 |
return nodetest1 == null || |
| 139 |
nodetest2 == null || |
| 140 |
nodetest1.matchesNode(node) || |
| 141 |
nodetest2.matchesNode(node); |
| 142 |
case Token.INTERSECT: |
| 143 |
return (nodetest1 == null || nodetest1.matchesNode(node)) && |
| 144 |
(nodetest2 == null || nodetest2.matchesNode(node)); |
| 145 |
case Token.EXCEPT: |
| 146 |
return (nodetest1 == null || nodetest1.matchesNode(node)) && |
| 147 |
!(nodetest2 == null || nodetest2.matchesNode(node)); |
| 148 |
default: |
| 149 |
throw new IllegalArgumentException("Unknown operator in Combined Node Test"); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
public String toString() { |
| 154 |
return makeString(false); |
| 155 |
} |
| 156 |
|
| 157 |
private String makeString(boolean forExport) { |
| 158 |
if (nodetest1 instanceof NameTest && operator == Token.INTERSECT) { |
| 159 |
int kind = nodetest1.getPrimitiveType(); |
| 160 |
String skind = kind == Type.ELEMENT ? "element(" : "attribute("; |
| 161 |
String content = ""; |
| 162 |
if (nodetest2 instanceof ContentTypeTest) { |
| 163 |
SchemaType schemaType = ((ContentTypeTest) nodetest2).getSchemaType(); |
| 164 |
if (forExport) { |
| 165 |
schemaType = schemaType.getNearestNamedType(); |
| 166 |
} |
| 167 |
content = ", " + schemaType.getEQName(); |
| 168 |
if (nodetest2.isNillable()) { |
| 169 |
content += "?"; |
| 170 |
} |
| 171 |
} |
| 172 |
String name = nodetest1.getMatchingNodeName().getEQName(); |
| 173 |
return skind + name + content + ')'; |
| 174 |
} else { |
| 175 |
String nt1 = nodetest1 == null ? "item()" : nodetest1.toString(); |
| 176 |
String nt2 = nodetest2 == null ? "item()" : nodetest2.toString(); |
| 177 |
return '(' + nt1 + ' ' + Token.tokens[operator] + ' ' + nt2 + ')'; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
@Override |
| 191 |
public String toExportString() { |
| 192 |
return makeString(true); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
public void addTypeDetails(DictionaryMap map) { |
| 201 |
if (nodetest1 instanceof NameTest && operator == Token.INTERSECT) { |
| 202 |
map.initialPut("n", new StringValue(nodetest1.getMatchingNodeName().getEQName())); |
| 203 |
if (nodetest2 instanceof ContentTypeTest) { |
| 204 |
SchemaType schemaType = ((ContentTypeTest) nodetest2).getSchemaType(); |
| 205 |
|
| 206 |
map.initialPut("c", new StringValue(schemaType.getEQName() + |
| 207 |
(nodetest2.isNillable() ? "?" : ""))); |
| 208 |
|
| 209 |
|
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
public int getPrimitiveType() { |
| 223 |
UType mask = getUType(); |
| 224 |
if (mask.equals(UType.ELEMENT)) { |
| 225 |
return Type.ELEMENT; |
| 226 |
} |
| 227 |
if (mask.equals(UType.ATTRIBUTE)) { |
| 228 |
return Type.ATTRIBUTE; |
| 229 |
} |
| 230 |
if (mask.equals(UType.DOCUMENT)) { |
| 231 |
return Type.DOCUMENT; |
| 232 |
} |
| 233 |
return Type.NODE; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
public Optional<IntSet> getRequiredNodeNames() { |
| 243 |
Optional<IntSet> os1 = nodetest1.getRequiredNodeNames(); |
| 244 |
Optional<IntSet> os2 = nodetest2.getRequiredNodeNames(); |
| 245 |
if (os1.isPresent() && os2.isPresent()) { |
| 246 |
IntSet s1 = os1.get(); |
| 247 |
IntSet s2 = os2.get(); |
| 248 |
switch (operator) { |
| 249 |
case Token.UNION: { |
| 250 |
return Optional.of(s1.union(s2)); |
| 251 |
} |
| 252 |
case Token.INTERSECT: { |
| 253 |
return Optional.of(s1.intersect(s2)); |
| 254 |
} |
| 255 |
case Token.EXCEPT: { |
| 256 |
return Optional.of(s1.except(s2)); |
| 257 |
} |
| 258 |
default: |
| 259 |
throw new IllegalStateException(); |
| 260 |
} |
| 261 |
} else { |
| 262 |
return Optional.empty(); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
public SchemaType getContentType() { |
| 272 |
SchemaType type1 = nodetest1.getContentType(); |
| 273 |
SchemaType type2 = nodetest2.getContentType(); |
| 274 |
if (type1.isSameType(type2)) { |
| 275 |
return type1; |
| 276 |
} |
| 277 |
if (operator == Token.INTERSECT) { |
| 278 |
if (type2 instanceof AnyType || (type2 instanceof AnySimpleType && type1.isSimpleType())) { |
| 279 |
return type1; |
| 280 |
} |
| 281 |
if (type1 instanceof AnyType || (type1 instanceof AnySimpleType && type2.isSimpleType())) { |
| 282 |
return type2; |
| 283 |
} |
| 284 |
} |
| 285 |
return AnyType.getInstance(); |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
public AtomicType getAtomizedItemType() { |
| 295 |
AtomicType type1 = nodetest1.getAtomizedItemType(); |
| 296 |
AtomicType type2 = nodetest2.getAtomizedItemType(); |
| 297 |
if (type1.isSameType(type2)) { |
| 298 |
return type1; |
| 299 |
} |
| 300 |
if (operator == Token.INTERSECT) { |
| 301 |
if (type2.equals(BuiltInAtomicType.ANY_ATOMIC)) { |
| 302 |
return type1; |
| 303 |
} |
| 304 |
if (type1.equals(BuiltInAtomicType.ANY_ATOMIC)) { |
| 305 |
return type2; |
| 306 |
} |
| 307 |
} |
| 308 |
return BuiltInAtomicType.ANY_ATOMIC; |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
public boolean isAtomizable(TypeHierarchy th) { |
| 320 |
switch (operator) { |
| 321 |
case Token.UNION: |
| 322 |
return nodetest1.isAtomizable(th) || nodetest2.isAtomizable(th); |
| 323 |
case Token.INTERSECT: |
| 324 |
return nodetest1.isAtomizable(th) && nodetest2.isAtomizable(th); |
| 325 |
case Token.EXCEPT: |
| 326 |
return nodetest1.isAtomizable(th); |
| 327 |
default: |
| 328 |
return true; |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
public int getFingerprint() { |
| 338 |
int fp1 = nodetest1.getFingerprint(); |
| 339 |
int fp2 = nodetest2.getFingerprint(); |
| 340 |
if (fp1 == fp2) { |
| 341 |
return fp1; |
| 342 |
} |
| 343 |
if (fp2 == -1 && operator == Token.INTERSECT) { |
| 344 |
return fp1; |
| 345 |
} |
| 346 |
if (fp1 == -1 && operator == Token.INTERSECT) { |
| 347 |
return fp2; |
| 348 |
} |
| 349 |
return -1; |
| 350 |
} |
| 351 |
|
| 352 |
@Override |
| 353 |
public StructuredQName getMatchingNodeName() { |
| 354 |
StructuredQName n1 = nodetest1.getMatchingNodeName(); |
| 355 |
StructuredQName n2 = nodetest2.getMatchingNodeName(); |
| 356 |
if (n1 != null && n1.equals(n2)) { |
| 357 |
return n1; |
| 358 |
} |
| 359 |
if (n1 == null && operator == Token.INTERSECT) { |
| 360 |
return n2; |
| 361 |
} |
| 362 |
if (n2 == null && operator == Token.INTERSECT) { |
| 363 |
return n1; |
| 364 |
} |
| 365 |
return null; |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
public boolean isNillable() { |
| 375 |
|
| 376 |
return nodetest1.isNillable() && nodetest2.isNillable(); |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
public int hashCode() { |
| 384 |
return nodetest1.hashCode() ^ nodetest2.hashCode(); |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
public boolean equals(Object other) { |
| 391 |
return other instanceof CombinedNodeTest && |
| 392 |
((CombinedNodeTest) other).nodetest1.equals(nodetest1) && |
| 393 |
((CombinedNodeTest) other).nodetest2.equals(nodetest2) && |
| 394 |
((CombinedNodeTest) other).operator == operator; |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
public double getDefaultPriority() { |
| 404 |
if (operator == Token.UNION) { |
| 405 |
return nodetest1.getDefaultPriority(); |
| 406 |
} else { |
| 407 |
|
| 408 |
return 0.25; |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
public NodeTest[] getComponentNodeTests() { |
| 420 |
return new NodeTest[]{nodetest1, nodetest2}; |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
public int getOperator() { |
| 431 |
return operator; |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
@Override |
| 449 |
public String generateJavaScriptItemTypeTest(ItemType knownToBe, int targetVersion) throws XPathException { |
| 450 |
FastStringBuffer fsb = new FastStringBuffer(256); |
| 451 |
String conjunctor = null; |
| 452 |
switch (operator) { |
| 453 |
case Token.UNION: |
| 454 |
conjunctor = "||"; |
| 455 |
break; |
| 456 |
case Token.INTERSECT: |
| 457 |
conjunctor = "&&"; |
| 458 |
break; |
| 459 |
case Token.EXCEPT: |
| 460 |
conjunctor = "&&!"; |
| 461 |
break; |
| 462 |
} |
| 463 |
fsb.append("function a() {" + nodetest1.generateJavaScriptItemTypeTest(knownToBe, targetVersion) + "};"); |
| 464 |
fsb.append("function b() {" + nodetest2.generateJavaScriptItemTypeTest(knownToBe, targetVersion) + "};"); |
| 465 |
fsb.append("return a()" + conjunctor + "b();"); |
| 466 |
return fsb.toString(); |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
@Override |
| 479 |
public Optional<String> explainMismatch(Item item, TypeHierarchy th) { |
| 480 |
Optional<String> explanation = super.explainMismatch(item, th); |
| 481 |
if (explanation.isPresent()) { |
| 482 |
return explanation; |
| 483 |
} |
| 484 |
if (operator == Token.INTERSECT) { |
| 485 |
|
| 486 |
if (!nodetest1.matchesNode((NodeInfo)item)) { |
| 487 |
return nodetest1.explainMismatch(item, th); |
| 488 |
} else if (!nodetest2.matchesNode((NodeInfo)item)) { |
| 489 |
return nodetest2.explainMismatch(item, th); |
| 490 |
} |
| 491 |
} |
| 492 |
return Optional.empty(); |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
} |
| 497 |
|