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.schema.cmp;
|
11
|
|
|
12
|
|
import java.math.BigDecimal;
|
13
|
|
import java.math.BigInteger;
|
14
|
|
|
15
|
|
import org.jbind.xml.base.IHasLocation;
|
16
|
|
import org.jbind.xml.base.ILanguage;
|
17
|
|
import org.jbind.xml.base.ILocation;
|
18
|
|
import org.jbind.xml.base.IRefs;
|
19
|
|
import org.jbind.xml.base.StringUtil;
|
20
|
|
import org.jbind.xml.core.constraint.ConstraintType;
|
21
|
|
import org.jbind.xml.core.constraint.IBuiltInConstraint;
|
22
|
|
import org.jbind.xml.core.constraint.ICheckContext;
|
23
|
|
import org.jbind.xml.core.constraint.IConstraint;
|
24
|
|
import org.jbind.xml.core.data.IAnySimpleTypeData;
|
25
|
|
import org.jbind.xml.core.data.IAnyTypeData;
|
26
|
|
import org.jbind.xml.core.data.IDataContext;
|
27
|
|
import org.jbind.xml.core.data.IDataReference;
|
28
|
|
import org.jbind.xml.core.data.IDateBase;
|
29
|
|
import org.jbind.xml.core.data.IDateTimeData;
|
30
|
|
import org.jbind.xml.core.data.IIntegerData;
|
31
|
|
import org.jbind.xml.core.data.ILanguageData;
|
32
|
|
import org.jbind.xml.core.data.IPointInTime;
|
33
|
|
import org.jbind.xml.core.data.IQNameData;
|
34
|
|
import org.jbind.xml.core.data.ISimpleData;
|
35
|
|
import org.jbind.xml.core.data.IStringData;
|
36
|
|
import org.jbind.xml.core.data.ITimeData;
|
37
|
|
import org.jbind.xml.core.data.IUnsignedIntData;
|
38
|
|
import org.jbind.xml.core.type.ISimpleType;
|
39
|
|
import org.jbind.xml.instance.data.GDate;
|
40
|
|
import org.jbind.xml.instance.data.TimeHelper;
|
41
|
|
import org.jbind.xml.msg.IConstraintViolations;
|
42
|
|
import org.jbind.xml.msg.XmlMessages;
|
43
|
|
import org.jbind.xml.schema.constraint.OpenReference;
|
44
|
|
|
45
|
|
public abstract class BuiltInConstraint implements IBuiltInConstraint {
|
46
|
|
|
47
|
|
private static final BigDecimal ONE = new BigDecimal("1");
|
48
|
|
|
49
|
|
public static class NameConstraint extends BuiltInConstraint {
|
50
|
|
|
51
|
7
|
public void localCheck(ICheckContext aCheckContext, IAnyTypeData aData, IConstraintViolations aViolations) {
|
52
|
7
|
String s = ((IStringData)aData).getCanonicalForm_();
|
53
|
7
|
int length = s.length();
|
54
|
7
|
boolean res = length > 0;
|
55
|
7
|
if (res) {
|
56
|
7
|
char c = s.charAt(0);
|
57
|
7
|
res = Character.isLetter(c) || (c == '_') || (c == ':');
|
58
|
7
|
for (int i = 1; res && (i < length); i++) {
|
59
|
23
|
c = s.charAt(i);
|
60
|
23
|
res = Character.isLetterOrDigit(c) || (c == '.') || (c == '-') || (c == '_') || (c == ':');
|
61
|
|
}
|
62
|
|
}
|
63
|
7
|
if (!res) {
|
64
|
0
|
aViolations.add(XmlMessages.stringIsNotAName(aData, aData.getImpl_()));
|
65
|
|
}
|
66
|
|
}
|
67
|
|
}
|
68
|
|
|
69
|
|
public static class NcNameConstraint extends BuiltInConstraint {
|
70
|
|
|
71
|
129
|
public void localCheck(ICheckContext aCheckContext, IAnyTypeData aData, IConstraintViolations aViolations) {
|
72
|
129
|
String s = ((IStringData)aData).getCanonicalForm_();
|
73
|
129
|
int length = s.length();
|
74
|
129
|
boolean res = length > 0;
|
75
|
129
|
if (res) {
|
76
|
129
|
char c = s.charAt(0);
|
77
|
129
|
res = Character.isLetter(c) || (c == '_');
|
78
|
129
|
for (int i = 1; res && (i < length); i++) {
|
79
|
472
|
c = s.charAt(i);
|
80
|
472
|
res = Character.isLetterOrDigit(c) || (c == '.') || (c == '-') || (c == '_');
|
81
|
|
}
|
82
|
|
}
|
83
|
129
|
if (!res) {
|
84
|
0
|
aViolations.add(XmlMessages.stringIsNotANcName(aData, aData.getImpl_()));
|
85
|
|
}
|
86
|
|
}
|
87
|
|
}
|
88
|
|
|
89
|
|
public static class NmTokenConstraint extends BuiltInConstraint {
|
90
|
|
|
91
|
6
|
public void localCheck(ICheckContext aCheckContext, IAnyTypeData aData, IConstraintViolations aViolations) {
|
92
|
6
|
String s = ((IStringData)aData).getCanonicalForm_();
|
93
|
6
|
int length = s.length();
|
94
|
6
|
boolean res = length > 0;
|
95
|
6
|
if (res) {
|
96
|
6
|
for (int i = 0; res && (i < length); i++) {
|
97
|
18
|
char c = s.charAt(i);
|
98
|
18
|
res = Character.isLetterOrDigit(c) || (c == '.') || (c == '-') || (c == '_') || (c == ':');
|
99
|
|
}
|
100
|
|
}
|
101
|
6
|
if (!res) {
|
102
|
0
|
aViolations.add(XmlMessages.stringIsNotANmToken(aData, aData.getImpl_()));
|
103
|
|
}
|
104
|
|
}
|
105
|
|
}
|
106
|
|
|
107
|
|
public static class IdConstraint extends NcNameConstraint {
|
108
|
14
|
public void globalCheck(IDataContext aContext, IAnyTypeData aData, IAnyTypeData anEnclosingData, IConstraintViolations aViolations) {
|
109
|
14
|
String key = ((ISimpleData)aData).getCanonicalForm_();
|
110
|
14
|
if (null != aContext.setIndexedData(IRefs.ID_INDEX, key, (null != anEnclosingData) ? anEnclosingData : aData)) {
|
111
|
0
|
aViolations.add(XmlMessages.idSimpleContentNotUnique(key, aData.getImpl_()));
|
112
|
|
}
|
113
|
|
}
|
114
|
|
}
|
115
|
|
|
116
|
|
public static class IdRefConstraint extends NcNameConstraint {
|
117
|
32
|
public void globalCheck(IDataContext aContext, IAnyTypeData aData, IAnyTypeData anEnclosingData, IConstraintViolations aViolations) {
|
118
|
32
|
String key = ((ISimpleData)aData).getCanonicalForm_();
|
119
|
32
|
IDataReference openReference = new OpenReference("cnt:", (null != anEnclosingData) ? anEnclosingData : aData, IRefs.ID_INDEX, key);
|
120
|
32
|
aContext.add(openReference);
|
121
|
|
}
|
122
|
|
}
|
123
|
|
|
124
|
|
public static class EntityConstraint extends NcNameConstraint {
|
125
|
0
|
public void globalCheck(IDataContext aContext, IAnyTypeData aData, IAnyTypeData anEnclosingData, IConstraintViolations aViolations) {
|
126
|
|
// TODO
|
127
|
|
}
|
128
|
|
}
|
129
|
|
|
130
|
|
public static class NotationConstraint extends QNameConstraint {
|
131
|
0
|
public void globalCheck(IDataContext aContext, IAnyTypeData aData, IAnyTypeData anEnclosingData, IConstraintViolations aViolations) {
|
132
|
|
// Notations data needs not to be checked.
|
133
|
|
// The notation type must not be used directly in attribute or element declarations.
|
134
|
|
// Only restricted forms with an enumeration constraint are allowed. This means that
|
135
|
|
// during the type definition of a derived notation type already the validity of the
|
136
|
|
// notation data can be checked.
|
137
|
|
}
|
138
|
|
}
|
139
|
|
|
140
|
|
protected abstract static class DateOrTimeConstraint extends BuiltInConstraint {
|
141
|
|
|
142
|
25
|
protected void checkDate(IDateBase aDate, IHasLocation aHasLocation, IConstraintViolations aViolations) {
|
143
|
25
|
if (aDate.getYear() == 0) {
|
144
|
0
|
aViolations.add(XmlMessages.invalidYear(aDate, aHasLocation));
|
145
|
|
}
|
146
|
25
|
if ((aDate.getMonth() < 1) || (aDate.getMonth() > 12)) {
|
147
|
0
|
aViolations.add(XmlMessages.invalidMonth(aDate, aHasLocation));
|
148
|
|
}
|
149
|
25
|
if ((aDate.getDay() < 1) || (aDate.getDay() > TimeHelper.maxDayInMonthFor(aDate.getYear(), aDate.getMonth()))) {
|
150
|
0
|
aViolations.add(XmlMessages.invalidDay(aDate, aHasLocation));
|
151
|
|
}
|
152
|
|
}
|
153
|
|
|
154
|
18
|
protected void checkTime(IPointInTime aTime, IHasLocation aHasLocation, IConstraintViolations aViolations) {
|
155
|
18
|
if ((aTime.getHour() < 0) || (aTime.getHour() > 23)) {
|
156
|
0
|
aViolations.add(XmlMessages.invalidHour(aTime, aHasLocation));
|
157
|
|
}
|
158
|
18
|
if ((aTime.getMinute() < 0) || (aTime.getMinute() > 59)) {
|
159
|
0
|
aViolations.add(XmlMessages.invalidMinute(aTime, aHasLocation));
|
160
|
|
}
|
161
|
18
|
if ((aTime.getSecond() < 0) || (aTime.getSecond() > 59)) {
|
162
|
0
|
aViolations.add(XmlMessages.invalidSecond(aTime, aHasLocation));
|
163
|
|
}
|
164
|
18
|
if ((null != aTime.getFraction()) && ((aTime.getFraction().signum() < 0) || (ONE.compareTo(aTime.getFraction()) <= 0))) {
|
165
|
0
|
aViolations.add(XmlMessages.invalidFraction(aTime, aHasLocation));
|
166
|
|
}
|
167
|
|
}
|
168
|
|
}
|
169
|
|
|
170
|
|
public static class TimeConstraint extends DateOrTimeConstraint {
|
171
|
7
|
public void localCheck(ICheckContext aCheckContext, IAnyTypeData aData, IConstraintViolations aViolations) {
|
172
|
7
|
checkTime(((ITimeData)aData).getTime(), aData.getImpl_(), aViolations);
|
173
|
|
}
|
174
|
|
}
|
175
|
|
|
176
|
|
public static class DateTimeConstraint extends DateOrTimeConstraint {
|
177
|
11
|
public void localCheck(ICheckContext aCheckContext, IAnyTypeData aData, IConstraintViolations aViolations) {
|
178
|
11
|
checkDate(((IDateTimeData)aData).getDateTime(), aData.getImpl_(), aViolations);
|
179
|
11
|
checkTime(((IDateTimeData)aData).getDateTime(), aData.getImpl_(), aViolations);
|
180
|
|
}
|
181
|
|
}
|
182
|
|
|
183
|
|
public static class GDateConstraint extends DateOrTimeConstraint {
|
184
|
|
|
185
|
14
|
public void localCheck(ICheckContext aCheckContext, IAnyTypeData aData, IConstraintViolations aViolations) {
|
186
|
14
|
GDate gDate = (GDate)((IAnySimpleTypeData)aData).getSimpleStorageObject();
|
187
|
14
|
checkDate(gDate, aData.getImpl_(), aViolations);
|
188
|
14
|
if ((null != gDate.getTimeZone()) && ((gDate.getTimeZone().getMinutes() < 0) || (gDate.getTimeZone().getMinutes() > 59))) {
|
189
|
0
|
aViolations.add(XmlMessages.invalidTimeZoneMinutes(aData, aData.getImpl_()));
|
190
|
|
}
|
191
|
14
|
if ((null != gDate.getTimeZone()) && ((gDate.getTimeZone().getHours() > 14) || ((gDate.getTimeZone().getHours() == 14) && (gDate.getTimeZone().getMinutes() != 0)))) {
|
192
|
0
|
aViolations.add(XmlMessages.invalidTimeZoneShift(aData, aData.getImpl_()));
|
193
|
|
}
|
194
|
|
}
|
195
|
|
}
|
196
|
|
|
197
|
|
protected abstract static class DerivedIntegerConstraint extends BuiltInConstraint {
|
198
|
|
protected BigInteger ZERO = new BigInteger("0");
|
199
|
|
}
|
200
|
|
|
201
|
|
public static class LessThanOrEqualToZero extends DerivedIntegerConstraint {
|
202
|
1
|
public void localCheck(ICheckContext aCheckContext, IAnyTypeData aData, IConstraintViolations aViolations) {
|
203
|
1
|
IIntegerData d = (IIntegerData)aData;
|
204
|
1
|
if (d.getBigInteger().compareTo(ZERO) > 0) {
|
205
|
0
|
aViolations.add(XmlMessages.numberMustBeLessThanOrEqualToZero(aData, aData.getImpl_()));
|
206
|
|
}
|
207
|
|
}
|
208
|
|
}
|
209
|
|
|
210
|
|
public static class LessThanZero extends DerivedIntegerConstraint {
|
211
|
1
|
public void localCheck(ICheckContext aCheckContext, IAnyTypeData aData, IConstraintViolations aViolations) {
|
212
|
1
|
IIntegerData d = (IIntegerData)aData;
|
213
|
1
|
if (d.getBigInteger().compareTo(ZERO) >= 0) {
|
214
|
0
|
aViolations.add(XmlMessages.numberMustBeLessThanZero(aData, aData.getImpl_()));
|
215
|
|
}
|
216
|
|
}
|
217
|
|
}
|
218
|
|
|
219
|
|
public static class GreaterThanOrEqualToZero extends DerivedIntegerConstraint {
|
220
|
5
|
public void localCheck(ICheckContext aCheckContext, IAnyTypeData aData, IConstraintViolations aViolations) {
|
221
|
5
|
IIntegerData d = (IIntegerData)aData;
|
222
|
5
|
if (d.getBigInteger().compareTo(ZERO) < 0) {
|
223
|
0
|
aViolations.add(XmlMessages.numberMustBeGreaterThanOrEqualToZero(aData, aData.getImpl_()));
|
224
|
|
}
|
225
|
|
}
|
226
|
|
}
|
227
|
|
|
228
|
|
public static class GreaterThanZero extends DerivedIntegerConstraint {
|
229
|
3
|
public void localCheck(ICheckContext aCheckContext, IAnyTypeData aData, IConstraintViolations aViolations) {
|
230
|
3
|
IIntegerData d = (IIntegerData)aData;
|
231
|
3
|
if (d.getBigInteger().compareTo(ZERO) <= 0) {
|
232
|
0
|
aViolations.add(XmlMessages.numberMustBeGreaterThanZero(aData, aData.getImpl_()));
|
233
|
|
}
|
234
|
|
}
|
235
|
|
}
|
236
|
|
|
237
|
|
public static class UnsignedInteger extends GreaterThanOrEqualToZero {
|
238
|
|
|
239
|
|
private BigInteger myUpperLimit = null;
|
240
|
3
|
public UnsignedInteger(BigInteger anUpperLimit) {
|
241
|
3
|
myUpperLimit = anUpperLimit;
|
242
|
|
}
|
243
|
1
|
public void localCheck(ICheckContext aCheckContext, IAnyTypeData aData, IConstraintViolations aViolations) {
|
244
|
1
|
super.localCheck(aCheckContext, aData, aViolations);
|
245
|
1
|
IIntegerData d = (IIntegerData)aData;
|
246
|
1
|
if (d.getBigInteger().compareTo(myUpperLimit) > 0) {
|
247
|
0
|
aViolations.add(XmlMessages.numberMustBeLessThanOrEqualTo(aData, myUpperLimit, aData.getImpl_()));
|
248
|
|
}
|
249
|
|
}
|
250
|
|
}
|
251
|
|
|
252
|
|
public static class UnsignedIntShortOrByte extends GreaterThanOrEqualToZero {
|
253
|
|
|
254
|
|
private long myUpperLimit;
|
255
|
9
|
public UnsignedIntShortOrByte(long anUpperLimit) {
|
256
|
9
|
myUpperLimit = anUpperLimit;
|
257
|
|
}
|
258
|
3
|
public void localCheck(ICheckContext aCheckContext, IAnyTypeData aData, IConstraintViolations aViolations) {
|
259
|
3
|
super.localCheck(aCheckContext, aData, aViolations);
|
260
|
3
|
IUnsignedIntData d = (IUnsignedIntData)aData;
|
261
|
3
|
if (myUpperLimit < d.getLong()) {
|
262
|
0
|
aViolations.add(XmlMessages.numberMustBeLessThanOrEqualTo(aData, new Long(myUpperLimit), aData.getImpl_()));
|
263
|
|
}
|
264
|
|
}
|
265
|
|
}
|
266
|
|
|
267
|
|
public static class AnyUri extends BuiltInConstraint {
|
268
|
7
|
public void localCheck(ICheckContext aCheckContext, IAnyTypeData aData, IConstraintViolations aViolations) {
|
269
|
|
// TODO
|
270
|
|
}
|
271
|
|
}
|
272
|
|
|
273
|
|
public static class QNameConstraint extends BuiltInConstraint {
|
274
|
1
|
public void localCheck(ICheckContext aCheckContext, IAnyTypeData aData, IConstraintViolations aViolations) {
|
275
|
1
|
String s = ((IQNameData)aData).getCanonicalForm_();
|
276
|
1
|
if (!StringUtil.isValidQName(s)) {
|
277
|
0
|
aViolations.add(XmlMessages.invalidQName(aData, aData.getImpl_()));
|
278
|
|
}
|
279
|
|
}
|
280
|
|
}
|
281
|
|
|
282
|
|
public static class Language extends BuiltInConstraint {
|
283
|
|
|
284
|
2
|
public void localCheck(ICheckContext aCheckContext, IAnyTypeData aData, IConstraintViolations aViolations) {
|
285
|
2
|
ILanguage l = ((ILanguageData)aData).getLanguage();
|
286
|
|
// The specificataion says that the codes must not be checked for validity.
|
287
|
2
|
if (aCheckContext.checkLanguageCode() && (null == aCheckContext.getLanguage(l.getPrimaryLanguage()))) {
|
288
|
0
|
aViolations.add(XmlMessages.unknownPrimaryLanguage(l, aData.getImpl_()));
|
289
|
|
}
|
290
|
2
|
if ((null != l.getCountryCode()) && (l.getCountryCode().length() == 2) && aCheckContext.checkCountryCode() && (null == aCheckContext.getCountry(l.getCountryCode()))) {
|
291
|
0
|
aViolations.add(XmlMessages.unknownCountry(l, aData.getImpl_()));
|
292
|
|
}
|
293
|
|
}
|
294
|
|
}
|
295
|
|
|
296
|
78
|
private BuiltInConstraint() {}
|
297
|
|
|
298
|
0
|
public void validate(ISimpleType aType) {}
|
299
|
|
|
300
|
0
|
public boolean isFinal() {
|
301
|
|
assert false;
|
302
|
0
|
return false;
|
303
|
|
}
|
304
|
|
|
305
|
0
|
public boolean isRestriction(IConstraint aConstraint) {
|
306
|
|
assert false;
|
307
|
0
|
return false;
|
308
|
|
}
|
309
|
|
|
310
|
78
|
public ConstraintType getConstraintType() {
|
311
|
78
|
return ConstraintType.BUILT_IN;
|
312
|
|
}
|
313
|
|
|
314
|
24
|
public void globalCheck(IDataContext aContext, IAnyTypeData aData, IAnyTypeData anEnclosingData, IConstraintViolations aViolations) {}
|
315
|
|
|
316
|
0
|
public ILocation getLocation() {
|
317
|
|
assert false;
|
318
|
0
|
return null;
|
319
|
|
}
|
320
|
|
}
|
321
|
|
|