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: EnumFieldHandler.java 6238 2006-09-25 07:32:41Z wguttmn $
044: */package org.exolab.castor.mapping.handlers;
045:
046: import org.exolab.castor.mapping.FieldHandler;
047: import org.exolab.castor.mapping.GeneralizedFieldHandler;
048:
049: import java.lang.reflect.Method;
050: import java.lang.reflect.Modifier;
051:
052: /**
053: * A specialized FieldHandler for the type-safe enum
054: * style classes.
055: *
056: * Adapted from org.exolab.castor.xml.handlers.EnumFieldHandler
057: * which is used for the generated source code.
058: *
059: *
060: * @author <a href="kvisco-at-intalio.com">Keith Visco</a>
061: * @version $Revision: 6238 $ $Date: 2006-04-13 06:47:36 -0600 (Thu, 13 Apr 2006) $
062: */
063: public class EnumFieldHandler extends GeneralizedFieldHandler {
064:
065: /**
066: * Class type for the type-safe enum
067: */
068: private Class _enumType = null;
069:
070: /**
071: * The create method (eg: #valueOf(String))
072: */
073: private Method _createMethod = null;
074:
075: /**
076: * The underlying FieldHandler
077: */
078: private FieldHandler _handler = null;
079:
080: /**
081: * Creates a new EnumFieldHandler with the given type and
082: * FieldHandler
083: *
084: * @param enumType the Class type of the described field
085: * @param handler the FieldHandler to delegate to
086: */
087: public EnumFieldHandler(Class enumType, FieldHandler handler,
088: Method createMethod) {
089:
090: if (enumType == null) {
091: String err = "The argument 'enumType' must not be null.";
092: throw new IllegalArgumentException(err);
093: }
094:
095: if (handler == null) {
096: String err = "The argument 'handler' must not be null.";
097: throw new IllegalArgumentException(err);
098: }
099:
100: if (createMethod == null) {
101: String err = "The argument 'createMethod' must not be null.";
102: throw new IllegalArgumentException(err);
103: }
104:
105: _handler = handler;
106:
107: //-- pass handler to superclass
108: setFieldHandler(handler);
109:
110: _enumType = enumType;
111: _createMethod = createMethod;
112:
113: int mods = createMethod.getModifiers();
114:
115: if (!Modifier.isStatic(mods)) {
116: String err = "The factory create method specified for "
117: + enumType.getName() + " must be static";
118: throw new IllegalArgumentException(err);
119: }
120:
121: } //-- EnumFieldHandler
122:
123: /**
124: * @see org.exolab.castor.mapping.GeneralizedFieldHandler#convertUponGet(java.lang.Object)
125: */
126: public Object convertUponGet(Object value) {
127: //-- no conversion for getter
128: return value;
129:
130: } //-- getValue
131:
132: /**
133: * @see org.exolab.castor.mapping.GeneralizedFieldHandler#convertUponSet(java.lang.Object)
134: */
135: public Object convertUponSet(Object value)
136: throws java.lang.IllegalStateException {
137: Object[] args = new String[1];
138: Object obj = null;
139: if (value != null) {
140: args[0] = value.toString();
141: try {
142: obj = _createMethod.invoke(null, args);
143: } catch (java.lang.reflect.InvocationTargetException ite) {
144: Throwable toss = ite.getTargetException();
145: throw new IllegalStateException(toss.toString());
146: } catch (java.lang.IllegalAccessException iae) {
147: throw new IllegalStateException(iae.toString());
148: }
149: }
150: return obj;
151:
152: } //-- setValue
153:
154: /**
155: * @see org.exolab.castor.mapping.GeneralizedFieldHandler#getFieldType()
156: */
157: public Class getFieldType() {
158: return _enumType;
159: }
160:
161: /**
162: * @see org.exolab.castor.mapping.FieldHandler#newInstance(java.lang.Object)
163: */
164: public Object newInstance(Object parent)
165: throws IllegalStateException {
166: return "";
167: } //-- newInstance
168:
169: /**
170: * @see org.exolab.castor.mapping.ExtendedFieldHandler#newInstance(java.lang.Object,
171: * java.lang.Object[])
172: */
173: public Object newInstance(Object parent, Object[] args)
174: throws IllegalStateException {
175: return "";
176: }
177:
178: /**
179: * Returns true if the given object is an XMLFieldHandler that
180: * is equivalent to the delegated handler. An equivalent XMLFieldHandler
181: * is an XMLFieldHandler that is an instances of the same class.
182: *
183: * @return true if the given object is an XMLFieldHandler that
184: * is equivalent to this one.
185: **/
186: public boolean equals(Object obj) {
187: if (obj == null)
188: return false;
189: if (obj == this )
190: return true;
191: if (!(obj instanceof FieldHandler))
192: return false;
193: return (_handler.getClass().isInstance(obj) || getClass()
194: .isInstance(obj));
195: } //-- equals
196:
197: } //-- EnumFieldHandler
|