1 /* 2 * Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved. 3 */ 4 /* 5 * Licensed to the Apache Software Foundation (ASF) under one or more 6 * contributor license agreements. See the NOTICE file distributed with 7 * this work for additional information regarding copyright ownership. 8 * The ASF licenses this file to You under the Apache License, Version 2.0 9 * (the "License"); you may not use this file except in compliance with 10 * the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 21 package com.sun.org.apache.xerces.internal.parsers; 22 23 import com.sun.org.apache.xerces.internal.impl.Constants; 24 import com.sun.org.apache.xerces.internal.impl.XML11DTDScannerImpl; 25 import com.sun.org.apache.xerces.internal.impl.XML11DocumentScannerImpl; 26 import com.sun.org.apache.xerces.internal.impl.XML11NSDocumentScannerImpl; 27 import com.sun.org.apache.xerces.internal.impl.XMLDTDScannerImpl; 28 import com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl; 29 import com.sun.org.apache.xerces.internal.impl.XMLEntityHandler; 30 import com.sun.org.apache.xerces.internal.impl.XMLEntityManager; 31 import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter; 32 import com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl; 33 import com.sun.org.apache.xerces.internal.impl.XMLVersionDetector; 34 import com.sun.org.apache.xerces.internal.impl.dtd.XML11DTDProcessor; 35 import com.sun.org.apache.xerces.internal.impl.dtd.XML11DTDValidator; 36 import com.sun.org.apache.xerces.internal.impl.dtd.XML11NSDTDValidator; 37 import com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDProcessor; 38 import com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator; 39 import com.sun.org.apache.xerces.internal.impl.dtd.XMLNSDTDValidator; 40 import com.sun.org.apache.xerces.internal.impl.dv.DTDDVFactory; 41 import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter; 42 import com.sun.org.apache.xerces.internal.impl.validation.ValidationManager; 43 import com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator; 44 import com.sun.org.apache.xerces.internal.impl.xs.XSMessageFormatter; 45 import com.sun.org.apache.xerces.internal.util.FeatureState; 46 import com.sun.org.apache.xerces.internal.util.ParserConfigurationSettings; 47 import com.sun.org.apache.xerces.internal.util.PropertyState; 48 import com.sun.org.apache.xerces.internal.util.SymbolTable; 49 import com.sun.org.apache.xerces.internal.xni.XMLDTDContentModelHandler; 50 import com.sun.org.apache.xerces.internal.xni.XMLDTDHandler; 51 import com.sun.org.apache.xerces.internal.xni.XMLDocumentHandler; 52 import com.sun.org.apache.xerces.internal.xni.XMLLocator; 53 import com.sun.org.apache.xerces.internal.xni.XNIException; 54 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarPool; 55 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponent; 56 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager; 57 import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException; 58 import com.sun.org.apache.xerces.internal.xni.parser.XMLDTDScanner; 59 import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentScanner; 60 import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentSource; 61 import com.sun.org.apache.xerces.internal.xni.parser.XMLEntityResolver; 62 import com.sun.org.apache.xerces.internal.xni.parser.XMLErrorHandler; 63 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource; 64 import com.sun.org.apache.xerces.internal.xni.parser.XMLPullParserConfiguration; 65 import java.io.IOException; 66 import java.util.ArrayList; 67 import java.util.List; 68 import java.util.Locale; 69 import javax.xml.XMLConstants; 70 import javax.xml.catalog.CatalogFeatures; 71 import jdk.xml.internal.JdkXmlUtils; 72 73 /** 74 * This class is the configuration used to parse XML 1.0 and XML 1.1 documents. 75 * 76 * @author Elena Litani, IBM 77 * @author Neil Graham, IBM 78 * @author Michael Glavassevich, IBM 79 * 80 * @LastModified: Oct 2017 81 */ 82 public class XML11Configuration extends ParserConfigurationSettings 83 implements XMLPullParserConfiguration, XML11Configurable { 84 85 // 86 // Constants 87 // 88 protected final static String XML11_DATATYPE_VALIDATOR_FACTORY = 89 "com.sun.org.apache.xerces.internal.impl.dv.dtd.XML11DTDDVFactoryImpl"; 90 91 // feature identifiers 92 93 /** Feature identifier: warn on duplicate attribute definition. */ 94 protected static final String WARN_ON_DUPLICATE_ATTDEF = 95 Constants.XERCES_FEATURE_PREFIX + Constants.WARN_ON_DUPLICATE_ATTDEF_FEATURE; 96 97 /** Feature identifier: warn on duplicate entity definition. */ 98 protected static final String WARN_ON_DUPLICATE_ENTITYDEF = 99 Constants.XERCES_FEATURE_PREFIX + Constants.WARN_ON_DUPLICATE_ENTITYDEF_FEATURE; 100 101 /** Feature identifier: warn on undeclared element definition. */ 102 protected static final String WARN_ON_UNDECLARED_ELEMDEF = 103 Constants.XERCES_FEATURE_PREFIX + Constants.WARN_ON_UNDECLARED_ELEMDEF_FEATURE; 104 105 /** Feature identifier: allow Java encodings. */ 106 protected static final String ALLOW_JAVA_ENCODINGS = 107 Constants.XERCES_FEATURE_PREFIX + Constants.ALLOW_JAVA_ENCODINGS_FEATURE; 108 109 /** Feature identifier: continue after fatal error. */ 110 protected static final String CONTINUE_AFTER_FATAL_ERROR = 111 Constants.XERCES_FEATURE_PREFIX + Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE; 112 113 /** Feature identifier: load external DTD. */ 114 protected static final String LOAD_EXTERNAL_DTD = 115 Constants.XERCES_FEATURE_PREFIX + Constants.LOAD_EXTERNAL_DTD_FEATURE; 116 117 /** Feature identifier: notify built-in refereces. */ 118 protected static final String NOTIFY_BUILTIN_REFS = 119 Constants.XERCES_FEATURE_PREFIX + Constants.NOTIFY_BUILTIN_REFS_FEATURE; 120 121 /** Feature identifier: notify character refereces. */ 122 protected static final String NOTIFY_CHAR_REFS = 123 Constants.XERCES_FEATURE_PREFIX + Constants.NOTIFY_CHAR_REFS_FEATURE; 124 125 /** Feature identifier: expose schema normalized value */ 126 protected static final String NORMALIZE_DATA = 127 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_NORMALIZED_VALUE; 128 129 /** Feature identifier: send element default value via characters() */ 130 protected static final String SCHEMA_ELEMENT_DEFAULT = 131 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_ELEMENT_DEFAULT; 132 133 /** Feature identifier: augment PSVI */ 134 protected static final String SCHEMA_AUGMENT_PSVI = 135 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_AUGMENT_PSVI; 136 137 /** feature identifier: XML Schema validation */ 138 protected static final String XMLSCHEMA_VALIDATION = 139 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_VALIDATION_FEATURE; 140 141 /** feature identifier: XML Schema validation -- full checking */ 142 protected static final String XMLSCHEMA_FULL_CHECKING = 143 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_FULL_CHECKING; 144 145 /** Feature: generate synthetic annotations */ 146 protected static final String GENERATE_SYNTHETIC_ANNOTATIONS = 147 Constants.XERCES_FEATURE_PREFIX + Constants.GENERATE_SYNTHETIC_ANNOTATIONS_FEATURE; 148 149 /** Feature identifier: validate annotations */ 150 protected static final String VALIDATE_ANNOTATIONS = 151 Constants.XERCES_FEATURE_PREFIX + Constants.VALIDATE_ANNOTATIONS_FEATURE; 152 153 /** Feature identifier: honour all schemaLocations */ 154 protected static final String HONOUR_ALL_SCHEMALOCATIONS = 155 Constants.XERCES_FEATURE_PREFIX + Constants.HONOUR_ALL_SCHEMALOCATIONS_FEATURE; 156 157 /** Feature identifier: namespace growth */ 158 protected static final String NAMESPACE_GROWTH = 159 Constants.XERCES_FEATURE_PREFIX + Constants.NAMESPACE_GROWTH_FEATURE; 160 161 /** Feature identifier: tolerate duplicates */ 162 protected static final String TOLERATE_DUPLICATES = 163 Constants.XERCES_FEATURE_PREFIX + Constants.TOLERATE_DUPLICATES_FEATURE; 164 165 /** Feature identifier: use grammar pool only */ 166 protected static final String USE_GRAMMAR_POOL_ONLY = 167 Constants.XERCES_FEATURE_PREFIX + Constants.USE_GRAMMAR_POOL_ONLY_FEATURE; 168 169 // feature identifiers 170 171 /** Feature identifier: validation. */ 172 protected static final String VALIDATION = 173 Constants.SAX_FEATURE_PREFIX + Constants.VALIDATION_FEATURE; 174 175 /** Feature identifier: namespaces. */ 176 protected static final String NAMESPACES = 177 Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE; 178 179 /** Feature identifier: external general entities. */ 180 protected static final String EXTERNAL_GENERAL_ENTITIES = 181 Constants.SAX_FEATURE_PREFIX + Constants.EXTERNAL_GENERAL_ENTITIES_FEATURE; 182 183 /** Feature identifier: external parameter entities. */ 184 protected static final String EXTERNAL_PARAMETER_ENTITIES = 185 Constants.SAX_FEATURE_PREFIX + Constants.EXTERNAL_PARAMETER_ENTITIES_FEATURE; 186 187 /** Feature identifier: whether to ignore xsi:type attributes until a global element declaration is encountered */ 188 protected static final String IGNORE_XSI_TYPE = 189 Constants.XERCES_FEATURE_PREFIX + Constants.IGNORE_XSI_TYPE_FEATURE; 190 191 /** Feature identifier: whether to ignore ID/IDREF errors */ 192 protected static final String ID_IDREF_CHECKING = 193 Constants.XERCES_FEATURE_PREFIX + Constants.ID_IDREF_CHECKING_FEATURE; 194 195 /** Feature identifier: whether to ignore unparsed entity errors */ 196 protected static final String UNPARSED_ENTITY_CHECKING = 197 Constants.XERCES_FEATURE_PREFIX + Constants.UNPARSED_ENTITY_CHECKING_FEATURE; 198 199 /** Feature identifier: whether to ignore identity constraint errors */ 200 protected static final String IDENTITY_CONSTRAINT_CHECKING = 201 Constants.XERCES_FEATURE_PREFIX + Constants.IDC_CHECKING_FEATURE; 202 203 // property identifiers 204 205 /** Property identifier: xml string. */ 206 protected static final String XML_STRING = 207 Constants.SAX_PROPERTY_PREFIX + Constants.XML_STRING_PROPERTY; 208 209 /** Property identifier: symbol table. */ 210 protected static final String SYMBOL_TABLE = 211 Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY; 212 213 /** Property identifier: error handler. */ 214 protected static final String ERROR_HANDLER = 215 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_HANDLER_PROPERTY; 216 217 /** Property identifier: entity resolver. */ 218 protected static final String ENTITY_RESOLVER = 219 Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_RESOLVER_PROPERTY; 220 221 /** Property identifier: XML Schema validator. */ 222 protected static final String SCHEMA_VALIDATOR = 223 Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_VALIDATOR_PROPERTY; 224 225 /** Property identifier: schema location. */ 226 protected static final String SCHEMA_LOCATION = 227 Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_LOCATION; 228 229 /** Property identifier: no namespace schema location. */ 230 protected static final String SCHEMA_NONS_LOCATION = 231 Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_NONS_LOCATION; 232 233 /** Property identifier: error reporter. */ 234 protected static final String ERROR_REPORTER = 235 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY; 236 237 /** Property identifier: entity manager. */ 238 protected static final String ENTITY_MANAGER = 239 Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_MANAGER_PROPERTY; 240 241 /** Property identifier document scanner: */ 242 protected static final String DOCUMENT_SCANNER = 243 Constants.XERCES_PROPERTY_PREFIX + Constants.DOCUMENT_SCANNER_PROPERTY; 244 245 /** Property identifier: DTD scanner. */ 246 protected static final String DTD_SCANNER = 247 Constants.XERCES_PROPERTY_PREFIX + Constants.DTD_SCANNER_PROPERTY; 248 249 /** Property identifier: grammar pool. */ 250 protected static final String XMLGRAMMAR_POOL = 251 Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY; 252 253 /** Property identifier: DTD loader. */ 254 protected static final String DTD_PROCESSOR = 255 Constants.XERCES_PROPERTY_PREFIX + Constants.DTD_PROCESSOR_PROPERTY; 256 257 /** Property identifier: DTD validator. */ 258 protected static final String DTD_VALIDATOR = 259 Constants.XERCES_PROPERTY_PREFIX + Constants.DTD_VALIDATOR_PROPERTY; 260 261 /** Property identifier: namespace binder. */ 262 protected static final String NAMESPACE_BINDER = 263 Constants.XERCES_PROPERTY_PREFIX + Constants.NAMESPACE_BINDER_PROPERTY; 264 265 /** Property identifier: datatype validator factory. */ 266 protected static final String DATATYPE_VALIDATOR_FACTORY = 267 Constants.XERCES_PROPERTY_PREFIX + Constants.DATATYPE_VALIDATOR_FACTORY_PROPERTY; 268 269 protected static final String VALIDATION_MANAGER = 270 Constants.XERCES_PROPERTY_PREFIX + Constants.VALIDATION_MANAGER_PROPERTY; 271 272 /** Property identifier: JAXP schema language / DOM schema-type. */ 273 protected static final String JAXP_SCHEMA_LANGUAGE = 274 Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_LANGUAGE; 275 276 /** Property identifier: JAXP schema source/ DOM schema-location. */ 277 protected static final String JAXP_SCHEMA_SOURCE = 278 Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_SOURCE; 279 280 /** Property identifier: root type definition. */ 281 protected static final String ROOT_TYPE_DEF = 282 Constants.XERCES_PROPERTY_PREFIX + Constants.ROOT_TYPE_DEFINITION_PROPERTY; 283 284 /** Property identifier: root element declaration. */ 285 protected static final String ROOT_ELEMENT_DECL = 286 Constants.XERCES_PROPERTY_PREFIX + Constants.ROOT_ELEMENT_DECLARATION_PROPERTY; 287 288 /** Property identifier: locale. */ 289 protected static final String LOCALE = 290 Constants.XERCES_PROPERTY_PREFIX + Constants.LOCALE_PROPERTY; 291 292 /** Property identifier: Schema DV Factory */ 293 protected static final String SCHEMA_DV_FACTORY = 294 Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_DV_FACTORY_PROPERTY; 295 296 /** Property identifier: Security property manager. */ 297 private static final String XML_SECURITY_PROPERTY_MANAGER = 298 Constants.XML_SECURITY_PROPERTY_MANAGER; 299 300 /** Property identifier: Security manager. */ 301 private static final String SECURITY_MANAGER = Constants.SECURITY_MANAGER; 302 303 // debugging 304 305 /** Set to true and recompile to print exception stack trace. */ 306 protected static final boolean PRINT_EXCEPTION_STACK_TRACE = false; 307 308 // 309 // Data 310 // 311 312 protected SymbolTable fSymbolTable; 313 protected XMLInputSource fInputSource; 314 protected ValidationManager fValidationManager; 315 protected XMLVersionDetector fVersionDetector; 316 protected XMLLocator fLocator; 317 protected Locale fLocale; 318 319 /** XML 1.0 Components. */ 320 protected List<XMLComponent> fComponents; 321 322 /** XML 1.1. Components. */ 323 protected List<XMLComponent> fXML11Components = null; 324 325 /** Common components: XMLEntityManager, XMLErrorReporter, XMLSchemaValidator */ 326 protected List<XMLComponent> fCommonComponents = null; 327 328 /** The document handler. */ 329 protected XMLDocumentHandler fDocumentHandler; 330 331 /** The DTD handler. */ 332 protected XMLDTDHandler fDTDHandler; 333 334 /** The DTD content model handler. */ 335 protected XMLDTDContentModelHandler fDTDContentModelHandler; 336 337 /** Last component in the document pipeline */ 338 protected XMLDocumentSource fLastComponent; 339 340 /** 341 * True if a parse is in progress. This state is needed because 342 * some features/properties cannot be set while parsing (e.g. 343 * validation and namespaces). 344 */ 345 protected boolean fParseInProgress = false; 346 347 /** fConfigUpdated is set to true if there has been any change to the configuration settings, 348 * i.e a feature or a property was changed. 349 */ 350 protected boolean fConfigUpdated = false; 351 352 // 353 // XML 1.0 components 354 // 355 356 /** The XML 1.0 Datatype validator factory. */ 357 protected DTDDVFactory fDatatypeValidatorFactory; 358 359 /** The XML 1.0 Document scanner that does namespace binding. */ 360 protected XMLNSDocumentScannerImpl fNamespaceScanner; 361 /** The XML 1.0 Non-namespace implementation of scanner */ 362 protected XMLDocumentScannerImpl fNonNSScanner; 363 /** The XML 1.0 DTD Validator: binds namespaces */ 364 protected XMLDTDValidator fDTDValidator; 365 /** The XML 1.0 DTD Validator that does not bind namespaces */ 366 protected XMLDTDValidator fNonNSDTDValidator; 367 /** The XML 1.0 DTD scanner. */ 368 protected XMLDTDScanner fDTDScanner; 369 /** The XML 1.0 DTD Processor . */ 370 protected XMLDTDProcessor fDTDProcessor; 371 372 // 373 // XML 1.1 components 374 // 375 376 /** The XML 1.1 datatype factory. **/ 377 protected DTDDVFactory fXML11DatatypeFactory = null; 378 379 /** The XML 1.1 document scanner that does namespace binding. **/ 380 protected XML11NSDocumentScannerImpl fXML11NSDocScanner = null; 381 382 /** The XML 1.1 document scanner that does not do namespace binding. **/ 383 protected XML11DocumentScannerImpl fXML11DocScanner = null; 384 385 /** The XML 1.1 DTD validator that does namespace binding. **/ 386 protected XML11NSDTDValidator fXML11NSDTDValidator = null; 387 388 /** The XML 1.1 DTD validator that does not do namespace binding. **/ 389 protected XML11DTDValidator fXML11DTDValidator = null; 390 391 /** The XML 1.1 DTD scanner. **/ 392 protected XML11DTDScannerImpl fXML11DTDScanner = null; 393 /** The XML 1.1 DTD processor. **/ 394 protected XML11DTDProcessor fXML11DTDProcessor = null; 395 396 // 397 // Common components 398 // 399 400 /** Grammar pool. */ 401 protected XMLGrammarPool fGrammarPool; 402 403 /** Error reporter. */ 404 protected XMLErrorReporter fErrorReporter; 405 406 /** Entity manager. */ 407 protected XMLEntityManager fEntityManager; 408 409 /** XML Schema Validator. */ 410 protected XMLSchemaValidator fSchemaValidator; 411 412 /** Current scanner */ 413 protected XMLDocumentScanner fCurrentScanner; 414 /** Current Datatype validator factory. */ 415 protected DTDDVFactory fCurrentDVFactory; 416 /** Current DTD scanner. */ 417 protected XMLDTDScanner fCurrentDTDScanner; 418 419 /** Flag indicating whether XML11 components have been initialized. */ 420 private boolean f11Initialized = false; 421 422 /** Flag indicating whether the symbol table instance was specified during construction **/ 423 private boolean fSymbolTableProvided = false; 424 425 /** Flag indicating if the symbol table was initialized and never used before that **/ 426 private boolean fSymbolTableJustInitialized = true; 427 428 // 429 // Constructors 430 // 431 432 /** Default constructor. */ 433 public XML11Configuration() { 434 this(null, null, null); 435 } // <init>() 436 437 /** 438 * Constructs a parser configuration using the specified symbol table. 439 * 440 * @param symbolTable The symbol table to use. 441 */ 442 public XML11Configuration(SymbolTable symbolTable) { 443 this(symbolTable, null, null); 444 } // <init>(SymbolTable) 445 446 /** 447 * Constructs a parser configuration using the specified symbol table and 448 * grammar pool. 449 * <p> 450 * <strong>REVISIT:</strong> 451 * Grammar pool will be updated when the new validation engine is 452 * implemented. 453 * 454 * @param symbolTable The symbol table to use. 455 * @param grammarPool The grammar pool to use. 456 */ 457 public XML11Configuration(SymbolTable symbolTable, XMLGrammarPool grammarPool) { 458 this(symbolTable, grammarPool, null); 459 } // <init>(SymbolTable,XMLGrammarPool) 460 461 /** 462 * Constructs a parser configuration using the specified symbol table, 463 * grammar pool, and parent settings. 464 * <p> 465 * <strong>REVISIT:</strong> 466 * Grammar pool will be updated when the new validation engine is 467 * implemented. 468 * 469 * @param symbolTable The symbol table to use. 470 * @param grammarPool The grammar pool to use. 471 * @param parentSettings The parent settings. 472 */ 473 public XML11Configuration( 474 SymbolTable symbolTable, 475 XMLGrammarPool grammarPool, 476 XMLComponentManager parentSettings) { 477 478 super(parentSettings); 479 480 // create a vector to hold all the components in use 481 // XML 1.0 specialized components 482 fComponents = new ArrayList<>(); 483 // XML 1.1 specialized components 484 fXML11Components = new ArrayList<>(); 485 // Common components for XML 1.1. and XML 1.0 486 fCommonComponents = new ArrayList<>(); 487 488 // add default recognized features 489 final String[] recognizedFeatures = 490 { 491 CONTINUE_AFTER_FATAL_ERROR, LOAD_EXTERNAL_DTD, // from XMLDTDScannerImpl 492 VALIDATION, 493 NAMESPACES, 494 NORMALIZE_DATA, SCHEMA_ELEMENT_DEFAULT, SCHEMA_AUGMENT_PSVI, 495 GENERATE_SYNTHETIC_ANNOTATIONS, VALIDATE_ANNOTATIONS, 496 HONOUR_ALL_SCHEMALOCATIONS, IGNORE_XSI_TYPE, 497 ID_IDREF_CHECKING, IDENTITY_CONSTRAINT_CHECKING, 498 UNPARSED_ENTITY_CHECKING, 499 NAMESPACE_GROWTH, TOLERATE_DUPLICATES, 500 USE_GRAMMAR_POOL_ONLY, 501 // NOTE: These shouldn't really be here but since the XML Schema 502 // validator is constructed dynamically, its recognized 503 // features might not have been set and it would cause a 504 // not-recognized exception to be thrown. -Ac 505 XMLSCHEMA_VALIDATION, XMLSCHEMA_FULL_CHECKING, 506 EXTERNAL_GENERAL_ENTITIES, 507 EXTERNAL_PARAMETER_ENTITIES, 508 PARSER_SETTINGS, 509 XMLConstants.FEATURE_SECURE_PROCESSING, 510 XMLConstants.USE_CATALOG, 511 JdkXmlUtils.RESET_SYMBOL_TABLE 512 }; 513 addRecognizedFeatures(recognizedFeatures); 514 // set state for default features 515 fFeatures.put(VALIDATION, Boolean.FALSE); 516 fFeatures.put(NAMESPACES, Boolean.TRUE); 517 fFeatures.put(EXTERNAL_GENERAL_ENTITIES, Boolean.TRUE); 518 fFeatures.put(EXTERNAL_PARAMETER_ENTITIES, Boolean.TRUE); 519 fFeatures.put(CONTINUE_AFTER_FATAL_ERROR, Boolean.FALSE); 520 fFeatures.put(LOAD_EXTERNAL_DTD, Boolean.TRUE); 521 fFeatures.put(SCHEMA_ELEMENT_DEFAULT, Boolean.TRUE); 522 fFeatures.put(NORMALIZE_DATA, Boolean.TRUE); 523 fFeatures.put(SCHEMA_AUGMENT_PSVI, Boolean.TRUE); 524 fFeatures.put(GENERATE_SYNTHETIC_ANNOTATIONS, Boolean.FALSE); 525 fFeatures.put(VALIDATE_ANNOTATIONS, Boolean.FALSE); 526 fFeatures.put(HONOUR_ALL_SCHEMALOCATIONS, Boolean.FALSE); 527 fFeatures.put(IGNORE_XSI_TYPE, Boolean.FALSE); 528 fFeatures.put(ID_IDREF_CHECKING, Boolean.TRUE); 529 fFeatures.put(IDENTITY_CONSTRAINT_CHECKING, Boolean.TRUE); 530 fFeatures.put(UNPARSED_ENTITY_CHECKING, Boolean.TRUE); 531 fFeatures.put(NAMESPACE_GROWTH, Boolean.FALSE); 532 fFeatures.put(TOLERATE_DUPLICATES, Boolean.FALSE); 533 fFeatures.put(USE_GRAMMAR_POOL_ONLY, Boolean.FALSE); 534 fFeatures.put(PARSER_SETTINGS, Boolean.TRUE); 535 fFeatures.put(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE); 536 fFeatures.put(XMLConstants.USE_CATALOG, JdkXmlUtils.USE_CATALOG_DEFAULT); 537 fFeatures.put(JdkXmlUtils.RESET_SYMBOL_TABLE, JdkXmlUtils.RESET_SYMBOL_TABLE_DEFAULT); 538 539 // add default recognized properties 540 final String[] recognizedProperties = 541 { 542 SYMBOL_TABLE, 543 ERROR_HANDLER, 544 ENTITY_RESOLVER, 545 ERROR_REPORTER, 546 ENTITY_MANAGER, 547 DOCUMENT_SCANNER, 548 DTD_SCANNER, 549 DTD_PROCESSOR, 550 DTD_VALIDATOR, 551 DATATYPE_VALIDATOR_FACTORY, 552 VALIDATION_MANAGER, 553 SCHEMA_VALIDATOR, 554 XML_STRING, 555 XMLGRAMMAR_POOL, 556 JAXP_SCHEMA_SOURCE, 557 JAXP_SCHEMA_LANGUAGE, 558 // NOTE: These shouldn't really be here but since the XML Schema 559 // validator is constructed dynamically, its recognized 560 // properties might not have been set and it would cause a 561 // not-recognized exception to be thrown. -Ac 562 SCHEMA_LOCATION, 563 SCHEMA_NONS_LOCATION, 564 ROOT_TYPE_DEF, 565 ROOT_ELEMENT_DECL, 566 LOCALE, 567 SCHEMA_DV_FACTORY, 568 SECURITY_MANAGER, 569 XML_SECURITY_PROPERTY_MANAGER, 570 JdkXmlUtils.CATALOG_DEFER, 571 JdkXmlUtils.CATALOG_FILES, 572 JdkXmlUtils.CATALOG_PREFER, 573 JdkXmlUtils.CATALOG_RESOLVE, 574 JdkXmlUtils.CDATA_CHUNK_SIZE 575 }; 576 addRecognizedProperties(recognizedProperties); 577 578 // Remember if symbolTable was provided from outside 579 fSymbolTableProvided = symbolTable != null; 580 if (!fSymbolTableProvided) { 581 fSymbolTable = new SymbolTable(); 582 } else { 583 fSymbolTable = symbolTable; 584 } 585 fProperties.put(SYMBOL_TABLE, fSymbolTable); 586 587 fGrammarPool = grammarPool; 588 if (fGrammarPool != null) { 589 fProperties.put(XMLGRAMMAR_POOL, fGrammarPool); 590 } 591 592 fEntityManager = new XMLEntityManager(); 593 fProperties.put(ENTITY_MANAGER, fEntityManager); 594 addCommonComponent(fEntityManager); 595 596 fErrorReporter = new XMLErrorReporter(); 597 fErrorReporter.setDocumentLocator(fEntityManager.getEntityScanner()); 598 fProperties.put(ERROR_REPORTER, fErrorReporter); 599 addCommonComponent(fErrorReporter); 600 601 fNamespaceScanner = new XMLNSDocumentScannerImpl(); 602 fProperties.put(DOCUMENT_SCANNER, fNamespaceScanner); 603 addComponent((XMLComponent) fNamespaceScanner); 604 605 fDTDScanner = new XMLDTDScannerImpl(); 606 fProperties.put(DTD_SCANNER, fDTDScanner); 607 addComponent((XMLComponent) fDTDScanner); 608 609 fDTDProcessor = new XMLDTDProcessor(); 610 fProperties.put(DTD_PROCESSOR, fDTDProcessor); 611 addComponent((XMLComponent) fDTDProcessor); 612 613 fDTDValidator = new XMLNSDTDValidator(); 614 fProperties.put(DTD_VALIDATOR, fDTDValidator); 615 addComponent(fDTDValidator); 616 617 fDatatypeValidatorFactory = DTDDVFactory.getInstance(); 618 fProperties.put(DATATYPE_VALIDATOR_FACTORY, fDatatypeValidatorFactory); 619 620 fValidationManager = new ValidationManager(); 621 fProperties.put(VALIDATION_MANAGER, fValidationManager); 622 623 fVersionDetector = new XMLVersionDetector(); 624 625 // add message formatters 626 if (fErrorReporter.getMessageFormatter(XMLMessageFormatter.XML_DOMAIN) == null) { 627 XMLMessageFormatter xmft = new XMLMessageFormatter(); 628 fErrorReporter.putMessageFormatter(XMLMessageFormatter.XML_DOMAIN, xmft); 629 fErrorReporter.putMessageFormatter(XMLMessageFormatter.XMLNS_DOMAIN, xmft); 630 } 631 632 // set locale 633 try { 634 setLocale(Locale.getDefault()); 635 } catch (XNIException e) { 636 // do nothing 637 // REVISIT: What is the right thing to do? -Ac 638 } 639 640 // Initialize Catalog features 641 for( CatalogFeatures.Feature f : CatalogFeatures.Feature.values()) { 642 fProperties.put(f.getPropertyName(), null); 643 } 644 645 setProperty(JdkXmlUtils.CDATA_CHUNK_SIZE, JdkXmlUtils.CDATA_CHUNK_SIZE_DEFAULT); 646 647 fConfigUpdated = false; 648 } // <init>(SymbolTable,XMLGrammarPool) 649 650 // 651 // Public methods 652 // 653 /** 654 * Sets the input source for the document to parse. 655 * 656 * @param inputSource The document's input source. 657 * 658 * @exception XMLConfigurationException Thrown if there is a 659 * configuration error when initializing the 660 * parser. 661 * @exception IOException Thrown on I/O error. 662 * 663 * @see #parse(boolean) 664 */ 665 public void setInputSource(XMLInputSource inputSource) 666 throws XMLConfigurationException, IOException { 667 668 // REVISIT: this method used to reset all the components and 669 // construct the pipeline. Now reset() is called 670 // in parse (boolean) just before we parse the document 671 // Should this method still throw exceptions..? 672 673 fInputSource = inputSource; 674 675 } // setInputSource(XMLInputSource) 676 677 /** 678 * Set the locale to use for messages. 679 * 680 * @param locale The locale object to use for localization of messages. 681 * 682 * @exception XNIException Thrown if the parser does not support the 683 * specified locale. 684 */ 685 public void setLocale(Locale locale) throws XNIException { 686 fLocale = locale; 687 fErrorReporter.setLocale(locale); 688 } // setLocale(Locale) 689 /** 690 * Sets the document handler on the last component in the pipeline 691 * to receive information about the document. 692 * 693 * @param documentHandler The document handler. 694 */ 695 public void setDocumentHandler(XMLDocumentHandler documentHandler) { 696 fDocumentHandler = documentHandler; 697 if (fLastComponent != null) { 698 fLastComponent.setDocumentHandler(fDocumentHandler); 699 if (fDocumentHandler !=null){ 700 fDocumentHandler.setDocumentSource(fLastComponent); 701 } 702 } 703 } // setDocumentHandler(XMLDocumentHandler) 704 705 /** Returns the registered document handler. */ 706 public XMLDocumentHandler getDocumentHandler() { 707 return fDocumentHandler; 708 } // getDocumentHandler():XMLDocumentHandler 709 710 /** 711 * Sets the DTD handler. 712 * 713 * @param dtdHandler The DTD handler. 714 */ 715 public void setDTDHandler(XMLDTDHandler dtdHandler) { 716 fDTDHandler = dtdHandler; 717 } // setDTDHandler(XMLDTDHandler) 718 719 /** Returns the registered DTD handler. */ 720 public XMLDTDHandler getDTDHandler() { 721 return fDTDHandler; 722 } // getDTDHandler():XMLDTDHandler 723 724 /** 725 * Sets the DTD content model handler. 726 * 727 * @param handler The DTD content model handler. 728 */ 729 public void setDTDContentModelHandler(XMLDTDContentModelHandler handler) { 730 fDTDContentModelHandler = handler; 731 } // setDTDContentModelHandler(XMLDTDContentModelHandler) 732 733 /** Returns the registered DTD content model handler. */ 734 public XMLDTDContentModelHandler getDTDContentModelHandler() { 735 return fDTDContentModelHandler; 736 } // getDTDContentModelHandler():XMLDTDContentModelHandler 737 738 /** 739 * Sets the resolver used to resolve external entities. The EntityResolver 740 * interface supports resolution of public and system identifiers. 741 * 742 * @param resolver The new entity resolver. Passing a null value will 743 * uninstall the currently installed resolver. 744 */ 745 public void setEntityResolver(XMLEntityResolver resolver) { 746 fProperties.put(ENTITY_RESOLVER, resolver); 747 } // setEntityResolver(XMLEntityResolver) 748 749 /** 750 * Return the current entity resolver. 751 * 752 * @return The current entity resolver, or null if none 753 * has been registered. 754 * @see #setEntityResolver 755 */ 756 public XMLEntityResolver getEntityResolver() { 757 return (XMLEntityResolver)fProperties.get(ENTITY_RESOLVER); 758 } // getEntityResolver():XMLEntityResolver 759 760 /** 761 * Allow an application to register an error event handler. 762 * 763 * <p>If the application does not register an error handler, all 764 * error events reported by the SAX parser will be silently 765 * ignored; however, normal processing may not continue. It is 766 * highly recommended that all SAX applications implement an 767 * error handler to avoid unexpected bugs.</p> 768 * 769 * <p>Applications may register a new or different handler in the 770 * middle of a parse, and the SAX parser must begin using the new 771 * handler immediately.</p> 772 * 773 * @param errorHandler The error handler. 774 * @exception java.lang.NullPointerException If the handler 775 * argument is null. 776 * @see #getErrorHandler 777 */ 778 public void setErrorHandler(XMLErrorHandler errorHandler) { 779 fProperties.put(ERROR_HANDLER, errorHandler); 780 } // setErrorHandler(XMLErrorHandler) 781 782 /** 783 * Return the current error handler. 784 * 785 * @return The current error handler, or null if none 786 * has been registered. 787 * @see #setErrorHandler 788 */ 789 public XMLErrorHandler getErrorHandler() { 790 // REVISIT: Should this be a property? 791 return (XMLErrorHandler)fProperties.get(ERROR_HANDLER); 792 } // getErrorHandler():XMLErrorHandler 793 794 795 /** 796 * If the application decides to terminate parsing before the xml document 797 * is fully parsed, the application should call this method to free any 798 * resource allocated during parsing. For example, close all opened streams. 799 */ 800 public void cleanup() { 801 fEntityManager.closeReaders(); 802 } 803 804 /** 805 * Parses the specified input source. 806 * 807 * @param source The input source. 808 * 809 * @exception XNIException Throws exception on XNI error. 810 * @exception java.io.IOException Throws exception on i/o error. 811 */ 812 public void parse(XMLInputSource source) throws XNIException, IOException { 813 814 if (fParseInProgress) { 815 // REVISIT - need to add new error message 816 throw new XNIException("FWK005 parse may not be called while parsing."); 817 } 818 fParseInProgress = true; 819 820 try { 821 setInputSource(source); 822 parse(true); 823 } catch (XNIException ex) { 824 if (PRINT_EXCEPTION_STACK_TRACE) 825 ex.printStackTrace(); 826 throw ex; 827 } catch (IOException ex) { 828 if (PRINT_EXCEPTION_STACK_TRACE) 829 ex.printStackTrace(); 830 throw ex; 831 } catch (RuntimeException ex) { 832 if (PRINT_EXCEPTION_STACK_TRACE) 833 ex.printStackTrace(); 834 throw ex; 835 } catch (Exception ex) { 836 if (PRINT_EXCEPTION_STACK_TRACE) 837 ex.printStackTrace(); 838 throw new XNIException(ex); 839 } finally { 840 fParseInProgress = false; 841 // close all streams opened by xerces 842 this.cleanup(); 843 } 844 845 } // parse(InputSource) 846 847 public boolean parse(boolean complete) throws XNIException, IOException { 848 // 849 // reset and configure pipeline and set InputSource. 850 if (fInputSource != null) { 851 try { 852 fValidationManager.reset(); 853 fVersionDetector.reset(this); 854 fConfigUpdated = true; 855 resetSymbolTable(); 856 resetCommon(); 857 858 short version = fVersionDetector.determineDocVersion(fInputSource); 859 if (version == Constants.XML_VERSION_1_1) { 860 initXML11Components(); 861 configureXML11Pipeline(); 862 resetXML11(); 863 } else { 864 configurePipeline(); 865 reset(); 866 } 867 868 // mark configuration as fixed 869 fConfigUpdated = false; 870 871 // resets and sets the pipeline. 872 fVersionDetector.startDocumentParsing((XMLEntityHandler) fCurrentScanner, version); 873 fInputSource = null; 874 } catch (IOException | RuntimeException ex) { 875 if (PRINT_EXCEPTION_STACK_TRACE) 876 ex.printStackTrace(); 877 throw ex; 878 } catch (Exception ex) { 879 if (PRINT_EXCEPTION_STACK_TRACE) 880 ex.printStackTrace(); 881 throw new XNIException(ex); 882 } 883 } 884 885 try { 886 return fCurrentScanner.scanDocument(complete); 887 } catch (IOException | RuntimeException ex) { 888 if (PRINT_EXCEPTION_STACK_TRACE) 889 ex.printStackTrace(); 890 throw ex; 891 } catch (Exception ex) { 892 if (PRINT_EXCEPTION_STACK_TRACE) 893 ex.printStackTrace(); 894 throw new XNIException(ex); 895 } 896 897 } // parse(boolean):boolean 898 899 /** 900 * Returns the state of a feature. 901 * 902 * @param featureId The feature identifier. 903 * @return true if the feature is supported 904 * 905 * @throws XMLConfigurationException Thrown for configuration error. 906 * In general, components should 907 * only throw this exception if 908 * it is <strong>really</strong> 909 * a critical error. 910 */ 911 public FeatureState getFeatureState(String featureId) 912 throws XMLConfigurationException { 913 // make this feature special 914 if (featureId.equals(PARSER_SETTINGS)){ 915 return FeatureState.is(fConfigUpdated); 916 } 917 return super.getFeatureState(featureId); 918 919 } // getFeature(String):boolean 920 921 /** 922 * Set the state of a feature. 923 * 924 * Set the state of any feature in a SAX2 parser. The parser 925 * might not recognize the feature, and if it does recognize 926 * it, it might not be able to fulfill the request. 927 * 928 * @param featureId The unique identifier (URI) of the feature. 929 * @param state The requested state of the feature (true or false). 930 * 931 * @exception com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException If the 932 * requested feature is not known. 933 */ 934 public void setFeature(String featureId, boolean state) 935 throws XMLConfigurationException { 936 fConfigUpdated = true; 937 // forward to every XML 1.0 component 938 for (XMLComponent c : fComponents) { 939 c.setFeature(featureId, state); 940 } 941 // forward it to common components 942 for (XMLComponent c : fCommonComponents) { 943 c.setFeature(featureId, state); 944 } 945 // forward to every XML 1.1 component 946 for (XMLComponent c : fXML11Components) { 947 try { 948 c.setFeature(featureId, state); 949 } 950 catch (Exception e){ 951 // no op 952 } 953 } 954 // save state if noone "objects" 955 super.setFeature(featureId, state); 956 957 } // setFeature(String,boolean) 958 959 /** 960 * Returns the value of a property. 961 * 962 * @param propertyId The property identifier. 963 * @return the value of the property 964 * 965 * @throws XMLConfigurationException Thrown for configuration error. 966 * In general, components should 967 * only throw this exception if 968 * it is <strong>really</strong> 969 * a critical error. 970 */ 971 public PropertyState getPropertyState(String propertyId) 972 throws XMLConfigurationException { 973 if (LOCALE.equals(propertyId)) { 974 return PropertyState.is(getLocale()); 975 } 976 return super.getPropertyState(propertyId); 977 } 978 979 /** 980 * setProperty 981 * 982 * @param propertyId 983 * @param value 984 */ 985 public void setProperty(String propertyId, Object value) 986 throws XMLConfigurationException { 987 fConfigUpdated = true; 988 if (LOCALE.equals(propertyId)) { 989 setLocale((Locale) value); 990 } 991 // forward to every XML 1.0 component 992 for (XMLComponent c : fComponents) { 993 c.setProperty(propertyId, value); 994 } 995 // forward it to every common Component 996 for (XMLComponent c : fCommonComponents) { 997 c.setProperty(propertyId, value); 998 } 999 // forward it to every XML 1.1 component 1000 for (XMLComponent c : fXML11Components) { 1001 try { 1002 c.setProperty(propertyId, value); 1003 } 1004 catch (Exception e){ 1005 // ignore it 1006 } 1007 } 1008 1009 // store value if noone "objects" 1010 super.setProperty(propertyId, value); 1011 1012 } // setProperty(String,Object) 1013 1014 1015 /** Returns the locale. */ 1016 public Locale getLocale() { 1017 return fLocale; 1018 } // getLocale():Locale 1019 1020 /** 1021 * reset all XML 1.0 components before parsing and namespace context 1022 */ 1023 protected void reset() throws XNIException { 1024 int count = fComponents.size(); 1025 for (int i = 0; i < count; i++) { 1026 XMLComponent c = fComponents.get(i); 1027 c.reset(this); 1028 } 1029 1030 } // reset() 1031 1032 /** 1033 * reset all common components before parsing 1034 */ 1035 protected void resetCommon() throws XNIException { 1036 // reset common components 1037 int count = fCommonComponents.size(); 1038 for (int i = 0; i < count; i++) { 1039 XMLComponent c = fCommonComponents.get(i); 1040 c.reset(this); 1041 } 1042 1043 } // resetCommon() 1044 1045 1046 /** 1047 * reset all components before parsing and namespace context 1048 */ 1049 protected void resetXML11() throws XNIException { 1050 // reset every component 1051 int count = fXML11Components.size(); 1052 for (int i = 0; i < count; i++) { 1053 XMLComponent c = fXML11Components.get(i); 1054 c.reset(this); 1055 } 1056 1057 } // resetXML11() 1058 1059 1060 /** 1061 * Configures the XML 1.1 pipeline. 1062 * Note: this method also resets the new XML11 components. 1063 */ 1064 protected void configureXML11Pipeline() { 1065 if (fCurrentDVFactory != fXML11DatatypeFactory) { 1066 fCurrentDVFactory = fXML11DatatypeFactory; 1067 setProperty(DATATYPE_VALIDATOR_FACTORY, fCurrentDVFactory); 1068 } 1069 if (fCurrentDTDScanner != fXML11DTDScanner) { 1070 fCurrentDTDScanner = fXML11DTDScanner; 1071 setProperty(DTD_SCANNER, fCurrentDTDScanner); 1072 setProperty(DTD_PROCESSOR, fXML11DTDProcessor); 1073 } 1074 1075 fXML11DTDScanner.setDTDHandler(fXML11DTDProcessor); 1076 fXML11DTDProcessor.setDTDSource(fXML11DTDScanner); 1077 fXML11DTDProcessor.setDTDHandler(fDTDHandler); 1078 if (fDTDHandler != null) { 1079 fDTDHandler.setDTDSource(fXML11DTDProcessor); 1080 } 1081 1082 fXML11DTDScanner.setDTDContentModelHandler(fXML11DTDProcessor); 1083 fXML11DTDProcessor.setDTDContentModelSource(fXML11DTDScanner); 1084 fXML11DTDProcessor.setDTDContentModelHandler(fDTDContentModelHandler); 1085 if (fDTDContentModelHandler != null) { 1086 fDTDContentModelHandler.setDTDContentModelSource(fXML11DTDProcessor); 1087 } 1088 1089 // setup XML 1.1 document pipeline 1090 if (fFeatures.get(NAMESPACES) == Boolean.TRUE) { 1091 if (fCurrentScanner != fXML11NSDocScanner) { 1092 fCurrentScanner = fXML11NSDocScanner; 1093 setProperty(DOCUMENT_SCANNER, fXML11NSDocScanner); 1094 setProperty(DTD_VALIDATOR, fXML11NSDTDValidator); 1095 } 1096 1097 fXML11NSDocScanner.setDTDValidator(fXML11NSDTDValidator); 1098 fXML11NSDocScanner.setDocumentHandler(fXML11NSDTDValidator); 1099 fXML11NSDTDValidator.setDocumentSource(fXML11NSDocScanner); 1100 fXML11NSDTDValidator.setDocumentHandler(fDocumentHandler); 1101 1102 if (fDocumentHandler != null) { 1103 fDocumentHandler.setDocumentSource(fXML11NSDTDValidator); 1104 } 1105 fLastComponent = fXML11NSDTDValidator; 1106 1107 } else { 1108 // create components 1109 if (fXML11DocScanner == null) { 1110 // non namespace document pipeline 1111 fXML11DocScanner = new XML11DocumentScannerImpl(); 1112 addXML11Component(fXML11DocScanner); 1113 fXML11DTDValidator = new XML11DTDValidator(); 1114 addXML11Component(fXML11DTDValidator); 1115 } 1116 if (fCurrentScanner != fXML11DocScanner) { 1117 fCurrentScanner = fXML11DocScanner; 1118 setProperty(DOCUMENT_SCANNER, fXML11DocScanner); 1119 setProperty(DTD_VALIDATOR, fXML11DTDValidator); 1120 } 1121 fXML11DocScanner.setDocumentHandler(fXML11DTDValidator); 1122 fXML11DTDValidator.setDocumentSource(fXML11DocScanner); 1123 fXML11DTDValidator.setDocumentHandler(fDocumentHandler); 1124 1125 if (fDocumentHandler != null) { 1126 fDocumentHandler.setDocumentSource(fXML11DTDValidator); 1127 } 1128 fLastComponent = fXML11DTDValidator; 1129 } 1130 1131 // setup document pipeline 1132 if (fFeatures.get(XMLSCHEMA_VALIDATION) == Boolean.TRUE) { 1133 // If schema validator was not in the pipeline insert it. 1134 if (fSchemaValidator == null) { 1135 fSchemaValidator = new XMLSchemaValidator(); 1136 // add schema component 1137 setProperty(SCHEMA_VALIDATOR, fSchemaValidator); 1138 addCommonComponent(fSchemaValidator); 1139 fSchemaValidator.reset(this); 1140 // add schema message formatter 1141 if (fErrorReporter.getMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN) == null) { 1142 XSMessageFormatter xmft = new XSMessageFormatter(); 1143 fErrorReporter.putMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN, xmft); 1144 } 1145 } 1146 1147 fLastComponent.setDocumentHandler(fSchemaValidator); 1148 fSchemaValidator.setDocumentSource(fLastComponent); 1149 fSchemaValidator.setDocumentHandler(fDocumentHandler); 1150 if (fDocumentHandler != null) { 1151 fDocumentHandler.setDocumentSource(fSchemaValidator); 1152 } 1153 fLastComponent = fSchemaValidator; 1154 } 1155 1156 } // configureXML11Pipeline() 1157 1158 /** Configures the pipeline. */ 1159 protected void configurePipeline() { 1160 if (fCurrentDVFactory != fDatatypeValidatorFactory) { 1161 fCurrentDVFactory = fDatatypeValidatorFactory; 1162 // use XML 1.0 datatype library 1163 setProperty(DATATYPE_VALIDATOR_FACTORY, fCurrentDVFactory); 1164 } 1165 1166 // setup DTD pipeline 1167 if (fCurrentDTDScanner != fDTDScanner) { 1168 fCurrentDTDScanner = fDTDScanner; 1169 setProperty(DTD_SCANNER, fCurrentDTDScanner); 1170 setProperty(DTD_PROCESSOR, fDTDProcessor); 1171 } 1172 fDTDScanner.setDTDHandler(fDTDProcessor); 1173 fDTDProcessor.setDTDSource(fDTDScanner); 1174 fDTDProcessor.setDTDHandler(fDTDHandler); 1175 if (fDTDHandler != null) { 1176 fDTDHandler.setDTDSource(fDTDProcessor); 1177 } 1178 1179 fDTDScanner.setDTDContentModelHandler(fDTDProcessor); 1180 fDTDProcessor.setDTDContentModelSource(fDTDScanner); 1181 fDTDProcessor.setDTDContentModelHandler(fDTDContentModelHandler); 1182 if (fDTDContentModelHandler != null) { 1183 fDTDContentModelHandler.setDTDContentModelSource(fDTDProcessor); 1184 } 1185 1186 // setup document pipeline 1187 if (fFeatures.get(NAMESPACES) == Boolean.TRUE) { 1188 if (fCurrentScanner != fNamespaceScanner) { 1189 fCurrentScanner = fNamespaceScanner; 1190 setProperty(DOCUMENT_SCANNER, fNamespaceScanner); 1191 setProperty(DTD_VALIDATOR, fDTDValidator); 1192 } 1193 fNamespaceScanner.setDTDValidator(fDTDValidator); 1194 fNamespaceScanner.setDocumentHandler(fDTDValidator); 1195 fDTDValidator.setDocumentSource(fNamespaceScanner); 1196 fDTDValidator.setDocumentHandler(fDocumentHandler); 1197 if (fDocumentHandler != null) { 1198 fDocumentHandler.setDocumentSource(fDTDValidator); 1199 } 1200 fLastComponent = fDTDValidator; 1201 } else { 1202 // create components 1203 if (fNonNSScanner == null) { 1204 fNonNSScanner = new XMLDocumentScannerImpl(); 1205 fNonNSDTDValidator = new XMLDTDValidator(); 1206 // add components 1207 addComponent((XMLComponent) fNonNSScanner); 1208 addComponent((XMLComponent) fNonNSDTDValidator); 1209 } 1210 if (fCurrentScanner != fNonNSScanner) { 1211 fCurrentScanner = fNonNSScanner; 1212 setProperty(DOCUMENT_SCANNER, fNonNSScanner); 1213 setProperty(DTD_VALIDATOR, fNonNSDTDValidator); 1214 } 1215 1216 fNonNSScanner.setDocumentHandler(fNonNSDTDValidator); 1217 fNonNSDTDValidator.setDocumentSource(fNonNSScanner); 1218 fNonNSDTDValidator.setDocumentHandler(fDocumentHandler); 1219 if (fDocumentHandler != null) { 1220 fDocumentHandler.setDocumentSource(fNonNSDTDValidator); 1221 } 1222 fLastComponent = fNonNSDTDValidator; 1223 } 1224 1225 // add XML Schema validator if needed 1226 if (fFeatures.get(XMLSCHEMA_VALIDATION) == Boolean.TRUE) { 1227 // If schema validator was not in the pipeline insert it. 1228 if (fSchemaValidator == null) { 1229 fSchemaValidator = new XMLSchemaValidator(); 1230 // add schema component 1231 setProperty(SCHEMA_VALIDATOR, fSchemaValidator); 1232 addCommonComponent(fSchemaValidator); 1233 fSchemaValidator.reset(this); 1234 // add schema message formatter 1235 if (fErrorReporter.getMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN) == null) { 1236 XSMessageFormatter xmft = new XSMessageFormatter(); 1237 fErrorReporter.putMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN, xmft); 1238 } 1239 1240 } 1241 fLastComponent.setDocumentHandler(fSchemaValidator); 1242 fSchemaValidator.setDocumentSource(fLastComponent); 1243 fSchemaValidator.setDocumentHandler(fDocumentHandler); 1244 if (fDocumentHandler != null) { 1245 fDocumentHandler.setDocumentSource(fSchemaValidator); 1246 } 1247 fLastComponent = fSchemaValidator; 1248 } 1249 } // configurePipeline() 1250 1251 1252 // features and properties 1253 1254 /** 1255 * Check a feature. If feature is know and supported, this method simply 1256 * returns. Otherwise, the appropriate exception is thrown. 1257 * 1258 * @param featureId The unique identifier (URI) of the feature. 1259 * 1260 * @throws XMLConfigurationException Thrown for configuration error. 1261 * In general, components should 1262 * only throw this exception if 1263 * it is <strong>really</strong> 1264 * a critical error. 1265 */ 1266 protected FeatureState checkFeature(String featureId) throws XMLConfigurationException { 1267 1268 // 1269 // Xerces Features 1270 // 1271 1272 if (featureId.startsWith(Constants.XERCES_FEATURE_PREFIX)) { 1273 final int suffixLength = featureId.length() - Constants.XERCES_FEATURE_PREFIX.length(); 1274 1275 // 1276 // http://apache.org/xml/features/validation/dynamic 1277 // Allows the parser to validate a document only when it 1278 // contains a grammar. Validation is turned on/off based 1279 // on each document instance, automatically. 1280 // 1281 if (suffixLength == Constants.DYNAMIC_VALIDATION_FEATURE.length() && 1282 featureId.endsWith(Constants.DYNAMIC_VALIDATION_FEATURE)) { 1283 return FeatureState.RECOGNIZED; 1284 } 1285 1286 // 1287 // http://apache.org/xml/features/validation/default-attribute-values 1288 // 1289 if (suffixLength == Constants.DEFAULT_ATTRIBUTE_VALUES_FEATURE.length() && 1290 featureId.endsWith(Constants.DEFAULT_ATTRIBUTE_VALUES_FEATURE)) { 1291 // REVISIT 1292 return FeatureState.NOT_SUPPORTED; 1293 } 1294 // 1295 // http://apache.org/xml/features/validation/default-attribute-values 1296 // 1297 if (suffixLength == Constants.VALIDATE_CONTENT_MODELS_FEATURE.length() && 1298 featureId.endsWith(Constants.VALIDATE_CONTENT_MODELS_FEATURE)) { 1299 // REVISIT 1300 return FeatureState.NOT_SUPPORTED; 1301 } 1302 // 1303 // http://apache.org/xml/features/validation/nonvalidating/load-dtd-grammar 1304 // 1305 if (suffixLength == Constants.LOAD_DTD_GRAMMAR_FEATURE.length() && 1306 featureId.endsWith(Constants.LOAD_DTD_GRAMMAR_FEATURE)) { 1307 return FeatureState.RECOGNIZED; 1308 } 1309 // 1310 // http://apache.org/xml/features/validation/nonvalidating/load-external-dtd 1311 // 1312 if (suffixLength == Constants.LOAD_EXTERNAL_DTD_FEATURE.length() && 1313 featureId.endsWith(Constants.LOAD_EXTERNAL_DTD_FEATURE)) { 1314 return FeatureState.RECOGNIZED; 1315 } 1316 1317 // 1318 // http://apache.org/xml/features/validation/default-attribute-values 1319 // 1320 if (suffixLength == Constants.VALIDATE_DATATYPES_FEATURE.length() && 1321 featureId.endsWith(Constants.VALIDATE_DATATYPES_FEATURE)) { 1322 return FeatureState.NOT_SUPPORTED; 1323 } 1324 1325 // 1326 // http://apache.org/xml/features/validation/schema 1327 // Lets the user turn Schema validation support on/off. 1328 // 1329 if (suffixLength == Constants.SCHEMA_VALIDATION_FEATURE.length() && 1330 featureId.endsWith(Constants.SCHEMA_VALIDATION_FEATURE)) { 1331 return FeatureState.RECOGNIZED; 1332 } 1333 // activate full schema checking 1334 if (suffixLength == Constants.SCHEMA_FULL_CHECKING.length() && 1335 featureId.endsWith(Constants.SCHEMA_FULL_CHECKING)) { 1336 return FeatureState.RECOGNIZED; 1337 } 1338 // Feature identifier: expose schema normalized value 1339 // http://apache.org/xml/features/validation/schema/normalized-value 1340 if (suffixLength == Constants.SCHEMA_NORMALIZED_VALUE.length() && 1341 featureId.endsWith(Constants.SCHEMA_NORMALIZED_VALUE)) { 1342 return FeatureState.RECOGNIZED; 1343 } 1344 // Feature identifier: send element default value via characters() 1345 // http://apache.org/xml/features/validation/schema/element-default 1346 if (suffixLength == Constants.SCHEMA_ELEMENT_DEFAULT.length() && 1347 featureId.endsWith(Constants.SCHEMA_ELEMENT_DEFAULT)) { 1348 return FeatureState.RECOGNIZED; 1349 } 1350 1351 // special performance feature: only component manager is allowed to set it. 1352 if (suffixLength == Constants.PARSER_SETTINGS.length() && 1353 featureId.endsWith(Constants.PARSER_SETTINGS)) { 1354 return FeatureState.NOT_SUPPORTED; 1355 } 1356 1357 } 1358 1359 // 1360 // Not recognized 1361 // 1362 1363 return super.checkFeature(featureId); 1364 1365 } // checkFeature(String) 1366 1367 /** 1368 * Check a property. If the property is know and supported, this method 1369 * simply returns. Otherwise, the appropriate exception is thrown. 1370 * 1371 * @param propertyId The unique identifier (URI) of the property 1372 * being set. 1373 * 1374 * @throws XMLConfigurationException Thrown for configuration error. 1375 * In general, components should 1376 * only throw this exception if 1377 * it is <strong>really</strong> 1378 * a critical error. 1379 */ 1380 protected PropertyState checkProperty(String propertyId) throws XMLConfigurationException { 1381 1382 // 1383 // Xerces Properties 1384 // 1385 1386 if (propertyId.startsWith(Constants.XERCES_PROPERTY_PREFIX)) { 1387 final int suffixLength = propertyId.length() - Constants.XERCES_PROPERTY_PREFIX.length(); 1388 1389 if (suffixLength == Constants.DTD_SCANNER_PROPERTY.length() && 1390 propertyId.endsWith(Constants.DTD_SCANNER_PROPERTY)) { 1391 return PropertyState.RECOGNIZED; 1392 } 1393 if (suffixLength == Constants.SCHEMA_LOCATION.length() && 1394 propertyId.endsWith(Constants.SCHEMA_LOCATION)) { 1395 return PropertyState.RECOGNIZED; 1396 } 1397 if (suffixLength == Constants.SCHEMA_NONS_LOCATION.length() && 1398 propertyId.endsWith(Constants.SCHEMA_NONS_LOCATION)) { 1399 return PropertyState.RECOGNIZED; 1400 } 1401 } 1402 1403 if (propertyId.startsWith(Constants.JAXP_PROPERTY_PREFIX)) { 1404 final int suffixLength = propertyId.length() - Constants.JAXP_PROPERTY_PREFIX.length(); 1405 1406 if (suffixLength == Constants.SCHEMA_SOURCE.length() && 1407 propertyId.endsWith(Constants.SCHEMA_SOURCE)) { 1408 return PropertyState.RECOGNIZED; 1409 } 1410 } 1411 1412 // special cases 1413 if (propertyId.startsWith(Constants.SAX_PROPERTY_PREFIX)) { 1414 final int suffixLength = propertyId.length() - Constants.SAX_PROPERTY_PREFIX.length(); 1415 1416 // 1417 // http://xml.org/sax/properties/xml-string 1418 // Value type: String 1419 // Access: read-only 1420 // Get the literal string of characters associated with the 1421 // current event. If the parser recognises and supports this 1422 // property but is not currently parsing text, it should return 1423 // null (this is a good way to check for availability before the 1424 // parse begins). 1425 // 1426 if (suffixLength == Constants.XML_STRING_PROPERTY.length() && 1427 propertyId.endsWith(Constants.XML_STRING_PROPERTY)) { 1428 // REVISIT - we should probably ask xml-dev for a precise 1429 // definition of what this is actually supposed to return, and 1430 // in exactly which circumstances. 1431 return PropertyState.NOT_SUPPORTED; 1432 } 1433 } 1434 1435 // 1436 // Not recognized 1437 // 1438 1439 return super.checkProperty(propertyId); 1440 1441 } // checkProperty(String) 1442 1443 1444 /** 1445 * Adds a component to the parser configuration. This method will 1446 * also add all of the component's recognized features and properties 1447 * to the list of default recognized features and properties. 1448 * 1449 * @param component The component to add. 1450 */ 1451 protected void addComponent(XMLComponent component) { 1452 1453 // don't add a component more than once 1454 if (fComponents.contains(component)) { 1455 return; 1456 } 1457 fComponents.add(component); 1458 addRecognizedParamsAndSetDefaults(component); 1459 1460 } // addComponent(XMLComponent) 1461 1462 /** 1463 * Adds common component to the parser configuration. This method will 1464 * also add all of the component's recognized features and properties 1465 * to the list of default recognized features and properties. 1466 * 1467 * @param component The component to add. 1468 */ 1469 protected void addCommonComponent(XMLComponent component) { 1470 1471 // don't add a component more than once 1472 if (fCommonComponents.contains(component)) { 1473 return; 1474 } 1475 fCommonComponents.add(component); 1476 addRecognizedParamsAndSetDefaults(component); 1477 1478 } // addCommonComponent(XMLComponent) 1479 1480 /** 1481 * Adds an XML 1.1 component to the parser configuration. This method will 1482 * also add all of the component's recognized features and properties 1483 * to the list of default recognized features and properties. 1484 * 1485 * @param component The component to add. 1486 */ 1487 protected void addXML11Component(XMLComponent component) { 1488 1489 // don't add a component more than once 1490 if (fXML11Components.contains(component)) { 1491 return; 1492 } 1493 fXML11Components.add(component); 1494 addRecognizedParamsAndSetDefaults(component); 1495 1496 } // addXML11Component(XMLComponent) 1497 1498 /** 1499 * Adds all of the component's recognized features and properties 1500 * to the list of default recognized features and properties, and 1501 * sets default values on the configuration for features and 1502 * properties which were previously absent from the configuration. 1503 * 1504 * @param component The component whose recognized features 1505 * and properties will be added to the configuration 1506 */ 1507 protected void addRecognizedParamsAndSetDefaults(XMLComponent component) { 1508 1509 // register component's recognized features 1510 String[] recognizedFeatures = component.getRecognizedFeatures(); 1511 addRecognizedFeatures(recognizedFeatures); 1512 1513 // register component's recognized properties 1514 String[] recognizedProperties = component.getRecognizedProperties(); 1515 addRecognizedProperties(recognizedProperties); 1516 1517 // set default values 1518 if (recognizedFeatures != null) { 1519 for (int i = 0; i < recognizedFeatures.length; ++i) { 1520 String featureId = recognizedFeatures[i]; 1521 Boolean state = component.getFeatureDefault(featureId); 1522 if (state != null) { 1523 // Do not overwrite values already set on the configuration. 1524 if (!fFeatures.containsKey(featureId)) { 1525 fFeatures.put(featureId, state); 1526 // For newly added components who recognize this feature 1527 // but did not offer a default value, we need to make 1528 // sure these components will get an opportunity to read 1529 // the value before parsing begins. 1530 fConfigUpdated = true; 1531 } 1532 } 1533 } 1534 } 1535 if (recognizedProperties != null) { 1536 for (int i = 0; i < recognizedProperties.length; ++i) { 1537 String propertyId = recognizedProperties[i]; 1538 Object value = component.getPropertyDefault(propertyId); 1539 if (value != null) { 1540 // Do not overwrite values already set on the configuration. 1541 if (!fProperties.containsKey(propertyId)) { 1542 fProperties.put(propertyId, value); 1543 // For newly added components who recognize this property 1544 // but did not offer a default value, we need to make 1545 // sure these components will get an opportunity to read 1546 // the value before parsing begins. 1547 fConfigUpdated = true; 1548 } 1549 } 1550 } 1551 } 1552 } 1553 1554 private void initXML11Components() { 1555 if (!f11Initialized) { 1556 1557 // create datatype factory 1558 fXML11DatatypeFactory = DTDDVFactory.getInstance(XML11_DATATYPE_VALIDATOR_FACTORY); 1559 1560 // setup XML 1.1 DTD pipeline 1561 fXML11DTDScanner = new XML11DTDScannerImpl(); 1562 addXML11Component(fXML11DTDScanner); 1563 fXML11DTDProcessor = new XML11DTDProcessor(); 1564 addXML11Component(fXML11DTDProcessor); 1565 1566 // setup XML 1.1. document pipeline - namespace aware 1567 fXML11NSDocScanner = new XML11NSDocumentScannerImpl(); 1568 addXML11Component(fXML11NSDocScanner); 1569 fXML11NSDTDValidator = new XML11NSDTDValidator(); 1570 addXML11Component(fXML11NSDTDValidator); 1571 1572 f11Initialized = true; 1573 } 1574 } 1575 1576 1577 /** 1578 * Reset the symbol table if it wasn't provided during construction, 1579 * its not the first time when parse is called after initialization 1580 * and RESET_SYMBOL_TABLE feature is set to true 1581 */ 1582 private void resetSymbolTable() { 1583 if (fFeatures.get(JdkXmlUtils.RESET_SYMBOL_TABLE) && !fSymbolTableProvided) { 1584 if (fSymbolTableJustInitialized) { 1585 // Skip symbol table reallocation for the first parsing process 1586 fSymbolTableJustInitialized = false; 1587 } else { 1588 fSymbolTable = new SymbolTable(); 1589 fProperties.put(SYMBOL_TABLE, fSymbolTable); 1590 } 1591 } 1592 } 1593 1594 /** 1595 * Returns the state of a feature. This method calls getFeature() 1596 * on ParserConfigurationSettings, bypassing getFeature() on this 1597 * class. 1598 */ 1599 FeatureState getFeatureState0(String featureId) 1600 throws XMLConfigurationException { 1601 return super.getFeatureState(featureId); 1602 } 1603 1604 } // class XML11Configuration