I'm working on an application that does a lot of XML manipulation, so I'm trying to get JAXB working in it. I have the following class for a CustomEscapeHandler: import com.sun.xml.bind.marshaller.CharacterEscapeHandler;
import java.io.IOException;
import java.io.Writer;
public class CustomEscapeHandler implements CharacterEscapeHandler {
private static final CharacterEscapeHandler theInstance = new CustomEscapeHandler();
private CustomEscapeHandler() {
}
public static CharacterEscapeHandler getInstance() {
return theInstance;
}
public void escape(char[] ch, int start, int length, boolean isAttVal, Writer out) throws IOException {
int limit = start + length;
for(int i = start; i < limit; ++i) {
char c = ch[i];
if (c == 38 || c == 39 || c == 60 || c == 62 || c == 13 || c == 34 && isAttVal) {
if (i != start) {
out.write(ch, start, i - start);
}
start = i + 1;
switch(ch[i]) {
case '\"':
out.write(""");
break;
case '&':
out.write("&");
break;
case '<':
out.write("<");
break;
case '>':
out.write(">");
break;
case '\'':
out.write("'");
break;
}
}
}
if (start != limit) {
out.write(ch, start, limit - start);
}
}
}
If I use
jaxbMarshaller.setProperty("com.sun.xml.internal.bind.characterEscapeHandler", CustomEscapeHandler.getInstance());
I get
javax.xml.bind.PropertyException: property "com.sun.xml.internal.bind.characterEscapeHandler" must be an instance of type com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler, not my.package.CustomEscapeHandler
So if I change it to
jaxbMarshaller.setProperty("com.sun.xml.bind.marshaller.CharacterEscapeHandler", CustomEscapeHandler.getInstance());
I get
javax.xml.bind.PropertyException: name: com.sun.xml.bind.marshaller.CharacterEscapeHandler value: my.package.CustomEscapeHandler@e2b4f3
at javax.xml.bind.helpers.AbstractMarshallerImpl.setProperty(AbstractMarshallerImpl.java:358)
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.setProperty(MarshallerImpl.java:527)
at my.package.util.XmlUtils.convert(XmlUtils.java:39)
at my.package.util.XmlUtils.convertFormToXml(XmlUtils.java:28)
at my.package.FormsMain.saveXML(FormsMain.java:362)
at my.package.FormsMain.saveForms(FormsMain.java:499)
at my.package.FormsMain.lambda$createWindow$3(FormsMain.java:231)
Looking at the stacktrace, it's trying to use the marshaller in the internal packages, which also looking at the decompiled code, is expecting the the escape handler to be an implementation of the CharacterEscapeHandler in the internal package.
How can I get JAXB to not use the internal package implementation?
A month later, still have been unable to find a solution. Every example I see online has extending the internal version of CharacterEscapeHandler.