SimpleXMLEventWriter implements XMLEventWriter { @Override public void add(XMLEvent event) throws XMLStreamException { switch (event.getEventType()) { case XMLStreamConstants.ATTRIBUTE: Attribute attribute = (Attribute) event; QName attrName = attribute.getName(); delegate.writeAttribute(attrName.getPrefix(), attrName.getNamespaceURI(), attrName.getLocalPart(), attribute.getValue()); break; case XMLStreamConstants.END_DOCUMENT: delegate.writeEndDocument(); break; case XMLStreamConstants.END_ELEMENT: delegate.writeEndElement(); break; case XMLStreamConstants.NAMESPACE: Namespace namespace = (Namespace) event; delegate.writeNamespace(namespace.getPrefix(), namespace.getNamespaceURI()); break; case XMLStreamConstants.START_DOCUMENT: StartDocument startDocument = (StartDocument) event; if (startDocument.encodingSet()) { delegate.writeStartDocument(startDocument.getCharacterEncodingScheme(), startDocument.getVersion()); } else { delegate.writeStartDocument(startDocument.getVersion()); } break; case XMLStreamConstants.START_ELEMENT: StartElement startElement = event.asStartElement(); QName elemName = startElement.getName(); delegate.writeStartElement(elemName.getPrefix(), elemName.getLocalPart(), elemName.getNamespaceURI()); Iterator<?> namespaces = startElement.getNamespaces(); while (namespaces.hasNext()) { add((Namespace)namespaces.next()); } Iterator<?> attributes = startElement.getAttributes(); while (attributes.hasNext()) { add((Attribute)attributes.next()); } break; case XMLStreamConstants.CHARACTERS: case XMLStreamConstants.CDATA: Characters characters = event.asCharacters(); if (characters.isCData()) { delegate.writeCData(characters.getData()); } else { delegate.writeCharacters(characters.getData()); } break; case XMLStreamConstants.COMMENT: delegate.writeComment(((Comment) event).getText()); break; case XMLStreamConstants.DTD: delegate.writeDTD(((DTD) event).getDocumentTypeDeclaration()); break; case XMLStreamConstants.ENTITY_REFERENCE: delegate.writeEntityRef(((EntityReference) event).getName()); break; case XMLStreamConstants.PROCESSING_INSTRUCTION: ProcessingInstruction processingInstruction = (ProcessingInstruction) event; delegate.writeProcessingInstruction(processingInstruction.getTarget(), processingInstruction.getData()); break; case XMLStreamConstants.SPACE: break; default: throw new XMLStreamException("Cannot write event " + event); } } SimpleXMLEventWriter(XMLStreamWriter delegate); @Override void add(XMLEvent event); @Override void add(XMLEventReader reader); @Override void close(); @Override void flush(); @Override NamespaceContext getNamespaceContext(); @Override String getPrefix(String uri); @Override void setDefaultNamespace(String uri); @Override void setNamespaceContext(NamespaceContext ctxt); @Override void setPrefix(String prefix, String uri);  }
@Test public void testStartEndDocument() throws XMLStreamException { StringWriter stringWriter = new StringWriter(); String xml = "<?xml version=\"1.0\"?>"; createXmlEventWriter(stringWriter).add(createXmlEventReader(xml)); Assert.assertEquals(xml, stringWriter.toString()); }
@Test public void testStartEndElement() throws XMLStreamException { StringWriter stringWriter = new StringWriter(); createXmlEventWriter(stringWriter).add(createXmlEventReader("<alice/>")); Assert.assertEquals("<alice></alice>", stringWriter.toString()); stringWriter = new StringWriter(); createXmlEventWriter(stringWriter).add(createXmlEventReader("<alice></alice>")); Assert.assertEquals("<alice></alice>", stringWriter.toString()); }
@Test public void testCharactersCData() throws XMLStreamException { StringWriter stringWriter = new StringWriter(); createXmlEventWriter(stringWriter).add(createXmlEventReader("<alice>bob</alice>")); Assert.assertEquals("<alice>bob</alice>", stringWriter.toString()); stringWriter = new StringWriter(); createXmlEventWriter(stringWriter).add(createXmlEventReader("<alice><![CDATA[bob]]></alice>")); Assert.assertEquals("<alice><![CDATA[bob]]></alice>", stringWriter.toString()); }
@Test public void testAttributeNamespace() throws XMLStreamException { StringWriter stringWriter = new StringWriter(); createXmlEventWriter(stringWriter).add(createXmlEventReader("<alice xmlns=\"http: Assert.assertEquals("<alice xmlns=\"http: }
@Test public void testComment() throws XMLStreamException { StringWriter stringWriter = new StringWriter(); createXmlEventWriter(stringWriter).add(createXmlEventReader("<!--james-->")); Assert.assertEquals("<!--james-->", stringWriter.toString()); }
@Test public void testProcessingInstruction() throws XMLStreamException { StringWriter stringWriter = new StringWriter(); createXmlEventWriter(stringWriter).add(createXmlEventReader("<?joe?>")); Assert.assertEquals("<?joe?>", stringWriter.toString()); }