Prev Class | Next Class | Frames | No Frames |
Summary: Nested | Field | Method | Constr | Detail: Nested | Field | Method | Constr |
java.lang.Object
au.id.jericho.lib.html.Segment
au.id.jericho.lib.html.CharacterReference
public abstract class CharacterReference
extends Segment
CharacterEntityReference
and NumericCharacterReference
.
This class, together with its subclasses, contains static methods to perform most required operations
without having to instantiate an object.
Instances of this class are useful when the positions of character references in a source document are required,
or to replace the found character references with customised text.
CharacterReference
instances are obtained using one of the following methods:
CharacterReference.parse(CharSequence characterReferenceText)
Source.findNextCharacterReference(int pos)
Source.findPreviousCharacterReference(int pos)
Segment.findAllCharacterReferences()
Field Summary | |
static int |
|
Method Summary | |
static String |
|
static String |
|
static String |
|
static String |
|
static String | |
static String |
|
char |
|
abstract String |
|
static String |
|
int |
|
static int |
|
String |
|
static String |
|
static Writer |
|
String |
|
static String |
|
String |
|
static String |
|
boolean |
|
static CharacterReference |
|
static String | |
static boolean |
|
Methods inherited from class au.id.jericho.lib.html.Segment | |
charAt , compareTo , encloses , encloses , equals , extractText , extractText , findAllCharacterReferences , findAllElements , findAllElements , findAllElements , findAllElements , findAllStartTags , findAllStartTags , findAllStartTags , findAllTags , findAllTags , findFormControls , findFormFields , getBegin , getChildElements , getDebugInfo , getEnd , getRenderer , getTextExtractor , hashCode , ignoreWhenParsing , isWhiteSpace , isWhiteSpace , length , parseAttributes , subSequence , toString |
public static final int INVALID_CODE_POINT
Represents an invalid unicode code point. This can be the result of parsing a numeric character reference outside of the valid unicode range of 0x000000-0x10FFFF, or any other invalid character reference.
- Field Value:
- -1
public static String decode(CharSequence encodedText)
Decodes the specified HTML encoded text into normal text. All character entity references and numeric character references are converted to their respective characters. This is equivalent todecode(encodedText,false)
. Unterminated character references are dealt with according to the rules for text outside of attribute values in the current compatibility mode. Although character entity reference names are case sensitive, and in some cases differ from other entity references only by their case, some browsers also recognise them in a case-insensitive way. For this reason, all decoding methods in this library recognise character entity reference names even if they are in the wrong case.
- Parameters:
encodedText
- the text to decode.
- Returns:
- the decoded string.
- See Also:
encode(CharSequence)
public static String decode(CharSequence encodedText, boolean insideAttributeValue)
Decodes the specified HTML encoded text into normal text. All character entity references and numeric character references are converted to their respective characters. Unterminated character references are dealt with according to the value of theinsideAttributeValue
parameter and the current compatibility mode. Although character entity reference names are case sensitive, and in some cases differ from other entity references only by their case, some browsers also recognise them in a case-insensitive way. For this reason, all decoding methods in this library recognise character entity reference names even if they are in the wrong case.
- Parameters:
encodedText
- the text to decode.insideAttributeValue
- specifies whether the encoded text is inside an attribute value.
- Returns:
- the decoded string.
- See Also:
decode(CharSequence)
,encode(CharSequence)
public static String decodeCollapseWhiteSpace(CharSequence text)
Decodes the specified text after collapsing its white space. All leading and trailing white space is omitted, and any sections of internal white space are replaced by a single space. The result is how the text would normally be rendered by a user agent, assuming it does not contain any tags. Unterminated character references are dealt with according to the rules for text outside of attribute values in the current compatibility mode. See the discussion of theinsideAttributeValue
parameter of thedecode(CharSequence, boolean insideAttributeValue)
method for a more detailed explanation of this topic.
- Parameters:
text
- the source text
- Returns:
- the decoded text with collapsed white space.
- See Also:
FormControl.getPredefinedValues()
public static String encode(CharSequence unencodedText)
Encodes the specified text, escaping special characters into character references. Each character is encoded only if therequiresEncoding(char)
method would returntrue
for that character, using itsCharacterEntityReference
if available, or a decimalNumericCharacterReference
if its unicode code point is greater than U+007F. The only exception to this is an apostrophe (U+0027), which depending on the current setting of the staticConfig.IsApostropheEncoded
property, is either left unencoded (default setting), or encoded as the numeric character reference "'
". This method never encodes an apostrophe into its character entity referenceCharacterEntityReference._apos '
as this entity is not defined for use in HTML. See the comments in theCharacterEntityReference
class for more information. To encode text using only numeric character references, use the
NumericCharacterReference.encode(CharSequence)
method instead.
- Parameters:
unencodedText
- the text to encode.
- Returns:
- the encoded string.
- See Also:
decode(CharSequence)
public static String encode(char ch)
Encodes the specified character into a character reference if required. The encoding of the character follows the same rules as for each character in theencode(CharSequence unencodedText)
method.
- Parameters:
ch
- the character to encode.
- Returns:
- a character reference if appropriate, otherwise a string containing the original character.
public static String encodeWithWhiteSpaceFormatting(CharSequence unencodedText)
Encodes the specified text, preserving line breaks, tabs and spaces for rendering by converting them to markup. This performs the same encoding as theencode(CharSequence)
method, but also performs the following conversions:The conversion of multiple consecutive spaces to alternating space/non-breaking-space allows the correct number of spaces to be rendered, but also allows the line to wrap in the middle of it. Note that zero-width spaces (U+200B) are converted to the numeric character reference "
- Line breaks, being Carriage Return (U+000D) or Line Feed (U+000A) characters, and Form Feed characters (U+000C) are converted to "
<br />
". CR/LF pairs are treated as a single line break.- Multiple consecutive spaces are converted so that every second space is converted to "
" while ensuring the last is always a normal space.- Tab characters (U+0009) are converted as if they were four consecutive spaces.
​
" through the normal encoding process, but IE6 does not render them properly either encoded or unencoded. There is no method provided to reverse this encoding.
- Parameters:
unencodedText
- the text to encode.
- Returns:
- the encoded string with whitespace formatting converted to markup.
- See Also:
encode(CharSequence)
public char getChar()
Returns the character represented by this character reference. If this character reference represents a unicode supplimentary code point, any bits outside of the least significant 16 bits of the code point are truncated, yielding an incorrect result.
- Returns:
- the character represented by this character reference.
public abstract String getCharacterReferenceString()
Returns the encoded form of this character reference. The exact behaviour of this method depends on the class of this object. See theCharacterEntityReference.getCharacterReferenceString()
andNumericCharacterReference.getCharacterReferenceString()
methods for more details.
CharacterReference.parse(">").getCharacterReferenceString()
>
CharacterReference.parse(">").getCharacterReferenceString()
e;
- Returns:
- the encoded form of this character reference.
- See Also:
getCharacterReferenceString(int codePoint)
,getDecimalCharacterReferenceString()
public static String getCharacterReferenceString(int codePoint)
Returns the encoded form of the specified unicode code point. This method returns the character entity reference encoded form of the unicode code point if one exists, otherwise it returns the decimal character reference encoded form. The only exception to this is an apostrophe (U+0027), which is encoded as the numeric character reference "'
" instead of its character entity reference "'
".
CharacterReference.getCharacterReferenceString(62)
>
CharacterReference.getCharacterReferenceString('>')
>
CharacterReference.getCharacterReferenceString('☺')
☺
- Parameters:
codePoint
- the unicode code point to encode.
- Returns:
- the encoded form of the specified unicode code point.
- See Also:
getHexadecimalCharacterReferenceString(int codePoint)
public int getCodePoint()
Returns the unicode code point represented by this character reference.
- Returns:
- the unicode code point represented by this character reference.
public static int getCodePointFromCharacterReferenceString(CharSequence characterReferenceText)
Parses a single encoded character reference text into a unicode code point. The character reference must be at the start of the given text, but may contain other characters at the end. If the text does not represent a valid character reference, this method returnsINVALID_CODE_POINT
. This is equivalent toparse(characterReferenceText)
.
getCodePoint()
, except that it returnsINVALID_CODE_POINT
if an invalid character reference is specified instead of throwing aNullPointerException
.
CharacterReference.getCodePointFromCharacterReferenceString(">")
38
- Parameters:
characterReferenceText
- the text containing a single encoded character reference.
- Returns:
- the unicode code point representing representing the specified text, or
INVALID_CODE_POINT
if the text does not represent a valid character reference.
public String getDecimalCharacterReferenceString()
Returns the decimal encoded form of this character reference. This is equivalent togetDecimalCharacterReferenceString
(
getCodePoint()
)
.
CharacterReference.parse(">").getDecimalCharacterReferenceString()
>
- Returns:
- the decimal encoded form of this character reference.
public static String getDecimalCharacterReferenceString(int codePoint)
Returns the decimal encoded form of the specified unicode code point.
CharacterReference.getDecimalCharacterReferenceString('>')
>
- Parameters:
codePoint
- the unicode code point to encode.
- Returns:
- the decimal encoded form of the specified unicode code point.
- See Also:
getCharacterReferenceString(int codePoint)
,getHexadecimalCharacterReferenceString(int codePoint)
public static Writer getEncodingFilterWriter(Writer writer)
- Parameters:
writer
- the destination for the encoded text
- Returns:
- a filter
Writer
that encodes all text before passing it through to the specifiedWriter
.
- See Also:
encode(CharSequence unencodedText)
public String getHexadecimalCharacterReferenceString()
Returns the hexadecimal encoded form of this character reference. This is equivalent togetHexadecimalCharacterReferenceString
(
getCodePoint()
)
.
CharacterReference.parse(">").getHexadecimalCharacterReferenceString()
>
- Returns:
- the hexadecimal encoded form of this character reference.
public static String getHexadecimalCharacterReferenceString(int codePoint)
Returns the hexadecimal encoded form of the specified unicode code point.
CharacterReference.getHexadecimalCharacterReferenceString('>')
>
- Parameters:
codePoint
- the unicode code point to encode.
- Returns:
- the hexadecimal encoded form of the specified unicode code point.
- See Also:
getCharacterReferenceString(int codePoint)
,getDecimalCharacterReferenceString(int codePoint)
public String getUnicodeText()
Returns the unicode code point of this character reference in U+ notation. This is equivalent togetUnicodeText(getCodePoint())
.
CharacterReference.parse(">").getUnicodeText()
U+003E
- Returns:
- the unicode code point of this character reference in U+ notation.
- See Also:
getUnicodeText(int codePoint)
public static String getUnicodeText(int codePoint)
Returns the specified unicode code point in U+ notation.
CharacterReference.getUnicodeText('>')
U+003E
- Parameters:
codePoint
- the unicode code point.
- Returns:
- the specified unicode code point in U+ notation.
public boolean isTerminated()
Indicates whether this character reference is terminated by a semicolon (;
). Conversely, this library defines an unterminated character reference as one which does not end with a semicolon. The SGML specification allows unterminated character references in some circumstances, and because the HTML 4.01 specification states simply that "authors may use SGML character references", it follows that they are also valid in HTML documents, although their use is strongly discouraged. Unterminated character references are not allowed in XHTML documents.
- Returns:
true
if this character reference is terminated by a semicolon, otherwisefalse
.
- See Also:
decode(CharSequence encodedText, boolean insideAttributeValue)
public static CharacterReference parse(CharSequence characterReferenceText)
Parses a single encoded character reference text into aCharacterReference
object. The character reference must be at the start of the given text, but may contain other characters at the end. ThegetEnd()
method can be used on the resulting object to determine at which character position the character reference ended. If the text does not represent a valid character reference, this method returnsnull
. Unterminated character references are always accepted, regardless of the settings in the current compatibility mode. To decode all character references in a given text, use thedecode(CharSequence)
method instead.
CharacterReference.parse(">").getChar()
>
- Parameters:
characterReferenceText
- the text containing a single encoded character reference.
- Returns:
- a
CharacterReference
object representing the specified text, ornull
if the text does not represent a valid character reference.
- See Also:
decode(CharSequence)
public static String reencode(CharSequence encodedText)
Re-encodes the specified text, equivalent to decoding and then encoding again. This process ensures that the specified encoded text does not contain any remaining unencoded characters. IMPLEMENTATION NOTE: At present this method simply calls thedecode
method followed by theencode
method, but a more efficient implementation may be used in future.
- Parameters:
encodedText
- the text to re-encode.
- Returns:
- the re-encoded string.
public static final boolean requiresEncoding(char ch)
Indicates whether the specified character would need to be encoded in HTML text. This is the case if a character entity reference exists for the character, or the unicode code point is greater than U+007F. The only exception to this is an apostrophe (U+0027), which only returnstrue
if the staticConfig.IsApostropheEncoded
property is currently set totrue
.
- Parameters:
ch
- the character to test.
- Returns:
true
if the specified character would need to be encoded in HTML text, otherwisefalse
.