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 2004 (C) Intalio, Inc. All Rights Reserved.
42   *
43   * $Id$
44   */
45  package org.exolab.castor.xml.handlers;
46  
47  import java.lang.reflect.Method;
48  import java.lang.reflect.Modifier;
49  
50  import org.exolab.castor.mapping.FieldDescriptor;
51  import org.exolab.castor.mapping.FieldHandler;
52  import org.exolab.castor.mapping.GeneralizedFieldHandler;
53  import org.exolab.castor.mapping.MappingException;
54  
55  /**
56   * An implementation of GeneralizedFieldHandler for classes that have a built-in
57   * valueOf(String) factory method, such as type-safe enumeration classes,
58   * java.sql.Timestamp, etc.
59   *
60   * @author <a href="kvisco@intalio.com">Keith Visco</a>
61   * @version $Revision$ $Date: 2006-04-14 04:14:43 -0600 (Fri, 14 Apr 2006) $
62   * @see FieldDescriptor
63   * @see FieldHandler
64   */
65  public class ValueOfFieldHandler extends GeneralizedFieldHandler {
66  
67      /** The Class[] holding the argument types of the factory method. */
68      private static final Class[] ARGS = new Class[] {String.class};
69      /** The Factory Method name. */
70      private static final String METHOD_NAME = "valueOf";
71  
72      /** The class type for this FieldHandler. */
73      private final Class _type;
74      /** The <code>valueOf</code> method reference. */
75      private final Method _valueOf;
76  
77      /**
78       * Creates a new ValueOfFieldHandler.
79       *
80       * @param type the class type to create the FieldHandler for
81       * @throws MappingException if the valueOf method cannot be found
82       */
83      public ValueOfFieldHandler(final Class type) throws MappingException {
84          super();
85          if (type == null) {
86              throw new IllegalArgumentException("The argument 'type' must not be null.");
87          }
88  
89          _type = type;
90  
91          Method method = null;
92          try {
93              method = type.getMethod(METHOD_NAME, ARGS);
94          } catch (java.lang.NoSuchMethodException nsme) {
95              throw new MappingException(nsme);
96          }
97          if (!Modifier.isStatic(method.getModifiers())) {
98              String err = "No static method '" + METHOD_NAME + "' found in class: " + type.getName();
99              throw new MappingException(err);
100         }
101 
102         _valueOf = method;
103     } //-- ValueOfFieldHandler
104 
105     /**
106      * This method is used to convert the value when the getValue method is
107      * called. The getValue method will obtain the actual field value from given
108      * 'parent' object. This convert method is then invoked with the field's
109      * value. The value returned from this method will be the actual value
110      * returned by getValue method.
111      *
112      * @param value the object value to convert after performing a get operation
113      * @return the converted value.
114      */
115     public Object convertUponGet(final Object value) {
116         //-- no conversion necessary for marshaling
117         return value;
118     } //-- convertUponGet
119 
120     /**
121      * This method is used to convert the value when the setValue method is
122      * called. The setValue method will call this method to obtain the converted
123      * value. The converted value will then be used as the value to set for the
124      * field.
125      *
126      * @param value the object value to convert before performing a set
127      *        operation
128      * @return the converted value.
129      */
130     public Object convertUponSet(final Object value) {
131         Object[] args = new Object[1];
132 
133         if (value != null) {
134             args[0] = value.toString();
135         }
136 
137         Object result = null;
138 
139         try {
140             result = _valueOf.invoke(null, args);
141         } catch (java.lang.IllegalAccessException iae) {
142             throw new IllegalStateException(iae.getMessage());
143         } catch (java.lang.reflect.InvocationTargetException ite) {
144             throw new IllegalStateException(ite.getMessage());
145         }
146 
147         return result;
148     } //-- convertUponSet;
149 
150     /**
151      * Returns the class type for the field that this GeneralizedFieldHandler
152      * converts to and from. This should be the type that is used in the object
153      * model.
154      *
155      * @return the class type of of the field
156      */
157     public Class getFieldType() {
158         return _type;
159     } //-- getFieldType
160 
161     /**
162      * Creates a new instance of the object described by this field.
163      *
164      * @param parent The object for which the field is created
165      * @return A new instance of the field's value
166      * @throws IllegalStateException This field is a simple type and cannot be
167      *         instantiated
168      */
169     public Object newInstance(final Object parent) throws IllegalStateException {
170         return null;
171     }
172 
173 } // -- ValueOfFieldHandler