001: /**
002: * Redistribution and use of this software and associated documentation
003: * ("Software"), with or without modification, are permitted provided
004: * that the following conditions are met:
005: *
006: * 1. Redistributions of source code must retain copyright
007: * statements and notices. Redistributions must also contain a
008: * copy of this document.
009: *
010: * 2. Redistributions in binary form must reproduce the
011: * above copyright notice, this list of conditions and the
012: * following disclaimer in the documentation and/or other
013: * materials provided with the distribution.
014: *
015: * 3. The name "Exolab" must not be used to endorse or promote
016: * products derived from this Software without prior written
017: * permission of Intalio, Inc. For written permission,
018: * please contact info@exolab.org.
019: *
020: * 4. Products derived from this Software may not be called "Exolab"
021: * nor may "Exolab" appear in their names without prior written
022: * permission of Intalio, Inc. Exolab is a registered
023: * trademark of Intalio, Inc.
024: *
025: * 5. Due credit should be given to the Exolab Project
026: * (http://www.exolab.org/).
027: *
028: * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
029: * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
030: * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
031: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
032: * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
033: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
034: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
035: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
036: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
037: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
038: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
039: * OF THE POSSIBILITY OF SUCH DAMAGE.
040: *
041: * Copyright 1999-2003 (C) Intalio, Inc. All Rights Reserved.
042: *
043: * $Id: TypeInfo.java 5951 2006-05-30 22:18:48Z bsnyder $
044: */package org.exolab.castor.mapping.loader;
045:
046: import org.exolab.castor.mapping.TypeConvertor;
047: import org.exolab.castor.mapping.CollectionHandler;
048:
049: /**
050: * Type information passed on creation of a {@link FieldHandlerImpl}.
051: *
052: *
053: * @author <a href="arkin@intalio.com">Assaf Arkin</a>
054: * @version $Revision: 5951 $ $Date: 2005-03-05 06:42:06 -0700 (Sat, 05 Mar 2005) $
055: */
056: public class TypeInfo {
057:
058: /**
059: * The field type.
060: */
061: private Class _fieldType;
062:
063: /**
064: * Convertor to the field type from external type.
065: */
066: private TypeConvertor _convertorTo;
067:
068: /**
069: * Convertor from the field type to external type.
070: */
071: private TypeConvertor _convertorFrom;
072:
073: /**
074: * Optional parameter for the convertor.
075: */
076: private String _convertorParam;
077:
078: /**
079: * True if the field type is immutable.
080: */
081: private boolean _immutable = false;
082:
083: /**
084: * True if the field is required.
085: */
086: private boolean _required = false;
087:
088: /**
089: * The default value of the field.
090: */
091: private Object _default;
092:
093: /**
094: * The collection handler of the field.
095: */
096: private CollectionHandler _colHandler;
097:
098: /**
099: * Construct new type information for a field. This field
100: * requires no type conversion, and has no default value.
101: *
102: * @param fieldType The field type
103: **/
104: public TypeInfo(Class fieldType) {
105: this (fieldType, null, null, null, false, null, null, true);
106: } //-- TypeInfo
107:
108: /**
109: * Construct new type information for the field.
110: *
111: * @param fieldType The field type
112: * @param convertorTo Convertor to the field type from external
113: * type, or null if no conversion is required
114: * @param convertorFrom Convertor from the field type to external
115: * type, or null if no conversion is required
116: * @param required True if the field is required
117: * @param defaultValue The default value of the field, null to
118: * use the known Java defaults
119: * @param colHandler The collection handler for this field, or null if
120: * field is singular
121: */
122: public TypeInfo(Class fieldType, TypeConvertor convertorTo,
123: TypeConvertor convertorFrom, boolean required,
124: Object defaultValue, CollectionHandler colHandler) {
125: this (fieldType, convertorTo, convertorFrom, null, required,
126: defaultValue, colHandler, true);
127: }
128:
129: /**
130: * Construct new type information for the field.
131: *
132: * @param fieldType The field type
133: * @param convertorTo Convertor to the field type from external
134: * type, or null if no conversion is required
135: * @param convertorFrom Convertor from the field type to external
136: * type, or null if no conversion is required
137: * @param convertorParam Optional parameter for the convertor,
138: * or null if either no conversion is required or no parameter is specified
139: * @param required True if the field is required
140: * @param defaultValue The default value of the field, null to
141: * use the known Java defaults
142: * @param colHandler The collection handler for this field, or null if
143: * field is singular
144: */
145: public TypeInfo(Class fieldType, TypeConvertor convertorTo,
146: TypeConvertor convertorFrom, String convertorParam,
147: boolean required, Object defaultValue,
148: CollectionHandler colHandler) {
149: this (fieldType, convertorTo, convertorFrom, convertorParam,
150: required, defaultValue, colHandler, true);
151: }
152:
153: /**
154: * Construct new type information for the field.
155: *
156: * @param fieldType The field type
157: * @param convertorTo Convertor to the field type from external
158: * type, or null if no conversion is required
159: * @param convertorFrom Convertor from the field type to external
160: * type, or null if no conversion is required
161: * @param convertorParam Optional parameter for the convertor,
162: * or null if either no conversion is required or no parameter is specified
163: * @param required True if the field is required
164: * @param defaultValue The default value of the field, null to
165: * use the known Java defaults
166: * @param colHandler The collection handler for this field, or null if
167: * field is singular
168: */
169: public TypeInfo(Class fieldType, TypeConvertor convertorTo,
170: TypeConvertor convertorFrom, String convertorParam,
171: boolean required, Object defaultValue,
172: CollectionHandler colHandler, boolean checkForCollection) {
173:
174: if ((colHandler == null) && checkForCollection) {
175:
176: if (fieldType.isArray()) {
177: //-- byte[] should not use a CollectionHandler since it
178: //-- needs to be base64 encoded/decoded.
179: if (fieldType.getComponentType() != Byte.TYPE) {
180: try {
181: colHandler = CollectionHandlers
182: .getHandler(Object[].class);
183: } catch (Exception e) {
184: //-- If we make it here, there was probably something wrong
185: //-- with loading the J1CollectionHandlers class...
186: throw new NullPointerException(
187: "Impossible to locate CollectionHandler for array.");
188: }
189: }
190:
191: } else {
192: try {
193: colHandler = CollectionHandlers
194: .getHandler(fieldType);
195: } catch (Exception e) {
196: // NOOP : It just mean that there is not handler for the collection
197: // and that this fieldType is not a collection
198: }
199: }
200: }
201:
202: _fieldType = fieldType;
203: _convertorTo = convertorTo;
204: _convertorFrom = convertorFrom;
205: _convertorParam = convertorParam;
206: _immutable = Types.isImmutable(fieldType);
207: _required = required;
208: // Note: must be called with fieldType (might be primitive) and not
209: // _fieldType (never primitive) to get the proper default value
210: _default = (defaultValue == null ? Types.getDefault(fieldType)
211: : defaultValue);
212: _colHandler = colHandler;
213: }
214:
215: /**
216: * Returns the field type.
217: *
218: * @return The field type
219: */
220: public Class getFieldType() {
221: return _fieldType;
222: }
223:
224: /**
225: * Returns the convertor to the field type from an external type.
226: *
227: * @return Convertor to field type
228: */
229: public TypeConvertor getConvertorTo() {
230: return _convertorTo;
231: }
232:
233: /**
234: * Returns the convertor from the field type to an external type.
235: *
236: * @return Convertor from field type
237: */
238: public TypeConvertor getConvertorFrom() {
239: return _convertorFrom;
240: }
241:
242: /**
243: * Returns the convertor parameter.
244: *
245: * @return Convertor parameter
246: */
247: public String getConvertorParam() {
248: return _convertorParam;
249: }
250:
251: /**
252: * Returns true if field type is immutable.
253: *
254: * @return True if type is immutable
255: */
256: public boolean isImmutable() {
257: return _immutable;
258: }
259:
260: /**
261: * Returns true if field type is required.
262: *
263: * @return True if field is required
264: */
265: public boolean isRequired() {
266: return _required;
267: }
268:
269: /**
270: * Returns the default value for the field.
271: *
272: * @return The default value
273: */
274: public Object getDefaultValue() {
275: return _default;
276: }
277:
278: /**
279: * Return the collection handler of this field.
280: *
281: * @return The collection handler of this field
282: */
283: public CollectionHandler getCollectionHandler() {
284: return _colHandler;
285: }
286:
287: /**
288: * Sets a flag indictating if the field is required.
289: *
290: * @param required the value of the flag. Should be true if the
291: * field is required, false otherwise.
292: */
293: public void setRequired(boolean required) {
294: this ._required = required;
295: } //-- setRequired
296:
297: /**
298: * Sets the CollectionHandler to use for the field
299: * described by this TypeInfo.
300: *
301: * @param handler the CollectionHandler, or null if no
302: * CollectionHandler should be used.
303: */
304: public void setCollectionHandler(CollectionHandler handler) {
305: _colHandler = handler;
306: } //-- setCollectionHandler
307:
308: /**
309: * Sets whether or not the type is immutable
310: *
311: * @param immutable a boolean that when true indicates the type
312: * is immutable
313: */
314: void setImmutable(boolean immutable) {
315: _immutable = immutable;
316: }
317:
318: }
|