1 57 58 package com.sun.org.apache.xerces.internal.parsers; 59 60 import java.io.IOException ; 61 import java.util.Locale ; 62 63 import com.sun.org.apache.xerces.internal.impl.Constants; 64 import com.sun.org.apache.xerces.internal.impl.XMLDTDScannerImpl; 65 import com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl; 66 import com.sun.org.apache.xerces.internal.impl.XMLEntityManager; 67 import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter; 68 import com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl; 69 import com.sun.org.apache.xerces.internal.impl.dv.DTDDVFactory; 70 import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter; 71 import com.sun.org.apache.xerces.internal.impl.validation.ValidationManager; 72 import com.sun.org.apache.xerces.internal.util.SymbolTable; 73 import com.sun.org.apache.xerces.internal.xni.XMLLocator; 74 import com.sun.org.apache.xerces.internal.xni.XNIException; 75 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarPool; 76 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponent; 77 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager; 78 import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException; 79 import com.sun.org.apache.xerces.internal.xni.parser.XMLDTDScanner; 80 import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentScanner; 81 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource; 82 import com.sun.org.apache.xerces.internal.xni.parser.XMLPullParserConfiguration; 83 84 97 public class NonValidatingConfiguration 98 extends BasicParserConfiguration 99 implements XMLPullParserConfiguration { 100 101 105 107 108 protected static final String WARN_ON_DUPLICATE_ATTDEF = 109 Constants.XERCES_FEATURE_PREFIX + Constants.WARN_ON_DUPLICATE_ATTDEF_FEATURE; 110 111 112 protected static final String WARN_ON_DUPLICATE_ENTITYDEF = 113 Constants.XERCES_FEATURE_PREFIX + Constants.WARN_ON_DUPLICATE_ENTITYDEF_FEATURE; 114 115 116 protected static final String WARN_ON_UNDECLARED_ELEMDEF = 117 Constants.XERCES_FEATURE_PREFIX + Constants.WARN_ON_UNDECLARED_ELEMDEF_FEATURE; 118 119 120 protected static final String ALLOW_JAVA_ENCODINGS = 121 Constants.XERCES_FEATURE_PREFIX + Constants.ALLOW_JAVA_ENCODINGS_FEATURE; 122 123 124 protected static final String CONTINUE_AFTER_FATAL_ERROR = 125 Constants.XERCES_FEATURE_PREFIX + Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE; 126 127 128 protected static final String LOAD_EXTERNAL_DTD = 129 Constants.XERCES_FEATURE_PREFIX + Constants.LOAD_EXTERNAL_DTD_FEATURE; 130 131 132 protected static final String NOTIFY_BUILTIN_REFS = 133 Constants.XERCES_FEATURE_PREFIX + Constants.NOTIFY_BUILTIN_REFS_FEATURE; 134 135 136 protected static final String NOTIFY_CHAR_REFS = 137 Constants.XERCES_FEATURE_PREFIX + Constants.NOTIFY_CHAR_REFS_FEATURE; 138 139 140 141 protected static final String NORMALIZE_DATA = 142 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_NORMALIZED_VALUE; 143 144 145 146 protected static final String SCHEMA_ELEMENT_DEFAULT = 147 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_ELEMENT_DEFAULT; 148 149 151 152 protected static final String ERROR_REPORTER = 153 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY; 154 155 156 protected static final String ENTITY_MANAGER = 157 Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_MANAGER_PROPERTY; 158 159 160 protected static final String DOCUMENT_SCANNER = 161 Constants.XERCES_PROPERTY_PREFIX + Constants.DOCUMENT_SCANNER_PROPERTY; 162 163 164 protected static final String DTD_SCANNER = 165 Constants.XERCES_PROPERTY_PREFIX + Constants.DTD_SCANNER_PROPERTY; 166 167 168 protected static final String XMLGRAMMAR_POOL = 169 Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY; 170 171 172 protected static final String DTD_VALIDATOR = 173 Constants.XERCES_PROPERTY_PREFIX + Constants.DTD_VALIDATOR_PROPERTY; 174 175 176 protected static final String NAMESPACE_BINDER = 177 Constants.XERCES_PROPERTY_PREFIX + Constants.NAMESPACE_BINDER_PROPERTY; 178 179 180 protected static final String DATATYPE_VALIDATOR_FACTORY = 181 Constants.XERCES_PROPERTY_PREFIX + Constants.DATATYPE_VALIDATOR_FACTORY_PROPERTY; 182 183 protected static final String VALIDATION_MANAGER = 184 Constants.XERCES_PROPERTY_PREFIX + Constants.VALIDATION_MANAGER_PROPERTY; 185 186 187 protected static final String SCHEMA_VALIDATOR = 188 Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_VALIDATOR_PROPERTY; 189 190 191 193 194 private static final boolean PRINT_EXCEPTION_STACK_TRACE = false; 195 196 200 202 203 protected XMLGrammarPool fGrammarPool; 204 205 206 protected DTDDVFactory fDatatypeValidatorFactory; 207 208 210 211 protected XMLErrorReporter fErrorReporter; 212 213 214 protected XMLEntityManager fEntityManager; 215 216 217 protected XMLDocumentScanner fScanner; 218 219 220 protected XMLInputSource fInputSource; 221 222 223 protected XMLDTDScanner fDTDScanner; 224 225 226 protected ValidationManager fValidationManager; 227 228 230 231 private XMLNSDocumentScannerImpl fNamespaceScanner; 232 233 234 private XMLDocumentScannerImpl fNonNSScanner; 235 236 237 240 protected boolean fConfigUpdated = false; 241 242 243 245 246 protected XMLLocator fLocator; 247 248 253 protected boolean fParseInProgress = false; 254 255 259 260 public NonValidatingConfiguration() { 261 this(null, null, null); 262 } 264 269 public NonValidatingConfiguration(SymbolTable symbolTable) { 270 this(symbolTable, null, null); 271 } 273 284 public NonValidatingConfiguration(SymbolTable symbolTable, 285 XMLGrammarPool grammarPool) { 286 this(symbolTable, grammarPool, null); 287 } 289 301 public NonValidatingConfiguration(SymbolTable symbolTable, 302 XMLGrammarPool grammarPool, 303 XMLComponentManager parentSettings) { 304 super(symbolTable, parentSettings); 305 306 final String [] recognizedFeatures = { 308 PARSER_SETTINGS, 309 NAMESPACES, 310 CONTINUE_AFTER_FATAL_ERROR, 314 }; 319 addRecognizedFeatures(recognizedFeatures); 320 321 fFeatures.put(CONTINUE_AFTER_FATAL_ERROR, Boolean.FALSE); 326 fFeatures.put(PARSER_SETTINGS, Boolean.TRUE); 327 fFeatures.put(NAMESPACES, Boolean.TRUE); 328 333 final String [] recognizedProperties = { 335 ERROR_REPORTER, 336 ENTITY_MANAGER, 337 DOCUMENT_SCANNER, 338 DTD_SCANNER, 339 DTD_VALIDATOR, 340 NAMESPACE_BINDER, 341 XMLGRAMMAR_POOL, 342 DATATYPE_VALIDATOR_FACTORY, 343 VALIDATION_MANAGER 344 }; 345 addRecognizedProperties(recognizedProperties); 346 347 fGrammarPool = grammarPool; 348 if(fGrammarPool != null){ 349 fProperties.put(XMLGRAMMAR_POOL, fGrammarPool); 350 } 351 352 fEntityManager = createEntityManager(); 353 fProperties.put(ENTITY_MANAGER, fEntityManager); 354 addComponent(fEntityManager); 355 356 fErrorReporter = createErrorReporter(); 357 fErrorReporter.setDocumentLocator(fEntityManager.getEntityScanner()); 358 fProperties.put(ERROR_REPORTER, fErrorReporter); 359 addComponent(fErrorReporter); 360 361 364 fDTDScanner = createDTDScanner(); 365 if (fDTDScanner != null) { 366 fProperties.put(DTD_SCANNER, fDTDScanner); 367 if (fDTDScanner instanceof XMLComponent) { 368 addComponent((XMLComponent)fDTDScanner); 369 } 370 } 371 372 fDatatypeValidatorFactory = createDatatypeValidatorFactory(); 373 if (fDatatypeValidatorFactory != null) { 374 fProperties.put(DATATYPE_VALIDATOR_FACTORY, 375 fDatatypeValidatorFactory); 376 } 377 fValidationManager = createValidationManager(); 378 379 if (fValidationManager != null) { 380 fProperties.put(VALIDATION_MANAGER, fValidationManager); 381 } 382 if (fErrorReporter.getMessageFormatter(XMLMessageFormatter.XML_DOMAIN) == null) { 384 XMLMessageFormatter xmft = new XMLMessageFormatter(); 385 fErrorReporter.putMessageFormatter(XMLMessageFormatter.XML_DOMAIN, xmft); 386 fErrorReporter.putMessageFormatter(XMLMessageFormatter.XMLNS_DOMAIN, xmft); 387 } 388 389 fConfigUpdated = false; 390 391 try { 393 setLocale(Locale.getDefault()); 394 } 395 catch (XNIException e) { 396 } 399 400 } 402 public void setFeature(String featureId, boolean state) 406 throws XMLConfigurationException { 407 fConfigUpdated = true; 408 super.setFeature(featureId, state); 409 } 410 411 public void setProperty(String propertyId, Object value) 412 throws XMLConfigurationException { 413 fConfigUpdated = true; 414 super.setProperty(propertyId, value); 415 } 416 424 public void setLocale(Locale locale) throws XNIException { 425 super.setLocale(locale); 426 fErrorReporter.setLocale(locale); 427 } 429 public boolean getFeature(String featureId) 430 throws XMLConfigurationException { 431 if (featureId.equals(PARSER_SETTINGS)){ 433 return fConfigUpdated; 434 } 435 return super.getFeature(featureId); 436 437 } 442 444 456 public void setInputSource(XMLInputSource inputSource) 457 throws XMLConfigurationException, IOException { 458 459 464 fInputSource = inputSource; 465 466 } 468 484 public boolean parse(boolean complete) throws XNIException, IOException { 485 if (fInputSource !=null) { 488 try { 489 reset(); 491 fScanner.setInputSource(fInputSource); 492 fInputSource = null; 493 } 494 catch (XNIException ex) { 495 if (PRINT_EXCEPTION_STACK_TRACE) 496 ex.printStackTrace(); 497 throw ex; 498 } 499 catch (IOException ex) { 500 if (PRINT_EXCEPTION_STACK_TRACE) 501 ex.printStackTrace(); 502 throw ex; 503 } 504 catch (RuntimeException ex) { 505 if (PRINT_EXCEPTION_STACK_TRACE) 506 ex.printStackTrace(); 507 throw ex; 508 } 509 catch (Exception ex) { 510 if (PRINT_EXCEPTION_STACK_TRACE) 511 ex.printStackTrace(); 512 throw new XNIException(ex); 513 } 514 } 515 516 try { 517 return fScanner.scanDocument(complete); 518 } 519 catch (XNIException ex) { 520 if (PRINT_EXCEPTION_STACK_TRACE) 521 ex.printStackTrace(); 522 throw ex; 523 } 524 catch (IOException ex) { 525 if (PRINT_EXCEPTION_STACK_TRACE) 526 ex.printStackTrace(); 527 throw ex; 528 } 529 catch (RuntimeException ex) { 530 if (PRINT_EXCEPTION_STACK_TRACE) 531 ex.printStackTrace(); 532 throw ex; 533 } 534 catch (Exception ex) { 535 if (PRINT_EXCEPTION_STACK_TRACE) 536 ex.printStackTrace(); 537 throw new XNIException(ex); 538 } 539 540 } 542 547 public void cleanup() { 548 fEntityManager.closeReaders(); 549 } 550 551 555 563 public void parse(XMLInputSource source) throws XNIException, IOException { 564 565 if (fParseInProgress) { 566 throw new XNIException("FWK005 parse may not be called while parsing."); 568 } 569 fParseInProgress = true; 570 571 try { 572 setInputSource(source); 573 parse(true); 574 } 575 catch (XNIException ex) { 576 if (PRINT_EXCEPTION_STACK_TRACE) 577 ex.printStackTrace(); 578 throw ex; 579 } 580 catch (IOException ex) { 581 if (PRINT_EXCEPTION_STACK_TRACE) 582 ex.printStackTrace(); 583 throw ex; 584 } 585 catch (RuntimeException ex) { 586 if (PRINT_EXCEPTION_STACK_TRACE) 587 ex.printStackTrace(); 588 throw ex; 589 } 590 catch (Exception ex) { 591 if (PRINT_EXCEPTION_STACK_TRACE) 592 ex.printStackTrace(); 593 throw new XNIException(ex); 594 } 595 finally { 596 fParseInProgress = false; 597 this.cleanup(); 599 } 600 601 } 603 607 612 protected void reset() throws XNIException { 613 614 if (fValidationManager != null) 615 fValidationManager.reset(); 616 configurePipeline(); 618 super.reset(); 619 620 } 622 623 protected void configurePipeline() { 624 if (fFeatures.get(NAMESPACES) == Boolean.TRUE) { 627 if (fNamespaceScanner == null) { 628 fNamespaceScanner = new XMLNSDocumentScannerImpl(); 629 addComponent((XMLComponent)fNamespaceScanner); 630 } 631 fProperties.put(DOCUMENT_SCANNER, fNamespaceScanner); 632 fNamespaceScanner.setDTDValidator(null); 633 fScanner = fNamespaceScanner; 634 } 635 else { 636 if (fNonNSScanner == null) { 637 fNonNSScanner = new XMLDocumentScannerImpl(); 638 addComponent((XMLComponent)fNonNSScanner); 639 } 640 fProperties.put(DOCUMENT_SCANNER, fNonNSScanner); 641 fScanner = fNonNSScanner; 642 } 643 644 fScanner.setDocumentHandler(fDocumentHandler); 645 fLastComponent = fScanner; 646 if (fDTDScanner != null) { 648 fDTDScanner.setDTDHandler(fDTDHandler); 649 fDTDScanner.setDTDContentModelHandler(fDTDContentModelHandler); 650 } 651 652 653 } 655 657 669 protected void checkFeature(String featureId) 670 throws XMLConfigurationException { 671 672 676 if (featureId.startsWith(Constants.XERCES_FEATURE_PREFIX)) { 677 final int suffixLength = featureId.length() - Constants.XERCES_FEATURE_PREFIX.length(); 678 679 if (suffixLength == Constants.DYNAMIC_VALIDATION_FEATURE.length() && 686 featureId.endsWith(Constants.DYNAMIC_VALIDATION_FEATURE)) { 687 return; 688 } 689 if (suffixLength == Constants.DEFAULT_ATTRIBUTE_VALUES_FEATURE.length() && 693 featureId.endsWith(Constants.DEFAULT_ATTRIBUTE_VALUES_FEATURE)) { 694 short type = XMLConfigurationException.NOT_SUPPORTED; 696 throw new XMLConfigurationException(type, featureId); 697 } 698 if (suffixLength == Constants.VALIDATE_CONTENT_MODELS_FEATURE.length() && 702 featureId.endsWith(Constants.VALIDATE_CONTENT_MODELS_FEATURE)) { 703 short type = XMLConfigurationException.NOT_SUPPORTED; 705 throw new XMLConfigurationException(type, featureId); 706 } 707 if (suffixLength == Constants.LOAD_DTD_GRAMMAR_FEATURE.length() && 711 featureId.endsWith(Constants.LOAD_DTD_GRAMMAR_FEATURE)) { 712 return; 713 } 714 if (suffixLength == Constants.LOAD_EXTERNAL_DTD_FEATURE.length() && 718 featureId.endsWith(Constants.LOAD_EXTERNAL_DTD_FEATURE)) { 719 return; 720 } 721 722 if (suffixLength == Constants.VALIDATE_DATATYPES_FEATURE.length() && 726 featureId.endsWith(Constants.VALIDATE_DATATYPES_FEATURE)) { 727 short type = XMLConfigurationException.NOT_SUPPORTED; 728 throw new XMLConfigurationException(type, featureId); 729 } 730 } 731 732 736 super.checkFeature(featureId); 737 738 } 740 753 protected void checkProperty(String propertyId) 754 throws XMLConfigurationException { 755 756 760 if (propertyId.startsWith(Constants.XERCES_PROPERTY_PREFIX)) { 761 final int suffixLength = propertyId.length() - Constants.XERCES_PROPERTY_PREFIX.length(); 762 763 if (suffixLength == Constants.DTD_SCANNER_PROPERTY.length() && 764 propertyId.endsWith(Constants.DTD_SCANNER_PROPERTY)) { 765 return; 766 } 767 } 768 769 if (propertyId.startsWith(Constants.JAXP_PROPERTY_PREFIX)) { 770 final int suffixLength = propertyId.length() - Constants.JAXP_PROPERTY_PREFIX.length(); 771 772 if (suffixLength == Constants.SCHEMA_SOURCE.length() && 773 propertyId.endsWith(Constants.SCHEMA_SOURCE)) { 774 return; 775 } 776 } 777 778 782 super.checkProperty(propertyId); 783 784 } 786 788 789 protected XMLEntityManager createEntityManager() { 790 return new XMLEntityManager(); 791 } 793 794 protected XMLErrorReporter createErrorReporter() { 795 return new XMLErrorReporter(); 796 } 798 799 protected XMLDocumentScanner createDocumentScanner() { 800 return null; 801 } 803 804 protected XMLDTDScanner createDTDScanner() { 805 return new XMLDTDScannerImpl(); 806 } 808 809 protected DTDDVFactory createDatatypeValidatorFactory() { 810 return DTDDVFactory.getInstance(); 811 } protected ValidationManager createValidationManager(){ 813 return new ValidationManager(); 814 } 815 816 } | Popular Tags |