Hi
On 29/02/12 09:52, Idar Borlaug wrote:
Hi

I am calling a webservice outside of my control. I am being told that
i need to send html tags as Cdata and not> tags for some reason.
Is it possible to force jaxb in cxf to encode a string in CDATA?

I looked at some available recommendations on the web but it appears the simplest and the most flexible solution is to register a custom XMLStreamWriter which will delegate to the default CXF utility writer and only override a couple of methods dealing with writing the characters, and optionally check the current element name if its content should not be wrapped, example:

private static class CDataContentWriter extends DelegatingXMLStreamWriter {

        private String currentElementName;

        public CDataContentWriter(XMLStreamWriter writer) {
            super(writer);
        }


public void writeCharacters(String text) throws XMLStreamException {
              boolean useCData = checkIfCDATAneededForCurrentElement();
              if (useCData) {
                  super.writeCharacters("<[![CDATA[");
              }
              super.write(text);
              if (useCData) {
                  super.writeCharacters("]]");
              }
        }

        // optional
public void writeStartElement(String prefix, String local, String uri) throws XMLStreamException {
            currentElementName = local;
            super.writeStartElement(prefix, local, uri);
        }
}

then from the interceptor or a filer do

XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(outputStream);
message.setContent(XMLStreamWriter.class, new CDataContentWriter(writer));

That should do

Sergey

Reply via email to