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