1 /* 2 * Copyright (c) 2013, 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.dom; 22 23 import com.sun.org.apache.xerces.internal.impl.Constants; 24 import com.sun.org.apache.xerces.internal.impl.XMLEntityManager; 25 import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter; 26 import com.sun.org.apache.xerces.internal.impl.dv.DTDDVFactory; 27 import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter; 28 import com.sun.org.apache.xerces.internal.impl.validation.ValidationManager; 29 import com.sun.org.apache.xerces.internal.util.DOMEntityResolverWrapper; 30 import com.sun.org.apache.xerces.internal.util.DOMErrorHandlerWrapper; 31 import com.sun.org.apache.xerces.internal.util.MessageFormatter; 32 import com.sun.org.apache.xerces.internal.util.ParserConfigurationSettings; 33 import com.sun.org.apache.xerces.internal.util.PropertyState; 34 import com.sun.org.apache.xerces.internal.util.SymbolTable; 35 import com.sun.org.apache.xerces.internal.utils.ObjectFactory; 36 import com.sun.org.apache.xerces.internal.utils.XMLSecurityManager; 37 import com.sun.org.apache.xerces.internal.utils.XMLSecurityPropertyManager; 38 import com.sun.org.apache.xerces.internal.xni.XMLDTDContentModelHandler; 39 import com.sun.org.apache.xerces.internal.xni.XMLDTDHandler; 40 import com.sun.org.apache.xerces.internal.xni.XMLDocumentHandler; 41 import com.sun.org.apache.xerces.internal.xni.XNIException; 42 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarPool; 43 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponent; 44 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager; 45 import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException; 46 import com.sun.org.apache.xerces.internal.xni.parser.XMLEntityResolver; 47 import com.sun.org.apache.xerces.internal.xni.parser.XMLErrorHandler; 48 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource; 49 import com.sun.org.apache.xerces.internal.xni.parser.XMLParserConfiguration; 50 import java.io.IOException; 51 import java.util.ArrayList; 52 import java.util.HashMap; 53 import java.util.List; 54 import java.util.Locale; 55 import javax.xml.XMLConstants; 56 import javax.xml.catalog.CatalogFeatures; 57 import jdk.xml.internal.JdkXmlUtils; 58 import org.w3c.dom.DOMConfiguration; 59 import org.w3c.dom.DOMErrorHandler; 60 import org.w3c.dom.DOMException; 61 import org.w3c.dom.DOMStringList; 62 import org.w3c.dom.ls.LSResourceResolver; 63 64 65 66 /** 67 * Xerces implementation of DOMConfiguration that maintains a table of recognized parameters. 68 * 69 * @xerces.internal 70 * 71 * @author Elena Litani, IBM 72 * @author Neeraj Bajaj, Sun Microsystems. 73 * @LastModified: Oct 2017 74 */ 75 public class DOMConfigurationImpl extends ParserConfigurationSettings 76 implements XMLParserConfiguration, DOMConfiguration { 77 78 // 79 // Constants 80 // 81 82 // feature identifiers 83 84 /** Feature identifier: validation. */ 85 protected static final String XERCES_VALIDATION = 86 Constants.SAX_FEATURE_PREFIX + Constants.VALIDATION_FEATURE; 87 88 /** Feature identifier: namespaces. */ 89 protected static final String XERCES_NAMESPACES = 90 Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE; 91 92 protected static final String SCHEMA = 93 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_VALIDATION_FEATURE; 94 95 protected static final String SCHEMA_FULL_CHECKING = 96 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_FULL_CHECKING; 97 98 protected static final String DYNAMIC_VALIDATION = 99 Constants.XERCES_FEATURE_PREFIX + Constants.DYNAMIC_VALIDATION_FEATURE; 100 101 protected static final String NORMALIZE_DATA = 102 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_NORMALIZED_VALUE; 103 104 /** sending psvi in the pipeline */ 105 protected static final String SEND_PSVI = 106 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_AUGMENT_PSVI; 107 108 protected final static String DTD_VALIDATOR_FACTORY_PROPERTY = 109 Constants.XERCES_PROPERTY_PREFIX + Constants.DATATYPE_VALIDATOR_FACTORY_PROPERTY; 110 111 /** Feature identifier: namespace growth */ 112 protected static final String NAMESPACE_GROWTH = 113 Constants.XERCES_FEATURE_PREFIX + Constants.NAMESPACE_GROWTH_FEATURE; 114 115 protected static final String TOLERATE_DUPLICATES = 116 Constants.XERCES_FEATURE_PREFIX + Constants.TOLERATE_DUPLICATES_FEATURE; 117 118 // property identifiers 119 120 /** Property identifier: entity manager. */ 121 protected static final String ENTITY_MANAGER = 122 Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_MANAGER_PROPERTY; 123 124 /** Property identifier: error reporter. */ 125 protected static final String ERROR_REPORTER = 126 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY; 127 128 /** Property identifier: xml string. */ 129 protected static final String XML_STRING = 130 Constants.SAX_PROPERTY_PREFIX + Constants.XML_STRING_PROPERTY; 131 132 /** Property identifier: symbol table. */ 133 protected static final String SYMBOL_TABLE = 134 Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY; 135 136 /** Property id: Grammar pool*/ 137 protected static final String GRAMMAR_POOL = 138 Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY; 139 140 /** Property identifier: error handler. */ 141 protected static final String ERROR_HANDLER = 142 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_HANDLER_PROPERTY; 143 144 /** Property identifier: entity resolver. */ 145 protected static final String ENTITY_RESOLVER = 146 Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_RESOLVER_PROPERTY; 147 148 /** Property identifier: JAXP schema language / DOM schema-type. */ 149 protected static final String JAXP_SCHEMA_LANGUAGE = 150 Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_LANGUAGE; 151 152 /** Property identifier: JAXP schema source/ DOM schema-location. */ 153 protected static final String JAXP_SCHEMA_SOURCE = 154 Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_SOURCE; 155 156 protected static final String VALIDATION_MANAGER = 157 Constants.XERCES_PROPERTY_PREFIX + Constants.VALIDATION_MANAGER_PROPERTY; 158 159 /** Property identifier: Schema DV Factory */ 160 protected static final String SCHEMA_DV_FACTORY = 161 Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_DV_FACTORY_PROPERTY; 162 163 /** Property identifier: Security manager. */ 164 private static final String SECURITY_MANAGER = Constants.SECURITY_MANAGER; 165 166 /** Property identifier: Security property manager. */ 167 private static final String XML_SECURITY_PROPERTY_MANAGER = 168 Constants.XML_SECURITY_PROPERTY_MANAGER; 169 170 // 171 // Data 172 // 173 XMLDocumentHandler fDocumentHandler; 174 175 /** Normalization features*/ 176 protected short features = 0; 177 178 protected final static short NAMESPACES = 0x1<<0; 179 protected final static short DTNORMALIZATION = 0x1<<1; 180 protected final static short ENTITIES = 0x1<<2; 181 protected final static short CDATA = 0x1<<3; 182 protected final static short SPLITCDATA = 0x1<<4; 183 protected final static short COMMENTS = 0x1<<5; 184 protected final static short VALIDATE = 0x1<<6; 185 protected final static short PSVI = 0x1<<7; 186 protected final static short WELLFORMED = 0x1<<8; 187 protected final static short NSDECL = 0x1<<9; 188 189 protected final static short INFOSET_TRUE_PARAMS = NAMESPACES | COMMENTS | WELLFORMED | NSDECL; 190 protected final static short INFOSET_FALSE_PARAMS = ENTITIES | DTNORMALIZATION | CDATA; 191 protected final static short INFOSET_MASK = INFOSET_TRUE_PARAMS | INFOSET_FALSE_PARAMS; 192 193 // components 194 195 /** Symbol table. */ 196 protected SymbolTable fSymbolTable; 197 198 /** Components. */ 199 protected List<XMLComponent> fComponents; 200 201 protected ValidationManager fValidationManager; 202 203 /** Locale. */ 204 protected Locale fLocale; 205 206 /** Error reporter */ 207 protected XMLErrorReporter fErrorReporter; 208 209 protected final DOMErrorHandlerWrapper fErrorHandlerWrapper = 210 new DOMErrorHandlerWrapper(); 211 212 // private data 213 214 private DOMStringList fRecognizedParameters; 215 216 217 // 218 // Constructors 219 // 220 221 /** Default Constructor. */ 222 protected DOMConfigurationImpl() { 223 this(null, null); 224 } // <init>() 225 226 /** 227 * Constructs a parser configuration using the specified symbol table. 228 * 229 * @param symbolTable The symbol table to use. 230 */ 231 protected DOMConfigurationImpl(SymbolTable symbolTable) { 232 this(symbolTable, null); 233 } // <init>(SymbolTable) 234 235 /** 236 * Constructs a parser configuration using the specified symbol table 237 * and parent settings. 238 * 239 * @param symbolTable The symbol table to use. 240 * @param parentSettings The parent settings. 241 */ 242 protected DOMConfigurationImpl(SymbolTable symbolTable, 243 XMLComponentManager parentSettings) { 244 super(parentSettings); 245 246 247 // create table for features and properties 248 fFeatures = new HashMap<>(); 249 fProperties = new HashMap<>(); 250 251 // add default recognized features 252 final String[] recognizedFeatures = { 253 XERCES_VALIDATION, 254 XERCES_NAMESPACES, 255 SCHEMA, 256 SCHEMA_FULL_CHECKING, 257 DYNAMIC_VALIDATION, 258 NORMALIZE_DATA, 259 SEND_PSVI, 260 NAMESPACE_GROWTH, 261 TOLERATE_DUPLICATES, 262 XMLConstants.USE_CATALOG 263 }; 264 addRecognizedFeatures(recognizedFeatures); 265 266 // set state for default features 267 setFeature(XERCES_VALIDATION, false); 268 setFeature(SCHEMA, false); 269 setFeature(SCHEMA_FULL_CHECKING, false); 270 setFeature(DYNAMIC_VALIDATION, false); 271 setFeature(NORMALIZE_DATA, false); 272 setFeature(XERCES_NAMESPACES, true); 273 setFeature(SEND_PSVI, true); 274 setFeature(NAMESPACE_GROWTH, false); 275 setFeature(XMLConstants.USE_CATALOG, JdkXmlUtils.USE_CATALOG_DEFAULT); 276 277 // add default recognized properties 278 final String[] recognizedProperties = { 279 XML_STRING, 280 SYMBOL_TABLE, 281 ERROR_HANDLER, 282 ENTITY_RESOLVER, 283 ERROR_REPORTER, 284 ENTITY_MANAGER, 285 VALIDATION_MANAGER, 286 GRAMMAR_POOL, 287 JAXP_SCHEMA_SOURCE, 288 JAXP_SCHEMA_LANGUAGE, 289 DTD_VALIDATOR_FACTORY_PROPERTY, 290 SCHEMA_DV_FACTORY, 291 SECURITY_MANAGER, 292 XML_SECURITY_PROPERTY_MANAGER, 293 JdkXmlUtils.CATALOG_DEFER, 294 JdkXmlUtils.CATALOG_FILES, 295 JdkXmlUtils.CATALOG_PREFER, 296 JdkXmlUtils.CATALOG_RESOLVE, 297 JdkXmlUtils.CDATA_CHUNK_SIZE 298 }; 299 addRecognizedProperties(recognizedProperties); 300 301 // set default values for normalization features 302 features |= NAMESPACES; 303 features |= ENTITIES; 304 features |= COMMENTS; 305 features |= CDATA; 306 features |= SPLITCDATA; 307 features |= WELLFORMED; 308 features |= NSDECL; 309 310 if (symbolTable == null) { 311 symbolTable = new SymbolTable(); 312 } 313 fSymbolTable = symbolTable; 314 315 fComponents = new ArrayList<>(); 316 317 setProperty(SYMBOL_TABLE, fSymbolTable); 318 fErrorReporter = new XMLErrorReporter(); 319 setProperty(ERROR_REPORTER, fErrorReporter); 320 addComponent(fErrorReporter); 321 322 setProperty(DTD_VALIDATOR_FACTORY_PROPERTY, DTDDVFactory.getInstance()); 323 324 XMLEntityManager manager = new XMLEntityManager(); 325 setProperty(ENTITY_MANAGER, manager); 326 addComponent(manager); 327 328 fValidationManager = createValidationManager(); 329 setProperty(VALIDATION_MANAGER, fValidationManager); 330 331 setProperty(SECURITY_MANAGER, new XMLSecurityManager(true)); 332 333 setProperty(Constants.XML_SECURITY_PROPERTY_MANAGER, 334 new XMLSecurityPropertyManager()); 335 336 // add message formatters 337 if (fErrorReporter.getMessageFormatter(XMLMessageFormatter.XML_DOMAIN) == null) { 338 XMLMessageFormatter xmft = new XMLMessageFormatter(); 339 fErrorReporter.putMessageFormatter(XMLMessageFormatter.XML_DOMAIN, xmft); 340 fErrorReporter.putMessageFormatter(XMLMessageFormatter.XMLNS_DOMAIN, xmft); 341 } 342 343 // REVISIT: try to include XML Schema formatter. 344 // This is a hack to allow DTD configuration to be build. 345 // 346 if (fErrorReporter.getMessageFormatter("http://www.w3.org/TR/xml-schema-1") == null) { 347 MessageFormatter xmft = null; 348 try { 349 xmft = (MessageFormatter)( 350 ObjectFactory.newInstance("com.sun.org.apache.xerces.internal.impl.xs.XSMessageFormatter", true)); 351 } catch (Exception exception){ 352 } 353 354 if (xmft != null) { 355 fErrorReporter.putMessageFormatter("http://www.w3.org/TR/xml-schema-1", xmft); 356 } 357 } 358 359 360 // set locale 361 try { 362 setLocale(Locale.getDefault()); 363 } 364 catch (XNIException e) { 365 // do nothing 366 // REVISIT: What is the right thing to do? -Ac 367 } 368 369 // Initialize Catalog features 370 for( CatalogFeatures.Feature f : CatalogFeatures.Feature.values()) { 371 setProperty(f.getPropertyName(), null); 372 } 373 374 setProperty(JdkXmlUtils.CDATA_CHUNK_SIZE, JdkXmlUtils.CDATA_CHUNK_SIZE_DEFAULT); 375 } // <init>(SymbolTable) 376 377 378 // 379 // XMLParserConfiguration methods 380 // 381 382 /** 383 * Parse an XML document. 384 * <p> 385 * The parser can use this method to instruct this configuration 386 * to begin parsing an XML document from any valid input source 387 * (a character stream, a byte stream, or a URI). 388 * <p> 389 * Parsers may not invoke this method while a parse is in progress. 390 * Once a parse is complete, the parser may then parse another XML 391 * document. 392 * <p> 393 * This method is synchronous: it will not return until parsing 394 * has ended. If a client application wants to terminate 395 * parsing early, it should throw an exception. 396 * 397 * @param source The input source for the top-level of the 398 * XML document. 399 * 400 * @exception XNIException Any XNI exception, possibly wrapping 401 * another exception. 402 * @exception IOException An IO exception from the parser, possibly 403 * from a byte stream or character stream 404 * supplied by the parser. 405 */ 406 public void parse(XMLInputSource inputSource) 407 throws XNIException, IOException{ 408 // no-op 409 } 410 411 /** 412 * Sets the document handler on the last component in the pipeline 413 * to receive information about the document. 414 * 415 * @param documentHandler The document handler. 416 */ 417 public void setDocumentHandler(XMLDocumentHandler documentHandler) { 418 fDocumentHandler = documentHandler; 419 } // setDocumentHandler(XMLDocumentHandler) 420 421 /** Returns the registered document handler. */ 422 public XMLDocumentHandler getDocumentHandler() { 423 return fDocumentHandler; 424 } // getDocumentHandler():XMLDocumentHandler 425 426 /** 427 * Sets the DTD handler. 428 * 429 * @param dtdHandler The DTD handler. 430 */ 431 public void setDTDHandler(XMLDTDHandler dtdHandler) { 432 //no-op 433 } // setDTDHandler(XMLDTDHandler) 434 435 /** Returns the registered DTD handler. */ 436 public XMLDTDHandler getDTDHandler() { 437 return null; 438 } // getDTDHandler():XMLDTDHandler 439 440 /** 441 * Sets the DTD content model handler. 442 * 443 * @param handler The DTD content model handler. 444 */ 445 public void setDTDContentModelHandler(XMLDTDContentModelHandler handler) { 446 //no-op 447 448 } // setDTDContentModelHandler(XMLDTDContentModelHandler) 449 450 /** Returns the registered DTD content model handler. */ 451 public XMLDTDContentModelHandler getDTDContentModelHandler() { 452 return null; 453 } // getDTDContentModelHandler():XMLDTDContentModelHandler 454 455 /** 456 * Sets the resolver used to resolve external entities. The EntityResolver 457 * interface supports resolution of public and system identifiers. 458 * 459 * @param resolver The new entity resolver. Passing a null value will 460 * uninstall the currently installed resolver. 461 */ 462 public void setEntityResolver(XMLEntityResolver resolver) { 463 if (resolver !=null) { 464 fProperties.put(ENTITY_RESOLVER, resolver); 465 } 466 } // setEntityResolver(XMLEntityResolver) 467 468 /** 469 * Return the current entity resolver. 470 * 471 * @return The current entity resolver, or null if none 472 * has been registered. 473 * @see #setEntityResolver 474 */ 475 public XMLEntityResolver getEntityResolver() { 476 return (XMLEntityResolver)fProperties.get(ENTITY_RESOLVER); 477 } // getEntityResolver():XMLEntityResolver 478 479 /** 480 * Allow an application to register an error event handler. 481 * 482 * <p>If the application does not register an error handler, all 483 * error events reported by the SAX parser will be silently 484 * ignored; however, normal processing may not continue. It is 485 * highly recommended that all SAX applications implement an 486 * error handler to avoid unexpected bugs.</p> 487 * 488 * <p>Applications may register a new or different handler in the 489 * middle of a parse, and the SAX parser must begin using the new 490 * handler immediately.</p> 491 * 492 * @param errorHandler The error handler. 493 * @exception java.lang.NullPointerException If the handler 494 * argument is null. 495 * @see #getErrorHandler 496 */ 497 public void setErrorHandler(XMLErrorHandler errorHandler) { 498 if (errorHandler != null) { 499 fProperties.put(ERROR_HANDLER, errorHandler); 500 } 501 } // setErrorHandler(XMLErrorHandler) 502 503 /** 504 * Return the current error handler. 505 * 506 * @return The current error handler, or null if none 507 * has been registered. 508 * @see #setErrorHandler 509 */ 510 public XMLErrorHandler getErrorHandler() { 511 return (XMLErrorHandler)fProperties.get(ERROR_HANDLER); 512 } // getErrorHandler():XMLErrorHandler 513 514 /** 515 * Set the state of a feature. 516 * 517 * Set the state of any feature in a SAX2 parser. The parser 518 * might not recognize the feature, and if it does recognize 519 * it, it might not be able to fulfill the request. 520 * 521 * @param featureId The unique identifier (URI) of the feature. 522 * @param state The requested state of the feature (true or false). 523 * 524 * @exception com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException If the 525 * requested feature is not known. 526 */ 527 public void setFeature(String featureId, boolean state) 528 throws XMLConfigurationException { 529 530 // save state if noone "objects" 531 super.setFeature(featureId, state); 532 533 } // setFeature(String,boolean) 534 535 /** 536 * setProperty 537 * 538 * @param propertyId 539 * @param value 540 */ 541 public void setProperty(String propertyId, Object value) 542 throws XMLConfigurationException { 543 544 // store value if noone "objects" 545 super.setProperty(propertyId, value); 546 547 } // setProperty(String,Object) 548 549 /** 550 * Set the locale to use for messages. 551 * 552 * @param locale The locale object to use for localization of messages. 553 * 554 * @exception XNIException Thrown if the parser does not support the 555 * specified locale. 556 */ 557 public void setLocale(Locale locale) throws XNIException { 558 fLocale = locale; 559 fErrorReporter.setLocale(locale); 560 561 } // setLocale(Locale) 562 563 /** Returns the locale. */ 564 public Locale getLocale() { 565 return fLocale; 566 } // getLocale():Locale 567 568 /** 569 * DOM Level 3 WD - Experimental. 570 * setParameter 571 */ 572 public void setParameter(String name, Object value) throws DOMException { 573 boolean found = true; 574 575 // REVISIT: Recognizes DOM L3 default features only. 576 // Does not yet recognize Xerces features. 577 if(value instanceof Boolean){ 578 boolean state = ((Boolean)value).booleanValue(); 579 580 if (name.equalsIgnoreCase(Constants.DOM_COMMENTS)) { 581 features = (short) (state ? features | COMMENTS : features & ~COMMENTS); 582 } 583 else if (name.equalsIgnoreCase(Constants.DOM_DATATYPE_NORMALIZATION)) { 584 setFeature(NORMALIZE_DATA, state); 585 features = 586 (short) (state ? features | DTNORMALIZATION : features & ~DTNORMALIZATION); 587 if (state) { 588 features = (short) (features | VALIDATE); 589 } 590 } 591 else if (name.equalsIgnoreCase(Constants.DOM_NAMESPACES)) { 592 features = (short) (state ? features | NAMESPACES : features & ~NAMESPACES); 593 } 594 else if (name.equalsIgnoreCase(Constants.DOM_CDATA_SECTIONS)) { 595 features = (short) (state ? features | CDATA : features & ~CDATA); 596 } 597 else if (name.equalsIgnoreCase(Constants.DOM_ENTITIES)) { 598 features = (short) (state ? features | ENTITIES : features & ~ENTITIES); 599 } 600 else if (name.equalsIgnoreCase(Constants.DOM_SPLIT_CDATA)) { 601 features = (short) (state ? features | SPLITCDATA : features & ~SPLITCDATA); 602 } 603 else if (name.equalsIgnoreCase(Constants.DOM_VALIDATE)) { 604 features = (short) (state ? features | VALIDATE : features & ~VALIDATE); 605 } 606 else if (name.equalsIgnoreCase(Constants.DOM_WELLFORMED)) { 607 features = (short) (state ? features | WELLFORMED : features & ~WELLFORMED ); 608 } 609 else if (name.equalsIgnoreCase(Constants.DOM_NAMESPACE_DECLARATIONS)) { 610 features = (short) (state ? features | NSDECL : features & ~NSDECL); 611 } 612 else if (name.equalsIgnoreCase(Constants.DOM_INFOSET)) { 613 // Setting to false has no effect. 614 if (state) { 615 features = (short) (features | INFOSET_TRUE_PARAMS); 616 features = (short) (features & ~INFOSET_FALSE_PARAMS); 617 setFeature(NORMALIZE_DATA, false); 618 } 619 } 620 else if (name.equalsIgnoreCase(Constants.DOM_NORMALIZE_CHARACTERS) 621 || name.equalsIgnoreCase(Constants.DOM_CANONICAL_FORM) 622 || name.equalsIgnoreCase(Constants.DOM_VALIDATE_IF_SCHEMA) 623 || name.equalsIgnoreCase(Constants.DOM_CHECK_CHAR_NORMALIZATION) 624 ) { 625 if (state) { // true is not supported 626 String msg = 627 DOMMessageFormatter.formatMessage( 628 DOMMessageFormatter.DOM_DOMAIN, 629 "FEATURE_NOT_SUPPORTED", 630 new Object[] { name }); 631 throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg); 632 } 633 } 634 else if ( name.equalsIgnoreCase(Constants.DOM_ELEMENT_CONTENT_WHITESPACE)) { 635 if (!state) { // false is not supported 636 String msg = 637 DOMMessageFormatter.formatMessage( 638 DOMMessageFormatter.DOM_DOMAIN, 639 "FEATURE_NOT_SUPPORTED", 640 new Object[] { name }); 641 throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg); 642 } 643 } 644 else if (name.equalsIgnoreCase(SEND_PSVI) ){ 645 // REVISIT: turning augmentation of PSVI is not support, 646 // because in this case we won't be able to retrieve element 647 // default value. 648 if (!state) { // false is not supported 649 String msg = 650 DOMMessageFormatter.formatMessage( 651 DOMMessageFormatter.DOM_DOMAIN, 652 "FEATURE_NOT_SUPPORTED", 653 new Object[] { name }); 654 throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg); 655 } 656 } 657 else if (name.equalsIgnoreCase(Constants.DOM_PSVI)){ 658 features = (short) (state ? features | PSVI : features & ~PSVI); 659 } 660 else { 661 found = false; 662 /* 663 String msg = 664 DOMMessageFormatter.formatMessage( 665 DOMMessageFormatter.DOM_DOMAIN, 666 "FEATURE_NOT_FOUND", 667 new Object[] { name }); 668 throw new DOMException(DOMException.NOT_FOUND_ERR, msg); 669 */ 670 } 671 672 } 673 674 if (!found || !(value instanceof Boolean)) { // set properties 675 found = true; 676 677 if (name.equalsIgnoreCase(Constants.DOM_ERROR_HANDLER)) { 678 if (value instanceof DOMErrorHandler || value == null) { 679 fErrorHandlerWrapper.setErrorHandler((DOMErrorHandler)value); 680 setErrorHandler(fErrorHandlerWrapper); 681 } 682 683 else { 684 // REVISIT: type mismatch 685 String msg = 686 DOMMessageFormatter.formatMessage( 687 DOMMessageFormatter.DOM_DOMAIN, 688 "TYPE_MISMATCH_ERR", 689 new Object[] { name }); 690 throw new DOMException(DOMException.TYPE_MISMATCH_ERR, msg); 691 } 692 } 693 else if (name.equalsIgnoreCase(Constants.DOM_RESOURCE_RESOLVER)) { 694 if (value instanceof LSResourceResolver || value == null) { 695 try { 696 setEntityResolver(new DOMEntityResolverWrapper((LSResourceResolver) value)); 697 } 698 catch (XMLConfigurationException e) {} 699 } 700 else { 701 // REVISIT: type mismatch 702 String msg = 703 DOMMessageFormatter.formatMessage( 704 DOMMessageFormatter.DOM_DOMAIN, 705 "TYPE_MISMATCH_ERR", 706 new Object[] { name }); 707 throw new DOMException(DOMException.TYPE_MISMATCH_ERR, msg); 708 } 709 710 } 711 else if (name.equalsIgnoreCase(Constants.DOM_SCHEMA_LOCATION)) { 712 if (value instanceof String || value == null) { 713 try { 714 // map DOM schema-location to JAXP schemaSource property 715 setProperty( 716 Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_SOURCE, 717 value); 718 } 719 catch (XMLConfigurationException e) {} 720 } 721 else { 722 // REVISIT: type mismatch 723 String msg = 724 DOMMessageFormatter.formatMessage( 725 DOMMessageFormatter.DOM_DOMAIN, 726 "TYPE_MISMATCH_ERR", 727 new Object[] { name }); 728 throw new DOMException(DOMException.TYPE_MISMATCH_ERR, msg); 729 } 730 731 } 732 else if (name.equalsIgnoreCase(Constants.DOM_SCHEMA_TYPE)) { 733 if (value instanceof String || value == null) { 734 try { 735 if (value == null) { 736 setProperty( 737 Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_LANGUAGE, 738 null); 739 } 740 else if (value.equals(Constants.NS_XMLSCHEMA)) { 741 // REVISIT: when add support to DTD validation 742 setProperty( 743 Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_LANGUAGE, 744 Constants.NS_XMLSCHEMA); 745 } 746 else if (value.equals(Constants.NS_DTD)) { 747 // Added support for revalidation against DTDs 748 setProperty(Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_LANGUAGE, 749 Constants.NS_DTD); 750 } 751 } 752 catch (XMLConfigurationException e) {} 753 } 754 else { 755 String msg = 756 DOMMessageFormatter.formatMessage( 757 DOMMessageFormatter.DOM_DOMAIN, 758 "TYPE_MISMATCH_ERR", 759 new Object[] { name }); 760 throw new DOMException(DOMException.TYPE_MISMATCH_ERR, msg); 761 } 762 763 } 764 else if (name.equalsIgnoreCase(SYMBOL_TABLE)){ 765 // Xerces Symbol Table 766 if (value instanceof SymbolTable){ 767 setProperty(SYMBOL_TABLE, value); 768 } 769 else { 770 // REVISIT: type mismatch 771 String msg = 772 DOMMessageFormatter.formatMessage( 773 DOMMessageFormatter.DOM_DOMAIN, 774 "TYPE_MISMATCH_ERR", 775 new Object[] { name }); 776 throw new DOMException(DOMException.TYPE_MISMATCH_ERR, msg); 777 } 778 } 779 else if (name.equalsIgnoreCase (GRAMMAR_POOL)){ 780 if (value instanceof XMLGrammarPool){ 781 setProperty(GRAMMAR_POOL, value); 782 } 783 else { 784 // REVISIT: type mismatch 785 String msg = 786 DOMMessageFormatter.formatMessage( 787 DOMMessageFormatter.DOM_DOMAIN, 788 "TYPE_MISMATCH_ERR", 789 new Object[] { name }); 790 throw new DOMException(DOMException.TYPE_MISMATCH_ERR, msg); 791 } 792 793 } 794 else { 795 // REVISIT: check if this is a boolean parameter -- type mismatch should be thrown. 796 //parameter is not recognized 797 String msg = 798 DOMMessageFormatter.formatMessage( 799 DOMMessageFormatter.DOM_DOMAIN, 800 "FEATURE_NOT_FOUND", 801 new Object[] { name }); 802 throw new DOMException(DOMException.NOT_FOUND_ERR, msg); 803 } 804 } 805 806 } 807 808 809 /** 810 * DOM Level 3 WD - Experimental. 811 * getParameter 812 */ 813 public Object getParameter(String name) throws DOMException { 814 815 // REVISIT: Recognizes DOM L3 default features only. 816 // Does not yet recognize Xerces features. 817 818 if (name.equalsIgnoreCase(Constants.DOM_COMMENTS)) { 819 return ((features & COMMENTS) != 0) ? Boolean.TRUE : Boolean.FALSE; 820 } 821 else if (name.equalsIgnoreCase(Constants.DOM_NAMESPACES)) { 822 return (features & NAMESPACES) != 0 ? Boolean.TRUE : Boolean.FALSE; 823 } 824 else if (name.equalsIgnoreCase(Constants.DOM_DATATYPE_NORMALIZATION)) { 825 // REVISIT: datatype-normalization only takes effect if validation is on 826 return (features & DTNORMALIZATION) != 0 ? Boolean.TRUE : Boolean.FALSE; 827 } 828 else if (name.equalsIgnoreCase(Constants.DOM_CDATA_SECTIONS)) { 829 return (features & CDATA) != 0 ? Boolean.TRUE : Boolean.FALSE; 830 } 831 else if (name.equalsIgnoreCase(Constants.DOM_ENTITIES)) { 832 return (features & ENTITIES) != 0 ? Boolean.TRUE : Boolean.FALSE; 833 } 834 else if (name.equalsIgnoreCase(Constants.DOM_SPLIT_CDATA)) { 835 return (features & SPLITCDATA) != 0 ? Boolean.TRUE : Boolean.FALSE; 836 } 837 else if (name.equalsIgnoreCase(Constants.DOM_VALIDATE)) { 838 return (features & VALIDATE) != 0 ? Boolean.TRUE : Boolean.FALSE; 839 } 840 else if (name.equalsIgnoreCase(Constants.DOM_WELLFORMED)) { 841 return (features & WELLFORMED) != 0 ? Boolean.TRUE : Boolean.FALSE; 842 } 843 else if (name.equalsIgnoreCase(Constants.DOM_NAMESPACE_DECLARATIONS)) { 844 return (features & NSDECL) != 0 ? Boolean.TRUE : Boolean.FALSE; 845 } 846 else if (name.equalsIgnoreCase(Constants.DOM_INFOSET)) { 847 return (features & INFOSET_MASK) == INFOSET_TRUE_PARAMS ? Boolean.TRUE : Boolean.FALSE; 848 } 849 else if (name.equalsIgnoreCase(Constants.DOM_NORMALIZE_CHARACTERS) 850 || name.equalsIgnoreCase(Constants.DOM_CANONICAL_FORM) 851 || name.equalsIgnoreCase(Constants.DOM_VALIDATE_IF_SCHEMA) 852 || name.equalsIgnoreCase(Constants.DOM_CHECK_CHAR_NORMALIZATION) 853 ) { 854 return Boolean.FALSE; 855 } 856 else if (name.equalsIgnoreCase(SEND_PSVI)) { 857 return Boolean.TRUE; 858 } 859 else if (name.equalsIgnoreCase(Constants.DOM_PSVI)) { 860 return (features & PSVI) != 0 ? Boolean.TRUE : Boolean.FALSE; 861 } 862 else if (name.equalsIgnoreCase(Constants.DOM_ELEMENT_CONTENT_WHITESPACE)) { 863 return Boolean.TRUE; 864 } 865 else if (name.equalsIgnoreCase(Constants.DOM_ERROR_HANDLER)) { 866 return fErrorHandlerWrapper.getErrorHandler(); 867 } 868 else if (name.equalsIgnoreCase(Constants.DOM_RESOURCE_RESOLVER)) { 869 XMLEntityResolver entityResolver = getEntityResolver(); 870 if (entityResolver != null && entityResolver instanceof DOMEntityResolverWrapper) { 871 return ((DOMEntityResolverWrapper) entityResolver).getEntityResolver(); 872 } 873 return null; 874 } 875 else if (name.equalsIgnoreCase(Constants.DOM_SCHEMA_TYPE)) { 876 return getProperty(Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_LANGUAGE); 877 } 878 else if (name.equalsIgnoreCase(Constants.DOM_SCHEMA_LOCATION)) { 879 return getProperty(Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_SOURCE); 880 } 881 else if (name.equalsIgnoreCase(SYMBOL_TABLE)){ 882 return getProperty(SYMBOL_TABLE); 883 } 884 else if (name.equalsIgnoreCase(GRAMMAR_POOL)){ 885 return getProperty(GRAMMAR_POOL); 886 } 887 else { 888 String msg = 889 DOMMessageFormatter.formatMessage( 890 DOMMessageFormatter.DOM_DOMAIN, 891 "FEATURE_NOT_FOUND", 892 new Object[] { name }); 893 throw new DOMException(DOMException.NOT_FOUND_ERR, msg); 894 } 895 896 } 897 898 /** 899 * DOM Level 3 WD - Experimental. 900 * Check if setting a parameter to a specific value is supported. 901 * 902 * @param name The name of the parameter to check. 903 * 904 * @param value An object. if null, the returned value is true. 905 * 906 * @return true if the parameter could be successfully set to the 907 * specified value, or false if the parameter is not recognized or 908 * the requested value is not supported. This does not change the 909 * current value of the parameter itself. 910 */ 911 public boolean canSetParameter(String name, Object value) { 912 913 if (value == null){ 914 //if null, the returned value is true. 915 //REVISIT: I dont like this --- even for unrecognized parameter it would 916 //return 'true'. I think it should return false in that case. 917 // Application will be surprised to find that setParameter throws not 918 //recognized exception when canSetParameter returns 'true' Then what is the use 919 //of having canSetParameter ??? - nb. 920 return true ; 921 } 922 if( value instanceof Boolean ){ 923 //features whose parameter value can be set either 'true' or 'false' 924 // or they accept any boolean value -- so we just need to check that 925 // its a boolean value.. 926 if (name.equalsIgnoreCase(Constants.DOM_COMMENTS) 927 || name.equalsIgnoreCase(Constants.DOM_DATATYPE_NORMALIZATION) 928 || name.equalsIgnoreCase(Constants.DOM_CDATA_SECTIONS) 929 || name.equalsIgnoreCase(Constants.DOM_ENTITIES) 930 || name.equalsIgnoreCase(Constants.DOM_SPLIT_CDATA) 931 || name.equalsIgnoreCase(Constants.DOM_NAMESPACES) 932 || name.equalsIgnoreCase(Constants.DOM_VALIDATE) 933 || name.equalsIgnoreCase(Constants.DOM_WELLFORMED) 934 || name.equalsIgnoreCase(Constants.DOM_INFOSET) 935 || name.equalsIgnoreCase(Constants.DOM_NAMESPACE_DECLARATIONS) 936 ) { 937 return true; 938 }//features whose parameter value can not be set to 'true' 939 else if ( 940 name.equalsIgnoreCase(Constants.DOM_NORMALIZE_CHARACTERS) 941 || name.equalsIgnoreCase(Constants.DOM_CANONICAL_FORM) 942 || name.equalsIgnoreCase(Constants.DOM_VALIDATE_IF_SCHEMA) 943 || name.equalsIgnoreCase(Constants.DOM_CHECK_CHAR_NORMALIZATION) 944 ) { 945 return (value.equals(Boolean.TRUE)) ? false : true; 946 }//features whose parameter value can not be set to 'false' 947 else if( name.equalsIgnoreCase(Constants.DOM_ELEMENT_CONTENT_WHITESPACE) 948 || name.equalsIgnoreCase(SEND_PSVI) 949 ) { 950 return (value.equals(Boolean.TRUE)) ? true : false; 951 }// if name is not among the above listed above -- its not recognized. return false 952 else { 953 return false ; 954 } 955 } 956 else if (name.equalsIgnoreCase(Constants.DOM_ERROR_HANDLER)) { 957 return (value instanceof DOMErrorHandler) ? true : false ; 958 } 959 else if (name.equalsIgnoreCase(Constants.DOM_RESOURCE_RESOLVER)) { 960 return (value instanceof LSResourceResolver) ? true : false ; 961 } 962 else if (name.equalsIgnoreCase(Constants.DOM_SCHEMA_LOCATION)) { 963 return (value instanceof String) ? true : false ; 964 } 965 else if (name.equalsIgnoreCase(Constants.DOM_SCHEMA_TYPE)) { 966 // REVISIT: should null value be supported? 967 //as of now we are only supporting W3C XML Schema 968 return ( (value instanceof String) && value.equals(Constants.NS_XMLSCHEMA) ) ? true : false ; 969 } 970 else if (name.equalsIgnoreCase(SYMBOL_TABLE)){ 971 // Xerces Symbol Table 972 return (value instanceof SymbolTable) ? true : false ; 973 } 974 else if (name.equalsIgnoreCase (GRAMMAR_POOL)){ 975 return (value instanceof XMLGrammarPool) ? true : false ; 976 } 977 else { 978 //false if the parameter is not recognized or the requested value is not supported. 979 return false ; 980 } 981 982 } //canSetParameter 983 984 /** 985 * DOM Level 3 CR - Experimental. 986 * 987 * The list of the parameters supported by this 988 * <code>DOMConfiguration</code> object and for which at least one value 989 * can be set by the application. Note that this list can also contain 990 * parameter names defined outside this specification. 991 */ 992 public DOMStringList getParameterNames() { 993 if (fRecognizedParameters == null){ 994 List<String> parameters = new ArrayList<>(); 995 996 //Add DOM recognized parameters 997 //REVISIT: Would have been nice to have a list of 998 //recognized paramters. 999 parameters.add(Constants.DOM_COMMENTS); 1000 parameters.add(Constants.DOM_DATATYPE_NORMALIZATION); 1001 parameters.add(Constants.DOM_CDATA_SECTIONS); 1002 parameters.add(Constants.DOM_ENTITIES); 1003 parameters.add(Constants.DOM_SPLIT_CDATA); 1004 parameters.add(Constants.DOM_NAMESPACES); 1005 parameters.add(Constants.DOM_VALIDATE); 1006 1007 parameters.add(Constants.DOM_INFOSET); 1008 parameters.add(Constants.DOM_NORMALIZE_CHARACTERS); 1009 parameters.add(Constants.DOM_CANONICAL_FORM); 1010 parameters.add(Constants.DOM_VALIDATE_IF_SCHEMA); 1011 parameters.add(Constants.DOM_CHECK_CHAR_NORMALIZATION); 1012 parameters.add(Constants.DOM_WELLFORMED); 1013 1014 parameters.add(Constants.DOM_NAMESPACE_DECLARATIONS); 1015 parameters.add(Constants.DOM_ELEMENT_CONTENT_WHITESPACE); 1016 1017 parameters.add(Constants.DOM_ERROR_HANDLER); 1018 parameters.add(Constants.DOM_SCHEMA_TYPE); 1019 parameters.add(Constants.DOM_SCHEMA_LOCATION); 1020 parameters.add(Constants.DOM_RESOURCE_RESOLVER); 1021 1022 //Add recognized xerces features and properties 1023 parameters.add(GRAMMAR_POOL); 1024 parameters.add(SYMBOL_TABLE); 1025 parameters.add(SEND_PSVI); 1026 1027 fRecognizedParameters = new DOMStringListImpl(parameters); 1028 } 1029 1030 return fRecognizedParameters; 1031 }//getParameterNames 1032 1033 // 1034 // Protected methods 1035 // 1036 1037 /** 1038 * reset all components before parsing 1039 */ 1040 protected void reset() throws XNIException { 1041 1042 if (fValidationManager != null) 1043 fValidationManager.reset(); 1044 1045 int count = fComponents.size(); 1046 for (int i = 0; i < count; i++) { 1047 XMLComponent c = fComponents.get(i); 1048 c.reset(this); 1049 } 1050 1051 } // reset() 1052 1053 /** 1054 * Check a property. If the property is known and supported, this method 1055 * simply returns. Otherwise, the appropriate exception is thrown. 1056 * 1057 * @param propertyId The unique identifier (URI) of the property 1058 * being set. 1059 * @exception com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException If the 1060 * requested feature is not known or supported. 1061 */ 1062 protected PropertyState checkProperty(String propertyId) 1063 throws XMLConfigurationException { 1064 1065 // special cases 1066 if (propertyId.startsWith(Constants.SAX_PROPERTY_PREFIX)) { 1067 final int suffixLength = propertyId.length() - Constants.SAX_PROPERTY_PREFIX.length(); 1068 1069 // 1070 // http://xml.org/sax/properties/xml-string 1071 // Value type: String 1072 // Access: read-only 1073 // Get the literal string of characters associated with the 1074 // current event. If the parser recognises and supports this 1075 // property but is not currently parsing text, it should return 1076 // null (this is a good way to check for availability before the 1077 // parse begins). 1078 // 1079 if (suffixLength == Constants.XML_STRING_PROPERTY.length() && 1080 propertyId.endsWith(Constants.XML_STRING_PROPERTY)) { 1081 // REVISIT - we should probably ask xml-dev for a precise 1082 // definition of what this is actually supposed to return, and 1083 // in exactly which circumstances. 1084 return PropertyState.NOT_SUPPORTED; 1085 } 1086 } 1087 1088 // check property 1089 return super.checkProperty(propertyId); 1090 1091 } // checkProperty(String) 1092 1093 1094 protected void addComponent(XMLComponent component) { 1095 1096 // don't add a component more than once 1097 if (fComponents.contains(component)) { 1098 return; 1099 } 1100 fComponents.add(component); 1101 1102 // register component's recognized features 1103 String[] recognizedFeatures = component.getRecognizedFeatures(); 1104 addRecognizedFeatures(recognizedFeatures); 1105 1106 // register component's recognized properties 1107 String[] recognizedProperties = component.getRecognizedProperties(); 1108 addRecognizedProperties(recognizedProperties); 1109 1110 } // addComponent(XMLComponent) 1111 1112 protected ValidationManager createValidationManager(){ 1113 return new ValidationManager(); 1114 } 1115 1116 } // class XMLParser