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 specified {@code ClassLoader}. 
 162    *   If {@code classLoader} is null, the {@linkplain
 163    *   java.util.ServiceLoader#load(java.lang.Class) default loading mechanism} will apply:
 164    *   That is, the service-provider loading facility will use the {@linkplain
 165    *   java.lang.Thread#getContextClassLoader() current thread's context class loader}
 166    *   to attempt to load the service. If the context class
 167    *   loader is null, the {@linkplain
 168    *   ClassLoader#getSystemClassLoader() system class loader} will be used.
 169    * </li>
 170    * <li>
 171    *   Otherwise, throws a {@link FactoryConfigurationError}.
 172    * </li>
 173    * </ul>
 174    *
 175    * <p>
 176    * Note that this is a new method that replaces the deprecated
 177    *   {@link #newInstance(java.lang.String, java.lang.ClassLoader)
 178    *   newInstance(String factoryId, ClassLoader classLoader)} method.
 179    * No changes in behavior are defined by this replacement method relative
 180    * to the deprecated method.
 181    * </p>
 182    * 
 183    * @apiNote The parameter factoryId defined here is inconsistent with that
 184    * of other JAXP factories where the first parameter is fully qualified 
 185    * factory class name that provides implementation of the factory.
 186    *
 187    * @param factoryId             Name of the factory to find, same as
 188    *                              a property name
 189    * @param classLoader           classLoader to use
 190    * @return the factory implementation
 191    * @throws FactoryConfigurationError in case of {@linkplain
 192    *   java.util.ServiceConfigurationError service configuration error} or if
 193    *   the implementation is not available or cannot be instantiated.
 194    */
 195   public static XMLEventFactory newFactory(String factoryId,
 196                                            ClassLoader classLoader)
 197           throws FactoryConfigurationError {
 198       //do not fallback if given classloader can't find the class, throw exception
 199       return FactoryFinder.find(XMLEventFactory.class, factoryId, classLoader, null);
 200   }
 201 
 202  /**
 203    * This method allows setting of the Location on each event that
 204    * is created by this factory.  The values are copied by value into
 205    * the events created by this factory.  To reset the location
 206    * information set the location to null.
 207    * @param location the location to set on each event created
 208    */
 209   public abstract void setLocation(Location location);
 210 
 211   /**
 212    * Create a new Attribute
 213    * @param prefix the prefix of this attribute, may not be null
 214    * @param namespaceURI the attribute value is set to this value, may not be null
 215    * @param localName the local name of the XML name of the attribute, localName cannot be null
 216    * @param value the attribute value to set, may not be null
 217    * @return the Attribute with specified values
 218    */
 219   public abstract Attribute createAttribute(String prefix, String namespaceURI, String localName, String value);
 220 
 221   /**
 222    * Create a new Attribute
 223    * @param localName the local name of the XML name of the attribute, localName cannot be null
 224    * @param value the attribute value to set, may not be null
 225    * @return the Attribute with specified values
 226    */
 227   public abstract Attribute createAttribute(String localName, String value);
 228 
 229   /**
 230    * Create a new Attribute
 231    * @param name the qualified name of the attribute, may not be null
 232    * @param value the attribute value to set, may not be null
 233    * @return the Attribute with specified values
 234    */
 235   public abstract Attribute createAttribute(QName name, String value);
 236 
 237   /**
 238    * Create a new default Namespace
 239    * @param namespaceURI the default namespace uri
 240    * @return the Namespace with the specified value
 241    */
 242   public abstract Namespace createNamespace(String namespaceURI);
 243 
 244   /**
 245    * Create a new Namespace
 246    * @param prefix the prefix of this namespace, may not be null
 247    * @param namespaceUri the attribute value is set to this value, may not be null
 248    * @return the Namespace with the specified values
 249    */
 250   public abstract Namespace createNamespace(String prefix, String namespaceUri);
 251 
 252   /**
 253    * Create a new StartElement.  Namespaces can be added to this StartElement
 254    * by passing in an Iterator that walks over a set of Namespace interfaces.
 255    * Attributes can be added to this StartElement by passing an iterator
 256    * that walks over a set of Attribute interfaces.
 257    *
 258    * @param name the qualified name of the attribute, may not be null
 259    * @param attributes an optional unordered set of objects that
 260    * implement Attribute to add to the new StartElement, may be null
 261    * @param namespaces an optional unordered set of objects that
 262    * implement Namespace to add to the new StartElement, may be null
 263    * @return an instance of the requested StartElement
 264    */
 265   public abstract StartElement createStartElement(QName name,
 266                                                   Iterator attributes,
 267                                                   Iterator namespaces);
 268 
 269   /**
 270    * Create a new StartElement.  This defaults the NamespaceContext to
 271    * an empty NamespaceContext.  Querying this event for its namespaces or
 272    * attributes will result in an empty iterator being returned.
 273    *
 274    * @param namespaceUri the uri of the QName of the new StartElement
 275    * @param localName the local name of the QName of the new StartElement
 276    * @param prefix the prefix of the QName of the new StartElement
 277    * @return an instance of the requested StartElement
 278    */
 279   public abstract StartElement createStartElement(String prefix,
 280                                                   String namespaceUri,
 281                                                   String localName);
 282   /**
 283    * Create a new StartElement.  Namespaces can be added to this StartElement
 284    * by passing in an Iterator that walks over a set of Namespace interfaces.
 285    * Attributes can be added to this StartElement by passing an iterator
 286    * that walks over a set of Attribute interfaces.
 287    *
 288    * @param namespaceUri the uri of the QName of the new StartElement
 289    * @param localName the local name of the QName of the new StartElement
 290    * @param prefix the prefix of the QName of the new StartElement
 291    * @param attributes an unordered set of objects that implement
 292    * Attribute to add to the new StartElement
 293    * @param namespaces an unordered set of objects that implement
 294    * Namespace to add to the new StartElement
 295    * @return an instance of the requested StartElement
 296    */
 297   public abstract StartElement createStartElement(String prefix,
 298                                                   String namespaceUri,
 299                                                   String localName,
 300                                                   Iterator attributes,
 301                                                   Iterator namespaces
 302                                                   );
 303   /**
 304    * Create a new StartElement.  Namespaces can be added to this StartElement
 305    * by passing in an Iterator that walks over a set of Namespace interfaces.
 306    * Attributes can be added to this StartElement by passing an iterator
 307    * that walks over a set of Attribute interfaces.
 308    *
 309    * @param namespaceUri the uri of the QName of the new StartElement
 310    * @param localName the local name of the QName of the new StartElement
 311    * @param prefix the prefix of the QName of the new StartElement
 312    * @param attributes an unordered set of objects that implement
 313    * Attribute to add to the new StartElement, may be null
 314    * @param namespaces an unordered set of objects that implement
 315    * Namespace to add to the new StartElement, may be null
 316    * @param context the namespace context of this element
 317    * @return an instance of the requested StartElement
 318    */
 319   public abstract StartElement createStartElement(String prefix,
 320                                                   String namespaceUri,
 321                                                   String localName,
 322                                                   Iterator attributes,
 323                                                   Iterator namespaces,
 324                                                   NamespaceContext context
 325                                                   );
 326 
 327   /**
 328    * Create a new EndElement
 329    * @param name the qualified name of the EndElement
 330    * @param namespaces an optional unordered set of objects that
 331    * implement Namespace that have gone out of scope, may be null
 332    * @return an instance of the requested EndElement
 333    */
 334   public abstract EndElement createEndElement(QName name,
 335                                               Iterator namespaces);
 336 
 337   /**
 338    * Create a new EndElement
 339    * @param namespaceUri the uri of the QName of the new StartElement
 340    * @param localName the local name of the QName of the new StartElement
 341    * @param prefix the prefix of the QName of the new StartElement
 342    * @return an instance of the requested EndElement
 343    */
 344   public abstract EndElement createEndElement(String prefix,
 345                                               String namespaceUri,
 346                                               String localName);
 347   /**
 348    * Create a new EndElement
 349    * @param namespaceUri the uri of the QName of the new StartElement
 350    * @param localName the local name of the QName of the new StartElement
 351    * @param prefix the prefix of the QName of the new StartElement
 352    * @param namespaces an unordered set of objects that implement
 353    * Namespace that have gone out of scope, may be null
 354    * @return an instance of the requested EndElement
 355    */
 356   public abstract EndElement createEndElement(String prefix,
 357                                               String namespaceUri,
 358                                               String localName,
 359                                               Iterator namespaces);
 360 
 361   /**
 362    * Create a Characters event, this method does not check if the content
 363    * is all whitespace.  To create a space event use #createSpace(String)
 364    * @param content the string to create
 365    * @return a Characters event
 366    */
 367   public abstract Characters createCharacters(String content);
 368 
 369   /**
 370    * Create a Characters event with the CData flag set to true
 371    * @param content the string to create
 372    * @return a Characters event
 373    */
 374   public abstract Characters createCData(String content);
 375 
 376   /**
 377    * Create a Characters event with the isSpace flag set to true
 378    * @param content the content of the space to create
 379    * @return a Characters event
 380    */
 381   public abstract Characters createSpace(String content);
 382   /**
 383    * Create an ignorable space
 384    * @param content the space to create
 385    * @return a Characters event
 386    */
 387   public abstract Characters createIgnorableSpace(String content);
 388 
 389   /**
 390    * Creates a new instance of a StartDocument event
 391    * @return a StartDocument event
 392    */
 393   public abstract StartDocument createStartDocument();
 394 
 395   /**
 396    * Creates a new instance of a StartDocument event
 397    *
 398    * @param encoding the encoding style
 399    * @param version the XML version
 400    * @param standalone the status of standalone may be set to "true" or "false"
 401    * @return a StartDocument event
 402    */
 403   public abstract StartDocument createStartDocument(String encoding,
 404                                                   String version,
 405                                                   boolean standalone);
 406 
 407   /**
 408    * Creates a new instance of a StartDocument event
 409    *
 410    * @param encoding the encoding style
 411    * @param version the XML version
 412    * @return a StartDocument event
 413    */
 414   public abstract StartDocument createStartDocument(String encoding,
 415                                                   String version);
 416 
 417   /**
 418    * Creates a new instance of a StartDocument event
 419    *
 420    * @param encoding the encoding style
 421    * @return a StartDocument event
 422    */
 423   public abstract StartDocument createStartDocument(String encoding);
 424 
 425   /**
 426    * Creates a new instance of an EndDocument event
 427    * @return an EndDocument event
 428    */
 429   public abstract EndDocument createEndDocument();
 430 
 431   /** Creates a new instance of a EntityReference event
 432    *
 433    * @param name The name of the reference
 434    * @param declaration the declaration for the event
 435    * @return an EntityReference event
 436    */
 437   public abstract EntityReference createEntityReference(String name,
 438                                                         EntityDeclaration declaration);
 439   /**
 440    * Create a comment
 441    * @param text The text of the comment
 442    * a Comment event
 443    */
 444   public abstract Comment createComment(String text);
 445 
 446   /**
 447    * Create a processing instruction
 448    * @param target The target of the processing instruction
 449    * @param data The text of the processing instruction
 450    * @return a ProcessingInstruction event
 451    */
 452   public abstract ProcessingInstruction createProcessingInstruction(String target,
 453                                                                    String data);
 454 
 455   /**
 456    * Create a document type definition event
 457    * This string contains the entire document type declaration that matches
 458    * the doctypedecl in the XML 1.0 specification
 459    * @param dtd the text of the document type definition
 460    * @return a DTD event
 461    */
 462   public abstract DTD createDTD(String dtd);
 463 }