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 2000-2004 (C) Intalio, Inc. All Rights Reserved. 32 * 33 * $Id$ 34 */ 35 package org.exolab.castor.xml.descriptors; 36 37 import java.sql.Timestamp; 38 import java.util.Date; 39 40 import org.exolab.castor.mapping.AccessMode; 41 import org.exolab.castor.mapping.ClassDescriptor; 42 import org.exolab.castor.mapping.FieldDescriptor; 43 import org.exolab.castor.xml.NodeType; 44 import org.exolab.castor.xml.TypeValidator; 45 import org.exolab.castor.xml.XMLFieldDescriptor; 46 import org.exolab.castor.xml.XMLFieldHandler; 47 import org.exolab.castor.xml.handlers.SQLTimestampFieldHandler; 48 import org.exolab.castor.xml.util.XMLFieldDescriptorImpl; 49 50 /** 51 * A ClassDescriptor for java.sql.Date. 52 * 53 * @author <a href="kvisco-at-intalio.com">Keith Visco</a> 54 * @version $Revision$ $Date: 2006-04-13 06:47:36 -0600 (Thu, 13 Apr 2006) $ 55 */ 56 public class SQLTimestampClassDescriptor extends BaseDescriptor { 57 58 /** Used for returning no attribute and no element fields. */ 59 private static final XMLFieldDescriptor[] NO_FIELDS = new XMLFieldDescriptor[0]; 60 /** The name of the XML element. */ 61 private static final String XML_NAME = "sql-timestamp"; 62 /** Our field descriptor for our "content". */ 63 private static final XMLFieldDescriptorImpl CONTENT_DESCRIPTOR; 64 /** Our field descriptor array. Lists the fields we describe. */ 65 private static final FieldDescriptor[] FIELDS; 66 /** Type validator to use to validate an instance of this type. */ 67 private static final TypeValidator VALIDATOR = null; 68 69 static { 70 CONTENT_DESCRIPTOR = 71 new XMLFieldDescriptorImpl(String.class, "content", "content", NodeType.Text); 72 73 CONTENT_DESCRIPTOR.setImmutable(true); 74 // -- setHandler 75 SQLTimestampFieldHandler handler = new SQLTimestampFieldHandler(); 76 handler.setFieldHandler(new XMLFieldHandler() { 77 78 /** 79 * {@inheritDoc} 80 */ 81 public Object getValue(final Object object) { 82 return object; 83 } 84 85 /** 86 * {@inheritDoc} 87 */ 88 public void setValue(final Object object, final Object value) { 89 if (value == null) { 90 return; 91 } 92 93 if (java.sql.Timestamp.class == object.getClass()) { 94 Class type = value.getClass(); 95 Timestamp target = (Timestamp) object; 96 if (Timestamp.class.isAssignableFrom(type)) { 97 Timestamp temp = (Timestamp) value; 98 target.setTime(temp.getTime()); 99 target.setNanos(temp.getNanos()); 100 } else if (java.util.Date.class.isAssignableFrom(type)) { 101 target.setTime(((Date) value).getTime()); 102 } 103 } 104 } 105 106 /** 107 * {@inheritDoc} 108 */ 109 public Object newInstance(final Object parent) { 110 return new java.sql.Timestamp(0); 111 } 112 }); 113 114 CONTENT_DESCRIPTOR.setHandler(handler); 115 116 FIELDS = new FieldDescriptor[1]; 117 FIELDS[0] = CONTENT_DESCRIPTOR; 118 } 119 120 // ----------------/ 121 // - Constructors -/ 122 // ----------------/ 123 124 /** 125 * No-arg constructor. 126 */ 127 public SQLTimestampClassDescriptor() { 128 super(); 129 } // -- DateDescriptor 130 131 // ------------------/ 132 // - Public Methods -/ 133 // ------------------/ 134 135 /** 136 * Returns the set of XMLFieldDescriptors for all members that should be marshaled as XML 137 * attributes. 138 * 139 * @return an array of XMLFieldDescriptors for all members that should be marshaled as XML 140 * attributes. 141 */ 142 public XMLFieldDescriptor[] getAttributeDescriptors() { 143 return NO_FIELDS; 144 } // getAttributeDescriptors 145 146 /** 147 * Returns the XMLFieldDescriptor for the member that should be marshaled as text content. 148 * 149 * @return the XMLFieldDescriptor for the member that should be marshaled as text content. 150 */ 151 public XMLFieldDescriptor getContentDescriptor() { 152 return CONTENT_DESCRIPTOR; 153 } // getContentDescriptor 154 155 /** 156 * Returns the set of XMLFieldDescriptors for all members that should be marshaled as XML 157 * elements. 158 * 159 * @return an array of XMLFieldDescriptors for all members that should be marshaled as XML 160 * elements. 161 */ 162 public XMLFieldDescriptor[] getElementDescriptors() { 163 return NO_FIELDS; 164 } // getElementDescriptors 165 166 /** 167 * Returns the XML field descriptor matching the given xml name and nodeType. If NodeType is null, 168 * then either an AttributeDescriptor, or ElementDescriptor may be returned. Null is returned if 169 * no matching descriptor is available. 170 * 171 * @param name the xml name to match against 172 * @param namespace the namespace uri 173 * @param nodeType the NodeType to match against, or null if the node type is not known. 174 * @return the matching descriptor, or null if no matching descriptor is available. 175 */ 176 public XMLFieldDescriptor getFieldDescriptor(final String name, final String namespace, 177 final NodeType nodeType) { 178 return null; 179 } // -- getFieldDescriptor 180 181 /** 182 * @return the namespace prefix to use when marshaling as XML. 183 */ 184 public String getNameSpacePrefix() { 185 return null; 186 } // -- getNameSpacePrefix 187 188 /** 189 * @return the namespace URI used when marshaling and unmarshaling as XML. 190 */ 191 public String getNameSpaceURI() { 192 return null; 193 } // -- getNameSpaceURI 194 195 /** 196 * Returns a specific validator for the class described by this ClassDescriptor. A null value may 197 * be returned if no specific validator exists. 198 * 199 * @return the type validator for the class described by this ClassDescriptor. 200 */ 201 public TypeValidator getValidator() { 202 return VALIDATOR; 203 } // -- getValidator 204 205 /** 206 * Returns the XML Name for the Class being described. 207 * 208 * @return the XML name. 209 */ 210 public String getXMLName() { 211 return XML_NAME; 212 } // -- getXMLName 213 214 /** 215 * Returns the String representation of this XMLClassDescriptor. 216 * 217 * @return the String representation of this XMLClassDescriptor. 218 */ 219 public String toString() { 220 return super.toString() + "; descriptor for class: java.sql.Timestamp; xml name: " + XML_NAME; 221 } // -- toString 222 223 // -------------------------------------/ 224 // - Implementation of ClassDescriptor -/ 225 // -------------------------------------/ 226 227 /** 228 * Returns the Java class represented by this descriptor. 229 * 230 * @return The Java class 231 */ 232 public Class getJavaClass() { 233 return java.sql.Timestamp.class; 234 } // -- getJavaClass 235 236 /** 237 * Returns a list of fields represented by this descriptor. 238 * 239 * @return A list of fields 240 */ 241 public FieldDescriptor[] getFields() { 242 return FIELDS; 243 } // -- getFields 244 245 /** 246 * Returns the class descriptor of the class extended by this class. 247 * 248 * @return The extended class descriptor 249 */ 250 public ClassDescriptor getExtends() { 251 return null; 252 } // -- getExtends 253 254 /** 255 * Returns the identity field, null if this class has no identity. 256 * 257 * @return The identity field 258 */ 259 public FieldDescriptor getIdentity() { 260 return null; 261 } // -- getIdentity 262 263 /** 264 * Returns the access mode specified for this class. 265 * 266 * @return The access mode 267 */ 268 public AccessMode getAccessMode() { 269 return null; 270 } // -- getAccessMode 271 272 } // -- class: SQLTimestampClassDescriptor