1 /*
   2  * Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.org.apache.xerces.internal.impl;
  27 
  28 import com.sun.org.apache.xerces.internal.utils.XMLSecurityManager;
  29 import com.sun.org.apache.xerces.internal.utils.XMLSecurityPropertyManager;
  30 import com.sun.xml.internal.stream.StaxEntityResolverWrapper;
  31 import java.util.HashMap;
  32 import javax.xml.XMLConstants;
  33 import javax.xml.catalog.CatalogFeatures;
  34 import javax.xml.stream.XMLInputFactory;
  35 import javax.xml.stream.XMLOutputFactory;
  36 import javax.xml.stream.XMLResolver;
  37 import jdk.xml.internal.JdkXmlUtils;
  38 
  39 /**
  40  *  This class manages different properties related to Stax specification and its implementation.
  41  * This class constructor also takes itself (PropertyManager object) as parameter and initializes the
  42  * object with the property taken from the object passed.
  43  *
  44  * @author  Neeraj Bajaj, neeraj.bajaj@sun.com
  45  * @author K.Venugopal@sun.com
  46  * @author Sunitha Reddy, sunitha.reddy@sun.com
  47  */
  48 
  49 public class PropertyManager {
  50 
  51 
  52     public static final String STAX_NOTATIONS = "javax.xml.stream.notations";
  53     public static final String STAX_ENTITIES = "javax.xml.stream.entities";
  54 
  55     private static final String STRING_INTERNING = "http://xml.org/sax/features/string-interning";
  56 
  57     /** Property identifier: Security manager. */
  58     private static final String SECURITY_MANAGER = Constants.SECURITY_MANAGER;
  59 
  60     /** Property identifier: Security property manager. */
  61     private static final String XML_SECURITY_PROPERTY_MANAGER =
  62             Constants.XML_SECURITY_PROPERTY_MANAGER;
  63 
  64     HashMap<String, Object> supportedProps = new HashMap<>();
  65 
  66     private XMLSecurityManager fSecurityManager;
  67     private XMLSecurityPropertyManager fSecurityPropertyMgr;
  68 
  69     public static final int CONTEXT_READER = 1;
  70     public static final int CONTEXT_WRITER = 2;
  71 
  72     /** Creates a new instance of PropertyManager */
  73     public PropertyManager(int context) {
  74         switch(context){
  75             case CONTEXT_READER:{
  76                 initConfigurableReaderProperties();
  77                 break;
  78             }
  79             case CONTEXT_WRITER:{
  80                 initWriterProps();
  81                 break;
  82             }
  83         }
  84     }
  85 
  86     /**
  87      * Initialize this object with the properties taken from passed PropertyManager object.
  88      */
  89     public PropertyManager(PropertyManager propertyManager){
  90 
  91         HashMap<String, Object> properties = propertyManager.getProperties();
  92         supportedProps.putAll(properties);
  93         fSecurityManager = (XMLSecurityManager)getProperty(SECURITY_MANAGER);
  94         fSecurityPropertyMgr = (XMLSecurityPropertyManager)getProperty(XML_SECURITY_PROPERTY_MANAGER);
  95     }
  96 
  97     private HashMap<String, Object> getProperties(){
  98         return supportedProps ;
  99     }
 100 
 101 
 102     /**
 103      * Important point:
 104      * 1. We are not exposing Xerces namespace property. Application should configure namespace through
 105      * Stax specific property.
 106      *
 107      */
 108     private void initConfigurableReaderProperties(){
 109         //spec default values
 110         supportedProps.put(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
 111         supportedProps.put(XMLInputFactory.IS_VALIDATING, Boolean.FALSE);
 112         supportedProps.put(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE);
 113         supportedProps.put(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.TRUE);
 114         supportedProps.put(XMLInputFactory.IS_COALESCING, Boolean.FALSE);
 115         supportedProps.put(XMLInputFactory.SUPPORT_DTD, Boolean.TRUE);
 116         supportedProps.put(XMLInputFactory.REPORTER, null);
 117         supportedProps.put(XMLInputFactory.RESOLVER, null);
 118         supportedProps.put(XMLInputFactory.ALLOCATOR, null);
 119         supportedProps.put(STAX_NOTATIONS,null );
 120 
 121         //zephyr (implementation) specific properties which can be set by the application.
 122         //interning is always done
 123         supportedProps.put(Constants.SAX_FEATURE_PREFIX + Constants.STRING_INTERNING_FEATURE , true);
 124         //recognizing java encoding names by default
 125         supportedProps.put(Constants.XERCES_FEATURE_PREFIX + Constants.ALLOW_JAVA_ENCODINGS_FEATURE, true) ;
 126         //in stax mode, namespace declarations are not added as attributes
 127         supportedProps.put(Constants.ADD_NAMESPACE_DECL_AS_ATTRIBUTE ,  Boolean.FALSE) ;
 128         supportedProps.put(Constants.READER_IN_DEFINED_STATE, true);
 129         supportedProps.put(Constants.REUSE_INSTANCE, true);
 130         supportedProps.put(Constants.ZEPHYR_PROPERTY_PREFIX + Constants.STAX_REPORT_CDATA_EVENT , false);
 131         supportedProps.put(Constants.ZEPHYR_PROPERTY_PREFIX + Constants.IGNORE_EXTERNAL_DTD, Boolean.FALSE);
 132         supportedProps.put(Constants.XERCES_FEATURE_PREFIX + Constants.WARN_ON_DUPLICATE_ATTDEF_FEATURE, false);
 133         supportedProps.put(Constants.XERCES_FEATURE_PREFIX + Constants.WARN_ON_DUPLICATE_ENTITYDEF_FEATURE, false);
 134         supportedProps.put(Constants.XERCES_FEATURE_PREFIX + Constants.WARN_ON_UNDECLARED_ELEMDEF_FEATURE, false);
 135 
 136         fSecurityManager = new XMLSecurityManager(true);
 137         supportedProps.put(SECURITY_MANAGER, fSecurityManager);
 138         fSecurityPropertyMgr = new XMLSecurityPropertyManager();
 139         supportedProps.put(XML_SECURITY_PROPERTY_MANAGER, fSecurityPropertyMgr);
 140 
 141         // Initialize Catalog features
 142         supportedProps.put(XMLConstants.USE_CATALOG, JdkXmlUtils.USE_CATALOG_DEFAULT);
 143         for( CatalogFeatures.Feature f : CatalogFeatures.Feature.values()) {
 144             supportedProps.put(f.getPropertyName(), null);
 145         }
 146 
 147         supportedProps.put(JdkXmlUtils.CDATA_CHUNK_SIZE, JdkXmlUtils.CDATA_CHUNK_SIZE_DEFAULT);
 148     }
 149 
 150     private void initWriterProps(){
 151         supportedProps.put(XMLOutputFactory.IS_REPAIRING_NAMESPACES , Boolean.FALSE);
 152         //default value of escaping characters is 'true'
 153         supportedProps.put(Constants.ESCAPE_CHARACTERS , Boolean.TRUE);
 154         supportedProps.put(Constants.REUSE_INSTANCE, true);
 155     }
 156 
 157     /**
 158      * public void reset(){
 159      * supportedProps.clear() ;
 160      * }
 161      */
 162     public boolean containsProperty(String property){
 163         return supportedProps.containsKey(property) ||
 164                 (fSecurityManager != null && fSecurityManager.getIndex(property) > -1) ||
 165                 (fSecurityPropertyMgr!=null && fSecurityPropertyMgr.getIndex(property) > -1) ;
 166     }
 167 
 168     public Object getProperty(String property){
 169         return supportedProps.get(property);
 170     }
 171 
 172     public void setProperty(String property, Object value){
 173         String equivalentProperty = null ;
 174         if(property.equals(XMLInputFactory.IS_NAMESPACE_AWARE)){
 175             equivalentProperty = Constants.XERCES_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE ;
 176         }
 177         else if(property.equals(XMLInputFactory.IS_VALIDATING)){
 178             if( (value instanceof Boolean) && ((Boolean)value).booleanValue()){
 179                 throw new java.lang.IllegalArgumentException("true value of isValidating not supported") ;
 180             }
 181         }
 182         else if(property.equals(STRING_INTERNING)){
 183             if( (value instanceof Boolean) && !((Boolean)value).booleanValue()){
 184                 throw new java.lang.IllegalArgumentException("false value of " + STRING_INTERNING + "feature is not supported") ;
 185             }
 186         }
 187         else if(property.equals(XMLInputFactory.RESOLVER)){
 188             //add internal stax property
 189             supportedProps.put( Constants.XERCES_PROPERTY_PREFIX + Constants.STAX_ENTITY_RESOLVER_PROPERTY , new StaxEntityResolverWrapper((XMLResolver)value)) ;
 190         }
 191 
 192         /**
 193          * It's possible for users to set a security manager through the interface.
 194          * If it's the old SecurityManager, convert it to the new XMLSecurityManager
 195          */
 196         if (property.equals(Constants.SECURITY_MANAGER)) {
 197             fSecurityManager = XMLSecurityManager.convert(value, fSecurityManager);
 198             supportedProps.put(Constants.SECURITY_MANAGER, fSecurityManager);
 199             return;
 200         }
 201         if (property.equals(Constants.XML_SECURITY_PROPERTY_MANAGER)) {
 202             if (value == null) {
 203                 fSecurityPropertyMgr = new XMLSecurityPropertyManager();
 204             } else {
 205                 fSecurityPropertyMgr = (XMLSecurityPropertyManager)value;
 206             }
 207             supportedProps.put(Constants.XML_SECURITY_PROPERTY_MANAGER, fSecurityPropertyMgr);
 208             return;
 209         }
 210 
 211         //check if the property is managed by security manager
 212         if (fSecurityManager == null ||
 213                 !fSecurityManager.setLimit(property, XMLSecurityManager.State.APIPROPERTY, value)) {
 214             //check if the property is managed by security property manager
 215             if (fSecurityPropertyMgr == null ||
 216                     !fSecurityPropertyMgr.setValue(property, XMLSecurityPropertyManager.State.APIPROPERTY, value)) {
 217                 //fall back to the existing property manager
 218                 supportedProps.put(property, value);
 219             }
 220         }
 221 
 222         if(equivalentProperty != null){
 223             supportedProps.put(equivalentProperty, value ) ;
 224         }
 225     }
 226 
 227     public String toString(){
 228         return supportedProps.toString();
 229     }
 230 
 231 }//PropertyManager