1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Copyright 1999-2004 The Apache Software Foundation.
   7  *
   8  * Licensed under the Apache License, Version 2.0 (the "License");
   9  * you may not use this file except in compliance with the License.
  10  * You may obtain a copy of the License at
  11  *
  12  *      http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 
  21 package com.sun.org.apache.xerces.internal.util;
  22 
  23 import java.util.Arrays;
  24 
  25 /**
  26  * This class defines the basic properties of characters in XML 1.1. The data
  27  * in this class can be used to verify that a character is a valid
  28  * XML 1.1 character or if the character is a space, name start, or name
  29  * character.
  30  * <p>
  31  * A series of convenience methods are supplied to ease the burden
  32  * of the developer.  Using the character as an index into the <code>XML11CHARS</code>
  33  * array and applying the appropriate mask flag (e.g.
  34  * <code>MASK_VALID</code>), yields the same results as calling the
  35  * convenience methods. There is one exception: check the comments
  36  * for the <code>isValid</code> method for details.
  37  *
  38  * @author Glenn Marcy, IBM
  39  * @author Andy Clark, IBM
  40  * @author Arnaud  Le Hors, IBM
  41  * @author Neil Graham, IBM
  42  * @author Michael Glavassevich, IBM
  43  *
  44  */
  45 public class XML11Char {
  46 
  47     //
  48     // Constants
  49     //
  50 
  51     /** Character flags for XML 1.1. */
  52     private static final byte XML11CHARS [] = new byte [1 << 16];
  53 
  54     /** XML 1.1 Valid character mask. */
  55     public static final int MASK_XML11_VALID = 0x01;
  56 
  57     /** XML 1.1 Space character mask. */
  58     public static final int MASK_XML11_SPACE = 0x02;
  59 
  60     /** XML 1.1 Name start character mask. */
  61     public static final int MASK_XML11_NAME_START = 0x04;
  62 
  63     /** XML 1.1 Name character mask. */
  64     public static final int MASK_XML11_NAME = 0x08;
  65 
  66     /** XML 1.1 control character mask */
  67     public static final int MASK_XML11_CONTROL = 0x10;
  68 
  69     /** XML 1.1 content for external entities (valid - "special" chars - control chars) */
  70     public static final int MASK_XML11_CONTENT = 0x20;
  71 
  72     /** XML namespaces 1.1 NCNameStart */
  73     public static final int MASK_XML11_NCNAME_START = 0x40;
  74 
  75     /** XML namespaces 1.1 NCName */
  76     public static final int MASK_XML11_NCNAME = 0x80;
  77 
  78     /** XML 1.1 content for internal entities (valid - "special" chars) */
  79     public static final int MASK_XML11_CONTENT_INTERNAL = MASK_XML11_CONTROL | MASK_XML11_CONTENT;
  80 
  81     //
  82     // Static initialization
  83     //
  84 
  85     static {
  86 
  87         // Initializing the Character Flag Array
  88         // Code generated by: XML11CharGenerator.
  89 
  90         Arrays.fill(XML11CHARS, 1, 9, (byte) 17 ); // Fill 8 of value (byte) 17
  91         XML11CHARS[9] = 35;
  92         XML11CHARS[10] = 3;
  93         Arrays.fill(XML11CHARS, 11, 13, (byte) 17 ); // Fill 2 of value (byte) 17
  94         XML11CHARS[13] = 3;
  95         Arrays.fill(XML11CHARS, 14, 32, (byte) 17 ); // Fill 18 of value (byte) 17
  96         XML11CHARS[32] = 35;
  97         Arrays.fill(XML11CHARS, 33, 38, (byte) 33 ); // Fill 5 of value (byte) 33
  98         XML11CHARS[38] = 1;
  99         Arrays.fill(XML11CHARS, 39, 45, (byte) 33 ); // Fill 6 of value (byte) 33
 100         Arrays.fill(XML11CHARS, 45, 47, (byte) -87 ); // Fill 2 of value (byte) -87
 101         XML11CHARS[47] = 33;
 102         Arrays.fill(XML11CHARS, 48, 58, (byte) -87 ); // Fill 10 of value (byte) -87
 103         XML11CHARS[58] = 45;
 104         XML11CHARS[59] = 33;
 105         XML11CHARS[60] = 1;
 106         Arrays.fill(XML11CHARS, 61, 65, (byte) 33 ); // Fill 4 of value (byte) 33
 107         Arrays.fill(XML11CHARS, 65, 91, (byte) -19 ); // Fill 26 of value (byte) -19
 108         Arrays.fill(XML11CHARS, 91, 93, (byte) 33 ); // Fill 2 of value (byte) 33
 109         XML11CHARS[93] = 1;
 110         XML11CHARS[94] = 33;
 111         XML11CHARS[95] = -19;
 112         XML11CHARS[96] = 33;
 113         Arrays.fill(XML11CHARS, 97, 123, (byte) -19 ); // Fill 26 of value (byte) -19
 114         Arrays.fill(XML11CHARS, 123, 127, (byte) 33 ); // Fill 4 of value (byte) 33
 115         Arrays.fill(XML11CHARS, 127, 133, (byte) 17 ); // Fill 6 of value (byte) 17
 116         XML11CHARS[133] = 35;
 117         Arrays.fill(XML11CHARS, 134, 160, (byte) 17 ); // Fill 26 of value (byte) 17
 118         Arrays.fill(XML11CHARS, 160, 183, (byte) 33 ); // Fill 23 of value (byte) 33
 119         XML11CHARS[183] = -87;
 120         Arrays.fill(XML11CHARS, 184, 192, (byte) 33 ); // Fill 8 of value (byte) 33
 121         Arrays.fill(XML11CHARS, 192, 215, (byte) -19 ); // Fill 23 of value (byte) -19
 122         XML11CHARS[215] = 33;
 123         Arrays.fill(XML11CHARS, 216, 247, (byte) -19 ); // Fill 31 of value (byte) -19
 124         XML11CHARS[247] = 33;
 125         Arrays.fill(XML11CHARS, 248, 768, (byte) -19 ); // Fill 520 of value (byte) -19
 126         Arrays.fill(XML11CHARS, 768, 880, (byte) -87 ); // Fill 112 of value (byte) -87
 127         Arrays.fill(XML11CHARS, 880, 894, (byte) -19 ); // Fill 14 of value (byte) -19
 128         XML11CHARS[894] = 33;
 129         Arrays.fill(XML11CHARS, 895, 8192, (byte) -19 ); // Fill 7297 of value (byte) -19
 130         Arrays.fill(XML11CHARS, 8192, 8204, (byte) 33 ); // Fill 12 of value (byte) 33
 131         Arrays.fill(XML11CHARS, 8204, 8206, (byte) -19 ); // Fill 2 of value (byte) -19
 132         Arrays.fill(XML11CHARS, 8206, 8232, (byte) 33 ); // Fill 26 of value (byte) 33
 133         XML11CHARS[8232] = 35;
 134         Arrays.fill(XML11CHARS, 8233, 8255, (byte) 33 ); // Fill 22 of value (byte) 33
 135         Arrays.fill(XML11CHARS, 8255, 8257, (byte) -87 ); // Fill 2 of value (byte) -87
 136         Arrays.fill(XML11CHARS, 8257, 8304, (byte) 33 ); // Fill 47 of value (byte) 33
 137         Arrays.fill(XML11CHARS, 8304, 8592, (byte) -19 ); // Fill 288 of value (byte) -19
 138         Arrays.fill(XML11CHARS, 8592, 11264, (byte) 33 ); // Fill 2672 of value (byte) 33
 139         Arrays.fill(XML11CHARS, 11264, 12272, (byte) -19 ); // Fill 1008 of value (byte) -19
 140         Arrays.fill(XML11CHARS, 12272, 12289, (byte) 33 ); // Fill 17 of value (byte) 33
 141         Arrays.fill(XML11CHARS, 12289, 55296, (byte) -19 ); // Fill 43007 of value (byte) -19
 142         Arrays.fill(XML11CHARS, 57344, 63744, (byte) 33 ); // Fill 6400 of value (byte) 33
 143         Arrays.fill(XML11CHARS, 63744, 64976, (byte) -19 ); // Fill 1232 of value (byte) -19
 144         Arrays.fill(XML11CHARS, 64976, 65008, (byte) 33 ); // Fill 32 of value (byte) 33
 145         Arrays.fill(XML11CHARS, 65008, 65534, (byte) -19 ); // Fill 526 of value (byte) -19
 146 
 147     } // <clinit>()
 148 
 149     //
 150     // Public static methods
 151     //
 152 
 153     /**
 154      * Returns true if the specified character is a space character
 155      * as amdended in the XML 1.1 specification.
 156      *
 157      * @param c The character to check.
 158      */
 159     public static boolean isXML11Space(int c) {
 160         return (c < 0x10000 && (XML11CHARS[c] & MASK_XML11_SPACE) != 0);
 161     } // isXML11Space(int):boolean
 162 
 163     /**
 164      * Returns true if the specified character is valid. This method
 165      * also checks the surrogate character range from 0x10000 to 0x10FFFF.
 166      * <p>
 167      * If the program chooses to apply the mask directly to the
 168      * <code>XML11CHARS</code> array, then they are responsible for checking
 169      * the surrogate character range.
 170      *
 171      * @param c The character to check.
 172      */
 173     public static boolean isXML11Valid(int c) {
 174         return (c < 0x10000 && (XML11CHARS[c] & MASK_XML11_VALID) != 0)
 175                 || (0x10000 <= c && c <= 0x10FFFF);
 176     } // isXML11Valid(int):boolean
 177 
 178     /**
 179      * Returns true if the specified character is invalid.
 180      *
 181      * @param c The character to check.
 182      */
 183     public static boolean isXML11Invalid(int c) {
 184         return !isXML11Valid(c);
 185     } // isXML11Invalid(int):boolean
 186 
 187     /**
 188      * Returns true if the specified character is valid and permitted outside
 189      * of a character reference.
 190      * That is, this method will return false for the same set as
 191      * isXML11Valid, except it also reports false for "control characters".
 192      *
 193      * @param c The character to check.
 194      */
 195     public static boolean isXML11ValidLiteral(int c) {
 196         return ((c < 0x10000 && ((XML11CHARS[c] & MASK_XML11_VALID) != 0 && (XML11CHARS[c] & MASK_XML11_CONTROL) == 0))
 197             || (0x10000 <= c && c <= 0x10FFFF));
 198     } // isXML11ValidLiteral(int):boolean
 199 
 200     /**
 201      * Returns true if the specified character can be considered
 202      * content in an external parsed entity.
 203      *
 204      * @param c The character to check.
 205      */
 206     public static boolean isXML11Content(int c) {
 207         return (c < 0x10000 && (XML11CHARS[c] & MASK_XML11_CONTENT) != 0) ||
 208                (0x10000 <= c && c <= 0x10FFFF);
 209     } // isXML11Content(int):boolean
 210 
 211     /**
 212      * Returns true if the specified character can be considered
 213      * content in an internal parsed entity.
 214      *
 215      * @param c The character to check.
 216      */
 217     public static boolean isXML11InternalEntityContent(int c) {
 218         return (c < 0x10000 && (XML11CHARS[c] & MASK_XML11_CONTENT_INTERNAL) != 0) ||
 219                (0x10000 <= c && c <= 0x10FFFF);
 220     } // isXML11InternalEntityContent(int):boolean
 221 
 222     /**
 223      * Returns true if the specified character is a valid name start
 224      * character as defined by production [4] in the XML 1.1
 225      * specification.
 226      *
 227      * @param c The character to check.
 228      */
 229     public static boolean isXML11NameStart(int c) {
 230         return (c < 0x10000 && (XML11CHARS[c] & MASK_XML11_NAME_START) != 0)
 231             || (0x10000 <= c && c < 0xF0000);
 232     } // isXML11NameStart(int):boolean
 233 
 234     /**
 235      * Returns true if the specified character is a valid name
 236      * character as defined by production [4a] in the XML 1.1
 237      * specification.
 238      *
 239      * @param c The character to check.
 240      */
 241     public static boolean isXML11Name(int c) {
 242         return (c < 0x10000 && (XML11CHARS[c] & MASK_XML11_NAME) != 0)
 243             || (c >= 0x10000 && c < 0xF0000);
 244     } // isXML11Name(int):boolean
 245 
 246     /**
 247      * Returns true if the specified character is a valid NCName start
 248      * character as defined by production [4] in Namespaces in XML
 249      * 1.1 recommendation.
 250      *
 251      * @param c The character to check.
 252      */
 253     public static boolean isXML11NCNameStart(int c) {
 254         return (c < 0x10000 && (XML11CHARS[c] & MASK_XML11_NCNAME_START) != 0)
 255             || (0x10000 <= c && c < 0xF0000);
 256     } // isXML11NCNameStart(int):boolean
 257 
 258     /**
 259      * Returns true if the specified character is a valid NCName
 260      * character as defined by production [5] in Namespaces in XML
 261      * 1.1 recommendation.
 262      *
 263      * @param c The character to check.
 264      */
 265     public static boolean isXML11NCName(int c) {
 266         return (c < 0x10000 && (XML11CHARS[c] & MASK_XML11_NCNAME) != 0)
 267             || (0x10000 <= c && c < 0xF0000);
 268     } // isXML11NCName(int):boolean
 269 
 270     /**
 271      * Returns whether the given character is a valid
 272      * high surrogate for a name character. This includes
 273      * all high surrogates for characters [0x10000-0xEFFFF].
 274      * In other words everything excluding planes 15 and 16.
 275      *
 276      * @param c The character to check.
 277      */
 278     public static boolean isXML11NameHighSurrogate(int c) {
 279         return (0xD800 <= c && c <= 0xDB7F);
 280     }
 281 
 282     /*
 283      * [5] Name ::= NameStartChar NameChar*
 284      */
 285     /**
 286      * Check to see if a string is a valid Name according to [5]
 287      * in the XML 1.1 Recommendation
 288      *
 289      * @param name string to check
 290      * @return true if name is a valid Name
 291      */
 292     public static boolean isXML11ValidName(String name) {
 293         final int length = name.length();
 294         if (length == 0) {
 295             return false;
 296         }
 297         int i = 1;
 298         char ch = name.charAt(0);
 299         if (!isXML11NameStart(ch)) {
 300             if (length > 1 && isXML11NameHighSurrogate(ch)) {
 301                 char ch2 = name.charAt(1);
 302                 if (!XMLChar.isLowSurrogate(ch2) ||
 303                     !isXML11NameStart(XMLChar.supplemental(ch, ch2))) {
 304                     return false;
 305                 }
 306                 i = 2;
 307             }
 308             else {
 309                 return false;
 310             }
 311         }
 312         while (i < length) {
 313             ch = name.charAt(i);
 314             if (!isXML11Name(ch)) {
 315                 if (++i < length && isXML11NameHighSurrogate(ch)) {
 316                     char ch2 = name.charAt(i);
 317                     if (!XMLChar.isLowSurrogate(ch2) ||
 318                         !isXML11Name(XMLChar.supplemental(ch, ch2))) {
 319                         return false;
 320                     }
 321                 }
 322                 else {
 323                     return false;
 324                 }
 325             }
 326             ++i;
 327         }
 328         return true;
 329     } // isXML11ValidName(String):boolean
 330 
 331     /*
 332      * from the namespace 1.1 rec
 333      * [4] NCName ::= NCNameStartChar NCNameChar*
 334      */
 335     /**
 336      * Check to see if a string is a valid NCName according to [4]
 337      * from the XML Namespaces 1.1 Recommendation
 338      *
 339      * @param ncName string to check
 340      * @return true if name is a valid NCName
 341      */
 342     public static boolean isXML11ValidNCName(String ncName) {
 343         final int length = ncName.length();
 344         if (length == 0) {
 345             return false;
 346         }
 347         int i = 1;
 348         char ch = ncName.charAt(0);
 349         if (!isXML11NCNameStart(ch)) {
 350             if (length > 1 && isXML11NameHighSurrogate(ch)) {
 351                 char ch2 = ncName.charAt(1);
 352                 if (!XMLChar.isLowSurrogate(ch2) ||
 353                     !isXML11NCNameStart(XMLChar.supplemental(ch, ch2))) {
 354                     return false;
 355                 }
 356                 i = 2;
 357             }
 358             else {
 359                 return false;
 360             }
 361         }
 362         while (i < length) {
 363             ch = ncName.charAt(i);
 364             if (!isXML11NCName(ch)) {
 365                 if (++i < length && isXML11NameHighSurrogate(ch)) {
 366                     char ch2 = ncName.charAt(i);
 367                     if (!XMLChar.isLowSurrogate(ch2) ||
 368                         !isXML11NCName(XMLChar.supplemental(ch, ch2))) {
 369                         return false;
 370                     }
 371                 }
 372                 else {
 373                     return false;
 374                 }
 375             }
 376             ++i;
 377         }
 378         return true;
 379     } // isXML11ValidNCName(String):boolean
 380 
 381     /*
 382      * [7] Nmtoken ::= (NameChar)+
 383      */
 384     /**
 385      * Check to see if a string is a valid Nmtoken according to [7]
 386      * in the XML 1.1 Recommendation
 387      *
 388      * @param nmtoken string to check
 389      * @return true if nmtoken is a valid Nmtoken
 390      */
 391     public static boolean isXML11ValidNmtoken(String nmtoken) {
 392         final int length = nmtoken.length();
 393         if (length == 0) {
 394             return false;
 395         }
 396         for (int i = 0; i < length; ++i) {
 397             char ch = nmtoken.charAt(i);
 398             if (!isXML11Name(ch)) {
 399                 if (++i < length && isXML11NameHighSurrogate(ch)) {
 400                     char ch2 = nmtoken.charAt(i);
 401                     if (!XMLChar.isLowSurrogate(ch2) ||
 402                         !isXML11Name(XMLChar.supplemental(ch, ch2))) {
 403                         return false;
 404                     }
 405                 }
 406                 else {
 407                     return false;
 408                 }
 409             }
 410         }
 411         return true;
 412     } // isXML11ValidName(String):boolean
 413 
 414 } // class XML11Char