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 12/05/2000 Arnaud Blandin Added the support for
34   * OperationNotSupportedException 11/01/2000 Arnaud Blandin Created
35   */
36  package org.exolab.castor.types;
37  
38  import java.text.ParseException;
39  import java.util.SimpleTimeZone;
40  import java.util.TimeZone;
41  import java.text.SimpleDateFormat;
42  
43  /**
44   * Describe an XML schema Century
45   * <p>
46   * The date type is derived from time period by setting up the facet:
47   * <ul>
48   * <li>duration to "P100Y"</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 Century extends TimePeriod {
62  
63    /** SerialVersionUID */
64    private static final long serialVersionUID = 88787876938390034L;
65    /** The month format used by the toDate() method */
66    private static final String YEAR_FORMAT = "yyyy";
67  
68    /** Set to true and recompile to include debugging code in class. */
69    private static final boolean DEBUG = false;
70  
71    public Century() {
72      super("P100Y");
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          // we are sure that we are dealing with a Century type
82          // so we can never reach that point
83        }
84      }
85  
86      try {
87        short zhour = (short) (temp / (60 * 60 * 1000));
88        temp = temp % (60 * 60 * 1000);
89        short zmin = (short) (temp / (60 * 1000));
90        super.setZone(zhour, zmin);
91      } catch (UnsupportedOperationException e) {
92        // we are sure that we are dealing with a Century type
93        // so we can never reach that point
94      }
95    }
96  
97    /**
98     * convert this Year to a string The format is defined by W3C XML Schema draft and ISO8601 i.e
99     * (+|-)CC
100    * 
101    * @return a string representing this Century
102    */
103   public String toString() {
104     StringBuilder result = new StringBuilder(String.valueOf(this.getCentury()));
105 
106     if (result.length() == 1) {
107       result.insert(0, 0);
108     }
109 
110     if (isNegative()) {
111       result.insert(0, '-');
112     }
113     return result.toString();
114   }// toString
115 
116   /**
117    * parse a String and convert it into a java.lang.Object
118    * 
119    * @param str the string to parse
120    * @return the java.lang.Object represented by the string
121    * @throws ParseException a parse exception is thrown if the string to parse does not follow the
122    *         rigth format (see the description of this class)
123    */
124   public static Object parse(String str) throws ParseException {
125     return parseCentury(str);
126   }
127 
128   /**
129    * parse a String and convert it into a Century
130    * 
131    * @param str the string to parse
132    * @return the Century represented by the string
133    * @throws ParseException a parse exception is thrown if the string to parse does not follow the
134    *         rigth format (see the description of this class)
135    */
136   public static Century parseCentury(String str) throws ParseException {
137     Century result = new Century();
138 
139     if (str.startsWith("-")) {
140       result.setNegative();
141       str = str.substring(1);
142     }
143 
144     if (DEBUG) {
145       System.out.println("In parsing method of Century");
146       System.out.println("String to parse : " + str);
147       System.out.println("Negative ? " + result.isNegative());
148     }
149 
150     if (str.length() != 2) {
151       throw new ParseException(str + ": Bad XML Schema Century type format (CC)", 0);
152     }
153 
154     if (DEBUG) {
155       System.out.println("Processing century: " + str.substring(0, 2));
156     }
157     result.setCentury(Short.parseShort(str));
158 
159     return result;
160   } // parse
161 
162   public java.util.Date toDate() throws ParseException {
163     java.util.Date date = null;
164     SimpleDateFormat df = new SimpleDateFormat(YEAR_FORMAT);
165     SimpleTimeZone timeZone = new SimpleTimeZone(0, "UTC");
166 
167     // Set the time zone
168     if (!isUTC()) {
169       int offset = 0;
170       offset = ((this.getZoneMinute() + this.getZoneHour() * 60) * 60 * 1000);
171       offset = isZoneNegative() ? -offset : offset;
172       timeZone.setRawOffset(offset);
173       timeZone.setID(TimeZone.getAvailableIDs(offset)[0]);
174     }
175     df.setTimeZone(timeZone);
176     date = df.parse(this.toString());
177     return date;
178   } // toDate()
179 
180   /////////////////////////// DISALLOWED METHODS ///////////////////////////
181 
182   public void setYear(short year) throws UnsupportedOperationException {
183     String err = "In a Century : the year field must not be changed";
184     throw new UnsupportedOperationException(err);
185   }
186 
187   public void setMonth(short month) throws UnsupportedOperationException {
188     String err = "In a Century : the month field must not be changed";
189     throw new UnsupportedOperationException(err);
190   }
191 
192   public void setDay(short day) throws UnsupportedOperationException {
193     String err = "In a Century : the day field must not be changed";
194     throw new UnsupportedOperationException(err);
195   }
196 
197   public void setHour(short hour) throws UnsupportedOperationException {
198     String err = "In a Century : the hour field must not be changed";
199     throw new UnsupportedOperationException(err);
200   }
201 
202   public void setMinute(short minute) throws UnsupportedOperationException {
203     String err = "In a Century : the minute field must not be changed";
204     throw new UnsupportedOperationException(err);
205   }
206 
207   public void setSecond(short second, short millsecond) throws UnsupportedOperationException {
208     String err = "In a Century : the second field must not be changed";
209     throw new UnsupportedOperationException(err);
210   }
211 
212   public void setZone(short hour, short minute) throws UnsupportedOperationException {
213     String err = "In a Century : the time zone field must not be changed";
214     throw new UnsupportedOperationException(err);
215   }
216 
217   public void setZoneNegative() throws UnsupportedOperationException {
218     String err = "In a Century : the time zone field must not be changed";
219     throw new UnsupportedOperationException(err);
220   }
221 
222 } // --Century