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 (C) Intalio, Inc. All Rights Reserved.
32   *
33   * $Id$ Date Author Changes 11/01/2000 Arnaud Blandin Created
34   */
35  package org.exolab.castor.types;
36  
37  import java.text.ParseException;
38  import java.util.SimpleTimeZone;
39  import java.util.TimeZone;
40  import java.text.SimpleDateFormat;
41  
42  /**
43   * <p>
44   * Describe an XML schema Year.
45   * <p>
46   * The date type is derived from time period by setting up the facet :
47   * <ul>
48   * <li>duration to "P1Y"</li>
49   * </ul>
50   * <p>
51   * Note: This datatype is not included in any recommendation. It was introduced in
52   * http://www.w3.org/TR/2000/WD-xmlschema-2-20000407/ and was last in
53   * http://www.w3.org/TR/2000/CR-xmlschema-2-20001024/ and was removed by
54   * http://www.w3.org/TR/2001/PR-xmlschema-2-20010316/. It was not in the final approved
55   * recommendation: http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/
56   *
57   * @author <a href="mailto:blandin@intalio.com">Arnaud Blandin</a>
58   * @version $Revision$
59   * @deprecated since Castor 1.0.6 since this type is not in any recommendation.
60   */
61  public class Year extends TimePeriod {
62    /** SerialVersionUID */
63    private static final long serialVersionUID = 8485456751196062574L;
64  
65    /** Set to true and recompile to include debugging code in class. */
66    private static final boolean DEBUG = false;
67  
68    /** The month format used by the toDate() method */
69    private static final String YEAR_FORMAT = "yyyy";
70  
71    public Year() {
72      super("P1Y");
73      // we need to set the time zone to the computer local time zone
74      // if we want to use the toDate() method.
75      int temp = TimeZone.getDefault().getRawOffset();
76      if (temp < 0) {
77        temp = -temp;
78        try {
79          this.setZoneNegative();
80        } catch (UnsupportedOperationException e) {
81        }
82      }
83      short zhour = (short) (temp / (60 * 60 * 1000));
84      temp = temp % (60 * 60 * 1000);
85      short zmin = (short) (temp / (60 * 1000));
86      try {
87        this.setZone(zhour, zmin);
88      } catch (UnsupportedOperationException e) {
89      }
90    }
91  
92  
93    /* Disallow the access to set month method */
94    public void setMonth(short month) throws UnsupportedOperationException {
95      String err = "In a Year : the month field must not be changed";
96      throw new UnsupportedOperationException(err);
97    }
98  
99    /* Disallow the access to set day method */
100   public void setDay(short day) throws UnsupportedOperationException {
101     String err = "In a Year : the day field must not be changed";
102     throw new UnsupportedOperationException(err);
103   }
104 
105   /* Disallow the access to set time methods */
106   public void setHour(short hour) throws UnsupportedOperationException {
107     String err = "In a Year : the hour field must not be changed";
108     throw new UnsupportedOperationException(err);
109   }
110 
111   public void setMinute(short minute) throws UnsupportedOperationException {
112     String err = "In a Year : the minute field must not be changed";
113     throw new UnsupportedOperationException(err);
114   }
115 
116   public void setSecond(short second, short millsecond) throws UnsupportedOperationException {
117     String err = "In a Year : the second fields must not be changed";
118     throw new UnsupportedOperationException(err);
119   }
120 
121   public void setZone(short hour, short minute) throws UnsupportedOperationException {
122     String err = "In a Year : the time zone fields must not be changed";
123     throw new UnsupportedOperationException(err);
124   }
125 
126   public void setZoneNegative() throws UnsupportedOperationException {
127     String err = "In a Year : the time zone fields must not be changed";
128     throw new UnsupportedOperationException(err);
129   }
130 
131   /**
132    * convert this Year to a string The format is defined by W3C XML Schema draft and ISO8601 i.e
133    * (+|-)CCYY
134    * 
135    * @return a string representing this Month
136    */
137   public String toString() {
138 
139     StringBuilder result = new StringBuilder();
140     result.append(this.getCentury());
141     if (result.length() == 1)
142       result.insert(0, 0);
143 
144     if ((this.getYear() / 10) == 0)
145       result.append(0);
146     result.append(this.getYear());
147 
148     if (isNegative())
149       result.insert(0, '-');
150 
151     return result.toString();
152 
153   }// toString
154 
155   /**
156    * parse a String and convert it into a java.lang.Object
157    * 
158    * @param str the string to parse
159    * @return the java.lang.Object represented by the string
160    * @throws ParseException a parse exception is thrown if the string to parse does not follow the
161    *         rigth format (see the description of this class)
162    */
163   public static Object parse(String str) throws ParseException {
164     return parseYear(str);
165   }
166 
167   /**
168    * parse a String and convert it into a Year
169    * 
170    * @param str the string to parse
171    * @return the Year represented by the string
172    * @throws ParseException a parse exception is thrown if the string to parse does not follow the
173    *         rigth format (see the description of this class)
174    */
175   public static Year parseYear(String str) throws ParseException {
176 
177     Year result = new Year();
178 
179     if (str.startsWith("-")) {
180       result.setNegative();
181       str = str.substring(1);
182     }
183 
184     if (DEBUG) {
185       System.out.println("In parsing method of Year");
186       System.out.println("String to parse : " + str);
187       System.out.println("Negative ? " + result.isNegative());
188     }
189 
190 
191     if (str.length() != 4)
192       throw new ParseException(str + ": Bad XML Schema Year type format (CCYY)", 0);
193 
194     if (DEBUG) {
195       System.out.println("Processing century: " + str.substring(0, 2));
196     }
197     result.setCentury(Short.parseShort(str.substring(0, 2)));
198     if (DEBUG) {
199       System.out.println("Processing year: " + str.substring(2, 4));
200     }
201     try {
202       result.setYear(Short.parseShort(str.substring(2, 4)));
203     } catch (UnsupportedOperationException e) {
204     }
205     return result;
206   }// parse
207 
208   public java.util.Date toDate() throws ParseException {
209     java.util.Date date = null;
210     SimpleDateFormat df = new SimpleDateFormat(YEAR_FORMAT);
211     SimpleTimeZone timeZone = new SimpleTimeZone(0, "UTC");
212 
213     // Set the time zone
214     if (!isUTC()) {
215       int offset = 0;
216       offset = ((this.getZoneMinute() + this.getZoneHour() * 60) * 60 * 1000);
217       offset = isZoneNegative() ? -offset : offset;
218       timeZone.setRawOffset(offset);
219       timeZone.setID(TimeZone.getAvailableIDs(offset)[0]);
220     }
221     df.setTimeZone(timeZone);
222     date = df.parse(this.toString());
223     return date;
224   }// toDate()
225 
226 
227 } // --Year