1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 public class TimePeriod extends RecurringDuration {
64
65 private static final long serialVersionUID = -7057026912711829943L;
66
67
68 private static final boolean DEBUG = false;
69
70 public TimePeriod() {
71 super("", "P0Y");
72 }
73
74
75
76
77
78
79 public TimePeriod(String duration) {
80 super(duration, "P0Y");
81 }
82
83
84
85
86
87
88 public void setFields(String str) throws ParseException {
89
90
91 if (str.endsWith("Z"))
92 str = str.substring(0, str.indexOf("Z"));
93
94
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
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
125 StringTokenizer token = new StringTokenizer(date, "-");
126
127 if (token.countTokens() != 3)
128 throw new ParseException(str + ": Bad date format", 0);
129
130
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
145
146 }
147
148
149
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
160
161 }
162
163
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
174
175 }
176
177
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
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
196
197 }
198
199
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
210
211 }
212
213
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
230
231 }
232
233
234
235 if (timeZone) {
236 try {
237 if (zoneStr.startsWith("-"))
238 this.setZoneNegative();
239 } catch (UnsupportedOperationException e) {
240
241
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
251
252 }
253
254 } else
255 this.isUTC();
256 temp = null;
257 }
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 }