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