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.Time; 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.SQLTimeFieldHandler; 48 import org.exolab.castor.xml.util.XMLFieldDescriptorImpl; 49 50 /** 51 * A ClassDescriptor for java.sql.Time. 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 SQLTimeClassDescriptor 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-time"; 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 SQLTimeFieldHandler sqlTimeHandler = new SQLTimeFieldHandler(); 76 77 XMLFieldHandler handler = new XMLFieldHandler() { 78 79 /** 80 * {@inheritDoc} 81 */ 82 public Object getValue(final Object object) { 83 return object; 84 } 85 86 /** 87 * {@inheritDoc} 88 */ 89 public void setValue(final Object object, final Object value) { 90 if (value == null) { 91 return; 92 } 93 94 if (java.sql.Time.class == object.getClass()) { 95 Time target = (Time) object; 96 if (java.util.Date.class.isAssignableFrom(value.getClass())) { 97 target.setTime(((Date) value).getTime()); 98 } 99 } 100 } 101 102 /** 103 * {@inheritDoc} 104 */ 105 public Object newInstance(final Object parent) { 106 return new java.sql.Time(0); 107 } 108 }; 109 sqlTimeHandler.setFieldHandler(handler); 110 CONTENT_DESCRIPTOR.setHandler(sqlTimeHandler); 111 112 FIELDS = new FieldDescriptor[1]; 113 FIELDS[0] = CONTENT_DESCRIPTOR; 114 } 115 116 // ----------------/ 117 // - Constructors -/ 118 // ----------------/ 119 120 /** 121 * No-arg constructor. 122 */ 123 public SQLTimeClassDescriptor() { 124 super(); 125 } // -- DateDescriptor 126 127 // ------------------/ 128 // - Public Methods -/ 129 // ------------------/ 130 131 /** 132 * Returns the set of XMLFieldDescriptors for all members that should be marshaled as XML 133 * attributes. 134 * 135 * @return an array of XMLFieldDescriptors for all members that should be marshaled as XML 136 * attributes. 137 */ 138 public XMLFieldDescriptor[] getAttributeDescriptors() { 139 return NO_FIELDS; 140 } // getAttributeDescriptors 141 142 /** 143 * Returns the XMLFieldDescriptor for the member that should be marshaled as text content. 144 * 145 * @return the XMLFieldDescriptor for the member that should be marshaled as text content. 146 */ 147 public XMLFieldDescriptor getContentDescriptor() { 148 return CONTENT_DESCRIPTOR; 149 } // getContentDescriptor 150 151 /** 152 * Returns the set of XMLFieldDescriptors for all members that should be marshaled as XML 153 * elements. 154 * 155 * @return an array of XMLFieldDescriptors for all members that should be marshaled as XML 156 * elements. 157 */ 158 public XMLFieldDescriptor[] getElementDescriptors() { 159 return NO_FIELDS; 160 } // getElementDescriptors 161 162 /** 163 * Returns the XML field descriptor matching the given xml name and nodeType. If NodeType is null, 164 * then either an AttributeDescriptor, or ElementDescriptor may be returned. Null is returned if 165 * no matching descriptor is available. 166 * 167 * @param name the xml name to match against 168 * @param namespace the namespace uri 169 * @param nodeType the NodeType to match against, or null if the node type is not known. 170 * @return the matching descriptor, or null if no matching descriptor is available. 171 */ 172 public XMLFieldDescriptor getFieldDescriptor(final String name, final String namespace, 173 final NodeType nodeType) { 174 return null; 175 } // -- getFieldDescriptor 176 177 /** 178 * @return the namespace prefix to use when marshaling as XML. 179 */ 180 public String getNameSpacePrefix() { 181 return null; 182 } // -- getNameSpacePrefix 183 184 /** 185 * @return the namespace URI used when marshaling and unmarshaling as XML. 186 */ 187 public String getNameSpaceURI() { 188 return null; 189 } // -- getNameSpaceURI 190 191 /** 192 * Returns a specific validator for the class described by this ClassDescriptor. A null value may 193 * be returned if no specific validator exists. 194 * 195 * @return the type validator for the class described by this ClassDescriptor. 196 */ 197 public TypeValidator getValidator() { 198 return VALIDATOR; 199 } // -- getValidator 200 201 /** 202 * Returns the XML Name for the Class being described. 203 * 204 * @return the XML name. 205 */ 206 public String getXMLName() { 207 return XML_NAME; 208 } // -- getXMLName 209 210 /** 211 * Returns the String representation of this XMLClassDescriptor. 212 * 213 * @return the String representation of this XMLClassDescriptor. 214 */ 215 public String toString() { 216 return super.toString() + "; descriptor for class: java.sql.Time; xml name: " + XML_NAME; 217 } // -- toString 218 219 // -------------------------------------/ 220 // - Implementation of ClassDescriptor -/ 221 // -------------------------------------/ 222 223 /** 224 * Returns the Java class represented by this descriptor. 225 * 226 * @return The Java class 227 */ 228 public Class getJavaClass() { 229 return java.sql.Time.class; 230 } // -- getJavaClass 231 232 /** 233 * Returns a list of fields represented by this descriptor. 234 * 235 * @return A list of fields 236 */ 237 public FieldDescriptor[] getFields() { 238 return FIELDS; 239 } // -- getFields 240 241 /** 242 * Returns the class descriptor of the class extended by this class. 243 * 244 * @return The extended class descriptor 245 */ 246 public ClassDescriptor getExtends() { 247 return null; 248 } // -- getExtends 249 250 /** 251 * Returns the identity field, null if this class has no identity. 252 * 253 * @return The identity field 254 */ 255 public FieldDescriptor getIdentity() { 256 return null; 257 } // -- getIdentity 258 259 /** 260 * Returns the access mode specified for this class. 261 * 262 * @return The access mode 263 */ 264 public AccessMode getAccessMode() { 265 return null; 266 } // -- getAccessMode 267 268 } // -- class: SQLTimeClassDescriptor