1 57 58 package com.sun.org.apache.xerces.internal.impl.xs.opti; 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.XMLEntityManager; 66 import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter; 67 import com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl; 68 import com.sun.org.apache.xerces.internal.impl.dv.DTDDVFactory; 69 import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter; 70 import com.sun.org.apache.xerces.internal.impl.validation.ValidationManager; 71 import com.sun.org.apache.xerces.internal.impl.xs.XSMessageFormatter; 72 import com.sun.org.apache.xerces.internal.parsers.BasicParserConfiguration; 73 import com.sun.org.apache.xerces.internal.util.SymbolTable; 74 import com.sun.org.apache.xerces.internal.xni.XMLLocator; 75 import com.sun.org.apache.xerces.internal.xni.XNIException; 76 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarPool; 77 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponent; 78 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager; 79 import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException; 80 import com.sun.org.apache.xerces.internal.xni.parser.XMLDTDScanner; 81 import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentScanner; 82 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource; 83 import com.sun.org.apache.xerces.internal.xni.parser.XMLPullParserConfiguration; 84 import org.w3c.dom.Document ; 85 86 87 92 public class SchemaParsingConfig extends BasicParserConfiguration 93 implements XMLPullParserConfiguration { 94 95 99 101 102 protected static final String WARN_ON_DUPLICATE_ATTDEF = 103 Constants.XERCES_FEATURE_PREFIX + Constants.WARN_ON_DUPLICATE_ATTDEF_FEATURE; 104 105 106 108 109 protected static final String WARN_ON_UNDECLARED_ELEMDEF = 110 Constants.XERCES_FEATURE_PREFIX + Constants.WARN_ON_UNDECLARED_ELEMDEF_FEATURE; 111 112 113 protected static final String ALLOW_JAVA_ENCODINGS = 114 Constants.XERCES_FEATURE_PREFIX + Constants.ALLOW_JAVA_ENCODINGS_FEATURE; 115 116 117 protected static final String CONTINUE_AFTER_FATAL_ERROR = 118 Constants.XERCES_FEATURE_PREFIX + Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE; 119 120 121 protected static final String LOAD_EXTERNAL_DTD = 122 Constants.XERCES_FEATURE_PREFIX + Constants.LOAD_EXTERNAL_DTD_FEATURE; 123 124 125 protected static final String NOTIFY_BUILTIN_REFS = 126 Constants.XERCES_FEATURE_PREFIX + Constants.NOTIFY_BUILTIN_REFS_FEATURE; 127 128 129 protected static final String NOTIFY_CHAR_REFS = 130 Constants.XERCES_FEATURE_PREFIX + Constants.NOTIFY_CHAR_REFS_FEATURE; 131 132 133 134 protected static final String NORMALIZE_DATA = 135 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_NORMALIZED_VALUE; 136 137 138 139 protected static final String SCHEMA_ELEMENT_DEFAULT = 140 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_ELEMENT_DEFAULT; 141 142 143 145 146 protected static final String ERROR_REPORTER = 147 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY; 148 149 150 protected static final String ENTITY_MANAGER = 151 Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_MANAGER_PROPERTY; 152 153 154 protected static final String DOCUMENT_SCANNER = 155 Constants.XERCES_PROPERTY_PREFIX + Constants.DOCUMENT_SCANNER_PROPERTY; 156 157 158 protected static final String DTD_SCANNER = 159 Constants.XERCES_PROPERTY_PREFIX + Constants.DTD_SCANNER_PROPERTY; 160 161 162 protected static final String XMLGRAMMAR_POOL = 163 Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY; 164 165 166 protected static final String DTD_VALIDATOR = 167 Constants.XERCES_PROPERTY_PREFIX + Constants.DTD_VALIDATOR_PROPERTY; 168 169 170 protected static final String NAMESPACE_BINDER = 171 Constants.XERCES_PROPERTY_PREFIX + Constants.NAMESPACE_BINDER_PROPERTY; 172 173 174 protected static final String DATATYPE_VALIDATOR_FACTORY = 175 Constants.XERCES_PROPERTY_PREFIX + Constants.DATATYPE_VALIDATOR_FACTORY_PROPERTY; 176 177 protected static final String VALIDATION_MANAGER = 178 Constants.XERCES_PROPERTY_PREFIX + Constants.VALIDATION_MANAGER_PROPERTY; 179 180 181 protected static final String SCHEMA_VALIDATOR = 182 Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_VALIDATOR_PROPERTY; 183 184 185 187 188 private static final boolean PRINT_EXCEPTION_STACK_TRACE = false; 189 190 194 196 197 protected XMLGrammarPool fGrammarPool; 198 199 200 protected DTDDVFactory fDatatypeValidatorFactory; 201 202 204 205 protected XMLErrorReporter fErrorReporter; 206 207 208 protected XMLEntityManager fEntityManager; 209 210 211 protected XMLDocumentScanner fScanner; 212 213 214 protected XMLInputSource fInputSource; 215 216 217 protected XMLDTDScanner fDTDScanner; 218 219 220 protected SchemaDOMParser fSchemaDOMParser; 221 222 protected ValidationManager fValidationManager; 223 225 226 protected XMLLocator fLocator; 227 228 233 protected boolean fParseInProgress = false; 234 235 239 240 public SchemaParsingConfig() { 241 this(null, null, null); 242 } 244 249 public SchemaParsingConfig(SymbolTable symbolTable) { 250 this(symbolTable, null, null); 251 } 253 264 public SchemaParsingConfig(SymbolTable symbolTable, 265 XMLGrammarPool grammarPool) { 266 this(symbolTable, grammarPool, null); 267 } 269 281 public SchemaParsingConfig(SymbolTable symbolTable, 282 XMLGrammarPool grammarPool, 283 XMLComponentManager parentSettings) { 284 super(symbolTable, parentSettings); 285 286 final String [] recognizedFeatures = { 288 PARSER_SETTINGS, WARN_ON_DUPLICATE_ATTDEF, WARN_ON_UNDECLARED_ELEMDEF, 289 ALLOW_JAVA_ENCODINGS, CONTINUE_AFTER_FATAL_ERROR, 290 LOAD_EXTERNAL_DTD, NOTIFY_BUILTIN_REFS, 291 NOTIFY_CHAR_REFS 292 }; 293 addRecognizedFeatures(recognizedFeatures); 294 fFeatures.put(PARSER_SETTINGS, Boolean.TRUE); 295 fFeatures.put(WARN_ON_DUPLICATE_ATTDEF, Boolean.FALSE); 297 fFeatures.put(WARN_ON_UNDECLARED_ELEMDEF, Boolean.FALSE); 299 fFeatures.put(ALLOW_JAVA_ENCODINGS, Boolean.FALSE); 300 fFeatures.put(CONTINUE_AFTER_FATAL_ERROR, Boolean.FALSE); 301 fFeatures.put(LOAD_EXTERNAL_DTD, Boolean.TRUE); 302 fFeatures.put(NOTIFY_BUILTIN_REFS, Boolean.FALSE); 303 fFeatures.put(NOTIFY_CHAR_REFS, Boolean.FALSE); 304 305 final String [] recognizedProperties = { 307 ERROR_REPORTER, 308 ENTITY_MANAGER, 309 DOCUMENT_SCANNER, 310 DTD_SCANNER, 311 DTD_VALIDATOR, 312 NAMESPACE_BINDER, 313 XMLGRAMMAR_POOL, 314 DATATYPE_VALIDATOR_FACTORY, 315 VALIDATION_MANAGER 316 }; 317 addRecognizedProperties(recognizedProperties); 318 319 fGrammarPool = grammarPool; 320 if(fGrammarPool != null){ 321 setProperty(XMLGRAMMAR_POOL, fGrammarPool); 322 } 323 324 fEntityManager = new XMLEntityManager(); 325 fProperties.put(ENTITY_MANAGER, fEntityManager); 326 addComponent(fEntityManager); 327 328 fErrorReporter = new XMLErrorReporter(); 329 fErrorReporter.setDocumentLocator(fEntityManager.getEntityScanner()); 330 fProperties.put(ERROR_REPORTER, fErrorReporter); 331 addComponent(fErrorReporter); 332 333 fScanner = new XMLNSDocumentScannerImpl(); 334 fProperties.put(DOCUMENT_SCANNER, fScanner); 335 addComponent((XMLComponent)fScanner); 336 337 fDTDScanner = new XMLDTDScannerImpl(); 338 fProperties.put(DTD_SCANNER, fDTDScanner); 339 addComponent((XMLComponent)fDTDScanner); 340 341 342 fDatatypeValidatorFactory = DTDDVFactory.getInstance();; 343 fProperties.put(DATATYPE_VALIDATOR_FACTORY, 344 fDatatypeValidatorFactory); 345 346 fValidationManager = new ValidationManager(); 347 fProperties.put(VALIDATION_MANAGER, fValidationManager); 348 349 if (fErrorReporter.getMessageFormatter(XMLMessageFormatter.XML_DOMAIN) == null) { 351 XMLMessageFormatter xmft = new XMLMessageFormatter(); 352 fErrorReporter.putMessageFormatter(XMLMessageFormatter.XML_DOMAIN, xmft); 353 fErrorReporter.putMessageFormatter(XMLMessageFormatter.XMLNS_DOMAIN, xmft); 354 } 355 356 if (fErrorReporter.getMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN) == null) { 357 XSMessageFormatter xmft = new XSMessageFormatter(); 358 fErrorReporter.putMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN, xmft); 359 } 360 361 try { 363 setLocale(Locale.getDefault()); 364 } 365 catch (XNIException e) { 366 } 369 370 } 372 376 384 public void setLocale(Locale locale) throws XNIException { 385 super.setLocale(locale); 386 fErrorReporter.setLocale(locale); 387 } 389 393 395 407 public void setInputSource(XMLInputSource inputSource) 408 throws XMLConfigurationException, IOException { 409 410 415 fInputSource = inputSource; 416 417 } 419 435 public boolean parse(boolean complete) throws XNIException, IOException { 436 if (fInputSource !=null) { 439 try { 440 reset(); 442 fScanner.setInputSource(fInputSource); 443 fInputSource = null; 444 } 445 catch (XNIException ex) { 446 if (PRINT_EXCEPTION_STACK_TRACE) 447 ex.printStackTrace(); 448 throw ex; 449 } 450 catch (IOException ex) { 451 if (PRINT_EXCEPTION_STACK_TRACE) 452 ex.printStackTrace(); 453 throw ex; 454 } 455 catch (RuntimeException ex) { 456 if (PRINT_EXCEPTION_STACK_TRACE) 457 ex.printStackTrace(); 458 throw ex; 459 } 460 catch (Exception ex) { 461 if (PRINT_EXCEPTION_STACK_TRACE) 462 ex.printStackTrace(); 463 throw new XNIException(ex); 464 } 465 } 466 467 try { 468 return fScanner.scanDocument(complete); 469 } 470 catch (XNIException ex) { 471 if (PRINT_EXCEPTION_STACK_TRACE) 472 ex.printStackTrace(); 473 throw ex; 474 } 475 catch (IOException ex) { 476 if (PRINT_EXCEPTION_STACK_TRACE) 477 ex.printStackTrace(); 478 throw ex; 479 } 480 catch (RuntimeException ex) { 481 if (PRINT_EXCEPTION_STACK_TRACE) 482 ex.printStackTrace(); 483 throw ex; 484 } 485 catch (Exception ex) { 486 if (PRINT_EXCEPTION_STACK_TRACE) 487 ex.printStackTrace(); 488 throw new XNIException(ex); 489 } 490 491 } 493 498 public void cleanup() { 499 fEntityManager.closeReaders(); 500 } 501 502 506 514 public void parse(XMLInputSource source) throws XNIException, IOException { 515 516 if (fParseInProgress) { 517 throw new XNIException("FWK005 parse may not be called while parsing."); 519 } 520 fParseInProgress = true; 521 522 try { 523 setInputSource(source); 524 parse(true); 525 } 526 catch (XNIException ex) { 527 if (PRINT_EXCEPTION_STACK_TRACE) 528 ex.printStackTrace(); 529 throw ex; 530 } 531 catch (IOException ex) { 532 if (PRINT_EXCEPTION_STACK_TRACE) 533 ex.printStackTrace(); 534 throw ex; 535 } 536 catch (RuntimeException ex) { 537 if (PRINT_EXCEPTION_STACK_TRACE) 538 ex.printStackTrace(); 539 throw ex; 540 } 541 catch (Exception ex) { 542 if (PRINT_EXCEPTION_STACK_TRACE) 543 ex.printStackTrace(); 544 throw new XNIException(ex); 545 } 546 finally { 547 fParseInProgress = false; 548 this.cleanup(); 550 } 551 552 } 554 558 563 public void reset() throws XNIException { 564 565 if (fSchemaDOMParser == null) 567 fSchemaDOMParser = new SchemaDOMParser(fErrorReporter); 568 fDocumentHandler = fSchemaDOMParser; 569 fDTDHandler = fSchemaDOMParser; 570 fDTDContentModelHandler = fSchemaDOMParser; 571 572 configurePipeline(); 574 super.reset(); 575 576 } 578 579 protected void configurePipeline() { 580 581 fScanner.setDocumentHandler(fDocumentHandler); 583 fDocumentHandler.setDocumentSource(fScanner); 584 fLastComponent = fScanner; 585 586 if (fDTDScanner != null) { 588 fDTDScanner.setDTDHandler(fDTDHandler); 589 fDTDScanner.setDTDContentModelHandler(fDTDContentModelHandler); 590 } 591 592 593 } 595 597 609 protected void checkFeature(String featureId) 610 throws XMLConfigurationException { 611 612 616 if (featureId.startsWith(Constants.XERCES_FEATURE_PREFIX)) { 617 final int suffixLength = featureId.length() - Constants.XERCES_FEATURE_PREFIX.length(); 618 619 if (suffixLength == Constants.DYNAMIC_VALIDATION_FEATURE.length() && 626 featureId.endsWith(Constants.DYNAMIC_VALIDATION_FEATURE)) { 627 return; 628 } 629 if (suffixLength == Constants.DEFAULT_ATTRIBUTE_VALUES_FEATURE.length() && 633 featureId.endsWith(Constants.DEFAULT_ATTRIBUTE_VALUES_FEATURE)) { 634 short type = XMLConfigurationException.NOT_SUPPORTED; 636 throw new XMLConfigurationException(type, featureId); 637 } 638 if (suffixLength == Constants.VALIDATE_CONTENT_MODELS_FEATURE.length() && 642 featureId.endsWith(Constants.VALIDATE_CONTENT_MODELS_FEATURE)) { 643 short type = XMLConfigurationException.NOT_SUPPORTED; 645 throw new XMLConfigurationException(type, featureId); 646 } 647 if (suffixLength == Constants.LOAD_DTD_GRAMMAR_FEATURE.length() && 651 featureId.endsWith(Constants.LOAD_DTD_GRAMMAR_FEATURE)) { 652 return; 653 } 654 if (suffixLength == Constants.LOAD_EXTERNAL_DTD_FEATURE.length() && 658 featureId.endsWith(Constants.LOAD_EXTERNAL_DTD_FEATURE)) { 659 return; 660 } 661 662 if (suffixLength == Constants.VALIDATE_DATATYPES_FEATURE.length() && 666 featureId.endsWith(Constants.VALIDATE_DATATYPES_FEATURE)) { 667 short type = XMLConfigurationException.NOT_SUPPORTED; 668 throw new XMLConfigurationException(type, featureId); 669 } 670 } 671 672 676 super.checkFeature(featureId); 677 678 } 680 693 protected void checkProperty(String propertyId) 694 throws XMLConfigurationException { 695 696 700 if (propertyId.startsWith(Constants.XERCES_PROPERTY_PREFIX)) { 701 final int suffixLength = propertyId.length() - Constants.XERCES_PROPERTY_PREFIX.length(); 702 703 if (suffixLength == Constants.DTD_SCANNER_PROPERTY.length() && 704 propertyId.endsWith(Constants.DTD_SCANNER_PROPERTY)) { 705 return; 706 } 707 } 708 709 if (propertyId.startsWith(Constants.JAXP_PROPERTY_PREFIX)) { 710 final int suffixLength = propertyId.length() - Constants.JAXP_PROPERTY_PREFIX.length(); 711 712 if (suffixLength == Constants.SCHEMA_SOURCE.length() && 713 propertyId.endsWith(Constants.SCHEMA_SOURCE)) { 714 return; 715 } 716 } 717 718 722 super.checkProperty(propertyId); 723 724 } 726 727 728 732 733 public Document getDocument() { 734 return fSchemaDOMParser.getDocument(); 735 } 736 737 738 public void resetNodePool() { 739 } 742 743 } 744 | Popular Tags |