1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 // $Id: XPathFactoryImpl.java 468655 2006-10-28 07:12:06Z minchau $ 19 20 package org.apache.xpath.jaxp; 21 22 import org.apache.xpath.res.XPATHErrorResources; 23 import org.apache.xalan.res.XSLMessages; 24 25 import javax.xml.XMLConstants; 26 import javax.xml.xpath.XPathFactory; 27 import javax.xml.xpath.XPathFactoryConfigurationException; 28 import javax.xml.xpath.XPathFunctionResolver; 29 import javax.xml.xpath.XPathVariableResolver; 30 31 /** 32 * The XPathFactory builds XPaths. 33 * 34 * @version $Revision: 468655 $ 35 * @author Ramesh Mandava 36 */ 37 public class XPathFactoryImpl extends XPathFactory { 38 39 /** 40 * <p>Name of class as a constant to use for debugging.</p> 41 */ 42 private static final String CLASS_NAME = "XPathFactoryImpl"; 43 44 /** 45 *<p>XPathFunctionResolver for this XPathFactory and created XPaths.</p> 46 */ 47 private XPathFunctionResolver xPathFunctionResolver = null; 48 49 /** 50 * <p>XPathVariableResolver for this XPathFactory and created XPaths</p> 51 */ 52 private XPathVariableResolver xPathVariableResolver = null; 53 54 /** 55 * <p>State of secure processing feature.</p> 56 */ 57 private boolean featureSecureProcessing = false; 58 59 /** 60 * <p>Is specified object model supported by this 61 * <code>XPathFactory</code>?</p> 62 * 63 * @param objectModel Specifies the object model which the returned 64 * <code>XPathFactory</code> will understand. 65 * 66 * @return <code>true</code> if <code>XPathFactory</code> supports 67 * <code>objectModel</code>, else <code>false</code>. 68 * 69 * @throws NullPointerException If <code>objectModel</code> is <code>null</code>. 70 * @throws IllegalArgumentException If <code>objectModel.length() == 0</code>. 71 */ isObjectModelSupported(String objectModel)72 public boolean isObjectModelSupported(String objectModel) { 73 74 if (objectModel == null) { 75 String fmsg = XSLMessages.createXPATHMessage( 76 XPATHErrorResources.ER_OBJECT_MODEL_NULL, 77 new Object[] { this.getClass().getName() } ); 78 79 throw new NullPointerException( fmsg ); 80 } 81 82 if (objectModel.length() == 0) { 83 String fmsg = XSLMessages.createXPATHMessage( 84 XPATHErrorResources.ER_OBJECT_MODEL_EMPTY, 85 new Object[] { this.getClass().getName() } ); 86 throw new IllegalArgumentException( fmsg ); 87 } 88 89 // know how to support default object model, W3C DOM 90 if (objectModel.equals(XPathFactory.DEFAULT_OBJECT_MODEL_URI)) { 91 return true; 92 } 93 94 // don't know how to support anything else 95 return false; 96 } 97 98 /** 99 * <p>Returns a new <code>XPath</code> object using the underlying 100 * object model determined when the factory was instantiated.</p> 101 * 102 * @return New <code>XPath</code> 103 */ newXPath()104 public javax.xml.xpath.XPath newXPath() { 105 return new org.apache.xpath.jaxp.XPathImpl( 106 xPathVariableResolver, xPathFunctionResolver, 107 featureSecureProcessing ); 108 } 109 110 /** 111 * <p>Set a feature for this <code>XPathFactory</code> and 112 * <code>XPath</code>s created by this factory.</p> 113 * 114 * <p> 115 * Feature names are fully qualified {@link java.net.URI}s. 116 * Implementations may define their own features. 117 * An {@link XPathFactoryConfigurationException} is thrown if this 118 * <code>XPathFactory</code> or the <code>XPath</code>s 119 * it creates cannot support the feature. 120 * It is possible for an <code>XPathFactory</code> to expose a feature 121 * value but be unable to change its state. 122 * </p> 123 * 124 * <p>See {@link javax.xml.xpath.XPathFactory} for full documentation 125 * of specific features.</p> 126 * 127 * @param name Feature name. 128 * @param value Is feature state <code>true</code> or <code>false</code>. 129 * 130 * @throws XPathFactoryConfigurationException if this 131 * <code>XPathFactory</code> or the <code>XPath</code>s 132 * it creates cannot support this feature. 133 * @throws NullPointerException if <code>name</code> is 134 * <code>null</code>. 135 */ setFeature(String name, boolean value)136 public void setFeature(String name, boolean value) 137 throws XPathFactoryConfigurationException { 138 139 // feature name cannot be null 140 if (name == null) { 141 String fmsg = XSLMessages.createXPATHMessage( 142 XPATHErrorResources.ER_FEATURE_NAME_NULL, 143 new Object[] { CLASS_NAME, new Boolean( value) } ); 144 throw new NullPointerException( fmsg ); 145 } 146 147 // secure processing? 148 if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) { 149 150 featureSecureProcessing = value; 151 152 // all done processing feature 153 return; 154 } 155 156 // unknown feature 157 String fmsg = XSLMessages.createXPATHMessage( 158 XPATHErrorResources.ER_FEATURE_UNKNOWN, 159 new Object[] { name, CLASS_NAME, new Boolean(value) } ); 160 throw new XPathFactoryConfigurationException( fmsg ); 161 } 162 163 /** 164 * <p>Get the state of the named feature.</p> 165 * 166 * <p> 167 * Feature names are fully qualified {@link java.net.URI}s. 168 * Implementations may define their own features. 169 * An {@link XPathFactoryConfigurationException} is thrown if this 170 * <code>XPathFactory</code> or the <code>XPath</code>s 171 * it creates cannot support the feature. 172 * It is possible for an <code>XPathFactory</code> to expose a feature 173 * value but be unable to change its state. 174 * </p> 175 * 176 * @param name Feature name. 177 * 178 * @return State of the named feature. 179 * 180 * @throws XPathFactoryConfigurationException if this 181 * <code>XPathFactory</code> or the <code>XPath</code>s 182 * it creates cannot support this feature. 183 * @throws NullPointerException if <code>name</code> is 184 * <code>null</code>. 185 */ getFeature(String name)186 public boolean getFeature(String name) 187 throws XPathFactoryConfigurationException { 188 189 // feature name cannot be null 190 if (name == null) { 191 String fmsg = XSLMessages.createXPATHMessage( 192 XPATHErrorResources.ER_GETTING_NULL_FEATURE, 193 new Object[] { CLASS_NAME } ); 194 throw new NullPointerException( fmsg ); 195 } 196 197 // secure processing? 198 if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) { 199 return featureSecureProcessing; 200 } 201 202 // unknown feature 203 String fmsg = XSLMessages.createXPATHMessage( 204 XPATHErrorResources.ER_GETTING_UNKNOWN_FEATURE, 205 new Object[] { name, CLASS_NAME } ); 206 207 throw new XPathFactoryConfigurationException( fmsg ); 208 } 209 210 /** 211 * <p>Establish a default function resolver.</p> 212 * 213 * <p>Any <code>XPath</code> objects constructed from this factory will use 214 * the specified resolver by default.</p> 215 * 216 * <p>A <code>NullPointerException</code> is thrown if 217 * <code>resolver</code> is <code>null</code>.</p> 218 * 219 * @param resolver XPath function resolver. 220 * 221 * @throws NullPointerException If <code>resolver</code> is 222 * <code>null</code>. 223 */ setXPathFunctionResolver(XPathFunctionResolver resolver)224 public void setXPathFunctionResolver(XPathFunctionResolver resolver) { 225 226 // resolver cannot be null 227 if (resolver == null) { 228 String fmsg = XSLMessages.createXPATHMessage( 229 XPATHErrorResources.ER_NULL_XPATH_FUNCTION_RESOLVER, 230 new Object[] { CLASS_NAME } ); 231 throw new NullPointerException( fmsg ); 232 } 233 234 xPathFunctionResolver = resolver; 235 } 236 237 /** 238 * <p>Establish a default variable resolver.</p> 239 * 240 * <p>Any <code>XPath</code> objects constructed from this factory will use 241 * the specified resolver by default.</p> 242 * 243 * <p>A <code>NullPointerException</code> is thrown if <code>resolver</code> is <code>null</code>.</p> 244 * 245 * @param resolver Variable resolver. 246 * 247 * @throws NullPointerException If <code>resolver</code> is 248 * <code>null</code>. 249 */ setXPathVariableResolver(XPathVariableResolver resolver)250 public void setXPathVariableResolver(XPathVariableResolver resolver) { 251 252 // resolver cannot be null 253 if (resolver == null) { 254 String fmsg = XSLMessages.createXPATHMessage( 255 XPATHErrorResources.ER_NULL_XPATH_VARIABLE_RESOLVER, 256 new Object[] { CLASS_NAME } ); 257 throw new NullPointerException( fmsg ); 258 } 259 260 xPathVariableResolver = resolver; 261 } 262 } 263 264 265 266