KickJava   Java API By Example, From Geeks To Geeks.

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


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 gMonthDay
63  *
64  * @author Tom Jordahl <tomj@macromedia.com>
65  * @see <a HREF="http://www.w3.org/TR/xmlschema-2/#gMonthDay">XML Schema 3.2.12</a>
66  */

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

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

87    public MonthDay(int month, int day, String JavaDoc timezone)
88            throws NumberFormatException JavaDoc
89    {
90       setValue(month, day, timezone);
91    }
92
93    /**
94     * Construct a MonthDay from a String in the format --MM-DD[timezone]
95     */

96    public MonthDay(String JavaDoc source) throws NumberFormatException JavaDoc
97    {
98       if (source.length() < 6)
99       {
100          throw new NumberFormatException JavaDoc(Messages.getMessage("badMonthDay00"));
101       }
102
103       if (source.charAt(0) != '-' ||
104               source.charAt(1) != '-' ||
105               source.charAt(4) != '-')
106       {
107          throw new NumberFormatException JavaDoc(Messages.getMessage("badMonthDay00"));
108       }
109
110       setValue(Integer.parseInt(source.substring(2, 4)),
111               Integer.parseInt(source.substring(5, 7)),
112               source.substring(7));
113    }
114
115    public int getMonth()
116    {
117       return month;
118    }
119
120    public void setMonth(int month)
121    {
122       // validate month
123
if (month < 1 || month > 12)
124       {
125          throw new NumberFormatException JavaDoc(Messages.getMessage("badMonthDay00"));
126       }
127       this.month = month;
128    }
129
130    public int getDay()
131    {
132       return day;
133    }
134
135    /**
136     * Set the day
137     * NOTE: if the month isn't set yet, the day isn't validated
138     */

139    public void setDay(int day)
140    {
141       // validate day
142
if (day < 1 || day > 31)
143       {
144          throw new NumberFormatException JavaDoc(Messages.getMessage("badMonthDay00"));
145       }
146       // 30 days has September... All the rest have 31 (except Feb!)
147
// NOTE: if month isn't set, we don't validate day.
148
if ((month == 2 && day > 29) ||
149               ((month == 9 || month == 4 || month == 6 || month == 11) && day > 30))
150       {
151          throw new NumberFormatException JavaDoc(Messages.getMessage("badMonthDay00"));
152       }
153       this.day = day;
154    }
155
156    public String JavaDoc getTimezone()
157    {
158       return timezone;
159    }
160
161    public void setTimezone(String JavaDoc timezone)
162    {
163       // validate timezone
164
if (timezone != null && timezone.length() > 0)
165       {
166          // Format [+/-]HH:MM
167
if (timezone.charAt(0) == '+' || (timezone.charAt(0) == '-'))
168          {
169             if (timezone.length() != 6 ||
170                     !Character.isDigit(timezone.charAt(1)) ||
171                     !Character.isDigit(timezone.charAt(2)) ||
172                     timezone.charAt(3) != ':' ||
173                     !Character.isDigit(timezone.charAt(4)) ||
174                     !Character.isDigit(timezone.charAt(5)))
175                throw new NumberFormatException JavaDoc(Messages.getMessage("badTimezone00"));
176
177          }
178          else if (!timezone.equals("Z"))
179          {
180             throw new NumberFormatException JavaDoc(Messages.getMessage("badTimezone00"));
181          }
182          // if we got this far, its good
183
this.timezone = timezone;
184       }
185    }
186
187    public void setValue(int month, int day, String JavaDoc timezone)
188            throws NumberFormatException JavaDoc
189    {
190       setMonth(month);
191       setDay(day);
192       setTimezone(timezone);
193    }
194
195    public void setValue(int month, int day) throws NumberFormatException JavaDoc
196    {
197       setMonth(month);
198       setDay(day);
199    }
200
201    public String JavaDoc toString()
202    {
203       // use NumberFormat to ensure leading zeros
204
NumberFormat JavaDoc nf = NumberFormat.getInstance();
205       nf.setGroupingUsed(false);
206
207       // month & Day: --MM-DD
208
nf.setMinimumIntegerDigits(2);
209       String JavaDoc s = "--" + nf.format(month) + "-" + nf.format(day);
210
211       // timezone
212
if (timezone != null)
213       {
214          s = s + timezone;
215       }
216       return s;
217    }
218
219    public boolean equals(Object JavaDoc obj)
220    {
221       if (!(obj instanceof MonthDay)) return false;
222       MonthDay other = (MonthDay)obj;
223       if (obj == null) return false;
224       if (this == obj) return true;
225
226       boolean equals = (this.month == other.month && this.day == other.day);
227       if (timezone != null)
228       {
229          equals = equals && timezone.equals(other.timezone);
230       }
231       return equals;
232    }
233 }
234
Popular Tags