1 16 17 package org.apache.xerces.parsers; 18 19 import java.io.StringReader ; 20 import java.util.Locale ; 21 import java.util.Stack ; 22 import java.util.StringTokenizer ; 23 import java.util.Vector ; 24 25 import org.apache.xerces.dom.DOMErrorImpl; 26 import org.apache.xerces.dom.DOMMessageFormatter; 27 import org.apache.xerces.dom.DOMStringListImpl; 28 import org.apache.xerces.impl.Constants; 29 import org.apache.xerces.util.DOMEntityResolverWrapper; 30 import org.apache.xerces.util.DOMErrorHandlerWrapper; 31 import org.apache.xerces.util.SymbolTable; 32 import org.apache.xerces.util.XMLSymbols; 33 import org.apache.xerces.xni.Augmentations; 34 import org.apache.xerces.xni.NamespaceContext; 35 import org.apache.xerces.xni.QName; 36 import org.apache.xerces.xni.XMLAttributes; 37 import org.apache.xerces.xni.XMLDTDContentModelHandler; 38 import org.apache.xerces.xni.XMLDTDHandler; 39 import org.apache.xerces.xni.XMLDocumentHandler; 40 import org.apache.xerces.xni.XMLLocator; 41 import org.apache.xerces.xni.XMLResourceIdentifier; 42 import org.apache.xerces.xni.XMLString; 43 import org.apache.xerces.xni.XNIException; 44 import org.apache.xerces.xni.grammars.XMLGrammarPool; 45 import org.apache.xerces.xni.parser.XMLConfigurationException; 46 import org.apache.xerces.xni.parser.XMLDTDContentModelSource; 47 import org.apache.xerces.xni.parser.XMLDTDSource; 48 import org.apache.xerces.xni.parser.XMLDocumentSource; 49 import org.apache.xerces.xni.parser.XMLEntityResolver; 50 import org.apache.xerces.xni.parser.XMLInputSource; 51 import org.apache.xerces.xni.parser.XMLParseException; 52 import org.apache.xerces.xni.parser.XMLParserConfiguration; 53 import org.w3c.dom.DOMConfiguration ; 54 import org.w3c.dom.DOMError ; 55 import org.w3c.dom.DOMErrorHandler ; 56 import org.w3c.dom.DOMException ; 57 import org.w3c.dom.DOMStringList ; 58 import org.w3c.dom.Document ; 59 import org.w3c.dom.Node ; 60 import org.w3c.dom.ls.LSException ; 61 import org.w3c.dom.ls.LSInput ; 62 import org.w3c.dom.ls.LSParser ; 63 import org.w3c.dom.ls.LSParserFilter ; 64 import org.w3c.dom.ls.LSResourceResolver ; 65 import org.xml.sax.SAXException ; 66 67 68 78 79 80 public class DOMParserImpl 81 extends AbstractDOMParser implements LSParser , DOMConfiguration { 82 83 84 85 87 88 protected static final String NAMESPACES = 89 Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE; 90 91 92 protected static final String VALIDATION_FEATURE = 93 Constants.SAX_FEATURE_PREFIX+Constants.VALIDATION_FEATURE; 94 95 96 protected static final String XMLSCHEMA = 97 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_VALIDATION_FEATURE; 98 99 100 protected static final String XMLSCHEMA_FULL_CHECKING = 101 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_FULL_CHECKING; 102 103 104 protected static final String DYNAMIC_VALIDATION = 105 Constants.XERCES_FEATURE_PREFIX + Constants.DYNAMIC_VALIDATION_FEATURE; 106 107 108 protected static final String NORMALIZE_DATA = 109 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_NORMALIZED_VALUE; 110 111 112 protected static final String DISALLOW_DOCTYPE_DECL_FEATURE = 113 Constants.XERCES_FEATURE_PREFIX + Constants.DISALLOW_DOCTYPE_DECL_FEATURE; 114 115 protected static final String SYMBOL_TABLE = 117 Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY; 118 119 protected static final String PSVI_AUGMENT = 120 Constants.XERCES_FEATURE_PREFIX +Constants.SCHEMA_AUGMENT_PSVI; 121 122 123 127 128 protected boolean fNamespaceDeclarations = true; 129 130 protected String fSchemaType = null; 133 134 protected boolean fBusy = false; 135 136 private boolean abortNow = false; 137 138 private Thread currentThread; 139 140 protected final static boolean DEBUG = false; 141 142 private Vector fSchemaLocations = new Vector (); 143 private String fSchemaLocation = null; 144 private DOMStringList fRecognizedParameters; 145 146 private AbortHandler abortHandler = new AbortHandler(); 147 148 152 155 public DOMParserImpl (String configuration, String schemaType) { 156 this ( 157 (XMLParserConfiguration) ObjectFactory.createObject ( 158 "org.apache.xerces.xni.parser.XMLParserConfiguration", 159 configuration)); 160 if (schemaType != null) { 161 if (schemaType.equals (Constants.NS_DTD)) { 162 fConfiguration.setProperty ( 168 Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_LANGUAGE, 169 Constants.NS_DTD); 170 fSchemaType = Constants.NS_DTD; 171 } 172 else if (schemaType.equals (Constants.NS_XMLSCHEMA)) { 173 fConfiguration.setProperty ( 175 Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_LANGUAGE, 176 Constants.NS_XMLSCHEMA); 177 } 178 } 179 180 } 181 182 185 public DOMParserImpl (XMLParserConfiguration config) { 186 super (config); 187 188 final String [] domRecognizedFeatures = { 190 Constants.DOM_CANONICAL_FORM, 191 Constants.DOM_CDATA_SECTIONS, 192 Constants.DOM_CHARSET_OVERRIDES_XML_ENCODING, 193 Constants.DOM_INFOSET, 194 Constants.DOM_NAMESPACE_DECLARATIONS, 195 Constants.DOM_SPLIT_CDATA, 196 Constants.DOM_SUPPORTED_MEDIATYPES_ONLY, 197 Constants.DOM_CERTIFIED, 198 Constants.DOM_WELLFORMED, 199 Constants.DOM_IGNORE_UNKNOWN_CHARACTER_DENORMALIZATIONS, 200 }; 201 202 fConfiguration.addRecognizedFeatures (domRecognizedFeatures); 203 204 fConfiguration.setFeature (DEFER_NODE_EXPANSION, false); 206 207 216 fConfiguration.setFeature(Constants.DOM_NAMESPACE_DECLARATIONS, true); 217 fConfiguration.setFeature(Constants.DOM_WELLFORMED, true); 218 fConfiguration.setFeature(INCLUDE_COMMENTS_FEATURE, true); 219 fConfiguration.setFeature(INCLUDE_IGNORABLE_WHITESPACE, true); 220 fConfiguration.setFeature(NAMESPACES, true); 221 222 fConfiguration.setFeature(DYNAMIC_VALIDATION, false); 223 fConfiguration.setFeature(CREATE_ENTITY_REF_NODES, false); 224 fConfiguration.setFeature(CREATE_CDATA_NODES_FEATURE, false); 225 226 fConfiguration.setFeature (Constants.DOM_CANONICAL_FORM, false); 228 fConfiguration.setFeature (Constants.DOM_CHARSET_OVERRIDES_XML_ENCODING, true); 229 fConfiguration.setFeature (Constants.DOM_SPLIT_CDATA, true); 230 fConfiguration.setFeature (Constants.DOM_SUPPORTED_MEDIATYPES_ONLY, false); 231 fConfiguration.setFeature (Constants.DOM_IGNORE_UNKNOWN_CHARACTER_DENORMALIZATIONS, true); 232 233 fConfiguration.setFeature (Constants.DOM_CERTIFIED, true); 236 237 try { 241 fConfiguration.setFeature ( NORMALIZE_DATA, false ); 242 } 243 catch (XMLConfigurationException exc) {} 244 245 } 247 250 public DOMParserImpl (SymbolTable symbolTable) { 251 this ( 252 (XMLParserConfiguration) ObjectFactory.createObject ( 253 "org.apache.xerces.xni.parser.XMLParserConfiguration", 254 "org.apache.xerces.parsers.XIncludeAwareParserConfiguration")); 255 fConfiguration.setProperty ( 256 Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY, 257 symbolTable); 258 } 260 261 265 public DOMParserImpl (SymbolTable symbolTable, XMLGrammarPool grammarPool) { 266 this ( 267 (XMLParserConfiguration) ObjectFactory.createObject ( 268 "org.apache.xerces.xni.parser.XMLParserConfiguration", 269 "org.apache.xerces.parsers.XIncludeAwareParserConfiguration")); 270 fConfiguration.setProperty ( 271 Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY, 272 symbolTable); 273 fConfiguration.setProperty ( 274 Constants.XERCES_PROPERTY_PREFIX 275 + Constants.XMLGRAMMAR_POOL_PROPERTY, 276 grammarPool); 277 } 278 279 284 public void reset () { 285 super.reset (); 286 287 fNamespaceDeclarations = 289 fConfiguration.getFeature(Constants.DOM_NAMESPACE_DECLARATIONS); 290 291 if (fSkippedElemStack!=null) { 293 fSkippedElemStack.removeAllElements (); 294 } 295 fSchemaLocations.clear (); 296 fRejectedElement.clear (); 297 fFilterReject = false; 298 fSchemaType = null; 299 300 } 302 306 public DOMConfiguration getDomConfig (){ 307 return this; 308 } 309 310 311 320 public LSParserFilter getFilter () { 321 return fDOMFilter; 322 } 323 324 333 public void setFilter (LSParserFilter filter) { 334 fDOMFilter = filter; 335 if (fSkippedElemStack == null) { 336 fSkippedElemStack = new Stack (); 337 } 338 } 339 340 343 public void setParameter (String name, Object value) throws DOMException { 344 346 if(value instanceof Boolean ){ 347 boolean state = ((Boolean )value).booleanValue (); 348 try { 349 if (name.equalsIgnoreCase (Constants.DOM_COMMENTS)) { 350 fConfiguration.setFeature (INCLUDE_COMMENTS_FEATURE, state); 351 } 352 else if (name.equalsIgnoreCase (Constants.DOM_DATATYPE_NORMALIZATION)) { 353 fConfiguration.setFeature (NORMALIZE_DATA, state); 354 } 355 else if (name.equalsIgnoreCase (Constants.DOM_ENTITIES)) { 356 fConfiguration.setFeature (CREATE_ENTITY_REF_NODES, state); 357 } 358 else if (name.equalsIgnoreCase (Constants.DOM_DISALLOW_DOCTYPE)) { 359 fConfiguration.setFeature (DISALLOW_DOCTYPE_DECL_FEATURE, state); 360 } 361 else if (name.equalsIgnoreCase (Constants.DOM_SUPPORTED_MEDIATYPES_ONLY) 362 || name.equalsIgnoreCase(Constants.DOM_NORMALIZE_CHARACTERS) 363 || name.equalsIgnoreCase (Constants.DOM_CHECK_CHAR_NORMALIZATION) 364 || name.equalsIgnoreCase (Constants.DOM_CANONICAL_FORM)) { 365 if (state) { String msg = 367 DOMMessageFormatter.formatMessage ( 368 DOMMessageFormatter.DOM_DOMAIN, 369 "FEATURE_NOT_SUPPORTED", 370 new Object [] { name }); 371 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, msg); 372 } 373 } 375 else if (name.equalsIgnoreCase (Constants.DOM_NAMESPACES)) { 376 fConfiguration.setFeature (NAMESPACES, state); 377 } 378 else if (name.equalsIgnoreCase (Constants.DOM_INFOSET)) { 379 if (state) { 381 fConfiguration.setFeature(NAMESPACES, true); 384 fConfiguration.setFeature(Constants.DOM_NAMESPACE_DECLARATIONS, true); 385 fConfiguration.setFeature(INCLUDE_COMMENTS_FEATURE, true); 386 fConfiguration.setFeature(INCLUDE_IGNORABLE_WHITESPACE, true); 387 388 fConfiguration.setFeature(DYNAMIC_VALIDATION, false); 391 fConfiguration.setFeature(CREATE_ENTITY_REF_NODES, false); 392 fConfiguration.setFeature(NORMALIZE_DATA, false); 393 fConfiguration.setFeature(CREATE_CDATA_NODES_FEATURE, false); 394 } 395 } 396 else if (name.equalsIgnoreCase(Constants.DOM_CDATA_SECTIONS)) { 397 fConfiguration.setFeature(CREATE_CDATA_NODES_FEATURE, state); 398 } 399 else if (name.equalsIgnoreCase (Constants.DOM_NAMESPACE_DECLARATIONS)) { 400 fConfiguration.setFeature(Constants.DOM_NAMESPACE_DECLARATIONS, state); 401 } 402 else if (name.equalsIgnoreCase (Constants.DOM_WELLFORMED) 403 || name.equalsIgnoreCase (Constants.DOM_IGNORE_UNKNOWN_CHARACTER_DENORMALIZATIONS)) { 404 if (!state) { String msg = 406 DOMMessageFormatter.formatMessage ( 407 DOMMessageFormatter.DOM_DOMAIN, 408 "FEATURE_NOT_SUPPORTED", 409 new Object [] { name }); 410 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, msg); 411 } 412 } 415 else if (name.equalsIgnoreCase (Constants.DOM_VALIDATE)) { 416 fConfiguration.setFeature (VALIDATION_FEATURE, state); 417 if (fSchemaType != Constants.NS_DTD) { 418 fConfiguration.setFeature (XMLSCHEMA, state); 419 fConfiguration.setFeature (XMLSCHEMA_FULL_CHECKING, state); 420 } 421 if (state){ 422 fConfiguration.setFeature (DYNAMIC_VALIDATION, false); 423 } 424 } 425 else if (name.equalsIgnoreCase (Constants.DOM_VALIDATE_IF_SCHEMA)) { 426 fConfiguration.setFeature (DYNAMIC_VALIDATION, state); 427 if (state){ 429 fConfiguration.setFeature (VALIDATION_FEATURE, false); 430 } 431 } 432 else if (name.equalsIgnoreCase (Constants.DOM_ELEMENT_CONTENT_WHITESPACE)) { 433 fConfiguration.setFeature (INCLUDE_IGNORABLE_WHITESPACE, state); 434 } 435 else if (name.equalsIgnoreCase (Constants.DOM_PSVI)){ 436 fConfiguration.setFeature (PSVI_AUGMENT, true); 438 fConfiguration.setProperty (DOCUMENT_CLASS_NAME, 439 "org.apache.xerces.dom.PSVIDocumentImpl"); 440 } 441 else { 442 fConfiguration.setFeature (name.toLowerCase(Locale.ENGLISH), state); 446 } 447 448 } 449 catch (XMLConfigurationException e) { 450 String msg = 451 DOMMessageFormatter.formatMessage ( 452 DOMMessageFormatter.DOM_DOMAIN, 453 "FEATURE_NOT_FOUND", 454 new Object [] { name }); 455 throw new DOMException (DOMException.NOT_FOUND_ERR, msg); 456 } 457 } 458 else { if (name.equalsIgnoreCase (Constants.DOM_ERROR_HANDLER)) { 460 if (value instanceof DOMErrorHandler || value == null) { 461 try { 462 fErrorHandler = new DOMErrorHandlerWrapper ((DOMErrorHandler ) value); 463 fConfiguration.setProperty (ERROR_HANDLER, fErrorHandler); 464 } 465 catch (XMLConfigurationException e) {} 466 } 467 else { 468 String msg = 470 DOMMessageFormatter.formatMessage ( 471 DOMMessageFormatter.DOM_DOMAIN, 472 "TYPE_MISMATCH_ERR", 473 new Object [] { name }); 474 throw new DOMException (DOMException.TYPE_MISMATCH_ERR, msg); 475 } 476 477 } 478 else if (name.equalsIgnoreCase (Constants.DOM_RESOURCE_RESOLVER)) { 479 if (value instanceof LSResourceResolver || value == null) { 480 try { 481 fConfiguration.setProperty (ENTITY_RESOLVER, new DOMEntityResolverWrapper ((LSResourceResolver ) value)); 482 } 483 catch (XMLConfigurationException e) {} 484 } 485 else { 486 String msg = 488 DOMMessageFormatter.formatMessage ( 489 DOMMessageFormatter.DOM_DOMAIN, 490 "TYPE_MISMATCH_ERR", 491 new Object [] { name }); 492 throw new DOMException (DOMException.TYPE_MISMATCH_ERR, msg); 493 } 494 495 } 496 else if (name.equalsIgnoreCase (Constants.DOM_SCHEMA_LOCATION)) { 497 if (value instanceof String || value == null) { 498 try { 499 if (value == null) { 500 fSchemaLocation = null; 501 fConfiguration.setProperty ( 502 Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_SOURCE, 503 null); 504 } 505 else { 506 fSchemaLocation = (String )value; 507 StringTokenizer t = new StringTokenizer (fSchemaLocation, " \n\t\r"); 510 if (t.hasMoreTokens ()){ 511 fSchemaLocations.clear (); 512 fSchemaLocations.add (t.nextToken ()); 513 while (t.hasMoreTokens ()) { 514 fSchemaLocations.add (t.nextToken ()); 515 } 516 fConfiguration.setProperty ( 517 Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_SOURCE, 518 fSchemaLocations.toArray ()); 519 } 520 else { 521 fConfiguration.setProperty ( 522 Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_SOURCE, 523 value); 524 } 525 } 526 } 527 catch (XMLConfigurationException e) {} 528 } 529 else { 530 String msg = 532 DOMMessageFormatter.formatMessage ( 533 DOMMessageFormatter.DOM_DOMAIN, 534 "TYPE_MISMATCH_ERR", 535 new Object [] { name }); 536 throw new DOMException (DOMException.TYPE_MISMATCH_ERR, msg); 537 } 538 539 } 540 else if (name.equalsIgnoreCase (Constants.DOM_SCHEMA_TYPE)) { 541 if (value instanceof String || value == null) { 542 try { 543 if (value == null) { 544 fConfiguration.setFeature (XMLSCHEMA, false); 546 fConfiguration.setFeature (XMLSCHEMA_FULL_CHECKING, false); 547 fConfiguration.setProperty ( Constants.JAXP_PROPERTY_PREFIX 549 + Constants.SCHEMA_LANGUAGE, 550 null); 551 fSchemaType = null; 552 } 553 else if (value.equals (Constants.NS_XMLSCHEMA)) { 554 fConfiguration.setFeature (XMLSCHEMA, true); 556 fConfiguration.setFeature (XMLSCHEMA_FULL_CHECKING, true); 557 fConfiguration.setProperty ( Constants.JAXP_PROPERTY_PREFIX 559 + Constants.SCHEMA_LANGUAGE, 560 Constants.NS_XMLSCHEMA); 561 fSchemaType = Constants.NS_XMLSCHEMA; 562 } 563 else if (value.equals (Constants.NS_DTD)) { 564 fConfiguration.setFeature (XMLSCHEMA, false); 566 fConfiguration.setFeature (XMLSCHEMA_FULL_CHECKING, false); 567 fConfiguration.setProperty ( Constants.JAXP_PROPERTY_PREFIX 569 + Constants.SCHEMA_LANGUAGE, 570 Constants.NS_DTD); 571 fSchemaType = Constants.NS_DTD; 572 } 573 } 574 catch (XMLConfigurationException e) {} 575 } 576 else { 577 String msg = 578 DOMMessageFormatter.formatMessage ( 579 DOMMessageFormatter.DOM_DOMAIN, 580 "TYPE_MISMATCH_ERR", 581 new Object [] { name }); 582 throw new DOMException (DOMException.TYPE_MISMATCH_ERR, msg); 583 } 584 585 } 586 else if (name.equalsIgnoreCase (DOCUMENT_CLASS_NAME)) { 587 fConfiguration.setProperty (DOCUMENT_CLASS_NAME, value); 588 } 589 else { 590 String msg = 593 DOMMessageFormatter.formatMessage ( 594 DOMMessageFormatter.DOM_DOMAIN, 595 "FEATURE_NOT_FOUND", 596 new Object [] { name }); 597 throw new DOMException (DOMException.NOT_FOUND_ERR, msg); 598 } 599 } 600 } 601 602 605 public Object getParameter (String name) throws DOMException { 606 if (name.equalsIgnoreCase (Constants.DOM_COMMENTS)) { 607 return (fConfiguration.getFeature (INCLUDE_COMMENTS_FEATURE)) 608 ? Boolean.TRUE 609 : Boolean.FALSE; 610 } 611 else if (name.equalsIgnoreCase (Constants.DOM_DATATYPE_NORMALIZATION)) { 612 return (fConfiguration.getFeature (NORMALIZE_DATA)) 613 ? Boolean.TRUE 614 : Boolean.FALSE; 615 } 616 else if (name.equalsIgnoreCase (Constants.DOM_ENTITIES)) { 617 return (fConfiguration.getFeature (CREATE_ENTITY_REF_NODES)) 618 ? Boolean.TRUE 619 : Boolean.FALSE; 620 } 621 else if (name.equalsIgnoreCase (Constants.DOM_NAMESPACES)) { 622 return (fConfiguration.getFeature (NAMESPACES)) 623 ? Boolean.TRUE 624 : Boolean.FALSE; 625 } 626 else if (name.equalsIgnoreCase (Constants.DOM_VALIDATE)) { 627 return (fConfiguration.getFeature (VALIDATION_FEATURE)) 628 ? Boolean.TRUE 629 : Boolean.FALSE; 630 } 631 else if (name.equalsIgnoreCase (Constants.DOM_VALIDATE_IF_SCHEMA)) { 632 return (fConfiguration.getFeature (DYNAMIC_VALIDATION)) 633 ? Boolean.TRUE 634 : Boolean.FALSE; 635 } 636 else if (name.equalsIgnoreCase (Constants.DOM_ELEMENT_CONTENT_WHITESPACE)) { 637 return (fConfiguration.getFeature (INCLUDE_IGNORABLE_WHITESPACE)) 638 ? Boolean.TRUE 639 : Boolean.FALSE; 640 } 641 else if (name.equalsIgnoreCase (Constants.DOM_DISALLOW_DOCTYPE)) { 642 return (fConfiguration.getFeature (DISALLOW_DOCTYPE_DECL_FEATURE)) 643 ? Boolean.TRUE 644 : Boolean.FALSE; 645 } 646 else if (name.equalsIgnoreCase (Constants.DOM_INFOSET)) { 647 boolean infoset = fConfiguration.getFeature(NAMESPACES) && 652 fConfiguration.getFeature(Constants.DOM_NAMESPACE_DECLARATIONS) && 653 fConfiguration.getFeature(INCLUDE_COMMENTS_FEATURE) && 654 fConfiguration.getFeature(INCLUDE_IGNORABLE_WHITESPACE) && 655 !fConfiguration.getFeature(DYNAMIC_VALIDATION) && 656 !fConfiguration.getFeature(CREATE_ENTITY_REF_NODES) && 657 !fConfiguration.getFeature(NORMALIZE_DATA) && 658 !fConfiguration.getFeature(CREATE_CDATA_NODES_FEATURE); 659 return (infoset) ? Boolean.TRUE : Boolean.FALSE; 660 } 661 else if (name.equalsIgnoreCase(Constants.DOM_CDATA_SECTIONS)) { 662 return (fConfiguration.getFeature(CREATE_CDATA_NODES_FEATURE)) 663 ? Boolean.TRUE : Boolean.FALSE; 664 } 665 else if (name.equalsIgnoreCase(Constants.DOM_CHECK_CHAR_NORMALIZATION ) || 666 name.equalsIgnoreCase(Constants.DOM_NORMALIZE_CHARACTERS)){ 667 return Boolean.FALSE; 668 } 669 else if (name.equalsIgnoreCase(Constants.DOM_NAMESPACE_DECLARATIONS) 670 || name.equalsIgnoreCase (Constants.DOM_WELLFORMED) 671 || name.equalsIgnoreCase (Constants.DOM_IGNORE_UNKNOWN_CHARACTER_DENORMALIZATIONS) 672 || name.equalsIgnoreCase (Constants.DOM_CANONICAL_FORM) 673 || name.equalsIgnoreCase (Constants.DOM_SUPPORTED_MEDIATYPES_ONLY) 674 || name.equalsIgnoreCase (Constants.DOM_SPLIT_CDATA) 675 || name.equalsIgnoreCase (Constants.DOM_CHARSET_OVERRIDES_XML_ENCODING)) { 676 return (fConfiguration.getFeature (name.toLowerCase(Locale.ENGLISH))) 677 ? Boolean.TRUE 678 : Boolean.FALSE; 679 } 680 else if (name.equalsIgnoreCase (Constants.DOM_ERROR_HANDLER)) { 681 if (fErrorHandler != null) { 682 return fErrorHandler.getErrorHandler (); 683 } 684 return null; 685 } 686 else if (name.equalsIgnoreCase (Constants.DOM_RESOURCE_RESOLVER)) { 687 try { 688 XMLEntityResolver entityResolver = 689 (XMLEntityResolver) fConfiguration.getProperty (ENTITY_RESOLVER); 690 if (entityResolver != null 691 && entityResolver instanceof DOMEntityResolverWrapper) { 692 return ((DOMEntityResolverWrapper) entityResolver).getEntityResolver (); 693 } 694 return null; 695 } 696 catch (XMLConfigurationException e) {} 697 } 698 else if (name.equalsIgnoreCase (Constants.DOM_SCHEMA_TYPE)) { 699 return fConfiguration.getProperty ( 700 Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_LANGUAGE); 701 } 702 else if (name.equalsIgnoreCase (Constants.DOM_SCHEMA_LOCATION)) { 703 return fSchemaLocation; 704 } 705 else if (name.equalsIgnoreCase (SYMBOL_TABLE)){ 706 return fConfiguration.getProperty (SYMBOL_TABLE); 707 } 708 else if (name.equalsIgnoreCase (DOCUMENT_CLASS_NAME)) { 709 return fConfiguration.getProperty (DOCUMENT_CLASS_NAME); 710 } 711 else { 712 String msg = 713 DOMMessageFormatter.formatMessage ( 714 DOMMessageFormatter.DOM_DOMAIN, 715 "FEATURE_NOT_FOUND", 716 new Object [] { name }); 717 throw new DOMException (DOMException.NOT_FOUND_ERR, msg); 718 } 719 return null; 720 } 721 722 public boolean canSetParameter (String name, Object value) { 723 if (value == null){ 724 return true; 725 } 726 727 if(value instanceof Boolean ){ 728 boolean state = ((Boolean )value).booleanValue (); 729 if ( name.equalsIgnoreCase (Constants.DOM_SUPPORTED_MEDIATYPES_ONLY) 730 || name.equalsIgnoreCase(Constants.DOM_NORMALIZE_CHARACTERS) 731 || name.equalsIgnoreCase(Constants.DOM_CHECK_CHAR_NORMALIZATION ) 732 || name.equalsIgnoreCase (Constants.DOM_CANONICAL_FORM) ) { 733 return (state) ? false : true; 735 } 736 else if (name.equalsIgnoreCase (Constants.DOM_WELLFORMED) 737 || name.equalsIgnoreCase (Constants.DOM_IGNORE_UNKNOWN_CHARACTER_DENORMALIZATIONS)) { 738 return (state) ? true : false; 740 } 741 else if (name.equalsIgnoreCase (Constants.DOM_CDATA_SECTIONS) 742 || name.equalsIgnoreCase (Constants.DOM_CHARSET_OVERRIDES_XML_ENCODING) 743 || name.equalsIgnoreCase (Constants.DOM_COMMENTS) 744 || name.equalsIgnoreCase (Constants.DOM_DATATYPE_NORMALIZATION) 745 || name.equalsIgnoreCase (Constants.DOM_DISALLOW_DOCTYPE) 746 || name.equalsIgnoreCase (Constants.DOM_ENTITIES) 747 || name.equalsIgnoreCase (Constants.DOM_INFOSET) 748 || name.equalsIgnoreCase (Constants.DOM_NAMESPACES) 749 || name.equalsIgnoreCase (Constants.DOM_NAMESPACE_DECLARATIONS) 750 || name.equalsIgnoreCase (Constants.DOM_VALIDATE) 751 || name.equalsIgnoreCase (Constants.DOM_VALIDATE_IF_SCHEMA) 752 || name.equalsIgnoreCase (Constants.DOM_ELEMENT_CONTENT_WHITESPACE) 753 || name.equalsIgnoreCase (Constants.DOM_XMLDECL)) { 754 return true; 755 } 756 757 try { 759 fConfiguration.getFeature(name.toLowerCase(Locale.ENGLISH)); 760 return true; 761 } 762 catch (XMLConfigurationException e) { 763 return false; 764 } 765 } 766 else { if (name.equalsIgnoreCase (Constants.DOM_ERROR_HANDLER)) { 768 if (value instanceof DOMErrorHandler || value == null) { 769 return true; 770 } 771 return false; 772 } 773 else if (name.equalsIgnoreCase (Constants.DOM_RESOURCE_RESOLVER)) { 774 if (value instanceof LSResourceResolver || value == null) { 775 return true; 776 } 777 return false; 778 } 779 else if (name.equalsIgnoreCase (Constants.DOM_SCHEMA_TYPE)) { 780 if ((value instanceof String 781 && (value.equals (Constants.NS_XMLSCHEMA) 782 || value.equals (Constants.NS_DTD))) || value == null) { 783 return true; 784 } 785 return false; 786 } 787 else if (name.equalsIgnoreCase (Constants.DOM_SCHEMA_LOCATION)) { 788 if (value instanceof String || value == null) 789 return true; 790 return false; 791 } 792 else if (name.equalsIgnoreCase (DOCUMENT_CLASS_NAME)){ 793 return true; 794 } 795 return false; 796 } 797 } 798 799 807 public DOMStringList getParameterNames () { 808 if (fRecognizedParameters == null){ 809 Vector parameters = new Vector (); 810 811 parameters.add(Constants.DOM_NAMESPACES); 813 parameters.add(Constants.DOM_CDATA_SECTIONS); 814 parameters.add(Constants.DOM_CANONICAL_FORM); 815 parameters.add(Constants.DOM_NAMESPACE_DECLARATIONS); 816 parameters.add(Constants.DOM_SPLIT_CDATA); 817 818 parameters.add(Constants.DOM_ENTITIES); 819 parameters.add(Constants.DOM_VALIDATE_IF_SCHEMA); 820 parameters.add(Constants.DOM_VALIDATE); 821 parameters.add(Constants.DOM_DATATYPE_NORMALIZATION); 822 823 parameters.add(Constants.DOM_CHARSET_OVERRIDES_XML_ENCODING); 824 parameters.add(Constants.DOM_CHECK_CHAR_NORMALIZATION); 825 parameters.add(Constants.DOM_SUPPORTED_MEDIATYPES_ONLY); 826 parameters.add(Constants.DOM_IGNORE_UNKNOWN_CHARACTER_DENORMALIZATIONS); 827 828 parameters.add(Constants.DOM_NORMALIZE_CHARACTERS); 829 parameters.add(Constants.DOM_WELLFORMED); 830 parameters.add(Constants.DOM_INFOSET); 831 parameters.add(Constants.DOM_DISALLOW_DOCTYPE); 832 parameters.add(Constants.DOM_ELEMENT_CONTENT_WHITESPACE); 833 parameters.add(Constants.DOM_COMMENTS); 834 835 parameters.add(Constants.DOM_ERROR_HANDLER); 836 parameters.add(Constants.DOM_RESOURCE_RESOLVER); 837 parameters.add(Constants.DOM_SCHEMA_LOCATION); 838 parameters.add(Constants.DOM_SCHEMA_TYPE); 839 840 fRecognizedParameters = new DOMStringListImpl(parameters); 841 842 } 843 844 return fRecognizedParameters; 845 } 846 847 853 public Document parseURI (String uri) throws LSException { 854 855 if ( fBusy ) { 858 String msg = DOMMessageFormatter.formatMessage ( 859 DOMMessageFormatter.DOM_DOMAIN, 860 "INVALID_STATE_ERR",null); 861 throw new DOMException ( DOMException.INVALID_STATE_ERR,msg); 862 } 863 864 XMLInputSource source = new XMLInputSource (null, uri, null); 865 try { 866 currentThread = Thread.currentThread(); 867 fBusy = true; 868 parse (source); 869 fBusy = false; 870 if (abortNow && currentThread.isInterrupted()) { 871 abortNow = false; 873 Thread.interrupted(); 874 } 875 } catch (Exception e){ 876 fBusy = false; 877 if (abortNow && currentThread.isInterrupted()) { 878 Thread.interrupted(); 879 } 880 if (abortNow) { 881 abortNow = false; 882 restoreHandlers(); 883 return null; 884 } 885 if (e != abort) { 888 if (!(e instanceof XMLParseException) && fErrorHandler != null) { 889 DOMErrorImpl error = new DOMErrorImpl (); 890 error.fException = e; 891 error.fMessage = e.getMessage (); 892 error.fSeverity = DOMError.SEVERITY_FATAL_ERROR; 893 fErrorHandler.getErrorHandler ().handleError (error); 894 } 895 if (DEBUG) { 896 e.printStackTrace (); 897 } 898 throw new LSException (LSException.PARSE_ERR, e.getMessage()); 899 } 900 } 901 return getDocument (); 902 } 903 904 909 public Document parse (LSInput is) throws LSException { 910 911 XMLInputSource xmlInputSource = dom2xmlInputSource (is); 913 if ( fBusy ) { 914 String msg = DOMMessageFormatter.formatMessage ( 915 DOMMessageFormatter.DOM_DOMAIN, 916 "INVALID_STATE_ERR",null); 917 throw new DOMException ( DOMException.INVALID_STATE_ERR,msg); 918 } 919 920 try { 921 currentThread = Thread.currentThread(); 922 fBusy = true; 923 parse (xmlInputSource); 924 fBusy = false; 925 if (abortNow && currentThread.isInterrupted()) { 926 abortNow = false; 928 Thread.interrupted(); 929 } 930 } catch (Exception e) { 931 fBusy = false; 932 if (abortNow && currentThread.isInterrupted()) { 933 Thread.interrupted(); 934 } 935 if (abortNow) { 936 abortNow = false; 937 restoreHandlers(); 938 return null; 939 } 940 if (e != abort) { 943 if (!(e instanceof XMLParseException) && fErrorHandler != null) { 944 DOMErrorImpl error = new DOMErrorImpl (); 945 error.fException = e; 946 error.fMessage = e.getMessage (); 947 error.fSeverity = DOMError.SEVERITY_FATAL_ERROR; 948 fErrorHandler.getErrorHandler().handleError (error); 949 } 950 if (DEBUG) { 951 e.printStackTrace (); 952 } 953 throw new LSException (LSException.PARSE_ERR, e.getMessage()); 954 } 955 } 956 return getDocument (); 957 } 958 959 960 private void restoreHandlers() { 961 fConfiguration.setDocumentHandler(this); 962 fConfiguration.setDTDHandler(this); 963 fConfiguration.setDTDContentModelHandler(this); 964 } 965 966 985 public Node parseWithContext (LSInput is, Node cnode, 986 short action) throws DOMException , LSException { 987 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, "Not supported"); 989 } 990 991 992 998 XMLInputSource dom2xmlInputSource (LSInput is) { 999 XMLInputSource xis = null; 1001 if (is.getCharacterStream () != null) { 1004 xis = new XMLInputSource (is.getPublicId (), is.getSystemId (), 1005 is.getBaseURI (), is.getCharacterStream (), 1006 "UTF-16"); 1007 } 1008 else if (is.getByteStream () != null) { 1010 xis = new XMLInputSource (is.getPublicId (), is.getSystemId (), 1011 is.getBaseURI (), is.getByteStream (), 1012 is.getEncoding ()); 1013 } 1014 else if (is.getStringData () != null && is.getStringData().length() > 0) { 1017 xis = new XMLInputSource (is.getPublicId (), is.getSystemId (), 1018 is.getBaseURI (), new StringReader (is.getStringData ()), 1019 "UTF-16"); 1020 } 1021 else if ((is.getSystemId() != null && is.getSystemId().length() > 0) || 1023 (is.getPublicId() != null && is.getPublicId().length() > 0)) { 1024 xis = new XMLInputSource (is.getPublicId (), is.getSystemId (), 1025 is.getBaseURI ()); 1026 } 1027 else { 1028 if (fErrorHandler != null) { 1030 DOMErrorImpl error = new DOMErrorImpl(); 1031 error.fType = "no-input-specified"; 1032 error.fMessage = "no-input-specified"; 1033 error.fSeverity = DOMError.SEVERITY_FATAL_ERROR; 1034 fErrorHandler.getErrorHandler().handleError(error); 1035 } 1036 throw new LSException (LSException.PARSE_ERR, "no-input-specified"); 1037 } 1038 return xis; 1039 } 1040 1041 1044 public boolean getAsync () { 1045 return false; 1046 } 1047 1048 1051 public boolean getBusy () { 1052 return fBusy; 1053 } 1054 1055 1058 public void abort () { 1059 if ( fBusy ) { 1061 fBusy = false; 1062 if(currentThread != null) { 1063 abortNow = true; 1064 1065 fConfiguration.setDocumentHandler(abortHandler); 1066 fConfiguration.setDTDHandler(abortHandler); 1067 fConfiguration.setDTDContentModelHandler(abortHandler); 1068 1069 if(currentThread == Thread.currentThread()) 1070 throw abort; 1071 1072 currentThread.interrupt(); 1073 } 1074 } 1075 return; } 1077 1078 1090 public void startElement (QName element, XMLAttributes attributes, Augmentations augs) { 1091 if (!fNamespaceDeclarations && fNamespaceAware) { 1093 int len = attributes.getLength(); 1094 for (int i = len - 1; i >= 0; --i) { 1095 if (XMLSymbols.PREFIX_XMLNS == attributes.getPrefix(i) || 1096 XMLSymbols.PREFIX_XMLNS == attributes.getQName(i)) { 1097 attributes.removeAttributeAt(i); 1098 } 1099 } 1100 } 1101 super.startElement(element, attributes, augs); 1102 } 1103 1104 private class AbortHandler implements XMLDocumentHandler, XMLDTDHandler, XMLDTDContentModelHandler { 1105 1106 private XMLDocumentSource documentSource; 1107 private XMLDTDContentModelSource dtdContentSource; 1108 private XMLDTDSource dtdSource; 1109 1110 public void startDocument(XMLLocator locator, String encoding, NamespaceContext namespaceContext, Augmentations augs) throws XNIException { 1111 throw abort; 1112 } 1113 1114 public void xmlDecl(String version, String encoding, String standalone, Augmentations augs) throws XNIException { 1115 throw abort; 1116 } 1117 1118 public void doctypeDecl(String rootElement, String publicId, String systemId, Augmentations augs) throws XNIException { 1119 throw abort; 1120 } 1121 1122 public void comment(XMLString text, Augmentations augs) throws XNIException { 1123 throw abort; 1124 } 1125 1126 public void processingInstruction(String target, XMLString data, Augmentations augs) throws XNIException { 1127 throw abort; 1128 } 1129 1130 public void startElement(QName element, XMLAttributes attributes, Augmentations augs) throws XNIException { 1131 throw abort; 1132 } 1133 1134 public void emptyElement(QName element, XMLAttributes attributes, Augmentations augs) throws XNIException { 1135 throw abort; 1136 } 1137 1138 public void startGeneralEntity(String name, XMLResourceIdentifier identifier, String encoding, Augmentations augs) throws XNIException { 1139 throw abort; 1140 } 1141 1142 public void textDecl(String version, String encoding, Augmentations augs) throws XNIException { 1143 throw abort; 1144 } 1145 1146 public void endGeneralEntity(String name, Augmentations augs) throws XNIException { 1147 throw abort; 1148 } 1149 1150 public void characters(XMLString text, Augmentations augs) throws XNIException { 1151 throw abort; 1152 } 1153 1154 public void ignorableWhitespace(XMLString text, Augmentations augs) throws XNIException { 1155 throw abort; 1156 } 1157 1158 public void endElement(QName element, Augmentations augs) throws XNIException { 1159 throw abort; 1160 } 1161 1162 public void startCDATA(Augmentations augs) throws XNIException { 1163 throw abort; 1164 } 1165 1166 public void endCDATA(Augmentations augs) throws XNIException { 1167 throw abort; 1168 } 1169 1170 public void endDocument(Augmentations augs) throws XNIException { 1171 throw abort; 1172 } 1173 1174 public void setDocumentSource(XMLDocumentSource source) { 1175 documentSource = source; 1176 } 1177 1178 public XMLDocumentSource getDocumentSource() { 1179 return documentSource; 1180 } 1181 1182 public void startDTD(XMLLocator locator, Augmentations augmentations) throws XNIException { 1183 throw abort; 1184 } 1185 1186 public void startParameterEntity(String name, XMLResourceIdentifier identifier, String encoding, Augmentations augmentations) throws XNIException { 1187 throw abort; 1188 } 1189 1190 public void endParameterEntity(String name, Augmentations augmentations) throws XNIException { 1191 throw abort; 1192 } 1193 1194 public void startExternalSubset(XMLResourceIdentifier identifier, Augmentations augmentations) throws XNIException { 1195 throw abort; 1196 } 1197 1198 public void endExternalSubset(Augmentations augmentations) throws XNIException { 1199 throw abort; 1200 } 1201 1202 public void elementDecl(String name, String contentModel, Augmentations augmentations) throws XNIException { 1203 throw abort; 1204 } 1205 1206 public void startAttlist(String elementName, Augmentations augmentations) throws XNIException { 1207 throw abort; 1208 } 1209 1210 public void attributeDecl(String elementName, String attributeName, String type, String [] enumeration, String defaultType, XMLString defaultValue, XMLString nonNormalizedDefaultValue, Augmentations augmentations) throws XNIException { 1211 throw abort; 1212 } 1213 1214 public void endAttlist(Augmentations augmentations) throws XNIException { 1215 throw abort; 1216 } 1217 1218 public void internalEntityDecl(String name, XMLString text, XMLString nonNormalizedText, Augmentations augmentations) throws XNIException { 1219 throw abort; 1220 } 1221 1222 public void externalEntityDecl(String name, XMLResourceIdentifier identifier, Augmentations augmentations) throws XNIException { 1223 throw abort; 1224 } 1225 1226 public void unparsedEntityDecl(String name, XMLResourceIdentifier identifier, String notation, Augmentations augmentations) throws XNIException { 1227 throw abort; 1228 } 1229 1230 public void notationDecl(String name, XMLResourceIdentifier identifier, Augmentations augmentations) throws XNIException { 1231 throw abort; 1232 } 1233 1234 public void startConditional(short type, Augmentations augmentations) throws XNIException { 1235 throw abort; 1236 } 1237 1238 public void ignoredCharacters(XMLString text, Augmentations augmentations) throws XNIException { 1239 throw abort; 1240 } 1241 1242 public void endConditional(Augmentations augmentations) throws XNIException { 1243 throw abort; 1244 } 1245 1246 public void endDTD(Augmentations augmentations) throws XNIException { 1247 throw abort; 1248 } 1249 1250 public void setDTDSource(XMLDTDSource source) { 1251 dtdSource = source; 1252 } 1253 1254 public XMLDTDSource getDTDSource() { 1255 return dtdSource; 1256 } 1257 1258 public void startContentModel(String elementName, Augmentations augmentations) throws XNIException { 1259 throw abort; 1260 } 1261 1262 public void any(Augmentations augmentations) throws XNIException { 1263 throw abort; 1264 } 1265 1266 public void empty(Augmentations augmentations) throws XNIException { 1267 throw abort; 1268 } 1269 1270 public void startGroup(Augmentations augmentations) throws XNIException { 1271 throw abort; 1272 } 1273 1274 public void pcdata(Augmentations augmentations) throws XNIException { 1275 throw abort; 1276 } 1277 1278 public void element(String elementName, Augmentations augmentations) throws XNIException { 1279 throw abort; 1280 } 1281 1282 public void separator(short separator, Augmentations augmentations) throws XNIException { 1283 throw abort; 1284 } 1285 1286 public void occurrence(short occurrence, Augmentations augmentations) throws XNIException { 1287 throw abort; 1288 } 1289 1290 public void endGroup(Augmentations augmentations) throws XNIException { 1291 throw abort; 1292 } 1293 1294 public void endContentModel(Augmentations augmentations) throws XNIException { 1295 throw abort; 1296 } 1297 1298 public void setDTDContentModelSource(XMLDTDContentModelSource source) { 1299 dtdContentSource = source; 1300 } 1301 1302 public XMLDTDContentModelSource getDTDContentModelSource() { 1303 return dtdContentSource; 1304 } 1305 1306 } 1307 1308} | Popular Tags |