View Javadoc
1   /*
2    * Copyright 2007 Keith Visco, Ralf Joachim
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    * 
7    * http://www.apache.org/licenses/LICENSE-2.0
8    * 
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  package org.exolab.castor.builder.types;
15  
16  import java.util.Enumeration;
17  
18  import org.exolab.castor.xml.schema.Facet;
19  import org.exolab.castor.xml.schema.SimpleType;
20  import org.exolab.javasource.JSourceCode;
21  import org.exolab.javasource.JType;
22  
23  /**
24   * The base XML Schema Type class.
25   * 
26   * @author <a href="mailto:keith AT kvisco DOT com">Keith Visco</a>
27   * @author <a href="mailto:ralf DOT joachim AT syscon DOT eu">Ralf Joachim</a>
28   * @version $Revision$ $Date: 2005-03-05 06:42:06 -0700 (Sat, 05 Mar 2005) $
29   */
30  public abstract class XSType {
31    // --------------------------------------------------------------------------
32  
33    // special types
34  
35    /** Value meaning the type is unassigned. */
36    public static final short NULL = -1;
37  
38    /** A user-defined type. */
39    public static final short CLASS = 0;
40  
41    // primitive types
42  
43    /** xsd:string. */
44    public static final short STRING_TYPE = 1;
45  
46    /** xsd:duration. */
47    public static final short DURATION_TYPE = 2;
48  
49    /** xsd:dateTime. */
50    public static final short DATETIME_TYPE = 3;
51  
52    /** xsd:time. */
53    public static final short TIME_TYPE = 4;
54  
55    /** xsd:date. */
56    public static final short DATE_TYPE = 5;
57  
58    /** xsd:gYearMonth. */
59    public static final short GYEARMONTH_TYPE = 6;
60  
61    /** xsd:gYear. */
62    public static final short GYEAR_TYPE = 7;
63  
64    /** xsd:gMonthDay. */
65    public static final short GMONTHDAY_TYPE = 8;
66  
67    /** xsd:gDay. */
68    public static final short GDAY_TYPE = 9;
69  
70    /** xsd:gMonth. */
71    public static final short GMONTH_TYPE = 10;
72  
73    /** xsd:boolean. */
74    public static final short BOOLEAN_TYPE = 11;
75  
76    /** xsd:base64Binary. */
77    public static final short BASE64BINARY_TYPE = 12;
78  
79    /** xsd:hexBinary. */
80    public static final short HEXBINARY_TYPE = 13;
81  
82    /** xsd:float. */
83    public static final short FLOAT_TYPE = 14;
84  
85    /** xsd:double. */
86    public static final short DOUBLE_TYPE = 15;
87  
88    /** xsd:decimal. */
89    public static final short DECIMAL_TYPE = 16;
90  
91    /** xsd:anyURI. */
92    public static final short ANYURI_TYPE = 17;
93  
94    /** xsd:QName. */
95    public static final short QNAME_TYPE = 18;
96  
97    /** xsd:notation. */
98    public static final short NOTATION_TYPE = 19;
99  
100   // derived types
101 
102   /** xsd:normalizedString. */
103   public static final short NORMALIZEDSTRING_TYPE = 20;
104 
105   /** xsd:token. */
106   public static final short TOKEN_TYPE = 21;
107 
108   /** xsd:language. */
109   public static final short LANGUAGE_TYPE = 22;
110 
111   /** xsd:name. */
112   public static final short NAME_TYPE = 23;
113 
114   /** xsd:NCNAME. */
115   public static final short NCNAME_TYPE = 24;
116 
117   /** xsd:ID. */
118   public static final short ID_TYPE = 25;
119 
120   /** xsd:IDREF. */
121   public static final short IDREF_TYPE = 26;
122 
123   /** xsd:IDREFS. */
124   public static final short IDREFS_TYPE = 27;
125 
126   /** xsd:ENTITY. */
127   public static final short ENTITY = 28;
128 
129   /** xsd:ENTITIES. */
130   public static final short ENTITIES = 29;
131 
132   /** xsd:NMTOKEN. */
133   public static final short NMTOKEN_TYPE = 30;
134 
135   /** xsd:NMTOKENS. */
136   public static final short NMTOKENS_TYPE = 31;
137 
138   /** xsd:integer. */
139   public static final short INTEGER_TYPE = 32;
140 
141   /** xsd:nonPositiveInteger. */
142   public static final short NON_POSITIVE_INTEGER_TYPE = 33;
143 
144   /** xsd:negativeInteger. */
145   public static final short NEGATIVE_INTEGER_TYPE = 34;
146 
147   /** xsd:long. */
148   public static final short LONG_TYPE = 35;
149 
150   /** xsd:int. */
151   public static final short INT_TYPE = 36;
152 
153   /** xsd:short. */
154   public static final short SHORT_TYPE = 37;
155 
156   /** xsd:byte. */
157   public static final short BYTE_TYPE = 38;
158 
159   /** xsd:nonNegativeInteger. */
160   public static final short NON_NEGATIVE_INTEGER_TYPE = 39;
161 
162   /** xsd:positiveInteger. */
163   public static final short POSITIVE_INTEGER_TYPE = 44;
164 
165   /** A collection type. */
166   public static final short COLLECTION = 45;
167 
168   /** xsd:unsignedLong. */
169   public static final short UNSIGNED_LONG_TYPE = 46;
170 
171   /** xsd:unsignedShort. */
172   public static final short UNSIGNED_SHORT_TYPE = 47;
173 
174   /** xsd:unsignedByte. */
175   public static final short UNSIGNED_BYTE_TYPE = 48;
176 
177   /** xsd:unsignedInt. */
178   public static final short UNSIGNED_INT_TYPE = 49;
179 
180   // --------------------------------------------------------------------------
181 
182   /** Flag signaling an enumerated type. */
183   private boolean _enumerated = false;
184 
185   // --------------------------------------------------------------------------
186 
187   /**
188    * Returns true if this XSType represents an enumerated type.
189    * 
190    * @return True if this XSType represents an enumerated type.
191    */
192   public final boolean isEnumerated() {
193     return _enumerated;
194   }
195 
196   /**
197    * Sets the enumerated flag for this XSClass.
198    *
199    * @param enumerated A boolean indicating whether or not this XSClass represents an enumerated
200    *        type.
201    */
202   public final void setAsEnumerated(final boolean enumerated) {
203     _enumerated = enumerated;
204   }
205 
206   // --------------------------------------------------------------------------
207 
208   /**
209    * Returns the name of this XSType.
210    * 
211    * @return The name of this XSType.
212    */
213   public abstract String getName();
214 
215   /**
216    * Returns the type of this XSType.
217    * 
218    * @return the type of this XSType.
219    */
220   public abstract short getType();
221 
222   /**
223    * Returns true if this XSType represents a primitive type.
224    * 
225    * @return True if this XSType represents a primitive type.
226    */
227   public abstract boolean isPrimitive();
228 
229   /**
230    * Returns true if the XSType represents an XML Schema date/time type.
231    * 
232    * @return True if the XSType represents an XML Schema date/time type.
233    */
234   public abstract boolean isDateTime();
235 
236   /**
237    * Returns true if this XSType represents a collection.
238    * 
239    * @return True if this XSType represents a collection.
240    */
241   public boolean isCollection() {
242     return false;
243   }
244 
245   /**
246    * Returns the JType that this XSType represents.
247    * 
248    * @return The JType that this XSType represents.
249    */
250   public abstract JType getJType();
251 
252   /**
253    * Returns the Java code neccessary to create a new instance of the JType associated with this
254    * XSType.
255    *
256    * @return The Java code neccessary to create a new instance.
257    */
258   public abstract String newInstanceCode();
259 
260   /**
261    * Returns the string necessary to convert an instance of this XSType to an Object. This method is
262    * really only useful for primitive types.
263    *
264    * @param variableName The name of the instance variable.
265    * @return The String necessary to convert an instance of this XSType to an Object.
266    */
267   public abstract String createToJavaObjectCode(final String variableName);
268 
269   /**
270    * Returns the string necessary to convert an Object to an instance of this XSType. This method is
271    * really only useful for primitive types.
272    *
273    * @param variableName The name of the Object.
274    * @return The String necessary to convert an Object to an instance of this XSType.
275    */
276   public abstract String createFromJavaObjectCode(final String variableName);
277 
278   // --------------------------------------------------------------------------
279 
280   /**
281    * Creates source code for an additional constructor that deals with default values as specified
282    * in the XML schema instance.
283    * 
284    * @param variableName Name of the constructor argument.
285    * @return Source code for dealing with default values.
286    */
287   public String createDefaultValueWithString(final String variableName) {
288     return " new " + getJType() + "(" + variableName + ")";
289   }
290 
291   /**
292    * Reads and sets the facets for XSType.
293    * 
294    * @param simpleType The SimpleType containing the facets.
295    */
296   public final void setFacets(final SimpleType simpleType) {
297     Enumeration<Facet> enumeration = simpleType.getEffectiveFacets();
298     while (enumeration.hasMoreElements()) {
299       setFacet(enumeration.nextElement());
300     }
301   }
302 
303   /**
304    * Set the given facet for XSType if applicable.
305    * 
306    * @param facet The facet to set for XSType.
307    */
308   protected abstract void setFacet(final Facet facet);
309 
310   /**
311    * Creates the validation code for an instance of this XSType. If necessary the validation code
312    * should create a newly configured TypeValidator, that should then be added to a FieldValidator
313    * instance whose name is provided.
314    *
315    * @param jsc The JSourceCode to fill in.
316    * @param fixedValue A fixed value to use if any.
317    * @param validatorInstanceName The name of the FieldValidator that the configured TypeValidator
318    *        should be added to.
319    */
320   public abstract void validationCode(final JSourceCode jsc, final String fixedValue,
321       final String validatorInstanceName);
322 
323   // --------------------------------------------------------------------------
324 }