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