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.sql.Timestamp; 38 import java.util.Date; 39 40 import org.exolab.castor.mapping.GeneralizedFieldHandler; 41 42 /** 43 * An implementation of GeneralizedFieldHandler for java.sql.Timestamp. 44 * 45 * @author <a href="kvisco@intalio.com">Keith Visco</a> 46 * @version $Revision$ $Date: 2006-04-14 04:14:43 -0600 (Fri, 14 Apr 2006) $ 47 * @see org.exolab.castor.mapping.FieldDescriptor 48 * @see org.exolab.castor.mapping.FieldHandler 49 */ 50 public class SQLTimestampFieldHandler extends GeneralizedFieldHandler { 51 /** Position of the literal 'T' character in an xsd:dateTime. */ 52 private static final int OFFSET_OF_LITERAL_T = 10; 53 54 /** 55 * Creates a new SQLTimestampFieldHandler. 56 */ 57 public SQLTimestampFieldHandler() { 58 super(); 59 } 60 61 /** 62 * This method is used to convert the value when the getValue method is called. The getValue 63 * method will obtain the actual field value from given 'parent' object. This convert method is 64 * then invoked with the field's value. The value returned from this method will be the actual 65 * value returned by getValue method. 66 * 67 * @param value the object value to convert after performing a get operation 68 * @return the converted value. 69 */ 70 public Object convertUponGet(final Object value) { 71 // -- no conversion necessary for marshaling 72 return value; 73 } // -- convertUponGet 74 75 /** 76 * This method is used to convert the value when the setValue method is called. The setValue 77 * method will call this method to obtain the converted value. The converted value will then be 78 * used as the value to set for the field. 79 * 80 * @param value the object value to convert before performing a set operation 81 * @return the converted value. 82 */ 83 public Object convertUponSet(final Object value) { 84 if (value == null) { 85 return null; 86 } 87 88 Timestamp timestamp = null; 89 90 // XML Schema compatibility: If 'T' exists at the correct spot, 91 // then we most likely have an XML Schema dateTime format. 92 String str = value.toString(); 93 if (str.indexOf('T') == OFFSET_OF_LITERAL_T) { 94 try { 95 Date date = DateFieldHandler.parse(str); 96 timestamp = new Timestamp(date.getTime()); 97 } catch (java.text.ParseException px) { 98 throw new IllegalStateException(px.getMessage()); 99 } 100 } else { 101 timestamp = Timestamp.valueOf(str); 102 } 103 return timestamp; 104 } // -- convertUponSet; 105 106 /** 107 * Returns the class type for the field that this GeneralizedFieldHandler converts to and from. 108 * This should be the type that is used in the object model. 109 * 110 * @return the class type of of the field 111 */ 112 public Class getFieldType() { 113 return java.sql.Timestamp.class; 114 } // -- getFieldType 115 116 /** 117 * Creates a new instance of the object described by this field. 118 * 119 * @param parent The object for which the field is created 120 * @return A new instance of the field's value 121 * @throws IllegalStateException This field is a simple type and cannot be instantiated 122 */ 123 public Object newInstance(final Object parent) throws IllegalStateException { 124 return new java.sql.Timestamp(0); 125 } 126 127 } // -- SQLTimestampFieldHandler