1 16 17 package org.apache.xerces.parsers; 18 19 import java.io.IOException ; 20 import java.util.Locale ; 21 22 import org.apache.xerces.impl.Constants; 23 import org.apache.xerces.impl.XMLDTDScannerImpl; 24 import org.apache.xerces.impl.XMLDocumentScannerImpl; 25 import org.apache.xerces.impl.XMLEntityManager; 26 import org.apache.xerces.impl.XMLErrorReporter; 27 import org.apache.xerces.impl.XMLNSDocumentScannerImpl; 28 import org.apache.xerces.impl.dv.DTDDVFactory; 29 import org.apache.xerces.impl.msg.XMLMessageFormatter; 30 import org.apache.xerces.impl.validation.ValidationManager; 31 import org.apache.xerces.util.SymbolTable; 32 import org.apache.xerces.xni.XMLLocator; 33 import org.apache.xerces.xni.XNIException; 34 import org.apache.xerces.xni.grammars.XMLGrammarPool; 35 import org.apache.xerces.xni.parser.XMLComponent; 36 import org.apache.xerces.xni.parser.XMLComponentManager; 37 import org.apache.xerces.xni.parser.XMLConfigurationException; 38 import org.apache.xerces.xni.parser.XMLDTDScanner; 39 import org.apache.xerces.xni.parser.XMLDocumentScanner; 40 import org.apache.xerces.xni.parser.XMLInputSource; 41 import org.apache.xerces.xni.parser.XMLPullParserConfiguration; 42 43 56 public class NonValidatingConfiguration 57 extends BasicParserConfiguration 58 implements XMLPullParserConfiguration { 59 60 64 66 67 protected static final String WARN_ON_DUPLICATE_ATTDEF = 68 Constants.XERCES_FEATURE_PREFIX + Constants.WARN_ON_DUPLICATE_ATTDEF_FEATURE; 69 70 71 protected static final String WARN_ON_DUPLICATE_ENTITYDEF = 72 Constants.XERCES_FEATURE_PREFIX + Constants.WARN_ON_DUPLICATE_ENTITYDEF_FEATURE; 73 74 75 protected static final String WARN_ON_UNDECLARED_ELEMDEF = 76 Constants.XERCES_FEATURE_PREFIX + Constants.WARN_ON_UNDECLARED_ELEMDEF_FEATURE; 77 78 79 protected static final String ALLOW_JAVA_ENCODINGS = 80 Constants.XERCES_FEATURE_PREFIX + Constants.ALLOW_JAVA_ENCODINGS_FEATURE; 81 82 83 protected static final String CONTINUE_AFTER_FATAL_ERROR = 84 Constants.XERCES_FEATURE_PREFIX + Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE; 85 86 87 protected static final String LOAD_EXTERNAL_DTD = 88 Constants.XERCES_FEATURE_PREFIX + Constants.LOAD_EXTERNAL_DTD_FEATURE; 89 90 91 protected static final String NOTIFY_BUILTIN_REFS = 92 Constants.XERCES_FEATURE_PREFIX + Constants.NOTIFY_BUILTIN_REFS_FEATURE; 93 94 95 protected static final String NOTIFY_CHAR_REFS = 96 Constants.XERCES_FEATURE_PREFIX + Constants.NOTIFY_CHAR_REFS_FEATURE; 97 98 99 100 protected static final String NORMALIZE_DATA = 101 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_NORMALIZED_VALUE; 102 103 104 105 protected static final String SCHEMA_ELEMENT_DEFAULT = 106 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_ELEMENT_DEFAULT; 107 108 110 111 protected static final String ERROR_REPORTER = 112 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY; 113 114 115 protected static final String ENTITY_MANAGER = 116 Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_MANAGER_PROPERTY; 117 118 119 protected static final String DOCUMENT_SCANNER = 120 Constants.XERCES_PROPERTY_PREFIX + Constants.DOCUMENT_SCANNER_PROPERTY; 121 122 123 protected static final String DTD_SCANNER = 124 Constants.XERCES_PROPERTY_PREFIX + Constants.DTD_SCANNER_PROPERTY; 125 126 127 protected static final String XMLGRAMMAR_POOL = 128 Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY; 129 130 131 protected static final String DTD_VALIDATOR = 132 Constants.XERCES_PROPERTY_PREFIX + Constants.DTD_VALIDATOR_PROPERTY; 133 134 135 protected static final String NAMESPACE_BINDER = 136 Constants.XERCES_PROPERTY_PREFIX + Constants.NAMESPACE_BINDER_PROPERTY; 137 138 139 protected static final String DATATYPE_VALIDATOR_FACTORY = 140 Constants.XERCES_PROPERTY_PREFIX + Constants.DATATYPE_VALIDATOR_FACTORY_PROPERTY; 141 142 protected static final String VALIDATION_MANAGER = 143 Constants.XERCES_PROPERTY_PREFIX + Constants.VALIDATION_MANAGER_PROPERTY; 144 145 146 protected static final String SCHEMA_VALIDATOR = 147 Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_VALIDATOR_PROPERTY; 148 149 150 152 153 private static final boolean PRINT_EXCEPTION_STACK_TRACE = false; 154 155 159 161 162 protected XMLGrammarPool fGrammarPool; 163 164 165 protected DTDDVFactory fDatatypeValidatorFactory; 166 167 169 170 protected XMLErrorReporter fErrorReporter; 171 172 173 protected XMLEntityManager fEntityManager; 174 175 176 protected XMLDocumentScanner fScanner; 177 178 179 protected XMLInputSource fInputSource; 180 181 182 protected XMLDTDScanner fDTDScanner; 183 184 185 protected ValidationManager fValidationManager; 186 187 189 190 private XMLNSDocumentScannerImpl fNamespaceScanner; 191 192 193 private XMLDocumentScannerImpl fNonNSScanner; 194 195 196 199 protected boolean fConfigUpdated = false; 200 201 202 204 205 protected XMLLocator fLocator; 206 207 212 protected boolean fParseInProgress = false; 213 214 218 219 public NonValidatingConfiguration() { 220 this(null, null, null); 221 } 223 228 public NonValidatingConfiguration(SymbolTable symbolTable) { 229 this(symbolTable, null, null); 230 } 232 243 public NonValidatingConfiguration(SymbolTable symbolTable, 244 XMLGrammarPool grammarPool) { 245 this(symbolTable, grammarPool, null); 246 } 248 260 public NonValidatingConfiguration(SymbolTable symbolTable, 261 XMLGrammarPool grammarPool, 262 XMLComponentManager parentSettings) { 263 super(symbolTable, parentSettings); 264 265 final String [] recognizedFeatures = { 267 PARSER_SETTINGS, 268 NAMESPACES, 269 CONTINUE_AFTER_FATAL_ERROR, 273 }; 278 addRecognizedFeatures(recognizedFeatures); 279 280 fFeatures.put(CONTINUE_AFTER_FATAL_ERROR, Boolean.FALSE); 285 fFeatures.put(PARSER_SETTINGS, Boolean.TRUE); 286 fFeatures.put(NAMESPACES, Boolean.TRUE); 287 292 final String [] recognizedProperties = { 294 ERROR_REPORTER, 295 ENTITY_MANAGER, 296 DOCUMENT_SCANNER, 297 DTD_SCANNER, 298 DTD_VALIDATOR, 299 NAMESPACE_BINDER, 300 XMLGRAMMAR_POOL, 301 DATATYPE_VALIDATOR_FACTORY, 302 VALIDATION_MANAGER 303 }; 304 addRecognizedProperties(recognizedProperties); 305 306 fGrammarPool = grammarPool; 307 if(fGrammarPool != null){ 308 fProperties.put(XMLGRAMMAR_POOL, fGrammarPool); 309 } 310 311 fEntityManager = createEntityManager(); 312 fProperties.put(ENTITY_MANAGER, fEntityManager); 313 addComponent(fEntityManager); 314 315 fErrorReporter = createErrorReporter(); 316 fErrorReporter.setDocumentLocator(fEntityManager.getEntityScanner()); 317 fProperties.put(ERROR_REPORTER, fErrorReporter); 318 addComponent(fErrorReporter); 319 320 323 fDTDScanner = createDTDScanner(); 324 if (fDTDScanner != null) { 325 fProperties.put(DTD_SCANNER, fDTDScanner); 326 if (fDTDScanner instanceof XMLComponent) { 327 addComponent((XMLComponent)fDTDScanner); 328 } 329 } 330 331 fDatatypeValidatorFactory = createDatatypeValidatorFactory(); 332 if (fDatatypeValidatorFactory != null) { 333 fProperties.put(DATATYPE_VALIDATOR_FACTORY, 334 fDatatypeValidatorFactory); 335 } 336 fValidationManager = createValidationManager(); 337 338 if (fValidationManager != null) { 339 fProperties.put(VALIDATION_MANAGER, fValidationManager); 340 } 341 if (fErrorReporter.getMessageFormatter(XMLMessageFormatter.XML_DOMAIN) == null) { 343 XMLMessageFormatter xmft = new XMLMessageFormatter(); 344 fErrorReporter.putMessageFormatter(XMLMessageFormatter.XML_DOMAIN, xmft); 345 fErrorReporter.putMessageFormatter(XMLMessageFormatter.XMLNS_DOMAIN, xmft); 346 } 347 348 fConfigUpdated = false; 349 350 try { 352 setLocale(Locale.getDefault()); 353 } 354 catch (XNIException e) { 355 } 358 359 } 361 public void setFeature(String featureId, boolean state) 365 throws XMLConfigurationException { 366 fConfigUpdated = true; 367 super.setFeature(featureId, state); 368 } 369 370 public void setProperty(String propertyId, Object value) 371 throws XMLConfigurationException { 372 fConfigUpdated = true; 373 super.setProperty(propertyId, value); 374 } 375 383 public void setLocale(Locale locale) throws XNIException { 384 super.setLocale(locale); 385 fErrorReporter.setLocale(locale); 386 } 388 public boolean getFeature(String featureId) 389 throws XMLConfigurationException { 390 if (featureId.equals(PARSER_SETTINGS)){ 392 return fConfigUpdated; 393 } 394 return super.getFeature(featureId); 395 396 } 401 403 415 public void setInputSource(XMLInputSource inputSource) 416 throws XMLConfigurationException, IOException { 417 418 423 fInputSource = inputSource; 424 425 } 427 443 public boolean parse(boolean complete) throws XNIException, IOException { 444 if (fInputSource !=null) { 447 try { 448 reset(); 450 fScanner.setInputSource(fInputSource); 451 fInputSource = null; 452 } 453 catch (XNIException ex) { 454 if (PRINT_EXCEPTION_STACK_TRACE) 455 ex.printStackTrace(); 456 throw ex; 457 } 458 catch (IOException ex) { 459 if (PRINT_EXCEPTION_STACK_TRACE) 460 ex.printStackTrace(); 461 throw ex; 462 } 463 catch (RuntimeException ex) { 464 if (PRINT_EXCEPTION_STACK_TRACE) 465 ex.printStackTrace(); 466 throw ex; 467 } 468 catch (Exception ex) { 469 if (PRINT_EXCEPTION_STACK_TRACE) 470 ex.printStackTrace(); 471 throw new XNIException(ex); 472 } 473 } 474 475 try { 476 return fScanner.scanDocument(complete); 477 } 478 catch (XNIException ex) { 479 if (PRINT_EXCEPTION_STACK_TRACE) 480 ex.printStackTrace(); 481 throw ex; 482 } 483 catch (IOException ex) { 484 if (PRINT_EXCEPTION_STACK_TRACE) 485 ex.printStackTrace(); 486 throw ex; 487 } 488 catch (RuntimeException ex) { 489 if (PRINT_EXCEPTION_STACK_TRACE) 490 ex.printStackTrace(); 491 throw ex; 492 } 493 catch (Exception ex) { 494 if (PRINT_EXCEPTION_STACK_TRACE) 495 ex.printStackTrace(); 496 throw new XNIException(ex); 497 } 498 499 } 501 506 public void cleanup() { 507 fEntityManager.closeReaders(); 508 } 509 510 514 522 public void parse(XMLInputSource source) throws XNIException, IOException { 523 524 if (fParseInProgress) { 525 throw new XNIException("FWK005 parse may not be called while parsing."); 527 } 528 fParseInProgress = true; 529 530 try { 531 setInputSource(source); 532 parse(true); 533 } 534 catch (XNIException ex) { 535 if (PRINT_EXCEPTION_STACK_TRACE) 536 ex.printStackTrace(); 537 throw ex; 538 } 539 catch (IOException ex) { 540 if (PRINT_EXCEPTION_STACK_TRACE) 541 ex.printStackTrace(); 542 throw ex; 543 } 544 catch (RuntimeException ex) { 545 if (PRINT_EXCEPTION_STACK_TRACE) 546 ex.printStackTrace(); 547 throw ex; 548 } 549 catch (Exception ex) { 550 if (PRINT_EXCEPTION_STACK_TRACE) 551 ex.printStackTrace(); 552 throw new XNIException(ex); 553 } 554 finally { 555 fParseInProgress = false; 556 this.cleanup(); 558 } 559 560 } 562 566 571 protected void reset() throws XNIException { 572 573 if (fValidationManager != null) 574 fValidationManager.reset(); 575 configurePipeline(); 577 super.reset(); 578 579 } 581 582 protected void configurePipeline() { 583 if (fFeatures.get(NAMESPACES) == Boolean.TRUE) { 586 if (fNamespaceScanner == null) { 587 fNamespaceScanner = new XMLNSDocumentScannerImpl(); 588 addComponent((XMLComponent)fNamespaceScanner); 589 } 590 fProperties.put(DOCUMENT_SCANNER, fNamespaceScanner); 591 fNamespaceScanner.setDTDValidator(null); 592 fScanner = fNamespaceScanner; 593 } 594 else { 595 if (fNonNSScanner == null) { 596 fNonNSScanner = new XMLDocumentScannerImpl(); 597 addComponent((XMLComponent)fNonNSScanner); 598 } 599 fProperties.put(DOCUMENT_SCANNER, fNonNSScanner); 600 fScanner = fNonNSScanner; 601 } 602 603 fScanner.setDocumentHandler(fDocumentHandler); 604 fLastComponent = fScanner; 605 if (fDTDScanner != null) { 607 fDTDScanner.setDTDHandler(fDTDHandler); 608 fDTDScanner.setDTDContentModelHandler(fDTDContentModelHandler); 609 } 610 611 612 } 614 616 628 protected void checkFeature(String featureId) 629 throws XMLConfigurationException { 630 631 635 if (featureId.startsWith(Constants.XERCES_FEATURE_PREFIX)) { 636 final int suffixLength = featureId.length() - Constants.XERCES_FEATURE_PREFIX.length(); 637 638 if (suffixLength == Constants.DYNAMIC_VALIDATION_FEATURE.length() && 645 featureId.endsWith(Constants.DYNAMIC_VALIDATION_FEATURE)) { 646 return; 647 } 648 if (suffixLength == Constants.DEFAULT_ATTRIBUTE_VALUES_FEATURE.length() && 652 featureId.endsWith(Constants.DEFAULT_ATTRIBUTE_VALUES_FEATURE)) { 653 short type = XMLConfigurationException.NOT_SUPPORTED; 655 throw new XMLConfigurationException(type, featureId); 656 } 657 if (suffixLength == Constants.VALIDATE_CONTENT_MODELS_FEATURE.length() && 661 featureId.endsWith(Constants.VALIDATE_CONTENT_MODELS_FEATURE)) { 662 short type = XMLConfigurationException.NOT_SUPPORTED; 664 throw new XMLConfigurationException(type, featureId); 665 } 666 if (suffixLength == Constants.LOAD_DTD_GRAMMAR_FEATURE.length() && 670 featureId.endsWith(Constants.LOAD_DTD_GRAMMAR_FEATURE)) { 671 return; 672 } 673 if (suffixLength == Constants.LOAD_EXTERNAL_DTD_FEATURE.length() && 677 featureId.endsWith(Constants.LOAD_EXTERNAL_DTD_FEATURE)) { 678 return; 679 } 680 681 if (suffixLength == Constants.VALIDATE_DATATYPES_FEATURE.length() && 685 featureId.endsWith(Constants.VALIDATE_DATATYPES_FEATURE)) { 686 short type = XMLConfigurationException.NOT_SUPPORTED; 687 throw new XMLConfigurationException(type, featureId); 688 } 689 } 690 691 695 super.checkFeature(featureId); 696 697 } 699 712 protected void checkProperty(String propertyId) 713 throws XMLConfigurationException { 714 715 719 if (propertyId.startsWith(Constants.XERCES_PROPERTY_PREFIX)) { 720 final int suffixLength = propertyId.length() - Constants.XERCES_PROPERTY_PREFIX.length(); 721 722 if (suffixLength == Constants.DTD_SCANNER_PROPERTY.length() && 723 propertyId.endsWith(Constants.DTD_SCANNER_PROPERTY)) { 724 return; 725 } 726 } 727 728 if (propertyId.startsWith(Constants.JAXP_PROPERTY_PREFIX)) { 729 final int suffixLength = propertyId.length() - Constants.JAXP_PROPERTY_PREFIX.length(); 730 731 if (suffixLength == Constants.SCHEMA_SOURCE.length() && 732 propertyId.endsWith(Constants.SCHEMA_SOURCE)) { 733 return; 734 } 735 } 736 737 741 super.checkProperty(propertyId); 742 743 } 745 747 748 protected XMLEntityManager createEntityManager() { 749 return new XMLEntityManager(); 750 } 752 753 protected XMLErrorReporter createErrorReporter() { 754 return new XMLErrorReporter(); 755 } 757 758 protected XMLDocumentScanner createDocumentScanner() { 759 return null; 760 } 762 763 protected XMLDTDScanner createDTDScanner() { 764 return new XMLDTDScannerImpl(); 765 } 767 768 protected DTDDVFactory createDatatypeValidatorFactory() { 769 return DTDDVFactory.getInstance(); 770 } protected ValidationManager createValidationManager(){ 772 return new ValidationManager(); 773 } 774 775 } | Popular Tags |