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 * 213 * @throws FactoryConfigurationError in case of {@linkplain 214 * java.util.ServiceConfigurationError service configuration error} or if 215 * the implementation is not available or cannot be instantiated. 216 */ 217 public static XMLInputFactory newFactory() 218 throws FactoryConfigurationError 219 { 220 return FactoryFinder.find(XMLInputFactory.class, DEFAULIMPL); 221 } 222 223 /** 224 * Create a new instance of the factory 225 * 226 * @param factoryId Name of the factory to find, same as 227 * a property name 228 * @param classLoader classLoader to use 229 * @return the factory implementation 230 * @throws FactoryConfigurationError if an instance of this factory cannot be loaded 231 * 232 * @deprecated This method has been deprecated to maintain API consistency. 233 * All newInstance methods have been replaced with corresponding 234 * newFactory methods. The replacement {@link 235 * #newFactory(java.lang.String, java.lang.ClassLoader)} method 236 * defines no changes in behavior. 237 */ 238 @Deprecated(since="1.7") 239 public static XMLInputFactory newInstance(String factoryId, 240 ClassLoader classLoader) 241 throws FactoryConfigurationError { 242 //do not fallback if given classloader can't find the class, throw exception 243 return FactoryFinder.find(XMLInputFactory.class, factoryId, classLoader, null); 244 } 245 246 /** 247 * Create a new instance of the factory. 248 * If the classLoader argument is null, then the ContextClassLoader is used. 249 * <p> 250 * This method uses the following ordered lookup procedure to determine 251 * the XMLInputFactory implementation class to load: 252 * <ul> 253 * <li> 254 * <p> 255 * Use the value of the system property identified by {@code factoryId}. 256 * </li> 257 * <li> 258 * <p> 259 * Use the configuration file "stax.properties". The file is in standard 260 * {@link java.util.Properties} format and typically located in the 261 * {@code conf} directory of the Java installation. It contains the fully qualified 262 * name of the implementation class with the key being the system property 263 * defined above. 264 * 265 * <p> 266 * The stax.properties file is read only once by the implementation 267 * and its values are then cached for future use. If the file does not exist 268 * when the first attempt is made to read from it, no further attempts are 269 * made to check for its existence. It is not possible to change the value 270 * of any property in stax.properties after it has been read for the first time. 271 * 272 * <p> 273 * Use the jaxp configuration file "jaxp.properties". The file is in the same 274 * format as stax.properties and will only be read if stax.properties does 275 * not exist. 276 * </li> 277 * <li> 278 * <p> 279 * If {@code factoryId} is "javax.xml.stream.XMLInputFactory", 280 * use the service-provider loading facility, defined by the 281 * {@link java.util.ServiceLoader} class, to attempt to {@linkplain 282 * java.util.ServiceLoader#load(java.lang.Class, java.lang.ClassLoader) locate and load} 283 * an implementation of the service using the specified {@code ClassLoader}. 284 * If {@code classLoader} is null, the {@linkplain 285 * java.util.ServiceLoader#load(java.lang.Class) default loading mechanism} will apply: 286 * That is, the service-provider loading facility will use the {@linkplain 287 * java.lang.Thread#getContextClassLoader() current thread's context class loader} 288 * to attempt to load the service. If the context class 289 * loader is null, the {@linkplain 290 * ClassLoader#getSystemClassLoader() system class loader} will be used. 291 * </li> 292 * <li> 293 * <p> 294 * Otherwise, throws a {@link FactoryConfigurationError}. 295 * </li> 296 * </ul> 297 * 298 * <p> 299 * Note that this is a new method that replaces the deprecated 300 * {@link #newInstance(java.lang.String, java.lang.ClassLoader) 301 * newInstance(String factoryId, ClassLoader classLoader)} method. 302 * No changes in behavior are defined by this replacement method relative 303 * to the deprecated method. 304 * 305 * 306 * @apiNote The parameter factoryId defined here is inconsistent with that 307 * of other JAXP factories where the first parameter is fully qualified 308 * factory class name that provides implementation of the factory. 309 * 310 * @param factoryId Name of the factory to find, same as 311 * a property name 312 * @param classLoader classLoader to use 313 * @return the factory implementation 314 * @throws FactoryConfigurationError in case of {@linkplain 315 * java.util.ServiceConfigurationError service configuration error} or if 316 * the implementation is not available or cannot be instantiated. 317 * @throws FactoryConfigurationError if an instance of this factory cannot be loaded 318 */ 319 public static XMLInputFactory newFactory(String factoryId, 320 ClassLoader classLoader) 321 throws FactoryConfigurationError { 322 //do not fallback if given classloader can't find the class, throw exception 323 return FactoryFinder.find(XMLInputFactory.class, factoryId, classLoader, null); 324 } 325 326 /** 327 * Create a new XMLStreamReader from a reader 328 * @param reader the XML data to read from 329 * @throws XMLStreamException 330 */ 331 public abstract XMLStreamReader createXMLStreamReader(java.io.Reader reader) 332 throws XMLStreamException; 333 334 /** 335 * Create a new XMLStreamReader from a JAXP source. This method is optional. 336 * @param source the source to read from 337 * @throws UnsupportedOperationException if this method is not 338 * supported by this XMLInputFactory 339 * @throws XMLStreamException 340 */ 341 public abstract XMLStreamReader createXMLStreamReader(Source source) 342 throws XMLStreamException; 343 344 /** 345 * Create a new XMLStreamReader from a java.io.InputStream 346 * @param stream the InputStream to read from 347 * @throws XMLStreamException 348 */ 349 public abstract XMLStreamReader createXMLStreamReader(java.io.InputStream stream) 350 throws XMLStreamException; 351 352 /** 353 * Create a new XMLStreamReader from a java.io.InputStream 354 * @param stream the InputStream to read from 355 * @param encoding the character encoding of the stream 356 * @throws XMLStreamException 357 */ 358 public abstract XMLStreamReader createXMLStreamReader(java.io.InputStream stream, String encoding) 359 throws XMLStreamException; 360 361 /** 362 * Create a new XMLStreamReader from a java.io.InputStream 363 * @param systemId the system ID of the stream 364 * @param stream the InputStream to read from 365 */ 366 public abstract XMLStreamReader createXMLStreamReader(String systemId, java.io.InputStream stream) 367 throws XMLStreamException; 368 369 /** 370 * Create a new XMLStreamReader from a java.io.InputStream 371 * @param systemId the system ID of the stream 372 * @param reader the InputStream to read from 373 */ 374 public abstract XMLStreamReader createXMLStreamReader(String systemId, java.io.Reader reader) 375 throws XMLStreamException; 376 377 /** 378 * Create a new XMLEventReader from a reader 379 * @param reader the XML data to read from 380 * @throws XMLStreamException 381 */ 382 public abstract XMLEventReader createXMLEventReader(java.io.Reader reader) 383 throws XMLStreamException; 384 385 /** 386 * Create a new XMLEventReader from a reader 387 * @param systemId the system ID of the input 388 * @param reader the XML data to read from 389 * @throws XMLStreamException 390 */ 391 public abstract XMLEventReader createXMLEventReader(String systemId, java.io.Reader reader) 392 throws XMLStreamException; 393 394 /** 395 * Create a new XMLEventReader from an XMLStreamReader. After being used 396 * to construct the XMLEventReader instance returned from this method 397 * the XMLStreamReader must not be used. 398 * @param reader the XMLStreamReader to read from (may not be modified) 399 * @return a new XMLEventReader 400 * @throws XMLStreamException 401 */ 402 public abstract XMLEventReader createXMLEventReader(XMLStreamReader reader) 403 throws XMLStreamException; 404 405 /** 406 * Create a new XMLEventReader from a JAXP source. 407 * Support of this method is optional. 408 * @param source the source to read from 409 * @throws UnsupportedOperationException if this method is not 410 * supported by this XMLInputFactory 411 */ 412 public abstract XMLEventReader createXMLEventReader(Source source) 413 throws XMLStreamException; 414 415 /** 416 * Create a new XMLEventReader from a java.io.InputStream 417 * @param stream the InputStream to read from 418 * @throws XMLStreamException 419 */ 420 public abstract XMLEventReader createXMLEventReader(java.io.InputStream stream) 421 throws XMLStreamException; 422 423 /** 424 * Create a new XMLEventReader from a java.io.InputStream 425 * @param stream the InputStream to read from 426 * @param encoding the character encoding of the stream 427 * @throws XMLStreamException 428 */ 429 public abstract XMLEventReader createXMLEventReader(java.io.InputStream stream, String encoding) 430 throws XMLStreamException; 431 432 /** 433 * Create a new XMLEventReader from a java.io.InputStream 434 * @param systemId the system ID of the stream 435 * @param stream the InputStream to read from 436 * @throws XMLStreamException 437 */ 438 public abstract XMLEventReader createXMLEventReader(String systemId, java.io.InputStream stream) 439 throws XMLStreamException; 440 441 /** 442 * Create a filtered reader that wraps the filter around the reader 443 * @param reader the reader to filter 444 * @param filter the filter to apply to the reader 445 * @throws XMLStreamException 446 */ 447 public abstract XMLStreamReader createFilteredReader(XMLStreamReader reader, StreamFilter filter) 448 throws XMLStreamException; 449 450 /** 451 * Create a filtered event reader that wraps the filter around the event reader 452 * @param reader the event reader to wrap 453 * @param filter the filter to apply to the event reader 454 * @throws XMLStreamException 455 */ 456 public abstract XMLEventReader createFilteredReader(XMLEventReader reader, EventFilter filter) 457 throws XMLStreamException; 458 459 /** 460 * The resolver that will be set on any XMLStreamReader or XMLEventReader created 461 * by this factory instance. 462 */ 463 public abstract XMLResolver getXMLResolver(); 464 465 /** 466 * The resolver that will be set on any XMLStreamReader or XMLEventReader created 467 * by this factory instance. 468 * @param resolver the resolver to use to resolve references 469 */ 470 public abstract void setXMLResolver(XMLResolver resolver); 471 472 /** 473 * The reporter that will be set on any XMLStreamReader or XMLEventReader created 474 * by this factory instance. 475 */ 476 public abstract XMLReporter getXMLReporter(); 477 478 /** 479 * The reporter that will be set on any XMLStreamReader or XMLEventReader created 480 * by this factory instance. 481 * @param reporter the resolver to use to report non fatal errors 482 */ 483 public abstract void setXMLReporter(XMLReporter reporter); 484 485 /** 486 * Allows the user to set specific feature/property on the underlying 487 * implementation. The underlying implementation is not required to support 488 * every setting of every property in the specification and may use 489 * IllegalArgumentException to signal that an unsupported property may not be 490 * set with the specified value. 491 * <p> 492 * All implementations that implement JAXP 1.5 or newer are required to 493 * support the {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_DTD} property. 494 * <ul> 495 * <li> 496 * <p> 497 * Access to external DTDs, external Entity References is restricted to the 498 * protocols specified by the property. If access is denied during parsing 499 * due to the restriction of this property, {@link javax.xml.stream.XMLStreamException} 500 * will be thrown by the {@link javax.xml.stream.XMLStreamReader#next()} or 501 * {@link javax.xml.stream.XMLEventReader#nextEvent()} method. 502 * 503 * </li> 504 * </ul> 505 * @param name The name of the property (may not be null) 506 * @param value The value of the property 507 * @throws java.lang.IllegalArgumentException if the property is not supported 508 */ 509 public abstract void setProperty(java.lang.String name, Object value) 510 throws java.lang.IllegalArgumentException; 511 512 /** 513 * Get the value of a feature/property from the underlying implementation 514 * @param name The name of the property (may not be null) 515 * @return The value of the property 516 * @throws IllegalArgumentException if the property is not supported 517 */ 518 public abstract Object getProperty(java.lang.String name) 519 throws java.lang.IllegalArgumentException; 520 521 522 /** 523 * Query the set of properties that this factory supports. 524 * 525 * @param name The name of the property (may not be null) 526 * @return true if the property is supported and false otherwise 527 */ 528 public abstract boolean isPropertySupported(String name); 529 530 /** 531 * Set a user defined event allocator for events 532 * @param allocator the user defined allocator 533 */ 534 public abstract void setEventAllocator(XMLEventAllocator allocator); 535 536 /** 537 * Gets the allocator used by streams created with this factory 538 */ 539 public abstract XMLEventAllocator getEventAllocator(); 540 541 }