-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.c
323 lines (259 loc) · 8.08 KB
/
schema.c
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
/* schema validation for RexxXML.
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is RexxXML.
*
* The Initial Developer of the Original Code is Patrick TJ McPhee.
* Portions created by Patrick McPhee are Copyright © 2003
* Patrick TJ McPhee. All Rights Reserved.
*
* Contributors:
*
* $Header: C:/ptjm/rexx/rexxxml/RCS/schema.c 1.3 2003/10/08 02:07:56 ptjm Rel $
*/
#include "rexxxml.h"
#include <libxml/xmlschemas.h>
#include <time.h>
static RXSTRING here;
/* xsd = xmlParseSchema([url], [inline]) */
rxfunc(xmlparseschema)
{
xmlSchemaParserCtxtPtr parsectx;
xmlSchemaPtr xsd;
char * url;
checkparam(0, 2);
if (argc > 0 && argv[0].strlength) {
rxstrdup(url, argv[0]);
parsectx = xmlSchemaNewParserCtxt(url);
}
else if (argc > 1 && argv[1].strlength) {
parsectx = xmlSchemaNewMemParserCtxt(argv[1].strptr, argv[1].strlength);
}
else if (!argc && here.strlength) {
parsectx = xmlSchemaNewMemParserCtxt(here.strptr, here.strlength);
}
if (!parsectx) {
result_zero();
}
else {
xmlSchemaSetParserErrors(parsectx, rxErrorFn, rxErrorFn, NULL);
xsd = xmlSchemaParse(parsectx);
xmlSchemaFreeParserCtxt(parsectx);
if (!xsd) {
result_zero();
}
else {
bin_to_rxstring(result, xsd);
}
}
return 0;
}
/* errcode = xmlValidateDoc(schema, doc, schematype, doctype) */
static void rctoresult(PRXSTRING result, int rc)
{
# define nmtoresult(x) { \
static const char nm[] = x; \
memcpy(result->strptr, nm, sizeof(nm)-1); \
result->strlength = sizeof(nm) - 1; \
break; \
}
switch (rc) {
case XML_SCHEMAS_ERR_OK: nmtoresult("OK");
case XML_SCHEMAS_ERR_NOROOT: nmtoresult("NOROOT");
case XML_SCHEMAS_ERR_UNDECLAREDELEM: nmtoresult("UNDECLAREDELEM");
case XML_SCHEMAS_ERR_NOTTOPLEVEL: nmtoresult("NOTTOPLEVEL");
case XML_SCHEMAS_ERR_MISSING: nmtoresult("MISSING");
case XML_SCHEMAS_ERR_WRONGELEM: nmtoresult("WRONGELEM");
case XML_SCHEMAS_ERR_NOTYPE: nmtoresult("NOTYPE");
case XML_SCHEMAS_ERR_NOROLLBACK: nmtoresult("NOROLLBACK");
case XML_SCHEMAS_ERR_ISABSTRACT: nmtoresult("ISABSTRACT");
case XML_SCHEMAS_ERR_NOTEMPTY: nmtoresult("NOTEMPTY");
case XML_SCHEMAS_ERR_ELEMCONT: nmtoresult("ELEMCONT");
case XML_SCHEMAS_ERR_HAVEDEFAULT: nmtoresult("HAVEDEFAULT");
case XML_SCHEMAS_ERR_NOTNILLABLE: nmtoresult("NOTNILLABLE");
case XML_SCHEMAS_ERR_EXTRACONTENT: nmtoresult("EXTRACONTENT");
case XML_SCHEMAS_ERR_INVALIDATTR: nmtoresult("INVALIDATTR");
case XML_SCHEMAS_ERR_INVALIDELEM: nmtoresult("INVALIDELEM");
case XML_SCHEMAS_ERR_NOTDETERMINIST: nmtoresult("NOTDETERMINIST");
case XML_SCHEMAS_ERR_CONSTRUCT: nmtoresult("CONSTRUCT");
case XML_SCHEMAS_ERR_INTERNAL: nmtoresult("INTERNAL");
case XML_SCHEMAS_ERR_NOTSIMPLE: nmtoresult("NOTSIMPLE");
case XML_SCHEMAS_ERR_ATTRUNKNOWN: nmtoresult("ATTRUNKNOWN");
case XML_SCHEMAS_ERR_ATTRINVALID: nmtoresult("ATTRINVALID");
case XML_SCHEMAS_ERR_VALUE: nmtoresult("VALUE");
case XML_SCHEMAS_ERR_FACET: nmtoresult("FACET");
case XML_SCHEMAS_ERR_: nmtoresult("nil");
case XML_SCHEMAS_ERR_XXX: nmtoresult("XXX");
default: nmtoresult("unknown");
}
# undef nmtoresult
}
rxfunc(xmlvalidatedoc)
{
xmlSchemaPtr xsd;
xmlSchemaValidCtxtPtr vctxt;
xmlDocPtr doc;
int sdurl = 0, docurl = 0, rc;
checkparam(2, 4);
if (argc > 2 && argv[2].strlength > 0 && tolower(argv[2].strptr[0]) == 'u') {
char * url;
xmlSchemaParserCtxtPtr parsectx;
rxstrdup(url, argv[0]);
sdurl = 1;
parsectx = xmlSchemaNewParserCtxt(url);
if (!parsectx) {
static const char nm[] = "BADSCHEMA";
memcpy(result->strptr, nm, sizeof(nm)-1);
result->strlength = sizeof(nm) - 1;
return 0;
}
else {
xmlSchemaSetParserErrors(parsectx, rxErrorFn, rxErrorFn, NULL);
xsd = xmlSchemaParse(parsectx);
xmlSchemaFreeParserCtxt(parsectx);
}
if (!xsd) {
static const char nm[] = "INVALIDSCHEMA";
memcpy(result->strptr, nm, sizeof(nm)-1);
result->strlength = sizeof(nm) - 1;
return 0;
}
}
else {
if (argv[0].strlength != sizeof(xsd))
return BADARGS;
}
if (argc > 3 && argv[3].strlength > 0 && tolower(argv[3].strptr[0]) == 'u') {
char * document;
rxstrdup(document, argv[1]);
docurl = 1;
doc = xmlParseFile(document);
if (!doc) {
static const char nm[] = "BADDOC";
if (sdurl) {
xmlSchemaFree(xsd);
}
memcpy(result->strptr, nm, sizeof(nm)-1);
result->strlength = sizeof(nm) - 1;
return 0;
}
}
else {
if (argv[1].strlength != sizeof(doc)) {
if (sdurl)
xmlSchemaFree(xsd);
return BADARGS;
}
}
if (!sdurl)
rxstring_to_bin(xsd, argv);
if (!docurl)
rxstring_to_bin(doc, argv+1);
vctxt = xmlSchemaNewValidCtxt(xsd);
if (!vctxt) {
result_zero();
return 0;
}
xmlSchemaSetValidErrors(vctxt, rxErrorFn, rxErrorFn, NULL);
rc = xmlSchemaValidateDoc(vctxt, doc);
xmlSchemaFreeValidCtxt(vctxt);
rctoresult(result, rc);
if (sdurl)
xmlSchemaFree(xsd);
if (docurl)
xmlFreeDoc(doc);
return 0;
}
/* xmlFreeSchema xsd, [xsd2], ... */
rxfunc(xmlfreeschema)
{
register int i;
xmlSchemaPtr xsd;
for (i = 0; i < argc; i++) {
if (argv[i].strlength != sizeof(xsd))
return BADARGS;
}
for (i = 0; i < argc; i++) {
rxstring_to_bin(xsd, argv+i);
xmlSchemaFree(xsd);
}
result->strlength = 0;
return 0;
}
rxfunc(xmldumpschema)
{
xmlSchemaPtr xsd;
FILE * fp;
char * fn;
register int i;
time_t now;
struct tm *tm;
checkparam(2, -1);
for (i = 1; i < argc; i++)
if (argv[i].strlength != sizeof(xsd)) {
return BADARGS;
}
rxstrdup(fn, argv[0]);
fp = fopen(fn, "a");
if (!fp) {
result->strlength = sprintf(result->strptr, "%s: %s\n", fn, strerror(errno));
return 0;
}
for (i = 1; i < argc; i++) {
rxstring_to_bin(xsd, argv+i);
time(&now);
tm = localtime(&now);
fprintf(fp, "Dump at %04d.%02d.%02d-%02d.%02d.%02d\n",
tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec);
xmlSchemaDump(fp, xsd);
fprintf(fp, "\n\n");
}
fclose(fp);
result->strlength = 0;
return 0;
}
/* XSD environment -- this is the same as the XML and XSLT environments.
* What I really want to do allow the user to define his own environment
* xmlNewEnvironment("SPREADSHEET")
* address spreadsheet '<ss><row><col/><col/>...'
* mydoc = xmlHere("SPREADSHEET")
* but I don't see a way of knowing which environment I'm in at the
* command-processing stage, so I've just created an environment for
* each type of embedded document, and put in a one-document-at-a-time
* cap
*/
static APIRET APIENTRY hereEnv(PRXSTRING cmd, PUSHORT rc, PRXSTRING result)
{
here.strptr = realloc(here.strptr, here.strlength + 1 + cmd->strlength);
if (!here.strptr)
return NOMEMORY;
if (here.strlength)
here.strptr[here.strlength++] = '\n';
memcpy(here.strptr + here.strlength, cmd->strptr, cmd->strlength);
here.strlength += cmd->strlength;
here.strptr[here.strlength] = 0;
*rc = 0;
result->strlength = 0;
return 0;
}
void xsdinit(int init)
{
RexxRegisterSubcomExe("XSD", hereEnv, NULL);
}
void xsdfini(int init)
{
if (here.strptr) {
free(here.strptr);
here.strptr = NULL;
}
}