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