1 16 17 package org.apache.xerces.impl.xs.opti; 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.XMLEntityManager; 25 import org.apache.xerces.impl.XMLErrorReporter; 26 import org.apache.xerces.impl.XMLNSDocumentScannerImpl; 27 import org.apache.xerces.impl.dv.DTDDVFactory; 28 import org.apache.xerces.impl.msg.XMLMessageFormatter; 29 import org.apache.xerces.impl.validation.ValidationManager; 30 import org.apache.xerces.impl.xs.XSMessageFormatter; 31 import org.apache.xerces.parsers.BasicParserConfiguration; 32 import org.apache.xerces.util.SymbolTable; 33 import org.apache.xerces.xni.XMLLocator; 34 import org.apache.xerces.xni.XNIException; 35 import org.apache.xerces.xni.grammars.XMLGrammarPool; 36 import org.apache.xerces.xni.parser.XMLComponent; 37 import org.apache.xerces.xni.parser.XMLComponentManager; 38 import org.apache.xerces.xni.parser.XMLConfigurationException; 39 import org.apache.xerces.xni.parser.XMLDTDScanner; 40 import org.apache.xerces.xni.parser.XMLDocumentScanner; 41 import org.apache.xerces.xni.parser.XMLInputSource; 42 import org.apache.xerces.xni.parser.XMLPullParserConfiguration; 43 import org.w3c.dom.Document ; 44 45 46 53 public class SchemaParsingConfig extends BasicParserConfiguration 54 implements XMLPullParserConfiguration { 55 56 60 62 63 protected static final String WARN_ON_DUPLICATE_ATTDEF = 64 Constants.XERCES_FEATURE_PREFIX + Constants.WARN_ON_DUPLICATE_ATTDEF_FEATURE; 65 66 67 69 70 protected static final String WARN_ON_UNDECLARED_ELEMDEF = 71 Constants.XERCES_FEATURE_PREFIX + Constants.WARN_ON_UNDECLARED_ELEMDEF_FEATURE; 72 73 74 protected static final String ALLOW_JAVA_ENCODINGS = 75 Constants.XERCES_FEATURE_PREFIX + Constants.ALLOW_JAVA_ENCODINGS_FEATURE; 76 77 78 protected static final String CONTINUE_AFTER_FATAL_ERROR = 79 Constants.XERCES_FEATURE_PREFIX + Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE; 80 81 82 protected static final String LOAD_EXTERNAL_DTD = 83 Constants.XERCES_FEATURE_PREFIX + Constants.LOAD_EXTERNAL_DTD_FEATURE; 84 85 86 protected static final String NOTIFY_BUILTIN_REFS = 87 Constants.XERCES_FEATURE_PREFIX + Constants.NOTIFY_BUILTIN_REFS_FEATURE; 88 89 90 protected static final String NOTIFY_CHAR_REFS = 91 Constants.XERCES_FEATURE_PREFIX + Constants.NOTIFY_CHAR_REFS_FEATURE; 92 93 94 95 protected static final String NORMALIZE_DATA = 96 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_NORMALIZED_VALUE; 97 98 99 100 protected static final String SCHEMA_ELEMENT_DEFAULT = 101 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_ELEMENT_DEFAULT; 102 103 104 protected static final String GENERATE_SYNTHETIC_ANNOTATION = 105 Constants.XERCES_FEATURE_PREFIX + Constants.GENERATE_SYNTHETIC_ANNOTATIONS_FEATURE; 106 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 SchemaDOMParser fSchemaDOMParser; 186 187 protected ValidationManager fValidationManager; 188 190 191 protected XMLLocator fLocator; 192 193 198 protected boolean fParseInProgress = false; 199 200 204 205 public SchemaParsingConfig() { 206 this(null, null, null); 207 } 209 214 public SchemaParsingConfig(SymbolTable symbolTable) { 215 this(symbolTable, null, null); 216 } 218 229 public SchemaParsingConfig(SymbolTable symbolTable, 230 XMLGrammarPool grammarPool) { 231 this(symbolTable, grammarPool, null); 232 } 234 246 public SchemaParsingConfig(SymbolTable symbolTable, 247 XMLGrammarPool grammarPool, 248 XMLComponentManager parentSettings) { 249 super(symbolTable, parentSettings); 250 251 final String [] recognizedFeatures = { 253 PARSER_SETTINGS, WARN_ON_DUPLICATE_ATTDEF, WARN_ON_UNDECLARED_ELEMDEF, 254 ALLOW_JAVA_ENCODINGS, CONTINUE_AFTER_FATAL_ERROR, 255 LOAD_EXTERNAL_DTD, NOTIFY_BUILTIN_REFS, 256 NOTIFY_CHAR_REFS, GENERATE_SYNTHETIC_ANNOTATION 257 }; 258 addRecognizedFeatures(recognizedFeatures); 259 fFeatures.put(PARSER_SETTINGS, Boolean.TRUE); 260 fFeatures.put(WARN_ON_DUPLICATE_ATTDEF, Boolean.FALSE); 262 fFeatures.put(WARN_ON_UNDECLARED_ELEMDEF, Boolean.FALSE); 264 fFeatures.put(ALLOW_JAVA_ENCODINGS, Boolean.FALSE); 265 fFeatures.put(CONTINUE_AFTER_FATAL_ERROR, Boolean.FALSE); 266 fFeatures.put(LOAD_EXTERNAL_DTD, Boolean.TRUE); 267 fFeatures.put(NOTIFY_BUILTIN_REFS, Boolean.FALSE); 268 fFeatures.put(NOTIFY_CHAR_REFS, Boolean.FALSE); 269 fFeatures.put(GENERATE_SYNTHETIC_ANNOTATION, Boolean.FALSE); 270 271 final String [] recognizedProperties = { 273 ERROR_REPORTER, 274 ENTITY_MANAGER, 275 DOCUMENT_SCANNER, 276 DTD_SCANNER, 277 DTD_VALIDATOR, 278 NAMESPACE_BINDER, 279 XMLGRAMMAR_POOL, 280 DATATYPE_VALIDATOR_FACTORY, 281 VALIDATION_MANAGER, 282 GENERATE_SYNTHETIC_ANNOTATION 283 }; 284 addRecognizedProperties(recognizedProperties); 285 286 fGrammarPool = grammarPool; 287 if(fGrammarPool != null){ 288 setProperty(XMLGRAMMAR_POOL, fGrammarPool); 289 } 290 291 fEntityManager = new XMLEntityManager(); 292 fProperties.put(ENTITY_MANAGER, fEntityManager); 293 addComponent(fEntityManager); 294 295 fErrorReporter = new XMLErrorReporter(); 296 fErrorReporter.setDocumentLocator(fEntityManager.getEntityScanner()); 297 fProperties.put(ERROR_REPORTER, fErrorReporter); 298 addComponent(fErrorReporter); 299 300 fScanner = new XMLNSDocumentScannerImpl(); 301 fProperties.put(DOCUMENT_SCANNER, fScanner); 302 addComponent((XMLComponent)fScanner); 303 304 fDTDScanner = new XMLDTDScannerImpl(); 305 fProperties.put(DTD_SCANNER, fDTDScanner); 306 addComponent((XMLComponent)fDTDScanner); 307 308 309 fDatatypeValidatorFactory = DTDDVFactory.getInstance();; 310 fProperties.put(DATATYPE_VALIDATOR_FACTORY, 311 fDatatypeValidatorFactory); 312 313 fValidationManager = new ValidationManager(); 314 fProperties.put(VALIDATION_MANAGER, fValidationManager); 315 316 if (fErrorReporter.getMessageFormatter(XMLMessageFormatter.XML_DOMAIN) == null) { 318 XMLMessageFormatter xmft = new XMLMessageFormatter(); 319 fErrorReporter.putMessageFormatter(XMLMessageFormatter.XML_DOMAIN, xmft); 320 fErrorReporter.putMessageFormatter(XMLMessageFormatter.XMLNS_DOMAIN, xmft); 321 } 322 323 if (fErrorReporter.getMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN) == null) { 324 XSMessageFormatter xmft = new XSMessageFormatter(); 325 fErrorReporter.putMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN, xmft); 326 } 327 328 try { 330 setLocale(Locale.getDefault()); 331 } 332 catch (XNIException e) { 333 } 336 337 } 339 343 351 public void setLocale(Locale locale) throws XNIException { 352 super.setLocale(locale); 353 fErrorReporter.setLocale(locale); 354 } 356 360 362 374 public void setInputSource(XMLInputSource inputSource) 375 throws XMLConfigurationException, IOException { 376 377 382 fInputSource = inputSource; 383 384 } 386 402 public boolean parse(boolean complete) throws XNIException, IOException { 403 if (fInputSource !=null) { 406 try { 407 reset(); 409 fScanner.setInputSource(fInputSource); 410 fInputSource = null; 411 } 412 catch (XNIException ex) { 413 if (PRINT_EXCEPTION_STACK_TRACE) 414 ex.printStackTrace(); 415 throw ex; 416 } 417 catch (IOException ex) { 418 if (PRINT_EXCEPTION_STACK_TRACE) 419 ex.printStackTrace(); 420 throw ex; 421 } 422 catch (RuntimeException ex) { 423 if (PRINT_EXCEPTION_STACK_TRACE) 424 ex.printStackTrace(); 425 throw ex; 426 } 427 catch (Exception ex) { 428 if (PRINT_EXCEPTION_STACK_TRACE) 429 ex.printStackTrace(); 430 throw new XNIException(ex); 431 } 432 } 433 434 try { 435 return fScanner.scanDocument(complete); 436 } 437 catch (XNIException ex) { 438 if (PRINT_EXCEPTION_STACK_TRACE) 439 ex.printStackTrace(); 440 throw ex; 441 } 442 catch (IOException ex) { 443 if (PRINT_EXCEPTION_STACK_TRACE) 444 ex.printStackTrace(); 445 throw ex; 446 } 447 catch (RuntimeException ex) { 448 if (PRINT_EXCEPTION_STACK_TRACE) 449 ex.printStackTrace(); 450 throw ex; 451 } 452 catch (Exception ex) { 453 if (PRINT_EXCEPTION_STACK_TRACE) 454 ex.printStackTrace(); 455 throw new XNIException(ex); 456 } 457 458 } 460 465 public void cleanup() { 466 fEntityManager.closeReaders(); 467 } 468 469 473 481 public void parse(XMLInputSource source) throws XNIException, IOException { 482 483 if (fParseInProgress) { 484 throw new XNIException("FWK005 parse may not be called while parsing."); 486 } 487 fParseInProgress = true; 488 489 try { 490 setInputSource(source); 491 parse(true); 492 } 493 catch (XNIException ex) { 494 if (PRINT_EXCEPTION_STACK_TRACE) 495 ex.printStackTrace(); 496 throw ex; 497 } 498 catch (IOException ex) { 499 if (PRINT_EXCEPTION_STACK_TRACE) 500 ex.printStackTrace(); 501 throw ex; 502 } 503 catch (RuntimeException ex) { 504 if (PRINT_EXCEPTION_STACK_TRACE) 505 ex.printStackTrace(); 506 throw ex; 507 } 508 catch (Exception ex) { 509 if (PRINT_EXCEPTION_STACK_TRACE) 510 ex.printStackTrace(); 511 throw new XNIException(ex); 512 } 513 finally { 514 fParseInProgress = false; 515 this.cleanup(); 517 } 518 519 } 521 525 530 public void reset() throws XNIException { 531 532 if (fSchemaDOMParser == null) 534 fSchemaDOMParser = new SchemaDOMParser(this); 535 fDocumentHandler = fSchemaDOMParser; 536 fDTDHandler = fSchemaDOMParser; 537 fDTDContentModelHandler = fSchemaDOMParser; 538 539 configurePipeline(); 541 super.reset(); 542 543 } 545 546 protected void configurePipeline() { 547 548 fScanner.setDocumentHandler(fDocumentHandler); 550 fDocumentHandler.setDocumentSource(fScanner); 551 fLastComponent = fScanner; 552 553 if (fDTDScanner != null) { 555 fDTDScanner.setDTDHandler(fDTDHandler); 556 fDTDScanner.setDTDContentModelHandler(fDTDContentModelHandler); 557 } 558 559 560 } 562 564 576 protected void checkFeature(String featureId) 577 throws XMLConfigurationException { 578 579 583 if (featureId.startsWith(Constants.XERCES_FEATURE_PREFIX)) { 584 final int suffixLength = featureId.length() - Constants.XERCES_FEATURE_PREFIX.length(); 585 586 if (suffixLength == Constants.DYNAMIC_VALIDATION_FEATURE.length() && 593 featureId.endsWith(Constants.DYNAMIC_VALIDATION_FEATURE)) { 594 return; 595 } 596 if (suffixLength == Constants.DEFAULT_ATTRIBUTE_VALUES_FEATURE.length() && 600 featureId.endsWith(Constants.DEFAULT_ATTRIBUTE_VALUES_FEATURE)) { 601 short type = XMLConfigurationException.NOT_SUPPORTED; 603 throw new XMLConfigurationException(type, featureId); 604 } 605 if (suffixLength == Constants.VALIDATE_CONTENT_MODELS_FEATURE.length() && 609 featureId.endsWith(Constants.VALIDATE_CONTENT_MODELS_FEATURE)) { 610 short type = XMLConfigurationException.NOT_SUPPORTED; 612 throw new XMLConfigurationException(type, featureId); 613 } 614 if (suffixLength == Constants.LOAD_DTD_GRAMMAR_FEATURE.length() && 618 featureId.endsWith(Constants.LOAD_DTD_GRAMMAR_FEATURE)) { 619 return; 620 } 621 if (suffixLength == Constants.LOAD_EXTERNAL_DTD_FEATURE.length() && 625 featureId.endsWith(Constants.LOAD_EXTERNAL_DTD_FEATURE)) { 626 return; 627 } 628 629 if (suffixLength == Constants.VALIDATE_DATATYPES_FEATURE.length() && 633 featureId.endsWith(Constants.VALIDATE_DATATYPES_FEATURE)) { 634 short type = XMLConfigurationException.NOT_SUPPORTED; 635 throw new XMLConfigurationException(type, featureId); 636 } 637 } 638 639 643 super.checkFeature(featureId); 644 645 } 647 660 protected void checkProperty(String propertyId) 661 throws XMLConfigurationException { 662 663 667 if (propertyId.startsWith(Constants.XERCES_PROPERTY_PREFIX)) { 668 final int suffixLength = propertyId.length() - Constants.XERCES_PROPERTY_PREFIX.length(); 669 670 if (suffixLength == Constants.DTD_SCANNER_PROPERTY.length() && 671 propertyId.endsWith(Constants.DTD_SCANNER_PROPERTY)) { 672 return; 673 } 674 } 675 676 if (propertyId.startsWith(Constants.JAXP_PROPERTY_PREFIX)) { 677 final int suffixLength = propertyId.length() - Constants.JAXP_PROPERTY_PREFIX.length(); 678 679 if (suffixLength == Constants.SCHEMA_SOURCE.length() && 680 propertyId.endsWith(Constants.SCHEMA_SOURCE)) { 681 return; 682 } 683 } 684 685 689 super.checkProperty(propertyId); 690 691 } 693 694 695 699 700 public Document getDocument() { 701 return fSchemaDOMParser.getDocument(); 702 } 703 704 705 public void resetNodePool() { 706 } 709 } 710 | Popular Tags |