1 /* 2 * Copyright (c) 2003, 2016, 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 javax.xml.xpath; 27 28 import com.sun.org.apache.xpath.internal.jaxp.XPathFactoryImpl; 29 import jdk.xml.internal.SecuritySupport; 30 31 /** 32 * <p>An {@code XPathFactory} instance can be used to create 33 * {@link javax.xml.xpath.XPath} objects.</p> 34 * 35 *<p>See {@link #newInstance(String uri)} for lookup mechanism.</p> 36 * 37 * <p>The {@link XPathFactory} class is not thread-safe. In other words, 38 * it is the application's responsibility to ensure that at most 39 * one thread is using a {@link XPathFactory} object at any 40 * given moment. Implementations are encouraged to mark methods 41 * as <code>synchronized</code> to protect themselves from broken clients. 42 * 43 * <p>{@link XPathFactory} is not re-entrant. While one of the 44 * <code>newInstance</code> methods is being invoked, applications 45 * may not attempt to recursively invoke a <code>newInstance</code> method, 46 * even from the same thread. 47 * 48 * @author <a href="mailto:Norman.Walsh@Sun.com">Norman Walsh</a> 49 * @author <a href="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</a> 50 * 51 * @since 1.5 52 */ 53 public abstract class XPathFactory { 54 55 56 /** 57 * <p>The default property name according to the JAXP spec.</p> 58 */ 59 public static final String DEFAULT_PROPERTY_NAME = "javax.xml.xpath.XPathFactory"; 60 61 /** 62 * <p>Default Object Model URI.</p> 63 */ 64 public static final String DEFAULT_OBJECT_MODEL_URI = "http://java.sun.com/jaxp/xpath/dom"; 65 66 /** 67 * <p>Protected constructor as {@link #newInstance()} or {@link #newInstance(String uri)} 68 * or {@link #newInstance(String uri, String factoryClassName, ClassLoader classLoader)} 69 * should be used to create a new instance of an {@code XPathFactory}.</p> 70 */ 71 protected XPathFactory() { 72 } 73 74 /** 75 * Creates a new instance of the {@code XPathFactory} builtin 76 * system-default implementation. 77 * 78 * @implSpec The {@code XPathFactory} builtin 79 * system-default implementation is only required to support the 80 * {@link #DEFAULT_OBJECT_MODEL_URI default object model}, the 81 * {@linkplain org.w3c.dom W3C DOM}, but may support additional 82 * object models. 83 * 84 * @return A new instance of the {@code XPathFactory} builtin 85 * system-default implementation. 86 * 87 * @since 9 88 */ 89 public static XPathFactory newDefaultInstance() { 90 return XPathFactoryImpl.newXPathFactoryNoServiceLoader(); 91 } 92 93 /** 94 * <p>Get a new {@code XPathFactory} instance using the default object model, 95 * {@link #DEFAULT_OBJECT_MODEL_URI}, 96 * the W3C DOM.</p> 97 * 98 * <p>This method is functionally equivalent to:</p> 99 * <pre> 100 * newInstance(DEFAULT_OBJECT_MODEL_URI) 101 * </pre> 102 * 103 * <p>Since the implementation for the W3C DOM is always available, this method will never fail.</p> 104 * 105 * @return Instance of an {@code XPathFactory}. 106 * 107 * @throws RuntimeException When there is a failure in creating an 108 * {@code XPathFactory} for the default object model. 109 */ 110 public static XPathFactory newInstance() { 111 112 try { 113 return newInstance(DEFAULT_OBJECT_MODEL_URI); 114 } catch (XPathFactoryConfigurationException e) { 115 throw new RuntimeException( 116 "XPathFactory#newInstance() failed to create an XPathFactory for the default object model: " 117 + DEFAULT_OBJECT_MODEL_URI 118 + " with the XPathFactoryConfigurationException: " 119 + e.getMessage(), e 120 ); 121 } 122 } 123 124 /** 125 * <p>Get a new {@code XPathFactory} instance using the specified object model.</p> 126 * 127 * <p>To find a {@code XPathFactory} object, 128 * this method looks the following places in the following order where "the class loader" refers to the context class loader:</p> 129 * <ol> 130 * <li> 131 * <p> 132 * If the system property {@link #DEFAULT_PROPERTY_NAME} + ":uri" is present, 133 * where uri is the parameter to this method, then its value is read as a class name. 134 * The method will try to create a new instance of this class by using the class loader, 135 * and returns it if it is successfully created. 136 * </li> 137 * <li> 138 * <p> 139 * Use the configuration file "jaxp.properties". The file is in standard 140 * {@link java.util.Properties} format and typically located in the 141 * conf directory of the Java installation. It contains the fully qualified 142 * name of the implementation class with the key being the system property 143 * defined above. 144 * <p> 145 * The jaxp.properties file is read only once by the JAXP implementation 146 * and its values are then cached for future use. If the file does not exist 147 * when the first attempt is made to read from it, no further attempts are 148 * made to check for its existence. It is not possible to change the value 149 * of any property in jaxp.properties after it has been read for the first time. 150 * </li> 151 * <li> 152 * <p> 153 * Use the service-provider loading facility, defined by the 154 * {@link java.util.ServiceLoader} class, to attempt to locate and load an 155 * implementation of the service using the {@linkplain 156 * java.util.ServiceLoader#load(java.lang.Class) default loading mechanism}: 157 * the service-provider loading facility will use the {@linkplain 158 * java.lang.Thread#getContextClassLoader() current thread's context class loader} 159 * to attempt to load the service. If the context class 160 * loader is null, the {@linkplain 161 * ClassLoader#getSystemClassLoader() system class loader} will be used. 162 * <br> 163 * Each potential service provider is required to implement the method 164 * {@link #isObjectModelSupported(String objectModel)}. 165 * The first service provider found that supports the specified object 166 * model is returned. 167 * <br> 168 * In case of {@link java.util.ServiceConfigurationError} an 169 * {@link XPathFactoryConfigurationException} will be thrown. 170 * </li> 171 * <li> 172 * <p> 173 * Platform default {@code XPathFactory} is located in a platform 174 * specific way. 175 * There must be a {@linkplain #newDefaultInstance() platform default} 176 * {@code XPathFactory} for the W3C DOM, i.e. 177 * {@link #DEFAULT_OBJECT_MODEL_URI}. 178 * </li> 179 * </ol> 180 * <p>If everything fails, an {@code XPathFactoryConfigurationException} will be thrown. 181 * 182 * <p>Tip for Trouble-shooting: 183 * <p>See {@link java.util.Properties#load(java.io.InputStream)} for exactly how a property file is parsed. 184 * In particular, colons ':' need to be escaped in a property file, so make sure the URIs are properly escaped in it. 185 * For example: 186 * <pre> 187 * http\://java.sun.com/jaxp/xpath/dom=org.acme.DomXPathFactory 188 * </pre> 189 * 190 * @param uri Identifies the underlying object model. 191 * The specification only defines the URI {@link #DEFAULT_OBJECT_MODEL_URI}, 192 * <code>http://java.sun.com/jaxp/xpath/dom</code> for the W3C DOM, 193 * the org.w3c.dom package, and implementations are free to introduce other URIs for other object models. 194 * 195 * @return Instance of an {@code XPathFactory}. 196 * 197 * @throws XPathFactoryConfigurationException If the specified object model 198 * is unavailable, or if there is a configuration error. 199 * @throws NullPointerException If <code>uri</code> is <code>null</code>. 200 * @throws IllegalArgumentException If <code>uri</code> is <code>null</code> 201 * or <code>uri.length() == 0</code>. 202 */ 203 public static XPathFactory newInstance(final String uri) 204 throws XPathFactoryConfigurationException { 205 206 if (uri == null) { 207 throw new NullPointerException( 208 "XPathFactory#newInstance(String uri) cannot be called with uri == null"); 209 } 210 211 if (uri.length() == 0) { 212 throw new IllegalArgumentException( 213 "XPathFactory#newInstance(String uri) cannot be called with uri == \"\""); 214 } 215 216 ClassLoader classLoader = SecuritySupport.getContextClassLoader(); 217 218 if (classLoader == null) { 219 //use the current class loader 220 classLoader = XPathFactory.class.getClassLoader(); 221 } 222 223 XPathFactory xpathFactory = new XPathFactoryFinder(classLoader).newFactory(uri); 224 225 if (xpathFactory == null) { 226 throw new XPathFactoryConfigurationException( 227 "No XPathFactory implementation found for the object model: " 228 + uri); 229 } 230 231 return xpathFactory; 232 } 233 234 /** 235 * <p>Obtain a new instance of a {@code XPathFactory} from a factory class name. {@code XPathFactory} 236 * is returned if specified factory class supports the specified object model. 237 * This function is useful when there are multiple providers in the classpath. 238 * It gives more control to the application as it can specify which provider 239 * should be loaded.</p> 240 * 241 * 242 * <h2>Tip for Trouble-shooting</h2> 243 * <p>Setting the <code>jaxp.debug</code> system property will cause 244 * this method to print a lot of debug messages 245 * to <code>System.err</code> about what it is doing and where it is looking at.</p> 246 * 247 * <p> If you have problems try:</p> 248 * <pre> 249 * java -Djaxp.debug=1 YourProgram .... 250 * </pre> 251 * 252 * @param uri Identifies the underlying object model. The specification only defines the URI 253 * {@link #DEFAULT_OBJECT_MODEL_URI},<code>http://java.sun.com/jaxp/xpath/dom</code> 254 * for the W3C DOM, the org.w3c.dom package, and implementations are free to introduce 255 * other URIs for other object models. 256 * 257 * @param factoryClassName fully qualified factory class name that provides implementation of <code>javax.xml.xpath.XPathFactory</code>. 258 * 259 * @param classLoader <code>ClassLoader</code> used to load the factory class. If <code>null</code> 260 * current <code>Thread</code>'s context classLoader is used to load the factory class. 261 * 262 * 263 * @return New instance of a {@code XPathFactory} 264 * 265 * @throws XPathFactoryConfigurationException 266 * if <code>factoryClassName</code> is <code>null</code>, or 267 * the factory class cannot be loaded, instantiated 268 * or the factory class does not support the object model specified 269 * in the <code>uri</code> parameter. 270 * 271 * @throws NullPointerException If <code>uri</code> is <code>null</code>. 272 * @throws IllegalArgumentException If <code>uri</code> is <code>null</code> 273 * or <code>uri.length() == 0</code>. 274 * 275 * @see #newInstance() 276 * @see #newInstance(String uri) 277 * 278 * @since 1.6 279 */ 280 public static XPathFactory newInstance(String uri, String factoryClassName, ClassLoader classLoader) 281 throws XPathFactoryConfigurationException{ 282 ClassLoader cl = classLoader; 283 284 if (uri == null) { 285 throw new NullPointerException( 286 "XPathFactory#newInstance(String uri) cannot be called with uri == null"); 287 } 288 289 if (uri.length() == 0) { 290 throw new IllegalArgumentException( 291 "XPathFactory#newInstance(String uri) cannot be called with uri == \"\""); 292 } 293 294 if (cl == null) { 295 cl = SecuritySupport.getContextClassLoader(); 296 } 297 298 XPathFactory f = new XPathFactoryFinder(cl).createInstance(factoryClassName); 299 300 if (f == null) { 301 throw new XPathFactoryConfigurationException( 302 "No XPathFactory implementation found for the object model: " 303 + uri); 304 } 305 //if this factory supports the given schemalanguage return this factory else thrown exception 306 if (f.isObjectModelSupported(uri)) { 307 return f; 308 } else { 309 throw new XPathFactoryConfigurationException("Factory " 310 + factoryClassName + " doesn't support given " + uri 311 + " object model"); 312 } 313 314 } 315 316 /** 317 * <p>Is specified object model supported by this {@code XPathFactory}?</p> 318 * 319 * @param objectModel Specifies the object model which the returned {@code XPathFactory} will understand. 320 * 321 * @return <code>true</code> if {@code XPathFactory} supports <code>objectModel</code>, else <code>false</code>. 322 * 323 * @throws NullPointerException If <code>objectModel</code> is <code>null</code>. 324 * @throws IllegalArgumentException If <code>objectModel.length() == 0</code>. 325 */ 326 public abstract boolean isObjectModelSupported(String objectModel); 327 328 /** 329 * <p>Set a feature for this {@code XPathFactory} and 330 * <code>XPath</code>s created by this factory.</p> 331 * 332 * <p> 333 * Feature names are fully qualified {@link java.net.URI}s. 334 * Implementations may define their own features. 335 * An {@link XPathFactoryConfigurationException} is thrown if this 336 * {@code XPathFactory} or the <code>XPath</code>s 337 * it creates cannot support the feature. 338 * It is possible for an {@code XPathFactory} to expose a feature value 339 * but be unable to change its state. 340 * </p> 341 * 342 * <p> 343 * All implementations are required to support the {@link javax.xml.XMLConstants#FEATURE_SECURE_PROCESSING} feature. 344 * When the feature is <code>true</code>, any reference to an external function is an error. 345 * Under these conditions, the implementation must not call the {@link XPathFunctionResolver} 346 * and must throw an {@link XPathFunctionException}. 347 * </p> 348 * 349 * @param name Feature name. 350 * @param value Is feature state <code>true</code> or <code>false</code>. 351 * 352 * @throws XPathFactoryConfigurationException if this {@code XPathFactory} or the <code>XPath</code>s 353 * it creates cannot support this feature. 354 * @throws NullPointerException if <code>name</code> is <code>null</code>. 355 */ 356 public abstract void setFeature(String name, boolean value) 357 throws XPathFactoryConfigurationException; 358 359 /** 360 * <p>Get the state of the named feature.</p> 361 * 362 * <p> 363 * Feature names are fully qualified {@link java.net.URI}s. 364 * Implementations may define their own features. 365 * An {@link XPathFactoryConfigurationException} is thrown if this 366 * {@code XPathFactory} or the <code>XPath</code>s 367 * it creates cannot support the feature. 368 * It is possible for an {@code XPathFactory} to expose a feature value 369 * but be unable to change its state. 370 * </p> 371 * 372 * @param name Feature name. 373 * 374 * @return State of the named feature. 375 * 376 * @throws XPathFactoryConfigurationException if this 377 * {@code XPathFactory} or the <code>XPath</code>s 378 * it creates cannot support this feature. 379 * @throws NullPointerException if <code>name</code> is <code>null</code>. 380 */ 381 public abstract boolean getFeature(String name) 382 throws XPathFactoryConfigurationException; 383 384 /** 385 * <p>Establish a default variable resolver.</p> 386 * 387 * <p>Any <code>XPath</code> objects constructed from this factory will use 388 * the specified resolver by default.</p> 389 * 390 * <p>A <code>NullPointerException</code> is thrown if <code>resolver</code> 391 * is <code>null</code>.</p> 392 * 393 * @param resolver Variable resolver. 394 * 395 * @throws NullPointerException If <code>resolver</code> is 396 * <code>null</code>. 397 */ 398 public abstract void setXPathVariableResolver(XPathVariableResolver resolver); 399 400 /** 401 * <p>Establish a default function resolver.</p> 402 * 403 * <p>Any <code>XPath</code> objects constructed from this factory will 404 * use the specified resolver by default.</p> 405 * 406 * <p>A <code>NullPointerException</code> is thrown if 407 * <code>resolver</code> is <code>null</code>.</p> 408 * 409 * @param resolver XPath function resolver. 410 * 411 * @throws NullPointerException If <code>resolver</code> is 412 * <code>null</code>. 413 */ 414 public abstract void setXPathFunctionResolver(XPathFunctionResolver resolver); 415 416 /** 417 * <p>Return a new <code>XPath</code> using the underlying object 418 * model determined when the {@code XPathFactory} was instantiated.</p> 419 * 420 * @return New instance of an <code>XPath</code>. 421 */ 422 public abstract XPath newXPath(); 423 }