1 7 package org.xml.sax.helpers; 8 9 import java.io.IOException ; 10 import java.util.Enumeration ; 11 import java.util.Vector ; 12 13 import org.xml.sax.Parser ; import org.xml.sax.InputSource ; 15 import org.xml.sax.Locator ; 16 import org.xml.sax.AttributeList ; import org.xml.sax.EntityResolver ; 18 import org.xml.sax.DTDHandler ; 19 import org.xml.sax.DocumentHandler ; import org.xml.sax.ErrorHandler ; 21 import org.xml.sax.SAXException ; 22 import org.xml.sax.SAXParseException ; 23 24 import org.xml.sax.XMLReader ; 25 import org.xml.sax.Attributes ; 26 import org.xml.sax.ContentHandler ; 27 import org.xml.sax.SAXNotRecognizedException ; 28 import org.xml.sax.SAXNotSupportedException ; 29 30 31 57 public class ParserAdapter implements XMLReader , DocumentHandler 58 { 59 60 61 65 66 76 public ParserAdapter () 77 throws SAXException 78 { 79 super(); 80 81 String driver = System.getProperty("org.xml.sax.parser"); 82 83 try { 84 setup(ParserFactory.makeParser()); 85 } catch (ClassNotFoundException e1) { 86 throw new 87 SAXException ("Cannot find SAX1 driver class " + 88 driver, e1); 89 } catch (IllegalAccessException e2) { 90 throw new 91 SAXException ("SAX1 driver class " + 92 driver + 93 " found but cannot be loaded", e2); 94 } catch (InstantiationException e3) { 95 throw new 96 SAXException ("SAX1 driver class " + 97 driver + 98 " loaded but cannot be instantiated", e3); 99 } catch (ClassCastException e4) { 100 throw new 101 SAXException ("SAX1 driver class " + 102 driver + 103 " does not implement org.xml.sax.Parser"); 104 } catch (NullPointerException e5) { 105 throw new 106 SAXException ("System property org.xml.sax.parser not specified"); 107 } 108 } 109 110 111 122 public ParserAdapter (Parser parser) 123 { 124 super(); 125 setup(parser); 126 } 127 128 129 136 private void setup (Parser parser) 137 { 138 if (parser == null) { 139 throw new 140 NullPointerException ("Parser argument must not be null"); 141 } 142 this.parser = parser; 143 atts = new AttributesImpl (); 144 nsSupport = new NamespaceSupport (); 145 attAdapter = new AttributeListAdapter(); 146 } 147 148 149 150 154 155 private final static String FEATURES = "http://xml.org/sax/features/"; 159 private final static String NAMESPACES = FEATURES + "namespaces"; 160 private final static String NAMESPACE_PREFIXES = FEATURES + "namespace-prefixes"; 161 private final static String XMLNS_URIs = FEATURES + "xmlns-uris"; 162 163 164 178 public void setFeature (String name, boolean value) 179 throws SAXNotRecognizedException , SAXNotSupportedException 180 { 181 if (name.equals(NAMESPACES)) { 182 checkNotParsing("feature", name); 183 namespaces = value; 184 if (!namespaces && !prefixes) { 185 prefixes = true; 186 } 187 } else if (name.equals(NAMESPACE_PREFIXES)) { 188 checkNotParsing("feature", name); 189 prefixes = value; 190 if (!prefixes && !namespaces) { 191 namespaces = true; 192 } 193 } else if (name.equals(XMLNS_URIs)) { 194 checkNotParsing("feature", name); 195 uris = value; 196 } else { 197 throw new SAXNotRecognizedException ("Feature: " + name); 198 } 199 } 200 201 202 216 public boolean getFeature (String name) 217 throws SAXNotRecognizedException , SAXNotSupportedException 218 { 219 if (name.equals(NAMESPACES)) { 220 return namespaces; 221 } else if (name.equals(NAMESPACE_PREFIXES)) { 222 return prefixes; 223 } else if (name.equals(XMLNS_URIs)) { 224 return uris; 225 } else { 226 throw new SAXNotRecognizedException ("Feature: " + name); 227 } 228 } 229 230 231 244 public void setProperty (String name, Object value) 245 throws SAXNotRecognizedException , SAXNotSupportedException 246 { 247 throw new SAXNotRecognizedException ("Property: " + name); 248 } 249 250 251 264 public Object getProperty (String name) 265 throws SAXNotRecognizedException , SAXNotSupportedException 266 { 267 throw new SAXNotRecognizedException ("Property: " + name); 268 } 269 270 271 277 public void setEntityResolver (EntityResolver resolver) 278 { 279 entityResolver = resolver; 280 } 281 282 283 289 public EntityResolver getEntityResolver () 290 { 291 return entityResolver; 292 } 293 294 295 301 public void setDTDHandler (DTDHandler handler) 302 { 303 dtdHandler = handler; 304 } 305 306 307 313 public DTDHandler getDTDHandler () 314 { 315 return dtdHandler; 316 } 317 318 319 325 public void setContentHandler (ContentHandler handler) 326 { 327 contentHandler = handler; 328 } 329 330 331 337 public ContentHandler getContentHandler () 338 { 339 return contentHandler; 340 } 341 342 343 349 public void setErrorHandler (ErrorHandler handler) 350 { 351 errorHandler = handler; 352 } 353 354 355 361 public ErrorHandler getErrorHandler () 362 { 363 return errorHandler; 364 } 365 366 367 378 public void parse (String systemId) 379 throws IOException , SAXException 380 { 381 parse(new InputSource (systemId)); 382 } 383 384 385 396 public void parse (InputSource input) 397 throws IOException , SAXException 398 { 399 if (parsing) { 400 throw new SAXException ("Parser is already in use"); 401 } 402 setupParser(); 403 parsing = true; 404 try { 405 parser.parse(input); 406 } finally { 407 parsing = false; 408 } 409 parsing = false; 410 } 411 412 413 414 418 419 426 public void setDocumentLocator (Locator locator) 427 { 428 this.locator = locator; 429 if (contentHandler != null) { 430 contentHandler.setDocumentLocator(locator); 431 } 432 } 433 434 435 443 public void startDocument () 444 throws SAXException 445 { 446 if (contentHandler != null) { 447 contentHandler.startDocument(); 448 } 449 } 450 451 452 460 public void endDocument () 461 throws SAXException 462 { 463 if (contentHandler != null) { 464 contentHandler.endDocument(); 465 } 466 } 467 468 469 480 public void startElement (String qName, AttributeList qAtts) 481 throws SAXException 482 { 483 Vector exceptions = null; 488 489 if (!namespaces) { 492 if (contentHandler != null) { 493 attAdapter.setAttributeList(qAtts); 494 contentHandler.startElement("", "", qName.intern(), 495 attAdapter); 496 } 497 return; 498 } 499 500 501 nsSupport.pushContext(); 503 int length = qAtts.getLength(); 504 505 for (int i = 0; i < length; i++) { 507 String attQName = qAtts.getName(i); 508 509 if (!attQName.startsWith("xmlns")) 510 continue; 511 String prefix; 513 int n = attQName.indexOf(':'); 514 515 if (n == -1 && attQName.length () == 5) { 517 prefix = ""; 518 } else if (n != 5) { 519 continue; 522 } else prefix = attQName.substring(n+1); 524 525 String value = qAtts.getValue(i); 526 if (!nsSupport.declarePrefix(prefix, value)) { 527 reportError("Illegal Namespace prefix: " + prefix); 528 continue; 529 } 530 if (contentHandler != null) 531 contentHandler.startPrefixMapping(prefix, value); 532 } 533 534 atts.clear(); 538 for (int i = 0; i < length; i++) { 539 String attQName = qAtts.getName(i); 540 String type = qAtts.getType(i); 541 String value = qAtts.getValue(i); 542 543 if (attQName.startsWith("xmlns")) { 545 String prefix; 546 int n = attQName.indexOf(':'); 547 548 if (n == -1 && attQName.length () == 5) { 549 prefix = ""; 550 } else if (n != 5) { 551 prefix = null; 554 } else { 555 prefix = attQName.substring(6); 556 } 557 if (prefix != null) { 559 if (prefixes) { 560 if (uris) 561 atts.addAttribute (nsSupport.XMLNS, prefix, 565 attQName.intern(), type, value); 566 else 567 atts.addAttribute ("", "", 568 attQName.intern(), type, value); 569 } 570 continue; 571 } 572 } 573 574 try { 576 String attName[] = processName(attQName, true, true); 577 atts.addAttribute(attName[0], attName[1], attName[2], 578 type, value); 579 } catch (SAXException e) { 580 if (exceptions == null) 581 exceptions = new Vector (); 582 exceptions.addElement(e); 583 atts.addAttribute("", attQName, attQName, type, value); 584 } 585 } 586 587 if (exceptions != null && errorHandler != null) { 589 for (int i = 0; i < exceptions.size(); i++) 590 errorHandler.error((SAXParseException ) 591 (exceptions.elementAt(i))); 592 } 593 594 if (contentHandler != null) { 596 String name[] = processName(qName, false, false); 597 contentHandler.startElement(name[0], name[1], name[2], atts); 598 } 599 } 600 601 602 611 public void endElement (String qName) 612 throws SAXException 613 { 614 if (!namespaces) { 617 if (contentHandler != null) { 618 contentHandler.endElement("", "", qName.intern()); 619 } 620 return; 621 } 622 623 String names[] = processName(qName, false, false); 625 if (contentHandler != null) { 626 contentHandler.endElement(names[0], names[1], names[2]); 627 Enumeration prefixes = nsSupport.getDeclaredPrefixes(); 628 while (prefixes.hasMoreElements()) { 629 String prefix = (String )prefixes.nextElement(); 630 contentHandler.endPrefixMapping(prefix); 631 } 632 } 633 nsSupport.popContext(); 634 } 635 636 637 648 public void characters (char ch[], int start, int length) 649 throws SAXException 650 { 651 if (contentHandler != null) { 652 contentHandler.characters(ch, start, length); 653 } 654 } 655 656 657 668 public void ignorableWhitespace (char ch[], int start, int length) 669 throws SAXException 670 { 671 if (contentHandler != null) { 672 contentHandler.ignorableWhitespace(ch, start, length); 673 } 674 } 675 676 677 687 public void processingInstruction (String target, String data) 688 throws SAXException 689 { 690 if (contentHandler != null) { 691 contentHandler.processingInstruction(target, data); 692 } 693 } 694 695 696 697 701 702 705 private void setupParser () 706 { 707 if (!prefixes && !namespaces) 709 throw new IllegalStateException (); 710 711 nsSupport.reset(); 712 if (uris) 713 nsSupport.setNamespaceDeclUris (true); 714 715 if (entityResolver != null) { 716 parser.setEntityResolver(entityResolver); 717 } 718 if (dtdHandler != null) { 719 parser.setDTDHandler(dtdHandler); 720 } 721 if (errorHandler != null) { 722 parser.setErrorHandler(errorHandler); 723 } 724 parser.setDocumentHandler(this); 725 locator = null; 726 } 727 728 729 742 private String [] processName (String qName, boolean isAttribute, 743 boolean useException) 744 throws SAXException 745 { 746 String parts[] = nsSupport.processName(qName, nameParts, 747 isAttribute); 748 if (parts == null) { 749 if (useException) 750 throw makeException("Undeclared prefix: " + qName); 751 reportError("Undeclared prefix: " + qName); 752 parts = new String [3]; 753 parts[0] = parts[1] = ""; 754 parts[2] = qName.intern(); 755 } 756 return parts; 757 } 758 759 760 767 void reportError (String message) 768 throws SAXException 769 { 770 if (errorHandler != null) 771 errorHandler.error(makeException(message)); 772 } 773 774 775 780 private SAXParseException makeException (String message) 781 { 782 if (locator != null) { 783 return new SAXParseException (message, locator); 784 } else { 785 return new SAXParseException (message, null, null, -1, -1); 786 } 787 } 788 789 790 801 private void checkNotParsing (String type, String name) 802 throws SAXNotSupportedException 803 { 804 if (parsing) { 805 throw new SAXNotSupportedException ("Cannot change " + 806 type + ' ' + 807 name + " while parsing"); 808 809 } 810 } 811 812 813 814 818 private NamespaceSupport nsSupport; 819 private AttributeListAdapter attAdapter; 820 821 private boolean parsing = false; 822 private String nameParts[] = new String [3]; 823 824 private Parser parser = null; 825 826 private AttributesImpl atts = null; 827 828 private boolean namespaces = true; 830 private boolean prefixes = false; 831 private boolean uris = false; 832 833 835 Locator locator; 837 838 EntityResolver entityResolver = null; 839 DTDHandler dtdHandler = null; 840 ContentHandler contentHandler = null; 841 ErrorHandler errorHandler = null; 842 843 844 845 849 850 861 final class AttributeListAdapter implements Attributes 862 { 863 864 867 AttributeListAdapter () 868 { 869 } 870 871 872 880 void setAttributeList (AttributeList qAtts) 881 { 882 this.qAtts = qAtts; 883 } 884 885 886 892 public int getLength () 893 { 894 return qAtts.getLength(); 895 } 896 897 898 905 public String getURI (int i) 906 { 907 return ""; 908 } 909 910 911 918 public String getLocalName (int i) 919 { 920 return ""; 921 } 922 923 924 930 public String getQName (int i) 931 { 932 return qAtts.getName(i).intern(); 933 } 934 935 936 942 public String getType (int i) 943 { 944 return qAtts.getType(i).intern(); 945 } 946 947 948 954 public String getValue (int i) 955 { 956 return qAtts.getValue(i); 957 } 958 959 960 968 public int getIndex (String uri, String localName) 969 { 970 return -1; 971 } 972 973 974 981 public int getIndex (String qName) 982 { 983 int max = atts.getLength(); 984 for (int i = 0; i < max; i++) { 985 if (qAtts.getName(i).equals(qName)) { 986 return i; 987 } 988 } 989 return -1; 990 } 991 992 993 1000 public String getType (String uri, String localName) 1001 { 1002 return null; 1003 } 1004 1005 1006 1012 public String getType (String qName) 1013 { 1014 return qAtts.getType(qName).intern(); 1015 } 1016 1017 1018 1025 public String getValue (String uri, String localName) 1026 { 1027 return null; 1028 } 1029 1030 1031 1037 public String getValue (String qName) 1038 { 1039 return qAtts.getValue(qName); 1040 } 1041 1042 private AttributeList qAtts; 1043 } 1044} 1045 1046 | Popular Tags |