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