1 | /*
|
---|
2 | * schematron.c : implementation of the Schematron schema validity checking
|
---|
3 | *
|
---|
4 | * See Copyright for the status of this software.
|
---|
5 | *
|
---|
6 | * Daniel Veillard <[email protected]>
|
---|
7 | */
|
---|
8 |
|
---|
9 | /*
|
---|
10 | * TODO:
|
---|
11 | * + double check the semantic, especially
|
---|
12 | * - multiple rules applying in a single pattern/node
|
---|
13 | * - the semantic of libxml2 patterns vs. XSLT production referenced
|
---|
14 | * by the spec.
|
---|
15 | * + export of results in SVRL
|
---|
16 | * + full parsing and coverage of the spec, conformance of the input to the
|
---|
17 | * spec
|
---|
18 | * + divergences between the draft and the ISO proposed standard :-(
|
---|
19 | * + hook and test include
|
---|
20 | * + try and compare with the XSLT version
|
---|
21 | */
|
---|
22 |
|
---|
23 | #define IN_LIBXML
|
---|
24 | #include "libxml.h"
|
---|
25 |
|
---|
26 | #ifdef LIBXML_SCHEMATRON_ENABLED
|
---|
27 |
|
---|
28 | #include <string.h>
|
---|
29 | #include <libxml/parser.h>
|
---|
30 | #include <libxml/tree.h>
|
---|
31 | #include <libxml/uri.h>
|
---|
32 | #include <libxml/xpath.h>
|
---|
33 | #include <libxml/xpathInternals.h>
|
---|
34 | #include <libxml/pattern.h>
|
---|
35 | #include <libxml/schematron.h>
|
---|
36 |
|
---|
37 | #define SCHEMATRON_PARSE_OPTIONS XML_PARSE_NOENT
|
---|
38 |
|
---|
39 | #define SCT_OLD_NS BAD_CAST "http://www.ascc.net/xml/schematron"
|
---|
40 |
|
---|
41 | #define XML_SCHEMATRON_NS BAD_CAST "http://purl.oclc.org/dsdl/schematron"
|
---|
42 |
|
---|
43 |
|
---|
44 | static const xmlChar *xmlSchematronNs = XML_SCHEMATRON_NS;
|
---|
45 | static const xmlChar *xmlOldSchematronNs = SCT_OLD_NS;
|
---|
46 |
|
---|
47 | #define IS_SCHEMATRON(node, elem) \
|
---|
48 | ((node != NULL) && (node->type == XML_ELEMENT_NODE ) && \
|
---|
49 | (node->ns != NULL) && \
|
---|
50 | (xmlStrEqual(node->name, (const xmlChar *) elem)) && \
|
---|
51 | ((xmlStrEqual(node->ns->href, xmlSchematronNs)) || \
|
---|
52 | (xmlStrEqual(node->ns->href, xmlOldSchematronNs))))
|
---|
53 |
|
---|
54 | #define NEXT_SCHEMATRON(node) \
|
---|
55 | while (node != NULL) { \
|
---|
56 | if ((node->type == XML_ELEMENT_NODE ) && (node->ns != NULL) && \
|
---|
57 | ((xmlStrEqual(node->ns->href, xmlSchematronNs)) || \
|
---|
58 | (xmlStrEqual(node->ns->href, xmlOldSchematronNs)))) \
|
---|
59 | break; \
|
---|
60 | node = node->next; \
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * TODO:
|
---|
65 | *
|
---|
66 | * macro to flag unimplemented blocks
|
---|
67 | */
|
---|
68 | #define TODO \
|
---|
69 | xmlGenericError(xmlGenericErrorContext, \
|
---|
70 | "Unimplemented block at %s:%d\n", \
|
---|
71 | __FILE__, __LINE__);
|
---|
72 |
|
---|
73 | typedef enum {
|
---|
74 | XML_SCHEMATRON_ASSERT=1,
|
---|
75 | XML_SCHEMATRON_REPORT=2
|
---|
76 | } xmlSchematronTestType;
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * _xmlSchematronTest:
|
---|
80 | *
|
---|
81 | * A Schematrons test, either an assert or a report
|
---|
82 | */
|
---|
83 | typedef struct _xmlSchematronTest xmlSchematronTest;
|
---|
84 | typedef xmlSchematronTest *xmlSchematronTestPtr;
|
---|
85 | struct _xmlSchematronTest {
|
---|
86 | xmlSchematronTestPtr next; /* the next test in the list */
|
---|
87 | xmlSchematronTestType type; /* the test type */
|
---|
88 | xmlNodePtr node; /* the node in the tree */
|
---|
89 | xmlChar *test; /* the expression to test */
|
---|
90 | xmlXPathCompExprPtr comp; /* the compiled expression */
|
---|
91 | xmlChar *report; /* the message to report */
|
---|
92 | };
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * _xmlSchematronRule:
|
---|
96 | *
|
---|
97 | * A Schematrons rule
|
---|
98 | */
|
---|
99 | typedef struct _xmlSchematronRule xmlSchematronRule;
|
---|
100 | typedef xmlSchematronRule *xmlSchematronRulePtr;
|
---|
101 | struct _xmlSchematronRule {
|
---|
102 | xmlSchematronRulePtr next; /* the next rule in the list */
|
---|
103 | xmlSchematronRulePtr patnext;/* the next rule in the pattern list */
|
---|
104 | xmlNodePtr node; /* the node in the tree */
|
---|
105 | xmlChar *context; /* the context evaluation rule */
|
---|
106 | xmlSchematronTestPtr tests; /* the list of tests */
|
---|
107 | xmlPatternPtr pattern; /* the compiled pattern associated */
|
---|
108 | xmlChar *report; /* the message to report */
|
---|
109 | };
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * _xmlSchematronPattern:
|
---|
113 | *
|
---|
114 | * A Schematrons pattern
|
---|
115 | */
|
---|
116 | typedef struct _xmlSchematronPattern xmlSchematronPattern;
|
---|
117 | typedef xmlSchematronPattern *xmlSchematronPatternPtr;
|
---|
118 | struct _xmlSchematronPattern {
|
---|
119 | xmlSchematronPatternPtr next;/* the next pattern in the list */
|
---|
120 | xmlSchematronRulePtr rules; /* the list of rules */
|
---|
121 | xmlChar *name; /* the name of the pattern */
|
---|
122 | };
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * _xmlSchematron:
|
---|
126 | *
|
---|
127 | * A Schematrons definition
|
---|
128 | */
|
---|
129 | struct _xmlSchematron {
|
---|
130 | const xmlChar *name; /* schema name */
|
---|
131 | int preserve; /* was the document passed by the user */
|
---|
132 | xmlDocPtr doc; /* pointer to the parsed document */
|
---|
133 | int flags; /* specific to this schematron */
|
---|
134 |
|
---|
135 | void *_private; /* unused by the library */
|
---|
136 | xmlDictPtr dict; /* the dictionnary used internally */
|
---|
137 |
|
---|
138 | const xmlChar *title; /* the title if any */
|
---|
139 |
|
---|
140 | int nbNs; /* the number of namespaces */
|
---|
141 |
|
---|
142 | int nbPattern; /* the number of patterns */
|
---|
143 | xmlSchematronPatternPtr patterns;/* the patterns found */
|
---|
144 | xmlSchematronRulePtr rules; /* the rules gathered */
|
---|
145 | int nbNamespaces; /* number of namespaces in the array */
|
---|
146 | int maxNamespaces; /* size of the array */
|
---|
147 | const xmlChar **namespaces; /* the array of namespaces */
|
---|
148 | };
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * xmlSchematronValidCtxt:
|
---|
152 | *
|
---|
153 | * A Schematrons validation context
|
---|
154 | */
|
---|
155 | struct _xmlSchematronValidCtxt {
|
---|
156 | int type;
|
---|
157 | int flags; /* an or of xmlSchematronValidOptions */
|
---|
158 |
|
---|
159 | xmlDictPtr dict;
|
---|
160 | int nberrors;
|
---|
161 | int err;
|
---|
162 |
|
---|
163 | xmlSchematronPtr schema;
|
---|
164 | xmlXPathContextPtr xctxt;
|
---|
165 |
|
---|
166 | FILE *outputFile; /* if using XML_SCHEMATRON_OUT_FILE */
|
---|
167 | xmlBufferPtr outputBuffer; /* if using XML_SCHEMATRON_OUT_BUFFER */
|
---|
168 | xmlOutputWriteCallback iowrite; /* if using XML_SCHEMATRON_OUT_IO */
|
---|
169 | xmlOutputCloseCallback ioclose;
|
---|
170 | void *ioctx;
|
---|
171 | };
|
---|
172 |
|
---|
173 | struct _xmlSchematronParserCtxt {
|
---|
174 | int type;
|
---|
175 | const xmlChar *URL;
|
---|
176 | xmlDocPtr doc;
|
---|
177 | int preserve; /* Whether the doc should be freed */
|
---|
178 | const char *buffer;
|
---|
179 | int size;
|
---|
180 |
|
---|
181 | xmlDictPtr dict; /* dictionnary for interned string names */
|
---|
182 |
|
---|
183 | int nberrors;
|
---|
184 | int err;
|
---|
185 | xmlXPathContextPtr xctxt; /* the XPath context used for compilation */
|
---|
186 | xmlSchematronPtr schema;
|
---|
187 |
|
---|
188 | int nbNamespaces; /* number of namespaces in the array */
|
---|
189 | int maxNamespaces; /* size of the array */
|
---|
190 | const xmlChar **namespaces; /* the array of namespaces */
|
---|
191 |
|
---|
192 | int nbIncludes; /* number of includes in the array */
|
---|
193 | int maxIncludes; /* size of the array */
|
---|
194 | xmlNodePtr *includes; /* the array of includes */
|
---|
195 |
|
---|
196 | /* error rreporting data */
|
---|
197 | void *userData; /* user specific data block */
|
---|
198 | xmlSchematronValidityErrorFunc error;/* the callback in case of errors */
|
---|
199 | xmlSchematronValidityWarningFunc warning;/* callback in case of warning */
|
---|
200 | xmlStructuredErrorFunc serror; /* the structured function */
|
---|
201 |
|
---|
202 | };
|
---|
203 |
|
---|
204 | #define XML_STRON_CTXT_PARSER 1
|
---|
205 | #define XML_STRON_CTXT_VALIDATOR 2
|
---|
206 |
|
---|
207 | /************************************************************************
|
---|
208 | * *
|
---|
209 | * Error reporting *
|
---|
210 | * *
|
---|
211 | ************************************************************************/
|
---|
212 |
|
---|
213 | /**
|
---|
214 | * xmlSchematronPErrMemory:
|
---|
215 | * @node: a context node
|
---|
216 | * @extra: extra informations
|
---|
217 | *
|
---|
218 | * Handle an out of memory condition
|
---|
219 | */
|
---|
220 | static void
|
---|
221 | xmlSchematronPErrMemory(xmlSchematronParserCtxtPtr ctxt,
|
---|
222 | const char *extra, xmlNodePtr node)
|
---|
223 | {
|
---|
224 | if (ctxt != NULL)
|
---|
225 | ctxt->nberrors++;
|
---|
226 | __xmlSimpleError(XML_FROM_SCHEMASP, XML_ERR_NO_MEMORY, node, NULL,
|
---|
227 | extra);
|
---|
228 | }
|
---|
229 |
|
---|
230 | /**
|
---|
231 | * xmlSchematronPErr:
|
---|
232 | * @ctxt: the parsing context
|
---|
233 | * @node: the context node
|
---|
234 | * @error: the error code
|
---|
235 | * @msg: the error message
|
---|
236 | * @str1: extra data
|
---|
237 | * @str2: extra data
|
---|
238 | *
|
---|
239 | * Handle a parser error
|
---|
240 | */
|
---|
241 | static void
|
---|
242 | xmlSchematronPErr(xmlSchematronParserCtxtPtr ctxt, xmlNodePtr node, int error,
|
---|
243 | const char *msg, const xmlChar * str1, const xmlChar * str2)
|
---|
244 | {
|
---|
245 | xmlGenericErrorFunc channel = NULL;
|
---|
246 | xmlStructuredErrorFunc schannel = NULL;
|
---|
247 | void *data = NULL;
|
---|
248 |
|
---|
249 | if (ctxt != NULL) {
|
---|
250 | ctxt->nberrors++;
|
---|
251 | channel = ctxt->error;
|
---|
252 | data = ctxt->userData;
|
---|
253 | schannel = ctxt->serror;
|
---|
254 | }
|
---|
255 | __xmlRaiseError(schannel, channel, data, ctxt, node, XML_FROM_SCHEMASP,
|
---|
256 | error, XML_ERR_ERROR, NULL, 0,
|
---|
257 | (const char *) str1, (const char *) str2, NULL, 0, 0,
|
---|
258 | msg, str1, str2);
|
---|
259 | }
|
---|
260 |
|
---|
261 | /**
|
---|
262 | * xmlSchematronVTypeErrMemory:
|
---|
263 | * @node: a context node
|
---|
264 | * @extra: extra informations
|
---|
265 | *
|
---|
266 | * Handle an out of memory condition
|
---|
267 | */
|
---|
268 | static void
|
---|
269 | xmlSchematronVErrMemory(xmlSchematronValidCtxtPtr ctxt,
|
---|
270 | const char *extra, xmlNodePtr node)
|
---|
271 | {
|
---|
272 | if (ctxt != NULL) {
|
---|
273 | ctxt->nberrors++;
|
---|
274 | ctxt->err = XML_SCHEMAV_INTERNAL;
|
---|
275 | }
|
---|
276 | __xmlSimpleError(XML_FROM_SCHEMASV, XML_ERR_NO_MEMORY, node, NULL,
|
---|
277 | extra);
|
---|
278 | }
|
---|
279 |
|
---|
280 | /************************************************************************
|
---|
281 | * *
|
---|
282 | * Parsing and compilation of the Schematrontrons *
|
---|
283 | * *
|
---|
284 | ************************************************************************/
|
---|
285 |
|
---|
286 | /**
|
---|
287 | * xmlSchematronAddTest:
|
---|
288 | * @ctxt: the schema parsing context
|
---|
289 | * @type: the type of test
|
---|
290 | * @rule: the parent rule
|
---|
291 | * @node: the node hosting the test
|
---|
292 | * @test: the associated test
|
---|
293 | * @report: the associated report string
|
---|
294 | *
|
---|
295 | * Add a test to a schematron
|
---|
296 | *
|
---|
297 | * Returns the new pointer or NULL in case of error
|
---|
298 | */
|
---|
299 | static xmlSchematronTestPtr
|
---|
300 | xmlSchematronAddTest(xmlSchematronParserCtxtPtr ctxt,
|
---|
301 | xmlSchematronTestType type,
|
---|
302 | xmlSchematronRulePtr rule,
|
---|
303 | xmlNodePtr node, xmlChar *test, xmlChar *report)
|
---|
304 | {
|
---|
305 | xmlSchematronTestPtr ret;
|
---|
306 | xmlXPathCompExprPtr comp;
|
---|
307 |
|
---|
308 | if ((ctxt == NULL) || (rule == NULL) || (node == NULL) ||
|
---|
309 | (test == NULL))
|
---|
310 | return(NULL);
|
---|
311 |
|
---|
312 | /*
|
---|
313 | * try first to compile the test expression
|
---|
314 | */
|
---|
315 | comp = xmlXPathCtxtCompile(ctxt->xctxt, test);
|
---|
316 | if (comp == NULL) {
|
---|
317 | xmlSchematronPErr(ctxt, node,
|
---|
318 | XML_SCHEMAP_NOROOT,
|
---|
319 | "Failed to compile test expression %s",
|
---|
320 | test, NULL);
|
---|
321 | return(NULL);
|
---|
322 | }
|
---|
323 |
|
---|
324 | ret = (xmlSchematronTestPtr) xmlMalloc(sizeof(xmlSchematronTest));
|
---|
325 | if (ret == NULL) {
|
---|
326 | xmlSchematronPErrMemory(ctxt, "allocating schema test", node);
|
---|
327 | return (NULL);
|
---|
328 | }
|
---|
329 | memset(ret, 0, sizeof(xmlSchematronTest));
|
---|
330 | ret->type = type;
|
---|
331 | ret->node = node;
|
---|
332 | ret->test = test;
|
---|
333 | ret->comp = comp;
|
---|
334 | ret->report = report;
|
---|
335 | ret->next = NULL;
|
---|
336 | if (rule->tests == NULL) {
|
---|
337 | rule->tests = ret;
|
---|
338 | } else {
|
---|
339 | xmlSchematronTestPtr prev = rule->tests;
|
---|
340 |
|
---|
341 | while (prev->next != NULL)
|
---|
342 | prev = prev->next;
|
---|
343 | prev->next = ret;
|
---|
344 | }
|
---|
345 | return (ret);
|
---|
346 | }
|
---|
347 |
|
---|
348 | /**
|
---|
349 | * xmlSchematronFreeTests:
|
---|
350 | * @tests: a list of tests
|
---|
351 | *
|
---|
352 | * Free a list of tests.
|
---|
353 | */
|
---|
354 | static void
|
---|
355 | xmlSchematronFreeTests(xmlSchematronTestPtr tests) {
|
---|
356 | xmlSchematronTestPtr next;
|
---|
357 |
|
---|
358 | while (tests != NULL) {
|
---|
359 | next = tests->next;
|
---|
360 | if (tests->test != NULL)
|
---|
361 | xmlFree(tests->test);
|
---|
362 | if (tests->comp != NULL)
|
---|
363 | xmlXPathFreeCompExpr(tests->comp);
|
---|
364 | if (tests->report != NULL)
|
---|
365 | xmlFree(tests->report);
|
---|
366 | xmlFree(tests);
|
---|
367 | tests = next;
|
---|
368 | }
|
---|
369 | }
|
---|
370 |
|
---|
371 | /**
|
---|
372 | * xmlSchematronAddRule:
|
---|
373 | * @ctxt: the schema parsing context
|
---|
374 | * @schema: a schema structure
|
---|
375 | * @node: the node hosting the rule
|
---|
376 | * @context: the associated context string
|
---|
377 | * @report: the associated report string
|
---|
378 | *
|
---|
379 | * Add a rule to a schematron
|
---|
380 | *
|
---|
381 | * Returns the new pointer or NULL in case of error
|
---|
382 | */
|
---|
383 | static xmlSchematronRulePtr
|
---|
384 | xmlSchematronAddRule(xmlSchematronParserCtxtPtr ctxt, xmlSchematronPtr schema,
|
---|
385 | xmlSchematronPatternPtr pat, xmlNodePtr node,
|
---|
386 | xmlChar *context, xmlChar *report)
|
---|
387 | {
|
---|
388 | xmlSchematronRulePtr ret;
|
---|
389 | xmlPatternPtr pattern;
|
---|
390 |
|
---|
391 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL) ||
|
---|
392 | (context == NULL))
|
---|
393 | return(NULL);
|
---|
394 |
|
---|
395 | /*
|
---|
396 | * Try first to compile the pattern
|
---|
397 | */
|
---|
398 | pattern = xmlPatterncompile(context, ctxt->dict, XML_PATTERN_XPATH,
|
---|
399 | ctxt->namespaces);
|
---|
400 | if (pattern == NULL) {
|
---|
401 | xmlSchematronPErr(ctxt, node,
|
---|
402 | XML_SCHEMAP_NOROOT,
|
---|
403 | "Failed to compile context expression %s",
|
---|
404 | context, NULL);
|
---|
405 | }
|
---|
406 |
|
---|
407 | ret = (xmlSchematronRulePtr) xmlMalloc(sizeof(xmlSchematronRule));
|
---|
408 | if (ret == NULL) {
|
---|
409 | xmlSchematronPErrMemory(ctxt, "allocating schema rule", node);
|
---|
410 | return (NULL);
|
---|
411 | }
|
---|
412 | memset(ret, 0, sizeof(xmlSchematronRule));
|
---|
413 | ret->node = node;
|
---|
414 | ret->context = context;
|
---|
415 | ret->pattern = pattern;
|
---|
416 | ret->report = report;
|
---|
417 | ret->next = NULL;
|
---|
418 | if (schema->rules == NULL) {
|
---|
419 | schema->rules = ret;
|
---|
420 | } else {
|
---|
421 | xmlSchematronRulePtr prev = schema->rules;
|
---|
422 |
|
---|
423 | while (prev->next != NULL)
|
---|
424 | prev = prev->next;
|
---|
425 | prev->next = ret;
|
---|
426 | }
|
---|
427 | ret->patnext = NULL;
|
---|
428 | if (pat->rules == NULL) {
|
---|
429 | pat->rules = ret;
|
---|
430 | } else {
|
---|
431 | xmlSchematronRulePtr prev = pat->rules;
|
---|
432 |
|
---|
433 | while (prev->patnext != NULL)
|
---|
434 | prev = prev->patnext;
|
---|
435 | prev->patnext = ret;
|
---|
436 | }
|
---|
437 | return (ret);
|
---|
438 | }
|
---|
439 |
|
---|
440 | /**
|
---|
441 | * xmlSchematronFreeRules:
|
---|
442 | * @rules: a list of rules
|
---|
443 | *
|
---|
444 | * Free a list of rules.
|
---|
445 | */
|
---|
446 | static void
|
---|
447 | xmlSchematronFreeRules(xmlSchematronRulePtr rules) {
|
---|
448 | xmlSchematronRulePtr next;
|
---|
449 |
|
---|
450 | while (rules != NULL) {
|
---|
451 | next = rules->next;
|
---|
452 | if (rules->tests)
|
---|
453 | xmlSchematronFreeTests(rules->tests);
|
---|
454 | if (rules->context != NULL)
|
---|
455 | xmlFree(rules->context);
|
---|
456 | if (rules->pattern)
|
---|
457 | xmlFreePattern(rules->pattern);
|
---|
458 | if (rules->report != NULL)
|
---|
459 | xmlFree(rules->report);
|
---|
460 | xmlFree(rules);
|
---|
461 | rules = next;
|
---|
462 | }
|
---|
463 | }
|
---|
464 |
|
---|
465 | /**
|
---|
466 | * xmlSchematronAddPattern:
|
---|
467 | * @ctxt: the schema parsing context
|
---|
468 | * @schema: a schema structure
|
---|
469 | * @node: the node hosting the pattern
|
---|
470 | * @id: the id or name of the pattern
|
---|
471 | *
|
---|
472 | * Add a pattern to a schematron
|
---|
473 | *
|
---|
474 | * Returns the new pointer or NULL in case of error
|
---|
475 | */
|
---|
476 | static xmlSchematronPatternPtr
|
---|
477 | xmlSchematronAddPattern(xmlSchematronParserCtxtPtr ctxt,
|
---|
478 | xmlSchematronPtr schema, xmlNodePtr node, xmlChar *name)
|
---|
479 | {
|
---|
480 | xmlSchematronPatternPtr ret;
|
---|
481 |
|
---|
482 | if ((ctxt == NULL) || (schema == NULL) || (node == NULL) || (name == NULL))
|
---|
483 | return(NULL);
|
---|
484 |
|
---|
485 | ret = (xmlSchematronPatternPtr) xmlMalloc(sizeof(xmlSchematronPattern));
|
---|
486 | if (ret == NULL) {
|
---|
487 | xmlSchematronPErrMemory(ctxt, "allocating schema pattern", node);
|
---|
488 | return (NULL);
|
---|
489 | }
|
---|
490 | memset(ret, 0, sizeof(xmlSchematronPattern));
|
---|
491 | ret->name = name;
|
---|
492 | ret->next = NULL;
|
---|
493 | if (schema->patterns == NULL) {
|
---|
494 | schema->patterns = ret;
|
---|
495 | } else {
|
---|
496 | xmlSchematronPatternPtr prev = schema->patterns;
|
---|
497 |
|
---|
498 | while (prev->next != NULL)
|
---|
499 | prev = prev->next;
|
---|
500 | prev->next = ret;
|
---|
501 | }
|
---|
502 | return (ret);
|
---|
503 | }
|
---|
504 |
|
---|
505 | /**
|
---|
506 | * xmlSchematronFreePatterns:
|
---|
507 | * @patterns: a list of patterns
|
---|
508 | *
|
---|
509 | * Free a list of patterns.
|
---|
510 | */
|
---|
511 | static void
|
---|
512 | xmlSchematronFreePatterns(xmlSchematronPatternPtr patterns) {
|
---|
513 | xmlSchematronPatternPtr next;
|
---|
514 |
|
---|
515 | while (patterns != NULL) {
|
---|
516 | next = patterns->next;
|
---|
517 | if (patterns->name != NULL)
|
---|
518 | xmlFree(patterns->name);
|
---|
519 | xmlFree(patterns);
|
---|
520 | patterns = next;
|
---|
521 | }
|
---|
522 | }
|
---|
523 |
|
---|
524 | /**
|
---|
525 | * xmlSchematronNewSchematron:
|
---|
526 | * @ctxt: a schema validation context
|
---|
527 | *
|
---|
528 | * Allocate a new Schematron structure.
|
---|
529 | *
|
---|
530 | * Returns the newly allocated structure or NULL in case or error
|
---|
531 | */
|
---|
532 | static xmlSchematronPtr
|
---|
533 | xmlSchematronNewSchematron(xmlSchematronParserCtxtPtr ctxt)
|
---|
534 | {
|
---|
535 | xmlSchematronPtr ret;
|
---|
536 |
|
---|
537 | ret = (xmlSchematronPtr) xmlMalloc(sizeof(xmlSchematron));
|
---|
538 | if (ret == NULL) {
|
---|
539 | xmlSchematronPErrMemory(ctxt, "allocating schema", NULL);
|
---|
540 | return (NULL);
|
---|
541 | }
|
---|
542 | memset(ret, 0, sizeof(xmlSchematron));
|
---|
543 | ret->dict = ctxt->dict;
|
---|
544 | xmlDictReference(ret->dict);
|
---|
545 |
|
---|
546 | return (ret);
|
---|
547 | }
|
---|
548 |
|
---|
549 | /**
|
---|
550 | * xmlSchematronFree:
|
---|
551 | * @schema: a schema structure
|
---|
552 | *
|
---|
553 | * Deallocate a Schematron structure.
|
---|
554 | */
|
---|
555 | void
|
---|
556 | xmlSchematronFree(xmlSchematronPtr schema)
|
---|
557 | {
|
---|
558 | if (schema == NULL)
|
---|
559 | return;
|
---|
560 |
|
---|
561 | if ((schema->doc != NULL) && (!(schema->preserve)))
|
---|
562 | xmlFreeDoc(schema->doc);
|
---|
563 |
|
---|
564 | if (schema->namespaces != NULL)
|
---|
565 | xmlFree((char **) schema->namespaces);
|
---|
566 |
|
---|
567 | xmlSchematronFreeRules(schema->rules);
|
---|
568 | xmlSchematronFreePatterns(schema->patterns);
|
---|
569 | xmlDictFree(schema->dict);
|
---|
570 | xmlFree(schema);
|
---|
571 | }
|
---|
572 |
|
---|
573 | /**
|
---|
574 | * xmlSchematronNewParserCtxt:
|
---|
575 | * @URL: the location of the schema
|
---|
576 | *
|
---|
577 | * Create an XML Schematrons parse context for that file/resource expected
|
---|
578 | * to contain an XML Schematrons file.
|
---|
579 | *
|
---|
580 | * Returns the parser context or NULL in case of error
|
---|
581 | */
|
---|
582 | xmlSchematronParserCtxtPtr
|
---|
583 | xmlSchematronNewParserCtxt(const char *URL)
|
---|
584 | {
|
---|
585 | xmlSchematronParserCtxtPtr ret;
|
---|
586 |
|
---|
587 | if (URL == NULL)
|
---|
588 | return (NULL);
|
---|
589 |
|
---|
590 | ret =
|
---|
591 | (xmlSchematronParserCtxtPtr)
|
---|
592 | xmlMalloc(sizeof(xmlSchematronParserCtxt));
|
---|
593 | if (ret == NULL) {
|
---|
594 | xmlSchematronPErrMemory(NULL, "allocating schema parser context",
|
---|
595 | NULL);
|
---|
596 | return (NULL);
|
---|
597 | }
|
---|
598 | memset(ret, 0, sizeof(xmlSchematronParserCtxt));
|
---|
599 | ret->type = XML_STRON_CTXT_PARSER;
|
---|
600 | ret->dict = xmlDictCreate();
|
---|
601 | ret->URL = xmlDictLookup(ret->dict, (const xmlChar *) URL, -1);
|
---|
602 | ret->includes = NULL;
|
---|
603 | ret->xctxt = xmlXPathNewContext(NULL);
|
---|
604 | if (ret->xctxt == NULL) {
|
---|
605 | xmlSchematronPErrMemory(NULL, "allocating schema parser XPath context",
|
---|
606 | NULL);
|
---|
607 | xmlSchematronFreeParserCtxt(ret);
|
---|
608 | return (NULL);
|
---|
609 | }
|
---|
610 | ret->xctxt->flags = XML_XPATH_CHECKNS;
|
---|
611 | return (ret);
|
---|
612 | }
|
---|
613 |
|
---|
614 | /**
|
---|
615 | * xmlSchematronNewMemParserCtxt:
|
---|
616 | * @buffer: a pointer to a char array containing the schemas
|
---|
617 | * @size: the size of the array
|
---|
618 | *
|
---|
619 | * Create an XML Schematrons parse context for that memory buffer expected
|
---|
620 | * to contain an XML Schematrons file.
|
---|
621 | *
|
---|
622 | * Returns the parser context or NULL in case of error
|
---|
623 | */
|
---|
624 | xmlSchematronParserCtxtPtr
|
---|
625 | xmlSchematronNewMemParserCtxt(const char *buffer, int size)
|
---|
626 | {
|
---|
627 | xmlSchematronParserCtxtPtr ret;
|
---|
628 |
|
---|
629 | if ((buffer == NULL) || (size <= 0))
|
---|
630 | return (NULL);
|
---|
631 |
|
---|
632 | ret =
|
---|
633 | (xmlSchematronParserCtxtPtr)
|
---|
634 | xmlMalloc(sizeof(xmlSchematronParserCtxt));
|
---|
635 | if (ret == NULL) {
|
---|
636 | xmlSchematronPErrMemory(NULL, "allocating schema parser context",
|
---|
637 | NULL);
|
---|
638 | return (NULL);
|
---|
639 | }
|
---|
640 | memset(ret, 0, sizeof(xmlSchematronParserCtxt));
|
---|
641 | ret->buffer = buffer;
|
---|
642 | ret->size = size;
|
---|
643 | ret->dict = xmlDictCreate();
|
---|
644 | ret->xctxt = xmlXPathNewContext(NULL);
|
---|
645 | if (ret->xctxt == NULL) {
|
---|
646 | xmlSchematronPErrMemory(NULL, "allocating schema parser XPath context",
|
---|
647 | NULL);
|
---|
648 | xmlSchematronFreeParserCtxt(ret);
|
---|
649 | return (NULL);
|
---|
650 | }
|
---|
651 | return (ret);
|
---|
652 | }
|
---|
653 |
|
---|
654 | /**
|
---|
655 | * xmlSchematronNewDocParserCtxt:
|
---|
656 | * @doc: a preparsed document tree
|
---|
657 | *
|
---|
658 | * Create an XML Schematrons parse context for that document.
|
---|
659 | * NB. The document may be modified during the parsing process.
|
---|
660 | *
|
---|
661 | * Returns the parser context or NULL in case of error
|
---|
662 | */
|
---|
663 | xmlSchematronParserCtxtPtr
|
---|
664 | xmlSchematronNewDocParserCtxt(xmlDocPtr doc)
|
---|
665 | {
|
---|
666 | xmlSchematronParserCtxtPtr ret;
|
---|
667 |
|
---|
668 | if (doc == NULL)
|
---|
669 | return (NULL);
|
---|
670 |
|
---|
671 | ret =
|
---|
672 | (xmlSchematronParserCtxtPtr)
|
---|
673 | xmlMalloc(sizeof(xmlSchematronParserCtxt));
|
---|
674 | if (ret == NULL) {
|
---|
675 | xmlSchematronPErrMemory(NULL, "allocating schema parser context",
|
---|
676 | NULL);
|
---|
677 | return (NULL);
|
---|
678 | }
|
---|
679 | memset(ret, 0, sizeof(xmlSchematronParserCtxt));
|
---|
680 | ret->doc = doc;
|
---|
681 | ret->dict = xmlDictCreate();
|
---|
682 | /* The application has responsibility for the document */
|
---|
683 | ret->preserve = 1;
|
---|
684 | ret->xctxt = xmlXPathNewContext(doc);
|
---|
685 | if (ret->xctxt == NULL) {
|
---|
686 | xmlSchematronPErrMemory(NULL, "allocating schema parser XPath context",
|
---|
687 | NULL);
|
---|
688 | xmlSchematronFreeParserCtxt(ret);
|
---|
689 | return (NULL);
|
---|
690 | }
|
---|
691 |
|
---|
692 | return (ret);
|
---|
693 | }
|
---|
694 |
|
---|
695 | /**
|
---|
696 | * xmlSchematronFreeParserCtxt:
|
---|
697 | * @ctxt: the schema parser context
|
---|
698 | *
|
---|
699 | * Free the resources associated to the schema parser context
|
---|
700 | */
|
---|
701 | void
|
---|
702 | xmlSchematronFreeParserCtxt(xmlSchematronParserCtxtPtr ctxt)
|
---|
703 | {
|
---|
704 | if (ctxt == NULL)
|
---|
705 | return;
|
---|
706 | if (ctxt->doc != NULL && !ctxt->preserve)
|
---|
707 | xmlFreeDoc(ctxt->doc);
|
---|
708 | if (ctxt->xctxt != NULL) {
|
---|
709 | xmlXPathFreeContext(ctxt->xctxt);
|
---|
710 | }
|
---|
711 | if (ctxt->namespaces != NULL)
|
---|
712 | xmlFree((char **) ctxt->namespaces);
|
---|
713 | xmlDictFree(ctxt->dict);
|
---|
714 | xmlFree(ctxt);
|
---|
715 | }
|
---|
716 |
|
---|
717 | #if 0
|
---|
718 | /**
|
---|
719 | * xmlSchematronPushInclude:
|
---|
720 | * @ctxt: the schema parser context
|
---|
721 | * @doc: the included document
|
---|
722 | * @cur: the current include node
|
---|
723 | *
|
---|
724 | * Add an included document
|
---|
725 | */
|
---|
726 | static void
|
---|
727 | xmlSchematronPushInclude(xmlSchematronParserCtxtPtr ctxt,
|
---|
728 | xmlDocPtr doc, xmlNodePtr cur)
|
---|
729 | {
|
---|
730 | if (ctxt->includes == NULL) {
|
---|
731 | ctxt->maxIncludes = 10;
|
---|
732 | ctxt->includes = (xmlNodePtr *)
|
---|
733 | xmlMalloc(ctxt->maxIncludes * 2 * sizeof(xmlNodePtr));
|
---|
734 | if (ctxt->includes == NULL) {
|
---|
735 | xmlSchematronPErrMemory(NULL, "allocating parser includes",
|
---|
736 | NULL);
|
---|
737 | return;
|
---|
738 | }
|
---|
739 | ctxt->nbIncludes = 0;
|
---|
740 | } else if (ctxt->nbIncludes + 2 >= ctxt->maxIncludes) {
|
---|
741 | xmlNodePtr *tmp;
|
---|
742 |
|
---|
743 | tmp = (xmlNodePtr *)
|
---|
744 | xmlRealloc(ctxt->includes, ctxt->maxIncludes * 4 *
|
---|
745 | sizeof(xmlNodePtr));
|
---|
746 | if (tmp == NULL) {
|
---|
747 | xmlSchematronPErrMemory(NULL, "allocating parser includes",
|
---|
748 | NULL);
|
---|
749 | return;
|
---|
750 | }
|
---|
751 | ctxt->includes = tmp;
|
---|
752 | ctxt->maxIncludes *= 2;
|
---|
753 | }
|
---|
754 | ctxt->includes[2 * ctxt->nbIncludes] = cur;
|
---|
755 | ctxt->includes[2 * ctxt->nbIncludes + 1] = (xmlNodePtr) doc;
|
---|
756 | ctxt->nbIncludes++;
|
---|
757 | }
|
---|
758 |
|
---|
759 | /**
|
---|
760 | * xmlSchematronPopInclude:
|
---|
761 | * @ctxt: the schema parser context
|
---|
762 | *
|
---|
763 | * Pop an include level. The included document is being freed
|
---|
764 | *
|
---|
765 | * Returns the node immediately following the include or NULL if the
|
---|
766 | * include list was empty.
|
---|
767 | */
|
---|
768 | static xmlNodePtr
|
---|
769 | xmlSchematronPopInclude(xmlSchematronParserCtxtPtr ctxt)
|
---|
770 | {
|
---|
771 | xmlDocPtr doc;
|
---|
772 | xmlNodePtr ret;
|
---|
773 |
|
---|
774 | if (ctxt->nbIncludes <= 0)
|
---|
775 | return(NULL);
|
---|
776 | ctxt->nbIncludes--;
|
---|
777 | doc = (xmlDocPtr) ctxt->includes[2 * ctxt->nbIncludes + 1];
|
---|
778 | ret = ctxt->includes[2 * ctxt->nbIncludes];
|
---|
779 | xmlFreeDoc(doc);
|
---|
780 | if (ret != NULL)
|
---|
781 | ret = ret->next;
|
---|
782 | if (ret == NULL)
|
---|
783 | return(xmlSchematronPopInclude(ctxt));
|
---|
784 | return(ret);
|
---|
785 | }
|
---|
786 | #endif
|
---|
787 |
|
---|
788 | /**
|
---|
789 | * xmlSchematronAddNamespace:
|
---|
790 | * @ctxt: the schema parser context
|
---|
791 | * @prefix: the namespace prefix
|
---|
792 | * @ns: the namespace name
|
---|
793 | *
|
---|
794 | * Add a namespace definition in the context
|
---|
795 | */
|
---|
796 | static void
|
---|
797 | xmlSchematronAddNamespace(xmlSchematronParserCtxtPtr ctxt,
|
---|
798 | const xmlChar *prefix, const xmlChar *ns)
|
---|
799 | {
|
---|
800 | if (ctxt->namespaces == NULL) {
|
---|
801 | ctxt->maxNamespaces = 10;
|
---|
802 | ctxt->namespaces = (const xmlChar **)
|
---|
803 | xmlMalloc(ctxt->maxNamespaces * 2 * sizeof(const xmlChar *));
|
---|
804 | if (ctxt->namespaces == NULL) {
|
---|
805 | xmlSchematronPErrMemory(NULL, "allocating parser namespaces",
|
---|
806 | NULL);
|
---|
807 | return;
|
---|
808 | }
|
---|
809 | ctxt->nbNamespaces = 0;
|
---|
810 | } else if (ctxt->nbNamespaces + 2 >= ctxt->maxNamespaces) {
|
---|
811 | const xmlChar **tmp;
|
---|
812 |
|
---|
813 | tmp = (const xmlChar **)
|
---|
814 | xmlRealloc((xmlChar **) ctxt->namespaces, ctxt->maxNamespaces * 4 *
|
---|
815 | sizeof(const xmlChar *));
|
---|
816 | if (tmp == NULL) {
|
---|
817 | xmlSchematronPErrMemory(NULL, "allocating parser namespaces",
|
---|
818 | NULL);
|
---|
819 | return;
|
---|
820 | }
|
---|
821 | ctxt->namespaces = tmp;
|
---|
822 | ctxt->maxNamespaces *= 2;
|
---|
823 | }
|
---|
824 | ctxt->namespaces[2 * ctxt->nbNamespaces] =
|
---|
825 | xmlDictLookup(ctxt->dict, ns, -1);
|
---|
826 | ctxt->namespaces[2 * ctxt->nbNamespaces + 1] =
|
---|
827 | xmlDictLookup(ctxt->dict, prefix, -1);
|
---|
828 | ctxt->nbNamespaces++;
|
---|
829 | ctxt->namespaces[2 * ctxt->nbNamespaces] = NULL;
|
---|
830 | ctxt->namespaces[2 * ctxt->nbNamespaces + 1] = NULL;
|
---|
831 |
|
---|
832 | }
|
---|
833 |
|
---|
834 | /**
|
---|
835 | * xmlSchematronParseRule:
|
---|
836 | * @ctxt: a schema validation context
|
---|
837 | * @rule: the rule node
|
---|
838 | *
|
---|
839 | * parse a rule element
|
---|
840 | */
|
---|
841 | static void
|
---|
842 | xmlSchematronParseRule(xmlSchematronParserCtxtPtr ctxt,
|
---|
843 | xmlSchematronPatternPtr pattern,
|
---|
844 | xmlNodePtr rule)
|
---|
845 | {
|
---|
846 | xmlNodePtr cur;
|
---|
847 | int nbChecks = 0;
|
---|
848 | xmlChar *test;
|
---|
849 | xmlChar *context;
|
---|
850 | xmlChar *report;
|
---|
851 | xmlSchematronRulePtr ruleptr;
|
---|
852 | xmlSchematronTestPtr testptr;
|
---|
853 |
|
---|
854 | if ((ctxt == NULL) || (rule == NULL)) return;
|
---|
855 |
|
---|
856 | context = xmlGetNoNsProp(rule, BAD_CAST "context");
|
---|
857 | if (context == NULL) {
|
---|
858 | xmlSchematronPErr(ctxt, rule,
|
---|
859 | XML_SCHEMAP_NOROOT,
|
---|
860 | "rule has no context attribute",
|
---|
861 | NULL, NULL);
|
---|
862 | return;
|
---|
863 | } else if (context[0] == 0) {
|
---|
864 | xmlSchematronPErr(ctxt, rule,
|
---|
865 | XML_SCHEMAP_NOROOT,
|
---|
866 | "rule has an empty context attribute",
|
---|
867 | NULL, NULL);
|
---|
868 | xmlFree(context);
|
---|
869 | return;
|
---|
870 | } else {
|
---|
871 | ruleptr = xmlSchematronAddRule(ctxt, ctxt->schema, pattern,
|
---|
872 | rule, context, NULL);
|
---|
873 | if (ruleptr == NULL) {
|
---|
874 | xmlFree(context);
|
---|
875 | return;
|
---|
876 | }
|
---|
877 | }
|
---|
878 |
|
---|
879 | cur = rule->children;
|
---|
880 | NEXT_SCHEMATRON(cur);
|
---|
881 | while (cur != NULL) {
|
---|
882 | if (IS_SCHEMATRON(cur, "assert")) {
|
---|
883 | nbChecks++;
|
---|
884 | test = xmlGetNoNsProp(cur, BAD_CAST "test");
|
---|
885 | if (test == NULL) {
|
---|
886 | xmlSchematronPErr(ctxt, cur,
|
---|
887 | XML_SCHEMAP_NOROOT,
|
---|
888 | "assert has no test attribute",
|
---|
889 | NULL, NULL);
|
---|
890 | } else if (test[0] == 0) {
|
---|
891 | xmlSchematronPErr(ctxt, cur,
|
---|
892 | XML_SCHEMAP_NOROOT,
|
---|
893 | "assert has an empty test attribute",
|
---|
894 | NULL, NULL);
|
---|
895 | xmlFree(test);
|
---|
896 | } else {
|
---|
897 | /* TODO will need dynamic processing instead */
|
---|
898 | report = xmlNodeGetContent(cur);
|
---|
899 |
|
---|
900 | testptr = xmlSchematronAddTest(ctxt, XML_SCHEMATRON_ASSERT,
|
---|
901 | ruleptr, cur, test, report);
|
---|
902 | if (testptr == NULL)
|
---|
903 | xmlFree(test);
|
---|
904 | }
|
---|
905 | } else if (IS_SCHEMATRON(cur, "report")) {
|
---|
906 | nbChecks++;
|
---|
907 | test = xmlGetNoNsProp(cur, BAD_CAST "test");
|
---|
908 | if (test == NULL) {
|
---|
909 | xmlSchematronPErr(ctxt, cur,
|
---|
910 | XML_SCHEMAP_NOROOT,
|
---|
911 | "assert has no test attribute",
|
---|
912 | NULL, NULL);
|
---|
913 | } else if (test[0] == 0) {
|
---|
914 | xmlSchematronPErr(ctxt, cur,
|
---|
915 | XML_SCHEMAP_NOROOT,
|
---|
916 | "assert has an empty test attribute",
|
---|
917 | NULL, NULL);
|
---|
918 | xmlFree(test);
|
---|
919 | } else {
|
---|
920 | /* TODO will need dynamic processing instead */
|
---|
921 | report = xmlNodeGetContent(cur);
|
---|
922 |
|
---|
923 | testptr = xmlSchematronAddTest(ctxt, XML_SCHEMATRON_REPORT,
|
---|
924 | ruleptr, cur, test, report);
|
---|
925 | if (testptr == NULL)
|
---|
926 | xmlFree(test);
|
---|
927 | }
|
---|
928 | } else {
|
---|
929 | xmlSchematronPErr(ctxt, cur,
|
---|
930 | XML_SCHEMAP_NOROOT,
|
---|
931 | "Expecting an assert or a report element instead of %s",
|
---|
932 | cur->name, NULL);
|
---|
933 | }
|
---|
934 | cur = cur->next;
|
---|
935 | NEXT_SCHEMATRON(cur);
|
---|
936 | }
|
---|
937 | if (nbChecks == 0) {
|
---|
938 | xmlSchematronPErr(ctxt, rule,
|
---|
939 | XML_SCHEMAP_NOROOT,
|
---|
940 | "rule has no assert nor report element", NULL, NULL);
|
---|
941 | }
|
---|
942 | }
|
---|
943 |
|
---|
944 | /**
|
---|
945 | * xmlSchematronParsePattern:
|
---|
946 | * @ctxt: a schema validation context
|
---|
947 | * @pat: the pattern node
|
---|
948 | *
|
---|
949 | * parse a pattern element
|
---|
950 | */
|
---|
951 | static void
|
---|
952 | xmlSchematronParsePattern(xmlSchematronParserCtxtPtr ctxt, xmlNodePtr pat)
|
---|
953 | {
|
---|
954 | xmlNodePtr cur;
|
---|
955 | xmlSchematronPatternPtr pattern;
|
---|
956 | int nbRules = 0;
|
---|
957 | xmlChar *id;
|
---|
958 |
|
---|
959 | if ((ctxt == NULL) || (pat == NULL)) return;
|
---|
960 |
|
---|
961 | id = xmlGetNoNsProp(pat, BAD_CAST "id");
|
---|
962 | if (id == NULL) {
|
---|
963 | id = xmlGetNoNsProp(pat, BAD_CAST "name");
|
---|
964 | }
|
---|
965 | pattern = xmlSchematronAddPattern(ctxt, ctxt->schema, pat, id);
|
---|
966 | if (pattern == NULL) {
|
---|
967 | if (id != NULL)
|
---|
968 | xmlFree(id);
|
---|
969 | return;
|
---|
970 | }
|
---|
971 | cur = pat->children;
|
---|
972 | NEXT_SCHEMATRON(cur);
|
---|
973 | while (cur != NULL) {
|
---|
974 | if (IS_SCHEMATRON(cur, "rule")) {
|
---|
975 | xmlSchematronParseRule(ctxt, pattern, cur);
|
---|
976 | nbRules++;
|
---|
977 | } else {
|
---|
978 | xmlSchematronPErr(ctxt, cur,
|
---|
979 | XML_SCHEMAP_NOROOT,
|
---|
980 | "Expecting a rule element instead of %s", cur->name, NULL);
|
---|
981 | }
|
---|
982 | cur = cur->next;
|
---|
983 | NEXT_SCHEMATRON(cur);
|
---|
984 | }
|
---|
985 | if (nbRules == 0) {
|
---|
986 | xmlSchematronPErr(ctxt, pat,
|
---|
987 | XML_SCHEMAP_NOROOT,
|
---|
988 | "Pattern has no rule element", NULL, NULL);
|
---|
989 | }
|
---|
990 | }
|
---|
991 |
|
---|
992 | #if 0
|
---|
993 | /**
|
---|
994 | * xmlSchematronLoadInclude:
|
---|
995 | * @ctxt: a schema validation context
|
---|
996 | * @cur: the include element
|
---|
997 | *
|
---|
998 | * Load the include document, Push the current pointer
|
---|
999 | *
|
---|
1000 | * Returns the updated node pointer
|
---|
1001 | */
|
---|
1002 | static xmlNodePtr
|
---|
1003 | xmlSchematronLoadInclude(xmlSchematronParserCtxtPtr ctxt, xmlNodePtr cur)
|
---|
1004 | {
|
---|
1005 | xmlNodePtr ret = NULL;
|
---|
1006 | xmlDocPtr doc = NULL;
|
---|
1007 | xmlChar *href = NULL;
|
---|
1008 | xmlChar *base = NULL;
|
---|
1009 | xmlChar *URI = NULL;
|
---|
1010 |
|
---|
1011 | if ((ctxt == NULL) || (cur == NULL))
|
---|
1012 | return(NULL);
|
---|
1013 |
|
---|
1014 | href = xmlGetNoNsProp(cur, BAD_CAST "href");
|
---|
1015 | if (href == NULL) {
|
---|
1016 | xmlSchematronPErr(ctxt, cur,
|
---|
1017 | XML_SCHEMAP_NOROOT,
|
---|
1018 | "Include has no href attribute", NULL, NULL);
|
---|
1019 | return(cur->next);
|
---|
1020 | }
|
---|
1021 |
|
---|
1022 | /* do the URI base composition, load and find the root */
|
---|
1023 | base = xmlNodeGetBase(cur->doc, cur);
|
---|
1024 | URI = xmlBuildURI(href, base);
|
---|
1025 | doc = xmlReadFile((const char *) URI, NULL, SCHEMATRON_PARSE_OPTIONS);
|
---|
1026 | if (doc == NULL) {
|
---|
1027 | xmlSchematronPErr(ctxt, cur,
|
---|
1028 | XML_SCHEMAP_FAILED_LOAD,
|
---|
1029 | "could not load include '%s'.\n",
|
---|
1030 | URI, NULL);
|
---|
1031 | goto done;
|
---|
1032 | }
|
---|
1033 | ret = xmlDocGetRootElement(doc);
|
---|
1034 | if (ret == NULL) {
|
---|
1035 | xmlSchematronPErr(ctxt, cur,
|
---|
1036 | XML_SCHEMAP_FAILED_LOAD,
|
---|
1037 | "could not find root from include '%s'.\n",
|
---|
1038 | URI, NULL);
|
---|
1039 | goto done;
|
---|
1040 | }
|
---|
1041 |
|
---|
1042 | /* Success, push the include for rollback on exit */
|
---|
1043 | xmlSchematronPushInclude(ctxt, doc, cur);
|
---|
1044 |
|
---|
1045 | done:
|
---|
1046 | if (ret == NULL) {
|
---|
1047 | if (doc != NULL)
|
---|
1048 | xmlFreeDoc(doc);
|
---|
1049 | }
|
---|
1050 | xmlFree(href);
|
---|
1051 | if (base != NULL)
|
---|
1052 | xmlFree(base);
|
---|
1053 | if (URI != NULL)
|
---|
1054 | xmlFree(URI);
|
---|
1055 | return(ret);
|
---|
1056 | }
|
---|
1057 | #endif
|
---|
1058 |
|
---|
1059 | /**
|
---|
1060 | * xmlSchematronParse:
|
---|
1061 | * @ctxt: a schema validation context
|
---|
1062 | *
|
---|
1063 | * parse a schema definition resource and build an internal
|
---|
1064 | * XML Shema struture which can be used to validate instances.
|
---|
1065 | *
|
---|
1066 | * Returns the internal XML Schematron structure built from the resource or
|
---|
1067 | * NULL in case of error
|
---|
1068 | */
|
---|
1069 | xmlSchematronPtr
|
---|
1070 | xmlSchematronParse(xmlSchematronParserCtxtPtr ctxt)
|
---|
1071 | {
|
---|
1072 | xmlSchematronPtr ret = NULL;
|
---|
1073 | xmlDocPtr doc;
|
---|
1074 | xmlNodePtr root, cur;
|
---|
1075 | int preserve = 0;
|
---|
1076 |
|
---|
1077 | if (ctxt == NULL)
|
---|
1078 | return (NULL);
|
---|
1079 |
|
---|
1080 | ctxt->nberrors = 0;
|
---|
1081 |
|
---|
1082 | /*
|
---|
1083 | * First step is to parse the input document into an DOM/Infoset
|
---|
1084 | */
|
---|
1085 | if (ctxt->URL != NULL) {
|
---|
1086 | doc = xmlReadFile((const char *) ctxt->URL, NULL,
|
---|
1087 | SCHEMATRON_PARSE_OPTIONS);
|
---|
1088 | if (doc == NULL) {
|
---|
1089 | xmlSchematronPErr(ctxt, NULL,
|
---|
1090 | XML_SCHEMAP_FAILED_LOAD,
|
---|
1091 | "xmlSchematronParse: could not load '%s'.\n",
|
---|
1092 | ctxt->URL, NULL);
|
---|
1093 | return (NULL);
|
---|
1094 | }
|
---|
1095 | ctxt->preserve = 0;
|
---|
1096 | } else if (ctxt->buffer != NULL) {
|
---|
1097 | doc = xmlReadMemory(ctxt->buffer, ctxt->size, NULL, NULL,
|
---|
1098 | SCHEMATRON_PARSE_OPTIONS);
|
---|
1099 | if (doc == NULL) {
|
---|
1100 | xmlSchematronPErr(ctxt, NULL,
|
---|
1101 | XML_SCHEMAP_FAILED_PARSE,
|
---|
1102 | "xmlSchematronParse: could not parse.\n",
|
---|
1103 | NULL, NULL);
|
---|
1104 | return (NULL);
|
---|
1105 | }
|
---|
1106 | doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
|
---|
1107 | ctxt->URL = xmlDictLookup(ctxt->dict, BAD_CAST "in_memory_buffer", -1);
|
---|
1108 | ctxt->preserve = 0;
|
---|
1109 | } else if (ctxt->doc != NULL) {
|
---|
1110 | doc = ctxt->doc;
|
---|
1111 | preserve = 1;
|
---|
1112 | ctxt->preserve = 1;
|
---|
1113 | } else {
|
---|
1114 | xmlSchematronPErr(ctxt, NULL,
|
---|
1115 | XML_SCHEMAP_NOTHING_TO_PARSE,
|
---|
1116 | "xmlSchematronParse: could not parse.\n",
|
---|
1117 | NULL, NULL);
|
---|
1118 | return (NULL);
|
---|
1119 | }
|
---|
1120 |
|
---|
1121 | /*
|
---|
1122 | * Then extract the root and Schematron parse it
|
---|
1123 | */
|
---|
1124 | root = xmlDocGetRootElement(doc);
|
---|
1125 | if (root == NULL) {
|
---|
1126 | xmlSchematronPErr(ctxt, (xmlNodePtr) doc,
|
---|
1127 | XML_SCHEMAP_NOROOT,
|
---|
1128 | "The schema has no document element.\n", NULL, NULL);
|
---|
1129 | if (!preserve) {
|
---|
1130 | xmlFreeDoc(doc);
|
---|
1131 | }
|
---|
1132 | return (NULL);
|
---|
1133 | }
|
---|
1134 |
|
---|
1135 | if (!IS_SCHEMATRON(root, "schema")) {
|
---|
1136 | xmlSchematronPErr(ctxt, root,
|
---|
1137 | XML_SCHEMAP_NOROOT,
|
---|
1138 | "The XML document '%s' is not a XML schematron document",
|
---|
1139 | ctxt->URL, NULL);
|
---|
1140 | goto exit;
|
---|
1141 | }
|
---|
1142 | ret = xmlSchematronNewSchematron(ctxt);
|
---|
1143 | if (ret == NULL)
|
---|
1144 | goto exit;
|
---|
1145 | ctxt->schema = ret;
|
---|
1146 |
|
---|
1147 | /*
|
---|
1148 | * scan the schema elements
|
---|
1149 | */
|
---|
1150 | cur = root->children;
|
---|
1151 | NEXT_SCHEMATRON(cur);
|
---|
1152 | if (IS_SCHEMATRON(cur, "title")) {
|
---|
1153 | xmlChar *title = xmlNodeGetContent(cur);
|
---|
1154 | if (title != NULL) {
|
---|
1155 | ret->title = xmlDictLookup(ret->dict, title, -1);
|
---|
1156 | xmlFree(title);
|
---|
1157 | }
|
---|
1158 | cur = cur->next;
|
---|
1159 | NEXT_SCHEMATRON(cur);
|
---|
1160 | }
|
---|
1161 | while (IS_SCHEMATRON(cur, "ns")) {
|
---|
1162 | xmlChar *prefix = xmlGetNoNsProp(cur, BAD_CAST "prefix");
|
---|
1163 | xmlChar *uri = xmlGetNoNsProp(cur, BAD_CAST "uri");
|
---|
1164 | if ((uri == NULL) || (uri[0] == 0)) {
|
---|
1165 | xmlSchematronPErr(ctxt, cur,
|
---|
1166 | XML_SCHEMAP_NOROOT,
|
---|
1167 | "ns element has no uri", NULL, NULL);
|
---|
1168 | }
|
---|
1169 | if ((prefix == NULL) || (prefix[0] == 0)) {
|
---|
1170 | xmlSchematronPErr(ctxt, cur,
|
---|
1171 | XML_SCHEMAP_NOROOT,
|
---|
1172 | "ns element has no prefix", NULL, NULL);
|
---|
1173 | }
|
---|
1174 | if ((prefix) && (uri)) {
|
---|
1175 | xmlXPathRegisterNs(ctxt->xctxt, prefix, uri);
|
---|
1176 | xmlSchematronAddNamespace(ctxt, prefix, uri);
|
---|
1177 | ret->nbNs++;
|
---|
1178 | }
|
---|
1179 | if (uri)
|
---|
1180 | xmlFree(uri);
|
---|
1181 | if (prefix)
|
---|
1182 | xmlFree(prefix);
|
---|
1183 | cur = cur->next;
|
---|
1184 | NEXT_SCHEMATRON(cur);
|
---|
1185 | }
|
---|
1186 | while (cur != NULL) {
|
---|
1187 | if (IS_SCHEMATRON(cur, "pattern")) {
|
---|
1188 | xmlSchematronParsePattern(ctxt, cur);
|
---|
1189 | ret->nbPattern++;
|
---|
1190 | } else {
|
---|
1191 | xmlSchematronPErr(ctxt, cur,
|
---|
1192 | XML_SCHEMAP_NOROOT,
|
---|
1193 | "Expecting a pattern element instead of %s", cur->name, NULL);
|
---|
1194 | }
|
---|
1195 | cur = cur->next;
|
---|
1196 | NEXT_SCHEMATRON(cur);
|
---|
1197 | }
|
---|
1198 | if (ret->nbPattern == 0) {
|
---|
1199 | xmlSchematronPErr(ctxt, root,
|
---|
1200 | XML_SCHEMAP_NOROOT,
|
---|
1201 | "The schematron document '%s' has no pattern",
|
---|
1202 | ctxt->URL, NULL);
|
---|
1203 | goto exit;
|
---|
1204 | }
|
---|
1205 | /* the original document must be kept for reporting */
|
---|
1206 | ret->doc = doc;
|
---|
1207 | if (preserve) {
|
---|
1208 | ret->preserve = 1;
|
---|
1209 | }
|
---|
1210 | preserve = 1;
|
---|
1211 |
|
---|
1212 | exit:
|
---|
1213 | if (!preserve) {
|
---|
1214 | xmlFreeDoc(doc);
|
---|
1215 | }
|
---|
1216 | if (ret != NULL) {
|
---|
1217 | if (ctxt->nberrors != 0) {
|
---|
1218 | xmlSchematronFree(ret);
|
---|
1219 | ret = NULL;
|
---|
1220 | } else {
|
---|
1221 | ret->namespaces = ctxt->namespaces;
|
---|
1222 | ret->nbNamespaces = ctxt->nbNamespaces;
|
---|
1223 | ctxt->namespaces = NULL;
|
---|
1224 | }
|
---|
1225 | }
|
---|
1226 | return (ret);
|
---|
1227 | }
|
---|
1228 |
|
---|
1229 | /************************************************************************
|
---|
1230 | * *
|
---|
1231 | * Schematrontron Reports handler *
|
---|
1232 | * *
|
---|
1233 | ************************************************************************/
|
---|
1234 |
|
---|
1235 | static xmlNodePtr
|
---|
1236 | xmlSchematronGetNode(xmlSchematronValidCtxtPtr ctxt,
|
---|
1237 | xmlNodePtr cur, const xmlChar *xpath) {
|
---|
1238 | xmlNodePtr node = NULL;
|
---|
1239 | xmlXPathObjectPtr ret;
|
---|
1240 |
|
---|
1241 | if ((ctxt == NULL) || (cur == NULL) || (xpath == NULL))
|
---|
1242 | return(NULL);
|
---|
1243 |
|
---|
1244 | ctxt->xctxt->doc = cur->doc;
|
---|
1245 | ctxt->xctxt->node = cur;
|
---|
1246 | ret = xmlXPathEval(xpath, ctxt->xctxt);
|
---|
1247 | if (ret == NULL)
|
---|
1248 | return(NULL);
|
---|
1249 |
|
---|
1250 | if ((ret->type == XPATH_NODESET) &&
|
---|
1251 | (ret->nodesetval != NULL) && (ret->nodesetval->nodeNr > 0))
|
---|
1252 | node = ret->nodesetval->nodeTab[0];
|
---|
1253 |
|
---|
1254 | xmlXPathFreeObject(ret);
|
---|
1255 | return(node);
|
---|
1256 | }
|
---|
1257 |
|
---|
1258 | /**
|
---|
1259 | * xmlSchematronReportOutput:
|
---|
1260 | * @ctxt: the validation context
|
---|
1261 | * @cur: the current node tested
|
---|
1262 | * @msg: the message output
|
---|
1263 | *
|
---|
1264 | * Output part of the report to whatever channel the user selected
|
---|
1265 | */
|
---|
1266 | static void
|
---|
1267 | xmlSchematronReportOutput(xmlSchematronValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
|
---|
1268 | xmlNodePtr cur ATTRIBUTE_UNUSED,
|
---|
1269 | const char *msg) {
|
---|
1270 | /* TODO */
|
---|
1271 | fprintf(stderr, "%s", msg);
|
---|
1272 | }
|
---|
1273 |
|
---|
1274 | /**
|
---|
1275 | * xmlSchematronFormatReport:
|
---|
1276 | * @ctxt: the validation context
|
---|
1277 | * @test: the test node
|
---|
1278 | * @cur: the current node tested
|
---|
1279 | *
|
---|
1280 | * Build the string being reported to the user.
|
---|
1281 | *
|
---|
1282 | * Returns a report string or NULL in case of error. The string needs
|
---|
1283 | * to be deallocated by teh caller
|
---|
1284 | */
|
---|
1285 | static xmlChar *
|
---|
1286 | xmlSchematronFormatReport(xmlSchematronValidCtxtPtr ctxt,
|
---|
1287 | xmlNodePtr test, xmlNodePtr cur) {
|
---|
1288 | xmlChar *ret = NULL;
|
---|
1289 | xmlNodePtr child, node;
|
---|
1290 |
|
---|
1291 | if ((test == NULL) || (cur == NULL))
|
---|
1292 | return(ret);
|
---|
1293 |
|
---|
1294 | child = test->children;
|
---|
1295 | while (child != NULL) {
|
---|
1296 | if ((child->type == XML_TEXT_NODE) ||
|
---|
1297 | (child->type == XML_CDATA_SECTION_NODE))
|
---|
1298 | ret = xmlStrcat(ret, child->content);
|
---|
1299 | else if (IS_SCHEMATRON(child, "name")) {
|
---|
1300 | xmlChar *path;
|
---|
1301 |
|
---|
1302 | path = xmlGetNoNsProp(child, BAD_CAST "path");
|
---|
1303 |
|
---|
1304 | node = cur;
|
---|
1305 | if (path != NULL) {
|
---|
1306 | node = xmlSchematronGetNode(ctxt, cur, path);
|
---|
1307 | if (node == NULL)
|
---|
1308 | node = cur;
|
---|
1309 | xmlFree(path);
|
---|
1310 | }
|
---|
1311 |
|
---|
1312 | if ((node->ns == NULL) || (node->ns->prefix == NULL))
|
---|
1313 | ret = xmlStrcat(ret, node->name);
|
---|
1314 | else {
|
---|
1315 | ret = xmlStrcat(ret, node->ns->prefix);
|
---|
1316 | ret = xmlStrcat(ret, BAD_CAST ":");
|
---|
1317 | ret = xmlStrcat(ret, node->name);
|
---|
1318 | }
|
---|
1319 | } else {
|
---|
1320 | child = child->next;
|
---|
1321 | continue;
|
---|
1322 | }
|
---|
1323 |
|
---|
1324 | /*
|
---|
1325 | * remove superfluous \n
|
---|
1326 | */
|
---|
1327 | if (ret != NULL) {
|
---|
1328 | int len = xmlStrlen(ret);
|
---|
1329 | xmlChar c;
|
---|
1330 |
|
---|
1331 | if (len > 0) {
|
---|
1332 | c = ret[len - 1];
|
---|
1333 | if ((c == ' ') || (c == '\n') || (c == '\r') || (c == '\t')) {
|
---|
1334 | while ((c == ' ') || (c == '\n') ||
|
---|
1335 | (c == '\r') || (c == '\t')) {
|
---|
1336 | len--;
|
---|
1337 | if (len == 0)
|
---|
1338 | break;
|
---|
1339 | c = ret[len - 1];
|
---|
1340 | }
|
---|
1341 | ret[len] = ' ';
|
---|
1342 | ret[len + 1] = 0;
|
---|
1343 | }
|
---|
1344 | }
|
---|
1345 | }
|
---|
1346 |
|
---|
1347 | child = child->next;
|
---|
1348 | }
|
---|
1349 | return(ret);
|
---|
1350 | }
|
---|
1351 |
|
---|
1352 | /**
|
---|
1353 | * xmlSchematronReportSuccess:
|
---|
1354 | * @ctxt: the validation context
|
---|
1355 | * @test: the compiled test
|
---|
1356 | * @cur: the current node tested
|
---|
1357 | * @success: boolean value for the result
|
---|
1358 | *
|
---|
1359 | * called from the validation engine when an assert or report test have
|
---|
1360 | * been done.
|
---|
1361 | */
|
---|
1362 | static void
|
---|
1363 | xmlSchematronReportSuccess(xmlSchematronValidCtxtPtr ctxt,
|
---|
1364 | xmlSchematronTestPtr test, xmlNodePtr cur, int success) {
|
---|
1365 | if ((ctxt == NULL) || (cur == NULL) || (test == NULL))
|
---|
1366 | return;
|
---|
1367 | /* if quiet and not SVRL report only failures */
|
---|
1368 | if ((ctxt->flags & XML_SCHEMATRON_OUT_QUIET) &&
|
---|
1369 | ((ctxt->flags & XML_SCHEMATRON_OUT_XML) == 0) &&
|
---|
1370 | (test->type == XML_SCHEMATRON_REPORT))
|
---|
1371 | return;
|
---|
1372 | if (ctxt->flags & XML_SCHEMATRON_OUT_XML) {
|
---|
1373 | TODO
|
---|
1374 | } else {
|
---|
1375 | xmlChar *path;
|
---|
1376 | char msg[1000];
|
---|
1377 | long line;
|
---|
1378 | const xmlChar *report = NULL;
|
---|
1379 |
|
---|
1380 | if (((test->type == XML_SCHEMATRON_REPORT) & (!success)) ||
|
---|
1381 | ((test->type == XML_SCHEMATRON_ASSERT) & (success)))
|
---|
1382 | return;
|
---|
1383 | line = xmlGetLineNo(cur);
|
---|
1384 | path = xmlGetNodePath(cur);
|
---|
1385 | if (path == NULL)
|
---|
1386 | path = (xmlChar *) cur->name;
|
---|
1387 | #if 0
|
---|
1388 | if ((test->report != NULL) && (test->report[0] != 0))
|
---|
1389 | report = test->report;
|
---|
1390 | #endif
|
---|
1391 | if (test->node != NULL)
|
---|
1392 | report = xmlSchematronFormatReport(ctxt, test->node, cur);
|
---|
1393 | if (report == NULL) {
|
---|
1394 | if (test->type == XML_SCHEMATRON_ASSERT) {
|
---|
1395 | snprintf(msg, 999, "%s line %ld: node failed assert\n",
|
---|
1396 | (const char *) path, line);
|
---|
1397 | } else {
|
---|
1398 | snprintf(msg, 999, "%s line %ld: node failed report\n",
|
---|
1399 | (const char *) path, line);
|
---|
1400 | }
|
---|
1401 | } else {
|
---|
1402 | snprintf(msg, 999, "%s line %ld: %s\n", (const char *) path,
|
---|
1403 | line, (const char *) report);
|
---|
1404 | xmlFree((char *) report);
|
---|
1405 | }
|
---|
1406 | xmlSchematronReportOutput(ctxt, cur, &msg[0]);
|
---|
1407 | if ((path != NULL) && (path != (xmlChar *) cur->name))
|
---|
1408 | xmlFree(path);
|
---|
1409 | }
|
---|
1410 | }
|
---|
1411 |
|
---|
1412 | /**
|
---|
1413 | * xmlSchematronReportPattern:
|
---|
1414 | * @ctxt: the validation context
|
---|
1415 | * @pattern: the current pattern
|
---|
1416 | *
|
---|
1417 | * called from the validation engine when starting to check a pattern
|
---|
1418 | */
|
---|
1419 | static void
|
---|
1420 | xmlSchematronReportPattern(xmlSchematronValidCtxtPtr ctxt,
|
---|
1421 | xmlSchematronPatternPtr pattern) {
|
---|
1422 | if ((ctxt == NULL) || (pattern == NULL))
|
---|
1423 | return;
|
---|
1424 | if (ctxt->flags & XML_SCHEMATRON_OUT_QUIET)
|
---|
1425 | return;
|
---|
1426 | if (ctxt->flags & XML_SCHEMATRON_OUT_XML) {
|
---|
1427 | TODO
|
---|
1428 | } else {
|
---|
1429 | char msg[1000];
|
---|
1430 |
|
---|
1431 | if (pattern->name == NULL)
|
---|
1432 | return;
|
---|
1433 | snprintf(msg, 999, "Pattern: %s\n", (const char *) pattern->name);
|
---|
1434 | xmlSchematronReportOutput(ctxt, NULL, &msg[0]);
|
---|
1435 | }
|
---|
1436 | }
|
---|
1437 |
|
---|
1438 |
|
---|
1439 | /************************************************************************
|
---|
1440 | * *
|
---|
1441 | * Validation against a Schematrontron *
|
---|
1442 | * *
|
---|
1443 | ************************************************************************/
|
---|
1444 |
|
---|
1445 | /**
|
---|
1446 | * xmlSchematronNewValidCtxt:
|
---|
1447 | * @schema: a precompiled XML Schematrons
|
---|
1448 | * @options: a set of xmlSchematronValidOptions
|
---|
1449 | *
|
---|
1450 | * Create an XML Schematrons validation context based on the given schema.
|
---|
1451 | *
|
---|
1452 | * Returns the validation context or NULL in case of error
|
---|
1453 | */
|
---|
1454 | xmlSchematronValidCtxtPtr
|
---|
1455 | xmlSchematronNewValidCtxt(xmlSchematronPtr schema, int options)
|
---|
1456 | {
|
---|
1457 | int i;
|
---|
1458 | xmlSchematronValidCtxtPtr ret;
|
---|
1459 |
|
---|
1460 | ret = (xmlSchematronValidCtxtPtr) xmlMalloc(sizeof(xmlSchematronValidCtxt));
|
---|
1461 | if (ret == NULL) {
|
---|
1462 | xmlSchematronVErrMemory(NULL, "allocating validation context",
|
---|
1463 | NULL);
|
---|
1464 | return (NULL);
|
---|
1465 | }
|
---|
1466 | memset(ret, 0, sizeof(xmlSchematronValidCtxt));
|
---|
1467 | ret->type = XML_STRON_CTXT_VALIDATOR;
|
---|
1468 | ret->schema = schema;
|
---|
1469 | ret->xctxt = xmlXPathNewContext(NULL);
|
---|
1470 | ret->flags = options;
|
---|
1471 | if (ret->xctxt == NULL) {
|
---|
1472 | xmlSchematronPErrMemory(NULL, "allocating schema parser XPath context",
|
---|
1473 | NULL);
|
---|
1474 | xmlSchematronFreeValidCtxt(ret);
|
---|
1475 | return (NULL);
|
---|
1476 | }
|
---|
1477 | for (i = 0;i < schema->nbNamespaces;i++) {
|
---|
1478 | if ((schema->namespaces[2 * i] == NULL) ||
|
---|
1479 | (schema->namespaces[2 * i + 1] == NULL))
|
---|
1480 | break;
|
---|
1481 | xmlXPathRegisterNs(ret->xctxt, schema->namespaces[2 * i + 1],
|
---|
1482 | schema->namespaces[2 * i]);
|
---|
1483 | }
|
---|
1484 | return (ret);
|
---|
1485 | }
|
---|
1486 |
|
---|
1487 | /**
|
---|
1488 | * xmlSchematronFreeValidCtxt:
|
---|
1489 | * @ctxt: the schema validation context
|
---|
1490 | *
|
---|
1491 | * Free the resources associated to the schema validation context
|
---|
1492 | */
|
---|
1493 | void
|
---|
1494 | xmlSchematronFreeValidCtxt(xmlSchematronValidCtxtPtr ctxt)
|
---|
1495 | {
|
---|
1496 | if (ctxt == NULL)
|
---|
1497 | return;
|
---|
1498 | if (ctxt->xctxt != NULL)
|
---|
1499 | xmlXPathFreeContext(ctxt->xctxt);
|
---|
1500 | if (ctxt->dict != NULL)
|
---|
1501 | xmlDictFree(ctxt->dict);
|
---|
1502 | xmlFree(ctxt);
|
---|
1503 | }
|
---|
1504 |
|
---|
1505 | static xmlNodePtr
|
---|
1506 | xmlSchematronNextNode(xmlNodePtr cur) {
|
---|
1507 | if (cur->children != NULL) {
|
---|
1508 | /*
|
---|
1509 | * Do not descend on entities declarations
|
---|
1510 | */
|
---|
1511 | if (cur->children->type != XML_ENTITY_DECL) {
|
---|
1512 | cur = cur->children;
|
---|
1513 | /*
|
---|
1514 | * Skip DTDs
|
---|
1515 | */
|
---|
1516 | if (cur->type != XML_DTD_NODE)
|
---|
1517 | return(cur);
|
---|
1518 | }
|
---|
1519 | }
|
---|
1520 |
|
---|
1521 | while (cur->next != NULL) {
|
---|
1522 | cur = cur->next;
|
---|
1523 | if ((cur->type != XML_ENTITY_DECL) &&
|
---|
1524 | (cur->type != XML_DTD_NODE))
|
---|
1525 | return(cur);
|
---|
1526 | }
|
---|
1527 |
|
---|
1528 | do {
|
---|
1529 | cur = cur->parent;
|
---|
1530 | if (cur == NULL) break;
|
---|
1531 | if (cur->type == XML_DOCUMENT_NODE) return(NULL);
|
---|
1532 | if (cur->next != NULL) {
|
---|
1533 | cur = cur->next;
|
---|
1534 | return(cur);
|
---|
1535 | }
|
---|
1536 | } while (cur != NULL);
|
---|
1537 | return(cur);
|
---|
1538 | }
|
---|
1539 |
|
---|
1540 | /**
|
---|
1541 | * xmlSchematronRunTest:
|
---|
1542 | * @ctxt: the schema validation context
|
---|
1543 | * @test: the current test
|
---|
1544 | * @instance: the document instace tree
|
---|
1545 | * @cur: the current node in the instance
|
---|
1546 | *
|
---|
1547 | * Validate a rule against a tree instance at a given position
|
---|
1548 | *
|
---|
1549 | * Returns 1 in case of success, 0 if error and -1 in case of internal error
|
---|
1550 | */
|
---|
1551 | static int
|
---|
1552 | xmlSchematronRunTest(xmlSchematronValidCtxtPtr ctxt,
|
---|
1553 | xmlSchematronTestPtr test, xmlDocPtr instance, xmlNodePtr cur)
|
---|
1554 | {
|
---|
1555 | xmlXPathObjectPtr ret;
|
---|
1556 | int failed;
|
---|
1557 |
|
---|
1558 | failed = 0;
|
---|
1559 | ctxt->xctxt->doc = instance;
|
---|
1560 | ctxt->xctxt->node = cur;
|
---|
1561 | ret = xmlXPathCompiledEval(test->comp, ctxt->xctxt);
|
---|
1562 | if (ret == NULL) {
|
---|
1563 | failed = 1;
|
---|
1564 | } else {
|
---|
1565 | switch (ret->type) {
|
---|
1566 | case XPATH_XSLT_TREE:
|
---|
1567 | case XPATH_NODESET:
|
---|
1568 | if ((ret->nodesetval == NULL) ||
|
---|
1569 | (ret->nodesetval->nodeNr == 0))
|
---|
1570 | failed = 1;
|
---|
1571 | break;
|
---|
1572 | case XPATH_BOOLEAN:
|
---|
1573 | failed = !ret->boolval;
|
---|
1574 | break;
|
---|
1575 | case XPATH_NUMBER:
|
---|
1576 | if ((xmlXPathIsNaN(ret->floatval)) ||
|
---|
1577 | (ret->floatval == 0.0))
|
---|
1578 | failed = 1;
|
---|
1579 | break;
|
---|
1580 | case XPATH_STRING:
|
---|
1581 | if ((ret->stringval == NULL) ||
|
---|
1582 | (ret->stringval[0] == 0))
|
---|
1583 | failed = 1;
|
---|
1584 | break;
|
---|
1585 | case XPATH_UNDEFINED:
|
---|
1586 | case XPATH_POINT:
|
---|
1587 | case XPATH_RANGE:
|
---|
1588 | case XPATH_LOCATIONSET:
|
---|
1589 | case XPATH_USERS:
|
---|
1590 | failed = 1;
|
---|
1591 | break;
|
---|
1592 | }
|
---|
1593 | xmlXPathFreeObject(ret);
|
---|
1594 | }
|
---|
1595 | if ((failed) && (test->type == XML_SCHEMATRON_ASSERT))
|
---|
1596 | ctxt->nberrors++;
|
---|
1597 | else if ((!failed) && (test->type == XML_SCHEMATRON_REPORT))
|
---|
1598 | ctxt->nberrors++;
|
---|
1599 |
|
---|
1600 | xmlSchematronReportSuccess(ctxt, test, cur, !failed);
|
---|
1601 |
|
---|
1602 | return(!failed);
|
---|
1603 | }
|
---|
1604 |
|
---|
1605 | /**
|
---|
1606 | * xmlSchematronValidateDoc:
|
---|
1607 | * @ctxt: the schema validation context
|
---|
1608 | * @instance: the document instace tree
|
---|
1609 | *
|
---|
1610 | * Validate a tree instance against the schematron
|
---|
1611 | *
|
---|
1612 | * Returns 0 in case of success, -1 in case of internal error
|
---|
1613 | * and an error count otherwise.
|
---|
1614 | */
|
---|
1615 | int
|
---|
1616 | xmlSchematronValidateDoc(xmlSchematronValidCtxtPtr ctxt, xmlDocPtr instance)
|
---|
1617 | {
|
---|
1618 | xmlNodePtr cur, root;
|
---|
1619 | xmlSchematronPatternPtr pattern;
|
---|
1620 | xmlSchematronRulePtr rule;
|
---|
1621 | xmlSchematronTestPtr test;
|
---|
1622 |
|
---|
1623 | if ((ctxt == NULL) || (ctxt->schema == NULL) ||
|
---|
1624 | (ctxt->schema->rules == NULL) || (instance == NULL))
|
---|
1625 | return(-1);
|
---|
1626 | ctxt->nberrors = 0;
|
---|
1627 | root = xmlDocGetRootElement(instance);
|
---|
1628 | if (root == NULL) {
|
---|
1629 | TODO
|
---|
1630 | ctxt->nberrors++;
|
---|
1631 | return(1);
|
---|
1632 | }
|
---|
1633 | if ((ctxt->flags & XML_SCHEMATRON_OUT_QUIET) ||
|
---|
1634 | (ctxt->flags == 0)) {
|
---|
1635 | /*
|
---|
1636 | * we are just trying to assert the validity of the document,
|
---|
1637 | * speed primes over the output, run in a single pass
|
---|
1638 | */
|
---|
1639 | cur = root;
|
---|
1640 | while (cur != NULL) {
|
---|
1641 | rule = ctxt->schema->rules;
|
---|
1642 | while (rule != NULL) {
|
---|
1643 | if (xmlPatternMatch(rule->pattern, cur) == 1) {
|
---|
1644 | test = rule->tests;
|
---|
1645 | while (test != NULL) {
|
---|
1646 | xmlSchematronRunTest(ctxt, test, instance, cur);
|
---|
1647 | test = test->next;
|
---|
1648 | }
|
---|
1649 | }
|
---|
1650 | rule = rule->next;
|
---|
1651 | }
|
---|
1652 |
|
---|
1653 | cur = xmlSchematronNextNode(cur);
|
---|
1654 | }
|
---|
1655 | } else {
|
---|
1656 | /*
|
---|
1657 | * Process all contexts one at a time
|
---|
1658 | */
|
---|
1659 | pattern = ctxt->schema->patterns;
|
---|
1660 |
|
---|
1661 | while (pattern != NULL) {
|
---|
1662 | xmlSchematronReportPattern(ctxt, pattern);
|
---|
1663 |
|
---|
1664 | /*
|
---|
1665 | * TODO convert the pattern rule to a direct XPath and
|
---|
1666 | * compute directly instead of using the pattern matching
|
---|
1667 | * over the full document...
|
---|
1668 | * Check the exact semantic
|
---|
1669 | */
|
---|
1670 | cur = root;
|
---|
1671 | while (cur != NULL) {
|
---|
1672 | rule = pattern->rules;
|
---|
1673 | while (rule != NULL) {
|
---|
1674 | if (xmlPatternMatch(rule->pattern, cur) == 1) {
|
---|
1675 | test = rule->tests;
|
---|
1676 | while (test != NULL) {
|
---|
1677 | xmlSchematronRunTest(ctxt, test, instance, cur);
|
---|
1678 | test = test->next;
|
---|
1679 | }
|
---|
1680 | }
|
---|
1681 | rule = rule->patnext;
|
---|
1682 | }
|
---|
1683 |
|
---|
1684 | cur = xmlSchematronNextNode(cur);
|
---|
1685 | }
|
---|
1686 | pattern = pattern->next;
|
---|
1687 | }
|
---|
1688 | }
|
---|
1689 | return(ctxt->nberrors);
|
---|
1690 | }
|
---|
1691 |
|
---|
1692 | #ifdef STANDALONE
|
---|
1693 | int
|
---|
1694 | main(void)
|
---|
1695 | {
|
---|
1696 | int ret;
|
---|
1697 | xmlDocPtr instance;
|
---|
1698 | xmlSchematronParserCtxtPtr pctxt;
|
---|
1699 | xmlSchematronValidCtxtPtr vctxt;
|
---|
1700 | xmlSchematronPtr schema = NULL;
|
---|
1701 |
|
---|
1702 | pctxt = xmlSchematronNewParserCtxt("tst.sct");
|
---|
1703 | if (pctxt == NULL) {
|
---|
1704 | fprintf(stderr, "failed to build schematron parser\n");
|
---|
1705 | } else {
|
---|
1706 | schema = xmlSchematronParse(pctxt);
|
---|
1707 | if (schema == NULL) {
|
---|
1708 | fprintf(stderr, "failed to compile schematron\n");
|
---|
1709 | }
|
---|
1710 | xmlSchematronFreeParserCtxt(pctxt);
|
---|
1711 | }
|
---|
1712 | instance = xmlReadFile("tst.sct", NULL,
|
---|
1713 | XML_PARSE_NOENT | XML_PARSE_NOCDATA);
|
---|
1714 | if (instance == NULL) {
|
---|
1715 | fprintf(stderr, "failed to parse instance\n");
|
---|
1716 | }
|
---|
1717 | if ((schema != NULL) && (instance != NULL)) {
|
---|
1718 | vctxt = xmlSchematronNewValidCtxt(schema);
|
---|
1719 | if (vctxt == NULL) {
|
---|
1720 | fprintf(stderr, "failed to build schematron validator\n");
|
---|
1721 | } else {
|
---|
1722 | ret = xmlSchematronValidateDoc(vctxt, instance);
|
---|
1723 | xmlSchematronFreeValidCtxt(vctxt);
|
---|
1724 | }
|
---|
1725 | }
|
---|
1726 | xmlSchematronFree(schema);
|
---|
1727 | xmlFreeDoc(instance);
|
---|
1728 |
|
---|
1729 | xmlCleanupParser();
|
---|
1730 | xmlMemoryDump();
|
---|
1731 |
|
---|
1732 | return (0);
|
---|
1733 | }
|
---|
1734 | #endif
|
---|
1735 | #define bottom_schematron
|
---|
1736 | #include "elfgcchack.h"
|
---|
1737 | #endif /* LIBXML_SCHEMATRON_ENABLED */
|
---|