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