Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 1 | /* |
Daniel Veillard | e70375c | 2005-07-30 21:09:12 +0000 | [diff] [blame] | 2 | * schematron.c : implementation of the Schematron schema validity checking |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 3 | * |
| 4 | * See Copyright for the status of this software. |
| 5 | * |
Daniel Veillard | e70375c | 2005-07-30 21:09:12 +0000 | [diff] [blame] | 6 | * Daniel Veillard <daniel@veillard.com> |
| 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 |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 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 | |
Daniel Veillard | e70375c | 2005-07-30 21:09:12 +0000 | [diff] [blame] | 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 | |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 44 | static const xmlChar *xmlSchematronNs = XML_SCHEMATRON_NS; |
Daniel Veillard | e70375c | 2005-07-30 21:09:12 +0000 | [diff] [blame] | 45 | static const xmlChar *xmlOldSchematronNs = SCT_OLD_NS; |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 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)) && \ |
Daniel Veillard | e70375c | 2005-07-30 21:09:12 +0000 | [diff] [blame] | 51 | ((xmlStrEqual(node->ns->href, xmlSchematronNs)) || \ |
| 52 | (xmlStrEqual(node->ns->href, xmlOldSchematronNs)))) |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 53 | |
| 54 | #define NEXT_SCHEMATRON(node) \ |
| 55 | while (node != NULL) { \ |
| 56 | if ((node->type == XML_ELEMENT_NODE ) && (node->ns != NULL) && \ |
Daniel Veillard | e70375c | 2005-07-30 21:09:12 +0000 | [diff] [blame] | 57 | ((xmlStrEqual(node->ns->href, xmlSchematronNs)) || \ |
| 58 | (xmlStrEqual(node->ns->href, xmlOldSchematronNs)))) \ |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 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 | |
Daniel Veillard | e70375c | 2005-07-30 21:09:12 +0000 | [diff] [blame] | 73 | typedef enum { |
| 74 | XML_SCHEMATRON_ASSERT=1, |
| 75 | XML_SCHEMATRON_REPORT=2 |
| 76 | } xmlSchematronTestType; |
| 77 | |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 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 */ |
Daniel Veillard | e70375c | 2005-07-30 21:09:12 +0000 | [diff] [blame] | 87 | xmlSchematronTestType type; /* the test type */ |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 88 | xmlNodePtr node; /* the node in the tree */ |
| 89 | xmlChar *test; /* the expression to test */ |
| 90 | xmlXPathCompExprPtr comp; /* the compiled expression */ |
Daniel Veillard | d4501d7 | 2005-07-24 14:27:16 +0000 | [diff] [blame] | 91 | xmlChar *report; /* the message to report */ |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 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 */ |
Daniel Veillard | e70375c | 2005-07-30 21:09:12 +0000 | [diff] [blame] | 103 | xmlSchematronRulePtr patnext;/* the next rule in the pattern list */ |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 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 */ |
Daniel Veillard | d4501d7 | 2005-07-24 14:27:16 +0000 | [diff] [blame] | 108 | xmlChar *report; /* the message to report */ |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | /** |
Daniel Veillard | e70375c | 2005-07-30 21:09:12 +0000 | [diff] [blame] | 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 | /** |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 125 | * _xmlSchematron: |
| 126 | * |
| 127 | * A Schematrons definition |
| 128 | */ |
| 129 | struct _xmlSchematron { |
| 130 | const xmlChar *name; /* schema name */ |
Daniel Veillard | eaecb3e | 2005-07-31 13:43:14 +0000 | [diff] [blame] | 131 | int preserve; /* was the document passed by the user */ |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 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 */ |
Daniel Veillard | e70375c | 2005-07-30 21:09:12 +0000 | [diff] [blame] | 143 | xmlSchematronPatternPtr patterns;/* the patterns found */ |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 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 |
Daniel Veillard | e70375c | 2005-07-30 21:09:12 +0000 | [diff] [blame] | 289 | * @type: the type of test |
| 290 | * @rule: the parent rule |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 291 | * @node: the node hosting the test |
Daniel Veillard | e70375c | 2005-07-30 21:09:12 +0000 | [diff] [blame] | 292 | * @test: the associated test |
Daniel Veillard | d4501d7 | 2005-07-24 14:27:16 +0000 | [diff] [blame] | 293 | * @report: the associated report string |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 294 | * |
| 295 | * Add a test to a schematron |
| 296 | * |
| 297 | * Returns the new pointer or NULL in case of error |
| 298 | */ |
| 299 | static xmlSchematronTestPtr |
Daniel Veillard | e70375c | 2005-07-30 21:09:12 +0000 | [diff] [blame] | 300 | xmlSchematronAddTest(xmlSchematronParserCtxtPtr ctxt, |
| 301 | xmlSchematronTestType type, |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 302 | xmlSchematronRulePtr rule, |
Daniel Veillard | d4501d7 | 2005-07-24 14:27:16 +0000 | [diff] [blame] | 303 | xmlNodePtr node, xmlChar *test, xmlChar *report) |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 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; |
Daniel Veillard | d4501d7 | 2005-07-24 14:27:16 +0000 | [diff] [blame] | 334 | ret->report = report; |
Daniel Veillard | c740a17 | 2005-07-31 12:17:24 +0000 | [diff] [blame] | 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 | } |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 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); |
Daniel Veillard | d4501d7 | 2005-07-24 14:27:16 +0000 | [diff] [blame] | 364 | if (tests->report != NULL) |
| 365 | xmlFree(tests->report); |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 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 |
Daniel Veillard | d4501d7 | 2005-07-24 14:27:16 +0000 | [diff] [blame] | 377 | * @report: the associated report string |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 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, |
Daniel Veillard | e70375c | 2005-07-30 21:09:12 +0000 | [diff] [blame] | 385 | xmlSchematronPatternPtr pat, xmlNodePtr node, |
| 386 | xmlChar *context, xmlChar *report) |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 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; |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 415 | ret->pattern = pattern; |
Daniel Veillard | d4501d7 | 2005-07-24 14:27:16 +0000 | [diff] [blame] | 416 | ret->report = report; |
Daniel Veillard | c740a17 | 2005-07-31 12:17:24 +0000 | [diff] [blame] | 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 | } |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 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); |
Daniel Veillard | d4501d7 | 2005-07-24 14:27:16 +0000 | [diff] [blame] | 458 | if (rules->report != NULL) |
| 459 | xmlFree(rules->report); |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 460 | xmlFree(rules); |
| 461 | rules = next; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | /** |
Daniel Veillard | e70375c | 2005-07-30 21:09:12 +0000 | [diff] [blame] | 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; |
Daniel Veillard | c740a17 | 2005-07-31 12:17:24 +0000 | [diff] [blame] | 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 | } |
Daniel Veillard | e70375c | 2005-07-30 21:09:12 +0000 | [diff] [blame] | 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 | /** |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 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) |
Rob Richards | 54a8f67 | 2005-10-07 02:33:00 +0000 | [diff] [blame] | 565 | xmlFree((char **) schema->namespaces); |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 566 | |
| 567 | xmlSchematronFreeRules(schema->rules); |
Daniel Veillard | e70375c | 2005-07-30 21:09:12 +0000 | [diff] [blame] | 568 | xmlSchematronFreePatterns(schema->patterns); |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 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); |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 602 | ret->includes = NULL; |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 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) |
Rob Richards | 54a8f67 | 2005-10-07 02:33:00 +0000 | [diff] [blame] | 712 | xmlFree((char **) ctxt->namespaces); |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 713 | xmlDictFree(ctxt->dict); |
| 714 | xmlFree(ctxt); |
| 715 | } |
| 716 | |
| 717 | /** |
| 718 | * xmlSchematronPushInclude: |
| 719 | * @ctxt: the schema parser context |
| 720 | * @doc: the included document |
| 721 | * @cur: the current include node |
| 722 | * |
| 723 | * Add an included document |
| 724 | */ |
| 725 | static void |
| 726 | xmlSchematronPushInclude(xmlSchematronParserCtxtPtr ctxt, |
| 727 | xmlDocPtr doc, xmlNodePtr cur) |
| 728 | { |
| 729 | if (ctxt->includes == NULL) { |
| 730 | ctxt->maxIncludes = 10; |
| 731 | ctxt->includes = (xmlNodePtr *) |
| 732 | xmlMalloc(ctxt->maxIncludes * 2 * sizeof(xmlNodePtr)); |
| 733 | if (ctxt->includes == NULL) { |
| 734 | xmlSchematronPErrMemory(NULL, "allocating parser includes", |
| 735 | NULL); |
| 736 | return; |
| 737 | } |
| 738 | ctxt->nbIncludes = 0; |
| 739 | } else if (ctxt->nbIncludes + 2 >= ctxt->maxIncludes) { |
| 740 | xmlNodePtr *tmp; |
| 741 | |
| 742 | tmp = (xmlNodePtr *) |
| 743 | xmlRealloc(ctxt->includes, ctxt->maxIncludes * 4 * |
| 744 | sizeof(xmlNodePtr)); |
| 745 | if (tmp == NULL) { |
| 746 | xmlSchematronPErrMemory(NULL, "allocating parser includes", |
| 747 | NULL); |
| 748 | return; |
| 749 | } |
| 750 | ctxt->includes = tmp; |
| 751 | ctxt->maxIncludes *= 2; |
| 752 | } |
| 753 | ctxt->includes[2 * ctxt->nbIncludes] = cur; |
| 754 | ctxt->includes[2 * ctxt->nbIncludes + 1] = (xmlNodePtr) doc; |
| 755 | ctxt->nbIncludes++; |
| 756 | } |
| 757 | |
| 758 | /** |
| 759 | * xmlSchematronPopInclude: |
| 760 | * @ctxt: the schema parser context |
| 761 | * |
| 762 | * Pop an include level. The included document is being freed |
| 763 | * |
| 764 | * Returns the node immediately following the include or NULL if the |
| 765 | * include list was empty. |
| 766 | */ |
| 767 | static xmlNodePtr |
| 768 | xmlSchematronPopInclude(xmlSchematronParserCtxtPtr ctxt) |
| 769 | { |
| 770 | xmlDocPtr doc; |
| 771 | xmlNodePtr ret; |
| 772 | |
| 773 | if (ctxt->nbIncludes <= 0) |
| 774 | return(NULL); |
| 775 | ctxt->nbIncludes--; |
| 776 | doc = (xmlDocPtr) ctxt->includes[2 * ctxt->nbIncludes + 1]; |
| 777 | ret = ctxt->includes[2 * ctxt->nbIncludes]; |
| 778 | xmlFreeDoc(doc); |
| 779 | if (ret != NULL) |
| 780 | ret = ret->next; |
| 781 | if (ret == NULL) |
| 782 | return(xmlSchematronPopInclude(ctxt)); |
| 783 | return(ret); |
| 784 | } |
| 785 | |
| 786 | /** |
| 787 | * xmlSchematronAddNamespace: |
| 788 | * @ctxt: the schema parser context |
| 789 | * @prefix: the namespace prefix |
| 790 | * @ns: the namespace name |
| 791 | * |
| 792 | * Add a namespace definition in the context |
| 793 | */ |
| 794 | static void |
| 795 | xmlSchematronAddNamespace(xmlSchematronParserCtxtPtr ctxt, |
| 796 | const xmlChar *prefix, const xmlChar *ns) |
| 797 | { |
| 798 | if (ctxt->namespaces == NULL) { |
| 799 | ctxt->maxNamespaces = 10; |
| 800 | ctxt->namespaces = (const xmlChar **) |
| 801 | xmlMalloc(ctxt->maxNamespaces * 2 * sizeof(const xmlChar *)); |
| 802 | if (ctxt->namespaces == NULL) { |
| 803 | xmlSchematronPErrMemory(NULL, "allocating parser namespaces", |
| 804 | NULL); |
| 805 | return; |
| 806 | } |
| 807 | ctxt->nbNamespaces = 0; |
| 808 | } else if (ctxt->nbNamespaces + 2 >= ctxt->maxNamespaces) { |
| 809 | const xmlChar **tmp; |
| 810 | |
| 811 | tmp = (const xmlChar **) |
Rob Richards | 54a8f67 | 2005-10-07 02:33:00 +0000 | [diff] [blame] | 812 | xmlRealloc((xmlChar **) ctxt->namespaces, ctxt->maxNamespaces * 4 * |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 813 | sizeof(const xmlChar *)); |
| 814 | if (tmp == NULL) { |
| 815 | xmlSchematronPErrMemory(NULL, "allocating parser namespaces", |
| 816 | NULL); |
| 817 | return; |
| 818 | } |
| 819 | ctxt->namespaces = tmp; |
| 820 | ctxt->maxNamespaces *= 2; |
| 821 | } |
| 822 | ctxt->namespaces[2 * ctxt->nbNamespaces] = |
| 823 | xmlDictLookup(ctxt->dict, ns, -1); |
| 824 | ctxt->namespaces[2 * ctxt->nbNamespaces + 1] = |
| 825 | xmlDictLookup(ctxt->dict, prefix, -1); |
| 826 | ctxt->nbNamespaces++; |
| 827 | ctxt->namespaces[2 * ctxt->nbNamespaces] = NULL; |
| 828 | ctxt->namespaces[2 * ctxt->nbNamespaces + 1] = NULL; |
| 829 | |
| 830 | } |
| 831 | |
| 832 | /** |
| 833 | * xmlSchematronParseRule: |
| 834 | * @ctxt: a schema validation context |
| 835 | * @rule: the rule node |
| 836 | * |
| 837 | * parse a rule element |
| 838 | */ |
| 839 | static void |
Daniel Veillard | e70375c | 2005-07-30 21:09:12 +0000 | [diff] [blame] | 840 | xmlSchematronParseRule(xmlSchematronParserCtxtPtr ctxt, |
| 841 | xmlSchematronPatternPtr pattern, |
| 842 | xmlNodePtr rule) |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 843 | { |
| 844 | xmlNodePtr cur; |
| 845 | int nbChecks = 0; |
| 846 | xmlChar *test; |
| 847 | xmlChar *context; |
Daniel Veillard | d4501d7 | 2005-07-24 14:27:16 +0000 | [diff] [blame] | 848 | xmlChar *report; |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 849 | xmlSchematronRulePtr ruleptr; |
| 850 | xmlSchematronTestPtr testptr; |
| 851 | |
| 852 | if ((ctxt == NULL) || (rule == NULL)) return; |
| 853 | |
| 854 | context = xmlGetNoNsProp(rule, BAD_CAST "context"); |
| 855 | if (context == NULL) { |
| 856 | xmlSchematronPErr(ctxt, rule, |
| 857 | XML_SCHEMAP_NOROOT, |
| 858 | "rule has no context attribute", |
| 859 | NULL, NULL); |
| 860 | return; |
| 861 | } else if (context[0] == 0) { |
| 862 | xmlSchematronPErr(ctxt, rule, |
| 863 | XML_SCHEMAP_NOROOT, |
| 864 | "rule has an empty context attribute", |
| 865 | NULL, NULL); |
| 866 | xmlFree(context); |
| 867 | return; |
| 868 | } else { |
Daniel Veillard | e70375c | 2005-07-30 21:09:12 +0000 | [diff] [blame] | 869 | ruleptr = xmlSchematronAddRule(ctxt, ctxt->schema, pattern, |
| 870 | rule, context, NULL); |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 871 | if (ruleptr == NULL) { |
| 872 | xmlFree(context); |
| 873 | return; |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | cur = rule->children; |
| 878 | NEXT_SCHEMATRON(cur); |
| 879 | while (cur != NULL) { |
| 880 | if (IS_SCHEMATRON(cur, "assert")) { |
| 881 | nbChecks++; |
| 882 | test = xmlGetNoNsProp(cur, BAD_CAST "test"); |
| 883 | if (test == NULL) { |
| 884 | xmlSchematronPErr(ctxt, cur, |
| 885 | XML_SCHEMAP_NOROOT, |
| 886 | "assert has no test attribute", |
| 887 | NULL, NULL); |
| 888 | } else if (test[0] == 0) { |
| 889 | xmlSchematronPErr(ctxt, cur, |
| 890 | XML_SCHEMAP_NOROOT, |
| 891 | "assert has an empty test attribute", |
| 892 | NULL, NULL); |
| 893 | xmlFree(test); |
| 894 | } else { |
Daniel Veillard | d4501d7 | 2005-07-24 14:27:16 +0000 | [diff] [blame] | 895 | /* TODO will need dynamic processing instead */ |
| 896 | report = xmlNodeGetContent(cur); |
| 897 | |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 898 | testptr = xmlSchematronAddTest(ctxt, XML_SCHEMATRON_ASSERT, |
Daniel Veillard | d4501d7 | 2005-07-24 14:27:16 +0000 | [diff] [blame] | 899 | ruleptr, cur, test, report); |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 900 | if (testptr == NULL) |
| 901 | xmlFree(test); |
| 902 | } |
| 903 | } else if (IS_SCHEMATRON(cur, "report")) { |
| 904 | nbChecks++; |
| 905 | test = xmlGetNoNsProp(cur, BAD_CAST "test"); |
| 906 | if (test == NULL) { |
| 907 | xmlSchematronPErr(ctxt, cur, |
| 908 | XML_SCHEMAP_NOROOT, |
| 909 | "assert has no test attribute", |
| 910 | NULL, NULL); |
| 911 | } else if (test[0] == 0) { |
| 912 | xmlSchematronPErr(ctxt, cur, |
| 913 | XML_SCHEMAP_NOROOT, |
| 914 | "assert has an empty test attribute", |
| 915 | NULL, NULL); |
| 916 | xmlFree(test); |
| 917 | } else { |
Daniel Veillard | d4501d7 | 2005-07-24 14:27:16 +0000 | [diff] [blame] | 918 | /* TODO will need dynamic processing instead */ |
| 919 | report = xmlNodeGetContent(cur); |
| 920 | |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 921 | testptr = xmlSchematronAddTest(ctxt, XML_SCHEMATRON_REPORT, |
Daniel Veillard | d4501d7 | 2005-07-24 14:27:16 +0000 | [diff] [blame] | 922 | ruleptr, cur, test, report); |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 923 | if (testptr == NULL) |
| 924 | xmlFree(test); |
| 925 | } |
| 926 | } else { |
| 927 | xmlSchematronPErr(ctxt, cur, |
| 928 | XML_SCHEMAP_NOROOT, |
| 929 | "Expecting an assert or a report element instead of %s", |
| 930 | cur->name, NULL); |
| 931 | } |
| 932 | cur = cur->next; |
| 933 | NEXT_SCHEMATRON(cur); |
| 934 | } |
| 935 | if (nbChecks == 0) { |
| 936 | xmlSchematronPErr(ctxt, rule, |
| 937 | XML_SCHEMAP_NOROOT, |
| 938 | "rule has no assert nor report element", NULL, NULL); |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | /** |
| 943 | * xmlSchematronParsePattern: |
| 944 | * @ctxt: a schema validation context |
| 945 | * @pat: the pattern node |
| 946 | * |
| 947 | * parse a pattern element |
| 948 | */ |
| 949 | static void |
| 950 | xmlSchematronParsePattern(xmlSchematronParserCtxtPtr ctxt, xmlNodePtr pat) |
| 951 | { |
| 952 | xmlNodePtr cur; |
Daniel Veillard | e70375c | 2005-07-30 21:09:12 +0000 | [diff] [blame] | 953 | xmlSchematronPatternPtr pattern; |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 954 | int nbRules = 0; |
Daniel Veillard | e70375c | 2005-07-30 21:09:12 +0000 | [diff] [blame] | 955 | xmlChar *id; |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 956 | |
| 957 | if ((ctxt == NULL) || (pat == NULL)) return; |
| 958 | |
Daniel Veillard | e70375c | 2005-07-30 21:09:12 +0000 | [diff] [blame] | 959 | id = xmlGetNoNsProp(pat, BAD_CAST "id"); |
| 960 | if (id == NULL) { |
| 961 | id = xmlGetNoNsProp(pat, BAD_CAST "name"); |
| 962 | } |
| 963 | pattern = xmlSchematronAddPattern(ctxt, ctxt->schema, pat, id); |
| 964 | if (pattern == NULL) { |
| 965 | if (id != NULL) |
| 966 | xmlFree(id); |
| 967 | return; |
| 968 | } |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 969 | cur = pat->children; |
| 970 | NEXT_SCHEMATRON(cur); |
| 971 | while (cur != NULL) { |
| 972 | if (IS_SCHEMATRON(cur, "rule")) { |
Daniel Veillard | e70375c | 2005-07-30 21:09:12 +0000 | [diff] [blame] | 973 | xmlSchematronParseRule(ctxt, pattern, cur); |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 974 | nbRules++; |
| 975 | } else { |
| 976 | xmlSchematronPErr(ctxt, cur, |
| 977 | XML_SCHEMAP_NOROOT, |
| 978 | "Expecting a rule element instead of %s", cur->name, NULL); |
| 979 | } |
| 980 | cur = cur->next; |
| 981 | NEXT_SCHEMATRON(cur); |
| 982 | } |
| 983 | if (nbRules == 0) { |
| 984 | xmlSchematronPErr(ctxt, pat, |
| 985 | XML_SCHEMAP_NOROOT, |
| 986 | "Pattern has no rule element", NULL, NULL); |
| 987 | } |
| 988 | } |
| 989 | |
| 990 | /** |
| 991 | * xmlSchematronLoadInclude: |
| 992 | * @ctxt: a schema validation context |
| 993 | * @cur: the include element |
| 994 | * |
| 995 | * Load the include document, Push the current pointer |
| 996 | * |
| 997 | * Returns the updated node pointer |
| 998 | */ |
| 999 | static xmlNodePtr |
| 1000 | xmlSchematronLoadInclude(xmlSchematronParserCtxtPtr ctxt, xmlNodePtr cur) |
| 1001 | { |
| 1002 | xmlNodePtr ret = NULL; |
| 1003 | xmlDocPtr doc = NULL; |
| 1004 | xmlChar *href = NULL; |
| 1005 | xmlChar *base = NULL; |
| 1006 | xmlChar *URI = NULL; |
| 1007 | |
| 1008 | if ((ctxt == NULL) || (cur == NULL)) |
| 1009 | return(NULL); |
| 1010 | |
| 1011 | href = xmlGetNoNsProp(cur, BAD_CAST "href"); |
| 1012 | if (href == NULL) { |
| 1013 | xmlSchematronPErr(ctxt, cur, |
| 1014 | XML_SCHEMAP_NOROOT, |
| 1015 | "Include has no href attribute", NULL, NULL); |
| 1016 | return(cur->next); |
| 1017 | } |
| 1018 | |
| 1019 | /* do the URI base composition, load and find the root */ |
| 1020 | base = xmlNodeGetBase(cur->doc, cur); |
| 1021 | URI = xmlBuildURI(href, base); |
| 1022 | doc = xmlReadFile((const char *) URI, NULL, SCHEMATRON_PARSE_OPTIONS); |
| 1023 | if (doc == NULL) { |
| 1024 | xmlSchematronPErr(ctxt, cur, |
| 1025 | XML_SCHEMAP_FAILED_LOAD, |
| 1026 | "could not load include '%s'.\n", |
| 1027 | URI, NULL); |
| 1028 | goto done; |
| 1029 | } |
| 1030 | ret = xmlDocGetRootElement(doc); |
| 1031 | if (ret == NULL) { |
| 1032 | xmlSchematronPErr(ctxt, cur, |
| 1033 | XML_SCHEMAP_FAILED_LOAD, |
| 1034 | "could not find root from include '%s'.\n", |
| 1035 | URI, NULL); |
| 1036 | goto done; |
| 1037 | } |
| 1038 | |
| 1039 | /* Success, push the include for rollback on exit */ |
| 1040 | xmlSchematronPushInclude(ctxt, doc, cur); |
| 1041 | |
| 1042 | done: |
| 1043 | if (ret == NULL) { |
| 1044 | if (doc != NULL) |
| 1045 | xmlFreeDoc(doc); |
| 1046 | } |
| 1047 | if (href == NULL) |
| 1048 | xmlFree(href); |
| 1049 | if (base == NULL) |
| 1050 | xmlFree(base); |
| 1051 | if (URI == NULL) |
| 1052 | xmlFree(URI); |
| 1053 | return(ret); |
| 1054 | } |
| 1055 | |
| 1056 | /** |
| 1057 | * xmlSchematronParse: |
| 1058 | * @ctxt: a schema validation context |
| 1059 | * |
| 1060 | * parse a schema definition resource and build an internal |
| 1061 | * XML Shema struture which can be used to validate instances. |
| 1062 | * |
| 1063 | * Returns the internal XML Schematron structure built from the resource or |
| 1064 | * NULL in case of error |
| 1065 | */ |
| 1066 | xmlSchematronPtr |
| 1067 | xmlSchematronParse(xmlSchematronParserCtxtPtr ctxt) |
| 1068 | { |
| 1069 | xmlSchematronPtr ret = NULL; |
| 1070 | xmlDocPtr doc; |
| 1071 | xmlNodePtr root, cur; |
| 1072 | int preserve = 0; |
| 1073 | |
| 1074 | if (ctxt == NULL) |
| 1075 | return (NULL); |
| 1076 | |
| 1077 | ctxt->nberrors = 0; |
| 1078 | |
| 1079 | /* |
| 1080 | * First step is to parse the input document into an DOM/Infoset |
| 1081 | */ |
| 1082 | if (ctxt->URL != NULL) { |
| 1083 | doc = xmlReadFile((const char *) ctxt->URL, NULL, |
| 1084 | SCHEMATRON_PARSE_OPTIONS); |
| 1085 | if (doc == NULL) { |
| 1086 | xmlSchematronPErr(ctxt, NULL, |
| 1087 | XML_SCHEMAP_FAILED_LOAD, |
| 1088 | "xmlSchematronParse: could not load '%s'.\n", |
| 1089 | ctxt->URL, NULL); |
| 1090 | return (NULL); |
| 1091 | } |
Daniel Veillard | eaecb3e | 2005-07-31 13:43:14 +0000 | [diff] [blame] | 1092 | ctxt->preserve = 0; |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 1093 | } else if (ctxt->buffer != NULL) { |
| 1094 | doc = xmlReadMemory(ctxt->buffer, ctxt->size, NULL, NULL, |
| 1095 | SCHEMATRON_PARSE_OPTIONS); |
| 1096 | if (doc == NULL) { |
| 1097 | xmlSchematronPErr(ctxt, NULL, |
| 1098 | XML_SCHEMAP_FAILED_PARSE, |
| 1099 | "xmlSchematronParse: could not parse.\n", |
| 1100 | NULL, NULL); |
| 1101 | return (NULL); |
| 1102 | } |
| 1103 | doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
| 1104 | ctxt->URL = xmlDictLookup(ctxt->dict, BAD_CAST "in_memory_buffer", -1); |
Daniel Veillard | eaecb3e | 2005-07-31 13:43:14 +0000 | [diff] [blame] | 1105 | ctxt->preserve = 0; |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 1106 | } else if (ctxt->doc != NULL) { |
| 1107 | doc = ctxt->doc; |
| 1108 | preserve = 1; |
Daniel Veillard | eaecb3e | 2005-07-31 13:43:14 +0000 | [diff] [blame] | 1109 | ctxt->preserve = 1; |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 1110 | } else { |
| 1111 | xmlSchematronPErr(ctxt, NULL, |
| 1112 | XML_SCHEMAP_NOTHING_TO_PARSE, |
| 1113 | "xmlSchematronParse: could not parse.\n", |
| 1114 | NULL, NULL); |
| 1115 | return (NULL); |
| 1116 | } |
| 1117 | |
| 1118 | /* |
| 1119 | * Then extract the root and Schematron parse it |
| 1120 | */ |
| 1121 | root = xmlDocGetRootElement(doc); |
| 1122 | if (root == NULL) { |
| 1123 | xmlSchematronPErr(ctxt, (xmlNodePtr) doc, |
| 1124 | XML_SCHEMAP_NOROOT, |
| 1125 | "The schema has no document element.\n", NULL, NULL); |
| 1126 | if (!preserve) { |
| 1127 | xmlFreeDoc(doc); |
| 1128 | } |
| 1129 | return (NULL); |
| 1130 | } |
| 1131 | |
| 1132 | if (!IS_SCHEMATRON(root, "schema")) { |
| 1133 | xmlSchematronPErr(ctxt, root, |
| 1134 | XML_SCHEMAP_NOROOT, |
| 1135 | "The XML document '%s' is not a XML schematron document", |
| 1136 | ctxt->URL, NULL); |
| 1137 | goto exit; |
| 1138 | } |
| 1139 | ret = xmlSchematronNewSchematron(ctxt); |
| 1140 | if (ret == NULL) |
| 1141 | goto exit; |
| 1142 | ctxt->schema = ret; |
| 1143 | |
| 1144 | /* |
| 1145 | * scan the schema elements |
| 1146 | */ |
| 1147 | cur = root->children; |
| 1148 | NEXT_SCHEMATRON(cur); |
| 1149 | if (IS_SCHEMATRON(cur, "title")) { |
| 1150 | xmlChar *title = xmlNodeGetContent(cur); |
| 1151 | if (title != NULL) { |
| 1152 | ret->title = xmlDictLookup(ret->dict, title, -1); |
| 1153 | xmlFree(title); |
| 1154 | } |
| 1155 | cur = cur->next; |
| 1156 | NEXT_SCHEMATRON(cur); |
| 1157 | } |
| 1158 | while (IS_SCHEMATRON(cur, "ns")) { |
| 1159 | xmlChar *prefix = xmlGetNoNsProp(cur, BAD_CAST "prefix"); |
| 1160 | xmlChar *uri = xmlGetNoNsProp(cur, BAD_CAST "uri"); |
| 1161 | if ((uri == NULL) || (uri[0] == 0)) { |
| 1162 | xmlSchematronPErr(ctxt, cur, |
| 1163 | XML_SCHEMAP_NOROOT, |
| 1164 | "ns element has no uri", NULL, NULL); |
| 1165 | } |
| 1166 | if ((prefix == NULL) || (prefix[0] == 0)) { |
| 1167 | xmlSchematronPErr(ctxt, cur, |
| 1168 | XML_SCHEMAP_NOROOT, |
| 1169 | "ns element has no prefix", NULL, NULL); |
| 1170 | } |
| 1171 | if ((prefix) && (uri)) { |
| 1172 | xmlXPathRegisterNs(ctxt->xctxt, prefix, uri); |
| 1173 | xmlSchematronAddNamespace(ctxt, prefix, uri); |
| 1174 | ret->nbNs++; |
| 1175 | } |
| 1176 | if (uri) |
| 1177 | xmlFree(uri); |
| 1178 | if (prefix) |
| 1179 | xmlFree(prefix); |
| 1180 | cur = cur->next; |
| 1181 | NEXT_SCHEMATRON(cur); |
| 1182 | } |
| 1183 | while (cur != NULL) { |
| 1184 | if (IS_SCHEMATRON(cur, "pattern")) { |
| 1185 | xmlSchematronParsePattern(ctxt, cur); |
| 1186 | ret->nbPattern++; |
| 1187 | } else { |
| 1188 | xmlSchematronPErr(ctxt, cur, |
| 1189 | XML_SCHEMAP_NOROOT, |
| 1190 | "Expecting a pattern element instead of %s", cur->name, NULL); |
| 1191 | } |
| 1192 | cur = cur->next; |
| 1193 | NEXT_SCHEMATRON(cur); |
| 1194 | } |
| 1195 | if (ret->nbPattern == 0) { |
| 1196 | xmlSchematronPErr(ctxt, root, |
| 1197 | XML_SCHEMAP_NOROOT, |
| 1198 | "The schematron document '%s' has no pattern", |
| 1199 | ctxt->URL, NULL); |
| 1200 | goto exit; |
| 1201 | } |
Daniel Veillard | eaecb3e | 2005-07-31 13:43:14 +0000 | [diff] [blame] | 1202 | /* the original document must be kept for reporting */ |
| 1203 | ret->doc = doc; |
| 1204 | preserve = 1; |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 1205 | |
| 1206 | exit: |
| 1207 | if (!preserve) { |
| 1208 | xmlFreeDoc(doc); |
| 1209 | } |
| 1210 | if (ctxt->nberrors != 0) { |
| 1211 | xmlSchematronFree(ret); |
| 1212 | ret = NULL; |
| 1213 | } else { |
| 1214 | ret->namespaces = ctxt->namespaces; |
| 1215 | ret->nbNamespaces = ctxt->nbNamespaces; |
| 1216 | ctxt->namespaces = NULL; |
| 1217 | } |
| 1218 | return (ret); |
| 1219 | } |
| 1220 | |
| 1221 | /************************************************************************ |
| 1222 | * * |
| 1223 | * Schematrontron Reports handler * |
| 1224 | * * |
| 1225 | ************************************************************************/ |
| 1226 | |
Daniel Veillard | d541c8f | 2005-07-31 16:49:51 +0000 | [diff] [blame] | 1227 | static xmlNodePtr |
| 1228 | xmlSchematronGetNode(xmlSchematronValidCtxtPtr ctxt, |
| 1229 | xmlNodePtr cur, const xmlChar *xpath) { |
| 1230 | xmlNodePtr node = NULL; |
| 1231 | xmlXPathObjectPtr ret; |
| 1232 | |
| 1233 | if ((ctxt == NULL) || (cur == NULL) || (xpath == NULL)) |
| 1234 | return(NULL); |
| 1235 | |
| 1236 | ctxt->xctxt->doc = cur->doc; |
| 1237 | ctxt->xctxt->node = cur; |
| 1238 | ret = xmlXPathEval(xpath, ctxt->xctxt); |
| 1239 | if (ret == NULL) |
| 1240 | return(NULL); |
| 1241 | |
| 1242 | if ((ret->type == XPATH_NODESET) && |
| 1243 | (ret->nodesetval != NULL) && (ret->nodesetval->nodeNr > 0)) |
| 1244 | node = ret->nodesetval->nodeTab[0]; |
| 1245 | |
| 1246 | xmlXPathFreeObject(ret); |
| 1247 | return(node); |
| 1248 | } |
| 1249 | |
Daniel Veillard | d4501d7 | 2005-07-24 14:27:16 +0000 | [diff] [blame] | 1250 | /** |
| 1251 | * xmlSchematronReportOutput: |
| 1252 | * @ctxt: the validation context |
| 1253 | * @cur: the current node tested |
| 1254 | * @msg: the message output |
| 1255 | * |
| 1256 | * Output part of the report to whatever channel the user selected |
| 1257 | */ |
| 1258 | static void |
| 1259 | xmlSchematronReportOutput(xmlSchematronValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 1260 | xmlNodePtr cur ATTRIBUTE_UNUSED, |
Daniel Veillard | c740a17 | 2005-07-31 12:17:24 +0000 | [diff] [blame] | 1261 | const char *msg) { |
Daniel Veillard | d4501d7 | 2005-07-24 14:27:16 +0000 | [diff] [blame] | 1262 | /* TODO */ |
| 1263 | fprintf(stderr, "%s", msg); |
| 1264 | } |
| 1265 | |
| 1266 | /** |
Daniel Veillard | eaecb3e | 2005-07-31 13:43:14 +0000 | [diff] [blame] | 1267 | * xmlSchematronFormatReport: |
| 1268 | * @ctxt: the validation context |
| 1269 | * @test: the test node |
| 1270 | * @cur: the current node tested |
| 1271 | * |
| 1272 | * Build the string being reported to the user. |
| 1273 | * |
| 1274 | * Returns a report string or NULL in case of error. The string needs |
| 1275 | * to be deallocated by teh caller |
| 1276 | */ |
| 1277 | static xmlChar * |
| 1278 | xmlSchematronFormatReport(xmlSchematronValidCtxtPtr ctxt, |
| 1279 | xmlNodePtr test, xmlNodePtr cur) { |
| 1280 | xmlChar *ret = NULL; |
Daniel Veillard | d541c8f | 2005-07-31 16:49:51 +0000 | [diff] [blame] | 1281 | xmlNodePtr child, node; |
Daniel Veillard | eaecb3e | 2005-07-31 13:43:14 +0000 | [diff] [blame] | 1282 | |
| 1283 | if ((test == NULL) || (cur == NULL)) |
| 1284 | return(ret); |
| 1285 | |
| 1286 | child = test->children; |
| 1287 | while (child != NULL) { |
| 1288 | if ((child->type == XML_TEXT_NODE) || |
| 1289 | (child->type == XML_CDATA_SECTION_NODE)) |
| 1290 | ret = xmlStrcat(ret, child->content); |
| 1291 | else if (IS_SCHEMATRON(child, "name")) { |
Daniel Veillard | d541c8f | 2005-07-31 16:49:51 +0000 | [diff] [blame] | 1292 | xmlChar *path; |
| 1293 | |
| 1294 | path = xmlGetNoNsProp(child, BAD_CAST "path"); |
| 1295 | |
| 1296 | node = cur; |
| 1297 | if (path != NULL) { |
| 1298 | node = xmlSchematronGetNode(ctxt, cur, path); |
| 1299 | if (node == NULL) |
| 1300 | node = cur; |
| 1301 | xmlFree(path); |
| 1302 | } |
| 1303 | |
| 1304 | if ((node->ns == NULL) || (node->ns->prefix == NULL)) |
| 1305 | ret = xmlStrcat(ret, node->name); |
Daniel Veillard | eaecb3e | 2005-07-31 13:43:14 +0000 | [diff] [blame] | 1306 | else { |
Daniel Veillard | d541c8f | 2005-07-31 16:49:51 +0000 | [diff] [blame] | 1307 | ret = xmlStrcat(ret, node->ns->prefix); |
Daniel Veillard | eaecb3e | 2005-07-31 13:43:14 +0000 | [diff] [blame] | 1308 | ret = xmlStrcat(ret, BAD_CAST ":"); |
Daniel Veillard | d541c8f | 2005-07-31 16:49:51 +0000 | [diff] [blame] | 1309 | ret = xmlStrcat(ret, node->name); |
Daniel Veillard | eaecb3e | 2005-07-31 13:43:14 +0000 | [diff] [blame] | 1310 | } |
| 1311 | } else { |
| 1312 | child = child->next; |
| 1313 | continue; |
| 1314 | } |
| 1315 | |
| 1316 | /* |
| 1317 | * remove superfluous \n |
| 1318 | */ |
| 1319 | if (ret != NULL) { |
| 1320 | int len = xmlStrlen(ret); |
| 1321 | xmlChar c; |
| 1322 | |
| 1323 | if (len > 0) { |
| 1324 | c = ret[len - 1]; |
| 1325 | if ((c == ' ') || (c == '\n') || (c == '\r') || (c == '\t')) { |
| 1326 | while ((c == ' ') || (c == '\n') || |
| 1327 | (c == '\r') || (c == '\t')) { |
| 1328 | len--; |
| 1329 | if (len == 0) |
| 1330 | break; |
| 1331 | c = ret[len - 1]; |
| 1332 | } |
| 1333 | ret[len] = ' '; |
| 1334 | ret[len + 1] = 0; |
| 1335 | } |
| 1336 | } |
| 1337 | } |
| 1338 | |
| 1339 | child = child->next; |
| 1340 | } |
| 1341 | return(ret); |
| 1342 | } |
| 1343 | |
| 1344 | /** |
Daniel Veillard | d4501d7 | 2005-07-24 14:27:16 +0000 | [diff] [blame] | 1345 | * xmlSchematronReportSuccess: |
| 1346 | * @ctxt: the validation context |
| 1347 | * @test: the compiled test |
| 1348 | * @cur: the current node tested |
| 1349 | * @success: boolean value for the result |
| 1350 | * |
| 1351 | * called from the validation engine when an assert or report test have |
| 1352 | * been done. |
| 1353 | */ |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 1354 | static void |
| 1355 | xmlSchematronReportSuccess(xmlSchematronValidCtxtPtr ctxt, |
Daniel Veillard | d4501d7 | 2005-07-24 14:27:16 +0000 | [diff] [blame] | 1356 | xmlSchematronTestPtr test, xmlNodePtr cur, int success) { |
| 1357 | if ((ctxt == NULL) || (cur == NULL) || (test == NULL)) |
| 1358 | return; |
| 1359 | /* if quiet and not SVRL report only failures */ |
| 1360 | if ((ctxt->flags & XML_SCHEMATRON_OUT_QUIET) && |
Daniel Veillard | e70375c | 2005-07-30 21:09:12 +0000 | [diff] [blame] | 1361 | ((ctxt->flags & XML_SCHEMATRON_OUT_XML) == 0) && |
Daniel Veillard | c740a17 | 2005-07-31 12:17:24 +0000 | [diff] [blame] | 1362 | (test->type == XML_SCHEMATRON_REPORT)) |
Daniel Veillard | d4501d7 | 2005-07-24 14:27:16 +0000 | [diff] [blame] | 1363 | return; |
| 1364 | if (ctxt->flags & XML_SCHEMATRON_OUT_XML) { |
| 1365 | TODO |
| 1366 | } else { |
| 1367 | xmlChar *path; |
| 1368 | char msg[1000]; |
| 1369 | long line; |
Daniel Veillard | eaecb3e | 2005-07-31 13:43:14 +0000 | [diff] [blame] | 1370 | const xmlChar *report = NULL; |
Daniel Veillard | d4501d7 | 2005-07-24 14:27:16 +0000 | [diff] [blame] | 1371 | |
Daniel Veillard | e70375c | 2005-07-30 21:09:12 +0000 | [diff] [blame] | 1372 | if (((test->type == XML_SCHEMATRON_REPORT) & (!success)) || |
| 1373 | ((test->type == XML_SCHEMATRON_ASSERT) & (success))) |
Daniel Veillard | d4501d7 | 2005-07-24 14:27:16 +0000 | [diff] [blame] | 1374 | return; |
| 1375 | line = xmlGetLineNo(cur); |
| 1376 | path = xmlGetNodePath(cur); |
| 1377 | if (path == NULL) |
| 1378 | path = (xmlChar *) cur->name; |
Daniel Veillard | eaecb3e | 2005-07-31 13:43:14 +0000 | [diff] [blame] | 1379 | #if 0 |
Daniel Veillard | d4501d7 | 2005-07-24 14:27:16 +0000 | [diff] [blame] | 1380 | if ((test->report != NULL) && (test->report[0] != 0)) |
| 1381 | report = test->report; |
Daniel Veillard | eaecb3e | 2005-07-31 13:43:14 +0000 | [diff] [blame] | 1382 | #endif |
| 1383 | if (test->node != NULL) |
| 1384 | report = xmlSchematronFormatReport(ctxt, test->node, cur); |
| 1385 | if (report == NULL) { |
| 1386 | if (test->type == XML_SCHEMATRON_ASSERT) { |
| 1387 | snprintf(msg, 999, "%s line %ld: node failed assert\n", |
| 1388 | (const char *) path, line); |
| 1389 | } else { |
| 1390 | snprintf(msg, 999, "%s line %ld: node failed report\n", |
| 1391 | (const char *) path, line); |
| 1392 | } |
Daniel Veillard | d4501d7 | 2005-07-24 14:27:16 +0000 | [diff] [blame] | 1393 | } else { |
Daniel Veillard | eaecb3e | 2005-07-31 13:43:14 +0000 | [diff] [blame] | 1394 | snprintf(msg, 999, "%s line %ld: %s\n", (const char *) path, |
| 1395 | line, (const char *) report); |
Daniel Veillard | d541c8f | 2005-07-31 16:49:51 +0000 | [diff] [blame] | 1396 | xmlFree((char *) report); |
Daniel Veillard | d4501d7 | 2005-07-24 14:27:16 +0000 | [diff] [blame] | 1397 | } |
Daniel Veillard | d4501d7 | 2005-07-24 14:27:16 +0000 | [diff] [blame] | 1398 | xmlSchematronReportOutput(ctxt, cur, &msg[0]); |
| 1399 | if ((path != NULL) && (path != (xmlChar *) cur->name)) |
| 1400 | xmlFree(path); |
| 1401 | } |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 1402 | } |
| 1403 | |
Daniel Veillard | c740a17 | 2005-07-31 12:17:24 +0000 | [diff] [blame] | 1404 | /** |
| 1405 | * xmlSchematronReportPattern: |
| 1406 | * @ctxt: the validation context |
| 1407 | * @pattern: the current pattern |
| 1408 | * |
| 1409 | * called from the validation engine when starting to check a pattern |
| 1410 | */ |
| 1411 | static void |
| 1412 | xmlSchematronReportPattern(xmlSchematronValidCtxtPtr ctxt, |
| 1413 | xmlSchematronPatternPtr pattern) { |
| 1414 | if ((ctxt == NULL) || (pattern == NULL)) |
| 1415 | return; |
| 1416 | if (ctxt->flags & XML_SCHEMATRON_OUT_QUIET) |
| 1417 | return; |
| 1418 | if (ctxt->flags & XML_SCHEMATRON_OUT_XML) { |
| 1419 | TODO |
| 1420 | } else { |
| 1421 | char msg[1000]; |
| 1422 | |
| 1423 | if (pattern->name == NULL) |
| 1424 | return; |
| 1425 | snprintf(msg, 999, "Pattern: %s\n", (const char *) pattern->name); |
| 1426 | xmlSchematronReportOutput(ctxt, NULL, &msg[0]); |
| 1427 | } |
| 1428 | } |
| 1429 | |
| 1430 | |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 1431 | /************************************************************************ |
| 1432 | * * |
| 1433 | * Validation against a Schematrontron * |
| 1434 | * * |
| 1435 | ************************************************************************/ |
| 1436 | |
| 1437 | /** |
| 1438 | * xmlSchematronNewValidCtxt: |
| 1439 | * @schema: a precompiled XML Schematrons |
| 1440 | * @options: a set of xmlSchematronValidOptions |
| 1441 | * |
| 1442 | * Create an XML Schematrons validation context based on the given schema. |
| 1443 | * |
| 1444 | * Returns the validation context or NULL in case of error |
| 1445 | */ |
| 1446 | xmlSchematronValidCtxtPtr |
| 1447 | xmlSchematronNewValidCtxt(xmlSchematronPtr schema, int options) |
| 1448 | { |
| 1449 | int i; |
| 1450 | xmlSchematronValidCtxtPtr ret; |
| 1451 | |
Daniel Veillard | c740a17 | 2005-07-31 12:17:24 +0000 | [diff] [blame] | 1452 | ret = (xmlSchematronValidCtxtPtr) xmlMalloc(sizeof(xmlSchematronValidCtxt)); |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 1453 | if (ret == NULL) { |
| 1454 | xmlSchematronVErrMemory(NULL, "allocating validation context", |
| 1455 | NULL); |
| 1456 | return (NULL); |
| 1457 | } |
| 1458 | memset(ret, 0, sizeof(xmlSchematronValidCtxt)); |
| 1459 | ret->type = XML_STRON_CTXT_VALIDATOR; |
| 1460 | ret->schema = schema; |
| 1461 | ret->xctxt = xmlXPathNewContext(NULL); |
Daniel Veillard | d4501d7 | 2005-07-24 14:27:16 +0000 | [diff] [blame] | 1462 | ret->flags = options; |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 1463 | if (ret->xctxt == NULL) { |
| 1464 | xmlSchematronPErrMemory(NULL, "allocating schema parser XPath context", |
| 1465 | NULL); |
| 1466 | xmlSchematronFreeValidCtxt(ret); |
| 1467 | return (NULL); |
| 1468 | } |
| 1469 | for (i = 0;i < schema->nbNamespaces;i++) { |
| 1470 | if ((schema->namespaces[2 * i] == NULL) || |
| 1471 | (schema->namespaces[2 * i + 1] == NULL)) |
| 1472 | break; |
| 1473 | xmlXPathRegisterNs(ret->xctxt, schema->namespaces[2 * i + 1], |
| 1474 | schema->namespaces[2 * i]); |
| 1475 | } |
| 1476 | return (ret); |
| 1477 | } |
| 1478 | |
| 1479 | /** |
| 1480 | * xmlSchematronFreeValidCtxt: |
| 1481 | * @ctxt: the schema validation context |
| 1482 | * |
| 1483 | * Free the resources associated to the schema validation context |
| 1484 | */ |
| 1485 | void |
| 1486 | xmlSchematronFreeValidCtxt(xmlSchematronValidCtxtPtr ctxt) |
| 1487 | { |
| 1488 | if (ctxt == NULL) |
| 1489 | return; |
| 1490 | if (ctxt->xctxt != NULL) |
| 1491 | xmlXPathFreeContext(ctxt->xctxt); |
| 1492 | if (ctxt->dict != NULL) |
| 1493 | xmlDictFree(ctxt->dict); |
| 1494 | xmlFree(ctxt); |
| 1495 | } |
| 1496 | |
| 1497 | static xmlNodePtr |
| 1498 | xmlSchematronNextNode(xmlNodePtr cur) { |
| 1499 | if (cur->children != NULL) { |
| 1500 | /* |
| 1501 | * Do not descend on entities declarations |
| 1502 | */ |
| 1503 | if (cur->children->type != XML_ENTITY_DECL) { |
| 1504 | cur = cur->children; |
| 1505 | /* |
| 1506 | * Skip DTDs |
| 1507 | */ |
| 1508 | if (cur->type != XML_DTD_NODE) |
| 1509 | return(cur); |
| 1510 | } |
| 1511 | } |
| 1512 | |
| 1513 | while (cur->next != NULL) { |
| 1514 | cur = cur->next; |
| 1515 | if ((cur->type != XML_ENTITY_DECL) && |
| 1516 | (cur->type != XML_DTD_NODE)) |
| 1517 | return(cur); |
| 1518 | } |
| 1519 | |
| 1520 | do { |
| 1521 | cur = cur->parent; |
| 1522 | if (cur == NULL) return(NULL); |
| 1523 | if (cur->type == XML_DOCUMENT_NODE) return(NULL); |
| 1524 | if (cur->next != NULL) { |
| 1525 | cur = cur->next; |
| 1526 | return(cur); |
| 1527 | } |
| 1528 | } while (cur != NULL); |
| 1529 | return(cur); |
| 1530 | } |
| 1531 | |
| 1532 | /** |
| 1533 | * xmlSchematronRunTest: |
| 1534 | * @ctxt: the schema validation context |
| 1535 | * @test: the current test |
| 1536 | * @instance: the document instace tree |
| 1537 | * @cur: the current node in the instance |
| 1538 | * |
| 1539 | * Validate a rule against a tree instance at a given position |
| 1540 | * |
| 1541 | * Returns 1 in case of success, 0 if error and -1 in case of internal error |
| 1542 | */ |
| 1543 | static int |
| 1544 | xmlSchematronRunTest(xmlSchematronValidCtxtPtr ctxt, |
| 1545 | xmlSchematronTestPtr test, xmlDocPtr instance, xmlNodePtr cur) |
| 1546 | { |
| 1547 | xmlXPathObjectPtr ret; |
| 1548 | int failed; |
| 1549 | |
| 1550 | failed = 0; |
| 1551 | ctxt->xctxt->doc = instance; |
| 1552 | ctxt->xctxt->node = cur; |
| 1553 | ret = xmlXPathCompiledEval(test->comp, ctxt->xctxt); |
| 1554 | if (ret == NULL) { |
| 1555 | failed = 1; |
Daniel Veillard | eaecb3e | 2005-07-31 13:43:14 +0000 | [diff] [blame] | 1556 | } else { |
| 1557 | switch (ret->type) { |
| 1558 | case XPATH_XSLT_TREE: |
| 1559 | case XPATH_NODESET: |
| 1560 | if ((ret->nodesetval == NULL) || |
| 1561 | (ret->nodesetval->nodeNr == 0)) |
| 1562 | failed = 1; |
| 1563 | break; |
| 1564 | case XPATH_BOOLEAN: |
| 1565 | failed = !ret->boolval; |
| 1566 | break; |
| 1567 | case XPATH_NUMBER: |
| 1568 | if ((xmlXPathIsNaN(ret->floatval)) || |
| 1569 | (ret->floatval == 0.0)) |
| 1570 | failed = 1; |
| 1571 | break; |
| 1572 | case XPATH_STRING: |
| 1573 | if ((ret->stringval == NULL) || |
| 1574 | (ret->stringval[0] == 0)) |
| 1575 | failed = 1; |
| 1576 | break; |
| 1577 | case XPATH_UNDEFINED: |
| 1578 | case XPATH_POINT: |
| 1579 | case XPATH_RANGE: |
| 1580 | case XPATH_LOCATIONSET: |
| 1581 | case XPATH_USERS: |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 1582 | failed = 1; |
Daniel Veillard | eaecb3e | 2005-07-31 13:43:14 +0000 | [diff] [blame] | 1583 | break; |
| 1584 | } |
| 1585 | xmlXPathFreeObject(ret); |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 1586 | } |
Daniel Veillard | eaecb3e | 2005-07-31 13:43:14 +0000 | [diff] [blame] | 1587 | if ((failed) && (test->type == XML_SCHEMATRON_ASSERT)) |
Daniel Veillard | d4501d7 | 2005-07-24 14:27:16 +0000 | [diff] [blame] | 1588 | ctxt->nberrors++; |
Daniel Veillard | eaecb3e | 2005-07-31 13:43:14 +0000 | [diff] [blame] | 1589 | else if ((!failed) && (test->type == XML_SCHEMATRON_REPORT)) |
| 1590 | ctxt->nberrors++; |
| 1591 | |
Daniel Veillard | d4501d7 | 2005-07-24 14:27:16 +0000 | [diff] [blame] | 1592 | xmlSchematronReportSuccess(ctxt, test, cur, !failed); |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 1593 | |
| 1594 | return(!failed); |
| 1595 | } |
| 1596 | |
| 1597 | /** |
| 1598 | * xmlSchematronValidateDoc: |
| 1599 | * @ctxt: the schema validation context |
| 1600 | * @instance: the document instace tree |
| 1601 | * |
| 1602 | * Validate a tree instance against the schematron |
| 1603 | * |
| 1604 | * Returns 0 in case of success, -1 in case of internal error |
| 1605 | * and an error count otherwise. |
| 1606 | */ |
| 1607 | int |
| 1608 | xmlSchematronValidateDoc(xmlSchematronValidCtxtPtr ctxt, xmlDocPtr instance) |
| 1609 | { |
Daniel Veillard | e70375c | 2005-07-30 21:09:12 +0000 | [diff] [blame] | 1610 | xmlNodePtr cur, root; |
| 1611 | xmlSchematronPatternPtr pattern; |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 1612 | xmlSchematronRulePtr rule; |
| 1613 | xmlSchematronTestPtr test; |
| 1614 | |
| 1615 | if ((ctxt == NULL) || (ctxt->schema == NULL) || |
| 1616 | (ctxt->schema->rules == NULL) || (instance == NULL)) |
| 1617 | return(-1); |
| 1618 | ctxt->nberrors = 0; |
Daniel Veillard | e70375c | 2005-07-30 21:09:12 +0000 | [diff] [blame] | 1619 | root = xmlDocGetRootElement(instance); |
| 1620 | if (root == NULL) { |
| 1621 | TODO |
| 1622 | ctxt->nberrors++; |
| 1623 | return(1); |
| 1624 | } |
| 1625 | if ((ctxt->flags & XML_SCHEMATRON_OUT_QUIET) || |
| 1626 | (ctxt->flags == 0)) { |
| 1627 | /* |
| 1628 | * we are just trying to assert the validity of the document, |
| 1629 | * speed primes over the output, run in a single pass |
| 1630 | */ |
| 1631 | cur = root; |
| 1632 | while (cur != NULL) { |
| 1633 | rule = ctxt->schema->rules; |
| 1634 | while (rule != NULL) { |
| 1635 | if (xmlPatternMatch(rule->pattern, cur) == 1) { |
| 1636 | test = rule->tests; |
| 1637 | while (test != NULL) { |
| 1638 | xmlSchematronRunTest(ctxt, test, instance, cur); |
| 1639 | test = test->next; |
| 1640 | } |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 1641 | } |
Daniel Veillard | e70375c | 2005-07-30 21:09:12 +0000 | [diff] [blame] | 1642 | rule = rule->next; |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 1643 | } |
Daniel Veillard | e70375c | 2005-07-30 21:09:12 +0000 | [diff] [blame] | 1644 | |
| 1645 | cur = xmlSchematronNextNode(cur); |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 1646 | } |
Daniel Veillard | e70375c | 2005-07-30 21:09:12 +0000 | [diff] [blame] | 1647 | } else { |
| 1648 | /* |
| 1649 | * Process all contexts one at a time |
| 1650 | */ |
| 1651 | pattern = ctxt->schema->patterns; |
| 1652 | |
| 1653 | while (pattern != NULL) { |
Daniel Veillard | c740a17 | 2005-07-31 12:17:24 +0000 | [diff] [blame] | 1654 | xmlSchematronReportPattern(ctxt, pattern); |
| 1655 | |
Daniel Veillard | e70375c | 2005-07-30 21:09:12 +0000 | [diff] [blame] | 1656 | /* |
| 1657 | * TODO convert the pattern rule to a direct XPath and |
| 1658 | * compute directly instead of using the pattern matching |
| 1659 | * over the full document... |
| 1660 | * Check the exact semantic |
| 1661 | */ |
| 1662 | cur = root; |
| 1663 | while (cur != NULL) { |
| 1664 | rule = pattern->rules; |
| 1665 | while (rule != NULL) { |
| 1666 | if (xmlPatternMatch(rule->pattern, cur) == 1) { |
| 1667 | test = rule->tests; |
| 1668 | while (test != NULL) { |
| 1669 | xmlSchematronRunTest(ctxt, test, instance, cur); |
| 1670 | test = test->next; |
| 1671 | } |
| 1672 | } |
| 1673 | rule = rule->patnext; |
| 1674 | } |
| 1675 | |
| 1676 | cur = xmlSchematronNextNode(cur); |
| 1677 | } |
Daniel Veillard | c740a17 | 2005-07-31 12:17:24 +0000 | [diff] [blame] | 1678 | pattern = pattern->next; |
Daniel Veillard | e70375c | 2005-07-30 21:09:12 +0000 | [diff] [blame] | 1679 | } |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 1680 | } |
| 1681 | return(ctxt->nberrors); |
| 1682 | } |
| 1683 | |
| 1684 | #ifdef STANDALONE |
| 1685 | int |
| 1686 | main(void) |
| 1687 | { |
| 1688 | int ret; |
| 1689 | xmlDocPtr instance; |
| 1690 | xmlSchematronParserCtxtPtr pctxt; |
| 1691 | xmlSchematronValidCtxtPtr vctxt; |
| 1692 | xmlSchematronPtr schema = NULL; |
| 1693 | |
| 1694 | pctxt = xmlSchematronNewParserCtxt("tst.sct"); |
| 1695 | if (pctxt == NULL) { |
| 1696 | fprintf(stderr, "failed to build schematron parser\n"); |
| 1697 | } else { |
| 1698 | schema = xmlSchematronParse(pctxt); |
| 1699 | if (schema == NULL) { |
| 1700 | fprintf(stderr, "failed to compile schematron\n"); |
| 1701 | } |
| 1702 | xmlSchematronFreeParserCtxt(pctxt); |
| 1703 | } |
| 1704 | instance = xmlReadFile("tst.sct", NULL, |
| 1705 | XML_PARSE_NOENT | XML_PARSE_NOCDATA); |
| 1706 | if (instance == NULL) { |
| 1707 | fprintf(stderr, "failed to parse instance\n"); |
| 1708 | } |
| 1709 | if ((schema != NULL) && (instance != NULL)) { |
| 1710 | vctxt = xmlSchematronNewValidCtxt(schema); |
| 1711 | if (vctxt == NULL) { |
| 1712 | fprintf(stderr, "failed to build schematron validator\n"); |
| 1713 | } else { |
| 1714 | ret = xmlSchematronValidateDoc(vctxt, instance); |
| 1715 | xmlSchematronFreeValidCtxt(vctxt); |
| 1716 | } |
| 1717 | } |
| 1718 | xmlSchematronFree(schema); |
| 1719 | xmlFreeDoc(instance); |
| 1720 | |
| 1721 | xmlCleanupParser(); |
| 1722 | xmlMemoryDump(); |
| 1723 | |
| 1724 | return (0); |
| 1725 | } |
| 1726 | #endif |
Daniel Veillard | ccb4d41 | 2005-08-23 13:41:17 +0000 | [diff] [blame] | 1727 | #define bottom_schematron |
| 1728 | #include "elfgcchack.h" |
Daniel Veillard | ed6c549 | 2005-07-23 15:00:22 +0000 | [diff] [blame] | 1729 | #endif /* LIBXML_SCHEMATRON_ENABLED */ |