Class CDATAEncoder


  • class CDATAEncoder
    extends Encoder
    CDATAEncoder -- encoder for CDATA sections. CDATA sections are generally good for including large blocks of text that contain characters that normally require encoding (ampersand, quotes, less-than, etc...). The CDATA context however still does not allow invalid characters, and can be closed by the sequence "]]>". This encoder removes invalid XML characters, and encodes "]]>" (to "]]]]><![CDATA[>"). The result is that the data integrity is maintained, but the code receiving the output will have to handle multiple CDATA events. As an alternate approach, the caller could pre-encode "]]>" to something of their choosing (e.g. data.replaceAll("\\]\\]>", "]] >")), then use this encoder to remove any invalid XML characters.
    • Constructor Summary

      Constructors 
      Constructor Description
      CDATAEncoder()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected java.nio.charset.CoderResult encodeArrays​(java.nio.CharBuffer input, java.nio.CharBuffer output, boolean endOfInput)
      The core encoding loop used when both the input and output buffers are array backed.
      protected int firstEncodedOffset​(java.lang.String input, int off, int len)
      Scans the input string for the first character index that requires encoding.
      protected int maxEncodedLength​(int n)
      Returns the maximum encoded length (in chars) of an input sequence of n characters.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • CDATA_END_ENCODED

        private static final char[] CDATA_END_ENCODED
        The encoding of @{code "]]>"}.
      • CDATA_END_ENCODED_LENGTH

        private static final int CDATA_END_ENCODED_LENGTH
        Length of "]]]]><![CDATA[>".
        See Also:
        Constant Field Values
      • CDATA_END_LENGTH

        private static final int CDATA_END_LENGTH
        Length of "]]>".
        See Also:
        Constant Field Values
    • Constructor Detail

      • CDATAEncoder

        CDATAEncoder()
    • Method Detail

      • maxEncodedLength

        protected int maxEncodedLength​(int n)
        Description copied from class: Encoder
        Returns the maximum encoded length (in chars) of an input sequence of n characters.
        Specified by:
        maxEncodedLength in class Encoder
        Parameters:
        n - the number of characters of input
        Returns:
        the worst-case number of characters required to encode
      • firstEncodedOffset

        protected int firstEncodedOffset​(java.lang.String input,
                                         int off,
                                         int len)
        Description copied from class: Encoder
        Scans the input string for the first character index that requires encoding. If the entire input does not require encoding then the length is returned. This method is used by the Encode.forXYZ methods to return input strings unchanged when possible.
        Specified by:
        firstEncodedOffset in class Encoder
        Parameters:
        input - the input to check for encoding
        off - the offset of the first character to check
        len - the number of characters to check
        Returns:
        the index of the first character to encode. The return value will be off+len if no characters in the input require encoding.
      • encodeArrays

        protected java.nio.charset.CoderResult encodeArrays​(java.nio.CharBuffer input,
                                                            java.nio.CharBuffer output,
                                                            boolean endOfInput)
        Description copied from class: Encoder
        The core encoding loop used when both the input and output buffers are array backed. The loop is expected to fetch the arrays and interact with the arrays directly for performance.
        Overrides:
        encodeArrays in class Encoder
        Parameters:
        input - the input buffer.
        output - the output buffer.
        endOfInput - when true, this is the last input to encode
        Returns:
        UNDERFLOW or OVERFLOW
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object