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 Aranud Blandin Added support for
34   * OperationNotSupportedException 11/02/2000 Arnaud Blandin Changed the constructor 26/10/2000
35   * Arnaud Blandin Created
36   */
37  package org.exolab.castor.types;
38  
39  import java.text.ParseException;
40  import java.util.StringTokenizer;
41  
42  import org.exolab.castor.types.RecurringDuration;
43  
44  /**
45   * Describe an XML schema TimePeriod.
46   * <p>
47   * The time period type is derived from recurringDuration by setting up the facet:
48   * <ul>
49   * <li>period to "P0Y"</li>
50   * </ul>
51   * <p>
52   * Note: This datatype is not included in any recommendation. It was introduced in
53   * http://www.w3.org/TR/2000/WD-xmlschema-2-20000407/ and was last in
54   * http://www.w3.org/TR/2000/CR-xmlschema-2-20001024/ and was removed by
55   * http://www.w3.org/TR/2001/PR-xmlschema-2-20010316/. It was not in the final approved
56   * recommendation: http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/
57   *
58   * @author <a href="mailto:blandin@intalio.com">Arnaud Blandin</a>
59   * @version $Revision$
60   * @see RecurringDuration
61   * @deprecated since Castor 1.0.6 since this type is not in any recommendation.
62   */
63  public class TimePeriod extends RecurringDuration {
64    /** SerialVersionUID */
65    private static final long serialVersionUID = -7057026912711829943L;
66  
67    /** Set to true and recompile to include debugging code in class. */
68    private static final boolean DEBUG = false;
69  
70    public TimePeriod() {
71      super("", "P0Y");
72    }
73  
74    /**
75     * returns a TimePeriod with the duration facet set up
76     * 
77     * @param duration the String value of the duration facet
78     */
79    public TimePeriod(String duration) {
80      super(duration, "P0Y");
81    }
82  
83    /**
84     * parse a string to set the fields of a TimePeriod
85     * 
86     * @param str the string to parse
87     */
88    public void setFields(String str) throws ParseException {
89  
90      // remove if necessary the Z at the end
91      if (str.endsWith("Z"))
92        str = str.substring(0, str.indexOf("Z"));
93  
94      // isNegative ? is there a time zone ?
95      if (str.startsWith("-"))
96        this.setNegative();
97  
98      String zoneStr = str.substring(str.length() - 6, str.length());
99      boolean timeZone = (((zoneStr.lastIndexOf("-") != -1) || (zoneStr.lastIndexOf("+") != -1))
100         && (zoneStr.lastIndexOf(":") != -1));
101 
102     if (DEBUG) {
103       System.out.println("In parsing method of TimePeriod");
104       System.out.println("String to parse : " + str);
105       System.out.println("Negative ? " + this.isNegative());
106       String tzone = timeZone ? zoneStr : "false";
107       System.out.println("Time zone :" + tzone);
108     }
109 
110     if (!timeZone)
111       zoneStr = null;
112     else {
113       int index = str.lastIndexOf("+") != -1 ? str.lastIndexOf("+") : str.lastIndexOf("-");
114       str = str.substring(0, index);
115     }
116 
117     // the 'T' is required
118     if (str.indexOf('T') == -1) {
119       throw new ParseException("The 'T' element is required", 0);
120     }
121     String date = str.substring(0, str.indexOf("T"));
122     String time = str.substring(str.indexOf("T"));
123 
124     // proceed date
125     StringTokenizer token = new StringTokenizer(date, "-");
126 
127     if (token.countTokens() != 3)
128       throw new ParseException(str + ": Bad date format", 0);
129 
130     // CCYY
131     String temp = token.nextToken();
132     if (temp.length() != 4)
133       throw new ParseException(str + ":Bad year format", 1);
134     if (DEBUG) {
135       System.out.println("Processing century: " + temp.substring(0, 2));
136     }
137     this.setCentury(Short.parseShort(temp.substring(0, 2)));
138     if (DEBUG) {
139       System.out.println("Processing year: " + temp.substring(2, 4));
140     }
141     try {
142       this.setYear(Short.parseShort(temp.substring(2, 4)));
143     } catch (UnsupportedOperationException e) {
144       // we are sure that this method is used with a timePeriod type
145       // (if not a ParseException is thrown) so we can never reach that point
146     }
147 
148 
149     // MM
150     temp = token.nextToken();
151     if (temp.length() != 2)
152       throw new ParseException(str + ": Bad month format", 5);
153     if (DEBUG) {
154       System.out.println("Processing month: " + temp);
155     }
156     try {
157       this.setMonth(Short.parseShort(temp));
158     } catch (UnsupportedOperationException e) {
159       // we are sure that this method is used with a timePeriod type
160       // (if not a ParseException is thrown) so we can never reach that point
161     }
162 
163     // DD
164     temp = token.nextToken();
165     if (temp.length() != 2)
166       throw new ParseException(str + ":Bad day format", 8);
167     if (DEBUG) {
168       System.out.println("Processing day: " + temp);
169     }
170     try {
171       this.setDay(Short.parseShort(temp));
172     } catch (UnsupportedOperationException e) {
173       // we are sure that this method is used with a timePeriod type
174       // (if not a ParseException is thrown) so we can never reach that point
175     }
176 
177     // proceed Time
178     token = new StringTokenizer(time, ":");
179 
180     if ((token.countTokens() < 3) && (token.countTokens() > 5))
181       throw new ParseException(str + ": Bad time format", 11);
182 
183     // hh
184     temp = token.nextToken();
185     temp = temp.substring(temp.indexOf("T") + 1);
186     if (temp.length() != 2)
187       throw new ParseException(str + ": Bad hour format", 11);
188     if (DEBUG) {
189       System.out.println("Processing hour: " + temp);
190     }
191 
192     try {
193       this.setHour(Short.parseShort(temp));
194     } catch (UnsupportedOperationException e) {
195       // we are sure that this method is used with a timePeriod type
196       // (if not a ParseException is thrown) so we can never reach that point
197     }
198 
199     // mm
200     temp = token.nextToken();
201     if (temp.length() != 2)
202       throw new ParseException(str + ": Bad minute format", 14);
203     if (DEBUG) {
204       System.out.println("Processing minute: " + temp);
205     }
206     try {
207       this.setMinute(Short.parseShort(temp));
208     } catch (UnsupportedOperationException e) {
209       // we are sure that this method is used with a timePeriod type
210       // (if not a ParseException is thrown) so we can never reach that point
211     }
212 
213     // ss
214     temp = token.nextToken();
215     String milsecond = "0";
216     if (temp.indexOf(".") != -1) {
217       milsecond = temp.substring(temp.indexOf(".") + 1);
218       temp = temp.substring(0, temp.indexOf("."));
219     }
220 
221     if (temp.length() != 2)
222       throw new ParseException(str + ": Bad second format", 17);
223     if (DEBUG) {
224       System.out.println("Processing seconds: " + temp);
225     }
226     try {
227       this.setSecond(Short.parseShort(temp.substring(0, 2)), Short.parseShort(milsecond));
228     } catch (UnsupportedOperationException e) {
229       // we are sure that this method is used with a timePeriod type
230       // (if not a ParseException is thrown) so we can never reach that point
231     }
232 
233 
234     // proceed TimeZone if any
235     if (timeZone) {
236       try {
237         if (zoneStr.startsWith("-"))
238           this.setZoneNegative();
239       } catch (UnsupportedOperationException e) {
240         // we are sure that this method is used with a timePeriod type
241         // (if not a ParseException is thrown) so we can never reach that point
242       }
243 
244       if (zoneStr.length() != 6)
245         throw new ParseException(str + ": Bad time zone format", 20);
246       try {
247         this.setZone(Short.parseShort(zoneStr.substring(1, 3)),
248             Short.parseShort(zoneStr.substring(4, 6)));
249       } catch (UnsupportedOperationException e) {
250         // we are sure that this method is used with a timePeriod type
251         // (if not a ParseException is thrown) so we can never reach that point
252       }
253 
254     } else
255       this.isUTC();
256     temp = null;
257   }// setFields
258 
259 
260   public void setPeriod(TimeDuration period) throws UnsupportedOperationException {
261     throw new UnsupportedOperationException("in a time period type,the period must not be changed");
262   }
263 
264 } // TimePeriod