Package org.owasp.encoder
Class CDATAEncoder
- java.lang.Object
-
- org.owasp.encoder.Encoder
-
- org.owasp.encoder.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.
-
-
Field Summary
Fields Modifier and Type Field Description private static char[]
CDATA_END_ENCODED
The encoding of @{code "]]>"}.private static int
CDATA_END_ENCODED_LENGTH
Length of"]]]]><![CDATA[>"
.private static int
CDATA_END_LENGTH
Length of"]]>"
.
-
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 ofn
characters.java.lang.String
toString()
-
Methods inherited from class org.owasp.encoder.Encoder
encode, encodeBuffers, overflow, underflow
-
-
-
-
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
-
-
Method Detail
-
maxEncodedLength
protected int maxEncodedLength(int n)
Description copied from class:Encoder
Returns the maximum encoded length (in chars) of an input sequence ofn
characters.- Specified by:
maxEncodedLength
in classEncoder
- 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 classEncoder
- Parameters:
input
- the input to check for encodingoff
- the offset of the first character to checklen
- 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 classEncoder
- 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 classjava.lang.Object
-
-