Escaping ]]> in MOXy [message #1699857] |
Sat, 27 June 2015 14:20 |
Sh Ee Messages: 1 Registered: June 2015 |
Junior Member |
|
|
Hi!
MOXy(2.6) does not escape ">" in generated XML and the unmarshalling fails in the following example:
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;
public class MoxyEscaping {
@XmlRootElement static class A {
@XmlValue String field = "a]]>b";
}
public static void main(String[] args) throws Exception {
System.setProperty(
"javax.xml.bind.context.factory",
"org.eclipse.persistence.jaxb.JAXBContextFactory");
JAXBContext ctx = JAXBContext.newInstance(A.class);
StringWriter out = new StringWriter();
ctx.createMarshaller().marshal(new A(), out);
ctx.createUnmarshaller().unmarshal(new StringReader(out.toString()));
}
}
Similar code works OK for RI.
Overriding the CharacterEscapeHandler has some cost. Is this a bug?
[Updated on: Mon, 27 July 2015 12:49] Report message to a moderator
|
|
|
Re: Escaping ]]> in MOXy [message #1719307 is a reply to message #1699857] |
Wed, 06 January 2016 23:34 |
Jeff Shern Messages: 2 Registered: January 2016 |
Junior Member |
|
|
Did you ever figure anything out on this?
Digging, I did find that the OutputStreamRecord.java in MOXY does not have any mechanism to add a > symbol. Generally, that's fine, but they do ignore the second half of the standard, where it says it is required when escaping a "]]>"
According to the standard (www.w3.org /TR/2008/REC-xml-20081126/#syntax)
Quote:The right angle bracket (>) may be represented using the string " > ", and MUST, for compatibility, be escaped using either " > " or a character reference when it appears in the string " ]]> " in content, when that string is not marking the end of a CDATA section.
Also, a little strange, is that org.eclipse.persistence.oxm.CharacterEscapeHandler does not implement com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler .. even though they appear to be identical. So you have to use a wrapper class to convert between the two.
I did modify your code, below, to use the NioEscapeHandler that is used in JAXB RI.
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;
import org.eclipse.persistence.jaxb.MarshallerProperties;
import org.eclipse.persistence.oxm.CharacterEscapeHandler;
public class MoxyEscaping {
@XmlRootElement static class A {
@XmlValue String field = "a]]>b";
}
public static void main(String[] args) throws Exception {
System.setProperty(
"javax.xml.bind.context.factory",
"org.eclipse.persistence.jaxb.JAXBContextFactory");
JAXBContext ctx = JAXBContext.newInstance(A.class);
StringWriter out = new StringWriter();
Marshaller m=ctx.createMarshaller();
String Encoding = (String) m.getProperty(Marshaller.JAXB_ENCODING);
CharacterEscapeHandler eh = new dxinittest.NioEscapeHandlerWrapper(Encoding);
m.setProperty(MarshallerProperties.CHARACTER_ESCAPE_HANDLER, eh);
m.marshal(new A(), out);
ctx.createUnmarshaller().unmarshal(new StringReader(out.toString()));
}
static class NioEscapeHandlerWrapper implements org.eclipse.persistence.oxm.CharacterEscapeHandler {
com.sun.xml.internal.bind.marshaller.NioEscapeHandler NEH;
NioEscapeHandlerWrapper(String charSetName) {
NEH = new com.sun.xml.internal.bind.marshaller.NioEscapeHandler(charSetName);
}
@Override
public void escape(char[] buffer, int start, int length, boolean isAttributeValue, Writer out) throws IOException {
NEH.escape(buffer, start, length, isAttributeValue, out);
}
}
}
It's not a fix, but it's a little better then writing your own EscapeHandlerWrapper.
[Updated on: Thu, 07 January 2016 17:15] Report message to a moderator
|
|
|
|
Powered by
FUDForum. Page generated in 0.03520 seconds