KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2001, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package com.sun.org.apache.xerces.internal.impl.dv.xs;
59
60 import java.util.StringTokenizer JavaDoc;
61 import java.util.Vector JavaDoc;
62
63 import com.sun.org.apache.xerces.internal.impl.Constants;
64 import com.sun.org.apache.xerces.internal.impl.dv.DatatypeException;
65 import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeFacetException;
66 import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeValueException;
67 import com.sun.org.apache.xerces.internal.impl.dv.ValidatedInfo;
68 import com.sun.org.apache.xerces.internal.impl.dv.ValidationContext;
69 import com.sun.org.apache.xerces.internal.impl.dv.XSFacets;
70 import com.sun.org.apache.xerces.internal.impl.dv.XSSimpleType;
71 import com.sun.org.apache.xerces.internal.impl.xpath.regex.RegularExpression;
72 import com.sun.org.apache.xerces.internal.xs.StringList;
73 import com.sun.org.apache.xerces.internal.xs.XSAnnotation;
74 import com.sun.org.apache.xerces.internal.xs.XSConstants;
75 import com.sun.org.apache.xerces.internal.xs.XSFacet;
76 import com.sun.org.apache.xerces.internal.xs.XSMultiValueFacet;
77 import com.sun.org.apache.xerces.internal.xs.XSNamespaceItem;
78 import com.sun.org.apache.xerces.internal.xs.XSObjectList;
79 import com.sun.org.apache.xerces.internal.xs.XSSimpleTypeDefinition;
80 import com.sun.org.apache.xerces.internal.xs.XSTypeDefinition;
81 import com.sun.org.apache.xerces.internal.impl.xs.util.ShortListImpl;
82 import com.sun.org.apache.xerces.internal.impl.xs.util.StringListImpl;
83 import com.sun.org.apache.xerces.internal.impl.xs.util.XSObjectListImpl;
84 import com.sun.org.apache.xerces.internal.util.XMLChar;
85 import com.sun.org.apache.xerces.internal.xni.NamespaceContext;
86
87 /**
88  * @author Sandy Gao, IBM
89  * @author Neeraj Bajaj, Sun Microsystems, inc.
90  *
91  * @version $Id: XSSimpleTypeDecl.java,v 1.48 2004/01/19 15:30:20 sandygao Exp $
92  */

93 public class XSSimpleTypeDecl implements XSSimpleType {
94
95     static final short DV_STRING = PRIMITIVE_STRING;
96     static final short DV_BOOLEAN = PRIMITIVE_BOOLEAN;
97     static final short DV_DECIMAL = PRIMITIVE_DECIMAL;
98     static final short DV_FLOAT = PRIMITIVE_FLOAT;
99     static final short DV_DOUBLE = PRIMITIVE_DOUBLE;
100     static final short DV_DURATION = PRIMITIVE_DURATION;
101     static final short DV_DATETIME = PRIMITIVE_DATETIME;
102     static final short DV_TIME = PRIMITIVE_TIME;
103     static final short DV_DATE = PRIMITIVE_DATE;
104     static final short DV_GYEARMONTH = PRIMITIVE_GYEARMONTH;
105     static final short DV_GYEAR = PRIMITIVE_GYEAR;
106     static final short DV_GMONTHDAY = PRIMITIVE_GMONTHDAY;
107     static final short DV_GDAY = PRIMITIVE_GDAY;
108     static final short DV_GMONTH = PRIMITIVE_GMONTH;
109     static final short DV_HEXBINARY = PRIMITIVE_HEXBINARY;
110     static final short DV_BASE64BINARY = PRIMITIVE_BASE64BINARY;
111     static final short DV_ANYURI = PRIMITIVE_ANYURI;
112     static final short DV_QNAME = PRIMITIVE_QNAME;
113     static final short DV_NOTATION = PRIMITIVE_NOTATION;
114
115     static final short DV_ANYSIMPLETYPE = 0;
116     static final short DV_ID = DV_NOTATION + 1;
117     static final short DV_IDREF = DV_NOTATION + 2;
118     static final short DV_ENTITY = DV_NOTATION + 3;
119     static final short DV_INTEGER = DV_NOTATION + 4;
120     static final short DV_LIST = DV_NOTATION + 5;
121     static final short DV_UNION = DV_NOTATION + 6;
122
123     static final TypeValidator[] fDVs = {
124         new AnySimpleDV(),
125         new StringDV(),
126         new BooleanDV(),
127         new DecimalDV(),
128         new FloatDV(),
129         new DoubleDV(),
130         new DurationDV(),
131         new DateTimeDV(),
132         new TimeDV(),
133         new DateDV(),
134         new YearMonthDV(),
135         new YearDV(),
136         new MonthDayDV(),
137         new DayDV(),
138         new MonthDV(),
139         new HexBinaryDV(),
140         new Base64BinaryDV(),
141         new AnyURIDV(),
142         new QNameDV(),
143         new QNameDV(), // notation use the same one as qname
144
new IDDV(),
145         new IDREFDV(),
146         new EntityDV(),
147         new IntegerDV(),
148         new ListDV(),
149         new UnionDV()
150     };
151
152     static final short NORMALIZE_NONE = 0;
153     static final short NORMALIZE_TRIM = 1;
154     static final short NORMALIZE_FULL = 2;
155     static final short[] fDVNormalizeType = {
156         NORMALIZE_NONE, //AnySimpleDV(),
157
NORMALIZE_FULL, //StringDV(),
158
NORMALIZE_TRIM, //BooleanDV(),
159
NORMALIZE_TRIM, //DecimalDV(),
160
NORMALIZE_TRIM, //FloatDV(),
161
NORMALIZE_TRIM, //DoubleDV(),
162
NORMALIZE_TRIM, //DurationDV(),
163
NORMALIZE_TRIM, //DateTimeDV(),
164
NORMALIZE_TRIM, //TimeDV(),
165
NORMALIZE_TRIM, //DateDV(),
166
NORMALIZE_TRIM, //YearMonthDV(),
167
NORMALIZE_TRIM, //YearDV(),
168
NORMALIZE_TRIM, //MonthDayDV(),
169
NORMALIZE_TRIM, //DayDV(),
170
NORMALIZE_TRIM, //MonthDV(),
171
NORMALIZE_TRIM, //HexBinaryDV(),
172
NORMALIZE_NONE, //Base64BinaryDV(), // Base64 know how to deal with spaces
173
NORMALIZE_TRIM, //AnyURIDV(),
174
NORMALIZE_TRIM, //QNameDV(),
175
NORMALIZE_TRIM, //QNameDV(), // notation
176
NORMALIZE_TRIM, //IDDV(),
177
NORMALIZE_TRIM, //IDREFDV(),
178
NORMALIZE_TRIM, //EntityDV(),
179
NORMALIZE_TRIM, //IntegerDV(),
180
NORMALIZE_FULL, //ListDV(),
181
NORMALIZE_NONE, //UnionDV()
182
};
183
184     static final short SPECIAL_PATTERN_NONE = 0;
185     static final short SPECIAL_PATTERN_NMTOKEN = 1;
186     static final short SPECIAL_PATTERN_NAME = 2;
187     static final short SPECIAL_PATTERN_NCNAME = 3;
188
189     static final String JavaDoc[] SPECIAL_PATTERN_STRING = {
190         "NONE", "NMTOKEN", "Name", "NCName"
191     };
192
193     static final String JavaDoc[] WS_FACET_STRING = {
194         "preserve", "replace", "collapse"
195     };
196
197     static final String JavaDoc URI_SCHEMAFORSCHEMA = "http://www.w3.org/2001/XMLSchema";
198     static final String JavaDoc ANY_TYPE = "anyType";
199
200     static final ValidationContext fEmptyContext = new ValidationContext() {
201         public boolean needFacetChecking() {
202             return true;
203         }
204         public boolean needExtraChecking() {
205             return false;
206         }
207         public boolean needToNormalize() {
208             return true;
209         }
210         public boolean useNamespaces () {
211             return true;
212         }
213         public boolean isEntityDeclared (String JavaDoc name) {
214             return false;
215         }
216         public boolean isEntityUnparsed (String JavaDoc name) {
217             return false;
218         }
219         public boolean isIdDeclared (String JavaDoc name) {
220             return false;
221         }
222         public void addId(String JavaDoc name) {
223         }
224         public void addIdRef(String JavaDoc name) {
225         }
226         public String JavaDoc getSymbol (String JavaDoc symbol) {
227             return null;
228         }
229         public String JavaDoc getURI(String JavaDoc prefix) {
230             return null;
231         }
232     };
233
234     // this will be true if this is a static XSSimpleTypeDecl
235
// and hence must remain immutable (i.e., applyFacets
236
// may not be permitted to have any effect).
237
private boolean fIsImmutable = false;
238
239     private XSSimpleTypeDecl fItemType;
240     private XSSimpleTypeDecl[] fMemberTypes;
241     // The most specific built-in type kind.
242
private short fBuiltInKind;
243
244     private String JavaDoc fTypeName;
245     private String JavaDoc fTargetNamespace;
246     private short fFinalSet = 0;
247     private XSSimpleTypeDecl fBase;
248     private short fVariety = -1;
249     private short fValidationDV = -1;
250
251     private short fFacetsDefined = 0;
252     private short fFixedFacet = 0;
253
254     //for constraining facets
255
private short fWhiteSpace = 0;
256     private int fLength = -1;
257     private int fMinLength = -1;
258     private int fMaxLength = -1;
259     private int fTotalDigits = -1;
260     private int fFractionDigits = -1;
261     private Vector JavaDoc fPattern;
262     private Vector JavaDoc fPatternStr;
263     private Vector JavaDoc fEnumeration;
264     private StringList fLexicalPattern;
265     private StringList fLexicalEnumeration;
266     private Object JavaDoc fMaxInclusive;
267     private Object JavaDoc fMaxExclusive;
268     private Object JavaDoc fMinExclusive;
269     private Object JavaDoc fMinInclusive;
270     
271     // annotations for constraining facets
272
public XSAnnotation lengthAnnotation;
273     public XSAnnotation minLengthAnnotation;
274     public XSAnnotation maxLengthAnnotation;
275     public XSAnnotation whiteSpaceAnnotation;
276     public XSAnnotation totalDigitsAnnotation;
277     public XSAnnotation fractionDigitsAnnotation;
278     public XSObjectListImpl patternAnnotations;
279     public XSObjectList enumerationAnnotations;
280     public XSAnnotation maxInclusiveAnnotation;
281     public XSAnnotation maxExclusiveAnnotation;
282     public XSAnnotation minInclusiveAnnotation;
283     public XSAnnotation minExclusiveAnnotation;
284     
285     // facets as objects
286
private XSObjectListImpl fFacets;
287     
288     // enumeration and pattern facets
289
private XSObjectListImpl fMultiValueFacets;
290
291     // simpleType annotations
292
private XSObjectList fAnnotations = null;
293
294     private short fPatternType = SPECIAL_PATTERN_NONE;
295
296     // for fundamental facets
297
private short fOrdered;
298     private boolean fFinite;
299     private boolean fBounded;
300     private boolean fNumeric;
301     
302     /**
303      * Unique ID for each simple type.
304      * This information is used to assign unique names for simple types.
305      */

306     private final int fUniqueId = getNextId();
307     
308     private static int fIdCounter=0;
309     private static synchronized int getNextId() { return fIdCounter++; }
310     
311     
312     // default constructor
313
public XSSimpleTypeDecl(){}
314
315     //Create a new built-in primitive types (and id/idref/entity/integer)
316
protected XSSimpleTypeDecl(XSSimpleTypeDecl base, String JavaDoc name, short validateDV,
317                                short ordered, boolean bounded, boolean finite,
318                                boolean numeric, boolean isImmutable, short builtInKind) {
319         fIsImmutable = isImmutable;
320         fBase = base;
321         fTypeName = name;
322         fTargetNamespace = URI_SCHEMAFORSCHEMA;
323         // To simplify the code for anySimpleType, we treat it as an atomic type
324
fVariety = VARIETY_ATOMIC;
325         fValidationDV = validateDV;
326         fFacetsDefined = FACET_WHITESPACE;
327         if (validateDV == DV_STRING) {
328             fWhiteSpace = WS_PRESERVE;
329         } else {
330             fWhiteSpace = WS_COLLAPSE;
331             fFixedFacet = FACET_WHITESPACE;
332         }
333         this.fOrdered = ordered;
334         this.fBounded = bounded;
335         this.fFinite = finite;
336         this.fNumeric = numeric;
337         fAnnotations = null;
338         
339         // Specify the build in kind for this primitive type
340
fBuiltInKind = builtInKind;
341     }
342
343     //Create a new simple type for restriction for built-in types
344
protected XSSimpleTypeDecl(XSSimpleTypeDecl base, String JavaDoc name, String JavaDoc uri, short finalSet, boolean isImmutable,
345                 XSObjectList annotations, short builtInKind) {
346         this(base, name, uri, finalSet, isImmutable, annotations);
347         // Specify the build in kind for this built-in type
348
fBuiltInKind = builtInKind;
349     }
350     
351     //Create a new simple type for restriction.
352
protected XSSimpleTypeDecl(XSSimpleTypeDecl base, String JavaDoc name, String JavaDoc uri, short finalSet, boolean isImmutable,
353                 XSObjectList annotations) {
354         fBase = base;
355         fTypeName = name;
356         fTargetNamespace = uri;
357         fFinalSet = finalSet;
358         fAnnotations = annotations;
359
360         fVariety = fBase.fVariety;
361         fValidationDV = fBase.fValidationDV;
362         switch (fVariety) {
363         case VARIETY_ATOMIC:
364             break;
365         case VARIETY_LIST:
366             fItemType = fBase.fItemType;
367             break;
368         case VARIETY_UNION:
369             fMemberTypes = fBase.fMemberTypes;
370             break;
371         }
372
373         // always inherit facets from the base.
374
// in case a type is created, but applyFacets is not called
375
fLength = fBase.fLength;
376         fMinLength = fBase.fMinLength;
377         fMaxLength = fBase.fMaxLength;
378         fPattern = fBase.fPattern;
379         fPatternStr = fBase.fPatternStr;
380         fEnumeration = fBase.fEnumeration;
381         fWhiteSpace = fBase.fWhiteSpace;
382         fMaxExclusive = fBase.fMaxExclusive;
383         fMaxInclusive = fBase.fMaxInclusive;
384         fMinExclusive = fBase.fMinExclusive;
385         fMinInclusive = fBase.fMinInclusive;
386         fTotalDigits = fBase.fTotalDigits;
387         fFractionDigits = fBase.fFractionDigits;
388         fPatternType = fBase.fPatternType;
389         fFixedFacet = fBase.fFixedFacet;
390         fFacetsDefined = fBase.fFacetsDefined;
391
392         //we also set fundamental facets information in case applyFacets is not called.
393
caclFundamentalFacets();
394         fIsImmutable = isImmutable;
395
396         // Inherit from the base type
397
fBuiltInKind = base.fBuiltInKind;
398     }
399
400     //Create a new simple type for list.
401
protected XSSimpleTypeDecl(String JavaDoc name, String JavaDoc uri, short finalSet, XSSimpleTypeDecl itemType, boolean isImmutable,
402                 XSObjectList annotations) {
403         fBase = fAnySimpleType;
404         fTypeName = name;
405         fTargetNamespace = uri;
406         fFinalSet = finalSet;
407         fAnnotations = annotations;
408
409         fVariety = VARIETY_LIST;
410         fItemType = (XSSimpleTypeDecl)itemType;
411         fValidationDV = DV_LIST;
412         fFacetsDefined = FACET_WHITESPACE;
413         fFixedFacet = FACET_WHITESPACE;
414         fWhiteSpace = WS_COLLAPSE;
415
416         //setting fundamental facets
417
caclFundamentalFacets();
418         fIsImmutable = isImmutable;
419
420         // Values of this type are lists
421
fBuiltInKind = XSConstants.LIST_DT;
422     }
423
424     //Create a new simple type for union.
425
protected XSSimpleTypeDecl(String JavaDoc name, String JavaDoc uri, short finalSet, XSSimpleTypeDecl[] memberTypes,
426                 XSObjectList annotations) {
427         fBase = fAnySimpleType;
428         fTypeName = name;
429         fTargetNamespace = uri;
430         fFinalSet = finalSet;
431         fAnnotations = annotations;
432
433         fVariety = VARIETY_UNION;
434         fMemberTypes = memberTypes;
435         fValidationDV = DV_UNION;
436         // even for union, we set whitespace to something
437
// this will never be used, but we can use fFacetsDefined to check
438
// whether applyFacets() is allwwed: it's not allowed
439
// if fFacetsDefined != 0
440
fFacetsDefined = FACET_WHITESPACE;
441         fWhiteSpace = WS_COLLAPSE;
442
443         //setting fundamental facets
444
caclFundamentalFacets();
445         // none of the schema-defined types are unions, so just set
446
// fIsImmutable to false.
447
fIsImmutable = false;
448
449         // No value can be of this type, so it's unavailable.
450
fBuiltInKind = XSConstants.UNAVAILABLE_DT;
451     }
452
453     //set values for restriction.
454
protected XSSimpleTypeDecl setRestrictionValues(XSSimpleTypeDecl base, String JavaDoc name, String JavaDoc uri, short finalSet,
455                 XSObjectList annotations) {
456         //decline to do anything if the object is immutable.
457
if(fIsImmutable) return null;
458         fBase = base;
459         fTypeName = name;
460         fTargetNamespace = uri;
461         fFinalSet = finalSet;
462         fAnnotations = annotations;
463
464         fVariety = fBase.fVariety;
465         fValidationDV = fBase.fValidationDV;
466         switch (fVariety) {
467         case VARIETY_ATOMIC:
468             break;
469         case VARIETY_LIST:
470             fItemType = fBase.fItemType;
471             break;
472         case VARIETY_UNION:
473             fMemberTypes = fBase.fMemberTypes;
474             break;
475         }
476
477         // always inherit facets from the base.
478
// in case a type is created, but applyFacets is not called
479
fLength = fBase.fLength;
480         fMinLength = fBase.fMinLength;
481         fMaxLength = fBase.fMaxLength;
482         fPattern = fBase.fPattern;
483         fPatternStr = fBase.fPatternStr;
484         fEnumeration = fBase.fEnumeration;
485         fWhiteSpace = fBase.fWhiteSpace;
486         fMaxExclusive = fBase.fMaxExclusive;
487         fMaxInclusive = fBase.fMaxInclusive;
488         fMinExclusive = fBase.fMinExclusive;
489         fMinInclusive = fBase.fMinInclusive;
490         fTotalDigits = fBase.fTotalDigits;
491         fFractionDigits = fBase.fFractionDigits;
492         fPatternType = fBase.fPatternType;
493         fFixedFacet = fBase.fFixedFacet;
494         fFacetsDefined = fBase.fFacetsDefined;
495
496         //we also set fundamental facets information in case applyFacets is not called.
497
caclFundamentalFacets();
498         return this;
499     }
500
501     //set values for list.
502
protected XSSimpleTypeDecl setListValues(String JavaDoc name, String JavaDoc uri, short finalSet, XSSimpleTypeDecl itemType,
503                 XSObjectList annotations) {
504         //decline to do anything if the object is immutable.
505
if(fIsImmutable) return null;
506         fBase = fAnySimpleType;
507         fTypeName = name;
508         fTargetNamespace = uri;
509         fFinalSet = finalSet;
510         fAnnotations = annotations;
511
512         fVariety = VARIETY_LIST;
513         fItemType = (XSSimpleTypeDecl)itemType;
514         fValidationDV = DV_LIST;
515         fFacetsDefined = FACET_WHITESPACE;
516         fFixedFacet = FACET_WHITESPACE;
517         fWhiteSpace = WS_COLLAPSE;
518
519         //setting fundamental facets
520
caclFundamentalFacets();
521         return this;
522     }
523
524     //set values for union.
525
protected XSSimpleTypeDecl setUnionValues(String JavaDoc name, String JavaDoc uri, short finalSet, XSSimpleTypeDecl[] memberTypes,
526                 XSObjectList annotations) {
527         //decline to do anything if the object is immutable.
528
if(fIsImmutable) return null;
529         fBase = fAnySimpleType;
530         fTypeName = name;
531         fTargetNamespace = uri;
532         fFinalSet = finalSet;
533         fAnnotations = annotations;
534
535         fVariety = VARIETY_UNION;
536         fMemberTypes = memberTypes;
537         fValidationDV = DV_UNION;
538         // even for union, we set whitespace to something
539
// this will never be used, but we can use fFacetsDefined to check
540
// whether applyFacets() is allwwed: it's not allowed
541
// if fFacetsDefined != 0
542
fFacetsDefined = FACET_WHITESPACE;
543         fWhiteSpace = WS_COLLAPSE;
544
545         //setting fundamental facets
546
caclFundamentalFacets();
547         return this;
548     }
549
550     public short getType () {
551         return XSConstants.TYPE_DEFINITION;
552     }
553
554     public short getTypeCategory () {
555         return SIMPLE_TYPE;
556     }
557
558     public String JavaDoc getName() {
559         return fTypeName;
560     }
561
562     public String JavaDoc getNamespace() {
563         return fTargetNamespace;
564     }
565
566     public short getFinal(){
567         return fFinalSet;
568     }
569
570     public boolean isFinal(short derivation) {
571         return (fFinalSet & derivation) != 0;
572     }
573
574     public XSTypeDefinition getBaseType(){
575         return fBase;
576     }
577
578     public boolean getAnonymous() {
579         return fTypeName == null;
580     }
581
582     public short getVariety(){
583         // for anySimpleType, return absent variaty
584
return fValidationDV == DV_ANYSIMPLETYPE ? VARIETY_ABSENT : fVariety;
585     }
586
587     public boolean isIDType(){
588         switch (fVariety) {
589         case VARIETY_ATOMIC:
590             return fValidationDV == DV_ID;
591         case VARIETY_LIST:
592             return fItemType.isIDType();
593         case VARIETY_UNION:
594             for (int i = 0; i < fMemberTypes.length; i++) {
595                 if (fMemberTypes[i].isIDType())
596                     return true;
597             }
598         }
599         return false;
600     }
601
602     public short getWhitespace() throws DatatypeException{
603         if (fVariety == VARIETY_UNION) {
604             throw new DatatypeException("dt-whitespace", new Object JavaDoc[]{fTypeName});
605         }
606         return fWhiteSpace;
607     }
608
609     public short getPrimitiveKind() {
610         if (fVariety == VARIETY_ATOMIC && fValidationDV != DV_ANYSIMPLETYPE) {
611             if (fValidationDV == DV_ID || fValidationDV == DV_IDREF || fValidationDV == DV_ENTITY)
612                 return DV_STRING;
613             else if (fValidationDV == DV_INTEGER)
614                 return DV_DECIMAL;
615             else
616                 return fValidationDV;
617         }
618         else {
619             // REVISIT: error situation. runtime exception?
620
return (short)0;
621         }
622     }
623
624     public short getBuiltInKind() {
625         return this.fBuiltInKind;
626     }
627
628     public XSSimpleTypeDefinition getPrimitiveType() {
629         if (fVariety == VARIETY_ATOMIC && fValidationDV != DV_ANYSIMPLETYPE) {
630             XSSimpleTypeDecl pri = this;
631             // recursively get base, until we reach anySimpleType
632
while (pri.fBase != fAnySimpleType)
633                 pri = pri.fBase;
634             return pri;
635         }
636         else {
637             // REVISIT: error situation. runtime exception?
638
return null;
639         }
640     }
641
642     public XSSimpleTypeDefinition getItemType() {
643         if (fVariety == VARIETY_LIST) {
644             return fItemType;
645         }
646         else {
647             // REVISIT: error situation. runtime exception?
648
return null;
649         }
650     }
651
652     public XSObjectList getMemberTypes() {
653         if (fVariety == VARIETY_UNION) {
654             return new XSObjectListImpl(fMemberTypes, fMemberTypes.length);
655         }
656         else {
657             // REVISIT: error situation. runtime exception?
658
return null;
659         }
660     }
661
662     /**
663      * If <restriction> is chosen
664      */

665     public void applyFacets(XSFacets facets, short presentFacet, short fixedFacet, ValidationContext context)
666         throws InvalidDatatypeFacetException {
667         applyFacets(facets, presentFacet, fixedFacet, SPECIAL_PATTERN_NONE, context);
668     }
669
670     /**
671      * built-in derived types by restriction
672      */

673     void applyFacets1(XSFacets facets, short presentFacet, short fixedFacet) {
674
675         try {
676             applyFacets(facets, presentFacet, fixedFacet, SPECIAL_PATTERN_NONE, fDummyContext);
677         } catch (InvalidDatatypeFacetException e) {
678             // should never gets here, internel error
679
throw new RuntimeException JavaDoc("internal error");
680         }
681         // we've now applied facets; so lock this object:
682
fIsImmutable = true;
683     }
684
685     /**
686      * built-in derived types by restriction
687      */

688     void applyFacets1(XSFacets facets, short presentFacet, short fixedFacet, short patternType) {
689
690         try {
691             applyFacets(facets, presentFacet, fixedFacet, patternType, fDummyContext);
692         } catch (InvalidDatatypeFacetException e) {
693             // should never gets here, internel error
694
throw new RuntimeException JavaDoc("internal error");
695         }
696         // we've now applied facets; so lock this object:
697
fIsImmutable = true;
698     }
699
700     /**
701      * If <restriction> is chosen, or built-in derived types by restriction
702      */

703     void applyFacets(XSFacets facets, short presentFacet, short fixedFacet, short patternType, ValidationContext context)
704         throws InvalidDatatypeFacetException {
705
706         // if the object is immutable, should not apply facets...
707
if(fIsImmutable) return;
708         ValidatedInfo tempInfo = new ValidatedInfo();
709
710         // clear facets. because we always inherit facets in the constructor
711
// REVISIT: in fact, we don't need to clear them.
712
// we can convert 5 string values (4 bounds + 1 enum) to actual values,
713
// store them somewhere, then do facet checking at once, instead of
714
// going through the following steps. (lots of checking are redundant:
715
// for example, ((presentFacet & FACET_XXX) != 0))
716

717         fFacetsDefined = 0;
718         fFixedFacet = 0;
719
720         int result = 0 ;
721
722         // step 1: parse present facets
723
short allowedFacet = fDVs[fValidationDV].getAllowedFacets();
724
725         // length
726
if ((presentFacet & FACET_LENGTH) != 0) {
727             if ((allowedFacet & FACET_LENGTH) == 0) {
728                 reportError("cos-applicable-facets", new Object JavaDoc[]{"length", fTypeName});
729             } else {
730                 fLength = facets.length;
731                 lengthAnnotation = facets.lengthAnnotation;
732                 fFacetsDefined |= FACET_LENGTH;
733                 if ((fixedFacet & FACET_LENGTH) != 0)
734                     fFixedFacet |= FACET_LENGTH;
735             }
736         }
737         // minLength
738
if ((presentFacet & FACET_MINLENGTH) != 0) {
739             if ((allowedFacet & FACET_MINLENGTH) == 0) {
740                 reportError("cos-applicable-facets", new Object JavaDoc[]{"minLength", fTypeName});
741             } else {
742                 fMinLength = facets.minLength;
743                 minLengthAnnotation = facets.minLengthAnnotation;
744                 fFacetsDefined |= FACET_MINLENGTH;
745                 if ((fixedFacet & FACET_MINLENGTH) != 0)
746                     fFixedFacet |= FACET_MINLENGTH;
747             }
748         }
749         // maxLength
750
if ((presentFacet & FACET_MAXLENGTH) != 0) {
751             if ((allowedFacet & FACET_MAXLENGTH) == 0) {
752                 reportError("cos-applicable-facets", new Object JavaDoc[]{"maxLength", fTypeName});
753             } else {
754                 fMaxLength = facets.maxLength;
755                 maxLengthAnnotation = facets.maxLengthAnnotation;
756                 fFacetsDefined |= FACET_MAXLENGTH;
757                 if ((fixedFacet & FACET_MAXLENGTH) != 0)
758                     fFixedFacet |= FACET_MAXLENGTH;
759             }
760         }
761         // pattern
762
if ((presentFacet & FACET_PATTERN) != 0) {
763             if ((allowedFacet & FACET_PATTERN) == 0) {
764                 reportError("cos-applicable-facets", new Object JavaDoc[]{"pattern", fTypeName});
765             } else {
766                 patternAnnotations = facets.patternAnnotations;
767                 RegularExpression regex = null;
768                 try {
769                     regex = new RegularExpression(facets.pattern, "X");
770                 } catch (Exception JavaDoc e) {
771                     reportError("InvalidRegex", new Object JavaDoc[]{facets.pattern, e.getLocalizedMessage()});
772                 }
773                 if (regex != null) {
774                     fPattern = new Vector JavaDoc();
775                     fPattern.addElement(regex);
776                     fPatternStr = new Vector JavaDoc();
777                     fPatternStr.addElement(facets.pattern);
778                     fFacetsDefined |= FACET_PATTERN;
779                     if ((fixedFacet & FACET_PATTERN) != 0)
780                         fFixedFacet |= FACET_PATTERN;
781                 }
782             }
783         }
784
785         // enumeration
786
if ((presentFacet & FACET_ENUMERATION) != 0) {
787             if ((allowedFacet & FACET_ENUMERATION) == 0) {
788                 reportError("cos-applicable-facets", new Object JavaDoc[]{"enumeration", fTypeName});
789             } else {
790                 fEnumeration = new Vector JavaDoc();
791                 Vector JavaDoc enumVals = facets.enumeration;
792                 Vector JavaDoc enumNSDecls = facets.enumNSDecls;
793                 ValidationContextImpl ctx = new ValidationContextImpl(context);
794                 enumerationAnnotations = facets.enumAnnotations;
795                 for (int i = 0; i < enumVals.size(); i++) {
796                     if (enumNSDecls != null)
797                         ctx.setNSContext((NamespaceContext)enumNSDecls.elementAt(i));
798                     try {
799                         // check 4.3.5.c0 must: enumeration values from the value space of base
800
fEnumeration.addElement(this.fBase.validate((String JavaDoc)enumVals.elementAt(i), ctx, tempInfo));
801                     } catch (InvalidDatatypeValueException ide) {
802                         reportError("enumeration-valid-restriction", new Object JavaDoc[]{enumVals.elementAt(i), this.getBaseType().getName()});
803                     }
804                 }
805                 fFacetsDefined |= FACET_ENUMERATION;
806                 if ((fixedFacet & FACET_ENUMERATION) != 0)
807                     fFixedFacet |= FACET_ENUMERATION;
808             }
809         }
810         // whiteSpace
811
if ((presentFacet & FACET_WHITESPACE) != 0) {
812             if ((allowedFacet & FACET_WHITESPACE) == 0) {
813                 reportError("cos-applicable-facets", new Object JavaDoc[]{"whiteSpace", fTypeName});
814             } else {
815                 fWhiteSpace = facets.whiteSpace;
816                 whiteSpaceAnnotation = facets.whiteSpaceAnnotation;
817                 fFacetsDefined |= FACET_WHITESPACE;
818                 if ((fixedFacet & FACET_WHITESPACE) != 0)
819                     fFixedFacet |= FACET_WHITESPACE;
820             }
821         }
822
823         boolean needCheckBase = true;
824         // maxInclusive
825
if ((presentFacet & FACET_MAXINCLUSIVE) != 0) {
826             if ((allowedFacet & FACET_MAXINCLUSIVE) == 0) {
827                 reportError("cos-applicable-facets", new Object JavaDoc[]{"maxInclusive", fTypeName});
828             } else {
829                 maxInclusiveAnnotation = facets.maxInclusiveAnnotation;
830                 try {
831                     fMaxInclusive = getActualValue(facets.maxInclusive, context, tempInfo, true);
832                     fFacetsDefined |= FACET_MAXINCLUSIVE;
833                     if ((fixedFacet & FACET_MAXINCLUSIVE) != 0)
834                         fFixedFacet |= FACET_MAXINCLUSIVE;
835                 } catch (InvalidDatatypeValueException ide) {
836                     reportError(ide.getKey(), ide.getArgs());
837                     reportError("FacetValueFromBase", new Object JavaDoc[]{fTypeName, facets.maxInclusive,
838                                                                    "maxInclusive", fBase.getName()});
839                 }
840
841                 // maxInclusive from base
842
if (((fBase.fFacetsDefined & FACET_MAXINCLUSIVE) != 0)) {
843                     result = fDVs[fValidationDV].compare(fMaxInclusive, fBase.fMaxInclusive);
844                     if ((fBase.fFixedFacet & FACET_MAXINCLUSIVE) != 0 && result != 0) {
845                         reportError( "FixedFacetValue", new Object JavaDoc[]{"maxInclusive", fMaxInclusive, fBase.fMaxInclusive, fTypeName});
846                     }
847                     if (result == 0) {
848                         needCheckBase = false;
849                     }
850                 }
851                 if (needCheckBase) {
852                     try {
853                         fBase.validate(context, tempInfo);
854                     } catch (InvalidDatatypeValueException ide) {
855                         reportError(ide.getKey(), ide.getArgs());
856                         reportError("FacetValueFromBase", new Object JavaDoc[]{fTypeName, facets.maxInclusive,
857                                                                        "maxInclusive", fBase.getName()});
858                     }
859                 }
860             }
861         }
862
863         // maxExclusive
864
needCheckBase = true;
865         if ((presentFacet & FACET_MAXEXCLUSIVE) != 0) {
866             if ((allowedFacet & FACET_MAXEXCLUSIVE) == 0) {
867                 reportError("cos-applicable-facets", new Object JavaDoc[]{"maxExclusive", fTypeName});
868             } else {
869                 maxExclusiveAnnotation = facets.maxExclusiveAnnotation;
870                 try {
871                     fMaxExclusive = getActualValue(facets.maxExclusive, context, tempInfo, true);
872                     fFacetsDefined |= FACET_MAXEXCLUSIVE;
873                     if ((fixedFacet & FACET_MAXEXCLUSIVE) != 0)
874                         fFixedFacet |= FACET_MAXEXCLUSIVE;
875                 } catch (InvalidDatatypeValueException ide) {
876                     reportError(ide.getKey(), ide.getArgs());
877                     reportError("FacetValueFromBase", new Object JavaDoc[]{fTypeName, facets.maxExclusive,
878                                                                    "maxExclusive", fBase.getName()});
879                 }
880
881                 // maxExclusive from base
882
if (((fBase.fFacetsDefined & FACET_MAXEXCLUSIVE) != 0)) {
883                     result = fDVs[fValidationDV].compare(fMaxExclusive, fBase.fMaxExclusive);
884                     if ((fBase.fFixedFacet & FACET_MAXEXCLUSIVE) != 0 && result != 0) {
885                         reportError( "FixedFacetValue", new Object JavaDoc[]{"maxExclusive", facets.maxExclusive, fBase.fMaxExclusive, fTypeName});
886                     }
887                     if (result == 0) {
888                         needCheckBase = false;
889                     }
890                 }
891                 if (needCheckBase) {
892                     try {
893                         fBase.validate(context, tempInfo);
894                     } catch (InvalidDatatypeValueException ide) {
895                         reportError(ide.getKey(), ide.getArgs());
896                         reportError("FacetValueFromBase", new Object JavaDoc[]{fTypeName, facets.maxExclusive,
897                                                                        "maxExclusive", fBase.getName()});
898                     }
899                 }
900             }
901         }
902         // minExclusive
903
needCheckBase = true;
904         if ((presentFacet & FACET_MINEXCLUSIVE) != 0) {
905             if ((allowedFacet & FACET_MINEXCLUSIVE) == 0) {
906                 reportError("cos-applicable-facets", new Object JavaDoc[]{"minExclusive", fTypeName});
907             } else {
908                 minExclusiveAnnotation = facets.minExclusiveAnnotation;
909                 try {
910                     fMinExclusive = getActualValue(facets.minExclusive, context, tempInfo, true);
911                     fFacetsDefined |= FACET_MINEXCLUSIVE;
912                     if ((fixedFacet & FACET_MINEXCLUSIVE) != 0)
913                         fFixedFacet |= FACET_MINEXCLUSIVE;
914                 } catch (InvalidDatatypeValueException ide) {
915                     reportError(ide.getKey(), ide.getArgs());
916                     reportError("FacetValueFromBase", new Object JavaDoc[]{fTypeName, facets.minExclusive,
917                                                                    "minExclusive", fBase.getName()});
918                 }
919
920                 // minExclusive from base
921
if (((fBase.fFacetsDefined & FACET_MINEXCLUSIVE) != 0)) {
922                     result = fDVs[fValidationDV].compare(fMinExclusive, fBase.fMinExclusive);
923                     if ((fBase.fFixedFacet & FACET_MINEXCLUSIVE) != 0 && result != 0) {
924                         reportError( "FixedFacetValue", new Object JavaDoc[]{"minExclusive", facets.minExclusive, fBase.fMinExclusive, fTypeName});
925                     }
926                     if (result == 0) {
927                         needCheckBase = false;
928                     }
929                 }
930                 if (needCheckBase) {
931                     try {
932                         fBase.validate(context, tempInfo);
933                     } catch (InvalidDatatypeValueException ide) {
934                         reportError(ide.getKey(), ide.getArgs());
935                         reportError("FacetValueFromBase", new Object JavaDoc[]{fTypeName, facets.minExclusive,
936                                                                        "minExclusive", fBase.getName()});
937                     }
938                 }
939             }
940         }
941         // minInclusive
942
needCheckBase = true;
943         if ((presentFacet & FACET_MININCLUSIVE) != 0) {
944             if ((allowedFacet & FACET_MININCLUSIVE) == 0) {
945                 reportError("cos-applicable-facets", new Object JavaDoc[]{"minInclusive", fTypeName});
946             } else {
947                 minInclusiveAnnotation = facets.minInclusiveAnnotation;
948                 try {
949                     fMinInclusive = getActualValue(facets.minInclusive, context, tempInfo, true);
950                     fFacetsDefined |= FACET_MININCLUSIVE;
951                     if ((fixedFacet & FACET_MININCLUSIVE) != 0)
952                         fFixedFacet |= FACET_MININCLUSIVE;
953                 } catch (InvalidDatatypeValueException ide) {
954                     reportError(ide.getKey(), ide.getArgs());
955                     reportError("FacetValueFromBase", new Object JavaDoc[]{fTypeName, facets.minInclusive,
956                                                                    "minInclusive", fBase.getName()});
957                 }
958
959                 // minInclusive from base
960
if (((fBase.fFacetsDefined & FACET_MININCLUSIVE) != 0)) {
961                     result = fDVs[fValidationDV].compare(fMinInclusive, fBase.fMinInclusive);
962                     if ((fBase.fFixedFacet & FACET_MININCLUSIVE) != 0 && result != 0) {
963                         reportError( "FixedFacetValue", new Object JavaDoc[]{"minInclusive", facets.minInclusive, fBase.fMinInclusive, fTypeName});
964                     }
965                     if (result == 0) {
966                         needCheckBase = false;
967                     }
968                 }
969                 if (needCheckBase) {
970                     try {
971                         fBase.validate(context, tempInfo);
972                     } catch (InvalidDatatypeValueException ide) {
973                         reportError(ide.getKey(), ide.getArgs());
974                         reportError("FacetValueFromBase", new Object JavaDoc[]{fTypeName, facets.minInclusive,
975                                                                        "minInclusive", fBase.getName()});
976                     }
977                 }
978             }
979         }
980
981         // totalDigits
982
if ((presentFacet & FACET_TOTALDIGITS) != 0) {
983             if ((allowedFacet & FACET_TOTALDIGITS) == 0) {
984                 reportError("cos-applicable-facets", new Object JavaDoc[]{"totalDigits", fTypeName});
985             } else {
986                 totalDigitsAnnotation = facets.totalDigitsAnnotation;
987                 fTotalDigits = facets.totalDigits;
988                 fFacetsDefined |= FACET_TOTALDIGITS;
989                 if ((fixedFacet & FACET_TOTALDIGITS) != 0)
990                     fFixedFacet |= FACET_TOTALDIGITS;
991             }
992         }
993         // fractionDigits
994
if ((presentFacet & FACET_FRACTIONDIGITS) != 0) {
995             if ((allowedFacet & FACET_FRACTIONDIGITS) == 0) {
996                 reportError("cos-applicable-facets", new Object JavaDoc[]{"fractionDigits", fTypeName});
997             } else {
998                 fFractionDigits = facets.fractionDigits;
999                 fractionDigitsAnnotation = facets.fractionDigitsAnnotation;
1000                fFacetsDefined |= FACET_FRACTIONDIGITS;
1001                if ((fixedFacet & FACET_FRACTIONDIGITS) != 0)
1002                    fFixedFacet |= FACET_FRACTIONDIGITS;
1003            }
1004        }
1005
1006        // token type: internal use, so do less checking
1007
if (patternType != SPECIAL_PATTERN_NONE) {
1008            fPatternType = patternType;
1009        }
1010
1011        // step 2: check facets against each other: length, bounds
1012
if(fFacetsDefined != 0) {
1013
1014            // check 4.3.1.c1 error: length & (maxLength | minLength)
1015
if((fFacetsDefined & FACET_LENGTH) != 0 ){
1016              if ((fFacetsDefined & FACET_MINLENGTH) != 0) {
1017                if ((fFacetsDefined & FACET_MAXLENGTH) != 0) {
1018                    // length, minLength and maxLength defined
1019
reportError("length-minLength-maxLength.a", new Object JavaDoc[]{fTypeName, Integer.toString(fLength), Integer.toString(fMinLength), Integer.toString(fMaxLength)});
1020                }
1021                else {
1022                    // length and minLength defined
1023
reportError("length-minLength-maxLength.b", new Object JavaDoc[]{fTypeName, Integer.toString(fLength), Integer.toString(fMinLength)});
1024                }
1025              }
1026              else if ((fFacetsDefined & FACET_MAXLENGTH) != 0) {
1027                // length and maxLength defined
1028
reportError("length-minLength-maxLength.c", new Object JavaDoc[]{fTypeName, Integer.toString(fLength), Integer.toString(fMaxLength)});
1029              }
1030            }
1031
1032            // check 4.3.2.c1 must: minLength <= maxLength
1033
if(((fFacetsDefined & FACET_MINLENGTH ) != 0 ) && ((fFacetsDefined & FACET_MAXLENGTH) != 0))
1034            {
1035              if(fMinLength > fMaxLength)
1036                reportError("minLength-less-than-equal-to-maxLength", new Object JavaDoc[]{Integer.toString(fMinLength), Integer.toString(fMaxLength), fTypeName});
1037            }
1038
1039            // check 4.3.8.c1 error: maxInclusive + maxExclusive
1040
if (((fFacetsDefined & FACET_MAXEXCLUSIVE) != 0) && ((fFacetsDefined & FACET_MAXINCLUSIVE) != 0)) {
1041                reportError( "maxInclusive-maxExclusive", new Object JavaDoc[]{fMaxInclusive, fMaxExclusive, fTypeName});
1042            }
1043
1044            // check 4.3.9.c1 error: minInclusive + minExclusive
1045
if (((fFacetsDefined & FACET_MINEXCLUSIVE) != 0) && ((fFacetsDefined & FACET_MININCLUSIVE) != 0)) {
1046                reportError("minInclusive-minExclusive", new Object JavaDoc[]{fMinInclusive, fMinExclusive, fTypeName});
1047            }
1048
1049            // check 4.3.7.c1 must: minInclusive <= maxInclusive
1050
if (((fFacetsDefined & FACET_MAXINCLUSIVE) != 0) && ((fFacetsDefined & FACET_MININCLUSIVE) != 0)) {
1051              result = fDVs[fValidationDV].compare(fMinInclusive, fMaxInclusive);
1052              if (result != -1 && result != 0)
1053                reportError("minInclusive-less-than-equal-to-maxInclusive", new Object JavaDoc[]{fMinInclusive, fMaxInclusive, fTypeName});
1054            }
1055
1056            // check 4.3.8.c2 must: minExclusive <= maxExclusive ??? minExclusive < maxExclusive
1057
if (((fFacetsDefined & FACET_MAXEXCLUSIVE) != 0) && ((fFacetsDefined & FACET_MINEXCLUSIVE) != 0)) {
1058              result = fDVs[fValidationDV].compare(fMinExclusive, fMaxExclusive);
1059              if (result != -1 && result != 0)
1060                reportError( "minExclusive-less-than-equal-to-maxExclusive", new Object JavaDoc[]{fMinExclusive, fMaxExclusive, fTypeName});
1061            }
1062
1063            // check 4.3.9.c2 must: minExclusive < maxInclusive
1064
if (((fFacetsDefined & FACET_MAXINCLUSIVE) != 0) && ((fFacetsDefined & FACET_MINEXCLUSIVE) != 0)) {
1065              if (fDVs[fValidationDV].compare(fMinExclusive, fMaxInclusive) != -1)
1066                reportError( "minExclusive-less-than-maxInclusive", new Object JavaDoc[]{fMinExclusive, fMaxInclusive, fTypeName});
1067            }
1068
1069            // check 4.3.10.c1 must: minInclusive < maxExclusive
1070
if (((fFacetsDefined & FACET_MAXEXCLUSIVE) != 0) && ((fFacetsDefined & FACET_MININCLUSIVE) != 0)) {
1071              if (fDVs[fValidationDV].compare(fMinInclusive, fMaxExclusive) != -1)
1072                reportError( "minInclusive-less-than-maxExclusive", new Object JavaDoc[]{fMinInclusive, fMaxExclusive, fTypeName});
1073            }
1074
1075            // check 4.3.12.c1 must: fractionDigits <= totalDigits
1076
if (((fFacetsDefined & FACET_FRACTIONDIGITS) != 0) &&
1077                ((fFacetsDefined & FACET_TOTALDIGITS) != 0)) {
1078                if (fFractionDigits > fTotalDigits)
1079                    reportError( "fractionDigits-totalDigits", new Object JavaDoc[]{Integer.toString(fFractionDigits), Integer.toString(fTotalDigits), fTypeName});
1080            }
1081
1082            // step 3: check facets against base
1083
// check 4.3.1.c1 error: length & (fBase.maxLength | fBase.minLength)
1084
if((fFacetsDefined & FACET_LENGTH) != 0 ){
1085                if ((fBase.fFacetsDefined & FACET_MINLENGTH) != 0 &&
1086                    fLength < fBase.fMinLength) {
1087                    // length, fBase.minLength and fBase.maxLength defined
1088
reportError("length-minLength-maxLength.d", new Object JavaDoc[]{fTypeName, Integer.toString(fLength), Integer.toString(fBase.fMinLength)});
1089                }
1090                if ((fBase.fFacetsDefined & FACET_MAXLENGTH) != 0 &&
1091                    fLength > fBase.fMaxLength) {
1092                    // length and fBase.maxLength defined
1093
reportError("length-minLength-maxLength.e", new Object JavaDoc[]{fTypeName, Integer.toString(fLength), Integer.toString(fBase.fMaxLength)});
1094                }
1095                if ( (fBase.fFacetsDefined & FACET_LENGTH) != 0 ) {
1096                    // check 4.3.1.c2 error: length != fBase.length
1097
if ( fLength != fBase.fLength )
1098                        reportError( "length-valid-restriction", new Object JavaDoc[]{Integer.toString(fLength), Integer.toString(fBase.fLength), fTypeName});
1099                }
1100            }
1101
1102            // check 4.3.1.c1 error: fBase.length & (maxLength | minLength)
1103
else if((fBase.fFacetsDefined & FACET_LENGTH) != 0 ){
1104                if ((fFacetsDefined & FACET_MINLENGTH) != 0 &&
1105                    fBase.fLength < fMinLength) {
1106                    // fBase.length, minLength and maxLength defined
1107
reportError("length-minLength-maxLength.d", new Object JavaDoc[]{fTypeName, Integer.toString(fBase.fLength), Integer.toString(fMinLength)});
1108                }
1109                if ((fFacetsDefined & FACET_MAXLENGTH) != 0 &&
1110                         fBase.fLength > fMaxLength) {
1111                    // fBase.length and maxLength defined
1112
reportError("length-minLength-maxLength.e", new Object JavaDoc[]{this, Integer.toString(fBase.fLength), Integer.toString(fMaxLength)});
1113                }
1114            }
1115
1116            // check 4.3.2.c1 must: minLength <= fBase.maxLength
1117
if ( ((fFacetsDefined & FACET_MINLENGTH ) != 0 ) ) {
1118                if ( (fBase.fFacetsDefined & FACET_MAXLENGTH ) != 0 ) {
1119                    if ( fMinLength > fBase.fMaxLength ) {
1120                        reportError("minLength-less-than-equal-to-maxLength", new Object JavaDoc[]{Integer.toString(fMinLength), Integer.toString(fBase.fMaxLength), fTypeName});
1121                    }
1122                }
1123                else if ( (fBase.fFacetsDefined & FACET_MINLENGTH) != 0 ) {
1124                    if ( (fBase.fFixedFacet & FACET_MINLENGTH) != 0 && fMinLength != fBase.fMinLength ) {
1125                        reportError( "FixedFacetValue", new Object JavaDoc[]{"minLength", Integer.toString(fMinLength), Integer.toString(fBase.fMinLength), fTypeName});
1126                    }
1127
1128                    // check 4.3.2.c2 error: minLength < fBase.minLength
1129
if ( fMinLength < fBase.fMinLength ) {
1130                        reportError( "minLength-valid-restriction", new Object JavaDoc[]{Integer.toString(fMinLength), Integer.toString(fBase.fMinLength), fTypeName});
1131                    }
1132                }
1133            }
1134
1135
1136            // check 4.3.2.c1 must: maxLength < fBase.minLength
1137
if ( ((fFacetsDefined & FACET_MAXLENGTH ) != 0 ) && ((fBase.fFacetsDefined & FACET_MINLENGTH ) != 0 )) {
1138                if ( fMaxLength < fBase.fMinLength) {
1139                    reportError("minLength-less-than-equal-to-maxLength", new Object JavaDoc[]{Integer.toString(fBase.fMinLength), Integer.toString(fMaxLength)});
1140                }
1141            }
1142
1143            // check 4.3.3.c1 error: maxLength > fBase.maxLength
1144
if ( (fFacetsDefined & FACET_MAXLENGTH) != 0 ) {
1145                if ( (fBase.fFacetsDefined & FACET_MAXLENGTH) != 0 ){
1146                    if(( (fBase.fFixedFacet & FACET_MAXLENGTH) != 0 )&& fMaxLength != fBase.fMaxLength ) {
1147                        reportError( "FixedFacetValue", new Object JavaDoc[]{"maxLength", Integer.toString(fMaxLength), Integer.toString(fBase.fMaxLength), fTypeName});
1148                    }
1149                    if ( fMaxLength > fBase.fMaxLength ) {
1150                        reportError( "maxLength-valid-restriction", new Object JavaDoc[]{Integer.toString(fMaxLength), Integer.toString(fBase.fMaxLength), fTypeName});
1151                    }
1152                }
1153            }
1154
1155/* // check 4.3.7.c2 error:
1156            // maxInclusive > fBase.maxInclusive
1157            // maxInclusive >= fBase.maxExclusive
1158            // maxInclusive < fBase.minInclusive
1159            // maxInclusive <= fBase.minExclusive
1160
1161            if (((fFacetsDefined & FACET_MAXINCLUSIVE) != 0)) {
1162                if (((fBase.fFacetsDefined & FACET_MAXINCLUSIVE) != 0)) {
1163                    result = fDVs[fValidationDV].compare(fMaxInclusive, fBase.fMaxInclusive);
1164                    if ((fBase.fFixedFacet & FACET_MAXINCLUSIVE) != 0 && result != 0) {
1165                        reportError( "FixedFacetValue", new Object[]{"maxInclusive", fMaxInclusive, fBase.fMaxInclusive, fTypeName});
1166                    }
1167                    if (result != -1 && result != 0) {
1168                        reportError( "maxInclusive-valid-restriction.1", new Object[]{fMaxInclusive, fBase.fMaxInclusive, fTypeName});
1169                    }
1170                }
1171                if (((fBase.fFacetsDefined & FACET_MAXEXCLUSIVE) != 0) &&
1172                    fDVs[fValidationDV].compare(fMaxInclusive, fBase.fMaxExclusive) != -1){
1173                        reportError( "maxInclusive-valid-restriction.1", new Object[]{fMaxInclusive, fBase.fMaxExclusive, fTypeName});
1174                }
1175
1176                if ((( fBase.fFacetsDefined & FACET_MININCLUSIVE) != 0)) {
1177                    result = fDVs[fValidationDV].compare(fMaxInclusive, fBase.fMinInclusive);
1178                    if (result != 1 && result != 0) {
1179                        reportError( "maxInclusive-valid-restriction.1", new Object[]{fMaxInclusive, fBase.fMinInclusive, fTypeName});
1180                    }
1181                }
1182
1183                if ((( fBase.fFacetsDefined & FACET_MINEXCLUSIVE) != 0) &&
1184                    fDVs[fValidationDV].compare(fMaxInclusive, fBase.fMinExclusive ) != 1)
1185                    reportError( "maxInclusive-valid-restriction.1", new Object[]{fMaxInclusive, fBase.fMinExclusive, fTypeName});
1186            }
1187
1188            // check 4.3.8.c3 error:
1189            // maxExclusive > fBase.maxExclusive
1190            // maxExclusive > fBase.maxInclusive
1191            // maxExclusive <= fBase.minInclusive
1192            // maxExclusive <= fBase.minExclusive
1193            if (((fFacetsDefined & FACET_MAXEXCLUSIVE) != 0)) {
1194                if ((( fBase.fFacetsDefined & FACET_MAXEXCLUSIVE) != 0)) {
1195                    result= fDVs[fValidationDV].compare(fMaxExclusive, fBase.fMaxExclusive);
1196                    if ((fBase.fFixedFacet & FACET_MAXEXCLUSIVE) != 0 && result != 0) {
1197                        reportError( "FixedFacetValue", new Object[]{"maxExclusive", fMaxExclusive, fBase.fMaxExclusive, fTypeName});
1198                    }
1199                    if (result != -1 && result != 0) {
1200                        reportError( "maxExclusive-valid-restriction.1", new Object[]{fMaxExclusive, fBase.fMaxExclusive, fTypeName});
1201                    }
1202                }
1203
1204                if ((( fBase.fFacetsDefined & FACET_MAXINCLUSIVE) != 0)) {
1205                    result= fDVs[fValidationDV].compare(fMaxExclusive, fBase.fMaxInclusive);
1206                    if (result != -1 && result != 0) {
1207                        reportError( "maxExclusive-valid-restriction.2", new Object[]{fMaxExclusive, fBase.fMaxInclusive, fTypeName});
1208                    }
1209                }
1210
1211                if ((( fBase.fFacetsDefined & FACET_MINEXCLUSIVE) != 0) &&
1212                    fDVs[fValidationDV].compare(fMaxExclusive, fBase.fMinExclusive ) != 1)
1213                    reportError( "maxExclusive-valid-restriction.3", new Object[]{fMaxExclusive, fBase.fMinExclusive, fTypeName});
1214
1215                if ((( fBase.fFacetsDefined & FACET_MININCLUSIVE) != 0) &&
1216                    fDVs[fValidationDV].compare(fMaxExclusive, fBase.fMinInclusive) != 1)
1217                    reportError( "maxExclusive-valid-restriction.4", new Object[]{fMaxExclusive, fBase.fMinInclusive, fTypeName});
1218            }
1219
1220            // check 4.3.9.c3 error:
1221            // minExclusive < fBase.minExclusive
1222            // minExclusive > fBase.maxInclusive
1223            // minExclusive < fBase.minInclusive
1224            // minExclusive >= fBase.maxExclusive
1225            if (((fFacetsDefined & FACET_MINEXCLUSIVE) != 0)) {
1226                if ((( fBase.fFacetsDefined & FACET_MINEXCLUSIVE) != 0)) {
1227                    result= fDVs[fValidationDV].compare(fMinExclusive, fBase.fMinExclusive);
1228                    if ((fBase.fFixedFacet & FACET_MINEXCLUSIVE) != 0 && result != 0) {
1229                        reportError( "FixedFacetValue", new Object[]{"minExclusive", fMinExclusive, fBase.fMinExclusive, fTypeName});
1230                    }
1231                    if (result != 1 && result != 0) {
1232                        reportError( "minExclusive-valid-restriction.1", new Object[]{fMinExclusive, fBase.fMinExclusive, fTypeName});
1233                    }
1234                }
1235
1236                if ((( fBase.fFacetsDefined & FACET_MAXINCLUSIVE) != 0)) {
1237                    result=fDVs[fValidationDV].compare(fMinExclusive, fBase.fMaxInclusive);
1238
1239                    if (result != -1 && result != 0) {
1240                        reportError( "minExclusive-valid-restriction.2", new Object[]{fMinExclusive, fBase.fMaxInclusive, fTypeName});
1241                    }
1242                }
1243
1244                if ((( fBase.fFacetsDefined & FACET_MININCLUSIVE) != 0)) {
1245                    result = fDVs[fValidationDV].compare(fMinExclusive, fBase.fMinInclusive);
1246
1247                    if (result != 1 && result != 0) {
1248                        reportError( "minExclusive-valid-restriction.3", new Object[]{fMinExclusive, fBase.fMinInclusive, fTypeName});
1249                    }
1250                }
1251
1252                if ((( fBase.fFacetsDefined & FACET_MAXEXCLUSIVE) != 0) &&
1253                    fDVs[fValidationDV].compare(fMinExclusive, fBase.fMaxExclusive) != -1)
1254                    reportError( "minExclusive-valid-restriction.4", new Object[]{fMinExclusive, fBase.fMaxExclusive, fTypeName});
1255            }
1256
1257            // check 4.3.10.c2 error:
1258            // minInclusive < fBase.minInclusive
1259            // minInclusive > fBase.maxInclusive
1260            // minInclusive <= fBase.minExclusive
1261            // minInclusive >= fBase.maxExclusive
1262            if (((fFacetsDefined & FACET_MININCLUSIVE) != 0)) {
1263                if (((fBase.fFacetsDefined & FACET_MININCLUSIVE) != 0)) {
1264                    result = fDVs[fValidationDV].compare(fMinInclusive, fBase.fMinInclusive);
1265
1266                    if ((fBase.fFixedFacet & FACET_MININCLUSIVE) != 0 && result != 0) {
1267                        reportError( "FixedFacetValue", new Object[]{"minInclusive", fMinInclusive, fBase.fMinInclusive, fTypeName});
1268                    }
1269                    if (result != 1 && result != 0) {
1270                        reportError( "minInclusive-valid-restriction.1", new Object[]{fMinInclusive, fBase.fMinInclusive, fTypeName});
1271                    }
1272                }
1273                if ((( fBase.fFacetsDefined & FACET_MAXINCLUSIVE) != 0)) {
1274                    result=fDVs[fValidationDV].compare(fMinInclusive, fBase.fMaxInclusive);
1275                    if (result != -1 && result != 0) {
1276                        reportError( "minInclusive-valid-restriction.2", new Object[]{fMinInclusive, fBase.fMaxInclusive, fTypeName});
1277                    }
1278                }
1279                if ((( fBase.fFacetsDefined & FACET_MINEXCLUSIVE) != 0) &&
1280                    fDVs[fValidationDV].compare(fMinInclusive, fBase.fMinExclusive ) != 1)
1281                    reportError( "minInclusive-valid-restriction.3", new Object[]{fMinInclusive, fBase.fMinExclusive, fTypeName});
1282                if ((( fBase.fFacetsDefined & FACET_MAXEXCLUSIVE) != 0) &&
1283                    fDVs[fValidationDV].compare(fMinInclusive, fBase.fMaxExclusive) != -1)
1284                    reportError( "minInclusive-valid-restriction.4", new Object[]{fMinInclusive, fBase.fMaxExclusive, fTypeName});
1285            }
1286*/

1287            // check 4.3.11.c1 error: totalDigits > fBase.totalDigits
1288
if (((fFacetsDefined & FACET_TOTALDIGITS) != 0)) {
1289                if ((( fBase.fFacetsDefined & FACET_TOTALDIGITS) != 0)) {
1290                    if ((fBase.fFixedFacet & FACET_TOTALDIGITS) != 0 && fTotalDigits != fBase.fTotalDigits) {
1291                        reportError("FixedFacetValue", new Object JavaDoc[]{"totalDigits", Integer.toString(fTotalDigits), Integer.toString(fBase.fTotalDigits), fTypeName});
1292                    }
1293                    if (fTotalDigits > fBase.fTotalDigits) {
1294                        reportError( "totalDigits-valid-restriction", new Object JavaDoc[]{Integer.toString(fTotalDigits), Integer.toString(fBase.fTotalDigits), fTypeName});
1295                    }
1296                }
1297            }
1298
1299            // check 4.3.12.c1 must: fractionDigits <= base.totalDigits
1300
if ((fFacetsDefined & FACET_FRACTIONDIGITS) != 0) {
1301                if ((fBase.fFacetsDefined & FACET_TOTALDIGITS) != 0) {
1302                    if (fFractionDigits > fBase.fTotalDigits)
1303                        reportError( "fractionDigits-totalDigits", new Object JavaDoc[]{Integer.toString(fFractionDigits), Integer.toString(fTotalDigits), fTypeName});
1304                }
1305            }
1306
1307            // check 4.3.12.c2 error: fractionDigits > fBase.fractionDigits
1308
// check fixed value for fractionDigits
1309
if (((fFacetsDefined & FACET_FRACTIONDIGITS) != 0)) {
1310                if ((( fBase.fFacetsDefined & FACET_FRACTIONDIGITS) != 0)) {
1311                    if ((fBase.fFixedFacet & FACET_FRACTIONDIGITS) != 0 && fFractionDigits != fBase.fFractionDigits) {
1312                        reportError("FixedFacetValue", new Object JavaDoc[]{"fractionDigits", Integer.toString(fFractionDigits), Integer.toString(fBase.fFractionDigits), fTypeName});
1313                    }
1314                    if (fFractionDigits > fBase.fFractionDigits) {
1315                        reportError( "fractionDigits-valid-restriction", new Object JavaDoc[]{Integer.toString(fFractionDigits), Integer.toString(fBase.fFractionDigits), fTypeName});
1316                    }
1317                }
1318            }
1319
1320            // check 4.3.6.c1 error:
1321
// (whiteSpace = preserve || whiteSpace = replace) && fBase.whiteSpace = collapese or
1322
// whiteSpace = preserve && fBase.whiteSpace = replace
1323

1324            if ( (fFacetsDefined & FACET_WHITESPACE) != 0 && (fBase.fFacetsDefined & FACET_WHITESPACE) != 0 ){
1325                if ( (fBase.fFixedFacet & FACET_WHITESPACE) != 0 && fWhiteSpace != fBase.fWhiteSpace ) {
1326                    reportError( "FixedFacetValue", new Object JavaDoc[]{"whiteSpace", whiteSpaceValue(fWhiteSpace), whiteSpaceValue(fBase.fWhiteSpace), fTypeName});
1327                }
1328
1329                if ( fWhiteSpace == WS_PRESERVE && fBase.fWhiteSpace == WS_COLLAPSE ){
1330                    reportError( "whiteSpace-valid-restriction.1", new Object JavaDoc[]{fTypeName, "preserve"});
1331                }
1332                if ( fWhiteSpace == WS_REPLACE && fBase.fWhiteSpace == WS_COLLAPSE ){
1333                    reportError( "whiteSpace-valid-restriction.1", new Object JavaDoc[]{fTypeName, "replace"});
1334                }
1335                if ( fWhiteSpace == WS_PRESERVE && fBase.fWhiteSpace == WS_REPLACE ){
1336                    reportError( "whiteSpace-valid-restriction.2", new Object JavaDoc[]{fTypeName});
1337                }
1338            }
1339        }//fFacetsDefined != null
1340

1341        // step 4: inherit other facets from base (including fTokeyType)
1342

1343        // inherit length
1344
if ( (fFacetsDefined & FACET_LENGTH) == 0 && (fBase.fFacetsDefined & FACET_LENGTH) != 0 ) {
1345            fFacetsDefined |= FACET_LENGTH;
1346            fLength = fBase.fLength;
1347            lengthAnnotation = fBase.lengthAnnotation;
1348        }
1349        // inherit minLength
1350
if ( (fFacetsDefined & FACET_MINLENGTH) == 0 && (fBase.fFacetsDefined & FACET_MINLENGTH) != 0 ) {
1351            fFacetsDefined |= FACET_MINLENGTH;
1352            fMinLength = fBase.fMinLength;
1353            minLengthAnnotation = fBase.minLengthAnnotation;
1354        }
1355        // inherit maxLength
1356
if ((fFacetsDefined & FACET_MAXLENGTH) == 0 && (fBase.fFacetsDefined & FACET_MAXLENGTH) != 0 ) {
1357            fFacetsDefined |= FACET_MAXLENGTH;
1358            fMaxLength = fBase.fMaxLength;
1359            maxLengthAnnotation = fBase.maxLengthAnnotation;
1360        }
1361        // inherit pattern
1362
if ( (fBase.fFacetsDefined & FACET_PATTERN) != 0 ) {
1363            if ((fFacetsDefined & FACET_PATTERN) == 0) {
1364                fPattern = fBase.fPattern;
1365                fPatternStr = fBase.fPatternStr;
1366                fFacetsDefined |= FACET_PATTERN;
1367            }
1368            else {
1369                for (int i = fBase.fPattern.size()-1; i >= 0; i--) {
1370                    fPattern.addElement(fBase.fPattern.elementAt(i));
1371                    fPatternStr.addElement(fBase.fPatternStr.elementAt(i));
1372                }
1373                if (fBase.patternAnnotations != null){
1374                    for (int i = fBase.patternAnnotations.getLength()-1;i>=0;i--){
1375                        patternAnnotations.add(fBase.patternAnnotations.item(i));
1376                    }
1377                }
1378            }
1379        }
1380        // inherit whiteSpace
1381
if ( (fFacetsDefined & FACET_WHITESPACE) == 0 && (fBase.fFacetsDefined & FACET_WHITESPACE) != 0 ) {
1382            fFacetsDefined |= FACET_WHITESPACE;
1383            fWhiteSpace = fBase.fWhiteSpace;
1384            whiteSpaceAnnotation = fBase.whiteSpaceAnnotation;
1385        }
1386        // inherit enumeration
1387
if ((fFacetsDefined & FACET_ENUMERATION) == 0 && (fBase.fFacetsDefined & FACET_ENUMERATION) != 0) {
1388            fFacetsDefined |= FACET_ENUMERATION;
1389            fEnumeration = fBase.fEnumeration;
1390            enumerationAnnotations = fBase.enumerationAnnotations;
1391        }
1392        // inherit maxExclusive
1393
if ((( fBase.fFacetsDefined & FACET_MAXEXCLUSIVE) != 0) &&
1394            !((fFacetsDefined & FACET_MAXEXCLUSIVE) != 0) && !((fFacetsDefined & FACET_MAXINCLUSIVE) != 0)) {
1395            fFacetsDefined |= FACET_MAXEXCLUSIVE;
1396            fMaxExclusive = fBase.fMaxExclusive;
1397            maxExclusiveAnnotation = fBase.maxExclusiveAnnotation;
1398        }
1399        // inherit maxInclusive
1400
if ((( fBase.fFacetsDefined & FACET_MAXINCLUSIVE) != 0) &&
1401            !((fFacetsDefined & FACET_MAXEXCLUSIVE) != 0) && !((fFacetsDefined & FACET_MAXINCLUSIVE) != 0)) {
1402            fFacetsDefined |= FACET_MAXINCLUSIVE;
1403            fMaxInclusive = fBase.fMaxInclusive;
1404            maxInclusiveAnnotation = fBase.maxInclusiveAnnotation;
1405        }
1406        // inherit minExclusive
1407
if ((( fBase.fFacetsDefined & FACET_MINEXCLUSIVE) != 0) &&
1408            !((fFacetsDefined & FACET_MINEXCLUSIVE) != 0) && !((fFacetsDefined & FACET_MININCLUSIVE) != 0)) {
1409            fFacetsDefined |= FACET_MINEXCLUSIVE;
1410            fMinExclusive = fBase.fMinExclusive;
1411            minExclusiveAnnotation = fBase.minExclusiveAnnotation;
1412        }
1413        // inherit minExclusive
1414
if ((( fBase.fFacetsDefined & FACET_MININCLUSIVE) != 0) &&
1415            !((fFacetsDefined & FACET_MINEXCLUSIVE) != 0) && !((fFacetsDefined & FACET_MININCLUSIVE) != 0)) {
1416            fFacetsDefined |= FACET_MININCLUSIVE;
1417            fMinInclusive = fBase.fMinInclusive;
1418            minInclusiveAnnotation = fBase.minInclusiveAnnotation;
1419        }
1420        // inherit totalDigits
1421
if ((( fBase.fFacetsDefined & FACET_TOTALDIGITS) != 0) &&
1422            !((fFacetsDefined & FACET_TOTALDIGITS) != 0)) {
1423            fFacetsDefined |= FACET_TOTALDIGITS;
1424            fTotalDigits = fBase.fTotalDigits;
1425            totalDigitsAnnotation = fBase.totalDigitsAnnotation;
1426        }
1427        // inherit fractionDigits
1428
if ((( fBase.fFacetsDefined & FACET_FRACTIONDIGITS) != 0)
1429            && !((fFacetsDefined & FACET_FRACTIONDIGITS) != 0)) {
1430            fFacetsDefined |= FACET_FRACTIONDIGITS;
1431            fFractionDigits = fBase.fFractionDigits;
1432            fractionDigitsAnnotation = fBase.fractionDigitsAnnotation;
1433        }
1434        //inherit tokeytype
1435
if ((fPatternType == SPECIAL_PATTERN_NONE ) && (fBase.fPatternType != SPECIAL_PATTERN_NONE)) {
1436            fPatternType = fBase.fPatternType ;
1437        }
1438
1439        // step 5: mark fixed values
1440
fFixedFacet |= fBase.fFixedFacet;
1441
1442        //step 6: setting fundamental facets
1443
caclFundamentalFacets();
1444
1445    } //applyFacets()
1446

1447    /**
1448     * validate a value, and return the compiled form
1449     */

1450    public Object JavaDoc validate(String JavaDoc content, ValidationContext context, ValidatedInfo validatedInfo) throws InvalidDatatypeValueException {
1451
1452        if (context == null)
1453            context = fEmptyContext;
1454
1455        if (validatedInfo == null)
1456            validatedInfo = new ValidatedInfo();
1457        else
1458            validatedInfo.memberType = null;
1459
1460        // first normalize string value, and convert it to actual value
1461
boolean needNormalize = context==null||context.needToNormalize();
1462        Object JavaDoc ob = getActualValue(content, context, validatedInfo, needNormalize);
1463
1464        validate(context, validatedInfo);
1465
1466        return ob;
1467
1468    }
1469
1470    /**
1471     * validate a value, and return the compiled form
1472     */

1473    public Object JavaDoc validate(Object JavaDoc content, ValidationContext context, ValidatedInfo validatedInfo) throws InvalidDatatypeValueException {
1474
1475        if (context == null)
1476            context = fEmptyContext;
1477
1478        if (validatedInfo == null)
1479            validatedInfo = new ValidatedInfo();
1480        else
1481            validatedInfo.memberType = null;
1482
1483        // first normalize string value, and convert it to actual value
1484
boolean needNormalize = context==null||context.needToNormalize();
1485        Object JavaDoc ob = getActualValue(content, context, validatedInfo, needNormalize);
1486
1487        validate(context, validatedInfo);
1488
1489        return ob;
1490
1491    }
1492
1493    /**
1494     * validate an actual value against this DV
1495     *
1496     * @param value the actual value that needs to be validated
1497     * @param context the validation context
1498     * @param validatedInfo used to provide the actual value and member types
1499     */

1500    public void validate(ValidationContext context, ValidatedInfo validatedInfo)
1501        throws InvalidDatatypeValueException {
1502
1503        if (context == null)
1504            context = fEmptyContext;
1505
1506        // then validate the actual value against the facets
1507
if (context.needFacetChecking() &&
1508            (fFacetsDefined != 0 && fFacetsDefined != FACET_WHITESPACE)) {
1509            checkFacets(validatedInfo);
1510        }
1511
1512        // now check extra rules: for ID/IDREF/ENTITY
1513
if (context.needExtraChecking()) {
1514            checkExtraRules(context, validatedInfo);
1515        }
1516
1517    }
1518
1519    private void checkFacets(ValidatedInfo validatedInfo) throws InvalidDatatypeValueException {
1520
1521        Object JavaDoc ob = validatedInfo.actualValue;
1522        String JavaDoc content = validatedInfo.normalizedValue;
1523
1524        // For QName and NOTATION types, we don't check length facets
1525
if (fValidationDV != DV_QNAME && fValidationDV != DV_NOTATION) {
1526            int length = fDVs[fValidationDV].getDataLength(ob);
1527
1528            // maxLength
1529
if ( (fFacetsDefined & FACET_MAXLENGTH) != 0 ) {
1530                if ( length > fMaxLength ) {
1531                    throw new InvalidDatatypeValueException("cvc-maxLength-valid",
1532                            new Object JavaDoc[]{content, Integer.toString(length), Integer.toString(fMaxLength), fTypeName});
1533                }
1534            }
1535
1536            //minLength
1537
if ( (fFacetsDefined & FACET_MINLENGTH) != 0 ) {
1538                if ( length < fMinLength ) {
1539                    throw new InvalidDatatypeValueException("cvc-minLength-valid",
1540                            new Object JavaDoc[]{content, Integer.toString(length), Integer.toString(fMinLength), fTypeName});
1541                }
1542            }
1543
1544            //length
1545
if ( (fFacetsDefined & FACET_LENGTH) != 0 ) {
1546                if ( length != fLength ) {
1547                    throw new InvalidDatatypeValueException("cvc-length-valid",
1548                            new Object JavaDoc[]{content, Integer.toString(length), Integer.toString(fLength), fTypeName});
1549                }
1550            }
1551        }
1552
1553        //enumeration
1554
if ( ((fFacetsDefined & FACET_ENUMERATION) != 0 ) ) {
1555            boolean present = false;
1556            for (int i = 0; i < fEnumeration.size(); i++) {
1557                if (fEnumeration.elementAt(i).equals(ob)) {
1558                    present = true;
1559                    break;
1560                }
1561            }
1562            if(!present){
1563                throw new InvalidDatatypeValueException("cvc-enumeration-valid",
1564                                                        new Object JavaDoc [] {content, fEnumeration.toString()});
1565            }
1566        }
1567
1568        //fractionDigits
1569
if ((fFacetsDefined & FACET_FRACTIONDIGITS) != 0) {
1570            int scale = fDVs[fValidationDV].getFractionDigits(ob);
1571            if (scale > fFractionDigits) {
1572                throw new InvalidDatatypeValueException("cvc-fractionDigits-valid",
1573                                                        new Object JavaDoc[] {content, Integer.toString(scale), Integer.toString(fFractionDigits)});
1574            }
1575        }
1576
1577        //totalDigits
1578
if ((fFacetsDefined & FACET_TOTALDIGITS)!=0) {
1579            int totalDigits = fDVs[fValidationDV].getTotalDigits(ob);
1580            if (totalDigits > fTotalDigits) {
1581                throw new InvalidDatatypeValueException("cvc-totalDigits-valid",
1582                                                        new Object JavaDoc[] {content, Integer.toString(totalDigits), Integer.toString(fTotalDigits)});
1583            }
1584        }
1585
1586        int compare;
1587
1588        //maxinclusive
1589
if ( (fFacetsDefined & FACET_MAXINCLUSIVE) != 0 ) {
1590            compare = fDVs[fValidationDV].compare(ob, fMaxInclusive);
1591            if (compare != -1 && compare != 0) {
1592                throw new InvalidDatatypeValueException("cvc-maxInclusive-valid",
1593                                                        new Object JavaDoc[] {content, fMaxInclusive, fTypeName});
1594            }
1595        }
1596
1597        //maxExclusive
1598
if ( (fFacetsDefined & FACET_MAXEXCLUSIVE) != 0 ) {
1599            compare = fDVs[fValidationDV].compare(ob, fMaxExclusive );
1600            if (compare != -1) {
1601                throw new InvalidDatatypeValueException("cvc-maxExclusive-valid",
1602                                                        new Object JavaDoc[] {content, fMaxExclusive, fTypeName});
1603            }
1604        }
1605
1606        //minInclusive
1607
if ( (fFacetsDefined & FACET_MININCLUSIVE) != 0 ) {
1608            compare = fDVs[fValidationDV].compare(ob, fMinInclusive);
1609            if (compare != 1 && compare != 0) {
1610                throw new InvalidDatatypeValueException("cvc-minInclusive-valid",
1611                                                        new Object JavaDoc[] {content, fMinInclusive, fTypeName});
1612            }
1613        }
1614
1615        //minExclusive
1616
if ( (fFacetsDefined & FACET_MINEXCLUSIVE) != 0 ) {
1617            compare = fDVs[fValidationDV].compare(ob, fMinExclusive);
1618            if (compare != 1) {
1619                throw new InvalidDatatypeValueException("cvc-minExclusive-valid",
1620                                                        new Object JavaDoc[] {content, fMinExclusive, fTypeName});
1621            }
1622        }
1623
1624    }
1625
1626    private void checkExtraRules(ValidationContext context, ValidatedInfo validatedInfo) throws InvalidDatatypeValueException {
1627
1628        Object JavaDoc ob = validatedInfo.actualValue;
1629
1630        if (fVariety == VARIETY_ATOMIC) {
1631
1632            fDVs[fValidationDV].checkExtraRules(ob, context);
1633
1634        } else if (fVariety == VARIETY_LIST) {
1635
1636            ListDV.ListData values = (ListDV.ListData)ob;
1637            int len = values.length();
1638            if (fItemType.fVariety == VARIETY_UNION) {
1639                XSSimpleTypeDecl[] memberTypes = (XSSimpleTypeDecl[])validatedInfo.memberTypes;
1640                XSSimpleType memberType = validatedInfo.memberType;
1641                for (int i = len-1; i >= 0; i--) {
1642                    validatedInfo.actualValue = values.item(i);
1643                    validatedInfo.memberType = memberTypes[i];
1644                    fItemType.checkExtraRules(context, validatedInfo);
1645                }
1646                validatedInfo.memberType = memberType;
1647            } else { // (fVariety == VARIETY_ATOMIC)
1648
for (int i = len-1; i >= 0; i--) {
1649                    validatedInfo.actualValue = values.item(i);
1650                    fItemType.checkExtraRules(context, validatedInfo);
1651                }
1652            }
1653            validatedInfo.actualValue = values;
1654
1655        } else { // (fVariety == VARIETY_UNION)
1656

1657            ((XSSimpleTypeDecl)validatedInfo.memberType).checkExtraRules(context, validatedInfo);
1658
1659        }
1660
1661    }// checkExtraRules()
1662

1663    //we can still return object for internal use.
1664
private Object JavaDoc getActualValue(Object JavaDoc content, ValidationContext context,
1665                                  ValidatedInfo validatedInfo, boolean needNormalize)
1666            throws InvalidDatatypeValueException{
1667
1668        String JavaDoc nvalue;
1669        if (needNormalize) {
1670            nvalue = normalize(content, fWhiteSpace);
1671        } else {
1672            nvalue = content.toString();
1673        }
1674        if ( (fFacetsDefined & FACET_PATTERN ) != 0 ) {
1675            RegularExpression regex;
1676            for (int idx = fPattern.size()-1; idx >= 0; idx--) {
1677                regex = (RegularExpression)fPattern.elementAt(idx);
1678                if (!regex.matches(nvalue)){
1679                    throw new InvalidDatatypeValueException("cvc-pattern-valid",
1680                                                            new Object JavaDoc[]{content,
1681                                                            fPatternStr.elementAt(idx),
1682
1683                                                            fTypeName});
1684                }
1685            }
1686        }
1687
1688        if (fVariety == VARIETY_ATOMIC) {
1689
1690            // validate special kinds of token, in place of old pattern matching
1691
if (fPatternType != SPECIAL_PATTERN_NONE) {
1692
1693                boolean seenErr = false;
1694                if (fPatternType == SPECIAL_PATTERN_NMTOKEN) {
1695                    // PATTERN "\\c+"
1696
seenErr = !XMLChar.isValidNmtoken(nvalue);
1697                }
1698                else if (fPatternType == SPECIAL_PATTERN_NAME) {
1699                    // PATTERN "\\i\\c*"
1700
seenErr = !XMLChar.isValidName(nvalue);
1701                }
1702                else if (fPatternType == SPECIAL_PATTERN_NCNAME) {
1703                    // PATTERN "[\\i-[:]][\\c-[:]]*"
1704
seenErr = !XMLChar.isValidNCName(nvalue);
1705                }
1706                if (seenErr) {
1707                    throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1",
1708                                                            new Object JavaDoc[]{nvalue, SPECIAL_PATTERN_STRING[fPatternType]});
1709                }
1710            }
1711
1712            validatedInfo.normalizedValue = nvalue;
1713            Object JavaDoc avalue = fDVs[fValidationDV].getActualValue(nvalue, context);
1714            validatedInfo.actualValue = avalue;
1715            validatedInfo.actualValueType = fBuiltInKind;
1716
1717            return avalue;
1718
1719        } else if (fVariety == VARIETY_LIST) {
1720
1721            StringTokenizer JavaDoc parsedList = new StringTokenizer JavaDoc(nvalue, " ");
1722            int countOfTokens = parsedList.countTokens() ;
1723            Object JavaDoc[] avalue = new Object JavaDoc[countOfTokens];
1724            boolean isUnion = fItemType.getVariety() == VARIETY_UNION;
1725            short[] itemTypes = new short[isUnion ? countOfTokens : 1];
1726            if (!isUnion)
1727                itemTypes[0] = fItemType.fBuiltInKind;
1728            XSSimpleTypeDecl[] memberTypes = new XSSimpleTypeDecl[countOfTokens];
1729            for(int i = 0 ; i < countOfTokens ; i ++){
1730                // we can't call fItemType.validate(), otherwise checkExtraRules()
1731
// will be called twice: once in fItemType.validate, once in
1732
// validate method of this type.
1733
// so we take two steps to get the actual value:
1734
// 1. fItemType.getActualValue()
1735
// 2. fItemType.chekcFacets()
1736
avalue[i] = fItemType.getActualValue(parsedList.nextToken(), context, validatedInfo, false);
1737                if (context.needFacetChecking() &&
1738                    (fItemType.fFacetsDefined != 0 && fItemType.fFacetsDefined != FACET_WHITESPACE)) {
1739                    fItemType.checkFacets(validatedInfo);
1740                }
1741                memberTypes[i] = (XSSimpleTypeDecl)validatedInfo.memberType;
1742                if (isUnion)
1743                    itemTypes[i] = memberTypes[i].fBuiltInKind;
1744            }
1745
1746            ListDV.ListData v = new ListDV.ListData(avalue);
1747            validatedInfo.actualValue = v;
1748            validatedInfo.actualValueType = isUnion ? XSConstants.LISTOFUNION_DT : XSConstants.LIST_DT;
1749            validatedInfo.memberType = null;
1750            validatedInfo.memberTypes = memberTypes;
1751            validatedInfo.itemValueTypes = new ShortListImpl(itemTypes, itemTypes.length);
1752            validatedInfo.normalizedValue = nvalue;
1753
1754            return v;
1755
1756        } else { // (fVariety == VARIETY_UNION)
1757
for(int i = 0 ; i < fMemberTypes.length; i++) {
1758                try {
1759                    // we can't call fMemberType[i].validate(), otherwise checkExtraRules()
1760
// will be called twice: once in fMemberType[i].validate, once in
1761
// validate method of this type.
1762
// so we take two steps to get the actual value:
1763
// 1. fMemberType[i].getActualValue()
1764
// 2. fMemberType[i].chekcFacets()
1765
Object JavaDoc aValue = fMemberTypes[i].getActualValue(content, context, validatedInfo, true);
1766                    if (context.needFacetChecking() &&
1767                        (fMemberTypes[i].fFacetsDefined != 0 && fMemberTypes[i].fFacetsDefined != FACET_WHITESPACE)) {
1768                        fMemberTypes[i].checkFacets(validatedInfo);
1769                    }
1770                    validatedInfo.memberType = fMemberTypes[i];
1771                    return aValue;
1772                } catch(InvalidDatatypeValueException invalidValue) {
1773                }
1774            }
1775
1776            throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.2",
1777                                                    new Object JavaDoc[]{content, fTypeName});
1778        }
1779
1780    }//getActualValue()
1781

1782    public boolean isEqual(Object JavaDoc value1, Object JavaDoc value2) {
1783        if (value1 == null)
1784            return false;
1785        return value1.equals(value2);
1786    }//isEqual()
1787

1788    // normalize the string according to the whiteSpace facet
1789
public static String JavaDoc normalize(String JavaDoc content, short ws) {
1790        int len = content == null ? 0 : content.length();
1791        if (len == 0 || ws == WS_PRESERVE)
1792            return content;
1793
1794        StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
1795        if (ws == WS_REPLACE) {
1796            char ch;
1797            // when it's replace, just replace #x9, #xa, #xd by #x20
1798
for (int i = 0; i < len; i++) {
1799                ch = content.charAt(i);
1800                if (ch != 0x9 && ch != 0xa && ch != 0xd)
1801                    sb.append(ch);
1802                else
1803                    sb.append((char)0x20);
1804            }
1805        } else {
1806            char ch;
1807            int i;
1808            boolean isLeading = true;
1809            // when it's collapse
1810
for (i = 0; i < len; i++) {
1811                ch = content.charAt(i);
1812                // append real characters, so we passed leading ws
1813
if (ch != 0x9 && ch != 0xa && ch != 0xd && ch != 0x20) {
1814                    sb.append(ch);
1815                    isLeading = false;
1816                }
1817                else {
1818                    // for whitespaces, we skip all following ws
1819
for (; i < len-1; i++) {
1820                        ch = content.charAt(i+1);
1821                        if (ch != 0x9 && ch != 0xa && ch != 0xd && ch != 0x20)
1822                            break;
1823                    }
1824                    // if it's not a leading or tailing ws, then append a space
1825
if (i < len - 1 && !isLeading)
1826                        sb.append((char)0x20);
1827                }
1828            }
1829        }
1830
1831        return sb.toString();
1832    }
1833
1834    // normalize the string according to the whiteSpace facet
1835
protected String JavaDoc normalize(Object JavaDoc content, short ws) {
1836        if (content == null)
1837            return null;
1838
1839        // If pattern is not defined, we can skip some of the normalization.
1840
// Otherwise we have to normalize the data for correct result of
1841
// pattern validation.
1842
if ( (fFacetsDefined & FACET_PATTERN ) == 0 ) {
1843            short norm_type = fDVNormalizeType[fValidationDV];
1844            if (norm_type == NORMALIZE_NONE) {
1845                return content.toString();
1846            }
1847            else if (norm_type == NORMALIZE_TRIM) {
1848                return content.toString().trim();
1849            }
1850        }
1851
1852        if (!(content instanceof StringBuffer JavaDoc)) {
1853            String JavaDoc strContent = content.toString();
1854            return normalize(strContent, ws);
1855        }
1856
1857        StringBuffer JavaDoc sb = (StringBuffer JavaDoc)content;
1858        int len = sb.length();
1859        if (len == 0)
1860            return "";
1861        if (ws == WS_PRESERVE)
1862            return sb.toString();
1863
1864        if (ws == WS_REPLACE) {
1865            char ch;
1866            // when it's replace, just replace #x9, #xa, #xd by #x20
1867
for (int i = 0; i < len; i++) {
1868                ch = sb.charAt(i);
1869                if (ch == 0x9 || ch == 0xa || ch == 0xd)
1870                    sb.setCharAt(i, (char)0x20);
1871            }
1872        } else {
1873            char ch;
1874            int i, j = 0;
1875            boolean isLeading = true;
1876            // when it's collapse
1877
for (i = 0; i < len; i++) {
1878                ch = sb.charAt(i);
1879                // append real characters, so we passed leading ws
1880
if (ch != 0x9 && ch != 0xa && ch != 0xd && ch != 0x20) {
1881                    sb.setCharAt(j++, ch);
1882                    isLeading = false;
1883                }
1884                else {
1885                    // for whitespaces, we skip all following ws
1886
for (; i < len-1; i++) {
1887                        ch = sb.charAt(i+1);
1888                        if (ch != 0x9 && ch != 0xa && ch != 0xd && ch != 0x20)
1889                            break;
1890                    }
1891                    // if it's not a leading or tailing ws, then append a space
1892
if (i < len - 1 && !isLeading)
1893                        sb.setCharAt(j++, (char)0x20);
1894                }
1895            }
1896            sb.setLength(j);
1897        }
1898
1899        return sb.toString();
1900    }
1901
1902    void reportError(String JavaDoc key, Object JavaDoc[] args) throws InvalidDatatypeFacetException {
1903        throw new InvalidDatatypeFacetException(key, args);
1904    }
1905
1906
1907    private String JavaDoc whiteSpaceValue(short ws){
1908        return WS_FACET_STRING[ws];
1909    }
1910
1911    public short getOrdered() {
1912        return fOrdered;
1913    }
1914
1915    public boolean getBounded(){
1916        return fBounded;
1917    }
1918
1919    public boolean getFinite(){
1920        return fFinite;
1921    }
1922
1923    public boolean getNumeric(){
1924        return fNumeric;
1925    }
1926
1927    public boolean isDefinedFacet(short facetName) {
1928        if ((fFacetsDefined & facetName) != 0)
1929            return true;
1930        if (fPatternType != SPECIAL_PATTERN_NONE)
1931            return facetName == FACET_PATTERN;
1932        if (fValidationDV == DV_INTEGER)
1933            return facetName == FACET_PATTERN || facetName == FACET_FRACTIONDIGITS;
1934        return false;
1935    }
1936
1937    public short getDefinedFacets() {
1938        if (fPatternType != SPECIAL_PATTERN_NONE)
1939            return (short)(fFacetsDefined | FACET_PATTERN);
1940        if (fValidationDV == DV_INTEGER)
1941            return (short)(fFacetsDefined | FACET_PATTERN | FACET_FRACTIONDIGITS);
1942        return fFacetsDefined;
1943    }
1944
1945    public boolean isFixedFacet(short facetName) {
1946        if ((fFixedFacet & facetName) != 0)
1947            return true;
1948        if (fValidationDV == DV_INTEGER)
1949            return facetName == FACET_FRACTIONDIGITS;
1950        return false;
1951    }
1952
1953    public short getFixedFacets() {
1954        if (fValidationDV == DV_INTEGER)
1955            return (short)(fFixedFacet | FACET_FRACTIONDIGITS);
1956        return fFixedFacet;
1957    }
1958
1959    public String JavaDoc getLexicalFacetValue(short facetName) {
1960        switch (facetName) {
1961        case FACET_LENGTH:
1962            return (fLength == -1)?null:Integer.toString(fLength);
1963        case FACET_MINLENGTH:
1964            return (fMinLength == -1)?null:Integer.toString(fMinLength);
1965        case FACET_MAXLENGTH:
1966            return (fMaxLength == -1)?null:Integer.toString(fMaxLength);
1967        case FACET_WHITESPACE:
1968            return WS_FACET_STRING[fWhiteSpace];
1969        case FACET_MAXINCLUSIVE:
1970            return (fMaxInclusive == null)?null:fMaxInclusive.toString();
1971        case FACET_MAXEXCLUSIVE:
1972            return (fMaxExclusive == null)?null:fMaxExclusive.toString();
1973        case FACET_MINEXCLUSIVE:
1974            return (fMinExclusive == null)?null:fMinExclusive.toString();
1975        case FACET_MININCLUSIVE:
1976            return (fMinInclusive == null)?null:fMinInclusive.toString();
1977        case FACET_TOTALDIGITS:
1978            if (fValidationDV == DV_INTEGER)
1979                return "0";
1980            return (fTotalDigits == -1)?null:Integer.toString(fTotalDigits);
1981        case FACET_FRACTIONDIGITS:
1982            return (fFractionDigits == -1)?null:Integer.toString(fFractionDigits);
1983        }
1984        return null;
1985    }
1986
1987    public StringList getLexicalEnumeration() {
1988        if (fLexicalEnumeration == null){
1989            if (fEnumeration == null)
1990                return null;
1991            int size = fEnumeration.size();
1992            String JavaDoc[] strs = new String JavaDoc[size];
1993            for (int i = 0; i < size; i++)
1994                strs[i] = fEnumeration.elementAt(i).toString();
1995            fLexicalEnumeration = new StringListImpl(strs, size);
1996        }
1997        return fLexicalEnumeration;
1998    }
1999
2000    public StringList getLexicalPattern() {
2001        if (fPatternType == SPECIAL_PATTERN_NONE && fValidationDV != DV_INTEGER && fPatternStr == null)
2002            return null;
2003        if (fLexicalPattern == null){
2004            int size = fPatternStr == null ? 0 : fPatternStr.size();
2005            String JavaDoc[] strs;
2006            if (fPatternType == SPECIAL_PATTERN_NMTOKEN) {
2007                strs = new String JavaDoc[size+1];
2008                strs[size] = "\\c+";
2009            }
2010            else if (fPatternType == SPECIAL_PATTERN_NAME) {
2011                strs = new String JavaDoc[size+1];
2012                strs[size] = "\\i\\c*";
2013            }
2014            else if (fPatternType == SPECIAL_PATTERN_NCNAME) {
2015                strs = new String JavaDoc[size+2];
2016                strs[size] = "\\i\\c*";
2017                strs[size+1] = "[\\i-[:]][\\c-[:]]*";
2018            }
2019            else if (fValidationDV == DV_INTEGER) {
2020                strs = new String JavaDoc[size+1];
2021                strs[size] = "[+\\-]?[0-9]+";
2022            }
2023            else {
2024                strs = new String JavaDoc[size];
2025            }
2026            for (int i = 0; i < size; i++)
2027                strs[i] = (String JavaDoc)fPatternStr.elementAt(i);
2028            fLexicalPattern = new StringListImpl(strs, size);
2029        }
2030        return fLexicalPattern;
2031    }
2032
2033    public XSObjectList getAnnotations() {
2034        return fAnnotations;
2035    }
2036
2037    private void caclFundamentalFacets() {
2038        setOrdered();
2039        setNumeric();
2040        setBounded();
2041        setCardinality();
2042    }
2043
2044    private void setOrdered(){
2045
2046        // When {variety} is atomic, {value} is inherited from {value} of {base type definition}. For all "primitive" types {value} is as specified in the table in Fundamental Facets (C.1).
2047
if(fVariety == VARIETY_ATOMIC){
2048            this.fOrdered = fBase.fOrdered;
2049        }
2050
2051        // When {variety} is list, {value} is false.
2052
else if(fVariety == VARIETY_LIST){
2053            this.fOrdered = ORDERED_FALSE;
2054        }
2055
2056        // When {variety} is union, the {value} is partial unless one of the following:
2057
// 1. If every member of {member type definitions} is derived from a common ancestor other than the simple ur-type, then {value} is the same as that ancestor's ordered facet.
2058
// 2. If every member of {member type definitions} has a {value} of false for the ordered facet, then {value} is false.
2059
else if(fVariety == VARIETY_UNION){
2060            int length = fMemberTypes.length;
2061            // REVISIT: is the length possible to be 0?
2062
if (length == 0) {
2063                this.fOrdered = ORDERED_PARTIAL;
2064                return;
2065            }
2066            // we need to process the first member type before entering the loop
2067
short ancestorId = getPrimitiveDV(fMemberTypes[0].fValidationDV);
2068            boolean commonAnc = ancestorId != DV_ANYSIMPLETYPE;
2069            boolean allFalse = fMemberTypes[0].fOrdered == ORDERED_FALSE;
2070            // for the other member types, check whether the value is false
2071
// and whether they have the same ancestor as the first one
2072
for (int i = 1; i < fMemberTypes.length && (commonAnc || allFalse); i++) {
2073                if (commonAnc)
2074                    commonAnc = ancestorId == getPrimitiveDV(fMemberTypes[i].fValidationDV);
2075                if (allFalse)
2076                    allFalse = fMemberTypes[i].fOrdered == ORDERED_FALSE;
2077            }
2078            if (commonAnc) {
2079                // REVISIT: all member types should have the same ordered value
2080
// just use the first one. Can we assume this?
2081
this.fOrdered = fMemberTypes[0].fOrdered;
2082            } else if (allFalse) {
2083                this.fOrdered = ORDERED_FALSE;
2084            } else {
2085                this.fOrdered = ORDERED_PARTIAL;
2086            }
2087        }
2088
2089    }//setOrdered
2090

2091    private void setNumeric(){
2092        if(fVariety == VARIETY_ATOMIC){
2093            this.fNumeric = fBase.fNumeric;
2094        }
2095        else if(fVariety == VARIETY_LIST){
2096            this.fNumeric = false;
2097        }
2098        else if(fVariety == VARIETY_UNION){
2099            XSSimpleType[] memberTypes = fMemberTypes;
2100            for(int i = 0 ; i < memberTypes.length ; i++){
2101                if(!memberTypes[i].getNumeric() ){
2102                    this.fNumeric = false;
2103                    return;
2104                }
2105            }
2106            this.fNumeric = true;
2107        }
2108
2109    }//setNumeric
2110

2111    private void setBounded(){
2112        if(fVariety == VARIETY_ATOMIC){
2113            if( (((this.fFacetsDefined & FACET_MININCLUSIVE) != 0) || ((this.fFacetsDefined & FACET_MINEXCLUSIVE) != 0))
2114                && (((this.fFacetsDefined & FACET_MAXINCLUSIVE) != 0) || ((this.fFacetsDefined & FACET_MAXEXCLUSIVE) != 0)) ){
2115                this.fBounded = true;
2116            }
2117            else{
2118                this.fBounded = false;
2119            }
2120        }
2121        else if(fVariety == VARIETY_LIST){
2122            if( ((this.fFacetsDefined & FACET_LENGTH) != 0 ) || ( ((this.fFacetsDefined & FACET_MINLENGTH) != 0 )
2123                                                            && ((this.fFacetsDefined & FACET_MAXLENGTH) != 0 )) ){
2124                this.fBounded = true;
2125            }
2126            else{
2127                this.fBounded = false;
2128            }
2129
2130        }
2131        else if(fVariety == VARIETY_UNION){
2132
2133            XSSimpleTypeDecl [] memberTypes = this.fMemberTypes;
2134            short ancestorId = 0 ;
2135
2136            if(memberTypes.length > 0){
2137                ancestorId = getPrimitiveDV(memberTypes[0].fValidationDV);
2138            }
2139
2140            for(int i = 0 ; i < memberTypes.length ; i++){
2141                if(!memberTypes[i].getBounded() || (ancestorId != getPrimitiveDV(memberTypes[i].fValidationDV)) ){
2142                    this.fBounded = false;
2143                    return;
2144                }
2145            }
2146            this.fBounded = true;
2147        }
2148
2149    }//setBounded
2150

2151    private boolean specialCardinalityCheck(){
2152        if( (fBase.fValidationDV == XSSimpleTypeDecl.DV_DATE) || (fBase.fValidationDV == XSSimpleTypeDecl.DV_GYEARMONTH)
2153            || (fBase.fValidationDV == XSSimpleTypeDecl.DV_GYEAR) || (fBase.fValidationDV == XSSimpleTypeDecl.DV_GMONTHDAY)
2154            || (fBase.fValidationDV == XSSimpleTypeDecl.DV_GDAY) || (fBase.fValidationDV == XSSimpleTypeDecl.DV_GMONTH) ){
2155            return true;
2156        }
2157        return false;
2158
2159    } //specialCardinalityCheck()
2160

2161    private void setCardinality(){
2162        if(fVariety == VARIETY_ATOMIC){
2163            if(fBase.fFinite){
2164                this.fFinite = true;
2165            }
2166            else {// (!fBase.fFinite)
2167
if ( ((this.fFacetsDefined & FACET_LENGTH) != 0 ) || ((this.fFacetsDefined & FACET_MAXLENGTH) != 0 )
2168                     || ((this.fFacetsDefined & FACET_TOTALDIGITS) != 0 ) ){
2169                    this.fFinite = true;
2170                }
2171                else if( (((this.fFacetsDefined & FACET_MININCLUSIVE) != 0 ) || ((this.fFacetsDefined & FACET_MINEXCLUSIVE) != 0 ))
2172                        && (((this.fFacetsDefined & FACET_MAXINCLUSIVE) != 0 ) || ((this.fFacetsDefined & FACET_MAXEXCLUSIVE) != 0 )) ){
2173                    if( ((this.fFacetsDefined & FACET_FRACTIONDIGITS) != 0 ) || specialCardinalityCheck()){
2174                        this.fFinite = true;
2175                    }
2176                    else{
2177                        this.fFinite = false;
2178                    }
2179                }
2180                else{
2181                    this.fFinite = false;
2182                }
2183            }
2184        }
2185        else if(fVariety == VARIETY_LIST){
2186            if( ((this.fFacetsDefined & FACET_LENGTH) != 0 ) || ( ((this.fFacetsDefined & FACET_MINLENGTH) != 0 )
2187                                                            && ((this.fFacetsDefined & FACET_MAXLENGTH) != 0 )) ){
2188                this.fFinite = true;
2189            }
2190            else{
2191                this.fFinite = false;
2192            }
2193
2194        }
2195        else if(fVariety == VARIETY_UNION){
2196            XSSimpleType [] memberTypes = fMemberTypes;
2197            for(int i = 0 ; i < memberTypes.length ; i++){
2198                if(!(memberTypes[i].getFinite()) ){
2199                    this.fFinite = false;
2200                    return;
2201                }
2202            }
2203            this.fFinite = true;
2204        }
2205
2206    }//setCardinality
2207

2208    private short getPrimitiveDV(short validationDV){
2209
2210        if (validationDV == DV_ID || validationDV == DV_IDREF || validationDV == DV_ENTITY){
2211            return DV_STRING;
2212        }
2213        else if (validationDV == DV_INTEGER) {
2214            return DV_DECIMAL;
2215        }
2216        else {
2217            return validationDV;
2218        }
2219
2220    }//getPrimitiveDV()
2221

2222    public boolean derivedFromType(XSTypeDefinition ancestor, short derivation) {
2223        // REVISIT: implement according to derivation
2224

2225        // ancestor is null, retur false
2226
if (ancestor == null)
2227            return false;
2228        // ancestor is anyType, return true
2229
// anyType is the only type whose base type is itself
2230
if (ancestor.getBaseType() == ancestor)
2231            return true;
2232        // recursively get base, and compare it with ancestor
2233
XSTypeDefinition type = this;
2234        while (type != ancestor && // compare with ancestor
2235
type != fAnySimpleType) { // reached anySimpleType
2236
type = type.getBaseType();
2237        }
2238
2239        return type == ancestor;
2240    }
2241
2242    public boolean derivedFrom(String JavaDoc ancestorNS, String JavaDoc ancestorName, short derivation) {
2243        // REVISIT: implement according to derivation
2244

2245        // ancestor is null, retur false
2246
if (ancestorName == null)
2247            return false;
2248        // ancestor is anyType, return true
2249
if (URI_SCHEMAFORSCHEMA.equals(ancestorNS) &&
2250            ANY_TYPE.equals(ancestorName)) {
2251            return true;
2252        }
2253
2254        // recursively get base, and compare it with ancestor
2255
XSTypeDefinition type = this;
2256        while (!(ancestorName.equals(type.getName()) &&
2257                 ((ancestorNS == null && type.getNamespace() == null) ||
2258                  (ancestorNS != null && ancestorNS.equals(type.getNamespace())))) && // compare with ancestor
2259
type != fAnySimpleType) { // reached anySimpleType
2260
type = (XSTypeDefinition)type.getBaseType();
2261        }
2262
2263        return type != fAnySimpleType;
2264    }
2265
2266    static final XSSimpleTypeDecl fAnySimpleType = new XSSimpleTypeDecl(null, "anySimpleType", DV_ANYSIMPLETYPE, ORDERED_FALSE, false, true, false, true, XSConstants.ANYSIMPLETYPE_DT);
2267
2268    /**
2269     * Validation context used to validate facet values.
2270     */

2271    static final ValidationContext fDummyContext = new ValidationContext() {
2272        public boolean needFacetChecking() {
2273            return true;
2274        }
2275
2276        public boolean needExtraChecking() {
2277            return false;
2278        }
2279        public boolean needToNormalize() {
2280            return false;
2281        }
2282        public boolean useNamespaces() {
2283            return true;
2284        }
2285
2286        public boolean isEntityDeclared(String JavaDoc name) {
2287            return false;
2288        }
2289
2290        public boolean isEntityUnparsed(String JavaDoc name) {
2291            return false;
2292        }
2293
2294        public boolean isIdDeclared(String JavaDoc name) {
2295            return false;
2296        }
2297
2298        public void addId(String JavaDoc name) {
2299        }
2300
2301        public void addIdRef(String JavaDoc name) {
2302        }
2303
2304        public String JavaDoc getSymbol (String JavaDoc symbol) {
2305            return null;
2306        }
2307
2308        public String JavaDoc getURI(String JavaDoc prefix) {
2309            return null;
2310        }
2311    };
2312
2313    /**
2314     * A wrapper of ValidationContext, to provide a way of switching to a
2315     * different Namespace declaration context.
2316     */

2317    class ValidationContextImpl implements ValidationContext {
2318        ValidationContext fExternal;
2319        ValidationContextImpl(ValidationContext external) {
2320            fExternal = external;
2321        }
2322
2323        NamespaceContext fNSContext;
2324        void setNSContext(NamespaceContext nsContext) {
2325            fNSContext = nsContext;
2326        }
2327
2328        public boolean needFacetChecking() {
2329            return fExternal.needFacetChecking();
2330        }
2331
2332        public boolean needExtraChecking() {
2333            return fExternal.needExtraChecking();
2334        }
2335        public boolean needToNormalize() {
2336            return fExternal.needToNormalize();
2337        }
2338        // schema validation is predicated upon namespaces
2339
public boolean useNamespaces() {
2340            return true;
2341        }
2342
2343        public boolean isEntityDeclared (String JavaDoc name) {
2344            return fExternal.isEntityDeclared(name);
2345        }
2346
2347        public boolean isEntityUnparsed (String JavaDoc name) {
2348            return fExternal.isEntityUnparsed(name);
2349        }
2350
2351        public boolean isIdDeclared (String JavaDoc name) {
2352            return fExternal.isIdDeclared(name);
2353        }
2354
2355        public void addId(String JavaDoc name) {
2356            fExternal.addId(name);
2357        }
2358
2359        public void addIdRef(String JavaDoc name) {
2360            fExternal.addIdRef(name);
2361        }
2362
2363        public String JavaDoc getSymbol (String JavaDoc symbol) {
2364            return fExternal.getSymbol(symbol);
2365        }
2366
2367        public String JavaDoc getURI(String JavaDoc prefix) {
2368            if (fNSContext == null)
2369                return fExternal.getURI(prefix);
2370            else
2371                return fNSContext.getURI(prefix);
2372        }
2373    }
2374
2375    public void reset(){
2376
2377        // if it's immutable, can't be reset:
2378
if (fIsImmutable) return;
2379        fItemType = null;
2380        fMemberTypes = null;
2381
2382        fTypeName = null;
2383        fTargetNamespace = null;
2384        fFinalSet = 0;
2385        fBase = null;
2386        fVariety = -1;
2387        fValidationDV = -1;
2388
2389        fFacetsDefined = 0;
2390        fFixedFacet = 0;
2391
2392        //for constraining facets
2393
fWhiteSpace = 0;
2394        fLength = -1;
2395        fMinLength = -1;
2396        fMaxLength = -1;
2397        fTotalDigits = -1;
2398        fFractionDigits = -1;
2399        fPattern = null;
2400        fPatternStr = null;
2401        fEnumeration = null;
2402        fLexicalPattern = null;
2403        fLexicalEnumeration = null;
2404        fMaxInclusive = null;
2405        fMaxExclusive = null;
2406        fMinExclusive = null;
2407        fMinInclusive = null;
2408        lengthAnnotation = null;
2409        minLengthAnnotation = null;
2410        maxLengthAnnotation = null;
2411        whiteSpaceAnnotation = null;
2412        totalDigitsAnnotation = null;
2413        fractionDigitsAnnotation = null;
2414        patternAnnotations = null;
2415        enumerationAnnotations = null;
2416        maxInclusiveAnnotation = null;
2417        maxExclusiveAnnotation = null;
2418        minInclusiveAnnotation = null;
2419        minExclusiveAnnotation = null;
2420
2421        fPatternType = SPECIAL_PATTERN_NONE;
2422        fAnnotations = null;
2423        fFacets = null;
2424
2425        // REVISIT: reset for fundamental facets
2426
}
2427    /**
2428     * @see com.sun.org.apache.xerces.internal.xs.XSObject#getNamespaceItem()
2429     */

2430    public XSNamespaceItem getNamespaceItem() {
2431        // REVISIT: implement
2432
return null;
2433    }
2434
2435    /**
2436     * @see java.lang.Object#toString()
2437     */

2438    public String JavaDoc toString() {
2439        return this.fTargetNamespace+"," +this.fTypeName;
2440    }
2441
2442    /* (non-Javadoc)
2443     * @see com.sun.org.apache.xerces.internal.xs.XSSimpleTypeDefinition#getFacets()
2444     */

2445    public XSObjectList getFacets() {
2446        if (fFacets == null &&
2447            (fFacetsDefined != 0 || fValidationDV == DV_INTEGER)) {
2448
2449            XSFacetImpl[] facets = new XSFacetImpl[10];
2450            int count = 0;
2451            if ((fFacetsDefined & FACET_WHITESPACE) != 0) {
2452                facets[count] =
2453                    new XSFacetImpl(
2454                        FACET_WHITESPACE,
2455                        WS_FACET_STRING[fWhiteSpace],
2456                        (fFixedFacet & FACET_WHITESPACE) != 0,
2457                        whiteSpaceAnnotation);
2458                count++;
2459            }
2460            if (fLength != -1) {
2461                facets[count] =
2462                    new XSFacetImpl(
2463                        FACET_LENGTH,
2464                        Integer.toString(fLength),
2465                        (fFixedFacet & FACET_LENGTH) != 0,
2466                        lengthAnnotation);
2467                count++;
2468            }
2469            if (fMinLength != -1) {
2470                facets[count] =
2471                    new XSFacetImpl(
2472                        FACET_MINLENGTH,
2473                        Integer.toString(fMinLength),
2474                        (fFixedFacet & FACET_MINLENGTH) != 0,
2475                        minLengthAnnotation);
2476                count++;
2477            }
2478            if (fMaxLength != -1) {
2479                facets[count] =
2480                    new XSFacetImpl(
2481                        FACET_MAXLENGTH,
2482                        Integer.toString(fMaxLength),
2483                        (fFixedFacet & FACET_MAXLENGTH) != 0,
2484                        maxLengthAnnotation);
2485                count++;
2486            }
2487            if (fTotalDigits != -1) {
2488                facets[count] =
2489                    new XSFacetImpl(
2490                        FACET_TOTALDIGITS,
2491                        Integer.toString(fTotalDigits),
2492                        (fFixedFacet & FACET_TOTALDIGITS) != 0,
2493                        totalDigitsAnnotation);
2494                count++;
2495            }
2496            if (fValidationDV == DV_INTEGER) {
2497                facets[count] =
2498                    new XSFacetImpl(
2499                        FACET_FRACTIONDIGITS,
2500                        "0",
2501                        true,
2502                        null);
2503                count++;
2504            }
2505            if (fFractionDigits != -1) {
2506                facets[count] =
2507                    new XSFacetImpl(
2508                        FACET_FRACTIONDIGITS,
2509                        Integer.toString(fFractionDigits),
2510                        (fFixedFacet & FACET_FRACTIONDIGITS) != 0,
2511                        fractionDigitsAnnotation);
2512                count++;
2513            }
2514            if (fMaxInclusive != null) {
2515                facets[count] =
2516                    new XSFacetImpl(
2517                        FACET_MAXINCLUSIVE,
2518                        fMaxInclusive.toString(),
2519                        (fFixedFacet & FACET_MAXINCLUSIVE) != 0,
2520                        maxInclusiveAnnotation);
2521                count++;
2522            }
2523            if (fMaxExclusive != null) {
2524                facets[count] =
2525                    new XSFacetImpl(
2526                        FACET_MAXEXCLUSIVE,
2527                        fMaxExclusive.toString(),
2528                        (fFixedFacet & FACET_MAXEXCLUSIVE) != 0,
2529                        maxExclusiveAnnotation);
2530                count++;
2531            }
2532            if (fMinExclusive != null) {
2533                facets[count] =
2534                    new XSFacetImpl(
2535                        FACET_MINEXCLUSIVE,
2536                        fMinExclusive.toString(),
2537                        (fFixedFacet & FACET_MINEXCLUSIVE) != 0,
2538                        minExclusiveAnnotation);
2539                count++;
2540            }
2541            if (fMinInclusive != null) {
2542                facets[count] =
2543                    new XSFacetImpl(
2544                        FACET_MININCLUSIVE,
2545                        fMinInclusive.toString(),
2546                        (fFixedFacet & FACET_MININCLUSIVE) != 0,
2547                        minInclusiveAnnotation);
2548                count++;
2549            }
2550            fFacets = new XSObjectListImpl(facets, count);
2551        }
2552        return fFacets;
2553    }
2554    
2555    public XSObjectList getMultiValueFacets(){
2556        if (fMultiValueFacets == null &&
2557            ((fFacetsDefined & FACET_ENUMERATION) != 0 ||
2558             (fFacetsDefined & FACET_PATTERN) != 0 ||
2559             fPatternType != SPECIAL_PATTERN_NONE ||
2560             fValidationDV == DV_INTEGER)) {
2561
2562            XSMVFacetImpl[] facets = new XSMVFacetImpl[2];
2563            int count = 0;
2564            if ((fFacetsDefined & FACET_PATTERN) != 0 ||
2565                fPatternType != SPECIAL_PATTERN_NONE ||
2566                fValidationDV == DV_INTEGER) {
2567                facets[count] =
2568                    new XSMVFacetImpl(
2569                        FACET_PATTERN,
2570                        this.getLexicalPattern(),
2571                        patternAnnotations);
2572                count++;
2573            }
2574            if (fEnumeration != null) {
2575                facets[count] =
2576                    new XSMVFacetImpl(
2577                        FACET_ENUMERATION,
2578                        this.getLexicalEnumeration(),
2579                        enumerationAnnotations);
2580                count++;
2581            }
2582            fMultiValueFacets = new XSObjectListImpl(facets, count);
2583        }
2584        return fMultiValueFacets;
2585    }
2586    
2587    private static final class XSFacetImpl implements XSFacet {
2588        final short kind;
2589        final String JavaDoc value;
2590        final boolean fixed;
2591        final XSAnnotation annotation;
2592
2593        public XSFacetImpl(short kind, String JavaDoc value, boolean fixed, XSAnnotation annotation) {
2594            this.kind = kind;
2595            this.value = value;
2596            this.fixed = fixed;
2597            this.annotation = annotation;
2598        }
2599        /* (non-Javadoc)
2600         * @see com.sun.org.apache.xerces.internal.xs.XSFacet#getAnnotation()
2601         */

2602        public XSAnnotation getAnnotation() {
2603            return annotation;
2604        }
2605
2606        /* (non-Javadoc)
2607         * @see com.sun.org.apache.xerces.internal.xs.XSFacet#getFacetKind()
2608         */

2609        public short getFacetKind() {
2610            return kind;
2611        }
2612
2613        /* (non-Javadoc)
2614         * @see com.sun.org.apache.xerces.internal.xs.XSFacet#getLexicalFacetValue()
2615         */

2616        public String JavaDoc getLexicalFacetValue() {
2617            return value;
2618        }
2619
2620        /* (non-Javadoc)
2621         * @see com.sun.org.apache.xerces.internal.xs.XSFacet#isFixed()
2622         */

2623        public boolean getFixed() {
2624            return fixed;
2625        }
2626
2627        /* (non-Javadoc)
2628         * @see com.sun.org.apache.xerces.internal.xs.XSObject#getName()
2629         */

2630        public String JavaDoc getName() {
2631            return null;
2632        }
2633
2634        /* (non-Javadoc)
2635         * @see com.sun.org.apache.xerces.internal.xs.XSObject#getNamespace()
2636         */

2637        public String JavaDoc getNamespace() {
2638            return null;
2639        }
2640
2641        /* (non-Javadoc)
2642         * @see com.sun.org.apache.xerces.internal.xs.XSObject#getNamespaceItem()
2643         */

2644        public XSNamespaceItem getNamespaceItem() {
2645            // REVISIT: implement
2646
return null;
2647        }
2648
2649        /* (non-Javadoc)
2650         * @see com.sun.org.apache.xerces.internal.xs.XSObject#getType()
2651         */

2652        public short getType() {
2653            return XSConstants.FACET;
2654        }
2655
2656    }
2657    
2658    private static final class XSMVFacetImpl implements XSMultiValueFacet {
2659        final short kind;
2660        XSObjectList annotations;
2661        StringList values;
2662
2663        public XSMVFacetImpl(short kind, StringList values, XSObjectList annotations) {
2664            this.kind = kind;
2665            this.values = values;
2666            this.annotations = annotations;
2667        }
2668 
2669
2670        /* (non-Javadoc)
2671         * @see com.sun.org.apache.xerces.internal.xs.XSFacet#getFacetKind()
2672         */

2673        public short getFacetKind() {
2674            return kind;
2675        }
2676
2677
2678        /* (non-Javadoc)
2679         * @see com.sun.org.apache.xerces.internal.xs.XSMultiValueFacet#getAnnotations()
2680         */

2681        public XSObjectList getAnnotations() {
2682            return annotations;
2683        }
2684
2685        /* (non-Javadoc)
2686         * @see com.sun.org.apache.xerces.internal.xs.XSMultiValueFacet#getLexicalFacetValues()
2687         */

2688        public StringList getLexicalFacetValues() {
2689            return values;
2690        }
2691
2692        /* (non-Javadoc)
2693         * @see com.sun.org.apache.xerces.internal.xs.XSObject#getName()
2694         */

2695        public String JavaDoc getName() {
2696            return null;
2697        }
2698
2699        /* (non-Javadoc)
2700         * @see com.sun.org.apache.xerces.internal.xs.XSObject#getNamespace()
2701         */

2702        public String JavaDoc getNamespace() {
2703            return null;
2704        }
2705
2706        /* (non-Javadoc)
2707         * @see com.sun.org.apache.xerces.internal.xs.XSObject#getNamespaceItem()
2708         */

2709        public XSNamespaceItem getNamespaceItem() {
2710            // REVISIT: implement
2711
return null;
2712        }
2713
2714        /* (non-Javadoc)
2715         * @see com.sun.org.apache.xerces.internal.xs.XSObject#getType()
2716         */

2717        public short getType() {
2718            return XSConstants.MULTIVALUE_FACET;
2719        }
2720    }
2721
2722    /**
2723     * @see org.w3c.dom.TypeInfo#getTypeName()
2724     */

2725    public String JavaDoc getTypeName() {
2726        if(getAnonymous())
2727            return "anonymousSimpleType"+fUniqueId;
2728        else
2729            return getName();
2730    }
2731    
2732    /**
2733     * @see org.w3c.dom.TypeInfo#getTypeNamespace()
2734     */

2735    public String JavaDoc getTypeNamespace() {
2736            return getNamespace();
2737    }
2738    
2739    public boolean isDerivedFrom(String JavaDoc typeNamespaceArg, String JavaDoc typeNameArg, int derivationMethod) {
2740        return derivedFrom(typeNamespaceArg,typeNameArg,(short)derivationMethod);
2741    }
2742    
2743
2744} // class XSSimpleTypeDecl
2745

2746
Popular Tags