src/java.xml/share/classes/com/sun/org/apache/xpath/internal/jaxp/XPathFactoryImpl.java

Print this page




   5 /*
   6  * Copyright 1999-2004 The Apache Software Foundation.
   7  *
   8  * Licensed under the Apache License, Version 2.0 (the "License");
   9  * you may not use this file except in compliance with the License.
  10  * 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 // $Id: XPathFactoryImpl.java,v 1.2 2005/08/16 22:41:13 jeffsuttor Exp $
  21 
  22 package com.sun.org.apache.xpath.internal.jaxp;
  23 
  24 import com.sun.org.apache.xalan.internal.XalanConstants;
  25 import com.sun.org.apache.xpath.internal.res.XPATHErrorResources;
  26 import com.sun.org.apache.xalan.internal.res.XSLMessages;
  27 import com.sun.org.apache.xalan.internal.utils.FeatureManager;
  28 import com.sun.org.apache.xalan.internal.utils.FeaturePropertyBase;
  29 
  30 import javax.xml.XMLConstants;
  31 import javax.xml.xpath.XPathFactory;
  32 import javax.xml.xpath.XPathFactoryConfigurationException;
  33 import javax.xml.xpath.XPathFunctionResolver;
  34 import javax.xml.xpath.XPathVariableResolver;

  35 
  36 /**
  37  * The XPathFactory builds XPaths.
  38  *
  39  * @author  Ramesh Mandava
  40  */
  41 public  class XPathFactoryImpl extends XPathFactory {
  42 
  43         /**
  44          * <p>Name of class as a constant to use for debugging.</p>
  45          */
  46         private static final String CLASS_NAME = "XPathFactoryImpl";
  47 
  48         /**
  49          *<p>XPathFunctionResolver for this XPathFactory and created XPaths.</p>
  50          */
  51         private XPathFunctionResolver xPathFunctionResolver = null;
  52 
  53         /**
  54          * <p>XPathVariableResolver for this XPathFactory and created XPaths</p>
  55          */
  56         private XPathVariableResolver xPathVariableResolver = null;
  57 
  58         /**
  59          * <p>State of secure processing feature.</p>
  60          */
  61         private boolean _isNotSecureProcessing = true;
  62         /**
  63          * <p>State of secure mode.</p>
  64          */
  65         private boolean _isSecureMode = false;
  66         /**
  67          * javax.xml.xpath.XPathFactory implementation.
  68          */
  69 
  70         private boolean _useServicesMechanism = true;
  71 
  72         private final FeatureManager _featureManager;
  73 
  74         public XPathFactoryImpl() {
  75             this(true);
  76         }
  77 
  78         public static XPathFactory newXPathFactoryNoServiceLoader() {
  79             return new XPathFactoryImpl(false);
  80         }
  81 
  82         public XPathFactoryImpl(boolean useServicesMechanism) {
  83             _featureManager = new FeatureManager();
  84             if (System.getSecurityManager() != null) {
  85                 _isSecureMode = true;
  86                 _isNotSecureProcessing = false;
  87                 _featureManager.setValue(FeatureManager.Feature.ORACLE_ENABLE_EXTENSION_FUNCTION,
  88                         FeaturePropertyBase.State.FSP, XalanConstants.FEATURE_FALSE);
  89             }

  90             this._useServicesMechanism = useServicesMechanism;
  91         }
  92         /**
  93          * <p>Is specified object model supported by this
  94          * <code>XPathFactory</code>?</p>
  95          *
  96          * @param objectModel Specifies the object model which the returned
  97          * <code>XPathFactory</code> will understand.
  98          *
  99          * @return <code>true</code> if <code>XPathFactory</code> supports
 100          * <code>objectModel</code>, else <code>false</code>.
 101          *
 102          * @throws NullPointerException If <code>objectModel</code> is <code>null</code>.
 103          * @throws IllegalArgumentException If <code>objectModel.length() == 0</code>.
 104          */
 105         public boolean isObjectModelSupported(String objectModel) {
 106 
 107             if (objectModel == null) {
 108                 String fmsg = XSLMessages.createXPATHMessage(
 109                         XPATHErrorResources.ER_OBJECT_MODEL_NULL,


 172 
 173             // feature name cannot be null
 174             if (name == null) {
 175                 String fmsg = XSLMessages.createXPATHMessage(
 176                         XPATHErrorResources.ER_FEATURE_NAME_NULL,
 177                         new Object[] { CLASS_NAME, new Boolean( value) } );
 178                 throw new NullPointerException( fmsg );
 179              }
 180 
 181             // secure processing?
 182             if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
 183                 if ((_isSecureMode) && (!value)) {
 184                     String fmsg = XSLMessages.createXPATHMessage(
 185                             XPATHErrorResources.ER_SECUREPROCESSING_FEATURE,
 186                             new Object[] { name, CLASS_NAME, new Boolean(value) } );
 187                     throw new XPathFactoryConfigurationException( fmsg );
 188                 }
 189 
 190                 _isNotSecureProcessing = !value;
 191                 if (value && _featureManager != null) {
 192                     _featureManager.setValue(FeatureManager.Feature.ORACLE_ENABLE_EXTENSION_FUNCTION,
 193                             FeaturePropertyBase.State.FSP, XalanConstants.FEATURE_FALSE);
 194                 }
 195 
 196                 // all done processing feature
 197                 return;
 198             }
 199             if (name.equals(XalanConstants.ORACLE_FEATURE_SERVICE_MECHANISM)) {
 200                 //in secure mode, let _useServicesMechanism be determined by the constructor
 201                 if (!_isSecureMode)
 202                     _useServicesMechanism = value;
 203                 return;
 204             }
 205 
 206             if (_featureManager != null &&
 207                     _featureManager.setValue(name, FeaturePropertyBase.State.APIPROPERTY, value)) {
 208                 return;
 209             }
 210 
 211             // unknown feature
 212             String fmsg = XSLMessages.createXPATHMessage(
 213                     XPATHErrorResources.ER_FEATURE_UNKNOWN,
 214                     new Object[] { name, CLASS_NAME, new Boolean(value) } );
 215             throw new XPathFactoryConfigurationException( fmsg );
 216         }
 217 
 218         /**
 219          * <p>Get the state of the named feature.</p>
 220          *
 221          * <p>
 222          * Feature names are fully qualified {@link java.net.URI}s.
 223          * Implementations may define their own features.
 224          * An {@link XPathFactoryConfigurationException} is thrown if this
 225          * <code>XPathFactory</code> or the <code>XPath</code>s
 226          * it creates cannot support the feature.
 227          * It is possible for an <code>XPathFactory</code> to expose a feature


 240          */
 241         public boolean getFeature(String name)
 242                 throws XPathFactoryConfigurationException {
 243 
 244             // feature name cannot be null
 245             if (name == null) {
 246                 String fmsg = XSLMessages.createXPATHMessage(
 247                         XPATHErrorResources.ER_GETTING_NULL_FEATURE,
 248                         new Object[] { CLASS_NAME } );
 249                 throw new NullPointerException( fmsg );
 250             }
 251 
 252             // secure processing?
 253             if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
 254                 return !_isNotSecureProcessing;
 255             }
 256             if (name.equals(XalanConstants.ORACLE_FEATURE_SERVICE_MECHANISM)) {
 257                 return _useServicesMechanism;
 258             }
 259 
 260             /** Check to see if the property is managed by the security manager **/
 261             String propertyValue = (_featureManager != null) ?
 262                     _featureManager.getValueAsString(name) : null;
 263             if (propertyValue != null) {
 264                 return _featureManager.isFeatureEnabled(name);
 265             }
 266 
 267             // unknown feature
 268             String fmsg = XSLMessages.createXPATHMessage(
 269                     XPATHErrorResources.ER_GETTING_UNKNOWN_FEATURE,
 270                     new Object[] { name, CLASS_NAME } );
 271 
 272             throw new XPathFactoryConfigurationException( fmsg );
 273         }
 274 
 275         /**
 276          * <p>Establish a default function resolver.</p>
 277          *
 278          * <p>Any <code>XPath</code> objects constructed from this factory will use
 279          * the specified resolver by default.</p>
 280          *
 281          * <p>A <code>NullPointerException</code> is thrown if
 282          * <code>resolver</code> is <code>null</code>.</p>
 283          *
 284          * @param resolver XPath function resolver.




   5 /*
   6  * Copyright 1999-2004 The Apache Software Foundation.
   7  *
   8  * Licensed under the Apache License, Version 2.0 (the "License");
   9  * you may not use this file except in compliance with the License.
  10  * 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 // $Id: XPathFactoryImpl.java,v 1.2 2005/08/16 22:41:13 jeffsuttor Exp $
  21 
  22 package com.sun.org.apache.xpath.internal.jaxp;
  23 
  24 import com.sun.org.apache.xalan.internal.XalanConstants;

  25 import com.sun.org.apache.xalan.internal.res.XSLMessages;
  26 import com.sun.org.apache.xpath.internal.res.XPATHErrorResources;


  27 import javax.xml.XMLConstants;
  28 import javax.xml.xpath.XPathFactory;
  29 import javax.xml.xpath.XPathFactoryConfigurationException;
  30 import javax.xml.xpath.XPathFunctionResolver;
  31 import javax.xml.xpath.XPathVariableResolver;
  32 import jdk.xml.internal.JdkXmlFeatures;
  33 
  34 /**
  35  * The XPathFactory builds XPaths.
  36  *
  37  * @author  Ramesh Mandava
  38  */
  39 public  class XPathFactoryImpl extends XPathFactory {
  40 
  41         /**
  42          * <p>Name of class as a constant to use for debugging.</p>
  43          */
  44         private static final String CLASS_NAME = "XPathFactoryImpl";
  45 
  46         /**
  47          *<p>XPathFunctionResolver for this XPathFactory and created XPaths.</p>
  48          */
  49         private XPathFunctionResolver xPathFunctionResolver = null;
  50 
  51         /**
  52          * <p>XPathVariableResolver for this XPathFactory and created XPaths</p>
  53          */
  54         private XPathVariableResolver xPathVariableResolver = null;
  55 
  56         /**
  57          * <p>State of secure processing feature.</p>
  58          */
  59         private boolean _isNotSecureProcessing = true;
  60         /**
  61          * <p>State of secure mode.</p>
  62          */
  63         private boolean _isSecureMode = false;
  64         /**
  65          * javax.xml.xpath.XPathFactory implementation.
  66          */
  67 
  68         private boolean _useServicesMechanism = true;
  69 
  70         private final JdkXmlFeatures _featureManager;
  71 
  72         public XPathFactoryImpl() {
  73             this(true);
  74         }
  75 
  76         public static XPathFactory newXPathFactoryNoServiceLoader() {
  77             return new XPathFactoryImpl(false);
  78         }
  79 
  80         public XPathFactoryImpl(boolean useServicesMechanism) {

  81             if (System.getSecurityManager() != null) {
  82                 _isSecureMode = true;
  83                 _isNotSecureProcessing = false;


  84             }
  85             _featureManager = new JdkXmlFeatures(!_isNotSecureProcessing);
  86             this._useServicesMechanism = useServicesMechanism;
  87         }
  88         /**
  89          * <p>Is specified object model supported by this
  90          * <code>XPathFactory</code>?</p>
  91          *
  92          * @param objectModel Specifies the object model which the returned
  93          * <code>XPathFactory</code> will understand.
  94          *
  95          * @return <code>true</code> if <code>XPathFactory</code> supports
  96          * <code>objectModel</code>, else <code>false</code>.
  97          *
  98          * @throws NullPointerException If <code>objectModel</code> is <code>null</code>.
  99          * @throws IllegalArgumentException If <code>objectModel.length() == 0</code>.
 100          */
 101         public boolean isObjectModelSupported(String objectModel) {
 102 
 103             if (objectModel == null) {
 104                 String fmsg = XSLMessages.createXPATHMessage(
 105                         XPATHErrorResources.ER_OBJECT_MODEL_NULL,


 168 
 169             // feature name cannot be null
 170             if (name == null) {
 171                 String fmsg = XSLMessages.createXPATHMessage(
 172                         XPATHErrorResources.ER_FEATURE_NAME_NULL,
 173                         new Object[] { CLASS_NAME, new Boolean( value) } );
 174                 throw new NullPointerException( fmsg );
 175              }
 176 
 177             // secure processing?
 178             if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
 179                 if ((_isSecureMode) && (!value)) {
 180                     String fmsg = XSLMessages.createXPATHMessage(
 181                             XPATHErrorResources.ER_SECUREPROCESSING_FEATURE,
 182                             new Object[] { name, CLASS_NAME, new Boolean(value) } );
 183                     throw new XPathFactoryConfigurationException( fmsg );
 184                 }
 185 
 186                 _isNotSecureProcessing = !value;
 187                 if (value && _featureManager != null) {
 188                     _featureManager.setFeature(JdkXmlFeatures.XmlFeature.ENABLE_EXTENSION_FUNCTION,
 189                             JdkXmlFeatures.State.FSP, false);
 190                 }
 191 
 192                 // all done processing feature
 193                 return;
 194             }
 195             if (name.equals(XalanConstants.ORACLE_FEATURE_SERVICE_MECHANISM)) {
 196                 //in secure mode, let _useServicesMechanism be determined by the constructor
 197                 if (!_isSecureMode)
 198                     _useServicesMechanism = value;
 199                 return;
 200             }
 201 
 202             if (_featureManager != null &&
 203                     _featureManager.setFeature(name, JdkXmlFeatures.State.APIPROPERTY, value)) {
 204                 return;
 205             }
 206 
 207             // unknown feature
 208             String fmsg = XSLMessages.createXPATHMessage(
 209                     XPATHErrorResources.ER_FEATURE_UNKNOWN,
 210                     new Object[] { name, CLASS_NAME, new Boolean(value) } );
 211             throw new XPathFactoryConfigurationException( fmsg );
 212         }
 213 
 214         /**
 215          * <p>Get the state of the named feature.</p>
 216          *
 217          * <p>
 218          * Feature names are fully qualified {@link java.net.URI}s.
 219          * Implementations may define their own features.
 220          * An {@link XPathFactoryConfigurationException} is thrown if this
 221          * <code>XPathFactory</code> or the <code>XPath</code>s
 222          * it creates cannot support the feature.
 223          * It is possible for an <code>XPathFactory</code> to expose a feature


 236          */
 237         public boolean getFeature(String name)
 238                 throws XPathFactoryConfigurationException {
 239 
 240             // feature name cannot be null
 241             if (name == null) {
 242                 String fmsg = XSLMessages.createXPATHMessage(
 243                         XPATHErrorResources.ER_GETTING_NULL_FEATURE,
 244                         new Object[] { CLASS_NAME } );
 245                 throw new NullPointerException( fmsg );
 246             }
 247 
 248             // secure processing?
 249             if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
 250                 return !_isNotSecureProcessing;
 251             }
 252             if (name.equals(XalanConstants.ORACLE_FEATURE_SERVICE_MECHANISM)) {
 253                 return _useServicesMechanism;
 254             }
 255 
 256             /** Check to see if the property is managed by the feature manager **/
 257             int index = _featureManager.getIndex(name);
 258             if (index > -1) {
 259                 return _featureManager.getFeature(index);

 260             }
 261 
 262             // unknown feature
 263             String fmsg = XSLMessages.createXPATHMessage(
 264                     XPATHErrorResources.ER_GETTING_UNKNOWN_FEATURE,
 265                     new Object[] { name, CLASS_NAME } );
 266 
 267             throw new XPathFactoryConfigurationException( fmsg );
 268         }
 269 
 270         /**
 271          * <p>Establish a default function resolver.</p>
 272          *
 273          * <p>Any <code>XPath</code> objects constructed from this factory will use
 274          * the specified resolver by default.</p>
 275          *
 276          * <p>A <code>NullPointerException</code> is thrown if
 277          * <code>resolver</code> is <code>null</code>.</p>
 278          *
 279          * @param resolver XPath function resolver.