View Javadoc
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.Time;
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.SQLTimeFieldHandler;
51  import org.exolab.castor.xml.util.XMLFieldDescriptorImpl;
52  
53  /**
54   * A ClassDescriptor for java.sql.Time.
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 2006) $
58   */
59  public class SQLTimeClassDescriptor extends BaseDescriptor {
60  
61      /** Used for returning no attribute and no element fields. */
62      private static final XMLFieldDescriptor[]   NO_FIELDS  = new XMLFieldDescriptor[0];
63      /** The name of the XML element. */
64      private static final String                 XML_NAME   = "sql-time";
65      /** Our field descriptor for our "content". */
66      private static final XMLFieldDescriptorImpl CONTENT_DESCRIPTOR;
67      /** Our field descriptor array. Lists the fields we describe. */
68      private static final FieldDescriptor[]      FIELDS;
69      /** Type validator to use to validate an instance of this type. */
70      private static final TypeValidator          VALIDATOR  = null;
71  
72      static {
73          CONTENT_DESCRIPTOR = new XMLFieldDescriptorImpl(String.class,
74                  "content", "content", NodeType.Text);
75  
76          CONTENT_DESCRIPTOR.setImmutable(true);
77          // -- setHandler
78          SQLTimeFieldHandler sqlTimeHandler = new SQLTimeFieldHandler();
79  
80          XMLFieldHandler handler = 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.Time.class == object.getClass()) {
98                      Time target = (Time) object;
99                      if (java.util.Date.class.isAssignableFrom(value.getClass())) {
100                         target.setTime(((Date) value).getTime());
101                     }
102                 }
103             }
104 
105             /**
106              * {@inheritDoc}
107              */
108             public Object newInstance(final Object parent) {
109                 return new java.sql.Time(0);
110             }
111         };
112         sqlTimeHandler.setFieldHandler(handler);
113         CONTENT_DESCRIPTOR.setHandler(sqlTimeHandler);
114 
115         FIELDS = new FieldDescriptor[1];
116         FIELDS[0] = CONTENT_DESCRIPTOR;
117     }
118 
119     // ----------------/
120     // - Constructors -/
121     // ----------------/
122 
123     /**
124      * No-arg constructor.
125      */
126     public SQLTimeClassDescriptor() {
127         super();
128     } // -- DateDescriptor
129 
130     // ------------------/
131     // - Public Methods -/
132     // ------------------/
133 
134     /**
135      * Returns the set of XMLFieldDescriptors for all members that should be
136      * marshaled as XML attributes.
137      *
138      * @return an array of XMLFieldDescriptors for all members that should be
139      *         marshaled as XML attributes.
140      */
141     public XMLFieldDescriptor[] getAttributeDescriptors() {
142         return NO_FIELDS;
143     } // getAttributeDescriptors
144 
145     /**
146      * Returns the XMLFieldDescriptor for the member that should be marshaled
147      * as text content.
148      *
149      * @return the XMLFieldDescriptor for the member that should be marshaled
150      *         as text content.
151      */
152     public XMLFieldDescriptor getContentDescriptor() {
153         return CONTENT_DESCRIPTOR;
154     } // getContentDescriptor
155 
156     /**
157      * Returns the set of XMLFieldDescriptors for all members that should be
158      * marshaled as XML elements.
159      *
160      * @return an array of XMLFieldDescriptors for all members that should be
161      *         marshaled as XML elements.
162      */
163     public XMLFieldDescriptor[] getElementDescriptors() {
164         return NO_FIELDS;
165     } // getElementDescriptors
166 
167     /**
168      * Returns the XML field descriptor matching the given xml name and
169      * nodeType. If NodeType is null, then either an AttributeDescriptor, or
170      * ElementDescriptor may be returned. Null is returned if no matching
171      * descriptor is available.
172      *
173      * @param name the xml name to match against
174      * @param namespace the namespace uri
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, final String namespace,
181             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      *
223      * @return the String representation of this XMLClassDescriptor.
224      */
225     public String toString() {
226         return super.toString() + "; descriptor for class: java.sql.Time; xml name: " + XML_NAME;
227     } // -- toString
228 
229     // -------------------------------------/
230     // - Implementation of ClassDescriptor -/
231     // -------------------------------------/
232 
233     /**
234      * Returns the Java class represented by this descriptor.
235      *
236      * @return The Java class
237      */
238     public Class getJavaClass() {
239         return java.sql.Time.class;
240     } // -- getJavaClass
241 
242     /**
243      * Returns a list of fields represented by this descriptor.
244      *
245      * @return A list of fields
246      */
247     public FieldDescriptor[] getFields() {
248         return FIELDS;
249     } // -- getFields
250 
251     /**
252      * Returns the class descriptor of the class extended by this class.
253      *
254      * @return The extended class descriptor
255      */
256     public ClassDescriptor getExtends() {
257         return null;
258     } // -- getExtends
259 
260     /**
261      * Returns the identity field, null if this class has no identity.
262      *
263      * @return The identity field
264      */
265     public FieldDescriptor getIdentity() {
266         return null;
267     } // -- getIdentity
268 
269     /**
270      * Returns the access mode specified for this class.
271      *
272      * @return The access mode
273      */
274     public AccessMode getAccessMode() {
275         return null;
276     } // -- getAccessMode
277 
278 } //-- class: SQLTimeClassDescriptor