KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

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

93    public Year(String JavaDoc source) throws NumberFormatException JavaDoc
94    {
95       int negative = 0;
96
97       if (source.charAt(0) == '-')
98       {
99          negative = 1;
100       }
101       if (source.length() < (4 + negative))
102       {
103          throw new NumberFormatException JavaDoc(Messages.getMessage("badYear00"));
104       }
105
106       // calculate how many more than 4 digits (if any) in the year
107
int pos = 4 + negative; // skip minus sign if present
108
while (pos < source.length() && Character.isDigit(source.charAt(pos)))
109       {
110          ++pos;
111       }
112
113       setValue(Integer.parseInt(source.substring(0, pos)),
114               source.substring(pos));
115    }
116
117    public int getYear()
118    {
119       return year;
120    }
121
122    public void setYear(int year)
123    {
124       // validate year, more than 4 digits are allowed!
125
if (year == 0)
126       {
127          throw new NumberFormatException JavaDoc(Messages.getMessage("badYear00"));
128       }
129
130       this.year = year;
131    }
132
133    public String JavaDoc getTimezone()
134    {
135       return timezone;
136    }
137
138    public void setTimezone(String JavaDoc timezone)
139    {
140       // validate timezone
141
if (timezone != null && timezone.length() > 0)
142       {
143          // Format [+/-]HH:MM
144
if (timezone.charAt(0) == '+' || (timezone.charAt(0) == '-'))
145          {
146             if (timezone.length() != 6 ||
147                     !Character.isDigit(timezone.charAt(1)) ||
148                     !Character.isDigit(timezone.charAt(2)) ||
149                     timezone.charAt(3) != ':' ||
150                     !Character.isDigit(timezone.charAt(4)) ||
151                     !Character.isDigit(timezone.charAt(5)))
152                throw new NumberFormatException JavaDoc(Messages.getMessage("badTimezone00"));
153
154          }
155          else if (!timezone.equals("Z"))
156          {
157             throw new NumberFormatException JavaDoc(Messages.getMessage("badTimezone00"));
158          }
159          // if we got this far, its good
160
this.timezone = timezone;
161       }
162    }
163
164    public void setValue(int year, String JavaDoc timezone)
165            throws NumberFormatException JavaDoc
166    {
167       setYear(year);
168       setTimezone(timezone);
169    }
170
171    public void setValue(int year) throws NumberFormatException JavaDoc
172    {
173       setYear(year);
174    }
175
176    public String JavaDoc toString()
177    {
178       // use NumberFormat to ensure leading zeros
179
NumberFormat JavaDoc nf = NumberFormat.getInstance();
180       nf.setGroupingUsed(false);
181
182       // year
183
nf.setMinimumIntegerDigits(4);
184       String JavaDoc s = nf.format(year);
185
186       // timezone
187
if (timezone != null)
188       {
189          s = s + timezone;
190       }
191       return s;
192    }
193
194    public boolean equals(Object JavaDoc obj)
195    {
196       if (!(obj instanceof Year)) return false;
197       Year other = (Year)obj;
198       if (obj == null) return false;
199       if (this == obj) return true;
200
201       boolean equals = (this.year == other.year);
202       if (timezone != null)
203       {
204          equals = equals && timezone.equals(other.timezone);
205       }
206       return equals;
207    }
208 }
209
Popular Tags