286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 1999-2005 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A
286N/Apackage com.sun.org.apache.xml.internal.utils;
286N/A
286N/Aimport java.util.Arrays;
286N/A
286N/A
286N/A/**
286N/A * THIS IS A COPY OF THE XERCES-2J CLASS com.sun.org.apache.xerces.internal.utls.XMLChar
286N/A *
286N/A * This class defines the basic properties of characters in XML 1.1. The data
286N/A * in this class can be used to verify that a character is a valid
286N/A * XML 1.1 character or if the character is a space, name start, or name
286N/A * character.
286N/A * <p>
286N/A * A series of convenience methods are supplied to ease the burden
286N/A * of the developer. Using the character as an index into the <code>XML11CHARS</code>
286N/A * array and applying the appropriate mask flag (e.g.
286N/A * <code>MASK_VALID</code>), yields the same results as calling the
286N/A * convenience methods. There is one exception: check the comments
286N/A * for the <code>isValid</code> method for details.
286N/A *
286N/A */
286N/Apublic class XML11Char {
286N/A
286N/A //
286N/A // Constants
286N/A //
286N/A
286N/A /** Character flags for XML 1.1. */
286N/A private static final byte XML11CHARS [] = new byte [1 << 16];
286N/A
286N/A /** XML 1.1 Valid character mask. */
286N/A public static final int MASK_XML11_VALID = 0x01;
286N/A
286N/A /** XML 1.1 Space character mask. */
286N/A public static final int MASK_XML11_SPACE = 0x02;
286N/A
286N/A /** XML 1.1 Name start character mask. */
286N/A public static final int MASK_XML11_NAME_START = 0x04;
286N/A
286N/A /** XML 1.1 Name character mask. */
286N/A public static final int MASK_XML11_NAME = 0x08;
286N/A
286N/A /** XML 1.1 control character mask */
286N/A public static final int MASK_XML11_CONTROL = 0x10;
286N/A
286N/A /** XML 1.1 content for external entities (valid - "special" chars - control chars) */
286N/A public static final int MASK_XML11_CONTENT = 0x20;
286N/A
286N/A /** XML namespaces 1.1 NCNameStart */
286N/A public static final int MASK_XML11_NCNAME_START = 0x40;
286N/A
286N/A /** XML namespaces 1.1 NCName */
286N/A public static final int MASK_XML11_NCNAME = 0x80;
286N/A
286N/A /** XML 1.1 content for internal entities (valid - "special" chars) */
286N/A public static final int MASK_XML11_CONTENT_INTERNAL = MASK_XML11_CONTROL | MASK_XML11_CONTENT;
286N/A
286N/A //
286N/A // Static initialization
286N/A //
286N/A
286N/A static {
286N/A
286N/A // Initializing the Character Flag Array
286N/A // Code generated by: XML11CharGenerator.
286N/A
286N/A Arrays.fill(XML11CHARS, 1, 9, (byte) 17 ); // Fill 8 of value (byte) 17
286N/A XML11CHARS[9] = 35;
286N/A XML11CHARS[10] = 3;
286N/A Arrays.fill(XML11CHARS, 11, 13, (byte) 17 ); // Fill 2 of value (byte) 17
286N/A XML11CHARS[13] = 3;
286N/A Arrays.fill(XML11CHARS, 14, 32, (byte) 17 ); // Fill 18 of value (byte) 17
286N/A XML11CHARS[32] = 35;
286N/A Arrays.fill(XML11CHARS, 33, 38, (byte) 33 ); // Fill 5 of value (byte) 33
286N/A XML11CHARS[38] = 1;
286N/A Arrays.fill(XML11CHARS, 39, 45, (byte) 33 ); // Fill 6 of value (byte) 33
286N/A Arrays.fill(XML11CHARS, 45, 47, (byte) -87 ); // Fill 2 of value (byte) -87
286N/A XML11CHARS[47] = 33;
286N/A Arrays.fill(XML11CHARS, 48, 58, (byte) -87 ); // Fill 10 of value (byte) -87
286N/A XML11CHARS[58] = 45;
286N/A XML11CHARS[59] = 33;
286N/A XML11CHARS[60] = 1;
286N/A Arrays.fill(XML11CHARS, 61, 65, (byte) 33 ); // Fill 4 of value (byte) 33
286N/A Arrays.fill(XML11CHARS, 65, 91, (byte) -19 ); // Fill 26 of value (byte) -19
286N/A Arrays.fill(XML11CHARS, 91, 93, (byte) 33 ); // Fill 2 of value (byte) 33
286N/A XML11CHARS[93] = 1;
286N/A XML11CHARS[94] = 33;
286N/A XML11CHARS[95] = -19;
286N/A XML11CHARS[96] = 33;
286N/A Arrays.fill(XML11CHARS, 97, 123, (byte) -19 ); // Fill 26 of value (byte) -19
286N/A Arrays.fill(XML11CHARS, 123, 127, (byte) 33 ); // Fill 4 of value (byte) 33
286N/A Arrays.fill(XML11CHARS, 127, 133, (byte) 17 ); // Fill 6 of value (byte) 17
286N/A XML11CHARS[133] = 35;
286N/A Arrays.fill(XML11CHARS, 134, 160, (byte) 17 ); // Fill 26 of value (byte) 17
286N/A Arrays.fill(XML11CHARS, 160, 183, (byte) 33 ); // Fill 23 of value (byte) 33
286N/A XML11CHARS[183] = -87;
286N/A Arrays.fill(XML11CHARS, 184, 192, (byte) 33 ); // Fill 8 of value (byte) 33
286N/A Arrays.fill(XML11CHARS, 192, 215, (byte) -19 ); // Fill 23 of value (byte) -19
286N/A XML11CHARS[215] = 33;
286N/A Arrays.fill(XML11CHARS, 216, 247, (byte) -19 ); // Fill 31 of value (byte) -19
286N/A XML11CHARS[247] = 33;
286N/A Arrays.fill(XML11CHARS, 248, 768, (byte) -19 ); // Fill 520 of value (byte) -19
286N/A Arrays.fill(XML11CHARS, 768, 880, (byte) -87 ); // Fill 112 of value (byte) -87
286N/A Arrays.fill(XML11CHARS, 880, 894, (byte) -19 ); // Fill 14 of value (byte) -19
286N/A XML11CHARS[894] = 33;
286N/A Arrays.fill(XML11CHARS, 895, 8192, (byte) -19 ); // Fill 7297 of value (byte) -19
286N/A Arrays.fill(XML11CHARS, 8192, 8204, (byte) 33 ); // Fill 12 of value (byte) 33
286N/A Arrays.fill(XML11CHARS, 8204, 8206, (byte) -19 ); // Fill 2 of value (byte) -19
286N/A Arrays.fill(XML11CHARS, 8206, 8232, (byte) 33 ); // Fill 26 of value (byte) 33
286N/A XML11CHARS[8232] = 35;
286N/A Arrays.fill(XML11CHARS, 8233, 8255, (byte) 33 ); // Fill 22 of value (byte) 33
286N/A Arrays.fill(XML11CHARS, 8255, 8257, (byte) -87 ); // Fill 2 of value (byte) -87
286N/A Arrays.fill(XML11CHARS, 8257, 8304, (byte) 33 ); // Fill 47 of value (byte) 33
286N/A Arrays.fill(XML11CHARS, 8304, 8592, (byte) -19 ); // Fill 288 of value (byte) -19
286N/A Arrays.fill(XML11CHARS, 8592, 11264, (byte) 33 ); // Fill 2672 of value (byte) 33
286N/A Arrays.fill(XML11CHARS, 11264, 12272, (byte) -19 ); // Fill 1008 of value (byte) -19
286N/A Arrays.fill(XML11CHARS, 12272, 12289, (byte) 33 ); // Fill 17 of value (byte) 33
286N/A Arrays.fill(XML11CHARS, 12289, 55296, (byte) -19 ); // Fill 43007 of value (byte) -19
286N/A Arrays.fill(XML11CHARS, 57344, 63744, (byte) 33 ); // Fill 6400 of value (byte) 33
286N/A Arrays.fill(XML11CHARS, 63744, 64976, (byte) -19 ); // Fill 1232 of value (byte) -19
286N/A Arrays.fill(XML11CHARS, 64976, 65008, (byte) 33 ); // Fill 32 of value (byte) 33
286N/A Arrays.fill(XML11CHARS, 65008, 65534, (byte) -19 ); // Fill 526 of value (byte) -19
286N/A
286N/A } // <clinit>()
286N/A
286N/A //
286N/A // Public static methods
286N/A //
286N/A
286N/A /**
286N/A * Returns true if the specified character is a space character
286N/A * as amdended in the XML 1.1 specification.
286N/A *
286N/A * @param c The character to check.
286N/A */
286N/A public static boolean isXML11Space(int c) {
286N/A return (c < 0x10000 && (XML11CHARS[c] & MASK_XML11_SPACE) != 0);
286N/A } // isXML11Space(int):boolean
286N/A
286N/A /**
286N/A * Returns true if the specified character is valid. This method
286N/A * also checks the surrogate character range from 0x10000 to 0x10FFFF.
286N/A * <p>
286N/A * If the program chooses to apply the mask directly to the
286N/A * <code>XML11CHARS</code> array, then they are responsible for checking
286N/A * the surrogate character range.
286N/A *
286N/A * @param c The character to check.
286N/A */
286N/A public static boolean isXML11Valid(int c) {
286N/A return (c < 0x10000 && (XML11CHARS[c] & MASK_XML11_VALID) != 0)
286N/A || (0x10000 <= c && c <= 0x10FFFF);
286N/A } // isXML11Valid(int):boolean
286N/A
286N/A /**
286N/A * Returns true if the specified character is invalid.
286N/A *
286N/A * @param c The character to check.
286N/A */
286N/A public static boolean isXML11Invalid(int c) {
286N/A return !isXML11Valid(c);
286N/A } // isXML11Invalid(int):boolean
286N/A
286N/A /**
286N/A * Returns true if the specified character is valid and permitted outside
286N/A * of a character reference.
286N/A * That is, this method will return false for the same set as
286N/A * isXML11Valid, except it also reports false for "control characters".
286N/A *
286N/A * @param c The character to check.
286N/A */
286N/A public static boolean isXML11ValidLiteral(int c) {
286N/A return ((c < 0x10000 && ((XML11CHARS[c] & MASK_XML11_VALID) != 0 && (XML11CHARS[c] & MASK_XML11_CONTROL) == 0))
286N/A || (0x10000 <= c && c <= 0x10FFFF));
286N/A } // isXML11ValidLiteral(int):boolean
286N/A
286N/A /**
286N/A * Returns true if the specified character can be considered
286N/A * content in an external parsed entity.
286N/A *
286N/A * @param c The character to check.
286N/A */
286N/A public static boolean isXML11Content(int c) {
286N/A return (c < 0x10000 && (XML11CHARS[c] & MASK_XML11_CONTENT) != 0) ||
286N/A (0x10000 <= c && c <= 0x10FFFF);
286N/A } // isXML11Content(int):boolean
286N/A
286N/A /**
286N/A * Returns true if the specified character can be considered
286N/A * content in an internal parsed entity.
286N/A *
286N/A * @param c The character to check.
286N/A */
286N/A public static boolean isXML11InternalEntityContent(int c) {
286N/A return (c < 0x10000 && (XML11CHARS[c] & MASK_XML11_CONTENT_INTERNAL) != 0) ||
286N/A (0x10000 <= c && c <= 0x10FFFF);
286N/A } // isXML11InternalEntityContent(int):boolean
286N/A
286N/A /**
286N/A * Returns true if the specified character is a valid name start
286N/A * character as defined by production [4] in the XML 1.1
286N/A * specification.
286N/A *
286N/A * @param c The character to check.
286N/A */
286N/A public static boolean isXML11NameStart(int c) {
286N/A return (c < 0x10000 && (XML11CHARS[c] & MASK_XML11_NAME_START) != 0)
286N/A || (0x10000 <= c && c < 0xF0000);
286N/A } // isXML11NameStart(int):boolean
286N/A
286N/A /**
286N/A * Returns true if the specified character is a valid name
286N/A * character as defined by production [4a] in the XML 1.1
286N/A * specification.
286N/A *
286N/A * @param c The character to check.
286N/A */
286N/A public static boolean isXML11Name(int c) {
286N/A return (c < 0x10000 && (XML11CHARS[c] & MASK_XML11_NAME) != 0)
286N/A || (c >= 0x10000 && c < 0xF0000);
286N/A } // isXML11Name(int):boolean
286N/A
286N/A /**
286N/A * Returns true if the specified character is a valid NCName start
286N/A * character as defined by production [4] in Namespaces in XML
286N/A * 1.1 recommendation.
286N/A *
286N/A * @param c The character to check.
286N/A */
286N/A public static boolean isXML11NCNameStart(int c) {
286N/A return (c < 0x10000 && (XML11CHARS[c] & MASK_XML11_NCNAME_START) != 0)
286N/A || (0x10000 <= c && c < 0xF0000);
286N/A } // isXML11NCNameStart(int):boolean
286N/A
286N/A /**
286N/A * Returns true if the specified character is a valid NCName
286N/A * character as defined by production [5] in Namespaces in XML
286N/A * 1.1 recommendation.
286N/A *
286N/A * @param c The character to check.
286N/A */
286N/A public static boolean isXML11NCName(int c) {
286N/A return (c < 0x10000 && (XML11CHARS[c] & MASK_XML11_NCNAME) != 0)
286N/A || (0x10000 <= c && c < 0xF0000);
286N/A } // isXML11NCName(int):boolean
286N/A
286N/A /**
286N/A * Returns whether the given character is a valid
286N/A * high surrogate for a name character. This includes
286N/A * all high surrogates for characters [0x10000-0xEFFFF].
286N/A * In other words everything excluding planes 15 and 16.
286N/A *
286N/A * @param c The character to check.
286N/A */
286N/A public static boolean isXML11NameHighSurrogate(int c) {
286N/A return (0xD800 <= c && c <= 0xDB7F);
286N/A }
286N/A
286N/A /*
286N/A * [5] Name ::= NameStartChar NameChar*
286N/A */
286N/A /**
286N/A * Check to see if a string is a valid Name according to [5]
286N/A * in the XML 1.1 Recommendation
286N/A *
286N/A * @param name string to check
286N/A * @return true if name is a valid Name
286N/A */
286N/A public static boolean isXML11ValidName(String name) {
286N/A int length = name.length();
286N/A if (length == 0)
286N/A return false;
286N/A int i = 1;
286N/A char ch = name.charAt(0);
286N/A if( !isXML11NameStart(ch) ) {
286N/A if ( length > 1 && isXML11NameHighSurrogate(ch) ) {
286N/A char ch2 = name.charAt(1);
286N/A if ( !XMLChar.isLowSurrogate(ch2) ||
286N/A !isXML11NameStart(XMLChar.supplemental(ch, ch2)) ) {
286N/A return false;
286N/A }
286N/A i = 2;
286N/A }
286N/A else {
286N/A return false;
286N/A }
286N/A }
286N/A while (i < length) {
286N/A ch = name.charAt(i);
286N/A if ( !isXML11Name(ch) ) {
286N/A if ( ++i < length && isXML11NameHighSurrogate(ch) ) {
286N/A char ch2 = name.charAt(i);
286N/A if ( !XMLChar.isLowSurrogate(ch2) ||
286N/A !isXML11Name(XMLChar.supplemental(ch, ch2)) ) {
286N/A return false;
286N/A }
286N/A }
286N/A else {
286N/A return false;
286N/A }
286N/A }
286N/A ++i;
286N/A }
286N/A return true;
286N/A } // isXML11ValidName(String):boolean
286N/A
286N/A
286N/A /*
286N/A * from the namespace 1.1 rec
286N/A * [4] NCName ::= NCNameStartChar NCNameChar*
286N/A */
286N/A /**
286N/A * Check to see if a string is a valid NCName according to [4]
286N/A * from the XML Namespaces 1.1 Recommendation
286N/A *
286N/A * @param ncName string to check
286N/A * @return true if name is a valid NCName
286N/A */
286N/A public static boolean isXML11ValidNCName(String ncName) {
286N/A int length = ncName.length();
286N/A if (length == 0)
286N/A return false;
286N/A int i = 1;
286N/A char ch = ncName.charAt(0);
286N/A if( !isXML11NCNameStart(ch) ) {
286N/A if ( length > 1 && isXML11NameHighSurrogate(ch) ) {
286N/A char ch2 = ncName.charAt(1);
286N/A if ( !XMLChar.isLowSurrogate(ch2) ||
286N/A !isXML11NCNameStart(XMLChar.supplemental(ch, ch2)) ) {
286N/A return false;
286N/A }
286N/A i = 2;
286N/A }
286N/A else {
286N/A return false;
286N/A }
286N/A }
286N/A while (i < length) {
286N/A ch = ncName.charAt(i);
286N/A if ( !isXML11NCName(ch) ) {
286N/A if ( ++i < length && isXML11NameHighSurrogate(ch) ) {
286N/A char ch2 = ncName.charAt(i);
286N/A if ( !XMLChar.isLowSurrogate(ch2) ||
286N/A !isXML11NCName(XMLChar.supplemental(ch, ch2)) ) {
286N/A return false;
286N/A }
286N/A }
286N/A else {
286N/A return false;
286N/A }
286N/A }
286N/A ++i;
286N/A }
286N/A return true;
286N/A } // isXML11ValidNCName(String):boolean
286N/A
286N/A /*
286N/A * [7] Nmtoken ::= (NameChar)+
286N/A */
286N/A /**
286N/A * Check to see if a string is a valid Nmtoken according to [7]
286N/A * in the XML 1.1 Recommendation
286N/A *
286N/A * @param nmtoken string to check
286N/A * @return true if nmtoken is a valid Nmtoken
286N/A */
286N/A public static boolean isXML11ValidNmtoken(String nmtoken) {
286N/A int length = nmtoken.length();
286N/A if (length == 0)
286N/A return false;
286N/A for (int i = 0; i < length; ++i ) {
286N/A char ch = nmtoken.charAt(i);
286N/A if( !isXML11Name(ch) ) {
286N/A if ( ++i < length && isXML11NameHighSurrogate(ch) ) {
286N/A char ch2 = nmtoken.charAt(i);
286N/A if ( !XMLChar.isLowSurrogate(ch2) ||
286N/A !isXML11Name(XMLChar.supplemental(ch, ch2)) ) {
286N/A return false;
286N/A }
286N/A }
286N/A else {
286N/A return false;
286N/A }
286N/A }
286N/A }
286N/A return true;
286N/A } // isXML11ValidName(String):boolean
286N/A
286N/A /**
286N/A * Simple check to determine if qname is legal. If it returns false
286N/A * then <param>str</param> is illegal; if it returns true then
286N/A * <param>str</param> is legal.
286N/A */
286N/A public static boolean isXML11ValidQName(String str) {
286N/A
286N/A final int colon = str.indexOf(':');
286N/A
286N/A if (colon == 0 || colon == str.length() - 1) {
286N/A return false;
286N/A }
286N/A
286N/A if (colon > 0) {
286N/A final String prefix = str.substring(0,colon);
286N/A final String localPart = str.substring(colon+1);
286N/A return isXML11ValidNCName(prefix) && isXML11ValidNCName(localPart);
286N/A }
286N/A else {
286N/A return isXML11ValidNCName(str);
286N/A }
286N/A }
286N/A
286N/A} // class XML11Char