1 16 19 package org.apache.xml.utils; 20 21 import java.util.EmptyStackException ; 22 import java.util.Enumeration ; 23 import java.util.Hashtable ; 24 import java.util.Vector ; 25 26 42 public class NamespaceSupport2 43 extends org.xml.sax.helpers.NamespaceSupport 44 { 45 49 private Context2 currentContext; 51 52 56 57 63 public final static String XMLNS = 64 "http://www.w3.org/XML/1998/namespace"; 65 66 67 71 72 75 public NamespaceSupport2 () 76 { 77 reset(); 78 } 79 80 81 85 86 92 public void reset () 93 { 94 currentContext = new Context2(null); 98 currentContext.declarePrefix("xml", XMLNS); 99 } 100 101 102 116 public void pushContext () 117 { 118 123 Context2 parentContext=currentContext; 124 currentContext = parentContext.getChild(); 125 if (currentContext == null){ 126 currentContext = new Context2(parentContext); 127 } 128 else{ 129 currentContext.setParent(parentContext); 132 } 133 } 134 135 136 149 public void popContext () 150 { 151 Context2 parentContext=currentContext.getParent(); 152 if(parentContext==null) 153 throw new EmptyStackException (); 154 else 155 currentContext = parentContext; 156 } 157 158 159 160 164 165 193 public boolean declarePrefix (String prefix, String uri) 194 { 195 if (prefix.equals("xml") || prefix.equals("xmlns")) { 196 return false; 197 } else { 198 currentContext.declarePrefix(prefix, uri); 199 return true; 200 } 201 } 202 203 204 242 public String [] processName (String qName, String [] parts, 243 boolean isAttribute) 244 { 245 String [] name=currentContext.processName(qName, isAttribute); 246 if(name==null) 247 return null; 248 249 System.arraycopy(name,0,parts,0,3); 252 return parts; 253 } 254 255 256 268 public String getURI (String prefix) 269 { 270 return currentContext.getURI(prefix); 271 } 272 273 274 287 public Enumeration getPrefixes () 288 { 289 return currentContext.getPrefixes(); 290 } 291 292 293 311 public String getPrefix (String uri) 312 { 313 return currentContext.getPrefix(uri); 314 } 315 316 317 337 public Enumeration getPrefixes (String uri) 338 { 339 return new PrefixForUriEnumerator(this,uri,getPrefixes()); 350 } 351 352 353 365 public Enumeration getDeclaredPrefixes () 366 { 367 return currentContext.getDeclaredPrefixes(); 368 } 369 370 371 372 } 373 374 380 386 class PrefixForUriEnumerator implements Enumeration 387 { 388 private Enumeration allPrefixes; 389 private String uri; 390 private String lookahead=null; 391 private NamespaceSupport2 nsup; 392 393 PrefixForUriEnumerator(NamespaceSupport2 nsup,String uri, Enumeration allPrefixes) 396 { 397 this.nsup=nsup; 398 this.uri=uri; 399 this.allPrefixes=allPrefixes; 400 } 401 402 public boolean hasMoreElements() 403 { 404 if(lookahead!=null) 405 return true; 406 407 while(allPrefixes.hasMoreElements()) 408 { 409 String prefix=(String )allPrefixes.nextElement(); 410 if(uri.equals(nsup.getURI(prefix))) 411 { 412 lookahead=prefix; 413 return true; 414 } 415 } 416 return false; 417 } 418 419 public Object nextElement() 420 { 421 if(hasMoreElements()) 422 { 423 String tmp=lookahead; 424 lookahead=null; 425 return tmp; 426 } 427 else 428 throw new java.util.NoSuchElementException (); 429 } 430 } 431 432 439 final class Context2 { 440 441 445 448 private final static Enumeration EMPTY_ENUMERATION = 449 new Vector ().elements(); 450 451 455 Hashtable prefixTable; 456 Hashtable uriTable; 457 Hashtable elementNameTable; 458 Hashtable attributeNameTable; 459 String defaultNS = null; 460 461 465 private Vector declarations = null; 466 private boolean tablesDirty = false; 467 private Context2 parent = null; 468 private Context2 child = null; 469 470 473 Context2 (Context2 parent) 474 { 475 if(parent==null) 476 { 477 prefixTable = new Hashtable (); 478 uriTable = new Hashtable (); 479 elementNameTable=null; 480 attributeNameTable=null; 481 } 482 else 483 setParent(parent); 484 } 485 486 487 491 Context2 getChild() 492 { 493 return child; 494 } 495 496 500 Context2 getParent() 501 { 502 return parent; 503 } 504 505 512 void setParent (Context2 parent) 513 { 514 this.parent = parent; 515 parent.child = this; declarations = null; 517 prefixTable = parent.prefixTable; 518 uriTable = parent.uriTable; 519 elementNameTable = parent.elementNameTable; 520 attributeNameTable = parent.attributeNameTable; 521 defaultNS = parent.defaultNS; 522 tablesDirty = false; 523 } 524 525 526 533 void declarePrefix (String prefix, String uri) 534 { 535 if (!tablesDirty) { 537 copyTables(); 538 } 539 if (declarations == null) { 540 declarations = new Vector (); 541 } 542 543 prefix = prefix.intern(); 544 uri = uri.intern(); 545 if ("".equals(prefix)) { 546 if ("".equals(uri)) { 547 defaultNS = null; 548 } else { 549 defaultNS = uri; 550 } 551 } else { 552 prefixTable.put(prefix, uri); 553 uriTable.put(uri, prefix); } 555 declarations.addElement(prefix); 556 } 557 558 559 570 String [] processName (String qName, boolean isAttribute) 571 { 572 String name[]; 573 Hashtable table; 574 575 if (isAttribute) { 577 if(elementNameTable==null) 578 elementNameTable=new Hashtable (); 579 table = elementNameTable; 580 } else { 581 if(attributeNameTable==null) 582 attributeNameTable=new Hashtable (); 583 table = attributeNameTable; 584 } 585 586 name = (String [])table.get(qName); 590 if (name != null) { 591 return name; 592 } 593 594 name = new String [3]; 597 int index = qName.indexOf(':'); 598 599 600 if (index == -1) { 602 if (isAttribute || defaultNS == null) { 603 name[0] = ""; 604 } else { 605 name[0] = defaultNS; 606 } 607 name[1] = qName.intern(); 608 name[2] = name[1]; 609 } 610 611 else { 613 String prefix = qName.substring(0, index); 614 String local = qName.substring(index+1); 615 String uri; 616 if ("".equals(prefix)) { 617 uri = defaultNS; 618 } else { 619 uri = (String )prefixTable.get(prefix); 620 } 621 if (uri == null) { 622 return null; 623 } 624 name[0] = uri; 625 name[1] = local.intern(); 626 name[2] = qName.intern(); 627 } 628 629 table.put(name[2], name); 631 tablesDirty = true; 632 return name; 633 } 634 635 636 644 String getURI (String prefix) 645 { 646 if ("".equals(prefix)) { 647 return defaultNS; 648 } else if (prefixTable == null) { 649 return null; 650 } else { 651 return (String )prefixTable.get(prefix); 652 } 653 } 654 655 656 666 String getPrefix (String uri) 667 { 668 if (uriTable == null) { 669 return null; 670 } else { 671 return (String )uriTable.get(uri); 672 } 673 } 674 675 676 682 Enumeration getDeclaredPrefixes () 683 { 684 if (declarations == null) { 685 return EMPTY_ENUMERATION; 686 } else { 687 return declarations.elements(); 688 } 689 } 690 691 692 701 Enumeration getPrefixes () 702 { 703 if (prefixTable == null) { 704 return EMPTY_ENUMERATION; 705 } else { 706 return prefixTable.keys(); 707 } 708 } 709 710 714 726 private void copyTables () 727 { 728 prefixTable = (Hashtable )prefixTable.clone(); 730 uriTable = (Hashtable )uriTable.clone(); 731 732 if(elementNameTable!=null) 738 elementNameTable=new Hashtable (); 739 if(attributeNameTable!=null) 740 attributeNameTable=new Hashtable (); 741 tablesDirty = true; 742 } 743 744 } 745 746 747 | Popular Tags |