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