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