-
-
Notifications
You must be signed in to change notification settings - Fork 648
/
Check.hx
329 lines (312 loc) · 8.25 KB
/
Check.hx
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package haxe.xml;
enum Filter {
FInt;
FBool;
FEnum(values:Array<String>);
FReg(matcher:EReg);
}
enum Attrib {
Att(name:String, ?filter:Filter, ?defvalue:String);
}
enum Rule {
RNode(name:String, ?attribs:Array<Attrib>, ?childs:Rule);
RData(?filter:Filter);
RMulti(rule:Rule, ?atLeastOne:Bool);
RList(rules:Array<Rule>, ?ordered:Bool);
RChoice(choices:Array<Rule>);
ROptional(rule:Rule);
}
private enum CheckResult {
CMatch;
CMissing(r:Rule);
CExtra(x:Xml);
CElementExpected(name:String, x:Xml);
CDataExpected(x:Xml);
CExtraAttrib(att:String, x:Xml);
CMissingAttrib(att:String, x:Xml);
CInvalidAttrib(att:String, x:Xml, f:Filter);
CInvalidData(x:Xml, f:Filter);
CInElement(x:Xml, r:CheckResult);
}
class Check {
static var blanks = ~/^[ \r\n\t]*$/;
static function isBlank(x:Xml) {
return (x.nodeType == Xml.PCData && blanks.match(x.nodeValue)) || x.nodeType == Xml.Comment;
}
static function filterMatch(s:String, f:Filter) {
switch (f) {
case FInt:
return filterMatch(s, FReg(~/[0-9]+/));
case FBool:
return filterMatch(s, FEnum(["true", "false", "0", "1"]));
case FEnum(values):
for (v in values)
if (s == v)
return true;
return false;
case FReg(r):
return r.match(s);
}
}
static function isNullable(r:Rule) {
switch (r) {
case RMulti(r, one):
return (one != true || isNullable(r));
case RList(rl, _):
for (r in rl)
if (!isNullable(r))
return false;
return true;
case RChoice(rl):
for (r in rl)
if (isNullable(r))
return true;
return false;
case RData(_):
return false;
case RNode(_, _, _):
return false;
case ROptional(_):
return true;
}
}
static function check(x:Xml, r:Rule) {
switch (r) {
// check the node validity
case RNode(name, attribs, childs):
if (x.nodeType != Xml.Element || x.nodeName != name)
return CElementExpected(name, x);
var attribs = if (attribs == null) new Array() else attribs.copy();
// check defined attributes
for (xatt in x.attributes()) {
var found = false;
for (att in attribs)
switch (att) {
case Att(name, filter, _):
if (xatt != name)
continue;
if (filter != null && !filterMatch(x.get(xatt), filter))
return CInvalidAttrib(name, x, filter);
attribs.remove(att);
found = true;
}
if (!found)
return CExtraAttrib(xatt, x);
}
// check remaining unchecked attributes
for (att in attribs)
switch (att) {
case Att(name, _, defvalue):
if (defvalue == null)
return CMissingAttrib(name, x);
}
// check childs
if (childs == null)
childs = RList([]);
var m = checkList(x.iterator(), childs);
if (m != CMatch)
return CInElement(x, m);
// set default attribs values
for (att in attribs)
switch (att) {
case Att(name, _, defvalue):
x.set(name, defvalue);
}
return CMatch;
// check the data validity
case RData(filter):
if (x.nodeType != Xml.PCData && x.nodeType != Xml.CData)
return CDataExpected(x);
if (filter != null && !filterMatch(x.nodeValue, filter))
return CInvalidData(x, filter);
return CMatch;
// several choices
case RChoice(choices):
if (choices.length == 0)
throw "No choice possible";
for (c in choices)
if (check(x, c) == CMatch)
return CMatch;
return check(x, choices[0]);
case ROptional(r):
return check(x, r);
default:
throw "Unexpected " + Std.string(r);
}
}
static function checkList(it:Iterator<Xml>, r:Rule) {
switch (r) {
case RList(rules, ordered):
var rules = rules.copy();
for (x in it) {
if (isBlank(x))
continue;
var found = false;
for (r in rules) {
var m = checkList([x].iterator(), r);
if (m == CMatch) {
found = true;
switch (r) {
case RMulti(rsub, one):
if (one) {
var i;
for (i in 0...rules.length)
if (rules[i] == r)
rules[i] = RMulti(rsub);
}
default:
rules.remove(r);
}
break;
} else if (ordered && !isNullable(r))
return m;
}
if (!found)
return CExtra(x);
}
for (r in rules)
if (!isNullable(r))
return CMissing(r);
return CMatch;
case RMulti(r, one):
var found = false;
for (x in it) {
if (isBlank(x))
continue;
var m = checkList([x].iterator(), r);
if (m != CMatch)
return m;
found = true;
}
if (one && !found)
return CMissing(r);
return CMatch;
default:
var found = false;
for (x in it) {
if (isBlank(x))
continue;
var m = check(x, r);
if (m != CMatch)
return m;
found = true;
break;
}
if (!found) {
switch (r) {
case ROptional(_):
default: return CMissing(r);
}
}
for (x in it) {
if (isBlank(x))
continue;
return CExtra(x);
}
return CMatch;
}
}
static function makeWhere(path:Array<Xml>) {
if (path.length == 0)
return "";
var s = "In ";
var first = true;
for (x in path) {
if (first)
first = false;
else
s += ".";
s += x.nodeName;
}
return s + ": ";
}
static function makeString(x:Xml) {
if (x.nodeType == Xml.Element)
return "element " + x.nodeName;
var s = x.nodeValue.split("\r").join("\\r").split("\n").join("\\n").split("\t").join("\\t");
if (s.length > 20)
return s.substr(0, 17) + "...";
return s;
}
static function makeRule(r:Rule) {
switch (r) {
case RNode(name, _, _):
return "element " + name;
case RData(_):
return "data";
case RMulti(r, _):
return makeRule(r);
case RList(rules, _):
return makeRule(rules[0]);
case RChoice(choices):
return makeRule(choices[0]);
case ROptional(r):
return makeRule(r);
}
}
static function makeError(m, ?path) {
if (path == null)
path = new Array();
switch (m) {
case CMatch:
throw "assert";
case CMissing(r):
return makeWhere(path) + "Missing " + makeRule(r);
case CExtra(x):
return makeWhere(path) + "Unexpected " + makeString(x);
case CElementExpected(name, x):
return makeWhere(path) + makeString(x) + " while expected element " + name;
case CDataExpected(x):
return makeWhere(path) + makeString(x) + " while data expected";
case CExtraAttrib(att, x):
path.push(x);
return makeWhere(path) + "unexpected attribute " + att;
case CMissingAttrib(att, x):
path.push(x);
return makeWhere(path) + "missing required attribute " + att;
case CInvalidAttrib(att, x, _):
path.push(x);
return makeWhere(path) + "invalid attribute value for " + att;
case CInvalidData(x, _):
return makeWhere(path) + "invalid data format for " + makeString(x);
case CInElement(x, m):
path.push(x);
return makeError(m, path);
}
}
public static function checkNode(x:Xml, r:Rule) {
var m = checkList([x].iterator(), r);
if (m == CMatch)
return;
throw makeError(m);
}
public static function checkDocument(x:Xml, r:Rule) {
if (x.nodeType != Xml.Document)
throw "Document expected";
var m = checkList(x.iterator(), r);
if (m == CMatch)
return;
throw makeError(m);
}
}