1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49:
50:
55: public class SimpleType
56: extends Type
57: implements Datatype
58: {
59:
60:
63: public static final int ANY = 0;
64:
65:
68: public static final int ATOMIC = 1;
69:
70:
73: public static final int LIST = 2;
74:
75:
78: public static final int UNION = 3;
79:
80: public static final int ID_TYPE_NULL = 0;
81: public static final int ID_TYPE_ID = 1;
82: public static final int ID_TYPE_IDREF = 2;
83: public static final int ID_TYPE_IDREFS = 3;
84:
85:
88: public final int variety;
89:
90:
93: public Set facets;
94:
95:
98: public int fundamentalFacets;
99:
100:
104: public final SimpleType baseType;
105:
106:
109: public final Annotation annotation;
110:
111: public SimpleType(QName name, int variety, Set facets,
112: int fundamentalFacets, SimpleType baseType,
113: Annotation annotation)
114: {
115: super(name);
116: this.variety = variety;
117: this.facets = facets;
118: this.fundamentalFacets = fundamentalFacets;
119: this.baseType = baseType;
120: this.annotation = annotation;
121: }
122:
123:
126: public boolean isValid(String value, ValidationContext context)
127: {
128: try
129: {
130: checkValid(value, context);
131: return true;
132: }
133: catch (DatatypeException e)
134: {
135: return false;
136: }
137: }
138:
139: public void checkValid(String value, ValidationContext context)
140: throws DatatypeException
141: {
142: if (facets != null && !facets.isEmpty())
143: {
144: Object parsedValue = createValue(value, context);
145: for (Iterator i = facets.iterator(); i.hasNext(); )
146: {
147: Facet facet = (Facet) i.next();
148: switch (facet.type)
149: {
150: case Facet.LENGTH:
151: LengthFacet lf = (LengthFacet) facet;
152: if (value.length() != lf.value)
153: throw new DatatypeException("invalid length");
154: break;
155: case Facet.MIN_LENGTH:
156: MinLengthFacet nlf = (MinLengthFacet) facet;
157: if (value.length() < nlf.value)
158: throw new DatatypeException("invalid minimum length");
159: break;
160: case Facet.MAX_LENGTH:
161: MaxLengthFacet xlf = (MaxLengthFacet) facet;
162: if (value.length() > xlf.value)
163: throw new DatatypeException("invalid maximum length");
164: break;
165: case Facet.PATTERN:
166: PatternFacet pf = (PatternFacet) facet;
167: Matcher matcher = pf.value.matcher(value);
168: if (!matcher.find())
169: throw new DatatypeException("invalid match for pattern");
170: break;
171: case Facet.ENUMERATION:
172:
173: break;
174: case Facet.WHITESPACE:
175:
176: break;
177: case Facet.MAX_INCLUSIVE:
178: MaxInclusiveFacet xif = (MaxInclusiveFacet) facet;
179: if (!xif.matches(parsedValue))
180: throw new DatatypeException("beyond upper bound");
181: break;
182: case Facet.MAX_EXCLUSIVE:
183: MaxExclusiveFacet xef = (MaxExclusiveFacet) facet;
184: if (!xef.matches(parsedValue))
185: throw new DatatypeException("beyond upper bound");
186: break;
187: case Facet.MIN_EXCLUSIVE:
188: MinExclusiveFacet nef = (MinExclusiveFacet) facet;
189: if (!nef.matches(parsedValue))
190: throw new DatatypeException("beyond lower bound");
191: break;
192: case Facet.MIN_INCLUSIVE:
193: MinInclusiveFacet nif = (MinInclusiveFacet) facet;
194: if (!nif.matches(parsedValue))
195: throw new DatatypeException("beyond lower bound");
196: break;
197: case Facet.TOTAL_DIGITS:
198: TotalDigitsFacet tdf = (TotalDigitsFacet) facet;
199: if (countDigits(value, true) > tdf.value)
200: throw new DatatypeException("too many digits");
201: break;
202: case Facet.FRACTION_DIGITS:
203: FractionDigitsFacet fdf = (FractionDigitsFacet) facet;
204: if (countDigits(value, false) > fdf.value)
205: throw new DatatypeException("too many fraction digits");
206: break;
207: }
208: }
209: }
210: }
211:
212: private static int countDigits(String value, boolean any)
213: {
214: int count = 0;
215: int len = value.length();
216: boolean seenDecimal = false;
217: for (int i = 0; i < len; i++)
218: {
219: char c = value.charAt(i);
220: if (c == 0x2e)
221: seenDecimal = true;
222: else if (c >= 0x30 && c <= 0x39 && (any || seenDecimal))
223: count++;
224: }
225: return count;
226: }
227:
228:
229: public DatatypeStreamingValidator createStreamingValidator(ValidationContext context)
230: {
231: throw new UnsupportedOperationException();
232: }
233:
234: public Object createValue(String literal, ValidationContext context) {
235: return literal;
236: }
237:
238: public boolean sameValue(Object value1, Object value2) {
239: return value1.equals(value2);
240: }
241:
242: public int valueHashCode(Object value) {
243: return value.hashCode();
244: }
245:
246: public int getIdType()
247: {
248: return ID_TYPE_NULL;
249: }
250:
251: public boolean isContextDependent()
252: {
253: return false;
254: }
255:
256: }