Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/com/sun/xml/internal/stream/writers/XMLEventWriterImpl.java
48527 views
/*1* Copyright (c) 2005, 2006, 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 com.sun.xml.internal.stream.writers;2627import java.util.Iterator;28import javax.xml.namespace.QName;29import javax.xml.stream.XMLEventWriter;30import javax.xml.stream.XMLStreamException;31import javax.xml.stream.events.Attribute;32import javax.xml.stream.events.Characters;33import javax.xml.stream.events.Comment;34import javax.xml.stream.events.DTD;35import javax.xml.stream.events.EntityReference;36import javax.xml.stream.events.Namespace;37import javax.xml.stream.events.ProcessingInstruction;38import javax.xml.stream.events.StartDocument;39import javax.xml.stream.events.StartElement;40import javax.xml.stream.events.XMLEvent;41import javax.xml.stream.XMLStreamWriter;4243/**44*45* @author Neeraj Bajaj, Sun Microsystems.46*47*/48public class XMLEventWriterImpl implements XMLEventWriter{4950//delegate everything to XMLStreamWriter..51private XMLStreamWriter fStreamWriter ;52private static final boolean DEBUG = false;53/**54*55* @param streamWriter56*/57public XMLEventWriterImpl(XMLStreamWriter streamWriter){58fStreamWriter = streamWriter;59}6061/**62*63* @param xMLEventReader64* @throws XMLStreamException65*/66public void add(javax.xml.stream.XMLEventReader xMLEventReader) throws javax.xml.stream.XMLStreamException {67if(xMLEventReader == null) throw new XMLStreamException("Event reader shouldn't be null");68while(xMLEventReader.hasNext()){69add(xMLEventReader.nextEvent());70}71}7273/**74*75* @param xMLEvent76* @throws XMLStreamException77*/78public void add(javax.xml.stream.events.XMLEvent xMLEvent) throws javax.xml.stream.XMLStreamException {79int type = xMLEvent.getEventType();80switch(type){81case XMLEvent.DTD:{82DTD dtd = (DTD)xMLEvent ;83if (DEBUG)System.out.println("Adding DTD = " + dtd.toString());84fStreamWriter.writeDTD(dtd.getDocumentTypeDeclaration());85break;86}87case XMLEvent.START_DOCUMENT :{88StartDocument startDocument = (StartDocument)xMLEvent ;89if (DEBUG)System.out.println("Adding StartDocument = " + startDocument.toString());90try {91fStreamWriter.writeStartDocument(startDocument.getCharacterEncodingScheme(), startDocument.getVersion());92}catch(XMLStreamException e) {93fStreamWriter.writeStartDocument(startDocument.getVersion());94}95break;96}97case XMLEvent.START_ELEMENT :{98StartElement startElement = xMLEvent.asStartElement() ;99if (DEBUG)System.out.println("Adding startelement = " + startElement.toString());100QName qname = startElement.getName();101fStreamWriter.writeStartElement(qname.getPrefix(), qname.getLocalPart(), qname.getNamespaceURI());102103//getNamespaces() Returns an Iterator of namespaces declared on this element. This Iterator does not contain104//previously declared namespaces unless they appear on the current START_ELEMENT. Therefore105//this list may contain redeclared namespaces and duplicate namespace declarations. Use the106//getNamespaceContext() method to get the current context of namespace declarations.107108//so we should be using getNamespaces() to write namespace declarations for this START_ELEMENT109Iterator iterator = startElement.getNamespaces();110while(iterator.hasNext()){111Namespace namespace = (Namespace)iterator.next();112fStreamWriter.writeNamespace(namespace.getPrefix(), namespace.getNamespaceURI());113}114//REVISIT: What about writing attributes ?115Iterator attributes = startElement.getAttributes();116while(attributes.hasNext()){117Attribute attribute = (Attribute)attributes.next();118QName aqname = attribute.getName();119fStreamWriter.writeAttribute(aqname.getPrefix(), aqname.getNamespaceURI(), aqname.getLocalPart(),attribute.getValue());120}121break;122}123case XMLEvent.NAMESPACE:{124Namespace namespace = (Namespace)xMLEvent;125if (DEBUG)System.out.println("Adding namespace = " + namespace.toString());126fStreamWriter.writeNamespace(namespace.getPrefix(), namespace.getNamespaceURI());127break ;128}129case XMLEvent.COMMENT: {130Comment comment = (Comment)xMLEvent ;131if (DEBUG)System.out.println("Adding comment = " + comment.toString());132fStreamWriter.writeComment(comment.getText());133break;134}135case XMLEvent.PROCESSING_INSTRUCTION:{136ProcessingInstruction processingInstruction = (ProcessingInstruction)xMLEvent ;137if (DEBUG)System.out.println("Adding processing instruction = " + processingInstruction.toString());138fStreamWriter.writeProcessingInstruction(processingInstruction.getTarget(), processingInstruction.getData());139break;140}141case XMLEvent.CHARACTERS:{142Characters characters = xMLEvent.asCharacters();143if (DEBUG)System.out.println("Adding characters = " + characters.toString());144//check if the CHARACTERS are CDATA145if(characters.isCData()){146fStreamWriter.writeCData(characters.getData());147}148else{149fStreamWriter.writeCharacters(characters.getData());150}151break;152}153case XMLEvent.ENTITY_REFERENCE:{154EntityReference entityReference = (EntityReference)xMLEvent ;155if (DEBUG)System.out.println("Adding Entity Reference = "+ entityReference.toString());156fStreamWriter.writeEntityRef(entityReference.getName());157break;158}159case XMLEvent.ATTRIBUTE:{160Attribute attribute = (Attribute)xMLEvent;161if (DEBUG)System.out.println("Adding Attribute = " + attribute.toString());162QName qname = attribute.getName();163fStreamWriter.writeAttribute(qname.getPrefix(), qname.getNamespaceURI(), qname.getLocalPart(),attribute.getValue());164break;165}166case XMLEvent.CDATA:{167//there is no separate CDATA datatype but CDATA event can be reported168//by using vendor specific CDATA property.169Characters characters = (Characters)xMLEvent;170if (DEBUG)System.out.println("Adding characters = " + characters.toString());171if(characters.isCData()){172fStreamWriter.writeCData(characters.getData());173}174break;175}176//xxx: Why there isn't any event called Notation.177//case XMLEvent.NOTATION_DECLARATION:{178//}179180case XMLEvent.END_ELEMENT:{181//we dont need to typecast it.. just call writeEndElement() and fStreamWriter will take care of it.182//EndElement endElement = (EndElement)xMLEvent;183fStreamWriter.writeEndElement();184break;185}186case XMLEvent.END_DOCUMENT:{187//we dont need to typecast just call writeEndDocument() and fStreamWriter will take care rest.188//EndDocument endDocument = (EndDocument)xMLEvent;189fStreamWriter.writeEndDocument();190break;191}192//throw new XMLStreamException("Unknown Event type = " + type);193};194195}196197/**198*199* @throws XMLStreamException200*/201public void close() throws javax.xml.stream.XMLStreamException {202fStreamWriter.close();203}204205/**206*207* @throws XMLStreamException will inturn call flush on the stream to which data is being208* written.209*/210public void flush() throws javax.xml.stream.XMLStreamException {211fStreamWriter.flush();212}213214/**215*216* @return217*/218public javax.xml.namespace.NamespaceContext getNamespaceContext() {219return fStreamWriter.getNamespaceContext();220}221222/**223*224* @param namespaceURI Namespace URI225* @throws XMLStreamException226* @return prefix associated with the URI.227*/228public String getPrefix(String namespaceURI) throws javax.xml.stream.XMLStreamException {229return fStreamWriter.getPrefix(namespaceURI);230}231232/**233*234* @param uri Namespace URI235* @throws XMLStreamException236*/237public void setDefaultNamespace(String uri) throws javax.xml.stream.XMLStreamException {238fStreamWriter.setDefaultNamespace(uri);239}240241/**242*243* @param namespaceContext Namespace Context244* @throws XMLStreamException245*/246public void setNamespaceContext(javax.xml.namespace.NamespaceContext namespaceContext) throws javax.xml.stream.XMLStreamException {247fStreamWriter.setNamespaceContext(namespaceContext);248}249250/**251*252* @param prefix namespace prefix associated with the uri.253* @param uri Namespace URI254* @throws XMLStreamException255*/256public void setPrefix(String prefix, String uri) throws javax.xml.stream.XMLStreamException {257fStreamWriter.setPrefix(prefix, uri);258}259260}//XMLEventWriterImpl261262263