1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 package org.exolab.castor.xml.schema;
37
38 import java.util.Enumeration;
39
40 import org.exolab.castor.xml.ValidationException;
41
42
43
44
45
46
47
48
49 public abstract class SimpleType extends XMLType implements Referable {
50
51
52
53
54 public static final String FINAL_ALL = "#all";
55
56
57
58
59 public static final String FINAL_LIST = "list";
60
61
62
63
64 public static final String FINAL_UNION = "union";
65
66
67
68
69 public static final String FINAL_RESTRICTION = "restriction";
70
71
72
73
74 private final FacetList _facets = new FacetList();
75
76
77
78
79 private String _final = null;
80
81
82
83
84 private Structure _parent = null;
85
86
87
88
89 private int _typeCode = SimpleTypesFactory.INVALID_TYPE;
90
91
92
93
94 private boolean _redefinition = false;
95
96
97
98
99
100
101 public void addFacet(final Facet facet) {
102
103 if (facet == null) {
104 return;
105 }
106
107 String name = facet.getName();
108
109 if (name == null) {
110 return;
111 }
112
113 _facets.add(facet);
114
115 }
116
117
118
119
120
121
122
123 public Facet getFacet(final String name) {
124 Enumeration<Facet> facets = getFacets(name);
125
126 if (facets == null) {
127 return null;
128 }
129
130 return facets.nextElement();
131 }
132
133
134
135
136
137
138
139 public Enumeration<Facet> getFacets(final String name) {
140 FacetListEnumerator fle = null;
141 SimpleType datatype = (SimpleType) getBaseType();
142 if (datatype != null) {
143 fle = (FacetListEnumerator) datatype.getFacets(name);
144 }
145 fle = new FacetListEnumerator(_facets, fle);
146 fle.setMask(name);
147 return fle;
148 }
149
150
151
152
153
154
155 public Enumeration<Facet> getFacets() {
156 FacetListEnumerator fle = null;
157 SimpleType datatype = (SimpleType) getBaseType();
158 if (datatype != null) {
159 fle = (FacetListEnumerator) datatype.getFacets();
160 }
161 fle = new FacetListEnumerator(_facets, fle);
162 return fle;
163 }
164
165
166
167
168
169
170
171 public String getFinal() {
172 return _final;
173 }
174
175
176
177
178
179
180 public Enumeration<Facet> getLocalFacets() {
181 if (_facets == null) {
182 return null;
183 }
184 return _facets.enumerate();
185 }
186
187
188
189
190
191
192
193
194
195
196
197 public Enumeration<Facet> getEffectiveFacets() {
198 final Enumeration<Facet> localFacets = getLocalFacets();
199 final SimpleType baseType = (SimpleType) getBaseType();
200 if (baseType == null) {
201
202 return localFacets;
203 }
204 final Enumeration<Facet> effectiveBaseFacets = baseType.getEffectiveFacets();
205 if (localFacets == null) {
206
207
208 return effectiveBaseFacets;
209 }
210
211 final FacetList filteredBaseFacets = new FacetList();
212 OUTER: while (effectiveBaseFacets.hasMoreElements()) {
213 final Facet baseFacet = effectiveBaseFacets.nextElement();
214
215
216 for (int i = 0; i < _facets.size(); i++) {
217 final Facet localFacet = _facets.get(i);
218 if (localFacet.overridesBase(baseFacet)) {
219 continue OUTER;
220 }
221 }
222
223 filteredBaseFacets.add(baseFacet);
224 }
225 return new FacetListEnumerator(_facets, (FacetListEnumerator) filteredBaseFacets.enumerate());
226 }
227
228
229
230
231 public SimpleType getBuiltInBaseType() {
232 SimpleType base = this;
233 while ((base != null) && (!SimpleTypesFactory.isBuiltInType(base.getTypeCode()))) {
234 base = (SimpleType) base.getBaseType();
235 }
236 return base;
237 }
238
239
240
241
242
243
244
245 public Structure getParent() {
246 return _parent;
247 }
248
249
250
251
252
253
254
255 public String getReferenceId() {
256 return "datatype:" + getName();
257 }
258
259
260
261
262
263
264
265 public boolean hasFacet(final String name) {
266 if (name == null) {
267 return false;
268 }
269 for (int i = 0; i < _facets.size(); i++) {
270 Facet facet = _facets.get(i);
271 if (name.equals(facet.getName())) {
272 return true;
273 }
274 }
275 return false;
276 }
277
278
279
280
281
282
283 public boolean isBuiltInType() {
284 return SimpleTypesFactory.isBuiltInType(_typeCode);
285 }
286
287
288
289
290
291
292 public boolean isNumericType() {
293 if (!isBuiltInType()) {
294 return ((SimpleType) getBaseType()).isNumericType();
295 }
296 return false;
297 }
298
299
300
301
302
303
304 public boolean isDateTimeType() {
305 return SimpleTypesFactory.isDateTimeType(_typeCode);
306 }
307
308
309
310
311
312
313 public boolean isRedefined() {
314 return _redefinition;
315 }
316
317
318
319
320 public void setRedefined() {
321 _redefinition = true;
322 }
323
324
325
326
327
328
329 public int getTypeCode() {
330 return _typeCode;
331 }
332
333
334
335
336 void setTypeCode(final int code) {
337 _typeCode = code;
338 }
339
340
341
342
343
344
345
346
347
348
349 public Long getLength() {
350 Facet lengthFacet = getFacet(Facet.LENGTH);
351 if (lengthFacet == null)
352 return null;
353
354 try {
355 return Long.valueOf(lengthFacet.toLong());
356 } catch (Exception e) {
357 return null;
358 }
359 }
360
361
362
363
364 public Long getMinLength() {
365 Facet minLengthFacet = getFacet(Facet.MIN_LENGTH);
366 if (minLengthFacet == null)
367 return null;
368
369 try {
370 return Long.valueOf(minLengthFacet.toLong());
371 } catch (Exception e) {
372 return null;
373 }
374 }
375
376
377
378
379 public Long getMaxLength() {
380 Facet maxLengthFacet = getFacet(Facet.MAX_LENGTH);
381 if (maxLengthFacet == null)
382 return null;
383
384 try {
385 return Long.valueOf(maxLengthFacet.toLong());
386 } catch (Exception e) {
387 return null;
388 }
389 }
390
391
392
393
394
395
396
397
398
399
400
401
402 public boolean removeFacet(Facet facet) {
403 if (facet == null)
404 return false;
405 return _facets.remove(facet);
406 }
407
408
409
410
411
412
413
414
415
416
417
418
419 public boolean removeFacet(String name) {
420 if (name == null)
421 return false;
422 for (int i = _facets.size() - 1; i > 0; i--) {
423 Facet facet = _facets.get(i);
424 if (name.equals(facet.getName())) {
425 _facets.remove(i);
426 return true;
427 }
428 }
429 return false;
430 }
431
432
433
434
435
436
437
438
439 public void setFinal(String finalValue) {
440 if ((finalValue == null) || finalValue.equals(FINAL_ALL) || finalValue.equals(FINAL_UNION)
441 || finalValue.equals(FINAL_LIST) || finalValue.equals(FINAL_RESTRICTION)) {
442 _final = finalValue;
443 } else {
444 String err = "The value '" + finalValue + "' is not a valid" + "value of the final property.";
445 throw new IllegalArgumentException(err);
446 }
447 }
448
449
450
451
452
453
454 public short getStructureType() {
455 return Structure.SIMPLE_TYPE;
456 }
457
458
459
460
461
462
463 public void validate() throws ValidationException {
464 final Enumeration<Facet> localFacets = getLocalFacets();
465 final SimpleType datatype = (SimpleType) getBaseType();
466 if (localFacets != null) {
467 while (localFacets.hasMoreElements()) {
468 final Facet facet = localFacets.nextElement();
469 Enumeration<Facet> baseFacets = null;
470 if (datatype != null) {
471 baseFacets = datatype.getFacets();
472 }
473 try {
474 facet.checkConstraints(getLocalFacets(), baseFacets);
475 } catch (SchemaException e) {
476 throw new ValidationException("Facet validation failed for type '" + getName() + "'", e);
477 }
478 }
479 }
480
481
482
483 }
484
485
486
487
488
489
490
491 protected SimpleType createReference(String name) {
492 return new SimpleTypeReference(getSchema(), name);
493 }
494
495
496
497
498
499
500
501
502 protected static SimpleType resolveReference(SimpleType simpleType) {
503 return (SimpleType) simpleType.getType();
504 }
505
506
507
508
509
510
511
512 protected void setParent(Structure parent) {
513 this._parent = parent;
514 }
515
516
517
518
519
520
521 protected void copyFacets(SimpleType target) {
522 target._facets.add(_facets);
523 }
524
525
526
527
528
529
530
531 public int getNumberOfFacets(final String name) {
532 int counter = 0;
533 for (Enumeration<Facet> enumerator = getFacets(); enumerator.hasMoreElements();) {
534 Facet facet = enumerator.nextElement();
535 if (facet.getName().equals(name)) {
536 counter++;
537 }
538 }
539 return counter;
540 }
541
542 }