KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thaiopensource > datatype > xsd > HexBinaryDatatype


1 package com.thaiopensource.datatype.xsd;
2
3 import org.relaxng.datatype.ValidationContext;
4
5 class HexBinaryDatatype extends BinaryDatatype {
6   static private final int INVALID = -1;
7   static private final int WHITESPACE = -2;
8
9   boolean lexicallyAllows(String JavaDoc str) {
10     int len = str.length();
11     int i = 0;
12     while (i < len && weight(str.charAt(i)) == WHITESPACE)
13       i++;
14     while (i + 1 < len && weight(str.charAt(i)) >= 0 && weight(str.charAt(i + 1)) >= 0)
15       i += 2;
16     while (i < len && weight(str.charAt(i)) == WHITESPACE)
17       i++;
18     return i == len;
19   }
20
21   Object JavaDoc getValue(String JavaDoc str, ValidationContext vc) {
22     int len = str.length();
23     int start = 0;
24     while (start < len && weight(str.charAt(start)) == WHITESPACE)
25       start++;
26     int end = len;
27     while (end > start && weight(str.charAt(end - 1)) == WHITESPACE)
28       end--;
29     byte[] value = new byte[(end - start) >> 1];
30     int j = 0;
31     for (int i = start; i < end; i += 2, j++)
32       value[j] = (byte)((weight(str.charAt(i)) << 4) | weight(str.charAt(i + 1)));
33     return value;
34   }
35
36   static private int weight(char c) {
37     switch (c) {
38     case '0': case '1': case '2': case '3': case '4':
39     case '5': case '6': case '7': case '8': case '9':
40       return c - '0';
41     case 'A': case 'B': case 'C':
42     case 'D': case 'E': case 'F':
43       return c + (10 - 'A');
44     case 'a': case 'b': case 'c':
45     case 'd': case 'e': case 'f':
46       return c + (10 - 'a');
47     case ' ': case '\n': case '\r': case '\t':
48       return WHITESPACE;
49     }
50     return INVALID;
51   }
52
53 }
54
Popular Tags