1 package net.sf.saxon.om; 2 3 import net.sf.saxon.Configuration; 4 import net.sf.saxon.event.Receiver; 5 import net.sf.saxon.pattern.AnyNodeTest; 6 import net.sf.saxon.pattern.NodeKindTest; 7 import net.sf.saxon.pattern.NodeTest; 8 import net.sf.saxon.trans.XPathException; 9 import net.sf.saxon.type.Type; 10 import net.sf.saxon.value.StringValue; 11 import net.sf.saxon.value.Value; 12 13 import java.util.Set ; 14 import java.util.HashSet ; 15 import java.util.Iterator ; 16 17 22 public class NamespaceIterator implements AxisIterator { 23 24 private NodeInfo element; 25 private NodeTest test; 26 private int index; 27 private int position; 28 private NamespaceNodeImpl next; 29 private NamespaceNodeImpl current; 30 private Iterator nsIterator; 31 private int count; 32 33 34 private static final Integer xmlnscode = new Integer (NamespaceConstant.XML_NAMESPACE_CODE); 35 36 public NamespaceIterator(NodeInfo element, NodeTest test) { 37 this.element = element; 38 this.test = test; 39 if (test instanceof AnyNodeTest || test == NodeKindTest.NAMESPACE) { 40 this.test = null; 41 } 42 index = -1; 43 44 Set undeclared = new HashSet (10); 45 Set declared = new HashSet (10); 46 int[] buffer = new int[10]; 47 NodeInfo node = element; 48 declared.add(xmlnscode); 49 while (node != null && node.getNodeKind() == Type.ELEMENT) { 50 51 int[] nslist = node.getDeclaredNamespaces(buffer); 52 if (nslist != null) { 53 for (int i=0; i<nslist.length; i++) { 54 if (nslist[i] ==-1) { 55 break; 56 } 57 short uriCode = (short)(nslist[i] & 0xffff); 58 short prefixCode = (short)(nslist[i] >> 16); 59 if (uriCode == 0) { 60 undeclared.add(new Integer (prefixCode)); 62 } else { 63 Integer key = new Integer (prefixCode); 64 if (!undeclared.contains(key)) { 65 declared.add(new Integer (nslist[i])); 66 undeclared.add(key); 67 } 68 } 69 } 70 } 71 node = node.getParent(); 72 } 73 count = declared.size(); 74 nsIterator = declared.iterator(); 75 } 76 77 80 81 public void advance() { 82 while (nsIterator.hasNext()) { 83 int nscode = ((Integer )nsIterator.next()).intValue(); 84 next = new NamespaceNodeImpl(element, nscode, index++); 85 if (test == null || test.matches(next)) { 86 return; 87 } 88 } 89 next = null; 90 } 91 92 97 98 public Item next() { 99 if (index == -1) { 100 advance(); 101 index = 0; 102 } 103 current = next; 104 if (current == null) { 105 position = -1; 106 return null; 107 } 108 advance(); 109 position++; 110 return current; 111 } 112 113 119 120 public Item current() { 121 return current; 122 } 123 124 130 131 public int position() { 132 return position; 133 } 134 135 141 142 public SequenceIterator getAnother() { 143 return new NamespaceIterator(element, test); 144 } 145 146 155 156 public int getProperties() { 157 return 0; 158 } 159 160 167 168 public int[] getInScopeNamespaceCodes() { 169 int[] codes = new int[count]; 170 int i = 0; 171 while (nsIterator.hasNext()) { 172 codes[i++] = ((Integer )nsIterator.next()).intValue(); 173 } 174 return codes; 175 } 176 177 180 181 public static class NamespaceNodeImpl implements NodeInfo, FingerprintedNode { 182 183 NodeInfo element; 184 int nscode; 185 int position; 186 int namecode; 187 188 public NamespaceNodeImpl(NodeInfo element, int nscode, int position) { 189 this.element = element; 190 this.nscode = nscode; 191 this.position = position; 192 NamePool pool = element.getNamePool(); 193 String prefix = pool.getPrefixFromNamespaceCode(nscode); 194 if ("".equals(prefix)) { 195 this.namecode = -1; 196 } else { 197 this.namecode = pool.allocate("", "", prefix); 198 } 199 } 200 201 208 209 public int getNodeKind() { 210 return Type.NAMESPACE; 211 } 212 213 223 224 public boolean isSameNodeInfo(NodeInfo other) { 225 if (!(other instanceof NamespaceNodeImpl)) { 226 return false; 227 } 228 return element.isSameNodeInfo(((NamespaceNodeImpl)other).element) && 229 nscode == ((NamespaceNodeImpl)other).nscode; 230 231 } 232 233 241 242 public String getSystemId() { 243 return element.getSystemId(); 244 } 245 246 252 253 public String getBaseURI() { 254 return element.getBaseURI(); 255 } 256 257 263 264 public int getLineNumber() { 265 return element.getLineNumber(); 266 } 267 268 279 280 public int compareOrder(NodeInfo other) { 281 if (other instanceof NamespaceNodeImpl && element.isSameNodeInfo(((NamespaceNodeImpl)other).element)) { 282 int c = position - ((NamespaceNodeImpl)other).position; 284 if (c == 0) return 0; 285 if (c < 0) return -1; 286 return +1; 287 } else { 288 return element.compareOrder(other); 289 } 290 } 291 292 298 299 public String getStringValue() { 300 return element.getNamePool().getURIFromURICode((short)(nscode & 0xffff)); 301 } 302 303 307 308 public CharSequence getStringValueCS() { 309 return getStringValue(); 310 } 311 312 324 325 public int getNameCode() { 326 return namecode; 327 } 328 329 338 339 public int getFingerprint() { 340 if (namecode == -1) { 341 return -1; 342 } 343 return namecode & NamePool.FP_MASK; 344 } 345 346 352 353 public String getLocalPart() { 354 if (namecode == -1) { 355 return ""; 356 } else { 357 return element.getNamePool().getLocalName(namecode); 358 } 359 } 360 361 368 369 public String getURI() { 370 return ""; 371 } 372 373 380 381 public String getDisplayName() { 382 return getLocalPart(); 383 } 384 385 391 392 public String getPrefix() { 393 return ""; 394 } 395 396 399 400 public Configuration getConfiguration() { 401 return element.getConfiguration(); 402 } 403 404 409 410 public NamePool getNamePool() { 411 return element.getNamePool(); 412 } 413 414 422 423 public int getTypeAnnotation() { 424 return -1; 425 } 426 427 432 433 public NodeInfo getParent() { 434 return element; 435 } 436 437 448 449 public AxisIterator iterateAxis(byte axisNumber) { 450 return iterateAxis(axisNumber, new AnyNodeTest()); 451 } 452 453 467 468 public AxisIterator iterateAxis(byte axisNumber, NodeTest nodeTest) { 469 switch (axisNumber) { 470 case Axis.ANCESTOR: 471 return element.iterateAxis(Axis.ANCESTOR_OR_SELF, nodeTest); 472 473 case Axis.ANCESTOR_OR_SELF: 474 if (nodeTest.matches(this)) { 475 return new PrependIterator(this, element.iterateAxis(Axis.ANCESTOR_OR_SELF, nodeTest)); 476 } else { 477 return element.iterateAxis(Axis.ANCESTOR_OR_SELF, nodeTest); 478 } 479 480 case Axis.ATTRIBUTE: 481 case Axis.CHILD: 482 case Axis.DESCENDANT: 483 case Axis.DESCENDANT_OR_SELF: 484 case Axis.FOLLOWING_SIBLING: 485 case Axis.NAMESPACE: 486 case Axis.PRECEDING_SIBLING: 487 return EmptyIterator.getInstance(); 488 489 case Axis.FOLLOWING: 490 return new Navigator.AxisFilter( 491 new Navigator.FollowingEnumeration(this), 492 nodeTest); 493 494 case Axis.PARENT: 495 if (nodeTest.matches(element)) { 496 return SingletonIterator.makeIterator(element); 497 } 498 return EmptyIterator.getInstance(); 499 500 case Axis.PRECEDING: 501 return new Navigator.AxisFilter( 502 new Navigator.PrecedingEnumeration(this, false), 503 nodeTest); 504 505 case Axis.SELF: 506 if (nodeTest.matches(this)) { 507 return SingletonIterator.makeIterator(this); 508 } 509 return EmptyIterator.getInstance(); 510 511 case Axis.PRECEDING_OR_ANCESTOR: 512 return new Navigator.AxisFilter( 513 new Navigator.PrecedingEnumeration(this, true), 514 nodeTest); 515 default: 516 throw new IllegalArgumentException ("Unknown axis number " + axisNumber); 517 } 518 } 519 520 526 527 public String getAttributeValue(int fingerprint) { 528 return null; 529 } 530 531 537 538 public NodeInfo getRoot() { 539 return element.getRoot(); 540 } 541 542 549 550 public DocumentInfo getDocumentRoot() { 551 return element.getDocumentRoot(); 552 } 553 554 561 562 public boolean hasChildNodes() { 563 return false; 564 } 565 566 573 574 public String generateId() { 575 return element.generateId() + 'n' + position; 576 } 577 578 582 583 public int getDocumentNumber() { 584 return element.getDocumentNumber(); 585 } 586 587 603 604 public void copy(Receiver out, int whichNamespaces, boolean copyAnnotations, int locationId) throws XPathException { 605 out.namespace(nscode, 0); 606 } 607 608 615 616 public void sendNamespaceDeclarations(Receiver out, boolean includeAncestors) throws XPathException { 617 618 } 619 620 635 636 public int[] getDeclaredNamespaces(int[] buffer) { 637 return null; 638 } 639 640 650 public void setSystemId(String systemId) { 651 652 } 653 654 662 663 public SequenceIterator getTypedValue() throws XPathException { 664 return SingletonIterator.makeIterator(new StringValue(getStringValueCS())); 665 } 666 667 677 678 public Value atomize() throws XPathException { 679 return new StringValue(getStringValueCS()); 680 } 681 } 682 } 683 | Popular Tags |