XMLEncoder should never produce "]]>"

Issue #621 resolved
Chase Tingley created an issue

The following test code will fail:

        StringBuilder sb = new StringBuilder();
        sb.append(enc.encode(']', EncoderContext.TEXT));
        sb.append(enc.encode(']', EncoderContext.TEXT));
        sb.append(enc.encode('>', EncoderContext.TEXT));
        assertEquals("]]>", sb.toString());

The XMLEncoder will write out ]]> instead. This is invalid XML (that char sequence is only allowed when terminating a CDATA section. It's also inconsistent behavior within the encoder itself, because this passes:

        assertEquals("]]>", enc.encode("]]>", EncoderContext.TEXT));

The XMLEncoder probably needs to track state across calls to encode(char), so it can provide consistent and correct behavior. ie, in all cases calling encode(String) should be equivalent to calling encode(char) on each char of the String in succession.

Comments (4)

  1. Chase Tingley reporter

    Fix issue #621: XMLEncoder should never produce "]>"

    This makes the XMLEncoder stateful and refactors everything to fully
    use an internal _encode() method that works on characters.
    

    → <<cset 2d0a31bc2b6e>>

  2. Log in to comment