1
|
|
/*
|
2
|
|
* JBind
|
3
|
|
*
|
4
|
|
* Copyright (c) by Stefan Wachter. All rights reserved.
|
5
|
|
*
|
6
|
|
* Usage, modification, and redistribution is subject to license terms that are
|
7
|
|
* available at 'http://www.jbind.org'. The JBind license is like the
|
8
|
|
* 'Apache Software License V 1.1'.
|
9
|
|
*/
|
10
|
|
package org.jbind.xml.instance.data;
|
11
|
|
|
12
|
|
import java.math.BigDecimal;
|
13
|
|
|
14
|
|
import org.jbind.xml.core.data.CompareResult;
|
15
|
|
import org.jbind.xml.core.data.IHasOrder;
|
16
|
|
import org.jbind.xml.core.data.ITimeZone;
|
17
|
|
import org.jbind.xml.msg.XmlException;
|
18
|
|
import org.jbind.xml.msg.XmlMessages;
|
19
|
|
|
20
|
|
public abstract class AbstractDateOrTimeData extends AbstractSimpleData {
|
21
|
|
|
22
|
|
protected static class TimeStruct {
|
23
|
|
|
24
|
|
public int hour;
|
25
|
|
public int minute;
|
26
|
|
public int second;
|
27
|
|
public BigDecimal fraction;
|
28
|
|
public ITimeZone timeZone;
|
29
|
29
|
public TimeStruct(int anHour, int aMinute, int aSecond, BigDecimal aFraction, ITimeZone aTimeZone) {
|
30
|
29
|
hour = anHour;
|
31
|
29
|
minute = aMinute;
|
32
|
29
|
second = aSecond;
|
33
|
29
|
fraction = aFraction;
|
34
|
29
|
timeZone = aTimeZone;
|
35
|
|
}
|
36
|
11
|
public TimeStruct(TimeStruct aTimeStruct) {
|
37
|
11
|
this(aTimeStruct.hour, aTimeStruct.minute, aTimeStruct.second, aTimeStruct.fraction, aTimeStruct.timeZone);
|
38
|
|
}
|
39
|
|
}
|
40
|
|
|
41
|
|
protected static class DateTimeStruct extends TimeStruct {
|
42
|
|
|
43
|
|
public int sign;
|
44
|
|
public int year;
|
45
|
|
public int month;
|
46
|
|
public int day;
|
47
|
11
|
public DateTimeStruct(int aSign, int aYear, int aMonth, int aDay, TimeStruct aTimeStruct) {
|
48
|
11
|
super(aTimeStruct);
|
49
|
11
|
sign = aSign;
|
50
|
11
|
year = aYear;
|
51
|
11
|
month = aMonth;
|
52
|
11
|
day = aDay;
|
53
|
|
}
|
54
|
|
}
|
55
|
|
|
56
|
|
protected static class DateStruct {
|
57
|
|
|
58
|
|
public int sign;
|
59
|
|
public int year;
|
60
|
|
public int month;
|
61
|
|
public int day;
|
62
|
|
public ITimeZone timeZone = null;
|
63
|
7
|
public DateStruct(int aSign, int aYear, int aMonth, int aDay, ITimeZone aTimeZone) {
|
64
|
7
|
sign = aSign;
|
65
|
7
|
year = aYear;
|
66
|
7
|
month = aMonth;
|
67
|
7
|
day = aDay;
|
68
|
7
|
timeZone = aTimeZone;
|
69
|
|
}
|
70
|
|
}
|
71
|
|
|
72
|
40
|
public AbstractDateOrTimeData() {}
|
73
|
|
|
74
|
40
|
protected void doAccept(String aString) throws XmlException {
|
75
|
40
|
super.doAccept(aString);
|
76
|
40
|
ensureChar(aString, 0);
|
77
|
|
}
|
78
|
|
|
79
|
139
|
protected int parseInt(String aString, int anIndex, int aLength) throws XmlException {
|
80
|
139
|
int res = 0;
|
81
|
139
|
if (aString.length() <= anIndex + aLength - 1) {
|
82
|
0
|
throw new XmlException(XmlMessages.truncatedData(aString, getImpl_()));
|
83
|
|
}
|
84
|
139
|
for (int i = anIndex; i < anIndex + aLength; i++) {
|
85
|
318
|
char c = aString.charAt(i);
|
86
|
318
|
if (!Character.isDigit(c)) {
|
87
|
0
|
throw new XmlException(XmlMessages.mustBeDigit(aString, new Integer(i), getImpl_()));
|
88
|
|
}
|
89
|
318
|
res *= 10;
|
90
|
318
|
res += c - '0';
|
91
|
|
}
|
92
|
139
|
return res;
|
93
|
|
}
|
94
|
|
|
95
|
29
|
protected int parseInt(String aString, int anIndex) throws XmlException {
|
96
|
29
|
int res = 0;
|
97
|
29
|
int idx = anIndex;
|
98
|
63
|
while (idx < aString.length()) {
|
99
|
63
|
char c = aString.charAt(idx);
|
100
|
63
|
if (!Character.isDigit(c)) {
|
101
|
29
|
break;
|
102
|
|
}
|
103
|
34
|
res *= 10;
|
104
|
34
|
res += c - '0';
|
105
|
34
|
idx++;
|
106
|
|
}
|
107
|
29
|
if (anIndex == idx) {
|
108
|
0
|
throw new XmlException(XmlMessages.invalidDuration(aString, getImpl_()));
|
109
|
|
}
|
110
|
29
|
return res;
|
111
|
|
}
|
112
|
|
|
113
|
29
|
protected int advanceInt(String aString, int anIndex) {
|
114
|
29
|
int res = anIndex;
|
115
|
29
|
while ((res < aString.length()) && Character.isDigit(aString.charAt(res))) {
|
116
|
34
|
res++;
|
117
|
|
}
|
118
|
29
|
return res;
|
119
|
|
}
|
120
|
|
|
121
|
185
|
protected void ensureChar(String aString, int anIndex) throws XmlException {
|
122
|
185
|
if (anIndex >= aString.length()) {
|
123
|
0
|
throw new XmlException(XmlMessages.truncatedData(aString, getImpl_()));
|
124
|
|
}
|
125
|
|
}
|
126
|
|
|
127
|
119
|
protected void parseChar(String aString, int anIndex, char aChar) throws XmlException {
|
128
|
119
|
ensureChar(aString, anIndex);
|
129
|
119
|
if (aString.charAt(anIndex) != aChar) {
|
130
|
0
|
throw new XmlException(XmlMessages.invalidCharacter(aString, new Integer(anIndex), new Character(aChar), getImpl_()));
|
131
|
|
}
|
132
|
|
}
|
133
|
|
|
134
|
32
|
protected ITimeZone acceptTimeZone(String aString, int anIndex) throws XmlException {
|
135
|
32
|
ITimeZone res = null;
|
136
|
32
|
if (anIndex < aString.length()) {
|
137
|
21
|
int idx = anIndex;
|
138
|
21
|
char c = aString.charAt(idx);
|
139
|
21
|
idx++;
|
140
|
21
|
if (c == 'Z') {
|
141
|
10
|
res = TimeZone.UTC;
|
142
|
11
|
} else if ((c == '+') || (c == '-')) {
|
143
|
11
|
int sign = (c == '+') ? 1 : -1;
|
144
|
11
|
int tzHours = parseInt(aString, idx, 2);
|
145
|
11
|
idx += 2;
|
146
|
11
|
parseChar(aString, idx, ':');
|
147
|
11
|
idx++;
|
148
|
11
|
int tzMinutes = parseInt(aString, idx, 2);
|
149
|
11
|
idx += 2;
|
150
|
11
|
res = new TimeZone(sign, tzHours, tzMinutes);
|
151
|
|
} else {
|
152
|
0
|
throw new XmlException(XmlMessages.invalidTimeZone(aString, getImpl_()));
|
153
|
|
}
|
154
|
21
|
if (aString.length() > idx) {
|
155
|
0
|
throw new XmlException(XmlMessages.unexpectedCharactersAfterTimeZone(aString, getImpl_()));
|
156
|
|
}
|
157
|
|
}
|
158
|
32
|
return res;
|
159
|
|
}
|
160
|
|
|
161
|
18
|
protected TimeStruct acceptTime(String aString, int anIndex) throws XmlException {
|
162
|
18
|
int idx = anIndex;
|
163
|
18
|
int hour = parseInt(aString, idx, 2);
|
164
|
18
|
idx += 2;
|
165
|
18
|
parseChar(aString, idx, ':');
|
166
|
18
|
idx++;
|
167
|
18
|
int minute = parseInt(aString, idx, 2);
|
168
|
18
|
idx += 2;
|
169
|
18
|
parseChar(aString, idx, ':');
|
170
|
18
|
idx++;
|
171
|
18
|
int second = parseInt(aString, idx, 2);
|
172
|
18
|
idx += 2;
|
173
|
18
|
BigDecimal fraction = null;
|
174
|
18
|
if ((aString.length() > idx) && (aString.charAt(idx) == '.')) {
|
175
|
7
|
idx++;
|
176
|
7
|
int start = idx;
|
177
|
7
|
while ((idx < aString.length()) && Character.isDigit(aString.charAt(idx))) {
|
178
|
25
|
idx++;
|
179
|
|
}
|
180
|
7
|
fraction = new BigDecimal("0." + aString.substring(start, idx));
|
181
|
|
}
|
182
|
18
|
ITimeZone timeZone = acceptTimeZone(aString, idx);
|
183
|
18
|
return new TimeStruct(hour, minute, second, fraction, timeZone);
|
184
|
|
}
|
185
|
|
|
186
|
11
|
protected DateTimeStruct acceptDateTime(String aString) throws XmlException {
|
187
|
11
|
int idx = 0;
|
188
|
11
|
ensureChar(aString, idx);
|
189
|
11
|
int sign = 1;
|
190
|
11
|
char c = aString.charAt(idx);
|
191
|
11
|
if ((c == '+') || (c == '-')) {
|
192
|
2
|
idx++;
|
193
|
2
|
sign = (c == '+') ? 1 : -1;
|
194
|
|
}
|
195
|
11
|
int year = parseInt(aString, idx, 4);
|
196
|
11
|
idx += 4;
|
197
|
11
|
while ((idx < aString.length()) && Character.isDigit(aString.charAt(idx))) {
|
198
|
4
|
if (year > Integer.MAX_VALUE / 10 - 10) {
|
199
|
0
|
throw new XmlException(XmlMessages.yearTooGreat(aString, getImpl_()));
|
200
|
|
}
|
201
|
4
|
year *= 10;
|
202
|
4
|
year += aString.charAt(idx++) - '0';
|
203
|
|
}
|
204
|
11
|
parseChar(aString, idx, '-');
|
205
|
11
|
idx++;
|
206
|
11
|
int month = parseInt(aString, idx, 2);
|
207
|
11
|
idx += 2;
|
208
|
11
|
parseChar(aString, idx, '-');
|
209
|
11
|
idx++;
|
210
|
11
|
int day = parseInt(aString, idx, 2);
|
211
|
11
|
idx += 2;
|
212
|
11
|
parseChar(aString, idx, 'T');
|
213
|
11
|
idx++;
|
214
|
11
|
TimeStruct ts = acceptTime(aString, idx);
|
215
|
11
|
return new DateTimeStruct(sign, year, month, day, ts);
|
216
|
|
}
|
217
|
|
|
218
|
7
|
protected DateStruct acceptDate(String aString) throws XmlException {
|
219
|
7
|
int idx = 0;
|
220
|
7
|
ensureChar(aString, idx);
|
221
|
7
|
int sign = 1;
|
222
|
7
|
char c = aString.charAt(idx);
|
223
|
7
|
if (c == '-') {
|
224
|
1
|
sign = -1;
|
225
|
1
|
idx++;
|
226
|
|
}
|
227
|
7
|
int year = parseYear(aString, idx);
|
228
|
7
|
idx = advanceNumber(aString, idx);
|
229
|
7
|
parseChar(aString, idx, '-');
|
230
|
7
|
idx++;
|
231
|
7
|
int month = parseInt(aString, idx, 2);
|
232
|
7
|
idx += 2;
|
233
|
7
|
parseChar(aString, idx, '-');
|
234
|
7
|
idx++;
|
235
|
7
|
int day = parseInt(aString, idx, 2);
|
236
|
7
|
idx += 2;
|
237
|
7
|
ITimeZone timeZone = acceptTimeZone(aString, idx);
|
238
|
7
|
return new DateStruct(sign, year, month, day, timeZone);
|
239
|
|
}
|
240
|
|
|
241
|
9
|
public int parseYear(String aString, int anIndex) throws XmlException {
|
242
|
9
|
int idx = anIndex;
|
243
|
9
|
int year = parseInt(aString, idx, 4);
|
244
|
9
|
idx += 4;
|
245
|
9
|
while ((idx < aString.length()) && Character.isDigit(aString.charAt(idx))) {
|
246
|
1
|
if (year > Integer.MAX_VALUE / 10 - 10) {
|
247
|
0
|
throw new XmlException(XmlMessages.yearTooGreat(aString, getImpl_()));
|
248
|
|
}
|
249
|
1
|
year *= 10;
|
250
|
1
|
year += aString.charAt(idx++) - '0';
|
251
|
|
}
|
252
|
9
|
return year;
|
253
|
|
}
|
254
|
|
|
255
|
9
|
public int advanceNumber(String aString, int anIndex) {
|
256
|
9
|
int res = anIndex;
|
257
|
9
|
while ((res < aString.length()) && Character.isDigit(aString.charAt(res))) {
|
258
|
37
|
res++;
|
259
|
|
}
|
260
|
9
|
return res;
|
261
|
|
}
|
262
|
|
|
263
|
0
|
public final boolean isEqual(IHasOrder aData) {
|
264
|
0
|
CompareResult r = doGetCompareResult(aData);
|
265
|
0
|
return CompareResult.EQUAL == r;
|
266
|
|
}
|
267
|
|
|
268
|
0
|
public final boolean isGreater(IHasOrder aData) {
|
269
|
0
|
CompareResult r = doGetCompareResult(aData);
|
270
|
0
|
return CompareResult.GREATER == r;
|
271
|
|
}
|
272
|
|
|
273
|
0
|
public final boolean isGreaterOrEqual(IHasOrder aData) {
|
274
|
0
|
CompareResult r = doGetCompareResult(aData);
|
275
|
0
|
return (CompareResult.GREATER == r) || (CompareResult.EQUAL == r);
|
276
|
|
}
|
277
|
|
|
278
|
0
|
public final boolean isLess(IHasOrder aData) {
|
279
|
0
|
CompareResult r = doGetCompareResult(aData);
|
280
|
0
|
return CompareResult.LESS == r;
|
281
|
|
}
|
282
|
|
|
283
|
0
|
public final boolean isLessOrEqual(IHasOrder aData) {
|
284
|
0
|
CompareResult r = doGetCompareResult(aData);
|
285
|
0
|
return (CompareResult.LESS == r) || (CompareResult.EQUAL == r);
|
286
|
|
}
|
287
|
|
|
288
|
|
protected abstract CompareResult doGetCompareResult(IHasOrder aDate);
|
289
|
|
}
|
290
|
|
|