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