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