KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > schema > SimpleType


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.schema;
24
25 import java.util.ArrayList JavaDoc;
26
27 import org.xquark.schema.datatypes.PrimitiveType;
28 import org.xquark.schema.validation.DefaultValidationInfo;
29 import org.xquark.schema.validation.ValidationContextProvider;
30 import org.xquark.schema.validation.ValidationInfo;
31
32 /**
33  * class for Simple Type Definition.
34  *
35  * <p>This is the implementation of the Simple Type Definition.
36  * The following properties are defined:</p>
37  * <p> 1. name : optional, null or a NCName, defined in SchemaComponent.</p>
38  * <p> 2. target namespace : null or a namespace name, defined in SchemaComponent.</p>
39  * <p> 3. base type defintion : a simple type defintion, defined in Type.</p>
40  * <p> 4. facets : a set of constraining facets. </p>
41  * <p> 5. variety : one of {VARIETY_ATOMIC, VARIETY_LIST, VARIETY_UNION}. </p>
42  * <p> 6. primitive type definition : if variety is VARIETY_ATOMIC, a built-in primitive type must be defined. </p>
43  * <p> 7. item type definition : if variety is VARIETY_LIST, an item type (which is a simple type) must be defined. </p>
44  * <p> 8. member type definitions : if variety is VARIETY_UNION, a non-empty sequence of simple type definitions must be defined. </p>
45  *
46  * <p>See XML Schema Part 1: Structures 3.14
47  * W3C Recommendation 2 May 2001</p>
48  *
49  * @see org.xquark.xml.schema.SchemaComponent
50  * @see org.xquark.xml.schema.Type
51  */

52 public final class SimpleType extends Type {
53     private static final String JavaDoc RCSRevision = "$Revision: 1.2 $";
54     private static final String JavaDoc RCSName = "$Name: $";
55     // constants used by SimpleType
56
public static final int VARIETY_ATOMIC = 1;
57     public static final int VARIETY_LIST = 2;
58     public static final int VARIETY_UNION = 3;
59
60     private PrimitiveType primitive = null;
61     private java.util.HashMap JavaDoc facets = new java.util.HashMap JavaDoc(10);
62
63     // nVariety : VARIETY_ATOMIC, VARIETY_LIST, VARIETY_UNION
64
private int nVariety = VARIETY_ATOMIC;
65
66     private Type itemType = null; // used only when nVariety = VARIETY_LIST
67
private ArrayList JavaDoc memberTypes = null; // used only when nVariety = VARIETY_UNION
68

69     /**
70      * Create a new SimpleType.
71      *
72      * @param schema The schema which contains this attribute declaration.
73      * @param name The simple type name, null or a NCName.
74      * @param baseType The base type definition, null or a simple type.
75      */

76     public SimpleType(Schema schema, String JavaDoc name, Type baseType) {
77         super(schema, name, baseType);
78     }
79
80     public void accept(SchemaVisitor visitor) throws SchemaException {
81         visitor.visit(this);
82     }
83
84     public int getContentType() {
85         return TEXT_ONLY;
86     }
87
88     public SimpleType getValueType() {
89         return this;
90     }
91
92     public void setVariety(int nVariety) {
93         switch (nVariety) {
94             case VARIETY_ATOMIC :
95             case VARIETY_LIST :
96                 this.nVariety = nVariety;
97                 break;
98             case VARIETY_UNION :
99                 this.nVariety = nVariety;
100                 if (memberTypes == null)
101                     memberTypes = new ArrayList JavaDoc();
102                 break;
103             default :
104                 // use the default value if nVariety is unfound
105
this.nVariety = VARIETY_ATOMIC;
106         }
107     }
108
109     /**
110      * Return the type's variety. One of {VARIETY_ATOMIC, VARIETY_LIST, VARIETY_UNION}.
111      *
112      * @return The variety.
113      */

114     public int getVariety() {
115         return nVariety;
116     }
117
118     public int getDerivationMethod() {
119         return RESTRICTION;
120     }
121
122     /**
123      * @return null if valid, a string containing the error code otherwise
124      */

125
126     public String JavaDoc checkTypeDerivationOK(Type ancestor, int exclusions, boolean useBlock) {
127         if (SIMPLE_UR_TYPE.equals(ancestor.getName()))
128             return null;
129         if (!ancestor.isSimpleType()) {
130             if (ANY_TYPE.equals(ancestor.getName()))
131                 return null;
132             else
133                 return "cos-st-derived-ok.2.2";
134         }
135         SimpleType simpleBase = (SimpleType) ancestor;
136         if (this == simpleBase)
137             return null;
138         java.util.Collection JavaDoc memberTypes = simpleBase.getMemberTypes();
139         if (memberTypes != null) {
140             java.util.Iterator JavaDoc it = memberTypes.iterator();
141             while (it.hasNext()) {
142                 if (it.next() == this)
143                     return null;
144             }
145         }
146         if ((getDerivationMethod() & exclusions) != 0)
147             return "cos-st-derived-ok.2.1";
148         Type parent = getBaseType();
149         if (parent == null)
150             return "cos-st-derived-ok.2.2";
151         return parent.checkTypeDerivationOK(ancestor, exclusions, useBlock);
152     }
153
154     public String JavaDoc normalizedValue(String JavaDoc value) {
155         return primitive.normalizedValue(value);
156     }
157
158     public StringBuffer JavaDoc normalizedValue(StringBuffer JavaDoc value) {
159         return primitive.normalizedValue(value);
160     }
161
162     public StringBuffer JavaDoc normalizedValue(StringBuffer JavaDoc value, StringBuffer JavaDoc out) {
163         return primitive.normalizedValue(value, out);
164     }
165
166     public String JavaDoc toXMLString(Object JavaDoc value, ValidationContextProvider context) {
167         return primitive.toXMLString(value, context);
168     }
169
170     public ValidationInfo validate(String JavaDoc value, boolean normalize, ValidationContextProvider context)
171         throws SchemaException {
172         try {
173             if (nVariety == VARIETY_UNION) return validateUnion(value, normalize, context);
174             else {
175                 DefaultValidationInfo info = new DefaultValidationInfo();
176                 info.setValidationType(this);
177                 if (normalize) value = primitive.normalizedValue(value);
178                 info.setNormalizedValue(value);
179                 info.setActualValue(primitive.convert(value, false, context));
180                 return info;
181             }
182         } catch (SchemaException ex) {
183             throw new SchemaException("cvc-simple-type", this, ex);
184         }
185     }
186
187     public ValidationInfo validate(StringBuffer JavaDoc value, boolean normalize, ValidationContextProvider context)
188         throws SchemaException {
189             try {
190                 if (nVariety == VARIETY_UNION) return validateUnion(value, normalize, context);
191                 else {
192                     DefaultValidationInfo info = new DefaultValidationInfo();
193                     info.setValidationType(this);
194                     if (normalize) value = primitive.normalizedValue(value);
195                     info.setNormalizedValueBuffer(value);
196                     info.setActualValue(primitive.convert(value, false, context));
197                     return info;
198                 }
199             } catch (SchemaException ex) {
200                 throw new SchemaException("cvc-simple-type", this, ex);
201             }
202     }
203
204     private ValidationInfo validateUnion(String JavaDoc value, boolean normalize, ValidationContextProvider context) throws SchemaException {
205         ValidationInfo info = null;
206         ArrayList JavaDoc exceptions = null;
207         primitive.checkPattern(value);
208         for (int i = 0; i < memberTypes.size(); i++) {
209             try {
210                 SimpleType member = (SimpleType) memberTypes.get(i);
211                 info = member.validate(value, normalize, context);
212                 break;
213             } catch (SchemaException ex) {
214                 if (exceptions == null) exceptions = new ArrayList JavaDoc();
215                 exceptions.add(ex);
216             }
217         }
218         if (info != null) {
219             primitive.checkFacets(info.getActualValue());
220             return info;
221         } else {
222             throw new SchemaException("cvc-datatype-valid.1.2.3", this, exceptions);
223         }
224     }
225     
226     private ValidationInfo validateUnion(StringBuffer JavaDoc value, boolean normalize, ValidationContextProvider context) throws SchemaException {
227         ValidationInfo info = validateUnion(value.toString(), normalize, context);
228         value.setLength(0);
229         value.append(info.getNormalizedValue());
230         return info;
231     }
232     
233     public Object JavaDoc convert(String JavaDoc value, boolean normalize, ValidationContextProvider context) throws SchemaException {
234         try {
235             return primitive.convert(value, normalize, context);
236         } catch (SchemaException ex) {
237             throw new SchemaException("cvc-simple-type", this, ex);
238         }
239     }
240
241     public Object JavaDoc convert(StringBuffer JavaDoc value, boolean normalize, ValidationContextProvider context)
242         throws SchemaException {
243         try {
244             return primitive.convert(value, normalize, context);
245         } catch (SchemaException ex) {
246             throw new SchemaException("cvc-simple-type", this, ex);
247         }
248     }
249     
250     public Object JavaDoc convertObject(Object JavaDoc data) throws SchemaException {
251         return primitive.convertObject(data);
252     }
253
254     public String JavaDoc toCanonicalForm(String JavaDoc value, ValidationContextProvider context) throws SchemaException {
255         ValidationInfo info = validate(value, true, context);
256         SimpleType realType = info.getValidationType();
257         Object JavaDoc obj = info.getActualValue();
258         return realType.toXMLString(obj, context);
259     }
260
261     public String JavaDoc validateDefault(String JavaDoc value, ValidationContextProvider context) throws SchemaException {
262         try {
263             return toCanonicalForm(value, context);
264         } catch (SchemaException ex) {
265             throw new SchemaException("cos-valid-default.1", this, ex);
266         }
267     }
268
269     /**
270      * Return the type's built-in primitive type;
271      *
272      * @return The PrimitiveType.
273      */

274     public PrimitiveType getPrimitive() {
275         return primitive;
276     }
277
278     /**
279      * Look up all contraining facets.
280      * example: type is an instance of SimpleType
281      * Collection facets = type.getAllFacets();
282      * Iterator it = facets.iterator();
283      * // find facet name and vale
284      * while ( it.hasNext() ) {
285      * Facet fct = (Facet)it.next();
286      * String name = fct.getName();
287      * String value = fct.getValue();
288      * Boolean fixed = fct.getFixed(); // if the facet value is fixed, return true
289      * HashSet enums = fct.getEnumFacets(); // get all enumeration values
290      * }
291      *
292      * @return A collection view of contraining facets.
293      */

294     public java.util.Collection JavaDoc getAllFacets() {
295         return facets.values();
296     }
297
298     public java.util.HashMap JavaDoc getFacets() {
299         return facets;
300     }
301
302     public void setFacet(Facet facet) throws SchemaException {
303         String JavaDoc facetName = facet.getName();
304
305         // facet : enumeration
306
if (ENUMERATION_TAG.equals(facetName)) {
307             Facet enumFacet = (Facet) facets.get(facetName);
308             if (enumFacet == null) {
309                 enumFacet = facet;
310                 facets.put(facetName, enumFacet);
311             }
312             enumFacet.addFacet(facet.getValue());
313         }
314
315         // facet : pattern
316
else if (PATTERN_TAG.equals(facetName)) {
317             Facet pattternFacet = (Facet) facets.get(facetName);
318             if (pattternFacet == null) {
319                 pattternFacet = facet;
320                 facets.put(facetName, pattternFacet);
321             } else {
322                 String JavaDoc pat = pattternFacet.getValue();
323                 String JavaDoc facetValue = facet.getValue();
324                 if (pat != null)
325                     facetValue = pat + "|" + facetValue;
326                 pattternFacet.setValue(facetValue);
327             }
328         }
329
330         // other facet
331
else {
332             if (facets.put(facetName, facet) != null) {
333                 // "Facet "+facetName+" cannot appear twice in a simple type restriction";
334
throw new SchemaException("src-single-facet-value", this);
335             }
336         }
337
338     }
339
340     public void setPrimitive(PrimitiveType primitive) {
341         this.primitive = primitive;
342     }
343
344     /**
345      * If variety is VARIETY_LIST, return the type's item type;
346      * else, return null.
347      *
348      * @return The item type, a simple type definition.
349      */

350     public Type getItemType() {
351         return itemType;
352     }
353
354     // used only when nVariety = VARIETY_LIST
355
public void setItemType(Type type) {
356         itemType = type;
357     }
358
359     /**
360      * If variety is VARIETY_UNION, return a non-empty sequence of simple type defintions;
361      * else, return null.
362      *
363      * @return The member types, an ArrayList which contains simple type definition.
364      */

365     public ArrayList JavaDoc getMemberTypes() {
366         return memberTypes;
367     }
368
369     public SimpleType getMemberType(int index) {
370         if (index < memberTypes.size())
371             return (SimpleType) memberTypes.get(index);
372         else
373             return null;
374     }
375
376     // used only when nVariety = VARIETY_UNION
377
public void addMemberType(Type type) {
378         if (memberTypes == null)
379             memberTypes = new ArrayList JavaDoc();
380         memberTypes.add(type);
381     }
382
383     // used only when nVariety = VARIETY_UNION
384
public void addMemberTypes(java.util.List JavaDoc memberTypes) {
385         if (this.memberTypes == null)
386             this.memberTypes = new ArrayList JavaDoc();
387         this.memberTypes.addAll(memberTypes);
388     }
389
390     // TYPING STUFF
391
public boolean isSimpleType() {
392         return true;
393     }
394
395 }
396
Popular Tags