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.XMLNamespaceBinder; 28 import org.apache.xerces.impl.dtd.XMLDTDProcessor; 29 import org.apache.xerces.impl.dtd.XMLDTDValidator; 30 import org.apache.xerces.impl.dv.DTDDVFactory; 31 import org.apache.xerces.impl.msg.XMLMessageFormatter; 32 import org.apache.xerces.impl.validation.ValidationManager; 33 import org.apache.xerces.util.SymbolTable; 34 import org.apache.xerces.xni.XMLLocator; 35 import org.apache.xerces.xni.XNIException; 36 import org.apache.xerces.xni.grammars.XMLGrammarPool; 37 import org.apache.xerces.xni.parser.XMLComponent; 38 import org.apache.xerces.xni.parser.XMLComponentManager; 39 import org.apache.xerces.xni.parser.XMLConfigurationException; 40 import org.apache.xerces.xni.parser.XMLDTDScanner; 41 import org.apache.xerces.xni.parser.XMLDocumentScanner; 42 import org.apache.xerces.xni.parser.XMLInputSource; 43 import org.apache.xerces.xni.parser.XMLPullParserConfiguration; 44 45 84 public class DTDConfiguration 85 extends BasicParserConfiguration 86 implements XMLPullParserConfiguration { 87 88 92 94 95 protected static final String WARN_ON_DUPLICATE_ATTDEF = 96 Constants.XERCES_FEATURE_PREFIX + Constants.WARN_ON_DUPLICATE_ATTDEF_FEATURE; 97 98 99 protected static final String WARN_ON_DUPLICATE_ENTITYDEF = 100 Constants.XERCES_FEATURE_PREFIX + Constants.WARN_ON_DUPLICATE_ENTITYDEF_FEATURE; 101 102 103 protected static final String WARN_ON_UNDECLARED_ELEMDEF = 104 Constants.XERCES_FEATURE_PREFIX + Constants.WARN_ON_UNDECLARED_ELEMDEF_FEATURE; 105 106 107 protected static final String ALLOW_JAVA_ENCODINGS = 108 Constants.XERCES_FEATURE_PREFIX + Constants.ALLOW_JAVA_ENCODINGS_FEATURE; 109 110 111 protected static final String CONTINUE_AFTER_FATAL_ERROR = 112 Constants.XERCES_FEATURE_PREFIX + Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE; 113 114 115 protected static final String LOAD_EXTERNAL_DTD = 116 Constants.XERCES_FEATURE_PREFIX + Constants.LOAD_EXTERNAL_DTD_FEATURE; 117 118 119 protected static final String NOTIFY_BUILTIN_REFS = 120 Constants.XERCES_FEATURE_PREFIX + Constants.NOTIFY_BUILTIN_REFS_FEATURE; 121 122 123 protected static final String NOTIFY_CHAR_REFS = 124 Constants.XERCES_FEATURE_PREFIX + Constants.NOTIFY_CHAR_REFS_FEATURE; 125 126 127 129 130 protected static final String ERROR_REPORTER = 131 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY; 132 133 134 protected static final String ENTITY_MANAGER = 135 Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_MANAGER_PROPERTY; 136 137 138 protected static final String DOCUMENT_SCANNER = 139 Constants.XERCES_PROPERTY_PREFIX + Constants.DOCUMENT_SCANNER_PROPERTY; 140 141 142 protected static final String DTD_SCANNER = 143 Constants.XERCES_PROPERTY_PREFIX + Constants.DTD_SCANNER_PROPERTY; 144 145 146 protected static final String XMLGRAMMAR_POOL = 147 Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY; 148 149 150 protected static final String DTD_PROCESSOR = 151 Constants.XERCES_PROPERTY_PREFIX + Constants.DTD_PROCESSOR_PROPERTY; 152 153 154 protected static final String DTD_VALIDATOR = 155 Constants.XERCES_PROPERTY_PREFIX + Constants.DTD_VALIDATOR_PROPERTY; 156 157 158 protected static final String NAMESPACE_BINDER = 159 Constants.XERCES_PROPERTY_PREFIX + Constants.NAMESPACE_BINDER_PROPERTY; 160 161 162 protected static final String DATATYPE_VALIDATOR_FACTORY = 163 Constants.XERCES_PROPERTY_PREFIX + Constants.DATATYPE_VALIDATOR_FACTORY_PROPERTY; 164 165 protected static final String VALIDATION_MANAGER = 166 Constants.XERCES_PROPERTY_PREFIX + Constants.VALIDATION_MANAGER_PROPERTY; 167 168 169 protected static final String JAXP_SCHEMA_LANGUAGE = 170 Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_LANGUAGE; 171 172 173 protected static final String JAXP_SCHEMA_SOURCE = 174 Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_SOURCE; 175 176 177 179 180 protected static final boolean PRINT_EXCEPTION_STACK_TRACE = false; 181 182 186 188 189 protected XMLGrammarPool fGrammarPool; 190 191 192 protected DTDDVFactory fDatatypeValidatorFactory; 193 194 196 197 protected XMLErrorReporter fErrorReporter; 198 199 200 protected XMLEntityManager fEntityManager; 201 202 203 protected XMLDocumentScanner fScanner; 204 205 206 protected XMLInputSource fInputSource; 207 208 209 protected XMLDTDScanner fDTDScanner; 210 211 212 protected XMLDTDProcessor fDTDProcessor; 213 214 215 protected XMLDTDValidator fDTDValidator; 216 217 218 protected XMLNamespaceBinder fNamespaceBinder; 219 220 protected ValidationManager fValidationManager; 221 223 224 protected XMLLocator fLocator; 225 226 231 protected boolean fParseInProgress = false; 232 233 237 238 public DTDConfiguration() { 239 this(null, null, null); 240 } 242 247 public DTDConfiguration(SymbolTable symbolTable) { 248 this(symbolTable, null, null); 249 } 251 262 public DTDConfiguration(SymbolTable symbolTable, 263 XMLGrammarPool grammarPool) { 264 this(symbolTable, grammarPool, null); 265 } 267 279 public DTDConfiguration(SymbolTable symbolTable, 280 XMLGrammarPool grammarPool, 281 XMLComponentManager parentSettings) { 282 super(symbolTable, parentSettings); 283 284 final String [] recognizedFeatures = { 286 CONTINUE_AFTER_FATAL_ERROR, 290 LOAD_EXTERNAL_DTD, }; 295 addRecognizedFeatures(recognizedFeatures); 296 297 setFeature(CONTINUE_AFTER_FATAL_ERROR, false); 302 setFeature(LOAD_EXTERNAL_DTD, true); 307 final String [] recognizedProperties = { 309 ERROR_REPORTER, 310 ENTITY_MANAGER, 311 DOCUMENT_SCANNER, 312 DTD_SCANNER, 313 DTD_PROCESSOR, 314 DTD_VALIDATOR, 315 NAMESPACE_BINDER, 316 XMLGRAMMAR_POOL, 317 DATATYPE_VALIDATOR_FACTORY, 318 VALIDATION_MANAGER, 319 JAXP_SCHEMA_SOURCE, 320 JAXP_SCHEMA_LANGUAGE 321 }; 322 addRecognizedProperties(recognizedProperties); 323 324 fGrammarPool = grammarPool; 325 if(fGrammarPool != null){ 326 setProperty(XMLGRAMMAR_POOL, fGrammarPool); 327 } 328 329 fEntityManager = createEntityManager(); 330 setProperty(ENTITY_MANAGER, fEntityManager); 331 addComponent(fEntityManager); 332 333 fErrorReporter = createErrorReporter(); 334 fErrorReporter.setDocumentLocator(fEntityManager.getEntityScanner()); 335 setProperty(ERROR_REPORTER, fErrorReporter); 336 addComponent(fErrorReporter); 337 338 fScanner = createDocumentScanner(); 339 setProperty(DOCUMENT_SCANNER, fScanner); 340 if (fScanner instanceof XMLComponent) { 341 addComponent((XMLComponent)fScanner); 342 } 343 344 fDTDScanner = createDTDScanner(); 345 if (fDTDScanner != null) { 346 setProperty(DTD_SCANNER, fDTDScanner); 347 if (fDTDScanner instanceof XMLComponent) { 348 addComponent((XMLComponent)fDTDScanner); 349 } 350 } 351 352 fDTDProcessor = createDTDProcessor(); 353 if (fDTDProcessor != null) { 354 setProperty(DTD_PROCESSOR, fDTDProcessor); 355 if (fDTDProcessor instanceof XMLComponent) { 356 addComponent((XMLComponent)fDTDProcessor); 357 } 358 } 359 360 fDTDValidator = createDTDValidator(); 361 if (fDTDValidator != null) { 362 setProperty(DTD_VALIDATOR, fDTDValidator); 363 addComponent(fDTDValidator); 364 } 365 366 fNamespaceBinder = createNamespaceBinder(); 367 if (fNamespaceBinder != null) { 368 setProperty(NAMESPACE_BINDER, fNamespaceBinder); 369 addComponent(fNamespaceBinder); 370 } 371 372 fDatatypeValidatorFactory = createDatatypeValidatorFactory(); 373 if (fDatatypeValidatorFactory != null) { 374 setProperty(DATATYPE_VALIDATOR_FACTORY, 375 fDatatypeValidatorFactory); 376 } 377 fValidationManager = createValidationManager(); 378 379 if (fValidationManager != null) { 380 setProperty (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 try { 391 setLocale(Locale.getDefault()); 392 } 393 catch (XNIException e) { 394 } 397 398 } 400 404 412 public void setLocale(Locale locale) throws XNIException { 413 super.setLocale(locale); 414 fErrorReporter.setLocale(locale); 415 } 417 421 423 435 public void setInputSource(XMLInputSource inputSource) 436 throws XMLConfigurationException, IOException { 437 438 443 fInputSource = inputSource; 444 445 } 447 463 public boolean parse(boolean complete) throws XNIException, IOException { 464 if (fInputSource !=null) { 467 try { 468 reset(); 470 fScanner.setInputSource(fInputSource); 471 fInputSource = null; 472 } 473 catch (XNIException ex) { 474 if (PRINT_EXCEPTION_STACK_TRACE) 475 ex.printStackTrace(); 476 throw ex; 477 } 478 catch (IOException ex) { 479 if (PRINT_EXCEPTION_STACK_TRACE) 480 ex.printStackTrace(); 481 throw ex; 482 } 483 catch (RuntimeException ex) { 484 if (PRINT_EXCEPTION_STACK_TRACE) 485 ex.printStackTrace(); 486 throw ex; 487 } 488 catch (Exception ex) { 489 if (PRINT_EXCEPTION_STACK_TRACE) 490 ex.printStackTrace(); 491 throw new XNIException(ex); 492 } 493 } 494 495 try { 496 return fScanner.scanDocument(complete); 497 } 498 catch (XNIException ex) { 499 if (PRINT_EXCEPTION_STACK_TRACE) 500 ex.printStackTrace(); 501 throw ex; 502 } 503 catch (IOException ex) { 504 if (PRINT_EXCEPTION_STACK_TRACE) 505 ex.printStackTrace(); 506 throw ex; 507 } 508 catch (RuntimeException ex) { 509 if (PRINT_EXCEPTION_STACK_TRACE) 510 ex.printStackTrace(); 511 throw ex; 512 } 513 catch (Exception ex) { 514 if (PRINT_EXCEPTION_STACK_TRACE) 515 ex.printStackTrace(); 516 throw new XNIException(ex); 517 } 518 519 } 521 526 public void cleanup() { 527 fEntityManager.closeReaders(); 528 } 529 530 534 542 public void parse(XMLInputSource source) throws XNIException, IOException { 543 544 if (fParseInProgress) { 545 throw new XNIException("FWK005 parse may not be called while parsing."); 547 } 548 fParseInProgress = true; 549 550 try { 551 setInputSource(source); 552 parse(true); 553 } 554 catch (XNIException ex) { 555 if (PRINT_EXCEPTION_STACK_TRACE) 556 ex.printStackTrace(); 557 throw ex; 558 } 559 catch (IOException ex) { 560 if (PRINT_EXCEPTION_STACK_TRACE) 561 ex.printStackTrace(); 562 throw ex; 563 } 564 catch (RuntimeException ex) { 565 if (PRINT_EXCEPTION_STACK_TRACE) 566 ex.printStackTrace(); 567 throw ex; 568 } 569 catch (Exception ex) { 570 if (PRINT_EXCEPTION_STACK_TRACE) 571 ex.printStackTrace(); 572 throw new XNIException(ex); 573 } 574 finally { 575 fParseInProgress = false; 576 this.cleanup(); 578 } 579 580 } 582 586 591 protected void reset() throws XNIException { 592 593 if (fValidationManager != null) 594 fValidationManager.reset(); 595 configurePipeline(); 597 super.reset(); 598 } 600 601 protected void configurePipeline() { 602 603 610 if (fDTDValidator != null) { 612 fScanner.setDocumentHandler(fDTDValidator); 613 if (fFeatures.get(NAMESPACES) == Boolean.TRUE) { 614 615 fDTDValidator.setDocumentHandler(fNamespaceBinder); 617 fDTDValidator.setDocumentSource(fScanner); 618 fNamespaceBinder.setDocumentHandler(fDocumentHandler); 619 fNamespaceBinder.setDocumentSource(fDTDValidator); 620 fLastComponent = fNamespaceBinder; 621 } 622 else { 623 fDTDValidator.setDocumentHandler(fDocumentHandler); 624 fDTDValidator.setDocumentSource(fScanner); 625 fLastComponent = fDTDValidator; 626 } 627 } 628 else { 629 if (fFeatures.get(NAMESPACES) == Boolean.TRUE) { 630 fScanner.setDocumentHandler(fNamespaceBinder); 631 fNamespaceBinder.setDocumentHandler(fDocumentHandler); 632 fNamespaceBinder.setDocumentSource(fScanner); 633 fLastComponent = fNamespaceBinder; 634 } 635 else { 636 fScanner.setDocumentHandler(fDocumentHandler); 637 fLastComponent = fScanner; 638 } 639 } 640 641 configureDTDPipeline(); 642 } 644 protected void configureDTDPipeline (){ 645 646 if (fDTDScanner != null) { 648 fProperties.put(DTD_SCANNER, fDTDScanner); 649 if (fDTDProcessor != null) { 650 fProperties.put(DTD_PROCESSOR, fDTDProcessor); 651 fDTDScanner.setDTDHandler(fDTDProcessor); 652 fDTDProcessor.setDTDSource(fDTDScanner); 653 fDTDProcessor.setDTDHandler(fDTDHandler); 654 if (fDTDHandler != null) { 655 fDTDHandler.setDTDSource(fDTDProcessor); 656 } 657 658 fDTDScanner.setDTDContentModelHandler(fDTDProcessor); 659 fDTDProcessor.setDTDContentModelSource(fDTDScanner); 660 fDTDProcessor.setDTDContentModelHandler(fDTDContentModelHandler); 661 if (fDTDContentModelHandler != null) { 662 fDTDContentModelHandler.setDTDContentModelSource(fDTDProcessor); 663 } 664 } 665 else { 666 fDTDScanner.setDTDHandler(fDTDHandler); 667 if (fDTDHandler != null) { 668 fDTDHandler.setDTDSource(fDTDScanner); 669 } 670 fDTDScanner.setDTDContentModelHandler(fDTDContentModelHandler); 671 if (fDTDContentModelHandler != null) { 672 fDTDContentModelHandler.setDTDContentModelSource(fDTDScanner); 673 } 674 } 675 } 676 677 678 } 679 680 682 694 protected void checkFeature(String featureId) 695 throws XMLConfigurationException { 696 697 701 if (featureId.startsWith(Constants.XERCES_FEATURE_PREFIX)) { 702 final int suffixLength = featureId.length() - Constants.XERCES_FEATURE_PREFIX.length(); 703 704 if (suffixLength == Constants.DYNAMIC_VALIDATION_FEATURE.length() && 711 featureId.endsWith(Constants.DYNAMIC_VALIDATION_FEATURE)) { 712 return; 713 } 714 715 if (suffixLength == Constants.DEFAULT_ATTRIBUTE_VALUES_FEATURE.length() && 719 featureId.endsWith(Constants.DEFAULT_ATTRIBUTE_VALUES_FEATURE)) { 720 short type = XMLConfigurationException.NOT_SUPPORTED; 722 throw new XMLConfigurationException(type, featureId); 723 } 724 if (suffixLength == Constants.VALIDATE_CONTENT_MODELS_FEATURE.length() && 728 featureId.endsWith(Constants.VALIDATE_CONTENT_MODELS_FEATURE)) { 729 short type = XMLConfigurationException.NOT_SUPPORTED; 731 throw new XMLConfigurationException(type, featureId); 732 } 733 if (suffixLength == Constants.LOAD_DTD_GRAMMAR_FEATURE.length() && 737 featureId.endsWith(Constants.LOAD_DTD_GRAMMAR_FEATURE)) { 738 return; 739 } 740 if (suffixLength == Constants.LOAD_EXTERNAL_DTD_FEATURE.length() && 744 featureId.endsWith(Constants.LOAD_EXTERNAL_DTD_FEATURE)) { 745 return; 746 } 747 748 if (suffixLength == Constants.VALIDATE_DATATYPES_FEATURE.length() && 752 featureId.endsWith(Constants.VALIDATE_DATATYPES_FEATURE)) { 753 short type = XMLConfigurationException.NOT_SUPPORTED; 754 throw new XMLConfigurationException(type, featureId); 755 } 756 } 757 758 762 super.checkFeature(featureId); 763 764 } 766 779 protected void checkProperty(String propertyId) 780 throws XMLConfigurationException { 781 782 786 if (propertyId.startsWith(Constants.XERCES_PROPERTY_PREFIX)) { 787 final int suffixLength = propertyId.length() - Constants.XERCES_PROPERTY_PREFIX.length(); 788 789 if (suffixLength == Constants.DTD_SCANNER_PROPERTY.length() && 790 propertyId.endsWith(Constants.DTD_SCANNER_PROPERTY)) { 791 return; 792 } 793 } 794 795 799 super.checkProperty(propertyId); 800 801 } 803 805 806 protected XMLEntityManager createEntityManager() { 807 return new XMLEntityManager(); 808 } 810 811 protected XMLErrorReporter createErrorReporter() { 812 return new XMLErrorReporter(); 813 } 815 816 protected XMLDocumentScanner createDocumentScanner() { 817 return new XMLDocumentScannerImpl(); 818 } 820 821 protected XMLDTDScanner createDTDScanner() { 822 return new XMLDTDScannerImpl(); 823 } 825 826 protected XMLDTDProcessor createDTDProcessor() { 827 return new XMLDTDProcessor(); 828 } 830 831 protected XMLDTDValidator createDTDValidator() { 832 return new XMLDTDValidator(); 833 } 835 836 protected XMLNamespaceBinder createNamespaceBinder() { 837 return new XMLNamespaceBinder(); 838 } 840 841 protected DTDDVFactory createDatatypeValidatorFactory() { 842 return DTDDVFactory.getInstance(); 843 } protected ValidationManager createValidationManager(){ 845 return new ValidationManager(); 846 } 847 848 } | Popular Tags |