View Javadoc
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-2003 (C) Intalio, Inc. All Rights Reserved.
32   *
33   * $Id$
34   */
35  package org.exolab.castor.types;
36  
37  import org.exolab.castor.mapping.FieldDescriptor;
38  import org.exolab.castor.mapping.ValidityException;
39  import org.exolab.castor.xml.NodeType;
40  import org.exolab.castor.xml.XMLFieldDescriptor;
41  import org.exolab.castor.xml.XMLFieldHandler;
42  import org.exolab.castor.xml.util.XMLFieldDescriptorImpl;
43  
44  /**
45   * The Date Descriptor.
46   *
47   * @author <a href="blandin@intalio.com">Arnaud Blandin</a>
48   * @author <a href="kvisco@intalio.com">Keith Visco</a>
49   * @version $Revision$ $Date: 2006-04-13 06:47:36 -0600 (Thu, 13 Apr 2006) $
50   * @see TimeDurationDescriptor
51   */
52  public class DateDescriptor extends BaseDescriptor {
53  
54    /** The name of the XML element. */
55    private static final String XML_NAME = "date";
56    /** Our field descriptor. */
57    private static final XMLFieldDescriptorImpl CONTENT_DESCRIPTOR;
58    /** Our field descriptor array. Lists the fields we describe. */
59    private static final FieldDescriptor[] FIELDS;
60  
61    static {
62      CONTENT_DESCRIPTOR =
63          new XMLFieldDescriptorImpl(String.class, "content", "content", NodeType.Text);
64      CONTENT_DESCRIPTOR.setHandler(new DateDescriptor().new DateFieldHandler());
65      FIELDS = new FieldDescriptor[1];
66      FIELDS[0] = CONTENT_DESCRIPTOR;
67    }
68  
69    // ----------------/
70    // - Constructors -/
71    // ----------------/
72  
73    public DateDescriptor() {
74      super(XML_NAME, Date.class);
75    } // -- DateDescriptor
76  
77    // ------------------/
78    // - Public Methods -/
79    // ------------------/
80  
81    /**
82     * Returns the XMLFieldDescriptor for the member that should be marshalled as text content.
83     *
84     * @return the XMLFieldDescriptor for the member that should be marshalled as text content.
85     */
86    public XMLFieldDescriptor getContentDescriptor() {
87      return CONTENT_DESCRIPTOR;
88    } // getContentDescriptor
89  
90    /**
91     * Returns a list of fields represented by this descriptor.
92     *
93     * @return A list of fields
94     */
95    public FieldDescriptor[] getFields() {
96      return FIELDS;
97    } // -- getFields
98  
99    /**
100    * A specialized FieldHandler for the XML Schema TimeDuration related types
101    *
102    * @author <a href="blandin@intalio.com">Arnaud Blandin</a>
103    * @version $Revision$ $Date: $
104    */
105   class DateFieldHandler extends XMLFieldHandler {
106 
107     // ----------------/
108     // - Constructors -/
109     // ----------------/
110 
111     /**
112      * Creates a new TimeFieldHandler
113      */
114     public DateFieldHandler() {
115       super();
116     } // -- TimeFieldHandler
117 
118     // ------------------/
119     // - Public Methods -/
120     // ------------------/
121 
122     /**
123      * Returns the value of the field associated with this descriptor from the given target object.
124      *
125      * @param target the object to get the value from
126      * @return the value of the field associated with this descriptor from the given target object.
127      * @throws IllegalStateException The Java object has changed and is no longer supported by this
128      *         handler, or the handler is not compatiable with the Java object
129      */
130     public Object getValue(Object target) throws java.lang.IllegalStateException {
131       // TODO: check for TimeDuration class -- add later
132       Date date = (Date) target;
133 
134       return date.toString();
135     } // -- getValue
136 
137     /**
138      * Sets the value of the field associated with this descriptor.
139      *
140      * @param target the object in which to set the value
141      * @param value the value of the field
142      * @throws IllegalStateException The Java object has changed and is no longer supported by this
143      *         handler, or the handler is not compatiable with the Java object
144      */
145     public void setValue(Object target, Object value) throws java.lang.IllegalStateException {
146       if (!(target instanceof Date)) {
147         String err =
148             "DateDescriptor#setValue: expected Date, received instead: " + target.getClass();
149         throw new IllegalStateException(err);
150       }
151 
152       Date dateTarget = (Date) target;
153 
154       if (value == null) {
155         String err = "DateDescriptor#setValue: null value.";
156         throw new IllegalStateException(err);
157       }
158 
159       // -- update current instance of time with new time
160       try {
161         Date temp = Date.parseDate(value.toString());
162         dateTarget.setCentury(temp.getCentury());
163         dateTarget.setYear(temp.getYear());
164         dateTarget.setMonth(temp.getMonth());
165         dateTarget.setDay(temp.getDay());
166         if (temp.isUTC()) {
167           dateTarget.setUTC();
168           dateTarget.setZone(temp.getZoneHour(), temp.getZoneMinute());
169           dateTarget.setZoneNegative(temp.isZoneNegative());
170         }
171       } catch (java.text.ParseException ex) {
172         String err = "DateDescriptor#setValue: wrong value\n" + ex.getMessage();
173         throw new IllegalStateException(err);
174       }
175     } // -- setValue
176 
177     public void resetValue(Object target) throws java.lang.IllegalStateException {
178       // nothing to do?
179     }
180 
181     /**
182      * Checks the field validity. Returns successfully if the field can be stored, is valid, etc,
183      * throws an exception otherwise.
184      *
185      * @param object The object
186      * @throws ValidityException The field is invalid, is required and null, or any other validity
187      *         violation
188      * @throws IllegalStateException The Java object has changed and is no longer supported by this
189      *         handler, or the handler is not compatiable with the Java object
190      */
191     public void checkValidity(Object object) throws ValidityException, IllegalStateException {
192       // nothing to do?
193     } // -- checkValidity
194 
195     /**
196      * Creates a new instance of the object described by this field.
197      *
198      * @param parent The object for which the field is created
199      * @return A new instance of the field's value
200      * @throws IllegalStateException This field is a simple type and cannot be instantiated
201      */
202     public Object newInstance(Object parent) throws IllegalStateException {
203       return new Date();
204     } // -- newInstance
205 
206   } // -- DateFieldHandler
207 
208 } // -- DateDescriptor