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