Package org.owasp.encoder
Class XMLEncoder
- java.lang.Object
-
- org.owasp.encoder.Encoder
-
- org.owasp.encoder.XMLEncoder
-
class XMLEncoder extends Encoder
XMLEncoder -- encoder for XML attribute and content data. It uses XML entity entity ("&...;") to encode valid but significant characters. Characters that are invalid according to the XML specification are replaced by a space character (U+0020). This encoder supports several modes of operation, allowing for varying contexts, such as: attribute data between single-quotes, attribute data between double-quotes, attribute data with indeterminate quotes, content, or a context safe for all of the above.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
XMLEncoder.Mode
An enum of supported "modes" of operation for the XMLEncoder.
-
Field Summary
Fields Modifier and Type Field Description private XMLEncoder.Mode
_mode
The mode of operation--only really stored to provide a relevant toString implementation.private long
_validMask
The mask of valid characters extracted from the mode for efficiency.(package private) static int
AMP_LENGTH
The encoded length of an ampersand.(package private) static int
APOS_LENGTH
The encoded length of an apostrophe.private static long
BASE_VALID_MASK
A bit-mask of valid characters with code-points in the range 0--63.(package private) static int
GT_LENGTH
The encoded length of a greater-than sign.(package private) static char
INVALID_CHARACTER_REPLACEMENT
Character to use as a replacement for invalid characters (Not to be confused with characters that require encoding).(package private) static int
LT_LENGTH
The encoded length of a less-than sign.(package private) static int
MAX_ENCODED_CHAR_LENGTH
Maximum number of encoded characters per input character.(package private) static int
QUOT_LENGTH
The encoded length of a double-quotation character.
-
Constructor Summary
Constructors Constructor Description XMLEncoder()
Default constructor--equivalent to XMLEncoder(Mode.ALL).XMLEncoder(XMLEncoder.Mode mode)
Creates an XMLEncoder for the specified mode constant.
-
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.int
firstEncodedOffset(java.lang.String input, int off, int len)
Scans the input string for the first character index that requires encoding.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
-
BASE_VALID_MASK
private static final long BASE_VALID_MASK
A bit-mask of valid characters with code-points in the range 0--63.- See Also:
- Constant Field Values
-
MAX_ENCODED_CHAR_LENGTH
static final int MAX_ENCODED_CHAR_LENGTH
Maximum number of encoded characters per input character.- See Also:
- Constant Field Values
-
AMP_LENGTH
static final int AMP_LENGTH
The encoded length of an ampersand.- See Also:
- Constant Field Values
-
LT_LENGTH
static final int LT_LENGTH
The encoded length of a less-than sign.- See Also:
- Constant Field Values
-
GT_LENGTH
static final int GT_LENGTH
The encoded length of a greater-than sign.- See Also:
- Constant Field Values
-
APOS_LENGTH
static final int APOS_LENGTH
The encoded length of an apostrophe.- See Also:
- Constant Field Values
-
QUOT_LENGTH
static final int QUOT_LENGTH
The encoded length of a double-quotation character.- See Also:
- Constant Field Values
-
INVALID_CHARACTER_REPLACEMENT
static final char INVALID_CHARACTER_REPLACEMENT
Character to use as a replacement for invalid characters (Not to be confused with characters that require encoding). Invalid characters have no encoding, and are not allowed in the context.- See Also:
- Constant Field Values
-
_validMask
private final long _validMask
The mask of valid characters extracted from the mode for efficiency.
-
_mode
private final XMLEncoder.Mode _mode
The mode of operation--only really stored to provide a relevant toString implementation.
-
-
Constructor Detail
-
XMLEncoder
XMLEncoder()
Default constructor--equivalent to XMLEncoder(Mode.ALL).
-
XMLEncoder
XMLEncoder(XMLEncoder.Mode mode)
Creates an XMLEncoder for the specified mode constant.- Parameters:
mode
- the mode of the encoder.
-
-
Method Detail
-
maxEncodedLength
public 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
public 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)
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
-
-