1 7 package org.xml.sax.helpers; 8 9 import java.util.EmptyStackException ; 10 import java.util.Enumeration ; 11 import java.util.Hashtable ; 12 import java.util.Vector ; 13 14 15 72 public class NamespaceSupport 73 { 74 75 76 80 81 89 public final static String XMLNS = 90 "http://www.w3.org/XML/1998/namespace"; 91 92 93 109 public final static String NSDECL = 110 "http://www.w3.org/xmlns/2000/"; 111 112 113 116 private final static Enumeration EMPTY_ENUMERATION = 117 new Vector ().elements(); 118 119 120 124 125 128 public NamespaceSupport () 129 { 130 reset(); 131 } 132 133 134 135 139 140 151 public void reset () 152 { 153 contexts = new Context[32]; 154 namespaceDeclUris = false; 155 contextPos = 0; 156 contexts[contextPos] = currentContext = new Context(); 157 currentContext.declarePrefix("xml", XMLNS); 158 } 159 160 161 197 public void pushContext () 198 { 199 int max = contexts.length; 200 201 contextPos++; 202 203 if (contextPos >= max) { 205 Context newContexts[] = new Context[max*2]; 206 System.arraycopy(contexts, 0, newContexts, 0, max); 207 max *= 2; 208 contexts = newContexts; 209 } 210 211 currentContext = contexts[contextPos]; 213 if (currentContext == null) { 214 contexts[contextPos] = currentContext = new Context(); 215 } 216 217 if (contextPos > 0) { 219 currentContext.setParent(contexts[contextPos - 1]); 220 } 221 } 222 223 224 237 public void popContext () 238 { 239 contexts[contextPos].clear(); 240 contextPos--; 241 if (contextPos < 0) { 242 throw new EmptyStackException (); 243 } 244 currentContext = contexts[contextPos]; 245 } 246 247 248 249 253 254 287 public boolean declarePrefix (String prefix, String uri) 288 { 289 if (prefix.equals("xml") || prefix.equals("xmlns")) { 290 return false; 291 } else { 292 currentContext.declarePrefix(prefix, uri); 293 return true; 294 } 295 } 296 297 298 338 public String [] processName (String qName, String parts[], 339 boolean isAttribute) 340 { 341 String myParts[] = currentContext.processName(qName, isAttribute); 342 if (myParts == null) { 343 return null; 344 } else { 345 parts[0] = myParts[0]; 346 parts[1] = myParts[1]; 347 parts[2] = myParts[2]; 348 return parts; 349 } 350 } 351 352 353 365 public String getURI (String prefix) 366 { 367 return currentContext.getURI(prefix); 368 } 369 370 371 385 public Enumeration getPrefixes () 386 { 387 return currentContext.getPrefixes(); 388 } 389 390 391 410 public String getPrefix (String uri) 411 { 412 return currentContext.getPrefix(uri); 413 } 414 415 416 439 public Enumeration getPrefixes (String uri) 440 { 441 Vector prefixes = new Vector (); 442 Enumeration allPrefixes = getPrefixes(); 443 while (allPrefixes.hasMoreElements()) { 444 String prefix = (String )allPrefixes.nextElement(); 445 if (uri.equals(getURI(prefix))) { 446 prefixes.addElement(prefix); 447 } 448 } 449 return prefixes.elements(); 450 } 451 452 453 465 public Enumeration getDeclaredPrefixes () 466 { 467 return currentContext.getDeclaredPrefixes(); 468 } 469 470 481 public void setNamespaceDeclUris (boolean value) 482 { 483 if (contextPos != 0) 484 throw new IllegalStateException (); 485 if (value == namespaceDeclUris) 486 return; 487 namespaceDeclUris = value; 488 if (value) 489 currentContext.declarePrefix ("xmlns", NSDECL); 490 else { 491 contexts[contextPos] = currentContext = new Context(); 492 currentContext.declarePrefix("xml", XMLNS); 493 } 494 } 495 496 502 public boolean isNamespaceDeclUris () 503 { return namespaceDeclUris; } 504 505 506 507 511 private Context contexts[]; 512 private Context currentContext; 513 private int contextPos; 514 private boolean namespaceDeclUris; 515 516 517 521 533 final class Context { 534 535 538 Context () 539 { 540 copyTables(); 541 } 542 543 544 551 void setParent (Context parent) 552 { 553 this.parent = parent; 554 declarations = null; 555 prefixTable = parent.prefixTable; 556 uriTable = parent.uriTable; 557 elementNameTable = parent.elementNameTable; 558 attributeNameTable = parent.attributeNameTable; 559 defaultNS = parent.defaultNS; 560 declSeen = false; 561 } 562 563 569 void clear () 570 { 571 parent = null; 572 prefixTable = null; 573 uriTable = null; 574 elementNameTable = null; 575 attributeNameTable = null; 576 defaultNS = null; 577 } 578 579 580 587 void declarePrefix (String prefix, String uri) 588 { 589 if (!declSeen) { 594 copyTables(); 595 } 596 if (declarations == null) { 597 declarations = new Vector (); 598 } 599 600 prefix = prefix.intern(); 601 uri = uri.intern(); 602 if ("".equals(prefix)) { 603 if ("".equals(uri)) { 604 defaultNS = null; 605 } else { 606 defaultNS = uri; 607 } 608 } else { 609 prefixTable.put(prefix, uri); 610 uriTable.put(uri, prefix); } 612 declarations.addElement(prefix); 613 } 614 615 616 627 String [] processName (String qName, boolean isAttribute) 628 { 629 String name[]; 630 Hashtable table; 631 632 if (isAttribute) { 634 table = attributeNameTable; 635 } else { 636 table = elementNameTable; 637 } 638 639 name = (String [])table.get(qName); 643 if (name != null) { 644 return name; 645 } 646 647 name = new String [3]; 652 name[2] = qName.intern(); 653 int index = qName.indexOf(':'); 654 655 656 if (index == -1) { 658 if (isAttribute) { 659 if (qName == "xmlns" && namespaceDeclUris) 660 name[0] = NSDECL; 661 else 662 name[0] = ""; 663 } else if (defaultNS == null) { 664 name[0] = ""; 665 } else { 666 name[0] = defaultNS; 667 } 668 name[1] = name[2]; 669 } 670 671 else { 673 String prefix = qName.substring(0, index); 674 String local = qName.substring(index+1); 675 String uri; 676 if ("".equals(prefix)) { 677 uri = defaultNS; 678 } else { 679 uri = (String )prefixTable.get(prefix); 680 } 681 if (uri == null 682 || (!isAttribute && "xmlns".equals (prefix))) { 683 return null; 684 } 685 name[0] = uri; 686 name[1] = local.intern(); 687 } 688 689 table.put(name[2], name); 692 return name; 693 } 694 695 696 704 String getURI (String prefix) 705 { 706 if ("".equals(prefix)) { 707 return defaultNS; 708 } else if (prefixTable == null) { 709 return null; 710 } else { 711 return (String )prefixTable.get(prefix); 712 } 713 } 714 715 716 726 String getPrefix (String uri) 727 { 728 if (uriTable == null) { 729 return null; 730 } else { 731 return (String )uriTable.get(uri); 732 } 733 } 734 735 736 742 Enumeration getDeclaredPrefixes () 743 { 744 if (declarations == null) { 745 return EMPTY_ENUMERATION; 746 } else { 747 return declarations.elements(); 748 } 749 } 750 751 752 761 Enumeration getPrefixes () 762 { 763 if (prefixTable == null) { 764 return EMPTY_ENUMERATION; 765 } else { 766 return prefixTable.keys(); 767 } 768 } 769 770 771 772 776 777 783 private void copyTables () 784 { 785 if (prefixTable != null) { 786 prefixTable = (Hashtable )prefixTable.clone(); 787 } else { 788 prefixTable = new Hashtable (); 789 } 790 if (uriTable != null) { 791 uriTable = (Hashtable )uriTable.clone(); 792 } else { 793 uriTable = new Hashtable (); 794 } 795 elementNameTable = new Hashtable (); 796 attributeNameTable = new Hashtable (); 797 declSeen = true; 798 } 799 800 801 802 806 Hashtable prefixTable; 807 Hashtable uriTable; 808 Hashtable elementNameTable; 809 Hashtable attributeNameTable; 810 String defaultNS = null; 811 812 813 814 818 private Vector declarations = null; 819 private boolean declSeen = false; 820 private Context parent = null; 821 } 822 } 823 824 | Popular Tags |