|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
package com.sun.xml.internal.stream.writers; |
|
|
|
import java.util.Iterator; |
|
import javax.xml.namespace.NamespaceContext; |
|
import javax.xml.namespace.QName; |
|
import javax.xml.stream.XMLEventReader; |
|
import javax.xml.stream.XMLEventWriter; |
|
import javax.xml.stream.XMLStreamException; |
|
import javax.xml.stream.XMLStreamWriter; |
|
import javax.xml.stream.events.Attribute; |
|
import javax.xml.stream.events.Characters; |
|
import javax.xml.stream.events.Comment; |
|
import javax.xml.stream.events.DTD; |
|
import javax.xml.stream.events.EntityReference; |
|
import javax.xml.stream.events.Namespace; |
|
import javax.xml.stream.events.ProcessingInstruction; |
|
import javax.xml.stream.events.StartDocument; |
|
import javax.xml.stream.events.StartElement; |
|
import javax.xml.stream.events.XMLEvent; |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public class XMLEventWriterImpl implements XMLEventWriter { |
|
|
|
|
|
private final XMLStreamWriterBase fStreamWriter; |
|
private static final boolean DEBUG = false; |
|
|
|
|
|
|
|
|
|
*/ |
|
public XMLEventWriterImpl(XMLStreamWriter streamWriter) { |
|
fStreamWriter = (XMLStreamWriterBase) streamWriter; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void add(XMLEventReader xMLEventReader) throws XMLStreamException { |
|
if (xMLEventReader == null) { |
|
throw new XMLStreamException("Event reader shouldn't be null"); |
|
} |
|
while (xMLEventReader.hasNext()) { |
|
add(xMLEventReader.nextEvent()); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void add(XMLEvent xMLEvent) throws XMLStreamException { |
|
int type = xMLEvent.getEventType(); |
|
switch (type) { |
|
case XMLEvent.DTD: { |
|
DTD dtd = (DTD) xMLEvent; |
|
if (DEBUG) { |
|
System.out.println("Adding DTD = " + dtd.toString()); |
|
} |
|
fStreamWriter.writeDTD(dtd.getDocumentTypeDeclaration()); |
|
break; |
|
} |
|
case XMLEvent.START_DOCUMENT: { |
|
StartDocument startDocument = (StartDocument) xMLEvent; |
|
if (DEBUG) { |
|
System.out.println("Adding StartDocument = " + startDocument.toString()); |
|
} |
|
try { |
|
fStreamWriter.writeStartDocument(startDocument.getCharacterEncodingScheme(), |
|
startDocument.getVersion(), |
|
startDocument.isStandalone(), startDocument.standaloneSet()); |
|
} catch (XMLStreamException e) { |
|
fStreamWriter.writeStartDocument(startDocument.getVersion()); |
|
} |
|
break; |
|
} |
|
case XMLEvent.START_ELEMENT: { |
|
StartElement startElement = xMLEvent.asStartElement(); |
|
if (DEBUG) { |
|
System.out.println("Adding startelement = " + startElement.toString()); |
|
} |
|
QName qname = startElement.getName(); |
|
fStreamWriter.writeStartElement(qname.getPrefix(), qname.getLocalPart(), |
|
qname.getNamespaceURI()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
Iterator<? extends Namespace> iterator = startElement.getNamespaces(); |
|
while (iterator.hasNext()) { |
|
Namespace namespace = iterator.next(); |
|
fStreamWriter.writeNamespace(namespace.getPrefix(), namespace.getNamespaceURI()); |
|
} |
|
|
|
Iterator<? extends Attribute> attributes = startElement.getAttributes(); |
|
while (attributes.hasNext()) { |
|
Attribute attribute = attributes.next(); |
|
QName aqname = attribute.getName(); |
|
fStreamWriter.writeAttribute(aqname.getPrefix(), aqname.getNamespaceURI(), |
|
aqname.getLocalPart(), attribute.getValue()); |
|
} |
|
break; |
|
} |
|
case XMLEvent.NAMESPACE: { |
|
Namespace namespace = (Namespace) xMLEvent; |
|
if (DEBUG) { |
|
System.out.println("Adding namespace = " + namespace.toString()); |
|
} |
|
fStreamWriter.writeNamespace(namespace.getPrefix(), namespace.getNamespaceURI()); |
|
break; |
|
} |
|
case XMLEvent.COMMENT: { |
|
Comment comment = (Comment) xMLEvent; |
|
if (DEBUG) { |
|
System.out.println("Adding comment = " + comment.toString()); |
|
} |
|
fStreamWriter.writeComment(comment.getText()); |
|
break; |
|
} |
|
case XMLEvent.PROCESSING_INSTRUCTION: { |
|
ProcessingInstruction processingInstruction = (ProcessingInstruction) xMLEvent; |
|
if (DEBUG) { |
|
System.out.println("Adding processing instruction = " + processingInstruction.toString()); |
|
} |
|
fStreamWriter.writeProcessingInstruction(processingInstruction.getTarget(), |
|
processingInstruction.getData()); |
|
break; |
|
} |
|
case XMLEvent.CHARACTERS: { |
|
Characters characters = xMLEvent.asCharacters(); |
|
if (DEBUG) { |
|
System.out.println("Adding characters = " + characters.toString()); |
|
} |
|
|
|
if (characters.isCData()) { |
|
fStreamWriter.writeCData(characters.getData()); |
|
} else { |
|
fStreamWriter.writeCharacters(characters.getData()); |
|
} |
|
break; |
|
} |
|
case XMLEvent.ENTITY_REFERENCE: { |
|
EntityReference entityReference = (EntityReference) xMLEvent; |
|
if (DEBUG) { |
|
System.out.println("Adding Entity Reference = " + entityReference.toString()); |
|
} |
|
fStreamWriter.writeEntityRef(entityReference.getName()); |
|
break; |
|
} |
|
case XMLEvent.ATTRIBUTE: { |
|
Attribute attribute = (Attribute) xMLEvent; |
|
if (DEBUG) { |
|
System.out.println("Adding Attribute = " + attribute.toString()); |
|
} |
|
QName qname = attribute.getName(); |
|
fStreamWriter.writeAttribute(qname.getPrefix(), qname.getNamespaceURI(), |
|
qname.getLocalPart(), attribute.getValue()); |
|
break; |
|
} |
|
case XMLEvent.CDATA: { |
|
//there is no separate CDATA datatype but CDATA event can be reported |
|
|
|
Characters characters = (Characters) xMLEvent; |
|
if (DEBUG) { |
|
System.out.println("Adding characters = " + characters.toString()); |
|
} |
|
if (characters.isCData()) { |
|
fStreamWriter.writeCData(characters.getData()); |
|
} |
|
break; |
|
} |
|
//xxx: Why there isn't any event called Notation. |
|
//case XMLEvent.NOTATION_DECLARATION:{ |
|
//} |
|
|
|
case XMLEvent.END_ELEMENT: { |
|
fStreamWriter.writeEndElement(); |
|
break; |
|
} |
|
case XMLEvent.END_DOCUMENT: { |
|
fStreamWriter.writeEndDocument(); |
|
break; |
|
} |
|
//throw new XMLStreamException("Unknown Event type = " + type); |
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
public void close() throws XMLStreamException { |
|
fStreamWriter.close(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void flush() throws XMLStreamException { |
|
fStreamWriter.flush(); |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
public NamespaceContext getNamespaceContext() { |
|
return fStreamWriter.getNamespaceContext(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public String getPrefix(String namespaceURI) throws XMLStreamException { |
|
return fStreamWriter.getPrefix(namespaceURI); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void setDefaultNamespace(String uri) throws XMLStreamException { |
|
fStreamWriter.setDefaultNamespace(uri); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void setNamespaceContext(NamespaceContext namespaceContext) |
|
throws XMLStreamException { |
|
fStreamWriter.setNamespaceContext(namespaceContext); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void setPrefix(String prefix, String uri) throws XMLStreamException { |
|
fStreamWriter.setPrefix(prefix, uri); |
|
} |
|
|
|
}//XMLEventWriterImpl |