KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > impl > dv > xs > XSSimpleTypeDecl


1 /*
2  * Copyright 2001-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.xerces.impl.dv.xs;
18
19 import java.util.StringTokenizer JavaDoc;
20 import java.util.Vector JavaDoc;
21
22 import org.apache.xerces.impl.Constants;
23 import org.apache.xerces.impl.dv.DatatypeException;
24 import org.apache.xerces.impl.dv.InvalidDatatypeFacetException;
25 import org.apache.xerces.impl.dv.InvalidDatatypeValueException;
26 import org.apache.xerces.impl.dv.ValidatedInfo;
27 import org.apache.xerces.impl.dv.ValidationContext;
28 import org.apache.xerces.impl.dv.XSFacets;
29 import org.apache.xerces.impl.dv.XSSimpleType;
30 import org.apache.xerces.impl.xpath.regex.RegularExpression;
31 import org.apache.xerces.impl.xs.SchemaSymbols;
32 import org.apache.xerces.impl.xs.util.ShortListImpl;
33 import org.apache.xerces.impl.xs.util.StringListImpl;
34 import org.apache.xerces.impl.xs.util.XSObjectListImpl;
35 import org.apache.xerces.util.XMLChar;
36 import org.apache.xerces.xni.NamespaceContext;
37 import org.apache.xerces.xs.ShortList;
38 import org.apache.xerces.xs.StringList;
39 import org.apache.xerces.xs.XSAnnotation;
40 import org.apache.xerces.xs.XSConstants;
41 import org.apache.xerces.xs.XSFacet;
42 import org.apache.xerces.xs.XSMultiValueFacet;
43 import org.apache.xerces.xs.XSNamespaceItem;
44 import org.apache.xerces.xs.XSObjectList;
45 import org.apache.xerces.xs.XSSimpleTypeDefinition;
46 import org.apache.xerces.xs.XSTypeDefinition;
47 import org.apache.xerces.xs.datatypes.ObjectList;
48 import org.w3c.dom.TypeInfo JavaDoc;
49
50 /**
51  * @xerces.internal
52  *
53  * @author Sandy Gao, IBM
54  * @author Neeraj Bajaj, Sun Microsystems, inc.
55  *
56  * @version $Id: XSSimpleTypeDecl.java,v 1.74 2005/06/24 17:19:21 mrglavas Exp $
57  */

58 public class XSSimpleTypeDecl implements XSSimpleType, TypeInfo JavaDoc {
59     
60     static final short DV_STRING = PRIMITIVE_STRING;
61     static final short DV_BOOLEAN = PRIMITIVE_BOOLEAN;
62     static final short DV_DECIMAL = PRIMITIVE_DECIMAL;
63     static final short DV_FLOAT = PRIMITIVE_FLOAT;
64     static final short DV_DOUBLE = PRIMITIVE_DOUBLE;
65     static final short DV_DURATION = PRIMITIVE_DURATION;
66     static final short DV_DATETIME = PRIMITIVE_DATETIME;
67     static final short DV_TIME = PRIMITIVE_TIME;
68     static final short DV_DATE = PRIMITIVE_DATE;
69     static final short DV_GYEARMONTH = PRIMITIVE_GYEARMONTH;
70     static final short DV_GYEAR = PRIMITIVE_GYEAR;
71     static final short DV_GMONTHDAY = PRIMITIVE_GMONTHDAY;
72     static final short DV_GDAY = PRIMITIVE_GDAY;
73     static final short DV_GMONTH = PRIMITIVE_GMONTH;
74     static final short DV_HEXBINARY = PRIMITIVE_HEXBINARY;
75     static final short DV_BASE64BINARY = PRIMITIVE_BASE64BINARY;
76     static final short DV_ANYURI = PRIMITIVE_ANYURI;
77     static final short DV_QNAME = PRIMITIVE_QNAME;
78     static final short DV_PRECISIONDECIMAL = PRIMITIVE_PRECISIONDECIMAL;
79     static final short DV_NOTATION = PRIMITIVE_NOTATION;
80     
81     static final short DV_ANYSIMPLETYPE = 0;
82     static final short DV_ID = DV_NOTATION + 1;
83     static final short DV_IDREF = DV_NOTATION + 2;
84     static final short DV_ENTITY = DV_NOTATION + 3;
85     static final short DV_INTEGER = DV_NOTATION + 4;
86     static final short DV_LIST = DV_NOTATION + 5;
87     static final short DV_UNION = DV_NOTATION + 6;
88     static final short DV_YEARMONTHDURATION = DV_NOTATION + 7;
89     static final short DV_DAYTIMEDURATION = DV_NOTATION + 8;
90     static final short DV_ANYATOMICTYPE = DV_NOTATION + 9;
91     
92     static final TypeValidator[] fDVs = {
93             new AnySimpleDV(),
94             new StringDV(),
95             new BooleanDV(),
96             new DecimalDV(),
97             new FloatDV(),
98             new DoubleDV(),
99             new DurationDV(),
100             new DateTimeDV(),
101             new TimeDV(),
102             new DateDV(),
103             new YearMonthDV(),
104             new YearDV(),
105             new MonthDayDV(),
106             new DayDV(),
107             new MonthDV(),
108             new HexBinaryDV(),
109             new Base64BinaryDV(),
110             new AnyURIDV(),
111             new QNameDV(),
112             new PrecisionDecimalDV(), // XML Schema 1.1 type
113
new QNameDV(), // notation use the same one as qname
114
new IDDV(),
115             new IDREFDV(),
116             new EntityDV(),
117             new IntegerDV(),
118             new ListDV(),
119             new UnionDV(),
120             new YearMonthDurationDV(), // XML Schema 1.1 type
121
new DayTimeDurationDV(), // XML Schema 1.1 type
122
new AnyAtomicDV() // XML Schema 1.1 type
123
};
124     
125     static final short NORMALIZE_NONE = 0;
126     static final short NORMALIZE_TRIM = 1;
127     static final short NORMALIZE_FULL = 2;
128     static final short[] fDVNormalizeType = {
129             NORMALIZE_NONE, //AnySimpleDV(),
130
NORMALIZE_FULL, //StringDV(),
131
NORMALIZE_TRIM, //BooleanDV(),
132
NORMALIZE_TRIM, //DecimalDV(),
133
NORMALIZE_TRIM, //FloatDV(),
134
NORMALIZE_TRIM, //DoubleDV(),
135
NORMALIZE_TRIM, //DurationDV(),
136
NORMALIZE_TRIM, //DateTimeDV(),
137
NORMALIZE_TRIM, //TimeDV(),
138
NORMALIZE_TRIM, //DateDV(),
139
NORMALIZE_TRIM, //YearMonthDV(),
140
NORMALIZE_TRIM, //YearDV(),
141
NORMALIZE_TRIM, //MonthDayDV(),
142
NORMALIZE_TRIM, //DayDV(),
143
NORMALIZE_TRIM, //MonthDV(),
144
NORMALIZE_TRIM, //HexBinaryDV(),
145
NORMALIZE_NONE, //Base64BinaryDV(), // Base64 know how to deal with spaces
146
NORMALIZE_TRIM, //AnyURIDV(),
147
NORMALIZE_TRIM, //QNameDV(),
148
NORMALIZE_TRIM, //PrecisionDecimalDV() (Schema 1.1)
149
NORMALIZE_TRIM, //QNameDV(), // notation
150
NORMALIZE_TRIM, //IDDV(),
151
NORMALIZE_TRIM, //IDREFDV(),
152
NORMALIZE_TRIM, //EntityDV(),
153
NORMALIZE_TRIM, //IntegerDV(),
154
NORMALIZE_FULL, //ListDV(),
155
NORMALIZE_NONE, //UnionDV(),
156
NORMALIZE_TRIM, //YearMonthDurationDV() (Schema 1.1)
157
NORMALIZE_TRIM, //DayTimeDurationDV() (Schema 1.1)
158
NORMALIZE_NONE, //AnyAtomicDV() (Schema 1.1)
159
};
160     
161     static final short SPECIAL_PATTERN_NONE = 0;
162     static final short SPECIAL_PATTERN_NMTOKEN = 1;
163     static final short SPECIAL_PATTERN_NAME = 2;
164     static final short SPECIAL_PATTERN_NCNAME = 3;
165     
166     static final String JavaDoc[] SPECIAL_PATTERN_STRING = {
167             "NONE", "NMTOKEN", "Name", "NCName"
168     };
169     
170     static final String JavaDoc[] WS_FACET_STRING = {
171             "preserve", "replace", "collapse"
172     };
173     
174     static final String JavaDoc URI_SCHEMAFORSCHEMA = "http://www.w3.org/2001/XMLSchema";
175     static final String JavaDoc ANY_TYPE = "anyType";
176     
177     // XML Schema 1.1 type constants
178
public static final short YEARMONTHDURATION_DT = 46;
179     public static final short DAYTIMEDURATION_DT = 47;
180     public static final short PRECISIONDECIMAL_DT = 48;
181     public static final short ANYATOMICTYPE_DT = 49;
182     
183     // DOM Level 3 TypeInfo Derivation Method constants
184
static final int DERIVATION_ANY = 0;
185     static final int DERIVATION_RESTRICTION = 1;
186     static final int DERIVATION_EXTENSION = 2;
187     static final int DERIVATION_UNION = 4;
188     static final int DERIVATION_LIST = 8;
189     
190     static final ValidationContext fEmptyContext = new ValidationContext() {
191         public boolean needFacetChecking() {
192             return true;
193         }
194         public boolean needExtraChecking() {
195             return false;
196         }
197         public boolean needToNormalize() {
198             return true;
199         }
200         public boolean useNamespaces () {
201             return true;
202         }
203         public boolean isEntityDeclared (String JavaDoc name) {
204             return false;
205         }
206         public boolean isEntityUnparsed (String JavaDoc name) {
207             return false;
208         }
209         public boolean isIdDeclared (String JavaDoc name) {
210             return false;
211         }
212         public void addId(String JavaDoc name) {
213         }
214         public void addIdRef(String JavaDoc name) {
215         }
216         public String JavaDoc getSymbol (String JavaDoc symbol) {
217             return symbol.intern();
218         }
219         public String JavaDoc getURI(String JavaDoc prefix) {
220             return null;
221         }
222     };
223     
224     // this will be true if this is a static XSSimpleTypeDecl
225
// and hence must remain immutable (i.e., applyFacets
226
// may not be permitted to have any effect).
227
private boolean fIsImmutable = false;
228     
229     private XSSimpleTypeDecl fItemType;
230     private XSSimpleTypeDecl[] fMemberTypes;
231     // The most specific built-in type kind.
232
private short fBuiltInKind;
233     
234     private String JavaDoc fTypeName;
235     private String JavaDoc fTargetNamespace;
236     private short fFinalSet = 0;
237     private XSSimpleTypeDecl fBase;
238     private short fVariety = -1;
239     private short fValidationDV = -1;
240     
241     private short fFacetsDefined = 0;
242     private short fFixedFacet = 0;
243     
244     //for constraining facets
245
private short fWhiteSpace = 0;
246     private int fLength = -1;
247     private int fMinLength = -1;
248     private int fMaxLength = -1;
249     private int fTotalDigits = -1;
250     private int fFractionDigits = -1;
251     private Vector JavaDoc fPattern;
252     private Vector JavaDoc fPatternStr;
253     private Vector JavaDoc fEnumeration;
254     private short[] fEnumerationType;
255     private ShortList[] fEnumerationItemType; // used in case fenumerationType value is LIST or LISTOFUNION
256
private ShortList fEnumerationTypeList;
257     private ObjectList fEnumerationItemTypeList;
258     private StringList fLexicalPattern;
259     private StringList fLexicalEnumeration;
260     private ObjectList fActualEnumeration;
261     private Object JavaDoc fMaxInclusive;
262     private Object JavaDoc fMaxExclusive;
263     private Object JavaDoc fMinExclusive;
264     private Object JavaDoc fMinInclusive;
265     
266     // annotations for constraining facets
267
public XSAnnotation lengthAnnotation;
268     public XSAnnotation minLengthAnnotation;
269     public XSAnnotation maxLengthAnnotation;
270     public XSAnnotation whiteSpaceAnnotation;
271     public XSAnnotation totalDigitsAnnotation;
272     public XSAnnotation fractionDigitsAnnotation;
273     public XSObjectListImpl patternAnnotations;
274     public XSObjectList enumerationAnnotations;
275     public XSAnnotation maxInclusiveAnnotation;
276     public XSAnnotation maxExclusiveAnnotation;
277     public XSAnnotation minInclusiveAnnotation;
278     public XSAnnotation minExclusiveAnnotation;
279     
280     // facets as objects
281
private XSObjectListImpl fFacets;
282     
283     // enumeration and pattern facets
284
private XSObjectListImpl fMultiValueFacets;
285     
286     // simpleType annotations
287
private XSObjectList fAnnotations = null;
288     
289     private short fPatternType = SPECIAL_PATTERN_NONE;
290     
291     // for fundamental facets
292
private short fOrdered;
293     private boolean fFinite;
294     private boolean fBounded;
295     private boolean fNumeric;
296     
297     // default constructor
298
public XSSimpleTypeDecl(){}
299     
300     //Create a new built-in primitive types (and id/idref/entity/integer/yearMonthDuration)
301
protected XSSimpleTypeDecl(XSSimpleTypeDecl base, String JavaDoc name, short validateDV,
302             short ordered, boolean bounded, boolean finite,
303             boolean numeric, boolean isImmutable, short builtInKind) {
304         fIsImmutable = isImmutable;
305         fBase = base;
306         fTypeName = name;
307         fTargetNamespace = URI_SCHEMAFORSCHEMA;
308         // To simplify the code for anySimpleType, we treat it as an atomic type
309
fVariety = VARIETY_ATOMIC;
310         fValidationDV = validateDV;
311         fFacetsDefined = FACET_WHITESPACE;
312         if (validateDV == DV_STRING) {
313             fWhiteSpace = WS_PRESERVE;
314         } else {
315             fWhiteSpace = WS_COLLAPSE;
316             fFixedFacet = FACET_WHITESPACE;
317         }
318         this.fOrdered = ordered;
319         this.fBounded = bounded;
320         this.fFinite = finite;
321         this.fNumeric = numeric;
322         fAnnotations = null;
323         
324         // Specify the build in kind for this primitive type
325
fBuiltInKind = builtInKind;
326     }
327     
328     //Create a new simple type for restriction for built-in types
329
protected XSSimpleTypeDecl(XSSimpleTypeDecl base, String JavaDoc name, String JavaDoc uri, short finalSet, boolean isImmutable,
330             XSObjectList annotations, short builtInKind) {
331         this(base, name, uri, finalSet, isImmutable, annotations);
332         // Specify the build in kind for this built-in type
333
fBuiltInKind = builtInKind;
334     }
335     
336     //Create a new simple type for restriction.
337
protected XSSimpleTypeDecl(XSSimpleTypeDecl base, String JavaDoc name, String JavaDoc uri, short finalSet, boolean isImmutable,
338             XSObjectList annotations) {
339         fBase = base;
340         fTypeName = name;
341         fTargetNamespace = uri;
342         fFinalSet = finalSet;
343         fAnnotations = annotations;
344         
345         fVariety = fBase.fVariety;
346         fValidationDV = fBase.fValidationDV;
347         switch (fVariety) {
348         case VARIETY_ATOMIC:
349             break;
350         case VARIETY_LIST:
351             fItemType = fBase.fItemType;
352             break;
353         case VARIETY_UNION:
354             fMemberTypes = fBase.fMemberTypes;
355             break;
356         }
357         
358         // always inherit facets from the base.
359
// in case a type is created, but applyFacets is not called
360
fLength = fBase.fLength;
361         fMinLength = fBase.fMinLength;
362         fMaxLength = fBase.fMaxLength;
363         fPattern = fBase.fPattern;
364         fPatternStr = fBase.fPatternStr;
365         fEnumeration = fBase.fEnumeration;
366         fEnumerationType = fBase.fEnumerationType;
367         fEnumerationItemType = fBase.fEnumerationItemType;
368         fWhiteSpace = fBase.fWhiteSpace;
369         fMaxExclusive = fBase.fMaxExclusive;
370         fMaxInclusive = fBase.fMaxInclusive;
371         fMinExclusive = fBase.fMinExclusive;
372         fMinInclusive = fBase.fMinInclusive;
373         fTotalDigits = fBase.fTotalDigits;
374         fFractionDigits = fBase.fFractionDigits;
375         fPatternType = fBase.fPatternType;
376         fFixedFacet = fBase.fFixedFacet;
377         fFacetsDefined = fBase.fFacetsDefined;
378         
379         //we also set fundamental facets information in case applyFacets is not called.
380
caclFundamentalFacets();
381         fIsImmutable = isImmutable;
382         
383         // Inherit from the base type
384
fBuiltInKind = base.fBuiltInKind;
385     }
386     
387     //Create a new simple type for list.
388
protected XSSimpleTypeDecl(String JavaDoc name, String JavaDoc uri, short finalSet, XSSimpleTypeDecl itemType, boolean isImmutable,
389             XSObjectList annotations) {
390         fBase = fAnySimpleType;
391         fTypeName = name;
392         fTargetNamespace = uri;
393         fFinalSet = finalSet;
394         fAnnotations = annotations;
395         
396         fVariety = VARIETY_LIST;
397         fItemType = (XSSimpleTypeDecl)itemType;
398         fValidationDV = DV_LIST;
399         fFacetsDefined = FACET_WHITESPACE;
400         fFixedFacet = FACET_WHITESPACE;
401         fWhiteSpace = WS_COLLAPSE;
402         
403         //setting fundamental facets
404
caclFundamentalFacets();
405         fIsImmutable = isImmutable;
406         
407         // Values of this type are lists
408
fBuiltInKind = XSConstants.LIST_DT;
409     }
410     
411     //Create a new simple type for union.
412
protected XSSimpleTypeDecl(String JavaDoc name, String JavaDoc uri, short finalSet, XSSimpleTypeDecl[] memberTypes,
413             XSObjectList annotations) {
414         fBase = fAnySimpleType;
415         fTypeName = name;
416         fTargetNamespace = uri;
417         fFinalSet = finalSet;
418         fAnnotations = annotations;
419         
420         fVariety = VARIETY_UNION;
421         fMemberTypes = memberTypes;
422         fValidationDV = DV_UNION;
423         // even for union, we set whitespace to something
424
// this will never be used, but we can use fFacetsDefined to check
425
// whether applyFacets() is allwwed: it's not allowed
426
// if fFacetsDefined != 0
427
fFacetsDefined = FACET_WHITESPACE;
428         fWhiteSpace = WS_COLLAPSE;
429         
430         //setting fundamental facets
431
caclFundamentalFacets();
432         // none of the schema-defined types are unions, so just set
433
// fIsImmutable to false.
434
fIsImmutable = false;
435         
436         // No value can be of this type, so it's unavailable.
437
fBuiltInKind = XSConstants.UNAVAILABLE_DT;
438     }
439     
440     //set values for restriction.
441
protected XSSimpleTypeDecl setRestrictionValues(XSSimpleTypeDecl base, String JavaDoc name, String JavaDoc uri, short finalSet,
442             XSObjectList annotations) {
443         //decline to do anything if the object is immutable.
444
if(fIsImmutable) return null;
445         fBase = base;
446         fTypeName = name;
447         fTargetNamespace = uri;
448         fFinalSet = finalSet;
449         fAnnotations = annotations;
450         
451         fVariety = fBase.fVariety;
452         fValidationDV = fBase.fValidationDV;
453         switch (fVariety) {
454         case VARIETY_ATOMIC:
455             break;
456         case VARIETY_LIST:
457             fItemType = fBase.fItemType;
458             break;
459         case VARIETY_UNION:
460             fMemberTypes = fBase.fMemberTypes;
461             break;
462         }
463         
464         // always inherit facets from the base.
465
// in case a type is created, but applyFacets is not called
466
fLength = fBase.fLength;
467         fMinLength = fBase.fMinLength;
468         fMaxLength = fBase.fMaxLength;
469         fPattern = fBase.fPattern;
470         fPatternStr = fBase.fPatternStr;
471         fEnumeration = fBase.fEnumeration;
472         fEnumerationType = fBase.fEnumerationType;
473         fEnumerationItemType = fBase.fEnumerationItemType;
474         fWhiteSpace = fBase.fWhiteSpace;
475         fMaxExclusive = fBase.fMaxExclusive;
476         fMaxInclusive = fBase.fMaxInclusive;
477         fMinExclusive = fBase.fMinExclusive;
478         fMinInclusive = fBase.fMinInclusive;
479         fTotalDigits = fBase.fTotalDigits;
480         fFractionDigits = fBase.fFractionDigits;
481         fPatternType = fBase.fPatternType;
482         fFixedFacet = fBase.fFixedFacet;
483         fFacetsDefined = fBase.fFacetsDefined;
484         
485         //we also set fundamental facets information in case applyFacets is not called.
486
caclFundamentalFacets();
487         
488         // Inherit from the base type
489
fBuiltInKind = base.fBuiltInKind;
490         
491         return this;
492     }
493     
494     //set values for list.
495
protected XSSimpleTypeDecl setListValues(String JavaDoc name, String JavaDoc uri, short finalSet, XSSimpleTypeDecl itemType,
496             XSObjectList annotations) {
497         //decline to do anything if the object is immutable.
498
if(fIsImmutable) return null;
499         fBase = fAnySimpleType;
500         fTypeName = name;
501         fTargetNamespace = uri;
502         fFinalSet = finalSet;
503         fAnnotations = annotations;
504         
505         fVariety = VARIETY_LIST;
506         fItemType = (XSSimpleTypeDecl)itemType;
507         fValidationDV = DV_LIST;
508         fFacetsDefined = FACET_WHITESPACE;
509         fFixedFacet = FACET_WHITESPACE;
510         fWhiteSpace = WS_COLLAPSE;
511         
512         //setting fundamental facets
513
caclFundamentalFacets();
514         
515         // Values of this type are lists
516
fBuiltInKind = XSConstants.LIST_DT;
517         
518         return this;
519     }
520     
521     //set values for union.
522
protected XSSimpleTypeDecl setUnionValues(String JavaDoc name, String JavaDoc uri, short finalSet, XSSimpleTypeDecl[] memberTypes,
523             XSObjectList annotations) {
524         //decline to do anything if the object is immutable.
525
if(fIsImmutable) return null;
526         fBase = fAnySimpleType;
527         fTypeName = name;
528         fTargetNamespace = uri;
529         fFinalSet = finalSet;
530         fAnnotations = annotations;
531         
532         fVariety = VARIETY_UNION;
533         fMemberTypes = memberTypes;
534         fValidationDV = DV_UNION;
535         // even for union, we set whitespace to something
536
// this will never be used, but we can use fFacetsDefined to check
537
// whether applyFacets() is allwwed: it's not allowed
538
// if fFacetsDefined != 0
539
fFacetsDefined = FACET_WHITESPACE;
540         fWhiteSpace = WS_COLLAPSE;
541         
542         //setting fundamental facets
543
caclFundamentalFacets();
544         
545         // No value can be of this type, so it's unavailable.
546
fBuiltInKind = XSConstants.UNAVAILABLE_DT;
547         
548         return this;
549     }
550     
551     public short getType () {
552         return XSConstants.TYPE_DEFINITION;
553     }
554     
555     public short getTypeCategory () {
556         return SIMPLE_TYPE;
557     }
558     
559     public String JavaDoc getName() {
560         return getAnonymous()?null:fTypeName;
561     }
562     
563     public String JavaDoc getTypeName() {
564         return fTypeName;
565     }
566     
567     public String JavaDoc getNamespace() {
568         return fTargetNamespace;
569     }
570     
571     public short getFinal(){
572         return fFinalSet;
573     }
574     
575     public boolean isFinal(short derivation) {
576         return (fFinalSet & derivation) != 0;
577     }
578     
579     public XSTypeDefinition getBaseType(){
580         return fBase;
581     }
582     
583     public boolean getAnonymous() {
584         return fAnonymous || (fTypeName == null);
585     }
586     
587     public short getVariety(){
588         // for anySimpleType, return absent variaty
589
return fValidationDV == DV_ANYSIMPLETYPE ? VARIETY_ABSENT : fVariety;
590     }
591     
592     public boolean isIDType(){
593         switch (fVariety) {
594         case VARIETY_ATOMIC:
595             return fValidationDV == DV_ID;
596         case VARIETY_LIST:
597             return fItemType.isIDType();
598         case VARIETY_UNION:
599             for (int i = 0; i < fMemberTypes.length; i++) {
600                 if (fMemberTypes[i].isIDType())
601                     return true;
602             }
603         }
604         return false;
605     }
606     
607     public short getWhitespace() throws DatatypeException{
608         if (fVariety == VARIETY_UNION) {
609             throw new DatatypeException("dt-whitespace", new Object JavaDoc[]{fTypeName});
610         }
611         return fWhiteSpace;
612     }
613     
614     public short getPrimitiveKind() {
615         if (fVariety == VARIETY_ATOMIC && fValidationDV != DV_ANYSIMPLETYPE) {
616             if (fValidationDV == DV_ID || fValidationDV == DV_IDREF || fValidationDV == DV_ENTITY) {
617                 return DV_STRING;
618             }
619             else if (fValidationDV == DV_INTEGER) {
620                 return DV_DECIMAL;
621             }
622             else if (Constants.SCHEMA_1_1_SUPPORT && (fValidationDV == DV_YEARMONTHDURATION || fValidationDV == DV_DAYTIMEDURATION)) {
623                 return DV_DURATION;
624             }
625             else {
626                 return fValidationDV;
627             }
628         }
629         else {
630             // REVISIT: error situation. runtime exception?
631
return (short)0;
632         }
633     }
634     
635     /**
636      * Returns the closest built-in type category this type represents or
637      * derived from. For example, if this simple type is a built-in derived
638      * type integer the <code>INTEGER_DV</code> is returned.
639      */

640     public short getBuiltInKind() {
641         return this.fBuiltInKind;
642     }
643     
644     /**
645      * If variety is <code>atomic</code> the primitive type definition (a
646      * built-in primitive datatype definition or the simple ur-type
647      * definition) is available, otherwise <code>null</code>.
648      */

649     public XSSimpleTypeDefinition getPrimitiveType() {
650         if (fVariety == VARIETY_ATOMIC && fValidationDV != DV_ANYSIMPLETYPE) {
651             XSSimpleTypeDecl pri = this;
652             // recursively get base, until we reach anySimpleType
653
while (pri.fBase != fAnySimpleType)
654                 pri = pri.fBase;
655             return pri;
656         }
657         else {
658             // REVISIT: error situation. runtime exception?
659
return null;
660         }
661     }
662     
663     /**
664      * If variety is <code>list</code> the item type definition (an atomic or
665      * union simple type definition) is available, otherwise
666      * <code>null</code>.
667      */

668     public XSSimpleTypeDefinition getItemType() {
669         if (fVariety == VARIETY_LIST) {
670             return fItemType;
671         }
672         else {
673             // REVISIT: error situation. runtime exception?
674
return null;
675         }
676     }
677     
678     /**
679      * If variety is <code>union</code> the list of member type definitions (a
680      * non-empty sequence of simple type definitions) is available,
681      * otherwise an empty <code>XSObjectList</code>.
682      */

683     public XSObjectList getMemberTypes() {
684         if (fVariety == VARIETY_UNION) {
685             return new XSObjectListImpl(fMemberTypes, fMemberTypes.length);
686         }
687         else {
688             return XSObjectListImpl.EMPTY_LIST;
689         }
690     }
691     
692     /**
693      * If <restriction> is chosen
694      */

695     public void applyFacets(XSFacets facets, short presentFacet, short fixedFacet, ValidationContext context)
696     throws InvalidDatatypeFacetException {
697         applyFacets(facets, presentFacet, fixedFacet, SPECIAL_PATTERN_NONE, context);
698     }
699     
700     /**
701      * built-in derived types by restriction
702      */

703     void applyFacets1(XSFacets facets, short presentFacet, short fixedFacet) {
704         
705         try {
706             applyFacets(facets, presentFacet, fixedFacet, SPECIAL_PATTERN_NONE, fDummyContext);
707         } catch (InvalidDatatypeFacetException e) {
708             // should never gets here, internel error
709
throw new RuntimeException JavaDoc("internal error");
710         }
711         // we've now applied facets; so lock this object:
712
fIsImmutable = true;
713     }
714     
715     /**
716      * built-in derived types by restriction
717      */

718     void applyFacets1(XSFacets facets, short presentFacet, short fixedFacet, short patternType) {
719         
720         try {
721             applyFacets(facets, presentFacet, fixedFacet, patternType, fDummyContext);
722         } catch (InvalidDatatypeFacetException e) {
723             // should never gets here, internel error
724
throw new RuntimeException JavaDoc("internal error");
725         }
726         // we've now applied facets; so lock this object:
727
fIsImmutable = true;
728     }
729     
730     /**
731      * If <restriction> is chosen, or built-in derived types by restriction
732      */

733     void applyFacets(XSFacets facets, short presentFacet, short fixedFacet, short patternType, ValidationContext context)
734     throws InvalidDatatypeFacetException {
735         
736         // if the object is immutable, should not apply facets...
737
if(fIsImmutable) return;
738         ValidatedInfo tempInfo = new ValidatedInfo();
739         
740         // clear facets. because we always inherit facets in the constructor
741
// REVISIT: in fact, we don't need to clear them.
742
// we can convert 5 string values (4 bounds + 1 enum) to actual values,
743
// store them somewhere, then do facet checking at once, instead of
744
// going through the following steps. (lots of checking are redundant:
745
// for example, ((presentFacet & FACET_XXX) != 0))
746

747         fFacetsDefined = 0;
748         fFixedFacet = 0;
749         
750         int result = 0 ;
751         
752         // step 1: parse present facets
753
short allowedFacet = fDVs[fValidationDV].getAllowedFacets();
754         
755         // length
756
if ((presentFacet & FACET_LENGTH) != 0) {
757             if ((allowedFacet & FACET_LENGTH) == 0) {
758                 reportError("cos-applicable-facets", new Object JavaDoc[]{"length", fTypeName});
759             } else {
760                 fLength = facets.length;
761                 lengthAnnotation = facets.lengthAnnotation;
762                 fFacetsDefined |= FACET_LENGTH;
763                 if ((fixedFacet & FACET_LENGTH) != 0)
764                     fFixedFacet |= FACET_LENGTH;
765             }
766         }
767         // minLength
768
if ((presentFacet & FACET_MINLENGTH) != 0) {
769             if ((allowedFacet & FACET_MINLENGTH) == 0) {
770                 reportError("cos-applicable-facets", new Object JavaDoc[]{"minLength", fTypeName});
771             } else {
772                 fMinLength = facets.minLength;
773                 minLengthAnnotation = facets.minLengthAnnotation;
774                 fFacetsDefined |= FACET_MINLENGTH;
775                 if ((fixedFacet & FACET_MINLENGTH) != 0)
776                     fFixedFacet |= FACET_MINLENGTH;
777             }
778         }
779         // maxLength
780
if ((presentFacet & FACET_MAXLENGTH) != 0) {
781             if ((allowedFacet & FACET_MAXLENGTH) == 0) {
782                 reportError("cos-applicable-facets", new Object JavaDoc[]{"maxLength", fTypeName});
783             } else {
784                 fMaxLength = facets.maxLength;
785                 maxLengthAnnotation = facets.maxLengthAnnotation;
786                 fFacetsDefined |= FACET_MAXLENGTH;
787                 if ((fixedFacet & FACET_MAXLENGTH) != 0)
788                     fFixedFacet |= FACET_MAXLENGTH;
789             }
790         }
791         // pattern
792
if ((presentF