1 /* 2 * Redistribution and use of this software and associated documentation 3 * ("Software"), with or without modification, are permitted provided 4 * that the following conditions are met: 5 * 6 * 1. Redistributions of source code must retain copyright 7 * statements and notices. Redistributions must also contain a 8 * copy of this document. 9 * 10 * 2. Redistributions in binary form must reproduce the 11 * above copyright notice, this list of conditions and the 12 * following disclaimer in the documentation and/or other 13 * materials provided with the distribution. 14 * 15 * 3. The name "Exolab" must not be used to endorse or promote 16 * products derived from this Software without prior written 17 * permission of Intalio, Inc. For written permission, 18 * please contact info@exolab.org. 19 * 20 * 4. Products derived from this Software may not be called "Exolab" 21 * nor may "Exolab" appear in their names without prior written 22 * permission of Intalio, Inc. Exolab is a registered 23 * trademark of Intalio, Inc. 24 * 25 * 5. Due credit should be given to the Exolab Project 26 * (http://www.exolab.org/). 27 * 28 * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS 29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 32 * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 39 * OF THE POSSIBILITY OF SUCH DAMAGE. 40 * 41 * Copyright 2000 (C) Intalio, Inc. All Rights Reserved. 42 * 43 * $Id$ 44 */ 45 package org.exolab.castor.xml.descriptors; 46 47 import java.util.Date; 48 49 import org.exolab.castor.mapping.AccessMode; 50 import org.exolab.castor.mapping.ClassDescriptor; 51 import org.exolab.castor.mapping.FieldDescriptor; 52 import org.exolab.castor.xml.NodeType; 53 import org.exolab.castor.xml.TypeValidator; 54 import org.exolab.castor.xml.XMLFieldDescriptor; 55 import org.exolab.castor.xml.XMLFieldHandler; 56 import org.exolab.castor.xml.handlers.DateFieldHandler; 57 import org.exolab.castor.xml.util.XMLFieldDescriptorImpl; 58 59 /** 60 * A ClassDescriptor for java.util.Date. 61 * 62 * @author <a href="kvisco-at-intalio.com">Keith Visco</a> 63 * @version $Revision$ $Date: 2004-12-16 22:49:25 -0700 (Thu, 16 Dec 2004) $ 64 */ 65 public class DateClassDescriptor extends BaseDescriptor { 66 67 /** Used for returning no attribute and no element fields. */ 68 private static final XMLFieldDescriptor[] NO_FIELDS = new XMLFieldDescriptor[0]; 69 /** The name of the XML element. */ 70 private static final String XML_NAME = "date"; 71 /** Our field descriptor. */ 72 private static final XMLFieldDescriptorImpl CONTENT_DESCRIPTOR; 73 /** Our field descriptor array. Lists the fields we describe. */ 74 private static final FieldDescriptor[] FIELDS; 75 /** The TypeValidator to use for validation of the described class. */ 76 private static final TypeValidator VALIDATOR = null; 77 78 static { 79 CONTENT_DESCRIPTOR = new XMLFieldDescriptorImpl(String.class, 80 "content", "content", NodeType.Text); 81 82 CONTENT_DESCRIPTOR.setImmutable(true); 83 CONTENT_DESCRIPTOR.setHandler(new DateFieldHandler(new XMLFieldHandler() { 84 85 /** 86 * {@inheritDoc} 87 */ 88 public Object getValue(final Object object) throws IllegalStateException { 89 return object; 90 } 91 92 /** 93 * {@inheritDoc} 94 */ 95 public void setValue(final Object object, final Object value) 96 throws IllegalStateException, IllegalArgumentException { 97 if (object.getClass() == java.util.Date.class) { 98 Date target = (Date) object; 99 if (value.getClass() == java.util.Date.class) { 100 target.setTime(((Date) value).getTime()); 101 } 102 } 103 } 104 105 /** 106 * {@inheritDoc} 107 */ 108 public Object newInstance(final Object parent) { 109 return null; 110 } 111 })); 112 113 FIELDS = new FieldDescriptor[1]; 114 FIELDS[0] = CONTENT_DESCRIPTOR; 115 } 116 117 //----------------/ 118 //- Constructors -/ 119 //----------------/ 120 121 /** 122 * No-arg constructor. 123 */ 124 public DateClassDescriptor() { 125 super(); 126 } //-- DateDescriptor 127 128 //------------------/ 129 //- Public Methods -/ 130 //------------------/ 131 132 /** 133 * Returns the set of XMLFieldDescriptors for all members that should be 134 * marshaled as XML attributes. 135 * 136 * @return an array of XMLFieldDescriptors for all members that should be 137 * marshaled as XML attributes. 138 */ 139 public XMLFieldDescriptor[] getAttributeDescriptors() { 140 return NO_FIELDS; 141 } // getAttributeDescriptors 142 143 /** 144 * Returns the XMLFieldDescriptor for the member that should be marshaled 145 * as text content. 146 * 147 * @return the XMLFieldDescriptor for the member that should be marshaled 148 * as text content. 149 */ 150 public XMLFieldDescriptor getContentDescriptor() { 151 return CONTENT_DESCRIPTOR; 152 } // getContentDescriptor 153 154 /** 155 * Returns the set of XMLFieldDescriptors for all members that should be 156 * marshaled as XML elements. 157 * 158 * @return an array of XMLFieldDescriptors for all members that should be 159 * marshaled as XML elements. 160 */ 161 public XMLFieldDescriptor[] getElementDescriptors() { 162 return NO_FIELDS; 163 } // getElementDescriptors 164 165 /** 166 * Returns the XML field descriptor matching the given xml name and 167 * nodeType. If NodeType is null, then either an AttributeDescriptor, or 168 * ElementDescriptor may be returned. Null is returned if no matching 169 * descriptor is available. 170 * 171 * @param name the xml name to match against 172 * @param namespace the namespace of the element. This may be null. Note: A 173 * null namespace is not the same as the default namespace unless the 174 * default namespace is also null. 175 * @param nodeType the NodeType to match against, or null if the node type 176 * is not known. 177 * @return the matching descriptor, or null if no matching descriptor is 178 * available. 179 */ 180 public XMLFieldDescriptor getFieldDescriptor(final String name, 181 final String namespace, final NodeType nodeType) { 182 return null; 183 } //-- getFieldDescriptor 184 185 /** 186 * @return the namespace prefix to use when marshaling as XML. 187 */ 188 public String getNameSpacePrefix() { 189 return null; 190 } //-- getNameSpacePrefix 191 192 /** 193 * @return the namespace URI used when marshaling and unmarshaling as XML. 194 */ 195 public String getNameSpaceURI() { 196 return null; 197 } //-- getNameSpaceURI 198 199 /** 200 * Returns a specific validator for the class described by this 201 * ClassDescriptor. A null value may be returned if no specific validator 202 * exists. 203 * 204 * @return the type validator for the class described by this 205 * ClassDescriptor. 206 */ 207 public TypeValidator getValidator() { 208 return VALIDATOR; 209 } //-- getValidator 210 211 /** 212 * Returns the XML Name for the Class being described. 213 * 214 * @return the XML name. 215 */ 216 public String getXMLName() { 217 return XML_NAME; 218 } //-- getXMLName 219 220 /** 221 * Returns the String representation of this XMLClassDescriptor. 222 * @return the String representation of this XMLClassDescriptor. 223 */ 224 public String toString() { 225 return super.toString() + "; descriptor for class: Date" + "; xml name: " + XML_NAME; 226 } //-- toString 227 228 //-------------------------------------/ 229 //- Implementation of ClassDescriptor -/ 230 //-------------------------------------/ 231 232 /** 233 * Returns the Java class represented by this descriptor. 234 * 235 * @return The Java class 236 */ 237 public Class getJavaClass() { 238 return java.util.Date.class; 239 } //-- getJavaClass 240 241 /** 242 * Returns a list of fields represented by this descriptor. 243 * 244 * @return A list of fields 245 */ 246 public FieldDescriptor[] getFields() { 247 return FIELDS; 248 } //-- getFields 249 250 /** 251 * Returns the class descriptor of the class extended by this class. 252 * 253 * @return The extended class descriptor 254 */ 255 public ClassDescriptor getExtends() { 256 return null; 257 } //-- getExtends 258 259 /** 260 * Returns the identity field, null if this class has no identity. 261 * 262 * @return The identity field 263 */ 264 public FieldDescriptor getIdentity() { 265 return null; 266 } //-- getIdentity 267 268 /** 269 * Returns the access mode specified for this class. 270 * 271 * @return The access mode 272 */ 273 public AccessMode getAccessMode() { 274 return null; 275 } //-- getAccessMode 276 277 } //-- class: DateClassDescriptor