src/java.xml/share/classes/com/sun/org/apache/xerces/internal/dom/DOMConfigurationImpl.java

Print this page


   1 /*
   2  * Copyright (c) 2013, 2014, 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 


  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.Locale;




  54 import org.w3c.dom.DOMConfiguration;
  55 import org.w3c.dom.DOMErrorHandler;
  56 import org.w3c.dom.DOMException;
  57 import org.w3c.dom.DOMStringList;
  58 import org.w3c.dom.ls.LSResourceResolver;
  59 
  60 
  61 
  62 /**
  63  * Xerces implementation of DOMConfiguration that maintains a table of recognized parameters.
  64  *
  65  * @xerces.internal
  66  *
  67  * @author Elena Litani, IBM
  68  * @author Neeraj Bajaj, Sun Microsystems.
  69  */
  70 public class DOMConfigurationImpl extends ParserConfigurationSettings
  71     implements XMLParserConfiguration, DOMConfiguration {
  72 
  73     //


 174     protected final static short DTNORMALIZATION     = 0x1<<1;
 175     protected final static short ENTITIES            = 0x1<<2;
 176     protected final static short CDATA               = 0x1<<3;
 177     protected final static short SPLITCDATA          = 0x1<<4;
 178     protected final static short COMMENTS            = 0x1<<5;
 179     protected final static short VALIDATE            = 0x1<<6;
 180     protected final static short PSVI                = 0x1<<7;
 181     protected final static short WELLFORMED          = 0x1<<8;
 182     protected final static short NSDECL              = 0x1<<9;
 183 
 184     protected final static short INFOSET_TRUE_PARAMS = NAMESPACES | COMMENTS | WELLFORMED | NSDECL;
 185     protected final static short INFOSET_FALSE_PARAMS = ENTITIES | DTNORMALIZATION | CDATA;
 186     protected final static short INFOSET_MASK = INFOSET_TRUE_PARAMS | INFOSET_FALSE_PARAMS;
 187 
 188     // components
 189 
 190     /** Symbol table. */
 191     protected SymbolTable fSymbolTable;
 192 
 193     /** Components. */
 194     protected ArrayList fComponents;
 195 
 196     protected ValidationManager fValidationManager;
 197 
 198     /** Locale. */
 199     protected Locale fLocale;
 200 
 201     /** Error reporter */
 202     protected XMLErrorReporter fErrorReporter;
 203 
 204     protected final DOMErrorHandlerWrapper fErrorHandlerWrapper =
 205                 new DOMErrorHandlerWrapper();
 206 
 207     // private data
 208 
 209     private DOMStringList fRecognizedParameters;
 210 
 211 
 212     //
 213     // Constructors
 214     //


 236      */
 237     protected DOMConfigurationImpl(SymbolTable symbolTable,
 238                                     XMLComponentManager parentSettings) {
 239         super(parentSettings);
 240 
 241 
 242         // create table for features and properties
 243         fFeatures = new HashMap();
 244         fProperties = new HashMap();
 245 
 246         // add default recognized features
 247         final String[] recognizedFeatures = {
 248             XERCES_VALIDATION,
 249             XERCES_NAMESPACES,
 250             SCHEMA,
 251             SCHEMA_FULL_CHECKING,
 252             DYNAMIC_VALIDATION,
 253             NORMALIZE_DATA,
 254             SEND_PSVI,
 255             NAMESPACE_GROWTH,
 256             TOLERATE_DUPLICATES

 257         };
 258         addRecognizedFeatures(recognizedFeatures);
 259 
 260         // set state for default features
 261         setFeature(XERCES_VALIDATION, false);
 262         setFeature(SCHEMA, false);
 263         setFeature(SCHEMA_FULL_CHECKING, false);
 264         setFeature(DYNAMIC_VALIDATION, false);
 265         setFeature(NORMALIZE_DATA, false);
 266         setFeature(XERCES_NAMESPACES, true);
 267         setFeature(SEND_PSVI, true);
 268         setFeature(NAMESPACE_GROWTH, false);

 269 
 270         // add default recognized properties
 271         final String[] recognizedProperties = {
 272             XML_STRING,
 273             SYMBOL_TABLE,
 274             ERROR_HANDLER,
 275             ENTITY_RESOLVER,
 276             ERROR_REPORTER,
 277             ENTITY_MANAGER,
 278             VALIDATION_MANAGER,
 279             GRAMMAR_POOL,
 280             JAXP_SCHEMA_SOURCE,
 281             JAXP_SCHEMA_LANGUAGE,
 282             DTD_VALIDATOR_FACTORY_PROPERTY,
 283             SCHEMA_DV_FACTORY,
 284             SECURITY_MANAGER,
 285             XML_SECURITY_PROPERTY_MANAGER




 286         };
 287         addRecognizedProperties(recognizedProperties);
 288 
 289         // set default values for normalization features
 290         features |= NAMESPACES;
 291         features |= ENTITIES;
 292         features |= COMMENTS;
 293         features |= CDATA;
 294         features |= SPLITCDATA;
 295         features |= WELLFORMED;
 296         features |= NSDECL;
 297 
 298         if (symbolTable == null) {
 299             symbolTable = new SymbolTable();
 300         }
 301         fSymbolTable = symbolTable;
 302 
 303         fComponents = new ArrayList();
 304 
 305         setProperty(SYMBOL_TABLE, fSymbolTable);
 306         fErrorReporter = new XMLErrorReporter();
 307         setProperty(ERROR_REPORTER, fErrorReporter);
 308         addComponent(fErrorReporter);
 309 
 310         setProperty(DTD_VALIDATOR_FACTORY_PROPERTY, DTDDVFactory.getInstance());
 311 
 312         XMLEntityManager manager =  new XMLEntityManager();
 313         setProperty(ENTITY_MANAGER, manager);
 314         addComponent(manager);
 315 
 316         fValidationManager = createValidationManager();
 317         setProperty(VALIDATION_MANAGER, fValidationManager);
 318 
 319         setProperty(SECURITY_MANAGER, new XMLSecurityManager(true));
 320 
 321         setProperty(Constants.XML_SECURITY_PROPERTY_MANAGER,
 322                 new XMLSecurityPropertyManager());
 323 


 337                xmft = (MessageFormatter)(
 338                     ObjectFactory.newInstance("com.sun.org.apache.xerces.internal.impl.xs.XSMessageFormatter", true));
 339             } catch (Exception exception){
 340             }
 341 
 342              if (xmft !=  null) {
 343                  fErrorReporter.putMessageFormatter("http://www.w3.org/TR/xml-schema-1", xmft);
 344              }
 345         }
 346 
 347 
 348         // set locale
 349         try {
 350             setLocale(Locale.getDefault());
 351         }
 352         catch (XNIException e) {
 353             // do nothing
 354             // REVISIT: What is the right thing to do? -Ac
 355         }
 356 
 357 



 358     } // <init>(SymbolTable)
 359 
 360 
 361     //
 362     // XMLParserConfiguration methods
 363     //
 364 
 365     /**
 366      * Parse an XML document.
 367      * <p>
 368      * The parser can use this method to instruct this configuration
 369      * to begin parsing an XML document from any valid input source
 370      * (a character stream, a byte stream, or a URI).
 371      * <p>
 372      * Parsers may not invoke this method while a parse is in progress.
 373      * Once a parse is complete, the parser may then parse another XML
 374      * document.
 375      * <p>
 376      * This method is synchronous: it will not return until parsing
 377      * has ended.  If a client application wants to terminate


1010             fRecognizedParameters = new DOMStringListImpl(parameters);
1011         }
1012 
1013         return fRecognizedParameters;
1014     }//getParameterNames
1015 
1016     //
1017     // Protected methods
1018     //
1019 
1020     /**
1021      * reset all components before parsing
1022      */
1023     protected void reset() throws XNIException {
1024 
1025         if (fValidationManager != null)
1026             fValidationManager.reset();
1027 
1028         int count = fComponents.size();
1029         for (int i = 0; i < count; i++) {
1030             XMLComponent c = (XMLComponent) fComponents.get(i);
1031             c.reset(this);
1032         }
1033 
1034     } // reset()
1035 
1036     /**
1037      * Check a property. If the property is known and supported, this method
1038      * simply returns. Otherwise, the appropriate exception is thrown.
1039      *
1040      * @param propertyId The unique identifier (URI) of the property
1041      *                   being set.
1042      * @exception com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException If the
1043      *            requested feature is not known or supported.
1044      */
1045     protected PropertyState checkProperty(String propertyId)
1046         throws XMLConfigurationException {
1047 
1048         // special cases
1049         if (propertyId.startsWith(Constants.SAX_PROPERTY_PREFIX)) {
1050             final int suffixLength = propertyId.length() - Constants.SAX_PROPERTY_PREFIX.length();


   1 /*
   2  * Copyright (c) 2013, 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.dom;
  22 


  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.Locale;
  54 import javax.xml.XMLConstants;
  55 import javax.xml.catalog.CatalogFeatures;
  56 import jdk.xml.internal.JdkXmlUtils;
  57 import jdk.xml.internal.SecuritySupport;
  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  */
  74 public class DOMConfigurationImpl extends ParserConfigurationSettings
  75     implements XMLParserConfiguration, DOMConfiguration {
  76 
  77     //


 178     protected final static short DTNORMALIZATION     = 0x1<<1;
 179     protected final static short ENTITIES            = 0x1<<2;
 180     protected final static short CDATA               = 0x1<<3;
 181     protected final static short SPLITCDATA          = 0x1<<4;
 182     protected final static short COMMENTS            = 0x1<<5;
 183     protected final static short VALIDATE            = 0x1<<6;
 184     protected final static short PSVI                = 0x1<<7;
 185     protected final static short WELLFORMED          = 0x1<<8;
 186     protected final static short NSDECL              = 0x1<<9;
 187 
 188     protected final static short INFOSET_TRUE_PARAMS = NAMESPACES | COMMENTS | WELLFORMED | NSDECL;
 189     protected final static short INFOSET_FALSE_PARAMS = ENTITIES | DTNORMALIZATION | CDATA;
 190     protected final static short INFOSET_MASK = INFOSET_TRUE_PARAMS | INFOSET_FALSE_PARAMS;
 191 
 192     // components
 193 
 194     /** Symbol table. */
 195     protected SymbolTable fSymbolTable;
 196 
 197     /** Components. */
 198     protected ArrayList<XMLComponent> fComponents;
 199 
 200     protected ValidationManager fValidationManager;
 201 
 202     /** Locale. */
 203     protected Locale fLocale;
 204 
 205     /** Error reporter */
 206     protected XMLErrorReporter fErrorReporter;
 207 
 208     protected final DOMErrorHandlerWrapper fErrorHandlerWrapper =
 209                 new DOMErrorHandlerWrapper();
 210 
 211     // private data
 212 
 213     private DOMStringList fRecognizedParameters;
 214 
 215 
 216     //
 217     // Constructors
 218     //


 240      */
 241     protected DOMConfigurationImpl(SymbolTable symbolTable,
 242                                     XMLComponentManager parentSettings) {
 243         super(parentSettings);
 244 
 245 
 246         // create table for features and properties
 247         fFeatures = new HashMap();
 248         fProperties = new HashMap();
 249 
 250         // add default recognized features
 251         final String[] recognizedFeatures = {
 252             XERCES_VALIDATION,
 253             XERCES_NAMESPACES,
 254             SCHEMA,
 255             SCHEMA_FULL_CHECKING,
 256             DYNAMIC_VALIDATION,
 257             NORMALIZE_DATA,
 258             SEND_PSVI,
 259             NAMESPACE_GROWTH,
 260             TOLERATE_DUPLICATES,
 261             XMLConstants.USE_CATALOG
 262         };
 263         addRecognizedFeatures(recognizedFeatures);
 264 
 265         // set state for default features
 266         setFeature(XERCES_VALIDATION, false);
 267         setFeature(SCHEMA, false);
 268         setFeature(SCHEMA_FULL_CHECKING, false);
 269         setFeature(DYNAMIC_VALIDATION, false);
 270         setFeature(NORMALIZE_DATA, false);
 271         setFeature(XERCES_NAMESPACES, true);
 272         setFeature(SEND_PSVI, true);
 273         setFeature(NAMESPACE_GROWTH, false);
 274         setFeature(XMLConstants.USE_CATALOG, JdkXmlUtils.USE_CATALOG_DEFAULT);
 275 
 276         // add default recognized properties
 277         final String[] recognizedProperties = {
 278             XML_STRING,
 279             SYMBOL_TABLE,
 280             ERROR_HANDLER,
 281             ENTITY_RESOLVER,
 282             ERROR_REPORTER,
 283             ENTITY_MANAGER,
 284             VALIDATION_MANAGER,
 285             GRAMMAR_POOL,
 286             JAXP_SCHEMA_SOURCE,
 287             JAXP_SCHEMA_LANGUAGE,
 288             DTD_VALIDATOR_FACTORY_PROPERTY,
 289             SCHEMA_DV_FACTORY,
 290             SECURITY_MANAGER,
 291             XML_SECURITY_PROPERTY_MANAGER,
 292             JdkXmlUtils.CATALOG_DEFER,
 293             JdkXmlUtils.CATALOG_FILES,
 294             JdkXmlUtils.CATALOG_PREFER,
 295             JdkXmlUtils.CATALOG_RESOLVE
 296         };
 297         addRecognizedProperties(recognizedProperties);
 298 
 299         // set default values for normalization features
 300         features |= NAMESPACES;
 301         features |= ENTITIES;
 302         features |= COMMENTS;
 303         features |= CDATA;
 304         features |= SPLITCDATA;
 305         features |= WELLFORMED;
 306         features |= NSDECL;
 307 
 308         if (symbolTable == null) {
 309             symbolTable = new SymbolTable();
 310         }
 311         fSymbolTable = symbolTable;
 312 
 313         fComponents = new ArrayList<>();
 314 
 315         setProperty(SYMBOL_TABLE, fSymbolTable);
 316         fErrorReporter = new XMLErrorReporter();
 317         setProperty(ERROR_REPORTER, fErrorReporter);
 318         addComponent(fErrorReporter);
 319 
 320         setProperty(DTD_VALIDATOR_FACTORY_PROPERTY, DTDDVFactory.getInstance());
 321 
 322         XMLEntityManager manager =  new XMLEntityManager();
 323         setProperty(ENTITY_MANAGER, manager);
 324         addComponent(manager);
 325 
 326         fValidationManager = createValidationManager();
 327         setProperty(VALIDATION_MANAGER, fValidationManager);
 328 
 329         setProperty(SECURITY_MANAGER, new XMLSecurityManager(true));
 330 
 331         setProperty(Constants.XML_SECURITY_PROPERTY_MANAGER,
 332                 new XMLSecurityPropertyManager());
 333 


 347                xmft = (MessageFormatter)(
 348                     ObjectFactory.newInstance("com.sun.org.apache.xerces.internal.impl.xs.XSMessageFormatter", true));
 349             } catch (Exception exception){
 350             }
 351 
 352              if (xmft !=  null) {
 353                  fErrorReporter.putMessageFormatter("http://www.w3.org/TR/xml-schema-1", xmft);
 354              }
 355         }
 356 
 357 
 358         // set locale
 359         try {
 360             setLocale(Locale.getDefault());
 361         }
 362         catch (XNIException e) {
 363             // do nothing
 364             // REVISIT: What is the right thing to do? -Ac
 365         }
 366 
 367         // Initialize Catalog features
 368         for( CatalogFeatures.Feature f : CatalogFeatures.Feature.values()) {
 369             setProperty(f.getPropertyName(), null);
 370         }
 371     } // <init>(SymbolTable)
 372 
 373 
 374     //
 375     // XMLParserConfiguration methods
 376     //
 377 
 378     /**
 379      * Parse an XML document.
 380      * <p>
 381      * The parser can use this method to instruct this configuration
 382      * to begin parsing an XML document from any valid input source
 383      * (a character stream, a byte stream, or a URI).
 384      * <p>
 385      * Parsers may not invoke this method while a parse is in progress.
 386      * Once a parse is complete, the parser may then parse another XML
 387      * document.
 388      * <p>
 389      * This method is synchronous: it will not return until parsing
 390      * has ended.  If a client application wants to terminate


1023             fRecognizedParameters = new DOMStringListImpl(parameters);
1024         }
1025 
1026         return fRecognizedParameters;
1027     }//getParameterNames
1028 
1029     //
1030     // Protected methods
1031     //
1032 
1033     /**
1034      * reset all components before parsing
1035      */
1036     protected void reset() throws XNIException {
1037 
1038         if (fValidationManager != null)
1039             fValidationManager.reset();
1040 
1041         int count = fComponents.size();
1042         for (int i = 0; i < count; i++) {
1043             XMLComponent c = fComponents.get(i);
1044             c.reset(this);
1045         }
1046 
1047     } // reset()
1048 
1049     /**
1050      * Check a property. If the property is known and supported, this method
1051      * simply returns. Otherwise, the appropriate exception is thrown.
1052      *
1053      * @param propertyId The unique identifier (URI) of the property
1054      *                   being set.
1055      * @exception com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException If the
1056      *            requested feature is not known or supported.
1057      */
1058     protected PropertyState checkProperty(String propertyId)
1059         throws XMLConfigurationException {
1060 
1061         // special cases
1062         if (propertyId.startsWith(Constants.SAX_PROPERTY_PREFIX)) {
1063             final int suffixLength = propertyId.length() - Constants.SAX_PROPERTY_PREFIX.length();