1 /* 2 * Copyright (c) 2000, 2015, 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.parsers; 27 28 import javax.xml.validation.Schema; 29 import org.xml.sax.SAXException; 30 import org.xml.sax.SAXNotRecognizedException; 31 import org.xml.sax.SAXNotSupportedException; 32 33 /** 34 * Defines a factory API that enables applications to configure and 35 * obtain a SAX based parser to parse XML documents. 36 * 37 * @author <a href="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</a> 38 * @author <a href="mailto:Neeraj.Bajaj@sun.com">Neeraj Bajaj</a> 39 * 40 * @since 1.4 41 */ 42 public abstract class SAXParserFactory { 43 44 /** 45 * <p>Should Parsers be validating?</p> 46 */ 47 private boolean validating = false; 48 49 /** 50 * <p>Should Parsers be namespace aware?</p> 51 */ 52 private boolean namespaceAware = false; 53 54 /** 55 * <p>Protected constructor to force use of {@link #newInstance()}.</p> 56 */ 57 protected SAXParserFactory () { 58 59 } 60 61 /** 62 * Obtain a new instance of a {@code SAXParserFactory}. This 63 * static method creates a new factory instance 64 * This method uses the following ordered lookup procedure to determine 65 * the {@code SAXParserFactory} implementation class to 66 * load: 67 * <p> 68 * <ul> 69 * <li> 70 * Use the {@code javax.xml.parsers.SAXParserFactory} system 71 * property. 72 * </li> 73 * <li> 74 * <p> 75 * Use the configuration file "jaxp.properties". The file is in standard 76 * {@link java.util.Properties} format and typically located in the 77 * {@code conf} directory of the Java installation. It contains the fully qualified 78 * name of the implementation class with the key being the system property 79 * defined above. 80 * <p> 81 * The jaxp.properties file is read only once by the JAXP implementation 82 * and its values are then cached for future use. If the file does not exist 83 * when the first attempt is made to read from it, no further attempts are 84 * made to check for its existence. It is not possible to change the value 85 * of any property in jaxp.properties after it has been read for the first time. 86 * </li> 87 * <li> 88 * <p> 89 * Use the service-provider loading facility, defined by the 90 * {@link java.util.ServiceLoader} class, to attempt to locate and load an 91 * implementation of the service using the {@linkplain 92 * java.util.ServiceLoader#load(java.lang.Class) default loading mechanism}: 93 * the service-provider loading facility will use the {@linkplain 94 * java.lang.Thread#getContextClassLoader() current thread's context class loader} 95 * to attempt to load the service. If the context class 96 * loader is null, the {@linkplain 97 * ClassLoader#getSystemClassLoader() system class loader} will be used. 98 * </li> 99 * <li> 100 * <p> 101 * Otherwise the system-default implementation is returned. 102 * </li> 103 * </ul> 104 * 105 * <p> 106 * Once an application has obtained a reference to a 107 * {@code SAXParserFactory} it can use the factory to 108 * configure and obtain parser instances. 109 * 110 * 111 * 112 * <h2>Tip for Trouble-shooting</h2> 113 * <p> 114 * Setting the {@code jaxp.debug} system property will cause 115 * this method to print a lot of debug messages 116 * to {@code System.err} about what it is doing and where it is looking at. 117 * 118 * <p> 119 * If you have problems loading {@link SAXParser}s, try: 120 * <pre> 121 * java -Djaxp.debug=1 YourProgram .... 122 * </pre> 123 * 124 * 125 * @return A new instance of a SAXParserFactory. 126 * 127 * @throws FactoryConfigurationError in case of {@linkplain 128 * java.util.ServiceConfigurationError service configuration error} or if 129 * the implementation is not available or cannot be instantiated. 130 */ 131 132 public static SAXParserFactory newInstance() { 133 return FactoryFinder.find( 134 /* The default property name according to the JAXP spec */ 135 SAXParserFactory.class, 136 /* The fallback implementation class name */ 137 "com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl"); 138 } 139 140 /** 141 * <p>Obtain a new instance of a {@code SAXParserFactory} from class name. 142 * This function is useful when there are multiple providers in the classpath. 143 * It gives more control to the application as it can specify which provider 144 * should be loaded.</p> 145 * 146 * <p>Once an application has obtained a reference to a {@code SAXParserFactory} 147 * it can use the factory to configure and obtain parser instances.</p> 148 * 149 * 150 * <h2>Tip for Trouble-shooting</h2> 151 * <p>Setting the {@code jaxp.debug} system property will cause 152 * this method to print a lot of debug messages 153 * to {@code System.err} about what it is doing and where it is looking at.</p> 154 * 155 * <p> 156 * If you have problems, try: 157 * <pre> 158 * java -Djaxp.debug=1 YourProgram .... 159 * </pre> 160 * 161 * @param factoryClassName fully qualified factory class name that provides implementation of {@code javax.xml.parsers.SAXParserFactory}. 162 * 163 * @param classLoader <code>ClassLoader</code> used to load the factory class. If <code>null</code> 164 * current <code>Thread</code>'s context classLoader is used to load the factory class. 165 * 166 * @return New instance of a {@code SAXParserFactory} 167 * 168 * @throws FactoryConfigurationError if <code>factoryClassName</code> is <code>null</code>, or 169 * the factory class cannot be loaded, instantiated. 170 * 171 * @see #newInstance() 172 * 173 * @since 1.6 174 */ 175 public static SAXParserFactory newInstance(String factoryClassName, ClassLoader classLoader){ 176 //do not fallback if given classloader can't find the class, throw exception 177 return FactoryFinder.newInstance(SAXParserFactory.class, 178 factoryClassName, classLoader, false); 179 } 180 181 /** 182 * <p>Creates a new instance of a SAXParser using the currently 183 * configured factory parameters.</p> 184 * 185 * @return A new instance of a SAXParser. 186 * 187 * @throws ParserConfigurationException if a parser cannot 188 * be created which satisfies the requested configuration. 189 * @throws SAXException for SAX errors. 190 */ 191 192 public abstract SAXParser newSAXParser() 193 throws ParserConfigurationException, SAXException; 194 195 196 /** 197 * Specifies that the parser produced by this code will 198 * provide support for XML namespaces. By default the value of this is set 199 * to <code>false</code>. 200 * 201 * @param awareness true if the parser produced by this code will 202 * provide support for XML namespaces; false otherwise. 203 */ 204 205 public void setNamespaceAware(boolean awareness) { 206 this.namespaceAware = awareness; 207 } 208 209 /** 210 * Specifies that the parser produced by this code will 211 * validate documents as they are parsed. By default the value of this is 212 * set to <code>false</code>. 213 * 214 * <p> 215 * Note that "the validation" here means 216 * <a href="http://www.w3.org/TR/REC-xml#proc-types">a validating 217 * parser</a> as defined in the XML recommendation. 218 * In other words, it essentially just controls the DTD validation. 219 * (except the legacy two properties defined in JAXP 1.2.) 220 * </p> 221 * 222 * <p> 223 * To use modern schema languages such as W3C XML Schema or 224 * RELAX NG instead of DTD, you can configure your parser to be 225 * a non-validating parser by leaving the {@link #setValidating(boolean)} 226 * method <code>false</code>, then use the {@link #setSchema(Schema)} 227 * method to associate a schema to a parser. 228 * </p> 229 * 230 * @param validating true if the parser produced by this code will 231 * validate documents as they are parsed; false otherwise. 232 */ 233 234 public void setValidating(boolean validating) { 235 this.validating = validating; 236 } 237 238 /** 239 * Indicates whether or not the factory is configured to produce 240 * parsers which are namespace aware. 241 * 242 * @return true if the factory is configured to produce 243 * parsers which are namespace aware; false otherwise. 244 */ 245 246 public boolean isNamespaceAware() { 247 return namespaceAware; 248 } 249 250 /** 251 * Indicates whether or not the factory is configured to produce 252 * parsers which validate the XML content during parse. 253 * 254 * @return true if the factory is configured to produce parsers which validate 255 * the XML content during parse; false otherwise. 256 */ 257 258 public boolean isValidating() { 259 return validating; 260 } 261 262 /** 263 * 264 * <p>Sets the particular feature in the underlying implementation of 265 * org.xml.sax.XMLReader. 266 * A list of the core features and properties can be found at 267 * <a href="http://www.saxproject.org/">http://www.saxproject.org/</a></p> 268 * 269 * <p>All implementations are required to support the {@link javax.xml.XMLConstants#FEATURE_SECURE_PROCESSING} feature. 270 * When the feature is</p> 271 * <ul> 272 * <li> 273 * <code>true</code>: the implementation will limit XML processing to conform to implementation limits. 274 * Examples include entity expansion limits and XML Schema constructs that would consume large amounts of resources. 275 * If XML processing is limited for security reasons, it will be reported via a call to the registered 276 * {@link org.xml.sax.ErrorHandler#fatalError(SAXParseException exception)}. 277 * See {@link SAXParser} <code>parse</code> methods for handler specification. 278 * </li> 279 * <li> 280 * When the feature is <code>false</code>, the implementation will processing XML according to the XML specifications without 281 * regard to possible implementation limits. 282 * </li> 283 * </ul> 284 * 285 * @param name The name of the feature to be set. 286 * @param value The value of the feature to be set. 287 * 288 * @throws ParserConfigurationException if a parser cannot 289 * be created which satisfies the requested configuration. 290 * @throws SAXNotRecognizedException When the underlying XMLReader does 291 * not recognize the property name. 292 * @throws SAXNotSupportedException When the underlying XMLReader 293 * recognizes the property name but doesn't support the 294 * property. 295 * @throws NullPointerException If the <code>name</code> parameter is null. 296 * 297 * @see org.xml.sax.XMLReader#setFeature 298 */ 299 public abstract void setFeature(String name, boolean value) 300 throws ParserConfigurationException, SAXNotRecognizedException, 301 SAXNotSupportedException; 302 303 /** 304 * 305 * <p>Returns the particular property requested for in the underlying 306 * implementation of org.xml.sax.XMLReader.</p> 307 * 308 * @param name The name of the property to be retrieved. 309 * 310 * @return Value of the requested property. 311 * 312 * @throws ParserConfigurationException if a parser cannot be created which satisfies the requested configuration. 313 * @throws SAXNotRecognizedException When the underlying XMLReader does not recognize the property name. 314 * @throws SAXNotSupportedException When the underlying XMLReader recognizes the property name but doesn't support the property. 315 * 316 * @see org.xml.sax.XMLReader#getProperty 317 */ 318 public abstract boolean getFeature(String name) 319 throws ParserConfigurationException, SAXNotRecognizedException, 320 SAXNotSupportedException; 321 322 323 /** 324 * Gets the {@link Schema} object specified through 325 * the {@link #setSchema(Schema schema)} method. 326 * 327 * 328 * @throws UnsupportedOperationException When implementation does not 329 * override this method 330 * 331 * @return 332 * the {@link Schema} object that was last set through 333 * the {@link #setSchema(Schema)} method, or null 334 * if the method was not invoked since a {@link SAXParserFactory} 335 * is created. 336 * 337 * @since 1.5 338 */ 339 public Schema getSchema() { 340 throw new UnsupportedOperationException( 341 "This parser does not support specification \"" 342 + this.getClass().getPackage().getSpecificationTitle() 343 + "\" version \"" 344 + this.getClass().getPackage().getSpecificationVersion() 345 + "\"" 346 ); 347 } 348 349 /** 350 * <p>Set the {@link Schema} to be used by parsers created 351 * from this factory.</p> 352 * 353 * <p>When a {@link Schema} is non-null, a parser will use a validator 354 * created from it to validate documents before it passes information 355 * down to the application.</p> 356 * 357 * <p>When warnings/errors/fatal errors are found by the validator, the parser must 358 * handle them as if those errors were found by the parser itself. 359 * In other words, if the user-specified {@link org.xml.sax.ErrorHandler} 360 * is set, it must receive those errors, and if not, they must be 361 * treated according to the implementation specific 362 * default error handling rules. 363 * 364 * <p>A validator may modify the SAX event stream (for example by 365 * adding default values that were missing in documents), and a parser 366 * is responsible to make sure that the application will receive 367 * those modified event stream.</p> 368 * 369 * <p>Initially, <code>null</code> is set as the {@link Schema}.</p> 370 * 371 * <p>This processing will take effect even if 372 * the {@link #isValidating()} method returns <code>false</code>. 373 * 374 * <p>It is an error to use 375 * the <code>http://java.sun.com/xml/jaxp/properties/schemaSource</code> 376 * property and/or the <code>http://java.sun.com/xml/jaxp/properties/schemaLanguage</code> 377 * property in conjunction with a non-null {@link Schema} object. 378 * Such configuration will cause a {@link SAXException} 379 * exception when those properties are set on a {@link SAXParser}.</p> 380 * 381 * <h4>Note for implementors</h4> 382 * <p> 383 * A parser must be able to work with any {@link Schema} 384 * implementation. However, parsers and schemas are allowed 385 * to use implementation-specific custom mechanisms 386 * as long as they yield the result described in the specification. 387 * </p> 388 * 389 * @param schema <code>Schema</code> to use, <code>null</code> to remove a schema. 390 * 391 * @throws UnsupportedOperationException When implementation does not 392 * override this method 393 * 394 * @since 1.5 395 */ 396 public void setSchema(Schema schema) { 397 throw new UnsupportedOperationException( 398 "This parser does not support specification \"" 399 + this.getClass().getPackage().getSpecificationTitle() 400 + "\" version \"" 401 + this.getClass().getPackage().getSpecificationVersion() 402 + "\"" 403 ); 404 } 405 406 /** 407 * <p>Set state of XInclude processing.</p> 408 * 409 * <p>If XInclude markup is found in the document instance, should it be 410 * processed as specified in <a href="http://www.w3.org/TR/xinclude/"> 411 * XML Inclusions (XInclude) Version 1.0</a>.</p> 412 * 413 * <p>XInclude processing defaults to <code>false</code>.</p> 414 * 415 * @param state Set XInclude processing to <code>true</code> or 416 * <code>false</code> 417 * 418 * @throws UnsupportedOperationException When implementation does not 419 * override this method 420 * 421 * @since 1.5 422 */ 423 public void setXIncludeAware(final boolean state) { 424 if (state) { 425 throw new UnsupportedOperationException(" setXIncludeAware " + 426 "is not supported on this JAXP" + 427 " implementation or earlier: " + this.getClass()); 428 } 429 } 430 431 /** 432 * <p>Get state of XInclude processing.</p> 433 * 434 * @return current state of XInclude processing 435 * 436 * @throws UnsupportedOperationException When implementation does not 437 * override this method 438 * 439 * @since 1.5 440 */ 441 public boolean isXIncludeAware() { 442 throw new UnsupportedOperationException( 443 "This parser does not support specification \"" 444 + this.getClass().getPackage().getSpecificationTitle() 445 + "\" version \"" 446 + this.getClass().getPackage().getSpecificationVersion() 447 + "\"" 448 ); 449 } 450 }