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