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 import java.util.Iterator; 31 import javax.xml.namespace.NamespaceContext; 32 import javax.xml.namespace.QName; 33 import javax.xml.stream.events.*; 34 /** 35 * This interface defines a utility class for creating instances of 36 * XMLEvents 37 * @version 1.2 38 * @author Copyright (c) 2009 by Oracle Corporation. All Rights Reserved. 39 * @see javax.xml.stream.events.StartElement 40 * @see javax.xml.stream.events.EndElement 41 * @see javax.xml.stream.events.ProcessingInstruction 42 * @see javax.xml.stream.events.Comment 43 * @see javax.xml.stream.events.Characters 44 * @see javax.xml.stream.events.StartDocument 45 * @see javax.xml.stream.events.EndDocument 46 * @see javax.xml.stream.events.DTD 47 * @since 1.6 48 */ 49 public abstract class XMLEventFactory { 50 protected XMLEventFactory(){} 51 52 static final String JAXPFACTORYID = "javax.xml.stream.XMLEventFactory"; 53 static final String DEFAULIMPL = "com.sun.xml.internal.stream.events.XMLEventFactoryImpl"; 54 55 56 /** 57 * Creates a new instance of the factory in exactly the same manner as the 58 * {@link #newFactory()} method. 59 * @throws FactoryConfigurationError if an instance of this factory cannot be loaded 60 */ 61 public static XMLEventFactory newInstance() 62 throws FactoryConfigurationError 63 { 64 return FactoryFinder.find(XMLEventFactory.class, DEFAULIMPL); 65 } 66 67 /** 68 * Create a new instance of the factory. 69 * <p> 70 * This static method creates a new factory instance. 71 * This method uses the following ordered lookup procedure to determine 72 * the XMLEventFactory implementation class to load: 73 * </p> 74 * <ul> 75 * <li> 76 * Use the javax.xml.stream.XMLEventFactory system property. 77 * </li> 78 * <li> 79 * Use the properties file "lib/stax.properties" in the JRE directory. 80 * This configuration file is in standard java.util.Properties format 81 * and contains the fully qualified name of the implementation class 82 * with the key being the system property defined above. 83 * </li> 84 * <li> 85 * Use the service-provider loading facilities, defined by the 86 * {@link java.util.ServiceLoader} class, to attempt to locate and load an 87 * implementation of the service using the {@linkplain 88 * java.util.ServiceLoader#load(java.lang.Class) default loading mechanism}: 89 * the service-provider loading facility will use the {@linkplain 90 * java.lang.Thread#getContextClassLoader() current thread's context class loader} 91 * to attempt to load the service. If the context class 92 * loader is null, the {@linkplain 93 * ClassLoader#getSystemClassLoader() system class loader} will be used. 94 * </li> 95 * <li> 96 * Otherwise, the system-default implementation is returned. 97 * </li> 98 * </ul> 99 * <p> 100 * Once an application has obtained a reference to a XMLEventFactory it 101 * can use the factory to configure and obtain stream instances. 102 * </p> 103 * <p> 104 * Note that this is a new method that replaces the deprecated newInstance() method. 105 * No changes in behavior are defined by this replacement method relative to 106 * the deprecated method. 107 * </p> 108 * @throws FactoryConfigurationError in case of {@linkplain 109 * java.util.ServiceConfigurationError service configuration error} or if 110 * the implementation is not available or cannot be instantiated. 111 */ 112 public static XMLEventFactory newFactory() 113 throws FactoryConfigurationError 114 { 115 return FactoryFinder.find(XMLEventFactory.class, DEFAULIMPL); 116 } 117 118 /** 119 * Create a new instance of the factory 120 * 121 * @param factoryId Name of the factory to find, same as 122 * a property name 123 * @param classLoader classLoader to use 124 * @return the factory implementation 125 * @throws FactoryConfigurationError if an instance of this factory cannot be loaded 126 * 127 * @deprecated This method has been deprecated to maintain API consistency. 128 * All newInstance methods have been replaced with corresponding 129 * newFactory methods. The replacement {@link 130 * #newFactory(java.lang.String, java.lang.ClassLoader)} 131 * method defines no changes in behavior. 132 */ 133 public static XMLEventFactory newInstance(String factoryId, 134 ClassLoader classLoader) 135 throws FactoryConfigurationError { 136 //do not fallback if given classloader can't find the class, throw exception 137 return FactoryFinder.find(XMLEventFactory.class, factoryId, classLoader, null); 138 } 139 140 /** 141 * Create a new instance of the factory. 142 * If the classLoader argument is null, then the ContextClassLoader is used. 143 * <p> 144 * This method uses the following ordered lookup procedure to determine 145 * the XMLEventFactory implementation class to load: 146 * </p> 147 * <ul> 148 * <li> 149 * Use the value of the system property identified by {@code factoryId}. 150 * </li> 151 * <li> 152 * Use the properties file "lib/stax.properties" in the JRE directory. 153 * This configuration file is in standard java.util.Properties format 154 * and contains the fully qualified name of the implementation class 155 * with the key being the given {@code factoryId}. 156 * </li> 157 * <li> 158 * If {@code factoryId} is "javax.xml.stream.XMLEventFactory", 159 * use the service-provider loading facilities, defined by the 160 * {@link java.util.ServiceLoader} class, to attempt to locate and load an 161 * implementation of the service using the {@linkplain 162 * java.util.ServiceLoader#load(java.lang.Class) default loading mechanism}: 163 * the service-provider loading facility will use the {@linkplain 164 * java.lang.Thread#getContextClassLoader() current thread's context class loader} 165 * to attempt to load the service. If the context class 166 * loader is null, the {@linkplain 167 * ClassLoader#getSystemClassLoader() system class loader} will be used. 168 * </li> 169 * <li> 170 * Otherwise, throws a {@link FactoryConfigurationError}. 171 * </li> 172 * </ul> 173 * 174 * <p> 175 * Note that this is a new method that replaces the deprecated 176 * {@link #newInstance(java.lang.String, java.lang.ClassLoader) 177 * newInstance(String factoryId, ClassLoader classLoader)} method. 178 * No changes in behavior are defined by this replacement method relative 179 * to the deprecated method. 180 * </p> 181 * 182 * @param factoryId Name of the factory to find, same as 183 * a property name 184 * @param classLoader classLoader to use 185 * @return the factory implementation 186 * @throws FactoryConfigurationError in case of {@linkplain 187 * java.util.ServiceConfigurationError service configuration error} or if 188 * the implementation is not available or cannot be instantiated. 189 */ 190 public static XMLEventFactory newFactory(String factoryId, 191 ClassLoader classLoader) 192 throws FactoryConfigurationError { 193 //do not fallback if given classloader can't find the class, throw exception 194 return FactoryFinder.find(XMLEventFactory.class, factoryId, classLoader, null); 195 } 196 197 /** 198 * This method allows setting of the Location on each event that 199 * is created by this factory. The values are copied by value into 200 * the events created by this factory. To reset the location 201 * information set the location to null. 202 * @param location the location to set on each event created 203 */ 204 public abstract void setLocation(Location location); 205 206 /** 207 * Create a new Attribute 208 * @param prefix the prefix of this attribute, may not be null 209 * @param namespaceURI the attribute value is set to this value, may not be null 210 * @param localName the local name of the XML name of the attribute, localName cannot be null 211 * @param value the attribute value to set, may not be null 212 * @return the Attribute with specified values 213 */ 214 public abstract Attribute createAttribute(String prefix, String namespaceURI, String localName, String value); 215 216 /** 217 * Create a new Attribute 218 * @param localName the local name of the XML name of the attribute, localName cannot be null 219 * @param value the attribute value to set, may not be null 220 * @return the Attribute with specified values 221 */ 222 public abstract Attribute createAttribute(String localName, String value); 223 224 /** 225 * Create a new Attribute 226 * @param name the qualified name of the attribute, may not be null 227 * @param value the attribute value to set, may not be null 228 * @return the Attribute with specified values 229 */ 230 public abstract Attribute createAttribute(QName name, String value); 231 232 /** 233 * Create a new default Namespace 234 * @param namespaceURI the default namespace uri 235 * @return the Namespace with the specified value 236 */ 237 public abstract Namespace createNamespace(String namespaceURI); 238 239 /** 240 * Create a new Namespace 241 * @param prefix the prefix of this namespace, may not be null 242 * @param namespaceUri the attribute value is set to this value, may not be null 243 * @return the Namespace with the specified values 244 */ 245 public abstract Namespace createNamespace(String prefix, String namespaceUri); 246 247 /** 248 * Create a new StartElement. Namespaces can be added to this StartElement 249 * by passing in an Iterator that walks over a set of Namespace interfaces. 250 * Attributes can be added to this StartElement by passing an iterator 251 * that walks over a set of Attribute interfaces. 252 * 253 * @param name the qualified name of the attribute, may not be null 254 * @param attributes an optional unordered set of objects that 255 * implement Attribute to add to the new StartElement, may be null 256 * @param namespaces an optional unordered set of objects that 257 * implement Namespace to add to the new StartElement, may be null 258 * @return an instance of the requested StartElement 259 */ 260 public abstract StartElement createStartElement(QName name, 261 Iterator attributes, 262 Iterator namespaces); 263 264 /** 265 * Create a new StartElement. This defaults the NamespaceContext to 266 * an empty NamespaceContext. Querying this event for its namespaces or 267 * attributes will result in an empty iterator being returned. 268 * 269 * @param namespaceUri the uri of the QName of the new StartElement 270 * @param localName the local name of the QName of the new StartElement 271 * @param prefix the prefix of the QName of the new StartElement 272 * @return an instance of the requested StartElement 273 */ 274 public abstract StartElement createStartElement(String prefix, 275 String namespaceUri, 276 String localName); 277 /** 278 * Create a new StartElement. Namespaces can be added to this StartElement 279 * by passing in an Iterator that walks over a set of Namespace interfaces. 280 * Attributes can be added to this StartElement by passing an iterator 281 * that walks over a set of Attribute interfaces. 282 * 283 * @param namespaceUri the uri of the QName of the new StartElement 284 * @param localName the local name of the QName of the new StartElement 285 * @param prefix the prefix of the QName of the new StartElement 286 * @param attributes an unordered set of objects that implement 287 * Attribute to add to the new StartElement 288 * @param namespaces an unordered set of objects that implement 289 * Namespace to add to the new StartElement 290 * @return an instance of the requested StartElement 291 */ 292 public abstract StartElement createStartElement(String prefix, 293 String namespaceUri, 294 String localName, 295 Iterator attributes, 296 Iterator namespaces 297 ); 298 /** 299 * Create a new StartElement. Namespaces can be added to this StartElement 300 * by passing in an Iterator that walks over a set of Namespace interfaces. 301 * Attributes can be added to this StartElement by passing an iterator 302 * that walks over a set of Attribute interfaces. 303 * 304 * @param namespaceUri the uri of the QName of the new StartElement 305 * @param localName the local name of the QName of the new StartElement 306 * @param prefix the prefix of the QName of the new StartElement 307 * @param attributes an unordered set of objects that implement 308 * Attribute to add to the new StartElement, may be null 309 * @param namespaces an unordered set of objects that implement 310 * Namespace to add to the new StartElement, may be null 311 * @param context the namespace context of this element 312 * @return an instance of the requested StartElement 313 */ 314 public abstract StartElement createStartElement(String prefix, 315 String namespaceUri, 316 String localName, 317 Iterator attributes, 318 Iterator namespaces, 319 NamespaceContext context 320 ); 321 322 /** 323 * Create a new EndElement 324 * @param name the qualified name of the EndElement 325 * @param namespaces an optional unordered set of objects that 326 * implement Namespace that have gone out of scope, may be null 327 * @return an instance of the requested EndElement 328 */ 329 public abstract EndElement createEndElement(QName name, 330 Iterator namespaces); 331 332 /** 333 * Create a new EndElement 334 * @param namespaceUri the uri of the QName of the new StartElement 335 * @param localName the local name of the QName of the new StartElement 336 * @param prefix the prefix of the QName of the new StartElement 337 * @return an instance of the requested EndElement 338 */ 339 public abstract EndElement createEndElement(String prefix, 340 String namespaceUri, 341 String localName); 342 /** 343 * Create a new EndElement 344 * @param namespaceUri the uri of the QName of the new StartElement 345 * @param localName the local name of the QName of the new StartElement 346 * @param prefix the prefix of the QName of the new StartElement 347 * @param namespaces an unordered set of objects that implement 348 * Namespace that have gone out of scope, may be null 349 * @return an instance of the requested EndElement 350 */ 351 public abstract EndElement createEndElement(String prefix, 352 String namespaceUri, 353 String localName, 354 Iterator namespaces); 355 356 /** 357 * Create a Characters event, this method does not check if the content 358 * is all whitespace. To create a space event use #createSpace(String) 359 * @param content the string to create 360 * @return a Characters event 361 */ 362 public abstract Characters createCharacters(String content); 363 364 /** 365 * Create a Characters event with the CData flag set to true 366 * @param content the string to create 367 * @return a Characters event 368 */ 369 public abstract Characters createCData(String content); 370 371 /** 372 * Create a Characters event with the isSpace flag set to true 373 * @param content the content of the space to create 374 * @return a Characters event 375 */ 376 public abstract Characters createSpace(String content); 377 /** 378 * Create an ignorable space 379 * @param content the space to create 380 * @return a Characters event 381 */ 382 public abstract Characters createIgnorableSpace(String content); 383 384 /** 385 * Creates a new instance of a StartDocument event 386 * @return a StartDocument event 387 */ 388 public abstract StartDocument createStartDocument(); 389 390 /** 391 * Creates a new instance of a StartDocument event 392 * 393 * @param encoding the encoding style 394 * @param version the XML version 395 * @param standalone the status of standalone may be set to "true" or "false" 396 * @return a StartDocument event 397 */ 398 public abstract StartDocument createStartDocument(String encoding, 399 String version, 400 boolean standalone); 401 402 /** 403 * Creates a new instance of a StartDocument event 404 * 405 * @param encoding the encoding style 406 * @param version the XML version 407 * @return a StartDocument event 408 */ 409 public abstract StartDocument createStartDocument(String encoding, 410 String version); 411 412 /** 413 * Creates a new instance of a StartDocument event 414 * 415 * @param encoding the encoding style 416 * @return a StartDocument event 417 */ 418 public abstract StartDocument createStartDocument(String encoding); 419 420 /** 421 * Creates a new instance of an EndDocument event 422 * @return an EndDocument event 423 */ 424 public abstract EndDocument createEndDocument(); 425 426 /** Creates a new instance of a EntityReference event 427 * 428 * @param name The name of the reference 429 * @param declaration the declaration for the event 430 * @return an EntityReference event 431 */ 432 public abstract EntityReference createEntityReference(String name, 433 EntityDeclaration declaration); 434 /** 435 * Create a comment 436 * @param text The text of the comment 437 * a Comment event 438 */ 439 public abstract Comment createComment(String text); 440 441 /** 442 * Create a processing instruction 443 * @param target The target of the processing instruction 444 * @param data The text of the processing instruction 445 * @return a ProcessingInstruction event 446 */ 447 public abstract ProcessingInstruction createProcessingInstruction(String target, 448 String data); 449 450 /** 451 * Create a document type definition event 452 * This string contains the entire document type declaration that matches 453 * the doctypedecl in the XML 1.0 specification 454 * @param dtd the text of the document type definition 455 * @return a DTD event 456 */ 457 public abstract DTD createDTD(String dtd); 458 }