KickJava   Java API By Example, From Geeks To Geeks.

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


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.NumberFormat JavaDoc;
60
61 /**
62  * Implementation of the XML Schema type gYearMonth
63  *
64  * @author Tom Jordahl <tomj@macromedia.com>
65  * @see <a HREF="http://www.w3.org/TR/xmlschema-2/#gYearMonth">XML Schema 3.2.10</a>
66  */

67 public class YearMonth
68 {
69    int year;
70    int month;
71    String JavaDoc timezone = null;
72
73    /**
74     * Constructs a YearMonth with the given values
75     * No timezone is specified
76     */

77    public YearMonth(int year, int month) throws NumberFormatException JavaDoc
78    {
79       setValue(year, month);
80    }
81
82    /**
83     * Constructs a YearMonth with the given values, including a timezone string
84     * The timezone is validated but not used.
85     */

86    public YearMonth(int year, int month, String JavaDoc timezone) throws NumberFormatException JavaDoc
87    {
88       setValue(year, month, timezone);
89    }
90
91    /**
92     * Construct a YearMonth from a String in the format [-]CCYY-MM
93     */

94    public YearMonth(String JavaDoc source) throws NumberFormatException JavaDoc
95    {
96       int negative = 0;
97
98       if (source.charAt(0) == '-')
99       {
100          negative = 1;
101       }
102       if (source.length() < (7 + negative))
103       {
104          throw new NumberFormatException JavaDoc(Messages.getMessage("badYearMonth00"));
105       }
106
107       // look for first '-'
108
int pos = source.substring(negative).indexOf('-');
109       if (pos < 0)
110       {
111          throw new NumberFormatException JavaDoc(Messages.getMessage("badYearMonth00"));
112       }
113       if (negative > 0) pos++; //adjust index for orginal string
114

115       setValue(Integer.parseInt(source.substring(0, pos)),
116               Integer.parseInt(source.substring(pos + 1, pos + 3)),
117               source.substring(pos + 3));
118    }
119
120    public int getYear()
121    {
122       return year;
123    }
124
125    public void setYear(int year)
126    {
127       // validate year, more than 4 digits are allowed!
128
if (year == 0)
129       {
130          throw new NumberFormatException JavaDoc(Messages.getMessage("badYearMonth00"));
131       }
132
133       this.year = year;
134    }
135
136    public int getMonth()
137    {
138       return month;
139    }
140
141    public void setMonth(int month)
142    {
143       // validate month
144
if (month < 1 || month > 12)
145       {
146          throw new NumberFormatException JavaDoc(Messages.getMessage("badYearMonth00"));
147       }
148       this.month = month;
149    }
150
151    public String JavaDoc getTimezone()
152    {
153       return timezone;
154    }
155
156    public void setTimezone(String JavaDoc timezone)
157    {
158       // validate timezone
159
if (timezone != null && timezone.length() > 0)
160       {
161          // Format [+/-]HH:MM
162
if (timezone.charAt(0) == '+' || (timezone.charAt(0) == '-'))
163          {
164             if (timezone.length() != 6 ||
165                     !Character.isDigit(timezone.charAt(1)) ||
166                     !Character.isDigit(timezone.charAt(2)) ||
167                     timezone.charAt(3) != ':' ||
168                     !Character.isDigit(timezone.charAt(4)) ||
169                     !Character.isDigit(timezone.charAt(5)))
170                throw new NumberFormatException JavaDoc(Messages.getMessage("badTimezone00"));
171
172          }
173          else if (!timezone.equals("Z"))
174          {
175             throw new NumberFormatException JavaDoc(Messages.getMessage("badTimezone00"));
176          }
177          // if we got this far, its good
178
this.timezone = timezone;
179       }
180    }
181
182    public void setValue(int year, int month, String JavaDoc timezone) throws NumberFormatException JavaDoc
183    {
184       setYear(year);
185       setMonth(month);
186       setTimezone(timezone);
187    }
188
189    public void setValue(int year, int month) throws NumberFormatException JavaDoc
190    {
191       setYear(year);
192       setMonth(month);
193    }
194
195    public String JavaDoc toString()
196    {
197       // use NumberFormat to ensure leading zeros
198
NumberFormat JavaDoc nf = NumberFormat.getInstance();
199       nf.setGroupingUsed(false);
200
201       // year
202
nf.setMinimumIntegerDigits(4);
203       String JavaDoc s = nf.format(year) + "-";
204
205       // month
206
nf.setMinimumIntegerDigits(2);
207       s += nf.format(month);
208
209       // timezone
210
if (timezone != null)
211       {
212          s = s + timezone;
213       }
214       return s;
215    }
216
217    public boolean equals(Object JavaDoc obj)
218    {
219       if (!(obj instanceof YearMonth)) return false;
220       YearMonth other = (YearMonth)obj;
221       if (obj == null) return false;
222       if (this == obj) return true;
223
224       boolean equals = (this.year == other.year && this.month == other.month);
225       if (timezone != null)
226       {
227          equals = equals && timezone.equals(other.timezone);
228       }
229       return equals;
230    }
231 }
232
Popular Tags