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-2005 (C) Intalio, Inc. All Rights Reserved.
042: *
043: * $Id: GeneralizedFieldHandler.java 6216 2006-09-15 23:26:59Z rjoachim $
044: */package org.exolab.castor.mapping;
045:
046: import org.exolab.castor.mapping.loader.CollectionHandlers;
047: import java.util.Enumeration;
048:
049: /**
050: * An extended version of the FieldHandler interface which is
051: * used for making generic libraries of FieldHandlers which
052: * can be used for more than one field or class, but have
053: * similar conversion algorithms.
054: *
055: * @author <a href="kvisco-at-intalio.com">Keith Visco</a>
056: * @version $Revision: 6216 $ $Date: 2005-01-18 17:29:45 -0700 (Tue, 18 Jan 2005) $
057: * @see FieldDescriptor
058: * @see FieldHandler
059: */
060: public abstract class GeneralizedFieldHandler extends
061: AbstractFieldHandler {
062:
063: /**
064: * Error message when a null FieldHandler is encountered
065: */
066: private static final String NULL_HANDLER_ERR = "A call to #setFieldHandler (with a non-null value) must be "
067: + "made before calling this method.";
068:
069: /**
070: * The actual FieldHandler to delegate to
071: */
072: private FieldHandler _handler = null;
073:
074: /**
075: * The flag controlling automatic collection iteration
076: * during convertUponGet
077: */
078: private boolean _autoCollectionIteration = true;
079:
080: /**
081: * Creates a new default GeneralizedFieldHandler. This method
082: * should be called by all extending classes so that any
083: * important initialization code will be executed.
084: */
085: protected GeneralizedFieldHandler() {
086: super ();
087: //-- currently nothing to do, but initialization
088: //-- code may be needed in the future
089: } //-- GeneralizedFieldHandler
090:
091: /**
092: * This method is used to convert the value when the getValue method
093: * is called. The getValue method will obtain the actual field value
094: * from given 'parent' object. This convert method is then invoked
095: * with the field's value. The value returned from this
096: * method will be the actual value returned by getValue method.
097: *
098: * @param value the object value to convert after performing a get
099: * operation
100: * @return the converted value.
101: */
102: public abstract Object convertUponGet(Object value);
103:
104: /**
105: * This method is used to convert the value when the setValue method
106: * is called. The setValue method will call this method to obtain
107: * the converted value. The converted value will then be used as
108: * the value to set for the field.
109: *
110: * @param value the object value to convert before performing a set
111: * operation
112: * @return the converted value.
113: */
114: public abstract Object convertUponSet(Object value);
115:
116: /**
117: * Returns the class type for the field that this GeneralizedFieldHandler
118: * converts to and from. This should be the type that is used in the
119: * object model.
120: *
121: * @return the class type of of the field
122: */
123: public abstract Class getFieldType();
124:
125: /**
126: * Sets the FieldHandler that this FieldHander delegates to.
127: * A call to this method must be made with a non-null
128: * FieldHandler before this GeneralizedFieldHandler can be used.
129: *
130: * @param handler the FieldHandler to delegate to
131: */
132: public final void setFieldHandler(FieldHandler handler) {
133: _handler = handler;
134: } //-- setFieldHandler
135:
136: /**
137: * Sets whether or not this GeneralizedFieldHandler should automatically
138: * iterate over the collection returned by the target object and pass
139: * only the items (one by one) to the convertUponGet method.
140: *
141: * As of Castor 0.9.6 this is true by default.
142: *
143: * @param autoCollectionIteration a boolean that when true indicates
144: * that this GeneralizedFieldHandler should automatically iterate over
145: * a collection and pass only collection items to the convertUponGet
146: * method.
147: */
148: public void setCollectionIteration(boolean autoCollectionIteration) {
149: _autoCollectionIteration = autoCollectionIteration;
150: } //-- setCollectionIteration
151:
152: //-----------------------------------------------/
153: //- Methods inherited from AbstractFieldHandler -/
154: //-----------------------------------------------/
155:
156: /**
157: * Returns the value of the field from the object.
158: *
159: * @param object The object
160: * @return The value of the field
161: * @throws IllegalStateException The Java object has changed and
162: * is no longer supported by this handler, or the handler is not
163: * compatiable with the Java object
164: */
165: public final Object getValue(Object object)
166: throws IllegalStateException {
167: if (_handler == null) {
168: throw new IllegalStateException(NULL_HANDLER_ERR);
169: }
170:
171: Object value = _handler.getValue(object);
172: if ((_autoCollectionIteration) && (value != null)) {
173:
174: if (value instanceof java.util.Enumeration) {
175: return new GFHConverterEnumeration(this ,
176: (Enumeration) value);
177: }
178: //-- other collection type?
179: if (CollectionHandlers.hasHandler(value.getClass())) {
180: CollectionHandler colHandler = null;
181: try {
182: colHandler = CollectionHandlers.getHandler(value
183: .getClass());
184: } catch (MappingException mx) {
185: throw new IllegalStateException(mx.getMessage());
186: }
187: return new GFHConverterEnumeration(this , colHandler
188: .elements(value));
189: }
190: }
191:
192: return convertUponGet(value);
193: } //-- getValue
194:
195: /**
196: * Creates a new instance of the object described by this field.
197: *
198: * @param parent The object for which the field is created
199: * @return A new instance of the field's value
200: * @throws IllegalStateException This field is a simple type and
201: * cannot be instantiated
202: */
203: public Object newInstance(Object parent)
204: throws IllegalStateException {
205: if (_handler == null) {
206: throw new IllegalStateException(NULL_HANDLER_ERR);
207: }
208: return _handler.newInstance(parent);
209: }
210:
211: /**
212: * Creates a new instance of the object described by this field.
213: *
214: * @param parent The object for which the field is created
215: * @param args the set of constructor arguments
216: * @return A new instance of the field's value
217: * @throws IllegalStateException This field is a simple type and
218: * cannot be instantiated
219: */
220: public Object newInstance(Object parent, Object[] args)
221: throws IllegalStateException {
222: if (_handler instanceof ExtendedFieldHandler) {
223: return ((ExtendedFieldHandler) _handler).newInstance(
224: parent, args);
225: }
226:
227: //-- backward compatibility: ignore arguments
228: return newInstance(parent);
229: }
230:
231: /**
232: * Sets the value of the field to a default value.
233: * <p>
234: * Reference fields are set to null, primitive fields are set to
235: * their default value, collection fields are emptied of all
236: * elements.
237: *
238: * @param object The object
239: * @throws IllegalStateException The Java object has changed and
240: * is no longer supported by this handler, or the handler is not
241: * compatiable with the Java object
242: */
243: public final void resetValue(Object object)
244: throws IllegalStateException, IllegalArgumentException {
245: if (_handler == null) {
246: throw new IllegalStateException(NULL_HANDLER_ERR);
247: }
248: _handler.resetValue(object);
249: }
250:
251: /**
252: * Sets the value of the field on the object.
253: *
254: * @param object The object.
255: * @param value The new value.
256: * @throws IllegalStateException The Java object has changed and is no longer
257: * supported by this handler, or the handler is not compatiable with the
258: * Java object.
259: * @throws IllegalArgumentException The value passed is not of a supported type.
260: */
261: public final void setValue(Object object, Object value)
262: throws IllegalStateException, IllegalArgumentException {
263: if (_handler == null) {
264: throw new IllegalStateException(NULL_HANDLER_ERR);
265: }
266: _handler.setValue(object, convertUponSet(value));
267: }
268:
269: static class GFHConverterEnumeration implements Enumeration {
270:
271: Enumeration _enumeration = null;
272: GeneralizedFieldHandler _handler = null;
273:
274: GFHConverterEnumeration(GeneralizedFieldHandler handler,
275: Enumeration enumeration) {
276: _enumeration = enumeration;
277: _handler = handler;
278: }
279:
280: public boolean hasMoreElements() {
281: return _enumeration.hasMoreElements();
282: }
283:
284: public Object nextElement() {
285: Object value = _enumeration.nextElement();
286: return _handler.convertUponGet(value);
287: }
288:
289: }
290:
291: } //-- GeneralizedFieldHandler
|