KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.thaiopensource.datatype.xsd;
2
3 import org.relaxng.datatype.Datatype;
4 import org.relaxng.datatype.DatatypeException;
5 import org.relaxng.datatype.ValidationContext;
6 import org.relaxng.datatype.DatatypeStreamingValidator;
7 import org.relaxng.datatype.helpers.StreamingValidatorImpl;
8 import com.thaiopensource.datatype.Datatype2;
9
10 abstract class DatatypeBase implements Datatype2 {
11   abstract boolean lexicallyAllows(String JavaDoc str);
12   private final int whiteSpace;
13
14   static final int WHITE_SPACE_PRESERVE = 0;
15   static final int WHITE_SPACE_REPLACE = 1;
16   static final int WHITE_SPACE_COLLAPSE = 2;
17
18   DatatypeBase() {
19     whiteSpace = WHITE_SPACE_COLLAPSE;
20   }
21
22   DatatypeBase(int whiteSpace) {
23     this.whiteSpace = whiteSpace;
24   }
25
26   int getWhiteSpace() {
27     return whiteSpace;
28   }
29
30   public boolean isValid(String JavaDoc str, ValidationContext vc) {
31     str = normalizeWhiteSpace(str);
32     return lexicallyAllows(str) && allowsValue(str, vc);
33   }
34
35   public void checkValid(String JavaDoc str, ValidationContext vc) throws DatatypeException {
36     if (!isValid(str, vc))
37       throw new DatatypeException();
38   }
39
40   public Object JavaDoc createValue(String JavaDoc str, ValidationContext vc) {
41     str = normalizeWhiteSpace(str);
42     if (!lexicallyAllows(str))
43       return null;
44     return getValue(str, vc);
45   }
46
47   final String JavaDoc normalizeWhiteSpace(String JavaDoc str) {
48     switch (whiteSpace) {
49     case WHITE_SPACE_COLLAPSE:
50       return collapseWhiteSpace(str);
51     case WHITE_SPACE_REPLACE:
52       return replaceWhiteSpace(str);
53     }
54     return str;
55   }
56     
57   /* Requires lexicallyAllows to be true. Must return same value as
58      getValue(str, vc) != null. */

59   boolean allowsValue(String JavaDoc str, ValidationContext vc) {
60     return true;
61   }
62
63   /* Requires lexicallyAllows to be true. Returns null if value does not satisfy
64      constraints on value space. */

65   abstract Object JavaDoc getValue(String JavaDoc str, ValidationContext vc);
66   
67   OrderRelation getOrderRelation() {
68     return null;
69   }
70
71   /* For datatypes that have a length. */
72   Measure getMeasure() {
73     return null;
74   }
75
76   static private final String JavaDoc collapseWhiteSpace(String JavaDoc s) {
77     int i = collapseStart(s);
78     if (i < 0)
79       return s;
80     StringBuffer JavaDoc buf = new StringBuffer JavaDoc(s.substring(0, i));
81     boolean collapsing = (i == 0 || s.charAt(i - 1) == ' ');
82     for (int len = s.length(); i < len; i++) {
83       char c = s.charAt(i);
84       switch (c) {
85       case '\r':
86       case '\n':
87       case '\t':
88       case ' ':
89         if (!collapsing) {
90           buf.append(' ');
91           collapsing = true;
92         }
93         break;
94       default:
95         collapsing = false;
96         buf.append(c);
97         break;
98       }
99     }
100     if (buf.length() > 0 && buf.charAt(buf.length() - 1) == ' ')
101       buf.setLength(buf.length() - 1);
102     return buf.toString();
103   }
104
105   static private final int collapseStart(String JavaDoc s) {
106     for (int i = 0, len = s.length(); i < len; i++) {
107       switch (s.charAt(i)) {
108       case ' ':
109         if (i == 0 || s.charAt(i - 1) == ' ' || i == len - 1)
110           return i;
111         break;
112       case '\r':
113       case '\n':
114       case '\t':
115         return i;
116       }
117     }
118     return -1;
119   }
120
121   static private final String JavaDoc replaceWhiteSpace(String JavaDoc s) {
122     int len = s.length();
123     for (int i = 0; i < len; i++)
124       switch (s.charAt(i)) {
125       case '\r':
126       case '\n':
127       case '\t':
128     {
129       char[] buf = s.toCharArray();
130       buf[i] = ' ';
131       for (++i; i < len; i++)
132         switch (buf[i]) {
133         case '\r':
134         case '\n':
135         case '\t':
136           buf[i] = ' ';
137         }
138       return new String JavaDoc(buf);
139     }
140       }
141     return s;
142   }
143
144   DatatypeBase getPrimitive() {
145     return this;
146   }
147
148   public boolean isContextDependent() {
149     return false;
150   }
151
152   public boolean alwaysValid() {
153     return false;
154   }
155
156   public int getIdType() {
157     return ID_TYPE_NULL;
158   }
159
160   public int valueHashCode(Object JavaDoc value) {
161     return value.hashCode();
162   }
163
164   public boolean sameValue(Object JavaDoc value1, Object JavaDoc value2) {
165     return value1.equals(value2);
166   }
167
168   public DatatypeStreamingValidator createStreamingValidator(ValidationContext vc) {
169     return new StreamingValidatorImpl(this, vc);
170   }
171 }
172
Popular Tags