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