KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > types > Time


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2002-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55 package org.jboss.axis.types;
56
57 import org.jboss.axis.utils.Messages;
58
59 import java.text.SimpleDateFormat JavaDoc;
60 import java.util.Calendar JavaDoc;
61 import java.util.Date JavaDoc;
62 import java.util.TimeZone JavaDoc;
63
64 /**
65  * Class that represents the xsd:time XML Schema type
66  */

67 public class Time
68 {
69    private Calendar JavaDoc _value;
70
71    private static SimpleDateFormat JavaDoc zulu =
72            new SimpleDateFormat JavaDoc("HH:mm:ss.SSS'Z'");
73
74    // We should always format dates in the GMT timezone
75
static
76    {
77       zulu.setTimeZone(TimeZone.getTimeZone("GMT"));
78    }
79
80
81    /**
82     * Initialize with a Calender, year month and date are ignored
83     */

84    public Time(Calendar JavaDoc value)
85    {
86       this._value = value;
87       _value.set(0, 0, 0); // ignore year, month, date
88
}
89
90    /**
91     * Converts a string formatted as HH:mm:ss[.SSS][+/-offset]
92     */

93    public Time(String JavaDoc value) throws NumberFormatException JavaDoc
94    {
95       _value = makeValue(value);
96    }
97
98    public Calendar JavaDoc getAsCalendar()
99    {
100       return _value;
101    }
102
103    public void setTime(Calendar JavaDoc date)
104    {
105       this._value = date;
106       _value.set(0, 0, 0); // ignore year, month, date
107
}
108
109    public void setTime(Date JavaDoc date)
110    {
111       _value.setTime(date);
112       _value.set(0, 0, 0); // ignore year, month, date
113
}
114
115    /**
116     * Utility function that parses xsd:time strings and returns a Date object
117     */

118    private Calendar JavaDoc makeValue(String JavaDoc source) throws NumberFormatException JavaDoc
119    {
120       Calendar JavaDoc calendar = Calendar.getInstance();
121       Date JavaDoc date;
122
123       // validate fixed portion of format
124
if (source != null)
125       {
126          if (source.charAt(2) != ':' || source.charAt(5) != ':')
127             throw new NumberFormatException JavaDoc(Messages.getMessage("badTime00"));
128          if (source.length() < 8)
129          {
130             throw new NumberFormatException JavaDoc(Messages.getMessage("badTime00"));
131          }
132       }
133
134       // convert what we have validated so far
135
try
136       {
137          synchronized (zulu)
138          {
139             date = zulu.parse(source == null ? null :
140                     (source.substring(0, 8) + ".000Z"));
141          }
142       }
143       catch (Exception JavaDoc e)
144       {
145          throw new NumberFormatException JavaDoc(e.toString());
146       }
147
148       int pos = 8; // The "." in hh:mm:ss.sss
149

150       // parse optional milliseconds
151
if (source != null)
152       {
153          if (pos < source.length() && source.charAt(pos) == '.')
154          {
155             int milliseconds = 0;
156             int start = ++pos;
157             while (pos < source.length() &&
158                     Character.isDigit(source.charAt(pos)))
159                pos++;
160
161             String JavaDoc decimal = source.substring(start, pos);
162             if (decimal.length() == 3)
163             {
164                milliseconds = Integer.parseInt(decimal);
165             }
166             else if (decimal.length() < 3)
167             {
168                milliseconds = Integer.parseInt((decimal + "000")
169                        .substring(0, 3));
170             }
171             else
172             {
173                milliseconds = Integer.parseInt(decimal.substring(0, 3));
174                if (decimal.charAt(3) >= '5') ++milliseconds;
175             }
176
177             // add milliseconds to the current date
178
date.setTime(date.getTime() + milliseconds);
179          }
180
181          // parse optional timezone
182
if (pos + 5 < source.length() &&
183                  (source.charAt(pos) == '+' || (source.charAt(pos) == '-')))
184          {
185             if (!Character.isDigit(source.charAt(pos + 1)) ||
186                     !Character.isDigit(source.charAt(pos + 2)) ||
187                     source.charAt(pos + 3) != ':' ||
188                     !Character.isDigit(source.charAt(pos + 4)) ||
189                     !Character.isDigit(source.charAt(pos + 5)))
190                throw new NumberFormatException JavaDoc(Messages.getMessage("badTimezone00"));
191
192             int hours = (source.charAt(pos + 1) - '0') * 10
193                     + source.charAt(pos + 2) - '0';
194             int mins = (source.charAt(pos + 4) - '0') * 10
195                     + source.charAt(pos + 5) - '0';
196             int milliseconds = (hours * 60 + mins) * 60 * 1000;
197
198             // subtract milliseconds from current date to obtain GMT
199
if (source.charAt(pos) == '+') milliseconds = -milliseconds;
200             date.setTime(date.getTime() + milliseconds);
201             pos += 6;
202          }
203
204          if (pos < source.length() && source.charAt(pos) == 'Z')
205          {
206             pos++;
207             calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
208          }
209
210          if (pos < source.length())
211             throw new NumberFormatException JavaDoc(Messages.getMessage("badChars00"));
212       }
213
214       calendar.setTime(date);
215       calendar.set(0, 0, 0); // ignore year, month, date
216

217       return calendar;
218    }
219
220    public String JavaDoc toString()
221    {
222       synchronized (zulu)
223       {
224          return zulu.format(_value.getTime());
225       }
226
227    }
228
229    public boolean equals(Object JavaDoc obj)
230    {
231       if (!(obj instanceof Time)) return false;
232       Time other = (Time)obj;
233       if (obj == null) return false;
234       if (this == obj) return true;
235
236       boolean _equals;
237       _equals = true &&
238               ((_value == null && other._value == null) ||
239               (_value != null &&
240               _value.equals(other._value)));
241
242       return _equals;
243
244    }
245 }
246
Popular Tags