1 /* 2 * Redistribution and use of this software and associated documentation ("Software"), with or 3 * without modification, are permitted provided that the following conditions are met: 4 * 5 * 1. Redistributions of source code must retain copyright statements and notices. Redistributions 6 * must also contain a copy of this document. 7 * 8 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of 9 * conditions and the following disclaimer in the documentation and/or other materials provided with 10 * the distribution. 11 * 12 * 3. The name "Exolab" must not be used to endorse or promote products derived from this Software 13 * without prior written permission of Intalio, Inc. For written permission, please contact 14 * info@exolab.org. 15 * 16 * 4. Products derived from this Software may not be called "Exolab" nor may "Exolab" appear in 17 * their names without prior written permission of Intalio, Inc. Exolab is a registered trademark of 18 * Intalio, Inc. 19 * 20 * 5. Due credit should be given to the Exolab Project (http://www.exolab.org/). 21 * 22 * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 24 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTALIO, INC. OR ITS 25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY 29 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 * 31 * Copyright 2004 (C) Intalio, Inc. All Rights Reserved. 32 * 33 * $Id$ 34 */ 35 package org.exolab.castor.xml.handlers; 36 37 import java.lang.reflect.Method; 38 import java.lang.reflect.Modifier; 39 40 import org.exolab.castor.mapping.FieldDescriptor; 41 import org.exolab.castor.mapping.FieldHandler; 42 import org.exolab.castor.mapping.GeneralizedFieldHandler; 43 import org.exolab.castor.mapping.MappingException; 44 45 /** 46 * An implementation of GeneralizedFieldHandler for classes that have a built-in valueOf(String) 47 * factory method, such as type-safe enumeration classes, java.sql.Timestamp, etc. 48 * 49 * @author <a href="kvisco@intalio.com">Keith Visco</a> 50 * @version $Revision$ $Date: 2006-04-14 04:14:43 -0600 (Fri, 14 Apr 2006) $ 51 * @see FieldDescriptor 52 * @see FieldHandler 53 */ 54 public class ValueOfFieldHandler extends GeneralizedFieldHandler { 55 56 /** The Class[] holding the argument types of the factory method. */ 57 private static final Class[] ARGS = new Class[] {String.class}; 58 /** The Factory Method name. */ 59 private static final String METHOD_NAME = "valueOf"; 60 61 /** The class type for this FieldHandler. */ 62 private final Class _type; 63 /** The <code>valueOf</code> method reference. */ 64 private final Method _valueOf; 65 66 /** 67 * Creates a new ValueOfFieldHandler. 68 * 69 * @param type the class type to create the FieldHandler for 70 * @throws MappingException if the valueOf method cannot be found 71 */ 72 public ValueOfFieldHandler(final Class type) throws MappingException { 73 super(); 74 if (type == null) { 75 throw new IllegalArgumentException("The argument 'type' must not be null."); 76 } 77 78 _type = type; 79 80 Method method = null; 81 try { 82 method = type.getMethod(METHOD_NAME, ARGS); 83 } catch (java.lang.NoSuchMethodException nsme) { 84 throw new MappingException(nsme); 85 } 86 if (!Modifier.isStatic(method.getModifiers())) { 87 String err = "No static method '" + METHOD_NAME + "' found in class: " + type.getName(); 88 throw new MappingException(err); 89 } 90 91 _valueOf = method; 92 } // -- ValueOfFieldHandler 93 94 /** 95 * This method is used to convert the value when the getValue method is called. The getValue 96 * method will obtain the actual field value from given 'parent' object. This convert method is 97 * then invoked with the field's value. The value returned from this method will be the actual 98 * value returned by getValue method. 99 * 100 * @param value the object value to convert after performing a get operation 101 * @return the converted value. 102 */ 103 public Object convertUponGet(final Object value) { 104 // -- no conversion necessary for marshaling 105 return value; 106 } // -- convertUponGet 107 108 /** 109 * This method is used to convert the value when the setValue method is called. The setValue 110 * method will call this method to obtain the converted value. The converted value will then be 111 * used as the value to set for the field. 112 * 113 * @param value the object value to convert before performing a set operation 114 * @return the converted value. 115 */ 116 public Object convertUponSet(final Object value) { 117 Object[] args = new Object[1]; 118 119 if (value != null) { 120 args[0] = value.toString(); 121 } 122 123 Object result = null; 124 125 try { 126 result = _valueOf.invoke(null, args); 127 } catch (java.lang.IllegalAccessException iae) { 128 throw new IllegalStateException(iae.getMessage()); 129 } catch (java.lang.reflect.InvocationTargetException ite) { 130 throw new IllegalStateException(ite.getMessage()); 131 } 132 133 return result; 134 } // -- convertUponSet; 135 136 /** 137 * Returns the class type for the field that this GeneralizedFieldHandler converts to and from. 138 * This should be the type that is used in the object model. 139 * 140 * @return the class type of of the field 141 */ 142 public Class getFieldType() { 143 return _type; 144 } // -- getFieldType 145 146 /** 147 * Creates a new instance of the object described by this field. 148 * 149 * @param parent The object for which the field is created 150 * @return A new instance of the field's value 151 * @throws IllegalStateException This field is a simple type and cannot be instantiated 152 */ 153 public Object newInstance(final Object parent) throws IllegalStateException { 154 return null; 155 } 156 157 } // -- ValueOfFieldHandler