Path: blob/master/src/java.xml/share/classes/javax/xml/stream/XMLEventFactory.java
40948 views
/*1* Copyright (c) 2009, 2021, Oracle and/or its affiliates. All rights reserved.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 it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* 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 WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 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 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.xml.stream;26import com.sun.xml.internal.stream.events.XMLEventFactoryImpl;27import java.util.Iterator;28import javax.xml.namespace.NamespaceContext;29import javax.xml.namespace.QName;30import javax.xml.stream.events.*;31/**32* This interface defines a utility class for creating instances of33* XMLEvents34* @version 1.235* @author Copyright (c) 2009 by Oracle Corporation. All Rights Reserved.36* @see javax.xml.stream.events.StartElement37* @see javax.xml.stream.events.EndElement38* @see javax.xml.stream.events.ProcessingInstruction39* @see javax.xml.stream.events.Comment40* @see javax.xml.stream.events.Characters41* @see javax.xml.stream.events.StartDocument42* @see javax.xml.stream.events.EndDocument43* @see javax.xml.stream.events.DTD44* @since 1.645*/46public abstract class XMLEventFactory {47static final String JAXPFACTORYID = "javax.xml.stream.XMLEventFactory";48static final String DEFAULIMPL = "com.sun.xml.internal.stream.events.XMLEventFactoryImpl";4950/**51* Protected constructor to prevent instantiation.52* Use {@link #newFactory()} instead.53*/54protected XMLEventFactory(){}5556/**57* Creates a new instance of the {@code XMLEventFactory} builtin58* system-default implementation.59*60* @return A new instance of the {@code XMLEventFactory} builtin61* system-default implementation.62*63* @since 964*/65public static XMLEventFactory newDefaultFactory() {66return new XMLEventFactoryImpl();67}6869/**70* Creates a new instance of the factory in exactly the same manner as the71* {@link #newFactory()} method.72*73* @return an instance of the {@code XMLEventFactory}74* @throws FactoryConfigurationError if an instance of this factory cannot be loaded75*/76public static XMLEventFactory newInstance()77throws FactoryConfigurationError78{79return FactoryFinder.find(XMLEventFactory.class, DEFAULIMPL);80}8182/**83* Creates a new instance of the factory. This method uses the84* <a href="../../../module-summary.html#LookupMechanism">JAXP Lookup Mechanism</a>85* to determine the {@code XMLEventFactory} implementation class to load.86* <p>87* Once an application has obtained a reference to a {@code XMLEventFactory}, it88* can use the factory to configure and obtain stream instances.89*90* @return an instance of the {@code XMLEventFactory}91* @throws FactoryConfigurationError in case of {@linkplain92* java.util.ServiceConfigurationError service configuration error} or if93* the implementation is not available or cannot be instantiated.94*/95public static XMLEventFactory newFactory()96throws FactoryConfigurationError97{98return FactoryFinder.find(XMLEventFactory.class, DEFAULIMPL);99}100101/**102* Create a new instance of the factory103*104* @param factoryId Name of the factory to find, same as105* a property name106* @param classLoader classLoader to use107* @return the factory implementation108* @throws FactoryConfigurationError if an instance of this factory cannot be loaded109*110* @deprecated This method has been deprecated to maintain API consistency.111* All newInstance methods have been replaced with corresponding112* newFactory methods. The replacement {@link113* #newFactory(java.lang.String, java.lang.ClassLoader)}114* method defines no changes in behavior.115*/116@Deprecated(since="1.7")117public static XMLEventFactory newInstance(String factoryId,118ClassLoader classLoader)119throws FactoryConfigurationError {120//do not fallback if given classloader can't find the class, throw exception121return FactoryFinder.find(XMLEventFactory.class, factoryId, classLoader, null);122}123124/**125* Create a new instance of the factory.126* If the classLoader argument is null, then the ContextClassLoader is used.127* <p>128* This method uses the following ordered lookup procedure to determine129* the XMLEventFactory implementation class to load:130* <ul>131* <li>132* Use the value of the system property identified by {@code factoryId}.133* </li>134* <li>135* <p>136* Use the configuration file "stax.properties". The file is in standard137* {@link java.util.Properties} format and typically located in the138* conf directory of the Java installation. It contains the fully qualified139* name of the implementation class with the key being the system property140* defined above.141*142* <p>143* The stax.properties file is read only once by the implementation144* and its values are then cached for future use. If the file does not exist145* when the first attempt is made to read from it, no further attempts are146* made to check for its existence. It is not possible to change the value147* of any property in stax.properties after it has been read for the first time.148*149* <p>150* Use the jaxp configuration file "jaxp.properties". The file is in the same151* format as stax.properties and will only be read if stax.properties does152* not exist.153* </li>154* <li>155* <p>156* If {@code factoryId} is "javax.xml.stream.XMLEventFactory",157* use the service-provider loading facility, defined by the158* {@link java.util.ServiceLoader} class, to attempt to {@linkplain159* java.util.ServiceLoader#load(java.lang.Class, java.lang.ClassLoader) locate and load}160* an implementation of the service using the specified {@code ClassLoader}.161* If {@code classLoader} is null, the {@linkplain162* java.util.ServiceLoader#load(java.lang.Class) default loading mechanism} will apply:163* That is, the service-provider loading facility will use the {@linkplain164* java.lang.Thread#getContextClassLoader() current thread's context class loader}165* to attempt to load the service. If the context class166* loader is null, the {@linkplain167* ClassLoader#getSystemClassLoader() system class loader} will be used.168* </li>169* <li>170* <p>171* Otherwise, throws a {@link FactoryConfigurationError}.172* </li>173* </ul>174*175* <p>176* Note that this is a new method that replaces the deprecated177* {@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 relative180* to the deprecated method.181*182* @apiNote The parameter factoryId defined here is inconsistent with that183* of other JAXP factories where the first parameter is fully qualified184* factory class name that provides implementation of the factory.185*186* @param factoryId Name of the factory to find, same as187* a property name188* @param classLoader classLoader to use189* @return the factory implementation190* @throws FactoryConfigurationError in case of {@linkplain191* java.util.ServiceConfigurationError service configuration error} or if192* the implementation is not available or cannot be instantiated.193*/194public static XMLEventFactory newFactory(String factoryId,195ClassLoader classLoader)196throws FactoryConfigurationError {197//do not fallback if given classloader can't find the class, throw exception198return FactoryFinder.find(XMLEventFactory.class, factoryId, classLoader, null);199}200201/**202* This method allows setting of the Location on each event that203* is created by this factory. The values are copied by value into204* the events created by this factory. To reset the location205* information set the location to null.206* @param location the location to set on each event created207*/208public abstract void setLocation(Location location);209210/**211* Create a new Attribute212* @param prefix the prefix of this attribute, may not be null213* @param namespaceURI the attribute value is set to this value, may not be null214* @param localName the local name of the XML name of the attribute, localName cannot be null215* @param value the attribute value to set, may not be null216* @return the Attribute with specified values217*/218public abstract Attribute createAttribute(String prefix, String namespaceURI, String localName, String value);219220/**221* Create a new Attribute222* @param localName the local name of the XML name of the attribute, localName cannot be null223* @param value the attribute value to set, may not be null224* @return the Attribute with specified values225*/226public abstract Attribute createAttribute(String localName, String value);227228/**229* Create a new Attribute230* @param name the qualified name of the attribute, may not be null231* @param value the attribute value to set, may not be null232* @return the Attribute with specified values233*/234public abstract Attribute createAttribute(QName name, String value);235236/**237* Create a new default Namespace238* @param namespaceURI the default namespace uri239* @return the Namespace with the specified value240*/241public abstract Namespace createNamespace(String namespaceURI);242243/**244* Create a new Namespace245* @param prefix the prefix of this namespace, may not be null246* @param namespaceUri the attribute value is set to this value, may not be null247* @return the Namespace with the specified values248*/249public abstract Namespace createNamespace(String prefix, String namespaceUri);250251/**252* Create a new StartElement. Namespaces can be added to this StartElement253* by passing in an Iterator that walks over a set of Namespace interfaces.254* Attributes can be added to this StartElement by passing an iterator255* that walks over a set of Attribute interfaces.256*257* @param name the qualified name of the attribute, may not be null258* @param attributes an optional unordered set of objects that259* implement Attribute to add to the new StartElement, may be null260* @param namespaces an optional unordered set of objects that261* implement Namespace to add to the new StartElement, may be null262* @return an instance of the requested StartElement263*/264public abstract StartElement createStartElement(QName name,265Iterator<? extends Attribute> attributes,266Iterator<? extends Namespace> namespaces);267268/**269* Create a new StartElement. This defaults the NamespaceContext to270* an empty NamespaceContext. Querying this event for its namespaces or271* attributes will result in an empty iterator being returned.272*273* @param namespaceUri the uri of the QName of the new StartElement274* @param localName the local name of the QName of the new StartElement275* @param prefix the prefix of the QName of the new StartElement276* @return an instance of the requested StartElement277*/278public abstract StartElement createStartElement(String prefix,279String namespaceUri,280String localName);281/**282* Create a new StartElement. Namespaces can be added to this StartElement283* by passing in an Iterator that walks over a set of Namespace interfaces.284* Attributes can be added to this StartElement by passing an iterator285* that walks over a set of Attribute interfaces.286*287* @param namespaceUri the uri of the QName of the new StartElement288* @param localName the local name of the QName of the new StartElement289* @param prefix the prefix of the QName of the new StartElement290* @param attributes an unordered set of objects that implement291* Attribute to add to the new StartElement292* @param namespaces an unordered set of objects that implement293* Namespace to add to the new StartElement294* @return an instance of the requested StartElement295*/296public abstract StartElement createStartElement(String prefix,297String namespaceUri,298String localName,299Iterator<? extends Attribute> attributes,300Iterator<? extends Namespace> namespaces301);302/**303* Create a new StartElement. Namespaces can be added to this StartElement304* by passing in an Iterator that walks over a set of Namespace interfaces.305* Attributes can be added to this StartElement by passing an iterator306* that walks over a set of Attribute interfaces.307*308* @param namespaceUri the uri of the QName of the new StartElement309* @param localName the local name of the QName of the new StartElement310* @param prefix the prefix of the QName of the new StartElement311* @param attributes an unordered set of objects that implement312* Attribute to add to the new StartElement, may be null313* @param namespaces an unordered set of objects that implement314* Namespace to add to the new StartElement, may be null315* @param context the namespace context of this element316* @return an instance of the requested StartElement317*/318public abstract StartElement createStartElement(String prefix,319String namespaceUri,320String localName,321Iterator<? extends Attribute> attributes,322Iterator<? extends Namespace> namespaces,323NamespaceContext context324);325326/**327* Create a new EndElement328* @param name the qualified name of the EndElement329* @param namespaces an optional unordered set of objects that330* implement Namespace that have gone out of scope, may be null331* @return an instance of the requested EndElement332*/333public abstract EndElement createEndElement(QName name,334Iterator<? extends Namespace> namespaces);335336/**337* Create a new EndElement338* @param namespaceUri the uri of the QName of the new StartElement339* @param localName the local name of the QName of the new StartElement340* @param prefix the prefix of the QName of the new StartElement341* @return an instance of the requested EndElement342*/343public abstract EndElement createEndElement(String prefix,344String namespaceUri,345String localName);346/**347* Create a new EndElement348* @param namespaceUri the uri of the QName of the new StartElement349* @param localName the local name of the QName of the new StartElement350* @param prefix the prefix of the QName of the new StartElement351* @param namespaces an unordered set of objects that implement352* Namespace that have gone out of scope, may be null353* @return an instance of the requested EndElement354*/355public abstract EndElement createEndElement(String prefix,356String namespaceUri,357String localName,358Iterator<? extends Namespace> namespaces);359360/**361* Create a Characters event, this method does not check if the content362* is all whitespace. To create a space event use #createSpace(String)363* @param content the string to create364* @return a Characters event365*/366public abstract Characters createCharacters(String content);367368/**369* Create a Characters event with the CData flag set to true370* @param content the string to create371* @return a Characters event372*/373public abstract Characters createCData(String content);374375/**376* Create a Characters event with the isSpace flag set to true377* @param content the content of the space to create378* @return a Characters event379*/380public abstract Characters createSpace(String content);381/**382* Create an ignorable space383* @param content the space to create384* @return a Characters event385*/386public abstract Characters createIgnorableSpace(String content);387388/**389* Creates a new instance of a StartDocument event390* @return a StartDocument event391*/392public abstract StartDocument createStartDocument();393394/**395* Creates a new instance of a StartDocument event396*397* @param encoding the encoding style398* @param version the XML version399* @param standalone the status of standalone may be set to "true" or "false"400* @return a StartDocument event401*/402public abstract StartDocument createStartDocument(String encoding,403String version,404boolean standalone);405406/**407* Creates a new instance of a StartDocument event408*409* @param encoding the encoding style410* @param version the XML version411* @return a StartDocument event412*/413public abstract StartDocument createStartDocument(String encoding,414String version);415416/**417* Creates a new instance of a StartDocument event418*419* @param encoding the encoding style420* @return a StartDocument event421*/422public abstract StartDocument createStartDocument(String encoding);423424/**425* Creates a new instance of an EndDocument event426* @return an EndDocument event427*/428public abstract EndDocument createEndDocument();429430/** Creates a new instance of a EntityReference event431*432* @param name The name of the reference433* @param declaration the declaration for the event434* @return an EntityReference event435*/436public abstract EntityReference createEntityReference(String name,437EntityDeclaration declaration);438/**439* Create a comment.440* @param text The text of the comment441* @return a Comment event442*/443public abstract Comment createComment(String text);444445/**446* Create a processing instruction447* @param target The target of the processing instruction448* @param data The text of the processing instruction449* @return a ProcessingInstruction event450*/451public abstract ProcessingInstruction createProcessingInstruction(String target,452String data);453454/**455* Create a document type definition event456* This string contains the entire document type declaration that matches457* the doctypedecl in the XML 1.0 specification458* @param dtd the text of the document type definition459* @return a DTD event460*/461public abstract DTD createDTD(String dtd);462}463464465