1 22 23 package org.xquark.xquery.parser; 24 25 import java.util.ArrayList ; 26 27 import org.xquark.schema.Type; 28 import org.xquark.xpath.*; 29 import org.xquark.xquery.typing.TypeException; 30 31 public class Step extends XQueryUnaryOperatorExpression implements StepExpr { 32 private static final String RCSRevision = "$Revision: 1.11 $"; 33 private static final String RCSName = "$Name: $"; 34 35 public static final int NONE = 0; 36 37 public static final int SELF_QNAME = 0x10; 38 public static final int CHILD_QNAME = 0x20; 39 public static final int PARENT_QNAME = 0x30; 40 public static final int ANCESTOR_QNAME = 0x40; 41 public static final int ANCESTOR_OR_SELF_QNAME = 0x50; 42 public static final int DESCENDANT_QNAME = 0x60; 43 public static final int DESCENDANT_OR_SELF_QNAME = 0x70; 44 public static final int PRECEDING_QNAME = 0x80; 45 public static final int PRECEDING_SIBLING_QNAME = 0x90; 46 public static final int FOLLOWING_QNAME = 0xa0; 47 public static final int FOLLOWING_SIBLING_QNAME = 0xb0; 48 49 51 public static final int SELF_NODE = 0x11; 52 public static final int CHILD_NODE = 0x21; 53 public static final int PARENT_NODE = 0x31; 54 public static final int ANCESTOR_NODE = 0x41; 55 public static final int ANCESTOR_OR_SELF_NODE = 0x51; 56 public static final int DESCENDANT_NODE = 0x61; 57 public static final int DESCENDANT_OR_SELF_NODE = 0x71; 58 public static final int PRECEDING_NODE = 0x81; 59 public static final int PRECEDING_SIBLING_NODE = 0x91; 60 public static final int FOLLOWING_NODE = 0xa1; 61 public static final int FOLLOWING_SIBLING_NODE = 0xb1; 62 63 public static final int SELF_DOCUMENT = 0x12; 64 public static final int CHILD_DOCUMENT = 0x22; 65 public static final int PARENT_DOCUMENT = 0x32; 66 public static final int ANCESTOR_DOCUMENT = 0x42; 67 public static final int ANCESTOR_OR_SELF_DOCUMENT = 0x52; 68 public static final int DESCENDANT_DOCUMENT = 0x62; 69 public static final int DESCENDANT_OR_SELF_DOCUMENT = 0x72; 70 public static final int PRECEDING_DOCUMENT = 0x82; 71 public static final int PRECEDING_SIBLING_DOCUMENT = 0x92; 72 public static final int FOLLOWING_DOCUMENT = 0xa2; 73 public static final int FOLLOWING_SIBLING_DOCUMENT = 0xb2; 74 75 public static final int SELF_ATTRIBUTE = 0x13; 76 public static final int CHILD_ATTRIBUTE = 0x23; 77 public static final int PARENT_ATTRIBUTE = 0x33; 78 public static final int ANCESTOR_ATTRIBUTE = 0x43; 79 public static final int ANCESTOR_OR_SELF_ATTRIBUTE = 0x53; 80 public static final int DESCENDANT_ATTRIBUTE = 0x63; 81 public static final int DESCENDANT_OR_SELF_ATTRIBUTE = 0x73; 82 public static final int PRECEDING_ATTRIBUTE = 0x83; 83 public static final int PRECEDING_SIBLING_ATTRIBUTE = 0x93; 84 public static final int FOLLOWING_ATTRIBUTE = 0xa3; 85 public static final int FOLLOWING_SIBLING_ATTRIBUTE = 0xb3; 86 87 public static final int SELF_PI = 0x14; 88 public static final int CHILD_PI = 0x24; 89 public static final int PARENT_PI = 0x34; 90 public static final int ANCESTOR_PI = 0x44; 91 public static final int ANCESTOR_OR_SELF_PI = 0x54; 92 public static final int DESCENDANT_PI = 0x64; 93 public static final int DESCENDANT_OR_SELF_PI = 0x74; 94 public static final int PRECEDING_PI = 0x84; 95 public static final int PRECEDING_SIBLING_PI = 0x94; 96 public static final int FOLLOWING_PI = 0xa4; 97 public static final int FOLLOWING_SIBLING_PI = 0xb4; 98 99 public static final int SELF_COMMENT = 0x15; 100 public static final int CHILD_COMMENT = 0x25; 101 public static final int PARENT_COMMENT = 0x35; 102 public static final int ANCESTOR_COMMENT = 0x45; 103 public static final int ANCESTOR_OR_SELF_COMMENT = 0x55; 104 public static final int DESCENDANT_COMMENT = 0x65; 105 public static final int DESCENDANT_OR_SELF_COMMENT = 0x75; 106 public static final int PRECEDING_COMMENT = 0x85; 107 public static final int PRECEDING_SIBLING_COMMENT = 0x95; 108 public static final int FOLLOWING_COMMENT = 0xa5; 109 public static final int FOLLOWING_SIBLING_COMMENT = 0xb5; 110 111 public static final int SELF_TEXT = 0x16; 112 public static final int CHILD_TEXT = 0x26; 113 public static final int PARENT_TEXT = 0x36; 114 public static final int ANCESTOR_TEXT = 0x46; 115 public static final int ANCESTOR_OR_SELF_TEXT = 0x56; 116 public static final int DESCENDANT_TEXT = 0x66; 117 public static final int DESCENDANT_OR_SELF_TEXT = 0x76; 118 public static final int PRECEDING_TEXT = 0x86; 119 public static final int PRECEDING_SIBLING_TEXT = 0x96; 120 public static final int FOLLOWING_TEXT = 0xa6; 121 public static final int FOLLOWING_SIBLING_TEXT = 0xb6; 122 123 public static final int SELF_NAMESPACE = 0x17; 124 public static final int CHILD_NAMESPACE = 0x27; 125 public static final int PARENT_NAMESPACE = 0x37; 126 public static final int ANCESTOR_NAMESPACE = 0x47; 127 public static final int ANCESTOR_OR_SELF_NAMESPACE = 0x57; 128 public static final int DESCENDANT_NAMESPACE = 0x67; 129 public static final int DESCENDANT_OR_SELF_NAMESPACE = 0x77; 130 public static final int PRECEDING_NAMESPACE = 0x87; 131 public static final int PRECEDING_SIBLING_NAMESPACE = 0x97; 132 public static final int FOLLOWING_NAMESPACE = 0xa7; 133 public static final int FOLLOWING_SIBLING_NAMESPACE = 0xb7; 134 135 public static final int SELF_ELEMENT = 0x18; 136 public static final int CHILD_ELEMENT = 0x28; 137 public static final int PARENT_ELEMENT = 0x38; 138 public static final int ANCESTOR_ELEMENT = 0x48; 139 public static final int ANCESTOR_OR_SELF_ELEMENT = 0x58; 140 public static final int DESCENDANT_ELEMENT = 0x68; 141 public static final int DESCENDANT_OR_SELF_ELEMENT = 0x78; 142 public static final int PRECEDING_ELEMENT = 0x88; 143 public static final int PRECEDING_SIBLING_ELEMENT = 0x98; 144 public static final int FOLLOWING_ELEMENT = 0xa8; 145 public static final int FOLLOWING_SIBLING_ELEMENT = 0xb8; 146 147 protected ArrayList predicates = null; 148 151 private boolean hasSeparator = false; 152 153 156 private byte axis = Axis.NONE; 157 158 161 private String localName = null; 162 163 166 private String nameSpace = null; 167 168 171 private byte wild = StepExpr.NO_WILDCARD; 172 173 176 private byte kindtest = NodeKind.NONE; 177 178 181 private Type type = null; 182 183 186 private boolean isTypeNillable = false; 187 188 191 private int stepKind = NONE; 192 193 197 public void accept(ParserVisitor visitor) throws XQueryException { 198 visitor.visit(this); 199 } 200 201 205 public Step(boolean hasSeparator, byte axis, XQueryExpression expression, ArrayList predicates, XQueryModule parentModule) throws TypeException, XQueryException { 206 super(expression); 207 setHasSeparator(hasSeparator); 208 setAxis(axis); 209 setPredicates(predicates); 210 setInterfaceProperties(); 211 setParentModule(parentModule); 212 } 215 216 220 private void setInterfaceProperties() { 221 stepKind = NONE; 224 if (expression instanceof QName) { 225 QName qname = (QName) expression; 226 localName = qname.getLocalName(); 227 nameSpace = qname.getNameSpace(); 228 kindtest = NodeKind.ELEMENT; 229 byte nodeKind = NodeKind.ELEMENT; 230 switch (axis) { 231 case Axis.ATTRIBUTE : 232 nodeKind = NodeKind.ATTRIBUTE; 233 break; 234 case Axis.NAMESPACE : 235 nodeKind = NodeKind.NAMESPACE; 236 break; 237 } 238 xnode = new XNode(nameSpace, localName, nodeKind); 239 if (NAME_WILDCARD.equals(qname.getName())) 241 wild = StepExpr.WILDCARD; 242 else if (NAME_WILDCARD.equals(localName)) 243 wild = StepExpr.LOCALNAME_WILDCARD; 244 else if (NAME_WILDCARD.equals(nameSpace)) 245 wild = StepExpr.NAMESPACE_WILDCARD; 246 stepKind = axis*16; 248 } else if (expression instanceof NodeTest) { 249 kindtest = ((NodeTest) expression).getKind(); 251 xnode = new XNode(kindtest); 252 stepKind = axis*16+kindtest; 254 } 255 } 256 257 public void setParentModule(XQueryModule parentUnit) { 258 if (parentUnit == null) 259 return; 260 this.parentModule = parentUnit; 261 super.setParentModule(parentUnit); 262 if (predicates != null) 263 for (int i = 0; i < predicates.size(); i++) 264 ((XQueryExpression) predicates.get(i)).setParentModule(parentUnit); 265 } 266 267 public boolean hasSeparator() { 268 return hasSeparator; 269 } 270 public void setHasSeparator(boolean hasSeparator) { 271 this.hasSeparator = hasSeparator; 272 } 273 274 public byte getAxis() { 275 return axis; 276 } 277 public void setAxis(byte axis) { 278 this.axis = axis; 279 } 280 281 public ArrayList getPredicates() { 282 return predicates; 283 } 284 public void setPredicates(ArrayList predicates) { 285 this.predicates = predicates; 286 if (predicates != null) 287 for (int i = 0; i < this.predicates.size(); i++) { 288 XQueryExpression expr = (XQueryExpression) this.predicates.get(i); 289 expr.setParentModule(parentModule); 290 expr.setParentExpression(this); 291 } 292 } 293 294 public void addPredicates(ArrayList predicates) { 295 if (predicates == null || predicates.isEmpty()) 296 return; 297 if (this.predicates == null || this.predicates.isEmpty()) { 298 setPredicates(predicates); 299 return; 300 } 301 this.predicates.addAll(predicates); 302 setPredicates(this.predicates); 303 } 304 305 public void addPredicate(XQueryExpression predicate) { 306 if (predicate == null) 307 return; 308 if (predicates == null) 309 predicates = new ArrayList (1); 310 predicates.add(predicate); 311 predicate.setParentModule(parentModule); 312 predicate.setParentExpression(this); 313 } 314 315 public void addPredicate(int index, XQueryExpression predicate) { 316 if (predicates == null) 317 predicates = new ArrayList (1); 318 predicates.add(index, predicate); 319 predicate.setParentModule(parentModule); 320 predicate.setParentExpression(this); 321 } 322 323 327 public String toHashCodeString() { 328 return toString(); 329 } 435 436 public void addParentExpression(XQueryExpression parentExpression) { 440 addParentExpression(parentExpression); 441 expression.addParentExpression(parentExpression); 442 if (predicates != null) 443 for (int i = 0; i < predicates.size(); i++) 444 ((XQueryExpression) predicates.get(i)).addParentExpression(parentExpression); 445 } 446 447 451 public boolean hasPredicates() { 452 if (predicates != null && !predicates.isEmpty()) 453 return true; 454 return false; 455 } 456 457 public boolean hasPositionPredicates() { 458 if (predicates != null) 459 for (int i = 0; i < predicates.size(); i++) { 460 XQueryExpression predExpr = (XQueryExpression) predicates.get(i); 461 if (predExpr instanceof ValueInteger) 463 return true; 464 } 465 return false; 466 } 467 468 public int getStepKind() { 469 return stepKind; 470 } 471 472 476 480 public String getNameSpace() { 481 return nameSpace; 482 } 483 484 488 public String getLocalName() { 489 return localName; 490 } 491 492 496 public byte getWildCardType() { 497 return wild; 498 } 499 500 504 public boolean hasWildcard() { 505 return wild != StepExpr.NO_WILDCARD; 506 } 507 508 512 public boolean isWildcard() { 513 return wild == StepExpr.WILDCARD; 514 } 515 516 520 public boolean isNameSpaceWildcard() { 521 return isWildcard() || wild == StepExpr.NAMESPACE_WILDCARD; 522 } 523 524 528 public boolean isLocalNameWildcard() { 529 return isWildcard() || wild == StepExpr.LOCALNAME_WILDCARD; 530 } 531 532 536 public byte getKindTest() { 537 return kindtest; 538 } 539 540 544 public org.xquark.schema.Type getType() { 545 return type; 546 } 547 548 552 public boolean isTypeNillable() { 553 return isTypeNillable; 554 } 555 556 558 public static final String NAME_WILDCARD = "*"; 559 public XNode xnode = null; 560 561 571 public XNode getXNode() { 572 return xnode; 573 } 574 575 public boolean match(XNode node) { 576 if (node == null) 577 return false; 578 if (isTypeTest()) { return typeMatch(node); 580 } else { 582 if (typeMatch(node) == false) { 583 return false; 584 } 585 if (getNamespace() == null) { 586 if (node.getNamespace() == null) { 587 } else { 589 return false; 590 } 591 } else if (isNamespaceWildcard()) { 592 } else if (node.getNamespace() == null) { 594 return false; 595 } else if (getNamespace().equals(node.getNamespace())) { 596 } else { 598 return false; 599 } 600 601 if (this.getLocalName() == null) { 603 if (node.getLocalName() == null) { 604 } else { 606 return false; 607 } 608 } else if (this.isLocalNameWildcard()) { 609 } else if (node.getLocalName() == null) { 611 return false; 612 } else if (this.getLocalName().equals(node.getLocalName())) { 613 } else { 615 return false; 616 } 617 return true; 618 } 619 } 620 public boolean isTypeTest() { 621 return xnode.getLocalName() == null; 622 } 623 private boolean typeMatch(XNode node) { 624 return ((getTypeTest() == node.getType()) || (getTypeTest() == NodeKind.NODE)); 625 } 626 public byte getTypeTest() { 627 return xnode.getType(); 628 } 629 public String getNamespace() { 630 return xnode.getNamespace(); 631 } 632 public boolean isNamespaceWildcard() { 633 return isNameTest() && (xnode.getNamespace() != null) && xnode.getNamespace().equals(NAME_WILDCARD); 634 } 635 public boolean isNameTest() { 636 return xnode.getLocalName() != null; 637 } 638 639 } 640 | Popular Tags |