package org.apache.jasper.xmlparser;
This class defines the basic XML character properties. The data
in this class can be used to verify that a character is a valid
XML character or if the character is a space, name start, or name
character.
A series of convenience methods are supplied to ease the burden
of the developer. Because inlining the checks can improve per
character performance, the tables of character properties are
public. Using the character as an index into the CHARS
array and applying the appropriate mask flag (e.g.
MASK_VALID
), yields the same results as calling the
convenience methods. There is one exception: check the comments
for the isValid
method for details.
- Author(s):
- Glenn Marcy, IBM
- Andy Clark, IBM
- Eric Ye, IBM
- Arnaud Le Hors, IBM
- Michael Glavassevich, IBM
- Rahul Srivastava, Sun Microsystems Inc.
- Version:
- $Id: XMLChar.java 515 2008-03-17 21:02:23Z jfrederic.clere@jboss.com $
private static final byte[] CHARS = new byte[1 << 16];
Name start character mask.
Content character mask. Special characters are those that can
be considered the start of markup, such as '<' and '&'.
The various newline characters are considered special as well.
All other valid XML characters can be considered content.
This is an optimization for the inner loop of character scanning.
NCName start character mask.
Arrays.fill(CHARS, 12296, 12321, (byte) 33 );
Arrays.fill(CHARS, 12321, 12330, (byte) -19 );
Arrays.fill(CHARS, 12330, 12336, (byte) -87 );
Arrays.fill(CHARS, 12337, 12342, (byte) -87 );
Arrays.fill(CHARS, 12342, 12353, (byte) 33 );
Arrays.fill(CHARS, 12353, 12437, (byte) -19 );
Arrays.fill(CHARS, 12437, 12441, (byte) 33 );
Arrays.fill(CHARS, 12441, 12443, (byte) -87 );
Arrays.fill(CHARS, 12443, 12445, (byte) 33 );
Arrays.fill(CHARS, 12445, 12447, (byte) -87 );
Arrays.fill(CHARS, 12447, 12449, (byte) 33 );
Arrays.fill(CHARS, 12449, 12539, (byte) -19 );
Arrays.fill(CHARS, 12540, 12543, (byte) -87 );
Arrays.fill(CHARS, 12543, 12549, (byte) 33 );
Arrays.fill(CHARS, 12549, 12589, (byte) -19 );
Arrays.fill(CHARS, 12589, 19968, (byte) 33 );
Arrays.fill(CHARS, 19968, 40870, (byte) -19 );
Arrays.fill(CHARS, 40870, 44032, (byte) 33 );
Arrays.fill(CHARS, 44032, 55204, (byte) -19 );
Arrays.fill(CHARS, 55204, 55296, (byte) 33 );
Arrays.fill(CHARS, 57344, 65534, (byte) 33 );
Returns true if the specified character is a supplemental character.
- Parameters:
c
The character to check.
return (c >= 0x10000 && c <= 0x10FFFF);
Returns true the supplemental character corresponding to the given
surrogates.
- Parameters:
h
The high surrogate.l
The low surrogate.
return (h - 0xD800) * 0x400 + (l - 0xDC00) + 0x10000;
Returns the high surrogate of a supplemental character
- Parameters:
c
The supplemental character to "split".
return (char) (((c - 0x00010000) >> 10) + 0xD800);
Returns the low surrogate of a supplemental character
- Parameters:
c
The supplemental character to "split".
return (char) (((c - 0x00010000) & 0x3FF) + 0xDC00);
Returns whether the given character is a high surrogate
- Parameters:
c
The character to check.
return (0xD800 <= c && c <= 0xDBFF);
Returns whether the given character is a low surrogate
- Parameters:
c
The character to check.
return (0xDC00 <= c && c <= 0xDFFF);
Returns true if the specified character is valid. This method
also checks the surrogate character range from 0x10000 to 0x10FFFF.
If the program chooses to apply the mask directly to the
CHARS
array, then they are responsible for checking
the surrogate character range.
- Parameters:
c
The character to check.
public static boolean isValid(int c) {
(0x10000 <= c && c <= 0x10FFFF);
Returns true if the specified character is invalid.
- Parameters:
c
The character to check.
Returns true if the specified character can be considered content.
- Parameters:
c
The character to check.
(0x10000 <= c && c <= 0x10FFFF);
Returns true if the specified character can be considered markup.
Markup characters include '<', '&', and '%'.
- Parameters:
c
The character to check.
return c == '<' || c == '&' || c == '%';
Returns true if the specified character is a space character
as defined by production [3] in the XML 1.0 specification.
- Parameters:
c
The character to check.
public static boolean isSpace(int c) {
Returns true if the specified character is a valid name start
character as defined by production [5] in the XML 1.0
specification.
- Parameters:
c
The character to check.
Returns true if the specified character is a valid name
character as defined by production [4] in the XML 1.0
specification.
- Parameters:
c
The character to check.
public static boolean isName(int c) {
Returns true if the specified character is a valid NCName start
character as defined by production [4] in Namespaces in XML
recommendation.
- Parameters:
c
The character to check.
Returns true if the specified character is a valid NCName
character as defined by production [5] in Namespaces in XML
recommendation.
- Parameters:
c
The character to check.
Returns true if the specified character is a valid Pubid
character as defined by production [13] in the XML 1.0
specification.
- Parameters:
c
The character to check.
public static boolean isPubid(int c) {
Check to see if a string is a valid Name according to [5]
in the XML 1.0 Recommendation
- Parameters:
name
string to check- Returns:
- true if name is a valid Name
for (int i = 1; i < name.length(); i++ ) {
Check to see if a string is a valid NCName according to [4]
from the XML Namespaces 1.0 Recommendation
- Parameters:
ncName
string to check- Returns:
- true if name is a valid NCName
for (int i = 1; i < ncName.length(); i++ ) {
Check to see if a string is a valid Nmtoken according to [7]
in the XML 1.0 Recommendation
- Parameters:
nmtoken
string to check- Returns:
- true if nmtoken is a valid Nmtoken
for (int i = 0; i < nmtoken.length(); i++ ) {
Returns true if the encoding name is a valid IANA encoding.
This method does not verify that there is a decoder available
for this encoding, only that the characters are valid for an
IANA encoding name.
- Parameters:
ianaEncoding
The IANA encoding name.
if (ianaEncoding != null) {
int length = ianaEncoding.length();
char c = ianaEncoding.charAt(0);
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) {
for (int i = 1; i < length; i++) {
if ((c < 'A' || c > 'Z') && (c < 'a' || c > 'z') &&
(c < '0' || c > '9') && c != '.' && c != '_' &&
Returns true if the encoding name is a valid Java encoding.
This method does not verify that there is a decoder available
for this encoding, only that the characters are valid for an
Java encoding name.
- Parameters:
javaEncoding
The Java encoding name.
if (javaEncoding != null) {
int length = javaEncoding.length();
for (int i = 1; i < length; i++) {
char c = javaEncoding.charAt(i);
if ((c < 'A' || c > 'Z') && (c < 'a' || c > 'z') &&
(c < '0' || c > '9') && c != '.' && c != '_' &&