Skip to content

Instantly share code, notes, and snippets.

@jbcpollak
Created January 8, 2014 05:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbcpollak/8312151 to your computer and use it in GitHub Desktop.
Save jbcpollak/8312151 to your computer and use it in GitHub Desktop.
An XmlFactory and XmlOutputWriter used to force all fields to be wrapped with CDATA.
package xml;
import java.io.OutputStream;
import java.io.Writer;
import javax.xml.stream.*;
import javax.xml.transform.Result;
import com.fasterxml.aalto.stax.OutputFactoryImpl;
public class CDataXmlOutputFactoryImpl extends XMLOutputFactory {
OutputFactoryImpl f = new OutputFactoryImpl();
public XMLEventWriter createXMLEventWriter(OutputStream out) throws XMLStreamException {
return f.createXMLEventWriter(out);
}
public XMLEventWriter createXMLEventWriter(OutputStream out, String enc) throws XMLStreamException {
return f.createXMLEventWriter(out, enc);
}
public XMLEventWriter createXMLEventWriter(Result result) throws XMLStreamException {
return f.createXMLEventWriter(result);
}
public XMLEventWriter createXMLEventWriter(Writer w) throws XMLStreamException {
return f.createXMLEventWriter(w);
}
public XMLStreamWriter createXMLStreamWriter(OutputStream out) throws XMLStreamException {
return new CDataXmlStreamWriter(f.createXMLStreamWriter(out));
}
public XMLStreamWriter createXMLStreamWriter(OutputStream out, String enc) throws XMLStreamException {
return new CDataXmlStreamWriter(f.createXMLStreamWriter(out, enc));
}
public XMLStreamWriter createXMLStreamWriter(Result result) throws XMLStreamException {
return new CDataXmlStreamWriter(f.createXMLStreamWriter(result));
}
public XMLStreamWriter createXMLStreamWriter(Writer w) throws XMLStreamException {
return new CDataXmlStreamWriter(f.createXMLStreamWriter(w));
}
public Object getProperty(String name) {
return f.getProperty(name);
}
public boolean isPropertySupported(String name) {
return f.isPropertySupported(name);
}
public void setProperty(String name, Object value) {
f.setProperty(name, value);
}
}
package xml;
import javax.xml.namespace.NamespaceContext;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
public class CDataXmlStreamWriter implements XMLStreamWriter {
private XMLStreamWriter w;
public CDataXmlStreamWriter(XMLStreamWriter w) {
this.w = w;
}
public void close() throws XMLStreamException {
w.close();
}
public void flush() throws XMLStreamException {
w.flush();
}
public NamespaceContext getNamespaceContext() {
return w.getNamespaceContext();
}
public String getPrefix(String uri) throws XMLStreamException {
return w.getPrefix(uri);
}
public Object getProperty(String name) throws IllegalArgumentException {
return w.getProperty(name);
}
public void setDefaultNamespace(String uri) throws XMLStreamException {
w.setDefaultNamespace(uri);
}
public void setNamespaceContext(NamespaceContext context) throws XMLStreamException {
w.setNamespaceContext(context);
}
public void setPrefix(String prefix, String uri) throws XMLStreamException {
w.setPrefix(prefix, uri);
}
public void writeAttribute(String prefix, String namespaceURI, String localName, String value) throws XMLStreamException {
w.writeAttribute(prefix, namespaceURI, localName, value);
}
public void writeAttribute(String namespaceURI, String localName, String value) throws XMLStreamException {
w.writeAttribute(namespaceURI, localName, value);
}
public void writeAttribute(String localName, String value) throws XMLStreamException {
w.writeAttribute(localName, value);
}
public void writeCData(String data) throws XMLStreamException {
w.writeCData(data);
}
public void writeCharacters(char[] text, int start, int len) throws XMLStreamException {
w.writeCharacters(text, start, len);
}
// All this code just to override this method
public void writeCharacters(String text) throws XMLStreamException {
w.writeCData(text);
}
public void writeComment(String data) throws XMLStreamException {
w.writeComment(data);
}
public void writeDTD(String dtd) throws XMLStreamException {
w.writeDTD(dtd);
}
public void writeDefaultNamespace(String namespaceURI) throws XMLStreamException {
w.writeDefaultNamespace(namespaceURI);
}
public void writeEmptyElement(String prefix, String localName, String namespaceURI) throws XMLStreamException {
w.writeEmptyElement(prefix, localName, namespaceURI);
}
public void writeEmptyElement(String namespaceURI, String localName) throws XMLStreamException {
w.writeEmptyElement(namespaceURI, localName);
}
public void writeEmptyElement(String localName) throws XMLStreamException {
w.writeEmptyElement(localName);
}
public void writeEndDocument() throws XMLStreamException {
w.writeEndDocument();
}
public void writeEndElement() throws XMLStreamException {
w.writeEndElement();
}
public void writeEntityRef(String name) throws XMLStreamException {
w.writeEntityRef(name);
}
public void writeNamespace(String prefix, String namespaceURI) throws XMLStreamException {
w.writeNamespace(prefix, namespaceURI);
}
public void writeProcessingInstruction(String target, String data) throws XMLStreamException {
w.writeProcessingInstruction(target, data);
}
public void writeProcessingInstruction(String target) throws XMLStreamException {
w.writeProcessingInstruction(target);
}
public void writeStartDocument() throws XMLStreamException {
w.writeStartDocument();
}
public void writeStartDocument(String encoding, String version) throws XMLStreamException {
w.writeStartDocument(encoding, version);
}
public void writeStartDocument(String version) throws XMLStreamException {
w.writeStartDocument(version);
}
public void writeStartElement(String prefix, String localName, String namespaceURI) throws XMLStreamException {
w.writeStartElement(prefix, localName, namespaceURI);
}
public void writeStartElement(String namespaceURI, String localName) throws XMLStreamException {
w.writeStartElement(namespaceURI, localName);
}
public void writeStartElement(String localName) throws XMLStreamException {
w.writeStartElement(localName);
}
}
public void init() {
XmlFactory factory = new XmlFactory(new InputFactoryImpl(),
new CDataXmlOutputFactoryImpl());
xmlMapper = new XmlMapper(factory);
xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment