jaxb-v2 icon indicating copy to clipboard operation
jaxb-v2 copied to clipboard

The default UTF CharacterEscapeHandler (MinimumEscapeHandler) eats carriage returns

Open glassfishrobot opened this issue 12 years ago • 5 comments

For some reason the default UTF CharacterEscapeHandler (MinimumEscapeHandler) eats carriage returns ("\r"), i.e. it doesn't write them to the output. NioEscapeHandler, an alternate CharacterEscapeHandler included in JAXB, doesn't ignore them. Here's the relevant code:

public void escape(char[] ch, int start, int length, boolean isAttVal, Writer out) throws IOException {
        // avoid calling the Writerwrite method too much by assuming
        // that the escaping occurs rarely.
        // profiling revealed that this is faster than the naive code.
        int limit = start+length;
        for (int i = start; i < limit; i++) {
            char c = ch[i];
if(c == '&' || c == '<' || c == '>' || c == '\r' || (c == '\"' && isAttVal) ) {
if(i!=start)
    out.write(ch,start,i-start);
start = i+1;
switch (ch[i]) {
    case '&':
        out.write("&amp;");
        break;
    case '<':
        out.write("&lt;");
        break;
    case '>':
        out.write("&gt;");
        break;
    case '\"':
        out.write("&quot;");
        break;
}
            }
        }

        if( start!=limit )
            out.write(ch,start,limit-start);
    }

Affected Versions

[2.2.6]

glassfishrobot avatar May 13 '13 15:05 glassfishrobot

Reported by gredler

glassfishrobot avatar May 13 '13 15:05 glassfishrobot

gredler said: Possibly relevant past issue (though I can't find a record in SVN of how it was addressed in the code): #449

glassfishrobot avatar May 13 '13 15:05 glassfishrobot

Was assigned to yaroska

glassfishrobot avatar May 13 '13 15:05 glassfishrobot

This issue was imported from java.net JIRA JAXB-959

glassfishrobot avatar Apr 24 '17 12:04 glassfishrobot

This happens for example when you marshall with JAXB to a StringWriter. This impacts the entire JAXB ecosystem: Java, JavaEE, JAX-RS, and all the projects depending on jaxb-core. And nobody notices that their XMLs get modified...

LucCappellaro avatar Jun 19 '18 08:06 LucCappellaro