View Javadoc
1   /**
2    * Redistribution and use of this software and associated documentation
3    * ("Software"), with or without modification, are permitted provided
4    * that the following conditions are met:
5    *
6    * 1. Redistributions of source code must retain copyright
7    *    statements and notices.  Redistributions must also contain a
8    *    copy of this document.
9    *
10   * 2. Redistributions in binary form must reproduce the
11   *    above copyright notice, this list of conditions and the
12   *    following disclaimer in the documentation and/or other
13   *    materials provided with the distribution.
14   *
15   * 3. The name "Exolab" must not be used to endorse or promote
16   *    products derived from this Software without prior written
17   *    permission of Intalio, Inc.  For written permission,
18   *    please contact info@exolab.org.
19   *
20   * 4. Products derived from this Software may not be called "Exolab"
21   *    nor may "Exolab" appear in their names without prior written
22   *    permission of Intalio, Inc. Exolab is a registered
23   *    trademark of Intalio, Inc.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.exolab.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
29   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32   * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39   * OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * Copyright 1999-2005 (C) Intalio, Inc. All Rights Reserved.
42   *
43   * $Id$
44   */
45  
46  package org.exolab.castor.mapping.handlers;
47  
48  import org.exolab.castor.mapping.FieldHandler;
49  import org.exolab.castor.mapping.GeneralizedFieldHandler;
50  
51  import java.lang.reflect.Method;
52  import java.lang.reflect.Modifier;
53  
54  /**
55   * A specialized FieldHandler for the type-safe enum
56   * style classes.
57   * 
58   * Adapted from org.exolab.castor.xml.handlers.EnumFieldHandler
59   * which is used for the generated source code.
60   * 
61   * 
62   * @author <a href="kvisco-at-intalio.com">Keith Visco</a>
63   * @version $Revision$ $Date: 2006-04-13 06:47:36 -0600 (Thu, 13 Apr 2006) $
64   */
65  public class EnumFieldHandler extends GeneralizedFieldHandler {
66  
67  
68      /**
69       * Class type for the type-safe enum
70       */
71      private Class _enumType = null;
72  
73      /**
74       * The create method (eg: #valueOf(String))
75       */
76      private Method _createMethod  = null;
77  
78      /**
79       * The underlying FieldHandler
80       */
81      private FieldHandler _handler = null;
82  
83  
84      /**
85       * Creates a new EnumFieldHandler with the given type and
86       * FieldHandler
87       *
88       * @param enumType the Class type of the described field
89       * @param handler the FieldHandler to delegate to
90       */
91      public EnumFieldHandler(Class enumType, FieldHandler handler, Method createMethod) {
92  
93          if (enumType == null) {
94              String err = "The argument 'enumType' must not be null.";
95              throw new IllegalArgumentException(err);
96          }
97  
98          if (handler == null) {
99              String err = "The argument 'handler' must not be null.";
100             throw new IllegalArgumentException(err);
101         }
102 
103         if (createMethod == null) {
104             String err = "The argument 'createMethod' must not be null.";
105             throw new IllegalArgumentException(err);
106         }
107 
108         _handler = handler;
109 
110         //-- pass handler to superclass
111         setFieldHandler(handler);
112 
113         _enumType = enumType;
114         _createMethod = createMethod;
115 
116         int mods = createMethod.getModifiers();
117 
118         if (!Modifier.isStatic(mods)) {
119             String err = "The factory create method specified for " + enumType.getName()
120             + " must be static";
121             throw new IllegalArgumentException(err);
122         }
123 
124     } //-- EnumFieldHandler
125 
126     /**
127      * @see org.exolab.castor.mapping.GeneralizedFieldHandler#convertUponGet(java.lang.Object)
128      */
129     public Object convertUponGet(Object value)
130     {
131         //-- no conversion for getter
132         return value;
133 
134     } //-- getValue
135 
136     /**
137      * @see org.exolab.castor.mapping.GeneralizedFieldHandler#convertUponSet(java.lang.Object)
138      */
139     public Object convertUponSet(Object value)
140         throws java.lang.IllegalStateException
141     {
142         Object obj = null;
143         if (value != null) {
144             Object[] args = new String[1];
145             args[0] = value.toString();
146             try {
147                 obj = _createMethod.invoke(null, args);
148             }
149             catch(java.lang.reflect.InvocationTargetException ite) {
150                 Throwable toss = ite.getTargetException();
151                 throw new IllegalStateException(toss.toString());
152             }
153             catch(java.lang.IllegalAccessException iae) {
154                 throw new IllegalStateException(iae.toString());
155             }
156         }
157         return obj;
158 
159     } //-- setValue
160 
161 
162     /**
163      * @see org.exolab.castor.mapping.GeneralizedFieldHandler#getFieldType()
164      */
165     public Class getFieldType() {
166         return _enumType;
167     }
168 
169 
170     /**
171      * @see org.exolab.castor.mapping.FieldHandler#newInstance(java.lang.Object)
172      */
173     public Object newInstance( Object parent )
174         throws IllegalStateException
175     {
176         return "";
177     } //-- newInstance
178 
179     /**
180      * @see org.exolab.castor.mapping.ExtendedFieldHandler#newInstance(java.lang.Object,
181      *      java.lang.Object[])
182      */
183     public Object newInstance( Object parent, Object[] args )
184         throws IllegalStateException
185     {
186         return "";
187     }
188 
189 
190     /**
191      * Returns true if the given object is an XMLFieldHandler that
192      * is equivalent to the delegated handler. An equivalent XMLFieldHandler
193      * is an XMLFieldHandler that is an instances of the same class.
194      *
195      * @return true if the given object is an XMLFieldHandler that
196      * is equivalent to this one.
197     **/
198     public boolean equals(Object obj) {
199         if (obj == null) return false;
200         if (obj == this) return true;
201         if (!(obj instanceof FieldHandler)) return false;
202         return (_handler.getClass().isInstance(obj) ||
203                 getClass().isInstance(obj));
204     } //-- equals
205 
206 } //-- EnumFieldHandler
207 
208 
209