aoqi@0: /* aoqi@0: * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.fastinfoset.stax.factory; aoqi@0: aoqi@0: import javax.xml.namespace.QName; aoqi@0: import javax.xml.namespace.NamespaceContext; aoqi@0: import javax.xml.stream.Location; aoqi@0: import javax.xml.stream.XMLEventFactory; aoqi@0: import javax.xml.stream.events.*; aoqi@0: import java.util.Iterator; aoqi@0: import com.sun.xml.internal.fastinfoset.stax.events.*; aoqi@0: aoqi@0: aoqi@0: public class StAXEventFactory extends XMLEventFactory { aoqi@0: Location location = null; aoqi@0: aoqi@0: /** Creates a new instance of StAXEventFactory */ aoqi@0: public StAXEventFactory() { aoqi@0: } aoqi@0: /** aoqi@0: * This method allows setting of the Location on each event that aoqi@0: * is created by this factory. The values are copied by value into aoqi@0: * the events created by this factory. To reset the location aoqi@0: * information set the location to null. aoqi@0: * @param location the location to set on each event created aoqi@0: */ aoqi@0: public void setLocation(Location location) { aoqi@0: this.location = location; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Create a new Attribute aoqi@0: * @param prefix the prefix of this attribute, may not be null aoqi@0: * @param namespaceURI the attribute value is set to this value, may not be null aoqi@0: * @param localName the local name of the XML name of the attribute, localName cannot be null aoqi@0: * @param value the attribute value to set, may not be null aoqi@0: * @return the Attribute with specified values aoqi@0: */ aoqi@0: public Attribute createAttribute(String prefix, String namespaceURI, String localName, String value) { aoqi@0: AttributeBase attr = new AttributeBase(prefix, namespaceURI, localName, value, null); aoqi@0: if(location != null)attr.setLocation(location); aoqi@0: return attr; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Create a new Attribute aoqi@0: * @param localName the local name of the XML name of the attribute, localName cannot be null aoqi@0: * @param value the attribute value to set, may not be null aoqi@0: * @return the Attribute with specified values aoqi@0: */ aoqi@0: public Attribute createAttribute(String localName, String value) { aoqi@0: AttributeBase attr = new AttributeBase(localName, value); aoqi@0: if(location != null)attr.setLocation(location); aoqi@0: return attr; aoqi@0: } aoqi@0: aoqi@0: public Attribute createAttribute(QName name, String value) { aoqi@0: AttributeBase attr = new AttributeBase(name, value); aoqi@0: if(location != null)attr.setLocation(location); aoqi@0: return attr; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Create a new default Namespace aoqi@0: * @param namespaceURI the default namespace uri aoqi@0: * @return the Namespace with the specified value aoqi@0: */ aoqi@0: public Namespace createNamespace(String namespaceURI) { aoqi@0: NamespaceBase event = new NamespaceBase(namespaceURI); aoqi@0: if(location != null)event.setLocation(location); aoqi@0: return event; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Create a new Namespace aoqi@0: * @param prefix the prefix of this namespace, may not be null aoqi@0: * @param namespaceURI the attribute value is set to this value, may not be null aoqi@0: * @return the Namespace with the specified values aoqi@0: */ aoqi@0: public Namespace createNamespace(String prefix, String namespaceURI) { aoqi@0: NamespaceBase event = new NamespaceBase(prefix, namespaceURI); aoqi@0: if(location != null)event.setLocation(location); aoqi@0: return event; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Create a new StartElement. aoqi@0: * @param name the qualified name of the attribute, may not be null aoqi@0: * @param attributes an optional unordered set of objects that aoqi@0: * implement Attribute to add to the new StartElement, may be null aoqi@0: * @param namespaces an optional unordered set of objects that aoqi@0: * implement Namespace to add to the new StartElement, may be null aoqi@0: * @return an instance of the requested StartElement aoqi@0: */ aoqi@0: public StartElement createStartElement(QName name, Iterator attributes, Iterator namespaces) { aoqi@0: return createStartElement(name.getPrefix(), name.getNamespaceURI(), name.getLocalPart(), attributes, namespaces); aoqi@0: } aoqi@0: aoqi@0: public StartElement createStartElement(String prefix, String namespaceUri, String localName) { aoqi@0: StartElementEvent event = new StartElementEvent(prefix, namespaceUri, localName); aoqi@0: if(location != null)event.setLocation(location); aoqi@0: return event; aoqi@0: } aoqi@0: aoqi@0: public StartElement createStartElement(String prefix, String namespaceUri, String localName, Iterator attributes, Iterator namespaces) { aoqi@0: return createStartElement(prefix, namespaceUri, localName, attributes, namespaces, null); aoqi@0: } aoqi@0: aoqi@0: public StartElement createStartElement(String prefix, String namespaceUri, String localName, Iterator attributes, Iterator namespaces, NamespaceContext context) { aoqi@0: StartElementEvent elem = new StartElementEvent(prefix, namespaceUri, localName); aoqi@0: elem.addAttributes(attributes); aoqi@0: elem.addNamespaces(namespaces); aoqi@0: elem.setNamespaceContext(context); aoqi@0: if(location != null)elem.setLocation(location); aoqi@0: return elem; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Create a new EndElement aoqi@0: * @param name the qualified name of the EndElement aoqi@0: * @param namespaces an optional unordered set of objects that aoqi@0: * implement Namespace that have gone out of scope, may be null aoqi@0: * @return an instance of the requested EndElement aoqi@0: */ aoqi@0: public EndElement createEndElement(QName name, Iterator namespaces) { aoqi@0: return createEndElement(name.getPrefix(), name.getNamespaceURI(), name.getLocalPart(), namespaces); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Create a new EndElement aoqi@0: * @param namespaceUri the uri of the QName of the new StartElement aoqi@0: * @param localName the local name of the QName of the new StartElement aoqi@0: * @param prefix the prefix of the QName of the new StartElement aoqi@0: * @return an instance of the requested EndElement aoqi@0: */ aoqi@0: public EndElement createEndElement(String prefix, String namespaceUri, String localName) { aoqi@0: EndElementEvent event = new EndElementEvent(prefix, namespaceUri, localName); aoqi@0: if(location != null)event.setLocation(location); aoqi@0: return event; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Create a new EndElement aoqi@0: * @param namespaceUri the uri of the QName of the new StartElement aoqi@0: * @param localName the local name of the QName of the new StartElement aoqi@0: * @param prefix the prefix of the QName of the new StartElement aoqi@0: * @param namespaces an unordered set of objects that implement aoqi@0: * Namespace that have gone out of scope, may be null aoqi@0: * @return an instance of the requested EndElement aoqi@0: */ aoqi@0: public EndElement createEndElement(String prefix, String namespaceUri, String localName, Iterator namespaces) { aoqi@0: aoqi@0: EndElementEvent event = new EndElementEvent(prefix, namespaceUri, localName); aoqi@0: if(namespaces!=null){ aoqi@0: while(namespaces.hasNext()) aoqi@0: event.addNamespace((Namespace)namespaces.next()); aoqi@0: } aoqi@0: if(location != null)event.setLocation(location); aoqi@0: return event; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Create a Characters event, this method does not check if the content aoqi@0: * is all whitespace. To create a space event use #createSpace(String) aoqi@0: * @param content the string to create aoqi@0: * @return a Characters event aoqi@0: */ aoqi@0: public Characters createCharacters(String content) { aoqi@0: CharactersEvent charEvent = new CharactersEvent(content); aoqi@0: if(location != null)charEvent.setLocation(location); aoqi@0: return charEvent; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Create a Characters event with the CData flag set to true aoqi@0: * @param content the string to create aoqi@0: * @return a Characters event aoqi@0: */ aoqi@0: public Characters createCData(String content) { aoqi@0: CharactersEvent charEvent = new CharactersEvent(content, true); aoqi@0: if(location != null)charEvent.setLocation(location); aoqi@0: return charEvent; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Create a Characters event with the isSpace flag set to true aoqi@0: * @param content the content of the space to create aoqi@0: * @return a Characters event aoqi@0: */ aoqi@0: public Characters createSpace(String content) { aoqi@0: CharactersEvent event = new CharactersEvent(content); aoqi@0: event.setSpace(true); aoqi@0: if(location != null)event.setLocation(location); aoqi@0: return event; aoqi@0: } aoqi@0: /** aoqi@0: * Create an ignorable space aoqi@0: * @param content the space to create aoqi@0: * @return a Characters event aoqi@0: */ aoqi@0: public Characters createIgnorableSpace(String content) { aoqi@0: CharactersEvent event = new CharactersEvent(content, false); aoqi@0: event.setSpace(true); aoqi@0: event.setIgnorable(true); aoqi@0: if(location != null)event.setLocation(location); aoqi@0: return event; aoqi@0: } aoqi@0: /** aoqi@0: * Creates a new instance of a StartDocument event aoqi@0: * @return a StartDocument event aoqi@0: */ aoqi@0: public StartDocument createStartDocument() { aoqi@0: StartDocumentEvent event = new StartDocumentEvent(); aoqi@0: if(location != null)event.setLocation(location); aoqi@0: return event; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Creates a new instance of a StartDocument event aoqi@0: * aoqi@0: * @param encoding the encoding style aoqi@0: * @return a StartDocument event aoqi@0: */ aoqi@0: public StartDocument createStartDocument(String encoding) { aoqi@0: StartDocumentEvent event = new StartDocumentEvent(encoding); aoqi@0: if(location != null)event.setLocation(location); aoqi@0: return event; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Creates a new instance of a StartDocument event aoqi@0: * aoqi@0: * @param encoding the encoding style aoqi@0: * @param version the XML version aoqi@0: * @return a StartDocument event aoqi@0: */ aoqi@0: public StartDocument createStartDocument(String encoding, String version) { aoqi@0: StartDocumentEvent event = new StartDocumentEvent(encoding, version); aoqi@0: if(location != null)event.setLocation(location); aoqi@0: return event; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Creates a new instance of a StartDocument event aoqi@0: * aoqi@0: * @param encoding the encoding style aoqi@0: * @param version the XML version aoqi@0: * @param standalone the status of standalone may be set to "true" or "false" aoqi@0: * @return a StartDocument event aoqi@0: */ aoqi@0: public StartDocument createStartDocument(String encoding, String version, boolean standalone) { aoqi@0: StartDocumentEvent event = new StartDocumentEvent(encoding, version); aoqi@0: event.setStandalone(standalone); aoqi@0: if(location != null)event.setLocation(location); aoqi@0: return event; aoqi@0: } aoqi@0: aoqi@0: public EndDocument createEndDocument() { aoqi@0: EndDocumentEvent event =new EndDocumentEvent(); aoqi@0: if(location != null)event.setLocation(location); aoqi@0: return event; aoqi@0: } aoqi@0: aoqi@0: /** Creates a new instance of a EntityReference event aoqi@0: * aoqi@0: * @param name The name of the reference aoqi@0: * @param entityDeclaration the declaration for the event aoqi@0: * @return an EntityReference event aoqi@0: */ aoqi@0: public EntityReference createEntityReference(String name, EntityDeclaration entityDeclaration) { aoqi@0: EntityReferenceEvent event = new EntityReferenceEvent(name, entityDeclaration); aoqi@0: if(location != null)event.setLocation(location); aoqi@0: return event; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Create a comment aoqi@0: * @param text The text of the comment aoqi@0: * a Comment event aoqi@0: */ aoqi@0: public Comment createComment(String text) { aoqi@0: CommentEvent charEvent = new CommentEvent(text); aoqi@0: if(location != null)charEvent.setLocation(location); aoqi@0: return charEvent; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Create a document type definition event aoqi@0: * This string contains the entire document type declaration that matches aoqi@0: * the doctypedecl in the XML 1.0 specification aoqi@0: * @param dtd the text of the document type definition aoqi@0: * @return a DTD event aoqi@0: */ aoqi@0: public DTD createDTD(String dtd) { aoqi@0: DTDEvent dtdEvent = new DTDEvent(dtd); aoqi@0: if(location != null)dtdEvent.setLocation(location); aoqi@0: return dtdEvent; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Create a processing instruction aoqi@0: * @param target The target of the processing instruction aoqi@0: * @param data The text of the processing instruction aoqi@0: * @return a ProcessingInstruction event aoqi@0: */ aoqi@0: public ProcessingInstruction createProcessingInstruction(String target, String data) { aoqi@0: ProcessingInstructionEvent event = new ProcessingInstructionEvent(target, data); aoqi@0: if(location != null)event.setLocation(location); aoqi@0: return event; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: aoqi@0: aoqi@0: aoqi@0: }