1 16 17 package com.sun.org.apache.xml.internal.utils; 18 19 import java.util.Arrays ; 20 21 22 39 public class XML11Char { 40 41 45 46 private static final byte XML11CHARS [] = new byte [1 << 16]; 47 48 49 public static final int MASK_XML11_VALID = 0x01; 50 51 52 public static final int MASK_XML11_SPACE = 0x02; 53 54 55 public static final int MASK_XML11_NAME_START = 0x04; 56 57 58 public static final int MASK_XML11_NAME = 0x08; 59 60 61 public static final int MASK_XML11_CONTROL = 0x10; 62 63 64 public static final int MASK_XML11_CONTENT = 0x20; 65 66 67 public static final int MASK_XML11_NCNAME_START = 0x40; 68 69 70 public static final int MASK_XML11_NCNAME = 0x80; 71 72 73 public static final int MASK_XML11_CONTENT_INTERNAL = MASK_XML11_CONTROL | MASK_XML11_CONTENT; 74 75 79 static { 80 81 84 Arrays.fill(XML11CHARS, 1, 9, (byte) 17 ); XML11CHARS[9] = 35; 86 XML11CHARS[10] = 3; 87 Arrays.fill(XML11CHARS, 11, 13, (byte) 17 ); XML11CHARS[13] = 3; 89 Arrays.fill(XML11CHARS, 14, 32, (byte) 17 ); XML11CHARS[32] = 35; 91 Arrays.fill(XML11CHARS, 33, 38, (byte) 33 ); XML11CHARS[38] = 1; 93 Arrays.fill(XML11CHARS, 39, 45, (byte) 33 ); Arrays.fill(XML11CHARS, 45, 47, (byte) -87 ); XML11CHARS[47] = 33; 96 Arrays.fill(XML11CHARS, 48, 58, (byte) -87 ); XML11CHARS[58] = 45; 98 XML11CHARS[59] = 33; 99 XML11CHARS[60] = 1; 100 Arrays.fill(XML11CHARS, 61, 65, (byte) 33 ); Arrays.fill(XML11CHARS, 65, 91, (byte) -19 ); Arrays.fill(XML11CHARS, 91, 93, (byte) 33 ); XML11CHARS[93] = 1; 104 XML11CHARS[94] = 33; 105 XML11CHARS[95] = -19; 106 XML11CHARS[96] = 33; 107 Arrays.fill(XML11CHARS, 97, 123, (byte) -19 ); Arrays.fill(XML11CHARS, 123, 127, (byte) 33 ); Arrays.fill(XML11CHARS, 127, 133, (byte) 17 ); XML11CHARS[133] = 35; 111 Arrays.fill(XML11CHARS, 134, 160, (byte) 17 ); Arrays.fill(XML11CHARS, 160, 183, (byte) 33 ); XML11CHARS[183] = -87; 114 Arrays.fill(XML11CHARS, 184, 192, (byte) 33 ); Arrays.fill(XML11CHARS, 192, 215, (byte) -19 ); XML11CHARS[215] = 33; 117 Arrays.fill(XML11CHARS, 216, 247, (byte) -19 ); XML11CHARS[247] = 33; 119 Arrays.fill(XML11CHARS, 248, 768, (byte) -19 ); Arrays.fill(XML11CHARS, 768, 880, (byte) -87 ); Arrays.fill(XML11CHARS, 880, 894, (byte) -19 ); XML11CHARS[894] = 33; 123 Arrays.fill(XML11CHARS, 895, 8192, (byte) -19 ); Arrays.fill(XML11CHARS, 8192, 8204, (byte) 33 ); Arrays.fill(XML11CHARS, 8204, 8206, (byte) -19 ); Arrays.fill(XML11CHARS, 8206, 8232, (byte) 33 ); XML11CHARS[8232] = 35; 128 Arrays.fill(XML11CHARS, 8233, 8255, (byte) 33 ); Arrays.fill(XML11CHARS, 8255, 8257, (byte) -87 ); Arrays.fill(XML11CHARS, 8257, 8304, (byte) 33 ); Arrays.fill(XML11CHARS, 8304, 8592, (byte) -19 ); Arrays.fill(XML11CHARS, 8592, 11264, (byte) 33 ); Arrays.fill(XML11CHARS, 11264, 12272, (byte) -19 ); Arrays.fill(XML11CHARS, 12272, 12289, (byte) 33 ); Arrays.fill(XML11CHARS, 12289, 55296, (byte) -19 ); Arrays.fill(XML11CHARS, 57344, 63744, (byte) 33 ); Arrays.fill(XML11CHARS, 63744, 64976, (byte) -19 ); Arrays.fill(XML11CHARS, 64976, 65008, (byte) 33 ); Arrays.fill(XML11CHARS, 65008, 65534, (byte) -19 ); 141 } 143 147 153 public static boolean isXML11Space(int c) { 154 return (c < 0x10000 && (XML11CHARS[c] & MASK_XML11_SPACE) != 0); 155 } 157 167 public static boolean isXML11Valid(int c) { 168 return (c < 0x10000 && (XML11CHARS[c] & MASK_XML11_VALID) != 0) 169 || (0x10000 <= c && c <= 0x10FFFF); 170 } 172 177 public static boolean isXML11Invalid(int c) { 178 return !isXML11Valid(c); 179 } 181 189 public static boolean isXML11ValidLiteral(int c) { 190 return ((c < 0x10000 && ((XML11CHARS[c] & MASK_XML11_VALID) != 0 && (XML11CHARS[c] & MASK_XML11_CONTROL) == 0)) 191 || (0x10000 <= c && c <= 0x10FFFF)); 192 } 194 200 public static boolean isXML11Content(int c) { 201 return (c < 0x10000 && (XML11CHARS[c] & MASK_XML11_CONTENT) != 0) || 202 (0x10000 <= c && c <= 0x10FFFF); 203 } 205 211 public static boolean isXML11InternalEntityContent(int c) { 212 return (c < 0x10000 && (XML11CHARS[c] & MASK_XML11_CONTENT_INTERNAL) != 0) || 213 (0x10000 <= c && c <= 0x10FFFF); 214 } 216 223 public static boolean isXML11NameStart(int c) { 224 return (c < 0x10000 && (XML11CHARS[c] & MASK_XML11_NAME_START) != 0) 225 || (0x10000 <= c && c < 0xF0000); 226 } 228 235 public static boolean isXML11Name(int c) { 236 return (c < 0x10000 && (XML11CHARS[c] & MASK_XML11_NAME) != 0) 237 || (c >= 0x10000 && c < 0xF0000); 238 } 240 247 public static boolean isXML11NCNameStart(int c) { 248 return (c < 0x10000 && (XML11CHARS[c] & MASK_XML11_NCNAME_START) != 0) 249 || (0x10000 <= c && c < 0xF0000); 250 } 252 259 public static boolean isXML11NCName(int c) { 260 return (c < 0x10000 && (XML11CHARS[c] & MASK_XML11_NCNAME) != 0) 261 || (0x10000 <= c && c < 0xF0000); 262 } 264 272 public static boolean isXML11NameHighSurrogate(int c) { 273 return (0xD800 <= c && c <= 0xDB7F); 274 } 275 276 279 286 public static boolean isXML11ValidName(String name) { 287 int length = name.length(); 288 if (length == 0) 289 return false; 290 int i = 1; 291 char ch = name.charAt(0); 292 if( !isXML11NameStart(ch) ) { 293 if ( length > 1 && isXML11NameHighSurrogate(ch) ) { 294 char ch2 = name.charAt(1); 295 if ( !XMLChar.isLowSurrogate(ch2) || 296 !isXML11NameStart(XMLChar.supplemental(ch, ch2)) ) { 297 return false; 298 } 299 i = 2; 300 } 301 else { 302 return false; 303 } 304 } 305 while (i < length) { 306 ch = name.charAt(i); 307 if ( !isXML11Name(ch) ) { 308 if ( ++i < length && isXML11NameHighSurrogate(ch) ) { 309 char ch2 = name.charAt(i); 310 if ( !XMLChar.isLowSurrogate(ch2) || 311 !isXML11Name(XMLChar.supplemental(ch, ch2)) ) { 312 return false; 313 } 314 } 315 else { 316 return false; 317 } 318 } 319 ++i; 320 } 321 return true; 322 } 324 325 329 336 public static boolean isXML11ValidNCName(String ncName) { 337 int length = ncName.length(); 338 if (length == 0) 339 return false; 340 int i = 1; 341 char ch = ncName.charAt(0); 342 if( !isXML11NCNameStart(ch) ) { 343 if ( length > 1 && isXML11NameHighSurrogate(ch) ) { 344 char ch2 = ncName.charAt(1); 345 if ( !XMLChar.isLowSurrogate(ch2) || 346 !isXML11NCNameStart(XMLChar.supplemental(ch, ch2)) ) { 347 return false; 348 } 349 i = 2; 350 } 351 else { 352 return false; 353 } 354 } 355 while (i < length) { 356 ch = ncName.charAt(i); 357 if ( !isXML11NCName(ch) ) { 358 if ( ++i < length && isXML11NameHighSurrogate(ch) ) { 359 char ch2 = ncName.charAt(i); 360 if ( !XMLChar.isLowSurrogate(ch2) || 361 !isXML11NCName(XMLChar.supplemental(ch, ch2)) ) { 362 return false; 363 } 364 } 365 else { 366 return false; 367 } 368 } 369 ++i; 370 } 371 return true; 372 } 374 377 384 public static boolean isXML11ValidNmtoken(String nmtoken) { 385 int length = nmtoken.length(); 386 if (length == 0) 387 return false; 388 for (int i = 0; i < length; ++i ) { 389 char ch = nmtoken.charAt(i); 390 if( !isXML11Name(ch) ) { 391 if ( ++i < length && isXML11NameHighSurrogate(ch) ) { 392 char ch2 = nmtoken.charAt(i); 393 if ( !XMLChar.isLowSurrogate(ch2) || 394 !isXML11Name(XMLChar.supplemental(ch, ch2)) ) { 395 return false; 396 } 397 } 398 else { 399 return false; 400 } 401 } 402 } 403 return true; 404 } 406 411 public static boolean isXML11ValidQName(String str) { 412 413 final int colon = str.indexOf(':'); 414 415 if (colon == 0 || colon == str.length() - 1) { 416 return false; 417 } 418 419 if (colon > 0) { 420 final String prefix = str.substring(0,colon); 421 final String localPart = str.substring(colon+1); 422 return isXML11ValidNCName(prefix) && isXML11ValidNCName(localPart); 423 } 424 else { 425 return isXML11ValidNCName(str); 426 } 427 } 428 429 } 431 | Popular Tags |