1 /*
   2  * Copyright (c) 2011, 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.xalan.internal.utils;
  27 
  28 
  29 import com.sun.org.apache.xalan.internal.XalanConstants;
  30 
  31 /**
  32  * This class manages security related properties
  33  *
  34  */
  35 public final class FeatureManager extends FeaturePropertyBase {
  36 
  37     /**
  38      * States of the settings of a property, in the order: default value, value
  39      * set by FEATURE_SECURE_PROCESSING, jaxp.properties file, jaxp system
  40      * properties, and jaxp api properties
  41      */
  42     public static enum State {
  43         //this order reflects the overriding order
  44         DEFAULT, FSP, JAXPDOTPROPERTIES, SYSTEMPROPERTY, APIPROPERTY
  45     }
  46 
  47     /**
  48      * Xalan Features
  49      */
  50     public static enum Feature {
  51         ORACLE_ENABLE_EXTENSION_FUNCTION(XalanConstants.ORACLE_ENABLE_EXTENSION_FUNCTION,
  52                 "true");
  53 
  54         final String name;
  55         final String defaultValue;
  56 
  57         Feature(String name, String value) {
  58             this.name = name;
  59             this.defaultValue = value;
  60         }
  61 
  62         public boolean equalsName(String propertyName) {
  63             return (propertyName == null) ? false : name.equals(propertyName);
  64         }
  65 
  66         String defaultValue() {
  67             return defaultValue;
  68         }
  69     }
  70 
  71     /**
  72      * Default constructor. Establishes default values
  73      */
  74     public FeatureManager() {
  75         values = new String[Feature.values().length];
  76         for (Feature feature : Feature.values()) {
  77             values[feature.ordinal()] = feature.defaultValue();
  78         }
  79         //read system properties or jaxp.properties
  80         readSystemProperties();
  81     }
  82 
  83 
  84     /**
  85      * Check if the feature is enabled
  86      * @param feature name of the feature
  87      * @return true if enabled, false otherwise
  88      */
  89     public boolean isFeatureEnabled(Feature feature) {
  90         return Boolean.parseBoolean(values[feature.ordinal()]);
  91     }
  92 
  93     /**
  94      * Check if the feature is enabled
  95      * @param propertyName name of the feature
  96      * @return true if enabled, false otherwise
  97      */
  98     public boolean isFeatureEnabled(String propertyName) {
  99         return Boolean.parseBoolean(values[getIndex(propertyName)]);
 100     }
 101 
 102     /**
 103      * Get the index by property name
 104      * @param propertyName property name
 105      * @return the index of the property if found; return -1 if not
 106      */
 107     public int getIndex(String propertyName){
 108         for (Feature feature : Feature.values()) {
 109             if (feature.equalsName(propertyName)) {
 110                 return feature.ordinal();
 111             }
 112         }
 113         return -1;
 114     }
 115 
 116     /**
 117      * Read from system properties, or those in jaxp.properties
 118      */
 119     private void readSystemProperties() {
 120         getSystemProperty(Feature.ORACLE_ENABLE_EXTENSION_FUNCTION,
 121                 XalanConstants.SP_ORACLE_ENABLE_EXTENSION_FUNCTION);
 122     }
 123 
 124 }