1 /* 2 * Copyright (c) 2009, 2017, 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.stream; 27 28 import com.sun.xml.internal.stream.XMLInputFactoryImpl; 29 import javax.xml.stream.util.XMLEventAllocator; 30 import javax.xml.transform.Source; 31 32 /** 33 * Defines an abstract implementation of a factory for getting streams. 34 * 35 * The following table defines the standard properties of this specification. 36 * Each property varies in the level of support required by each implementation. 37 * The level of support required is described in the 'Required' column. 38 * 39 * <table class="striped"> 40 * <caption>Configuration Parameters</caption> 41 * <thead> 42 * <tr> 43 * <th>Property Name</th> 44 * <th>Behavior</th> 45 * <th>Return type</th> 46 * <th>Default Value</th> 47 * <th>Required</th> 48 * </tr> 49 * </thead> 50 * <tbody> 51 * <tr><td>javax.xml.stream.isValidating</td><td>Turns on/off implementation specific DTD validation</td><td>Boolean</td><td>False</td><td>No</td></tr> 52 * <tr><td>javax.xml.stream.isNamespaceAware</td><td>Turns on/off namespace processing for XML 1.0 support</td><td>Boolean</td><td>True</td><td>True (required) / False (optional)</td></tr> 53 * <tr><td>javax.xml.stream.isCoalescing</td><td>Requires the processor to coalesce adjacent character data</td><td>Boolean</td><td>False</td><td>Yes</td></tr> 54 * <tr><td>javax.xml.stream.isReplacingEntityReferences</td><td>replace internal entity references with their replacement text and report them as characters</td><td>Boolean</td><td>True</td><td>Yes</td></tr> 55 *<tr><td>javax.xml.stream.isSupportingExternalEntities</td><td>Resolve external parsed entities</td><td>Boolean</td><td>Unspecified</td><td>Yes</td></tr> 56 *<tr><td>javax.xml.stream.supportDTD</td><td>Use this property to request processors that do not support DTDs</td><td>Boolean</td><td>True</td><td>Yes</td></tr> 57 *<tr><td>javax.xml.stream.reporter</td><td>sets/gets the impl of the XMLReporter </td><td>javax.xml.stream.XMLReporter</td><td>Null</td><td>Yes</td></tr> 58 *<tr><td>javax.xml.stream.resolver</td><td>sets/gets the impl of the XMLResolver interface</td><td>javax.xml.stream.XMLResolver</td><td>Null</td><td>Yes</td></tr> 59 *<tr><td>javax.xml.stream.allocator</td><td>sets/gets the impl of the XMLEventAllocator interface</td><td>javax.xml.stream.util.XMLEventAllocator</td><td>Null</td><td>Yes</td></tr> 60 * </tbody> 61 * </table> 62 * 63 * 64 * @version 1.2 65 * @author Copyright (c) 2009, 2015 by Oracle Corporation. All Rights Reserved. 66 * @see XMLOutputFactory 67 * @see XMLEventReader 68 * @see XMLStreamReader 69 * @see EventFilter 70 * @see XMLReporter 71 * @see XMLResolver 72 * @see javax.xml.stream.util.XMLEventAllocator 73 * @since 1.6 74 */ 75 76 public abstract class XMLInputFactory { 77 /** 78 * The property used to turn on/off namespace support, 79 * this is to support XML 1.0 documents, 80 * only the true setting must be supported 81 */ 82 public static final String IS_NAMESPACE_AWARE= 83 "javax.xml.stream.isNamespaceAware"; 84 85 /** 86 * The property used to turn on/off implementation specific validation 87 */ 88 public static final String IS_VALIDATING= 89 "javax.xml.stream.isValidating"; 90 91 /** 92 * The property that requires the parser to coalesce adjacent character data sections 93 */ 94 public static final String IS_COALESCING= 95 "javax.xml.stream.isCoalescing"; 96 97 /** 98 * Requires the parser to replace internal 99 * entity references with their replacement 100 * text and report them as characters 101 */ 102 public static final String IS_REPLACING_ENTITY_REFERENCES= 103 "javax.xml.stream.isReplacingEntityReferences"; 104 105 /** 106 * The property that requires the parser to resolve external parsed entities 107 */ 108 public static final String IS_SUPPORTING_EXTERNAL_ENTITIES= 109 "javax.xml.stream.isSupportingExternalEntities"; 110 111 /** 112 * The property that requires the parser to support DTDs 113 */ 114 public static final String SUPPORT_DTD= 115 "javax.xml.stream.supportDTD"; 116 117 /** 118 * The property used to 119 * set/get the implementation of the XMLReporter interface 120 */ 121 public static final String REPORTER= 122 "javax.xml.stream.reporter"; 123 124 /** 125 * The property used to set/get the implementation of the XMLResolver 126 */ 127 public static final String RESOLVER= 128 "javax.xml.stream.resolver"; 129 130 /** 131 * The property used to set/get the implementation of the allocator 132 */ 133 public static final String ALLOCATOR= 134 "javax.xml.stream.allocator"; 135 136 static final String DEFAULIMPL = "com.sun.xml.internal.stream.XMLInputFactoryImpl"; 137 138 protected XMLInputFactory(){} 139 140 /** 141 * Creates a new instance of the {@code XMLInputFactory} builtin 142 * system-default implementation. 143 * 144 * @return A new instance of the {@code XMLInputFactory} builtin 145 * system-default implementation. 146 * 147 * @since 9 148 */ 149 public static XMLInputFactory newDefaultFactory() { 150 return new XMLInputFactoryImpl(); 151 } 152 153 /** 154 * Creates a new instance of the factory in exactly the same manner as the 155 * {@link #newFactory()} method. 156 * @throws FactoryConfigurationError if an instance of this factory cannot be loaded 157 */ 158 public static XMLInputFactory newInstance() 159 throws FactoryConfigurationError 160 { 161 return FactoryFinder.find(XMLInputFactory.class, DEFAULIMPL); 162 } 163 164 /** 165 * Create a new instance of the factory. 166 * <p> 167 * This static method creates a new factory instance. 168 * This method uses the following ordered lookup procedure to determine 169 * the XMLInputFactory implementation class to load: 170 * 171 * <ul> 172 * <li> 173 * <p>Use the javax.xml.stream.XMLInputFactory system property. 174 * </li> 175 * <li> 176 * <p>Use the configuration file "stax.properties". The file is in standard 177 * {@link java.util.Properties} format and typically located in the 178 * {@code conf} directory of the Java installation. It contains the fully qualified 179 * name of the implementation class with the key being the system property 180 * defined above. 181 * 182 * <p>The stax.properties file is read only once by the implementation 183 * and its values are then cached for future use. If the file does not exist 184 * when the first attempt is made to read from it, no further attempts are 185 * made to check for its existence. It is not possible to change the value 186 * of any property in stax.properties after it has been read for the first time. 187 * 188 * <p> 189 * Use the jaxp configuration file "jaxp.properties". The file is in the same 190 * format as stax.properties and will only be read if stax.properties does 191 * not exist. 192 * </li> 193 * <li> 194 * <p>Use the service-provider loading facility, defined by the 195 * {@link java.util.ServiceLoader} class, to attempt to locate and load an 196 * implementation of the service using the {@linkplain 197 * java.util.ServiceLoader#load(java.lang.Class) default loading mechanism}: 198 * the service-provider loading facility will use the {@linkplain 199 * java.lang.Thread#getContextClassLoader() current thread's context class loader} 200 * to attempt to load the service. If the context class 201 * loader is null, the {@linkplain 202 * ClassLoader#getSystemClassLoader() system class loader} will be used. 203 * </li> 204 * <li> 205 * <p>Otherwise, the {@linkplain #newDefaultFactory() system-default} 206 * implementation is returned. 207 * </li> 208 * </ul> 209 * <p> 210 * Once an application has obtained a reference to a XMLInputFactory it 211 * can use the factory to configure and obtain stream instances. 212 * <p> 213 * Note that this is a new method that replaces the deprecated newInstance() method. 214 * No changes in behavior are defined by this replacement method relative to 215 * the deprecated method. 216 * 217 * @throws FactoryConfigurationError in case of {@linkplain 218 * java.util.ServiceConfigurationError service configuration error} or if 219 * the implementation is not available or cannot be instantiated. 220 */ 221 @Deprecated(since="1.7") 222 public static XMLInputFactory newFactory() 223 throws FactoryConfigurationError 224 { 225 return FactoryFinder.find(XMLInputFactory.class, DEFAULIMPL); 226 } 227 228 /** 229 * Create a new instance of the factory 230 * 231 * @param factoryId Name of the factory to find, same as 232 * a property name 233 * @param classLoader classLoader to use 234 * @return the factory implementation 235 * @throws FactoryConfigurationError if an instance of this factory cannot be loaded 236 * 237 * @deprecated This method has been deprecated to maintain API consistency. 238 * All newInstance methods have been replaced with corresponding 239 * newFactory methods. The replacement {@link 240 * #newFactory(java.lang.String, java.lang.ClassLoader)} method 241 * defines no changes in behavior. 242 */ 243 @Deprecated(since="1.7") 244 public static XMLInputFactory newInstance(String factoryId, 245 ClassLoader classLoader) 246 throws FactoryConfigurationError { 247 //do not fallback if given classloader can't find the class, throw exception 248 return FactoryFinder.find(XMLInputFactory.class, factoryId, classLoader, null); 249 } 250 251 /** 252 * Create a new instance of the factory. 253 * If the classLoader argument is null, then the ContextClassLoader is used. 254 * <p> 255 * This method uses the following ordered lookup procedure to determine 256 * the XMLInputFactory implementation class to load: 257 * <ul> 258 * <li> 259 * <p> 260 * Use the value of the system property identified by {@code factoryId}. 261 * </li> 262 * <li> 263 * <p> 264 * Use the configuration file "stax.properties". The file is in standard 265 * {@link java.util.Properties} format and typically located in the 266 * {@code conf} directory of the Java installation. It contains the fully qualified 267 * name of the implementation class with the key being the system property 268 * defined above. 269 * 270 * <p> 271 * The stax.properties file is read only once by the implementation 272 * and its values are then cached for future use. If the file does not exist 273 * when the first attempt is made to read from it, no further attempts are 274 * made to check for its existence. It is not possible to change the value 275 * of any property in stax.properties after it has been read for the first time. 276 * 277 * <p> 278 * Use the jaxp configuration file "jaxp.properties". The file is in the same 279 * format as stax.properties and will only be read if stax.properties does 280 * not exist. 281 * </li> 282 * <li> 283 * <p> 284 * If {@code factoryId} is "javax.xml.stream.XMLInputFactory", 285 * use the service-provider loading facility, defined by the 286 * {@link java.util.ServiceLoader} class, to attempt to {@linkplain 287 * java.util.ServiceLoader#load(java.lang.Class, java.lang.ClassLoader) locate and load} 288 * an implementation of the service using the specified {@code ClassLoader}. 289 * If {@code classLoader} is null, the {@linkplain 290 * java.util.ServiceLoader#load(java.lang.Class) default loading mechanism} will apply: 291 * That is, the service-provider loading facility will use the {@linkplain 292 * java.lang.Thread#getContextClassLoader() current thread's context class loader} 293 * to attempt to load the service. If the context class 294 * loader is null, the {@linkplain 295 * ClassLoader#getSystemClassLoader() system class loader} will be used. 296 * </li> 297 * <li> 298 * <p> 299 * Otherwise, throws a {@link FactoryConfigurationError}. 300 * </li> 301 * </ul> 302 * 303 * <p> 304 * Note that this is a new method that replaces the deprecated 305 * {@link #newInstance(java.lang.String, java.lang.ClassLoader) 306 * newInstance(String factoryId, ClassLoader classLoader)} method. 307 * No changes in behavior are defined by this replacement method relative 308 * to the deprecated method. 309 * 310 * 311 * @apiNote The parameter factoryId defined here is inconsistent with that 312 * of other JAXP factories where the first parameter is fully qualified 313 * factory class name that provides implementation of the factory. 314 * 315 * @param factoryId Name of the factory to find, same as 316 * a property name 317 * @param classLoader classLoader to use 318 * @return the factory implementation 319 * @throws FactoryConfigurationError in case of {@linkplain 320 * java.util.ServiceConfigurationError service configuration error} or if 321 * the implementation is not available or cannot be instantiated. 322 * @throws FactoryConfigurationError if an instance of this factory cannot be loaded 323 */ 324 public static XMLInputFactory newFactory(String factoryId, 325 ClassLoader classLoader) 326 throws FactoryConfigurationError { 327 //do not fallback if given classloader can't find the class, throw exception 328 return FactoryFinder.find(XMLInputFactory.class, factoryId, classLoader, null); 329 } 330 331 /** 332 * Create a new XMLStreamReader from a reader 333 * @param reader the XML data to read from 334 * @throws XMLStreamException 335 */ 336 public abstract XMLStreamReader createXMLStreamReader(java.io.Reader reader) 337 throws XMLStreamException; 338 339 /** 340 * Create a new XMLStreamReader from a JAXP source. This method is optional. 341 * @param source the source to read from 342 * @throws UnsupportedOperationException if this method is not 343 * supported by this XMLInputFactory 344 * @throws XMLStreamException 345 */ 346 public abstract XMLStreamReader createXMLStreamReader(Source source) 347 throws XMLStreamException; 348 349 /** 350 * Create a new XMLStreamReader from a java.io.InputStream 351 * @param stream the InputStream to read from 352 * @throws XMLStreamException 353 */ 354 public abstract XMLStreamReader createXMLStreamReader(java.io.InputStream stream) 355 throws XMLStreamException; 356 357 /** 358 * Create a new XMLStreamReader from a java.io.InputStream 359 * @param stream the InputStream to read from 360 * @param encoding the character encoding of the stream 361 * @throws XMLStreamException 362 */ 363 public abstract XMLStreamReader createXMLStreamReader(java.io.InputStream stream, String encoding) 364 throws XMLStreamException; 365 366 /** 367 * Create a new XMLStreamReader from a java.io.InputStream 368 * @param systemId the system ID of the stream 369 * @param stream the InputStream to read from 370 */ 371 public abstract XMLStreamReader createXMLStreamReader(String systemId, java.io.InputStream stream) 372 throws XMLStreamException; 373 374 /** 375 * Create a new XMLStreamReader from a java.io.InputStream 376 * @param systemId the system ID of the stream 377 * @param reader the InputStream to read from 378 */ 379 public abstract XMLStreamReader createXMLStreamReader(String systemId, java.io.Reader reader) 380 throws XMLStreamException; 381 382 /** 383 * Create a new XMLEventReader from a reader 384 * @param reader the XML data to read from 385 * @throws XMLStreamException 386 */ 387 public abstract XMLEventReader createXMLEventReader(java.io.Reader reader) 388 throws XMLStreamException; 389 390 /** 391 * Create a new XMLEventReader from a reader 392 * @param systemId the system ID of the input 393 * @param reader the XML data to read from 394 * @throws XMLStreamException 395 */ 396 public abstract XMLEventReader createXMLEventReader(String systemId, java.io.Reader reader) 397 throws XMLStreamException; 398 399 /** 400 * Create a new XMLEventReader from an XMLStreamReader. After being used 401 * to construct the XMLEventReader instance returned from this method 402 * the XMLStreamReader must not be used. 403 * @param reader the XMLStreamReader to read from (may not be modified) 404 * @return a new XMLEventReader 405 * @throws XMLStreamException 406 */ 407 public abstract XMLEventReader createXMLEventReader(XMLStreamReader reader) 408 throws XMLStreamException; 409 410 /** 411 * Create a new XMLEventReader from a JAXP source. 412 * Support of this method is optional. 413 * @param source the source to read from 414 * @throws UnsupportedOperationException if this method is not 415 * supported by this XMLInputFactory 416 */ 417 public abstract XMLEventReader createXMLEventReader(Source source) 418 throws XMLStreamException; 419 420 /** 421 * Create a new XMLEventReader from a java.io.InputStream 422 * @param stream the InputStream to read from 423 * @throws XMLStreamException 424 */ 425 public abstract XMLEventReader createXMLEventReader(java.io.InputStream stream) 426 throws XMLStreamException; 427 428 /** 429 * Create a new XMLEventReader from a java.io.InputStream 430 * @param stream the InputStream to read from 431 * @param encoding the character encoding of the stream 432 * @throws XMLStreamException 433 */ 434 public abstract XMLEventReader createXMLEventReader(java.io.InputStream stream, String encoding) 435 throws XMLStreamException; 436 437 /** 438 * Create a new XMLEventReader from a java.io.InputStream 439 * @param systemId the system ID of the stream 440 * @param stream the InputStream to read from 441 * @throws XMLStreamException 442 */ 443 public abstract XMLEventReader createXMLEventReader(String systemId, java.io.InputStream stream) 444 throws XMLStreamException; 445 446 /** 447 * Create a filtered reader that wraps the filter around the reader 448 * @param reader the reader to filter 449 * @param filter the filter to apply to the reader 450 * @throws XMLStreamException 451 */ 452 public abstract XMLStreamReader createFilteredReader(XMLStreamReader reader, StreamFilter filter) 453 throws XMLStreamException; 454 455 /** 456 * Create a filtered event reader that wraps the filter around the event reader 457 * @param reader the event reader to wrap 458 * @param filter the filter to apply to the event reader 459 * @throws XMLStreamException 460 */ 461 public abstract XMLEventReader createFilteredReader(XMLEventReader reader, EventFilter filter) 462 throws XMLStreamException; 463 464 /** 465 * The resolver that will be set on any XMLStreamReader or XMLEventReader created 466 * by this factory instance. 467 */ 468 public abstract XMLResolver getXMLResolver(); 469 470 /** 471 * The resolver that will be set on any XMLStreamReader or XMLEventReader created 472 * by this factory instance. 473 * @param resolver the resolver to use to resolve references 474 */ 475 public abstract void setXMLResolver(XMLResolver resolver); 476 477 /** 478 * The reporter that will be set on any XMLStreamReader or XMLEventReader created 479 * by this factory instance. 480 */ 481 public abstract XMLReporter getXMLReporter(); 482 483 /** 484 * The reporter that will be set on any XMLStreamReader or XMLEventReader created 485 * by this factory instance. 486 * @param reporter the resolver to use to report non fatal errors 487 */ 488 public abstract void setXMLReporter(XMLReporter reporter); 489 490 /** 491 * Allows the user to set specific feature/property on the underlying 492 * implementation. The underlying implementation is not required to support 493 * every setting of every property in the specification and may use 494 * IllegalArgumentException to signal that an unsupported property may not be 495 * set with the specified value. 496 * <p> 497 * All implementations that implement JAXP 1.5 or newer are required to 498 * support the {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_DTD} property. 499 * <ul> 500 * <li> 501 * <p> 502 * Access to external DTDs, external Entity References is restricted to the 503 * protocols specified by the property. If access is denied during parsing 504 * due to the restriction of this property, {@link javax.xml.stream.XMLStreamException} 505 * will be thrown by the {@link javax.xml.stream.XMLStreamReader#next()} or 506 * {@link javax.xml.stream.XMLEventReader#nextEvent()} method. 507 * 508 * </li> 509 * </ul> 510 * @param name The name of the property (may not be null) 511 * @param value The value of the property 512 * @throws java.lang.IllegalArgumentException if the property is not supported 513 */ 514 public abstract void setProperty(java.lang.String name, Object value) 515 throws java.lang.IllegalArgumentException; 516 517 /** 518 * Get the value of a feature/property from the underlying implementation 519 * @param name The name of the property (may not be null) 520 * @return The value of the property 521 * @throws IllegalArgumentException if the property is not supported 522 */ 523 public abstract Object getProperty(java.lang.String name) 524 throws java.lang.IllegalArgumentException; 525 526 527 /** 528 * Query the set of properties that this factory supports. 529 * 530 * @param name The name of the property (may not be null) 531 * @return true if the property is supported and false otherwise 532 */ 533 public abstract boolean isPropertySupported(String name); 534 535 /** 536 * Set a user defined event allocator for events 537 * @param allocator the user defined allocator 538 */ 539 public abstract void setEventAllocator(XMLEventAllocator allocator); 540 541 /** 542 * Gets the allocator used by streams created with this factory 543 */ 544 public abstract XMLEventAllocator getEventAllocator(); 545 546 }