Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1 | /* |
| 2 | * relaxng.c : implementation of the Relax-NG handling and validity checking |
| 3 | * |
| 4 | * See Copyright for the status of this software. |
| 5 | * |
| 6 | * Daniel Veillard <veillard@redhat.com> |
| 7 | */ |
| 8 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 9 | /** |
| 10 | * TODO: |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 11 | * - add support for DTD compatibility spec |
| 12 | * http://www.oasis-open.org/committees/relax-ng/compatibility-20011203.html |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 13 | * - report better mem allocations pbms at runtime and abort immediately. |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 16 | #define IN_LIBXML |
| 17 | #include "libxml.h" |
| 18 | |
| 19 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 20 | |
| 21 | #include <string.h> |
| 22 | #include <stdio.h> |
| 23 | #include <libxml/xmlmemory.h> |
| 24 | #include <libxml/parser.h> |
| 25 | #include <libxml/parserInternals.h> |
| 26 | #include <libxml/hash.h> |
| 27 | #include <libxml/uri.h> |
| 28 | |
| 29 | #include <libxml/relaxng.h> |
| 30 | |
| 31 | #include <libxml/xmlschemastypes.h> |
| 32 | #include <libxml/xmlautomata.h> |
| 33 | #include <libxml/xmlregexp.h> |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 34 | #include <libxml/xmlschemastypes.h> |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | * The Relax-NG namespace |
| 38 | */ |
| 39 | static const xmlChar *xmlRelaxNGNs = (const xmlChar *) |
| 40 | "http://relaxng.org/ns/structure/1.0"; |
| 41 | |
| 42 | #define IS_RELAXNG(node, type) \ |
| 43 | ((node != NULL) && (node->ns != NULL) && \ |
| 44 | (xmlStrEqual(node->name, (const xmlChar *) type)) && \ |
| 45 | (xmlStrEqual(node->ns->href, xmlRelaxNGNs))) |
| 46 | |
| 47 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 48 | /* #define DEBUG 1 */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 49 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 50 | /* #define DEBUG_GRAMMAR 1 */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 51 | |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 52 | /* #define DEBUG_CONTENT 1 */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 53 | |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 54 | /* #define DEBUG_TYPE 1 */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 55 | |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 56 | /* #define DEBUG_VALID 1 */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 57 | |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 58 | /* #define DEBUG_INTERLEAVE 1 */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 59 | |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 60 | /* #define DEBUG_LIST 1 */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 61 | |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 62 | /* #define DEBUG_INCLUDE */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 63 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 64 | /* #define DEBUG_ERROR 1 */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 65 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 66 | /* #define DEBUG_COMPILE 1 */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 67 | |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 68 | /* #define DEBUG_PROGRESSIVE 1 */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 69 | |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 70 | #define MAX_ERROR 5 |
| 71 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 72 | #define TODO \ |
| 73 | xmlGenericError(xmlGenericErrorContext, \ |
| 74 | "Unimplemented block at %s:%d\n", \ |
| 75 | __FILE__, __LINE__); |
| 76 | |
| 77 | typedef struct _xmlRelaxNGSchema xmlRelaxNGSchema; |
| 78 | typedef xmlRelaxNGSchema *xmlRelaxNGSchemaPtr; |
| 79 | |
| 80 | typedef struct _xmlRelaxNGDefine xmlRelaxNGDefine; |
| 81 | typedef xmlRelaxNGDefine *xmlRelaxNGDefinePtr; |
| 82 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 83 | typedef struct _xmlRelaxNGDocument xmlRelaxNGDocument; |
| 84 | typedef xmlRelaxNGDocument *xmlRelaxNGDocumentPtr; |
| 85 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 86 | typedef struct _xmlRelaxNGInclude xmlRelaxNGInclude; |
| 87 | typedef xmlRelaxNGInclude *xmlRelaxNGIncludePtr; |
| 88 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 89 | typedef enum { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 90 | XML_RELAXNG_COMBINE_UNDEFINED = 0, /* undefined */ |
| 91 | XML_RELAXNG_COMBINE_CHOICE, /* choice */ |
| 92 | XML_RELAXNG_COMBINE_INTERLEAVE /* interleave */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 93 | } xmlRelaxNGCombine; |
| 94 | |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 95 | typedef enum { |
| 96 | XML_RELAXNG_CONTENT_ERROR = -1, |
| 97 | XML_RELAXNG_CONTENT_EMPTY = 0, |
| 98 | XML_RELAXNG_CONTENT_SIMPLE, |
| 99 | XML_RELAXNG_CONTENT_COMPLEX |
| 100 | } xmlRelaxNGContentType; |
| 101 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 102 | typedef struct _xmlRelaxNGGrammar xmlRelaxNGGrammar; |
| 103 | typedef xmlRelaxNGGrammar *xmlRelaxNGGrammarPtr; |
| 104 | |
| 105 | struct _xmlRelaxNGGrammar { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 106 | xmlRelaxNGGrammarPtr parent; /* the parent grammar if any */ |
| 107 | xmlRelaxNGGrammarPtr children; /* the children grammar if any */ |
| 108 | xmlRelaxNGGrammarPtr next; /* the next grammar if any */ |
| 109 | xmlRelaxNGDefinePtr start; /* <start> content */ |
| 110 | xmlRelaxNGCombine combine; /* the default combine value */ |
| 111 | xmlRelaxNGDefinePtr startList; /* list of <start> definitions */ |
| 112 | xmlHashTablePtr defs; /* define* */ |
| 113 | xmlHashTablePtr refs; /* references */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 117 | typedef enum { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 118 | XML_RELAXNG_NOOP = -1, /* a no operation from simplification */ |
| 119 | XML_RELAXNG_EMPTY = 0, /* an empty pattern */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 120 | XML_RELAXNG_NOT_ALLOWED, /* not allowed top */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 121 | XML_RELAXNG_EXCEPT, /* except present in nameclass defs */ |
| 122 | XML_RELAXNG_TEXT, /* textual content */ |
| 123 | XML_RELAXNG_ELEMENT, /* an element */ |
| 124 | XML_RELAXNG_DATATYPE, /* extenal data type definition */ |
| 125 | XML_RELAXNG_PARAM, /* extenal data type parameter */ |
| 126 | XML_RELAXNG_VALUE, /* value from an extenal data type definition */ |
| 127 | XML_RELAXNG_LIST, /* a list of patterns */ |
| 128 | XML_RELAXNG_ATTRIBUTE, /* an attrbute following a pattern */ |
| 129 | XML_RELAXNG_DEF, /* a definition */ |
| 130 | XML_RELAXNG_REF, /* reference to a definition */ |
| 131 | XML_RELAXNG_EXTERNALREF, /* reference to an external def */ |
| 132 | XML_RELAXNG_PARENTREF, /* reference to a def in the parent grammar */ |
| 133 | XML_RELAXNG_OPTIONAL, /* optional patterns */ |
| 134 | XML_RELAXNG_ZEROORMORE, /* zero or more non empty patterns */ |
| 135 | XML_RELAXNG_ONEORMORE, /* one or more non empty patterns */ |
| 136 | XML_RELAXNG_CHOICE, /* a choice between non empty patterns */ |
| 137 | XML_RELAXNG_GROUP, /* a pair/group of non empty patterns */ |
| 138 | XML_RELAXNG_INTERLEAVE, /* interleaving choice of non-empty patterns */ |
| 139 | XML_RELAXNG_START /* Used to keep track of starts on grammars */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 140 | } xmlRelaxNGType; |
| 141 | |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 142 | #define IS_NULLABLE (1 << 0) |
| 143 | #define IS_NOT_NULLABLE (1 << 1) |
| 144 | #define IS_INDETERMINIST (1 << 2) |
| 145 | #define IS_MIXED (1 << 3) |
| 146 | #define IS_TRIABLE (1 << 4) |
| 147 | #define IS_PROCESSED (1 << 5) |
| 148 | #define IS_COMPILABLE (1 << 6) |
| 149 | #define IS_NOT_COMPILABLE (1 << 7) |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 150 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 151 | struct _xmlRelaxNGDefine { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 152 | xmlRelaxNGType type; /* the type of definition */ |
| 153 | xmlNodePtr node; /* the node in the source */ |
| 154 | xmlChar *name; /* the element local name if present */ |
| 155 | xmlChar *ns; /* the namespace local name if present */ |
| 156 | xmlChar *value; /* value when available */ |
| 157 | void *data; /* data lib or specific pointer */ |
| 158 | xmlRelaxNGDefinePtr content; /* the expected content */ |
| 159 | xmlRelaxNGDefinePtr parent; /* the parent definition, if any */ |
| 160 | xmlRelaxNGDefinePtr next; /* list within grouping sequences */ |
| 161 | xmlRelaxNGDefinePtr attrs; /* list of attributes for elements */ |
| 162 | xmlRelaxNGDefinePtr nameClass; /* the nameClass definition if any */ |
| 163 | xmlRelaxNGDefinePtr nextHash; /* next define in defs/refs hash tables */ |
| 164 | short depth; /* used for the cycle detection */ |
| 165 | short dflags; /* define related flags */ |
| 166 | xmlRegexpPtr contModel; /* a compiled content model if available */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | /** |
| 170 | * _xmlRelaxNG: |
| 171 | * |
| 172 | * A RelaxNGs definition |
| 173 | */ |
| 174 | struct _xmlRelaxNG { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 175 | void *_private; /* unused by the library for users or bindings */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 176 | xmlRelaxNGGrammarPtr topgrammar; |
| 177 | xmlDocPtr doc; |
| 178 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 179 | int idref; /* requires idref checking */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 180 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 181 | xmlHashTablePtr defs; /* define */ |
| 182 | xmlHashTablePtr refs; /* references */ |
| 183 | xmlRelaxNGDocumentPtr documents; /* all the documents loaded */ |
| 184 | xmlRelaxNGIncludePtr includes; /* all the includes loaded */ |
| 185 | int defNr; /* number of defines used */ |
| 186 | xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 187 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 188 | }; |
| 189 | |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 190 | #define XML_RELAXNG_IN_ATTRIBUTE (1 << 0) |
| 191 | #define XML_RELAXNG_IN_ONEORMORE (1 << 1) |
| 192 | #define XML_RELAXNG_IN_LIST (1 << 2) |
| 193 | #define XML_RELAXNG_IN_DATAEXCEPT (1 << 3) |
| 194 | #define XML_RELAXNG_IN_START (1 << 4) |
| 195 | #define XML_RELAXNG_IN_OOMGROUP (1 << 5) |
| 196 | #define XML_RELAXNG_IN_OOMINTERLEAVE (1 << 6) |
| 197 | #define XML_RELAXNG_IN_EXTERNALREF (1 << 7) |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 198 | #define XML_RELAXNG_IN_ANYEXCEPT (1 << 8) |
| 199 | #define XML_RELAXNG_IN_NSEXCEPT (1 << 9) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 200 | |
| 201 | struct _xmlRelaxNGParserCtxt { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 202 | void *userData; /* user specific data block */ |
| 203 | xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */ |
| 204 | xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */ |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 205 | xmlStructuredErrorFunc serror; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 206 | xmlRelaxNGValidErr err; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 207 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 208 | xmlRelaxNGPtr schema; /* The schema in use */ |
| 209 | xmlRelaxNGGrammarPtr grammar; /* the current grammar */ |
| 210 | xmlRelaxNGGrammarPtr parentgrammar; /* the parent grammar */ |
| 211 | int flags; /* parser flags */ |
| 212 | int nbErrors; /* number of errors at parse time */ |
| 213 | int nbWarnings; /* number of warnings at parse time */ |
| 214 | const xmlChar *define; /* the current define scope */ |
| 215 | xmlRelaxNGDefinePtr def; /* the current define */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 216 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 217 | int nbInterleaves; |
| 218 | xmlHashTablePtr interleaves; /* keep track of all the interleaves */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 219 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 220 | xmlRelaxNGDocumentPtr documents; /* all the documents loaded */ |
| 221 | xmlRelaxNGIncludePtr includes; /* all the includes loaded */ |
| 222 | xmlChar *URL; |
| 223 | xmlDocPtr document; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 224 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 225 | int defNr; /* number of defines used */ |
| 226 | int defMax; /* number of defines aloocated */ |
| 227 | xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */ |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 228 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 229 | const char *buffer; |
| 230 | int size; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 231 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 232 | /* the document stack */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 233 | xmlRelaxNGDocumentPtr doc; /* Current parsed external ref */ |
| 234 | int docNr; /* Depth of the parsing stack */ |
| 235 | int docMax; /* Max depth of the parsing stack */ |
| 236 | xmlRelaxNGDocumentPtr *docTab; /* array of docs */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 237 | |
| 238 | /* the include stack */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 239 | xmlRelaxNGIncludePtr inc; /* Current parsed include */ |
| 240 | int incNr; /* Depth of the include parsing stack */ |
| 241 | int incMax; /* Max depth of the parsing stack */ |
| 242 | xmlRelaxNGIncludePtr *incTab; /* array of incs */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 243 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 244 | int idref; /* requires idref checking */ |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 245 | |
| 246 | /* used to compile content models */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 247 | xmlAutomataPtr am; /* the automata */ |
| 248 | xmlAutomataStatePtr state; /* used to build the automata */ |
Daniel Veillard | 03c2f0a | 2004-01-25 19:54:59 +0000 | [diff] [blame] | 249 | |
| 250 | int crng; /* compact syntax and other flags */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 251 | }; |
| 252 | |
| 253 | #define FLAGS_IGNORABLE 1 |
| 254 | #define FLAGS_NEGATIVE 2 |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 255 | #define FLAGS_MIXED_CONTENT 4 |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 256 | |
| 257 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 258 | * xmlRelaxNGInterleaveGroup: |
| 259 | * |
| 260 | * A RelaxNGs partition set associated to lists of definitions |
| 261 | */ |
| 262 | typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup; |
| 263 | typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr; |
| 264 | struct _xmlRelaxNGInterleaveGroup { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 265 | xmlRelaxNGDefinePtr rule; /* the rule to satisfy */ |
| 266 | xmlRelaxNGDefinePtr *defs; /* the array of element definitions */ |
| 267 | xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 268 | }; |
| 269 | |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 270 | #define IS_DETERMINIST 1 |
| 271 | #define IS_NEEDCHECK 2 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 272 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 273 | /** |
| 274 | * xmlRelaxNGPartitions: |
| 275 | * |
| 276 | * A RelaxNGs partition associated to an interleave group |
| 277 | */ |
| 278 | typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition; |
| 279 | typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr; |
| 280 | struct _xmlRelaxNGPartition { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 281 | int nbgroups; /* number of groups in the partitions */ |
| 282 | xmlHashTablePtr triage; /* hash table used to direct nodes to the |
| 283 | * right group when possible */ |
| 284 | int flags; /* determinist ? */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 285 | xmlRelaxNGInterleaveGroupPtr *groups; |
| 286 | }; |
| 287 | |
| 288 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 289 | * xmlRelaxNGValidState: |
| 290 | * |
| 291 | * A RelaxNGs validation state |
| 292 | */ |
| 293 | #define MAX_ATTR 20 |
| 294 | typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState; |
| 295 | typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr; |
| 296 | struct _xmlRelaxNGValidState { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 297 | xmlNodePtr node; /* the current node */ |
| 298 | xmlNodePtr seq; /* the sequence of children left to validate */ |
| 299 | int nbAttrs; /* the number of attributes */ |
| 300 | int maxAttrs; /* the size of attrs */ |
| 301 | int nbAttrLeft; /* the number of attributes left to validate */ |
| 302 | xmlChar *value; /* the value when operating on string */ |
| 303 | xmlChar *endvalue; /* the end value when operating on string */ |
| 304 | xmlAttrPtr *attrs; /* the array of attributes */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 305 | }; |
| 306 | |
| 307 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 308 | * xmlRelaxNGStates: |
| 309 | * |
| 310 | * A RelaxNGs container for validation state |
| 311 | */ |
| 312 | typedef struct _xmlRelaxNGStates xmlRelaxNGStates; |
| 313 | typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr; |
| 314 | struct _xmlRelaxNGStates { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 315 | int nbState; /* the number of states */ |
| 316 | int maxState; /* the size of the array */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 317 | xmlRelaxNGValidStatePtr *tabState; |
| 318 | }; |
| 319 | |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 320 | #define ERROR_IS_DUP 1 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 321 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 322 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 323 | * xmlRelaxNGValidError: |
| 324 | * |
| 325 | * A RelaxNGs validation error |
| 326 | */ |
| 327 | typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError; |
| 328 | typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr; |
| 329 | struct _xmlRelaxNGValidError { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 330 | xmlRelaxNGValidErr err; /* the error number */ |
| 331 | int flags; /* flags */ |
| 332 | xmlNodePtr node; /* the current node */ |
| 333 | xmlNodePtr seq; /* the current child */ |
| 334 | const xmlChar *arg1; /* first arg */ |
| 335 | const xmlChar *arg2; /* second arg */ |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 336 | }; |
| 337 | |
| 338 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 339 | * xmlRelaxNGValidCtxt: |
| 340 | * |
| 341 | * A RelaxNGs validation context |
| 342 | */ |
| 343 | |
| 344 | struct _xmlRelaxNGValidCtxt { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 345 | void *userData; /* user specific data block */ |
| 346 | xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */ |
| 347 | xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */ |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 348 | xmlStructuredErrorFunc serror; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 349 | int nbErrors; /* number of errors in validation */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 350 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 351 | xmlRelaxNGPtr schema; /* The schema in use */ |
| 352 | xmlDocPtr doc; /* the document being validated */ |
| 353 | int flags; /* validation flags */ |
| 354 | int depth; /* validation depth */ |
| 355 | int idref; /* requires idref checking */ |
| 356 | int errNo; /* the first error found */ |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 357 | |
| 358 | /* |
| 359 | * Errors accumulated in branches may have to be stacked to be |
| 360 | * provided back when it's sure they affect validation. |
| 361 | */ |
| 362 | xmlRelaxNGValidErrorPtr err; /* Last error */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 363 | int errNr; /* Depth of the error stack */ |
| 364 | int errMax; /* Max depth of the error stack */ |
| 365 | xmlRelaxNGValidErrorPtr errTab; /* stack of errors */ |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 366 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 367 | xmlRelaxNGValidStatePtr state; /* the current validation state */ |
| 368 | xmlRelaxNGStatesPtr states; /* the accumulated state list */ |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 369 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 370 | xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */ |
| 371 | int freeStatesNr; |
| 372 | int freeStatesMax; |
| 373 | xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */ |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 374 | |
| 375 | /* |
| 376 | * This is used for "progressive" validation |
| 377 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 378 | xmlRegExecCtxtPtr elem; /* the current element regexp */ |
| 379 | int elemNr; /* the number of element validated */ |
| 380 | int elemMax; /* the max depth of elements */ |
| 381 | xmlRegExecCtxtPtr *elemTab; /* the stack of regexp runtime */ |
| 382 | int pstate; /* progressive state */ |
| 383 | xmlNodePtr pnode; /* the current node */ |
| 384 | xmlRelaxNGDefinePtr pdef; /* the non-streamable definition */ |
| 385 | int perr; /* signal error in content model |
| 386 | * outside the regexp */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 387 | }; |
| 388 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 389 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 390 | * xmlRelaxNGInclude: |
| 391 | * |
| 392 | * Structure associated to a RelaxNGs document element |
| 393 | */ |
| 394 | struct _xmlRelaxNGInclude { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 395 | xmlRelaxNGIncludePtr next; /* keep a chain of includes */ |
| 396 | xmlChar *href; /* the normalized href value */ |
| 397 | xmlDocPtr doc; /* the associated XML document */ |
| 398 | xmlRelaxNGDefinePtr content; /* the definitions */ |
| 399 | xmlRelaxNGPtr schema; /* the schema */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 400 | }; |
| 401 | |
| 402 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 403 | * xmlRelaxNGDocument: |
| 404 | * |
| 405 | * Structure associated to a RelaxNGs document element |
| 406 | */ |
| 407 | struct _xmlRelaxNGDocument { |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 408 | xmlRelaxNGDocumentPtr next; /* keep a chain of documents */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 409 | xmlChar *href; /* the normalized href value */ |
| 410 | xmlDocPtr doc; /* the associated XML document */ |
| 411 | xmlRelaxNGDefinePtr content; /* the definitions */ |
| 412 | xmlRelaxNGPtr schema; /* the schema */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 413 | }; |
| 414 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 415 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 416 | /************************************************************************ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 417 | * * |
| 418 | * Some factorized error routines * |
| 419 | * * |
| 420 | ************************************************************************/ |
| 421 | |
| 422 | /** |
| 423 | * xmlRngPErrMemory: |
| 424 | * @ctxt: an Relax-NG parser context |
| 425 | * @extra: extra informations |
| 426 | * |
| 427 | * Handle a redefinition of attribute error |
| 428 | */ |
| 429 | static void |
| 430 | xmlRngPErrMemory(xmlRelaxNGParserCtxtPtr ctxt, const char *extra) |
| 431 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 432 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 433 | xmlGenericErrorFunc channel = NULL; |
| 434 | void *data = NULL; |
| 435 | |
| 436 | if (ctxt != NULL) { |
| 437 | channel = ctxt->error; |
| 438 | data = ctxt->userData; |
| 439 | ctxt->nbErrors++; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 440 | schannel = ctxt->serror; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 441 | } |
| 442 | if (extra) |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 443 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 444 | NULL, NULL, XML_FROM_RELAXNGP, |
| 445 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra, |
| 446 | NULL, NULL, 0, 0, |
| 447 | "Memory allocation failed : %s\n", extra); |
| 448 | else |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 449 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 450 | NULL, NULL, XML_FROM_RELAXNGP, |
| 451 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL, |
| 452 | NULL, NULL, 0, 0, "Memory allocation failed\n"); |
| 453 | } |
| 454 | |
| 455 | /** |
| 456 | * xmlRngVErrMemory: |
| 457 | * @ctxt: a Relax-NG validation context |
| 458 | * @extra: extra informations |
| 459 | * |
| 460 | * Handle a redefinition of attribute error |
| 461 | */ |
| 462 | static void |
| 463 | xmlRngVErrMemory(xmlRelaxNGValidCtxtPtr ctxt, const char *extra) |
| 464 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 465 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 466 | xmlGenericErrorFunc channel = NULL; |
| 467 | void *data = NULL; |
| 468 | |
| 469 | if (ctxt != NULL) { |
| 470 | channel = ctxt->error; |
| 471 | data = ctxt->userData; |
| 472 | ctxt->nbErrors++; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 473 | schannel = ctxt->serror; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 474 | } |
| 475 | if (extra) |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 476 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 477 | NULL, NULL, XML_FROM_RELAXNGV, |
| 478 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra, |
| 479 | NULL, NULL, 0, 0, |
| 480 | "Memory allocation failed : %s\n", extra); |
| 481 | else |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 482 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 483 | NULL, NULL, XML_FROM_RELAXNGV, |
| 484 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL, |
| 485 | NULL, NULL, 0, 0, "Memory allocation failed\n"); |
| 486 | } |
| 487 | |
| 488 | /** |
| 489 | * xmlRngPErr: |
| 490 | * @ctxt: a Relax-NG parser context |
| 491 | * @node: the node raising the error |
| 492 | * @error: the error code |
| 493 | * @msg: message |
| 494 | * @str1: extra info |
| 495 | * @str2: extra info |
| 496 | * |
| 497 | * Handle a Relax NG Parsing error |
| 498 | */ |
| 499 | static void |
| 500 | xmlRngPErr(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, int error, |
| 501 | const char *msg, const xmlChar * str1, const xmlChar * str2) |
| 502 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 503 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 504 | xmlGenericErrorFunc channel = NULL; |
| 505 | void *data = NULL; |
| 506 | |
| 507 | if (ctxt != NULL) { |
| 508 | channel = ctxt->error; |
| 509 | data = ctxt->userData; |
| 510 | ctxt->nbErrors++; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 511 | schannel = ctxt->serror; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 512 | } |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 513 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 514 | NULL, node, XML_FROM_RELAXNGP, |
| 515 | error, XML_ERR_ERROR, NULL, 0, |
| 516 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
| 517 | msg, str1, str2); |
| 518 | } |
| 519 | |
| 520 | /** |
| 521 | * xmlRngVErr: |
| 522 | * @ctxt: a Relax-NG validation context |
| 523 | * @node: the node raising the error |
| 524 | * @error: the error code |
| 525 | * @msg: message |
| 526 | * @str1: extra info |
| 527 | * @str2: extra info |
| 528 | * |
| 529 | * Handle a Relax NG Validation error |
| 530 | */ |
| 531 | static void |
| 532 | xmlRngVErr(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node, int error, |
| 533 | const char *msg, const xmlChar * str1, const xmlChar * str2) |
| 534 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 535 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 536 | xmlGenericErrorFunc channel = NULL; |
| 537 | void *data = NULL; |
| 538 | |
| 539 | if (ctxt != NULL) { |
| 540 | channel = ctxt->error; |
| 541 | data = ctxt->userData; |
| 542 | ctxt->nbErrors++; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 543 | schannel = ctxt->serror; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 544 | } |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 545 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 546 | NULL, node, XML_FROM_RELAXNGV, |
| 547 | error, XML_ERR_ERROR, NULL, 0, |
| 548 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
| 549 | msg, str1, str2); |
| 550 | } |
| 551 | |
| 552 | /************************************************************************ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 553 | * * |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 554 | * Preliminary type checking interfaces * |
| 555 | * * |
| 556 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 557 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 558 | /** |
| 559 | * xmlRelaxNGTypeHave: |
| 560 | * @data: data needed for the library |
| 561 | * @type: the type name |
| 562 | * @value: the value to check |
| 563 | * |
| 564 | * Function provided by a type library to check if a type is exported |
| 565 | * |
| 566 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 567 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 568 | typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar * type); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 569 | |
| 570 | /** |
| 571 | * xmlRelaxNGTypeCheck: |
| 572 | * @data: data needed for the library |
| 573 | * @type: the type name |
| 574 | * @value: the value to check |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 575 | * @result: place to store the result if needed |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 576 | * |
| 577 | * Function provided by a type library to check if a value match a type |
| 578 | * |
| 579 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 580 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 581 | typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar * type, |
| 582 | const xmlChar * value, void **result, |
| 583 | xmlNodePtr node); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 584 | |
| 585 | /** |
| 586 | * xmlRelaxNGFacetCheck: |
| 587 | * @data: data needed for the library |
| 588 | * @type: the type name |
| 589 | * @facet: the facet name |
| 590 | * @val: the facet value |
| 591 | * @strval: the string value |
| 592 | * @value: the value to check |
| 593 | * |
| 594 | * Function provided by a type library to check a value facet |
| 595 | * |
| 596 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 597 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 598 | typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar * type, |
| 599 | const xmlChar * facet, |
| 600 | const xmlChar * val, |
| 601 | const xmlChar * strval, void *value); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 602 | |
| 603 | /** |
| 604 | * xmlRelaxNGTypeFree: |
| 605 | * @data: data needed for the library |
| 606 | * @result: the value to free |
| 607 | * |
| 608 | * Function provided by a type library to free a returned result |
| 609 | */ |
| 610 | typedef void (*xmlRelaxNGTypeFree) (void *data, void *result); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 611 | |
| 612 | /** |
| 613 | * xmlRelaxNGTypeCompare: |
| 614 | * @data: data needed for the library |
| 615 | * @type: the type name |
| 616 | * @value1: the first value |
| 617 | * @value2: the second value |
| 618 | * |
| 619 | * Function provided by a type library to compare two values accordingly |
| 620 | * to a type. |
| 621 | * |
| 622 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 623 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 624 | typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar * type, |
| 625 | const xmlChar * value1, |
| 626 | xmlNodePtr ctxt1, |
| 627 | void *comp1, |
| 628 | const xmlChar * value2, |
| 629 | xmlNodePtr ctxt2); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 630 | typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary; |
| 631 | typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr; |
| 632 | struct _xmlRelaxNGTypeLibrary { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 633 | const xmlChar *namespace; /* the datatypeLibrary value */ |
| 634 | void *data; /* data needed for the library */ |
| 635 | xmlRelaxNGTypeHave have; /* the export function */ |
| 636 | xmlRelaxNGTypeCheck check; /* the checking function */ |
| 637 | xmlRelaxNGTypeCompare comp; /* the compare function */ |
| 638 | xmlRelaxNGFacetCheck facet; /* the facet check function */ |
| 639 | xmlRelaxNGTypeFree freef; /* the freeing function */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 640 | }; |
| 641 | |
| 642 | /************************************************************************ |
| 643 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 644 | * Allocation functions * |
| 645 | * * |
| 646 | ************************************************************************/ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 647 | static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar); |
| 648 | static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 649 | static void xmlRelaxNGNormExtSpace(xmlChar * value); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 650 | static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 651 | static int xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt |
| 652 | ATTRIBUTE_UNUSED, |
| 653 | xmlRelaxNGValidStatePtr state1, |
| 654 | xmlRelaxNGValidStatePtr state2); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 655 | static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 656 | xmlRelaxNGValidStatePtr state); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 657 | |
| 658 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 659 | * xmlRelaxNGFreeDocument: |
| 660 | * @docu: a document structure |
| 661 | * |
| 662 | * Deallocate a RelaxNG document structure. |
| 663 | */ |
| 664 | static void |
| 665 | xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu) |
| 666 | { |
| 667 | if (docu == NULL) |
| 668 | return; |
| 669 | |
| 670 | if (docu->href != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 671 | xmlFree(docu->href); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 672 | if (docu->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 673 | xmlFreeDoc(docu->doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 674 | if (docu->schema != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 675 | xmlRelaxNGFreeInnerSchema(docu->schema); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 676 | xmlFree(docu); |
| 677 | } |
| 678 | |
| 679 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 680 | * xmlRelaxNGFreeDocumentList: |
| 681 | * @docu: a list of document structure |
| 682 | * |
| 683 | * Deallocate a RelaxNG document structures. |
| 684 | */ |
| 685 | static void |
| 686 | xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu) |
| 687 | { |
| 688 | xmlRelaxNGDocumentPtr next; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 689 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 690 | while (docu != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 691 | next = docu->next; |
| 692 | xmlRelaxNGFreeDocument(docu); |
| 693 | docu = next; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 694 | } |
| 695 | } |
| 696 | |
| 697 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 698 | * xmlRelaxNGFreeInclude: |
| 699 | * @incl: a include structure |
| 700 | * |
| 701 | * Deallocate a RelaxNG include structure. |
| 702 | */ |
| 703 | static void |
| 704 | xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl) |
| 705 | { |
| 706 | if (incl == NULL) |
| 707 | return; |
| 708 | |
| 709 | if (incl->href != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 710 | xmlFree(incl->href); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 711 | if (incl->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 712 | xmlFreeDoc(incl->doc); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 713 | if (incl->schema != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 714 | xmlRelaxNGFree(incl->schema); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 715 | xmlFree(incl); |
| 716 | } |
| 717 | |
| 718 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 719 | * xmlRelaxNGFreeIncludeList: |
| 720 | * @incl: a include structure list |
| 721 | * |
| 722 | * Deallocate a RelaxNG include structure. |
| 723 | */ |
| 724 | static void |
| 725 | xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl) |
| 726 | { |
| 727 | xmlRelaxNGIncludePtr next; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 728 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 729 | while (incl != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 730 | next = incl->next; |
| 731 | xmlRelaxNGFreeInclude(incl); |
| 732 | incl = next; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 733 | } |
| 734 | } |
| 735 | |
| 736 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 737 | * xmlRelaxNGNewRelaxNG: |
| 738 | * @ctxt: a Relax-NG validation context (optional) |
| 739 | * |
| 740 | * Allocate a new RelaxNG structure. |
| 741 | * |
| 742 | * Returns the newly allocated structure or NULL in case or error |
| 743 | */ |
| 744 | static xmlRelaxNGPtr |
| 745 | xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt) |
| 746 | { |
| 747 | xmlRelaxNGPtr ret; |
| 748 | |
| 749 | ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG)); |
| 750 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 751 | xmlRngPErrMemory(ctxt, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 752 | return (NULL); |
| 753 | } |
| 754 | memset(ret, 0, sizeof(xmlRelaxNG)); |
| 755 | |
| 756 | return (ret); |
| 757 | } |
| 758 | |
| 759 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 760 | * xmlRelaxNGFreeInnerSchema: |
| 761 | * @schema: a schema structure |
| 762 | * |
| 763 | * Deallocate a RelaxNG schema structure. |
| 764 | */ |
| 765 | static void |
| 766 | xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema) |
| 767 | { |
| 768 | if (schema == NULL) |
| 769 | return; |
| 770 | |
| 771 | if (schema->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 772 | xmlFreeDoc(schema->doc); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 773 | if (schema->defTab != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 774 | int i; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 775 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 776 | for (i = 0; i < schema->defNr; i++) |
| 777 | xmlRelaxNGFreeDefine(schema->defTab[i]); |
| 778 | xmlFree(schema->defTab); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | xmlFree(schema); |
| 782 | } |
| 783 | |
| 784 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 785 | * xmlRelaxNGFree: |
| 786 | * @schema: a schema structure |
| 787 | * |
| 788 | * Deallocate a RelaxNG structure. |
| 789 | */ |
| 790 | void |
| 791 | xmlRelaxNGFree(xmlRelaxNGPtr schema) |
| 792 | { |
| 793 | if (schema == NULL) |
| 794 | return; |
| 795 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 796 | if (schema->topgrammar != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 797 | xmlRelaxNGFreeGrammar(schema->topgrammar); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 798 | if (schema->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 799 | xmlFreeDoc(schema->doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 800 | if (schema->documents != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 801 | xmlRelaxNGFreeDocumentList(schema->documents); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 802 | if (schema->includes != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 803 | xmlRelaxNGFreeIncludeList(schema->includes); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 804 | if (schema->defTab != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 805 | int i; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 806 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 807 | for (i = 0; i < schema->defNr; i++) |
| 808 | xmlRelaxNGFreeDefine(schema->defTab[i]); |
| 809 | xmlFree(schema->defTab); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 810 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 811 | |
| 812 | xmlFree(schema); |
| 813 | } |
| 814 | |
| 815 | /** |
| 816 | * xmlRelaxNGNewGrammar: |
| 817 | * @ctxt: a Relax-NG validation context (optional) |
| 818 | * |
| 819 | * Allocate a new RelaxNG grammar. |
| 820 | * |
| 821 | * Returns the newly allocated structure or NULL in case or error |
| 822 | */ |
| 823 | static xmlRelaxNGGrammarPtr |
| 824 | xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt) |
| 825 | { |
| 826 | xmlRelaxNGGrammarPtr ret; |
| 827 | |
| 828 | ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar)); |
| 829 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 830 | xmlRngPErrMemory(ctxt, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 831 | return (NULL); |
| 832 | } |
| 833 | memset(ret, 0, sizeof(xmlRelaxNGGrammar)); |
| 834 | |
| 835 | return (ret); |
| 836 | } |
| 837 | |
| 838 | /** |
| 839 | * xmlRelaxNGFreeGrammar: |
| 840 | * @grammar: a grammar structure |
| 841 | * |
| 842 | * Deallocate a RelaxNG grammar structure. |
| 843 | */ |
| 844 | static void |
| 845 | xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar) |
| 846 | { |
| 847 | if (grammar == NULL) |
| 848 | return; |
| 849 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 850 | if (grammar->children != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 851 | xmlRelaxNGFreeGrammar(grammar->children); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 852 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 853 | if (grammar->next != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 854 | xmlRelaxNGFreeGrammar(grammar->next); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 855 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 856 | if (grammar->refs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 857 | xmlHashFree(grammar->refs, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 858 | } |
| 859 | if (grammar->defs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 860 | xmlHashFree(grammar->defs, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | xmlFree(grammar); |
| 864 | } |
| 865 | |
| 866 | /** |
| 867 | * xmlRelaxNGNewDefine: |
| 868 | * @ctxt: a Relax-NG validation context |
| 869 | * @node: the node in the input document. |
| 870 | * |
| 871 | * Allocate a new RelaxNG define. |
| 872 | * |
| 873 | * Returns the newly allocated structure or NULL in case or error |
| 874 | */ |
| 875 | static xmlRelaxNGDefinePtr |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 876 | xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 877 | { |
| 878 | xmlRelaxNGDefinePtr ret; |
| 879 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 880 | if (ctxt->defMax == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 881 | ctxt->defMax = 16; |
| 882 | ctxt->defNr = 0; |
| 883 | ctxt->defTab = (xmlRelaxNGDefinePtr *) |
| 884 | xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr)); |
| 885 | if (ctxt->defTab == NULL) { |
| 886 | xmlRngPErrMemory(ctxt, "allocating define\n"); |
| 887 | return (NULL); |
| 888 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 889 | } else if (ctxt->defMax <= ctxt->defNr) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 890 | xmlRelaxNGDefinePtr *tmp; |
| 891 | |
| 892 | ctxt->defMax *= 2; |
| 893 | tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab, |
| 894 | ctxt->defMax * |
| 895 | sizeof |
| 896 | (xmlRelaxNGDefinePtr)); |
| 897 | if (tmp == NULL) { |
| 898 | xmlRngPErrMemory(ctxt, "allocating define\n"); |
| 899 | return (NULL); |
| 900 | } |
| 901 | ctxt->defTab = tmp; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 902 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 903 | ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine)); |
| 904 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 905 | xmlRngPErrMemory(ctxt, "allocating define\n"); |
| 906 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 907 | } |
| 908 | memset(ret, 0, sizeof(xmlRelaxNGDefine)); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 909 | ctxt->defTab[ctxt->defNr++] = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 910 | ret->node = node; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 911 | ret->depth = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 912 | return (ret); |
| 913 | } |
| 914 | |
| 915 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 916 | * xmlRelaxNGFreePartition: |
| 917 | * @partitions: a partition set structure |
| 918 | * |
| 919 | * Deallocate RelaxNG partition set structures. |
| 920 | */ |
| 921 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 922 | xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions) |
| 923 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 924 | xmlRelaxNGInterleaveGroupPtr group; |
| 925 | int j; |
| 926 | |
| 927 | if (partitions != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 928 | if (partitions->groups != NULL) { |
| 929 | for (j = 0; j < partitions->nbgroups; j++) { |
| 930 | group = partitions->groups[j]; |
| 931 | if (group != NULL) { |
| 932 | if (group->defs != NULL) |
| 933 | xmlFree(group->defs); |
| 934 | if (group->attrs != NULL) |
| 935 | xmlFree(group->attrs); |
| 936 | xmlFree(group); |
| 937 | } |
| 938 | } |
| 939 | xmlFree(partitions->groups); |
| 940 | } |
| 941 | if (partitions->triage != NULL) { |
| 942 | xmlHashFree(partitions->triage, NULL); |
| 943 | } |
| 944 | xmlFree(partitions); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 945 | } |
| 946 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 947 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 948 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 949 | * xmlRelaxNGFreeDefine: |
| 950 | * @define: a define structure |
| 951 | * |
| 952 | * Deallocate a RelaxNG define structure. |
| 953 | */ |
| 954 | static void |
| 955 | xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define) |
| 956 | { |
| 957 | if (define == NULL) |
| 958 | return; |
| 959 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 960 | if ((define->type == XML_RELAXNG_VALUE) && (define->attrs != NULL)) { |
| 961 | xmlRelaxNGTypeLibraryPtr lib; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 962 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 963 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
| 964 | if ((lib != NULL) && (lib->freef != NULL)) |
| 965 | lib->freef(lib->data, (void *) define->attrs); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 966 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 967 | if ((define->data != NULL) && (define->type == XML_RELAXNG_INTERLEAVE)) |
| 968 | xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data); |
| 969 | if ((define->data != NULL) && (define->type == XML_RELAXNG_CHOICE)) |
| 970 | xmlHashFree((xmlHashTablePtr) define->data, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 971 | if (define->name != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 972 | xmlFree(define->name); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 973 | if (define->ns != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 974 | xmlFree(define->ns); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 975 | if (define->value != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 976 | xmlFree(define->value); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 977 | if (define->contModel != NULL) |
| 978 | xmlRegFreeRegexp(define->contModel); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 979 | xmlFree(define); |
| 980 | } |
| 981 | |
| 982 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 983 | * xmlRelaxNGNewStates: |
| 984 | * @ctxt: a Relax-NG validation context |
| 985 | * @size: the default size for the container |
| 986 | * |
| 987 | * Allocate a new RelaxNG validation state container |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 988 | * |
| 989 | * Returns the newly allocated structure or NULL in case or error |
| 990 | */ |
| 991 | static xmlRelaxNGStatesPtr |
| 992 | xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size) |
| 993 | { |
| 994 | xmlRelaxNGStatesPtr ret; |
| 995 | |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 996 | if ((ctxt != NULL) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 997 | (ctxt->freeState != NULL) && (ctxt->freeStatesNr > 0)) { |
| 998 | ctxt->freeStatesNr--; |
| 999 | ret = ctxt->freeStates[ctxt->freeStatesNr]; |
| 1000 | ret->nbState = 0; |
| 1001 | return (ret); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1002 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1003 | if (size < 16) |
| 1004 | size = 16; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1005 | |
| 1006 | ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) + |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1007 | (size - |
| 1008 | 1) * |
| 1009 | sizeof(xmlRelaxNGValidStatePtr)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1010 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1011 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1012 | return (NULL); |
| 1013 | } |
| 1014 | ret->nbState = 0; |
| 1015 | ret->maxState = size; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1016 | ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc((size) * |
| 1017 | sizeof |
| 1018 | (xmlRelaxNGValidStatePtr)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1019 | if (ret->tabState == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1020 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1021 | xmlFree(ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1022 | return (NULL); |
| 1023 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1024 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | /** |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1028 | * xmlRelaxNGAddStateUniq: |
| 1029 | * @ctxt: a Relax-NG validation context |
| 1030 | * @states: the states container |
| 1031 | * @state: the validation state |
| 1032 | * |
| 1033 | * Add a RelaxNG validation state to the container without checking |
| 1034 | * for unicity. |
| 1035 | * |
| 1036 | * Return 1 in case of success and 0 if this is a duplicate and -1 on error |
| 1037 | */ |
| 1038 | static int |
| 1039 | xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1040 | xmlRelaxNGStatesPtr states, |
| 1041 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1042 | { |
| 1043 | if (state == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1044 | return (-1); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1045 | } |
| 1046 | if (states->nbState >= states->maxState) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1047 | xmlRelaxNGValidStatePtr *tmp; |
| 1048 | int size; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1049 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1050 | size = states->maxState * 2; |
| 1051 | tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState, |
| 1052 | (size) * |
| 1053 | sizeof |
| 1054 | (xmlRelaxNGValidStatePtr)); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1055 | if (tmp == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1056 | xmlRngVErrMemory(ctxt, "adding states\n"); |
| 1057 | return (-1); |
| 1058 | } |
| 1059 | states->tabState = tmp; |
| 1060 | states->maxState = size; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1061 | } |
| 1062 | states->tabState[states->nbState++] = state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1063 | return (1); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1067 | * xmlRelaxNGAddState: |
| 1068 | * @ctxt: a Relax-NG validation context |
| 1069 | * @states: the states container |
| 1070 | * @state: the validation state |
| 1071 | * |
| 1072 | * Add a RelaxNG validation state to the container |
| 1073 | * |
| 1074 | * Return 1 in case of success and 0 if this is a duplicate and -1 on error |
| 1075 | */ |
| 1076 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1077 | xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt, |
| 1078 | xmlRelaxNGStatesPtr states, |
| 1079 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1080 | { |
| 1081 | int i; |
| 1082 | |
| 1083 | if (state == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1084 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1085 | } |
| 1086 | if (states->nbState >= states->maxState) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1087 | xmlRelaxNGValidStatePtr *tmp; |
| 1088 | int size; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1089 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1090 | size = states->maxState * 2; |
| 1091 | tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState, |
| 1092 | (size) * |
| 1093 | sizeof |
| 1094 | (xmlRelaxNGValidStatePtr)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1095 | if (tmp == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1096 | xmlRngVErrMemory(ctxt, "adding states\n"); |
| 1097 | return (-1); |
| 1098 | } |
| 1099 | states->tabState = tmp; |
| 1100 | states->maxState = size; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1101 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1102 | for (i = 0; i < states->nbState; i++) { |
| 1103 | if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) { |
| 1104 | xmlRelaxNGFreeValidState(ctxt, state); |
| 1105 | return (0); |
| 1106 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1107 | } |
| 1108 | states->tabState[states->nbState++] = state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1109 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | /** |
| 1113 | * xmlRelaxNGFreeStates: |
| 1114 | * @ctxt: a Relax-NG validation context |
| 1115 | * @states: teh container |
| 1116 | * |
| 1117 | * Free a RelaxNG validation state container |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1118 | */ |
| 1119 | static void |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1120 | xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1121 | xmlRelaxNGStatesPtr states) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1122 | { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1123 | if (states == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1124 | return; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1125 | if ((ctxt != NULL) && (ctxt->freeStates == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1126 | ctxt->freeStatesMax = 40; |
| 1127 | ctxt->freeStatesNr = 0; |
| 1128 | ctxt->freeStates = (xmlRelaxNGStatesPtr *) |
| 1129 | xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr)); |
| 1130 | if (ctxt->freeStates == NULL) { |
| 1131 | xmlRngVErrMemory(ctxt, "storing states\n"); |
| 1132 | } |
| 1133 | } else if ((ctxt != NULL) |
| 1134 | && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) { |
| 1135 | xmlRelaxNGStatesPtr *tmp; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1136 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1137 | tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates, |
| 1138 | 2 * ctxt->freeStatesMax * |
| 1139 | sizeof |
| 1140 | (xmlRelaxNGStatesPtr)); |
| 1141 | if (tmp == NULL) { |
| 1142 | xmlRngVErrMemory(ctxt, "storing states\n"); |
| 1143 | xmlFree(states->tabState); |
| 1144 | xmlFree(states); |
| 1145 | return; |
| 1146 | } |
| 1147 | ctxt->freeStates = tmp; |
| 1148 | ctxt->freeStatesMax *= 2; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1149 | } |
| 1150 | if ((ctxt == NULL) || (ctxt->freeState == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1151 | xmlFree(states->tabState); |
| 1152 | xmlFree(states); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1153 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1154 | ctxt->freeStates[ctxt->freeStatesNr++] = states; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1155 | } |
| 1156 | } |
| 1157 | |
| 1158 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1159 | * xmlRelaxNGNewValidState: |
| 1160 | * @ctxt: a Relax-NG validation context |
| 1161 | * @node: the current node or NULL for the document |
| 1162 | * |
| 1163 | * Allocate a new RelaxNG validation state |
| 1164 | * |
| 1165 | * Returns the newly allocated structure or NULL in case or error |
| 1166 | */ |
| 1167 | static xmlRelaxNGValidStatePtr |
| 1168 | xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node) |
| 1169 | { |
| 1170 | xmlRelaxNGValidStatePtr ret; |
| 1171 | xmlAttrPtr attr; |
| 1172 | xmlAttrPtr attrs[MAX_ATTR]; |
| 1173 | int nbAttrs = 0; |
| 1174 | xmlNodePtr root = NULL; |
| 1175 | |
| 1176 | if (node == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1177 | root = xmlDocGetRootElement(ctxt->doc); |
| 1178 | if (root == NULL) |
| 1179 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1180 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1181 | attr = node->properties; |
| 1182 | while (attr != NULL) { |
| 1183 | if (nbAttrs < MAX_ATTR) |
| 1184 | attrs[nbAttrs++] = attr; |
| 1185 | else |
| 1186 | nbAttrs++; |
| 1187 | attr = attr->next; |
| 1188 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1189 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1190 | if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) { |
| 1191 | ctxt->freeState->nbState--; |
| 1192 | ret = ctxt->freeState->tabState[ctxt->freeState->nbState]; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1193 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1194 | ret = |
| 1195 | (xmlRelaxNGValidStatePtr) |
| 1196 | xmlMalloc(sizeof(xmlRelaxNGValidState)); |
| 1197 | if (ret == NULL) { |
| 1198 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1199 | return (NULL); |
| 1200 | } |
| 1201 | memset(ret, 0, sizeof(xmlRelaxNGValidState)); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1202 | } |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 1203 | ret->value = NULL; |
| 1204 | ret->endvalue = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1205 | if (node == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1206 | ret->node = (xmlNodePtr) ctxt->doc; |
| 1207 | ret->seq = root; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1208 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1209 | ret->node = node; |
| 1210 | ret->seq = node->children; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1211 | } |
| 1212 | ret->nbAttrs = 0; |
| 1213 | if (nbAttrs > 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1214 | if (ret->attrs == NULL) { |
| 1215 | if (nbAttrs < 4) |
| 1216 | ret->maxAttrs = 4; |
| 1217 | else |
| 1218 | ret->maxAttrs = nbAttrs; |
| 1219 | ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs * |
| 1220 | sizeof(xmlAttrPtr)); |
| 1221 | if (ret->attrs == NULL) { |
| 1222 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1223 | return (ret); |
| 1224 | } |
| 1225 | } else if (ret->maxAttrs < nbAttrs) { |
| 1226 | xmlAttrPtr *tmp; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1227 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1228 | tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs * |
| 1229 | sizeof(xmlAttrPtr)); |
| 1230 | if (tmp == NULL) { |
| 1231 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1232 | return (ret); |
| 1233 | } |
| 1234 | ret->attrs = tmp; |
| 1235 | ret->maxAttrs = nbAttrs; |
| 1236 | } |
| 1237 | ret->nbAttrs = nbAttrs; |
| 1238 | if (nbAttrs < MAX_ATTR) { |
| 1239 | memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs); |
| 1240 | } else { |
| 1241 | attr = node->properties; |
| 1242 | nbAttrs = 0; |
| 1243 | while (attr != NULL) { |
| 1244 | ret->attrs[nbAttrs++] = attr; |
| 1245 | attr = attr->next; |
| 1246 | } |
| 1247 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1248 | } |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 1249 | ret->nbAttrLeft = ret->nbAttrs; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1250 | return (ret); |
| 1251 | } |
| 1252 | |
| 1253 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1254 | * xmlRelaxNGCopyValidState: |
| 1255 | * @ctxt: a Relax-NG validation context |
| 1256 | * @state: a validation state |
| 1257 | * |
| 1258 | * Copy the validation state |
| 1259 | * |
| 1260 | * Returns the newly allocated structure or NULL in case or error |
| 1261 | */ |
| 1262 | static xmlRelaxNGValidStatePtr |
| 1263 | xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1264 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1265 | { |
| 1266 | xmlRelaxNGValidStatePtr ret; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1267 | unsigned int maxAttrs; |
| 1268 | xmlAttrPtr *attrs; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1269 | |
| 1270 | if (state == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1271 | return (NULL); |
| 1272 | if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) { |
| 1273 | ctxt->freeState->nbState--; |
| 1274 | ret = ctxt->freeState->tabState[ctxt->freeState->nbState]; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1275 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1276 | ret = |
| 1277 | (xmlRelaxNGValidStatePtr) |
| 1278 | xmlMalloc(sizeof(xmlRelaxNGValidState)); |
| 1279 | if (ret == NULL) { |
| 1280 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1281 | return (NULL); |
| 1282 | } |
| 1283 | memset(ret, 0, sizeof(xmlRelaxNGValidState)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1284 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1285 | attrs = ret->attrs; |
| 1286 | maxAttrs = ret->maxAttrs; |
| 1287 | memcpy(ret, state, sizeof(xmlRelaxNGValidState)); |
| 1288 | ret->attrs = attrs; |
| 1289 | ret->maxAttrs = maxAttrs; |
| 1290 | if (state->nbAttrs > 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1291 | if (ret->attrs == NULL) { |
| 1292 | ret->maxAttrs = state->maxAttrs; |
| 1293 | ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs * |
| 1294 | sizeof(xmlAttrPtr)); |
| 1295 | if (ret->attrs == NULL) { |
| 1296 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1297 | ret->nbAttrs = 0; |
| 1298 | return (ret); |
| 1299 | } |
| 1300 | } else if (ret->maxAttrs < state->nbAttrs) { |
| 1301 | xmlAttrPtr *tmp; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1302 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1303 | tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs * |
| 1304 | sizeof(xmlAttrPtr)); |
| 1305 | if (tmp == NULL) { |
| 1306 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1307 | ret->nbAttrs = 0; |
| 1308 | return (ret); |
| 1309 | } |
| 1310 | ret->maxAttrs = state->maxAttrs; |
| 1311 | ret->attrs = tmp; |
| 1312 | } |
| 1313 | memcpy(ret->attrs, state->attrs, |
| 1314 | state->nbAttrs * sizeof(xmlAttrPtr)); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1315 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1316 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | /** |
| 1320 | * xmlRelaxNGEqualValidState: |
| 1321 | * @ctxt: a Relax-NG validation context |
| 1322 | * @state1: a validation state |
| 1323 | * @state2: a validation state |
| 1324 | * |
| 1325 | * Compare the validation states for equality |
| 1326 | * |
| 1327 | * Returns 1 if equald, 0 otherwise |
| 1328 | */ |
| 1329 | static int |
| 1330 | xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1331 | xmlRelaxNGValidStatePtr state1, |
| 1332 | xmlRelaxNGValidStatePtr state2) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1333 | { |
| 1334 | int i; |
| 1335 | |
| 1336 | if ((state1 == NULL) || (state2 == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1337 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1338 | if (state1 == state2) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1339 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1340 | if (state1->node != state2->node) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1341 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1342 | if (state1->seq != state2->seq) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1343 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1344 | if (state1->nbAttrLeft != state2->nbAttrLeft) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1345 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1346 | if (state1->nbAttrs != state2->nbAttrs) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1347 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1348 | if (state1->endvalue != state2->endvalue) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1349 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1350 | if ((state1->value != state2->value) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1351 | (!xmlStrEqual(state1->value, state2->value))) |
| 1352 | return (0); |
| 1353 | for (i = 0; i < state1->nbAttrs; i++) { |
| 1354 | if (state1->attrs[i] != state2->attrs[i]) |
| 1355 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1356 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1357 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
| 1360 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1361 | * xmlRelaxNGFreeValidState: |
| 1362 | * @state: a validation state structure |
| 1363 | * |
| 1364 | * Deallocate a RelaxNG validation state structure. |
| 1365 | */ |
| 1366 | static void |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1367 | xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1368 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1369 | { |
| 1370 | if (state == NULL) |
| 1371 | return; |
| 1372 | |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1373 | if ((ctxt != NULL) && (ctxt->freeState == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1374 | ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1375 | } |
| 1376 | if ((ctxt == NULL) || (ctxt->freeState == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1377 | if (state->attrs != NULL) |
| 1378 | xmlFree(state->attrs); |
| 1379 | xmlFree(state); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1380 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1381 | xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1382 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | /************************************************************************ |
| 1386 | * * |
Daniel Veillard | 03c2f0a | 2004-01-25 19:54:59 +0000 | [diff] [blame] | 1387 | * Semi internal functions * |
| 1388 | * * |
| 1389 | ************************************************************************/ |
| 1390 | |
| 1391 | /** |
| 1392 | * xmlRelaxParserSetFlag: |
| 1393 | * @ctxt: a RelaxNG parser context |
| 1394 | * @flags: a set of flags values |
| 1395 | * |
| 1396 | * Semi private function used to pass informations to a parser context |
| 1397 | * which are a combination of xmlRelaxNGParserFlag . |
| 1398 | * |
| 1399 | * Returns 0 if success and -1 in case of error |
| 1400 | */ |
| 1401 | int |
| 1402 | xmlRelaxParserSetFlag(xmlRelaxNGParserCtxtPtr ctxt, int flags) |
| 1403 | { |
| 1404 | if (ctxt == NULL) return(-1); |
| 1405 | if (flags & XML_RELAXNGP_FREE_DOC) { |
| 1406 | ctxt->crng |= XML_RELAXNGP_FREE_DOC; |
| 1407 | flags -= XML_RELAXNGP_FREE_DOC; |
| 1408 | } |
| 1409 | if (flags & XML_RELAXNGP_CRNG) { |
| 1410 | ctxt->crng |= XML_RELAXNGP_CRNG; |
| 1411 | flags -= XML_RELAXNGP_CRNG; |
| 1412 | } |
| 1413 | if (flags != 0) return(-1); |
| 1414 | return(0); |
| 1415 | } |
| 1416 | |
| 1417 | /************************************************************************ |
| 1418 | * * |
| 1419 | * Document functions * |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1420 | * * |
| 1421 | ************************************************************************/ |
| 1422 | static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1423 | xmlDocPtr doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1424 | |
| 1425 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1426 | * xmlRelaxNGIncludePush: |
| 1427 | * @ctxt: the parser context |
| 1428 | * @value: the element doc |
| 1429 | * |
| 1430 | * Pushes a new include on top of the include stack |
| 1431 | * |
| 1432 | * Returns 0 in case of error, the index in the stack otherwise |
| 1433 | */ |
| 1434 | static int |
| 1435 | xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1436 | xmlRelaxNGIncludePtr value) |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1437 | { |
| 1438 | if (ctxt->incTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1439 | ctxt->incMax = 4; |
| 1440 | ctxt->incNr = 0; |
| 1441 | ctxt->incTab = |
| 1442 | (xmlRelaxNGIncludePtr *) xmlMalloc(ctxt->incMax * |
| 1443 | sizeof(ctxt->incTab[0])); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1444 | if (ctxt->incTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1445 | xmlRngPErrMemory(ctxt, "allocating include\n"); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1446 | return (0); |
| 1447 | } |
| 1448 | } |
| 1449 | if (ctxt->incNr >= ctxt->incMax) { |
| 1450 | ctxt->incMax *= 2; |
| 1451 | ctxt->incTab = |
| 1452 | (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1453 | ctxt->incMax * |
| 1454 | sizeof(ctxt->incTab[0])); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1455 | if (ctxt->incTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1456 | xmlRngPErrMemory(ctxt, "allocating include\n"); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1457 | return (0); |
| 1458 | } |
| 1459 | } |
| 1460 | ctxt->incTab[ctxt->incNr] = value; |
| 1461 | ctxt->inc = value; |
| 1462 | return (ctxt->incNr++); |
| 1463 | } |
| 1464 | |
| 1465 | /** |
| 1466 | * xmlRelaxNGIncludePop: |
| 1467 | * @ctxt: the parser context |
| 1468 | * |
| 1469 | * Pops the top include from the include stack |
| 1470 | * |
| 1471 | * Returns the include just removed |
| 1472 | */ |
| 1473 | static xmlRelaxNGIncludePtr |
| 1474 | xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt) |
| 1475 | { |
| 1476 | xmlRelaxNGIncludePtr ret; |
| 1477 | |
| 1478 | if (ctxt->incNr <= 0) |
| 1479 | return (0); |
| 1480 | ctxt->incNr--; |
| 1481 | if (ctxt->incNr > 0) |
| 1482 | ctxt->inc = ctxt->incTab[ctxt->incNr - 1]; |
| 1483 | else |
| 1484 | ctxt->inc = NULL; |
| 1485 | ret = ctxt->incTab[ctxt->incNr]; |
| 1486 | ctxt->incTab[ctxt->incNr] = 0; |
| 1487 | return (ret); |
| 1488 | } |
| 1489 | |
| 1490 | /** |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1491 | * xmlRelaxNGRemoveRedefine: |
| 1492 | * @ctxt: the parser context |
| 1493 | * @URL: the normalized URL |
| 1494 | * @target: the included target |
| 1495 | * @name: the define name to eliminate |
| 1496 | * |
| 1497 | * Applies the elimination algorithm of 4.7 |
| 1498 | * |
| 1499 | * Returns 0 in case of error, 1 in case of success. |
| 1500 | */ |
| 1501 | static int |
| 1502 | xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1503 | const xmlChar * URL ATTRIBUTE_UNUSED, |
| 1504 | xmlNodePtr target, const xmlChar * name) |
| 1505 | { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1506 | int found = 0; |
| 1507 | xmlNodePtr tmp, tmp2; |
| 1508 | xmlChar *name2; |
| 1509 | |
| 1510 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 1511 | if (name == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1512 | xmlGenericError(xmlGenericErrorContext, |
| 1513 | "Elimination of <include> start from %s\n", URL); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 1514 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1515 | xmlGenericError(xmlGenericErrorContext, |
| 1516 | "Elimination of <include> define %s from %s\n", |
| 1517 | name, URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1518 | #endif |
| 1519 | tmp = target; |
| 1520 | while (tmp != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1521 | tmp2 = tmp->next; |
| 1522 | if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) { |
| 1523 | found = 1; |
| 1524 | xmlUnlinkNode(tmp); |
| 1525 | xmlFreeNode(tmp); |
| 1526 | } else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) { |
| 1527 | name2 = xmlGetProp(tmp, BAD_CAST "name"); |
| 1528 | xmlRelaxNGNormExtSpace(name2); |
| 1529 | if (name2 != NULL) { |
| 1530 | if (xmlStrEqual(name, name2)) { |
| 1531 | found = 1; |
| 1532 | xmlUnlinkNode(tmp); |
| 1533 | xmlFreeNode(tmp); |
| 1534 | } |
| 1535 | xmlFree(name2); |
| 1536 | } |
| 1537 | } else if (IS_RELAXNG(tmp, "include")) { |
| 1538 | xmlChar *href = NULL; |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 1539 | xmlRelaxNGDocumentPtr inc = tmp->psvi; |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1540 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1541 | if ((inc != NULL) && (inc->doc != NULL) && |
| 1542 | (inc->doc->children != NULL)) { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1543 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1544 | if (xmlStrEqual |
| 1545 | (inc->doc->children->name, BAD_CAST "grammar")) { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1546 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1547 | href = xmlGetProp(tmp, BAD_CAST "href"); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1548 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1549 | if (xmlRelaxNGRemoveRedefine(ctxt, href, |
| 1550 | inc->doc->children-> |
| 1551 | children, name) == 1) { |
| 1552 | found = 1; |
| 1553 | } |
| 1554 | if (href != NULL) |
| 1555 | xmlFree(href); |
| 1556 | } |
| 1557 | } |
| 1558 | } |
| 1559 | tmp = tmp2; |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1560 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1561 | return (found); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
| 1564 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1565 | * xmlRelaxNGLoadInclude: |
| 1566 | * @ctxt: the parser context |
| 1567 | * @URL: the normalized URL |
| 1568 | * @node: the include node. |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1569 | * @ns: the namespace passed from the context. |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1570 | * |
| 1571 | * First lookup if the document is already loaded into the parser context, |
| 1572 | * check against recursion. If not found the resource is loaded and |
| 1573 | * the content is preprocessed before being returned back to the caller. |
| 1574 | * |
| 1575 | * Returns the xmlRelaxNGIncludePtr or NULL in case of error |
| 1576 | */ |
| 1577 | static xmlRelaxNGIncludePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1578 | xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * URL, |
| 1579 | xmlNodePtr node, const xmlChar * ns) |
| 1580 | { |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1581 | xmlRelaxNGIncludePtr ret = NULL; |
| 1582 | xmlDocPtr doc; |
| 1583 | int i; |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1584 | xmlNodePtr root, cur; |
| 1585 | |
| 1586 | #ifdef DEBUG_INCLUDE |
| 1587 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1588 | "xmlRelaxNGLoadInclude(%s)\n", URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1589 | #endif |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1590 | |
| 1591 | /* |
| 1592 | * check against recursion in the stack |
| 1593 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1594 | for (i = 0; i < ctxt->incNr; i++) { |
| 1595 | if (xmlStrEqual(ctxt->incTab[i]->href, URL)) { |
| 1596 | xmlRngPErr(ctxt, NULL, XML_RNGP_INCLUDE_RECURSE, |
| 1597 | "Detected an Include recursion for %s\n", URL, |
| 1598 | NULL); |
| 1599 | return (NULL); |
| 1600 | } |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1601 | } |
| 1602 | |
| 1603 | /* |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1604 | * load the document |
| 1605 | */ |
Daniel Veillard | 87247e8 | 2004-01-13 20:42:02 +0000 | [diff] [blame] | 1606 | doc = xmlReadFile((const char *) URL,NULL,0); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1607 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1608 | xmlRngPErr(ctxt, node, XML_RNGP_PARSE_ERROR, |
| 1609 | "xmlRelaxNG: could not load %s\n", URL, NULL); |
| 1610 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1611 | } |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1612 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1613 | xmlGenericError(xmlGenericErrorContext, "Parsed %s Okay\n", URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1614 | #endif |
| 1615 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1616 | /* |
| 1617 | * Allocate the document structures and register it first. |
| 1618 | */ |
| 1619 | ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude)); |
| 1620 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1621 | xmlRngPErrMemory(ctxt, "allocating include\n"); |
| 1622 | xmlFreeDoc(doc); |
| 1623 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1624 | } |
| 1625 | memset(ret, 0, sizeof(xmlRelaxNGInclude)); |
| 1626 | ret->doc = doc; |
| 1627 | ret->href = xmlStrdup(URL); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1628 | ret->next = ctxt->includes; |
| 1629 | ctxt->includes = ret; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1630 | |
| 1631 | /* |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1632 | * transmit the ns if needed |
| 1633 | */ |
| 1634 | if (ns != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1635 | root = xmlDocGetRootElement(doc); |
| 1636 | if (root != NULL) { |
| 1637 | if (xmlHasProp(root, BAD_CAST "ns") == NULL) { |
| 1638 | xmlSetProp(root, BAD_CAST "ns", ns); |
| 1639 | } |
| 1640 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1641 | } |
| 1642 | |
| 1643 | /* |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1644 | * push it on the stack |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1645 | */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1646 | xmlRelaxNGIncludePush(ctxt, ret); |
| 1647 | |
| 1648 | /* |
| 1649 | * Some preprocessing of the document content, this include recursing |
| 1650 | * in the include stack. |
| 1651 | */ |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1652 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1653 | xmlGenericError(xmlGenericErrorContext, "cleanup of %s\n", URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1654 | #endif |
| 1655 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1656 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 1657 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1658 | ctxt->inc = NULL; |
| 1659 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
| 1662 | /* |
| 1663 | * Pop up the include from the stack |
| 1664 | */ |
| 1665 | xmlRelaxNGIncludePop(ctxt); |
| 1666 | |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1667 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1668 | xmlGenericError(xmlGenericErrorContext, "Checking of %s\n", URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1669 | #endif |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1670 | /* |
| 1671 | * Check that the top element is a grammar |
| 1672 | */ |
| 1673 | root = xmlDocGetRootElement(doc); |
| 1674 | if (root == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1675 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, |
| 1676 | "xmlRelaxNG: included document is empty %s\n", URL, |
| 1677 | NULL); |
| 1678 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1679 | } |
| 1680 | if (!IS_RELAXNG(root, "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1681 | xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING, |
| 1682 | "xmlRelaxNG: included document %s root is not a grammar\n", |
| 1683 | URL, NULL); |
| 1684 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1685 | } |
| 1686 | |
| 1687 | /* |
| 1688 | * Elimination of redefined rules in the include. |
| 1689 | */ |
| 1690 | cur = node->children; |
| 1691 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1692 | if (IS_RELAXNG(cur, "start")) { |
| 1693 | int found = 0; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1694 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1695 | found = |
| 1696 | xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL); |
| 1697 | if (!found) { |
| 1698 | xmlRngPErr(ctxt, node, XML_RNGP_START_MISSING, |
| 1699 | "xmlRelaxNG: include %s has a start but not the included grammar\n", |
| 1700 | URL, NULL); |
| 1701 | } |
| 1702 | } else if (IS_RELAXNG(cur, "define")) { |
| 1703 | xmlChar *name; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1704 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1705 | name = xmlGetProp(cur, BAD_CAST "name"); |
| 1706 | if (name == NULL) { |
| 1707 | xmlRngPErr(ctxt, node, XML_RNGP_NAME_MISSING, |
| 1708 | "xmlRelaxNG: include %s has define without name\n", |
| 1709 | URL, NULL); |
| 1710 | } else { |
| 1711 | int found; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1712 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1713 | xmlRelaxNGNormExtSpace(name); |
| 1714 | found = xmlRelaxNGRemoveRedefine(ctxt, URL, |
| 1715 | root->children, name); |
| 1716 | if (!found) { |
| 1717 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_MISSING, |
| 1718 | "xmlRelaxNG: include %s has a define %s but not the included grammar\n", |
| 1719 | URL, name); |
| 1720 | } |
| 1721 | xmlFree(name); |
| 1722 | } |
| 1723 | } |
| 1724 | cur = cur->next; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1725 | } |
| 1726 | |
| 1727 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1728 | return (ret); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
| 1731 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1732 | * xmlRelaxNGValidErrorPush: |
| 1733 | * @ctxt: the validation context |
| 1734 | * @err: the error code |
| 1735 | * @arg1: the first string argument |
| 1736 | * @arg2: the second string argument |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1737 | * @dup: arg need to be duplicated |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1738 | * |
| 1739 | * Pushes a new error on top of the error stack |
| 1740 | * |
| 1741 | * Returns 0 in case of error, the index in the stack otherwise |
| 1742 | */ |
| 1743 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1744 | xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt, |
| 1745 | xmlRelaxNGValidErr err, const xmlChar * arg1, |
| 1746 | const xmlChar * arg2, int dup) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1747 | { |
| 1748 | xmlRelaxNGValidErrorPtr cur; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1749 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 1750 | #ifdef DEBUG_ERROR |
| 1751 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1752 | "Pushing error %d at %d on stack\n", err, ctxt->errNr); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 1753 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1754 | if (ctxt->errTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1755 | ctxt->errMax = 8; |
| 1756 | ctxt->errNr = 0; |
| 1757 | ctxt->errTab = |
| 1758 | (xmlRelaxNGValidErrorPtr) xmlMalloc(ctxt->errMax * |
| 1759 | sizeof |
| 1760 | (xmlRelaxNGValidError)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1761 | if (ctxt->errTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1762 | xmlRngVErrMemory(ctxt, "pushing error\n"); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1763 | return (0); |
| 1764 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1765 | ctxt->err = NULL; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1766 | } |
| 1767 | if (ctxt->errNr >= ctxt->errMax) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1768 | ctxt->errMax *= 2; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1769 | ctxt->errTab = |
| 1770 | (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1771 | ctxt->errMax * |
| 1772 | sizeof |
| 1773 | (xmlRelaxNGValidError)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1774 | if (ctxt->errTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1775 | xmlRngVErrMemory(ctxt, "pushing error\n"); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1776 | return (0); |
| 1777 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1778 | ctxt->err = &ctxt->errTab[ctxt->errNr - 1]; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1779 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 1780 | if ((ctxt->err != NULL) && (ctxt->state != NULL) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1781 | (ctxt->err->node == ctxt->state->node) && (ctxt->err->err == err)) |
| 1782 | return (ctxt->errNr); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1783 | cur = &ctxt->errTab[ctxt->errNr]; |
| 1784 | cur->err = err; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1785 | if (dup) { |
| 1786 | cur->arg1 = xmlStrdup(arg1); |
| 1787 | cur->arg2 = xmlStrdup(arg2); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1788 | cur->flags = ERROR_IS_DUP; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1789 | } else { |
| 1790 | cur->arg1 = arg1; |
| 1791 | cur->arg2 = arg2; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1792 | cur->flags = 0; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1793 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1794 | if (ctxt->state != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1795 | cur->node = ctxt->state->node; |
| 1796 | cur->seq = ctxt->state->seq; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1797 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1798 | cur->node = NULL; |
| 1799 | cur->seq = NULL; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1800 | } |
| 1801 | ctxt->err = cur; |
| 1802 | return (ctxt->errNr++); |
| 1803 | } |
| 1804 | |
| 1805 | /** |
| 1806 | * xmlRelaxNGValidErrorPop: |
| 1807 | * @ctxt: the validation context |
| 1808 | * |
| 1809 | * Pops the top error from the error stack |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1810 | */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1811 | static void |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1812 | xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt) |
| 1813 | { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1814 | xmlRelaxNGValidErrorPtr cur; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1815 | |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1816 | if (ctxt->errNr <= 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1817 | ctxt->err = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1818 | return; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1819 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1820 | ctxt->errNr--; |
| 1821 | if (ctxt->errNr > 0) |
| 1822 | ctxt->err = &ctxt->errTab[ctxt->errNr - 1]; |
| 1823 | else |
| 1824 | ctxt->err = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1825 | cur = &ctxt->errTab[ctxt->errNr]; |
| 1826 | if (cur->flags & ERROR_IS_DUP) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1827 | if (cur->arg1 != NULL) |
| 1828 | xmlFree((xmlChar *) cur->arg1); |
| 1829 | cur->arg1 = NULL; |
| 1830 | if (cur->arg2 != NULL) |
| 1831 | xmlFree((xmlChar *) cur->arg2); |
| 1832 | cur->arg2 = NULL; |
| 1833 | cur->flags = 0; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1834 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1835 | } |
| 1836 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1837 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1838 | * xmlRelaxNGDocumentPush: |
| 1839 | * @ctxt: the parser context |
| 1840 | * @value: the element doc |
| 1841 | * |
| 1842 | * Pushes a new doc on top of the doc stack |
| 1843 | * |
| 1844 | * Returns 0 in case of error, the index in the stack otherwise |
| 1845 | */ |
| 1846 | static int |
| 1847 | xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1848 | xmlRelaxNGDocumentPtr value) |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1849 | { |
| 1850 | if (ctxt->docTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1851 | ctxt->docMax = 4; |
| 1852 | ctxt->docNr = 0; |
| 1853 | ctxt->docTab = |
| 1854 | (xmlRelaxNGDocumentPtr *) xmlMalloc(ctxt->docMax * |
| 1855 | sizeof(ctxt->docTab[0])); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1856 | if (ctxt->docTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1857 | xmlRngPErrMemory(ctxt, "adding document\n"); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1858 | return (0); |
| 1859 | } |
| 1860 | } |
| 1861 | if (ctxt->docNr >= ctxt->docMax) { |
| 1862 | ctxt->docMax *= 2; |
| 1863 | ctxt->docTab = |
| 1864 | (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1865 | ctxt->docMax * |
| 1866 | sizeof(ctxt->docTab[0])); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1867 | if (ctxt->docTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1868 | xmlRngPErrMemory(ctxt, "adding document\n"); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1869 | return (0); |
| 1870 | } |
| 1871 | } |
| 1872 | ctxt->docTab[ctxt->docNr] = value; |
| 1873 | ctxt->doc = value; |
| 1874 | return (ctxt->docNr++); |
| 1875 | } |
| 1876 | |
| 1877 | /** |
| 1878 | * xmlRelaxNGDocumentPop: |
| 1879 | * @ctxt: the parser context |
| 1880 | * |
| 1881 | * Pops the top doc from the doc stack |
| 1882 | * |
| 1883 | * Returns the doc just removed |
| 1884 | */ |
| 1885 | static xmlRelaxNGDocumentPtr |
| 1886 | xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt) |
| 1887 | { |
| 1888 | xmlRelaxNGDocumentPtr ret; |
| 1889 | |
| 1890 | if (ctxt->docNr <= 0) |
| 1891 | return (0); |
| 1892 | ctxt->docNr--; |
| 1893 | if (ctxt->docNr > 0) |
| 1894 | ctxt->doc = ctxt->docTab[ctxt->docNr - 1]; |
| 1895 | else |
| 1896 | ctxt->doc = NULL; |
| 1897 | ret = ctxt->docTab[ctxt->docNr]; |
| 1898 | ctxt->docTab[ctxt->docNr] = 0; |
| 1899 | return (ret); |
| 1900 | } |
| 1901 | |
| 1902 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1903 | * xmlRelaxNGLoadExternalRef: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1904 | * @ctxt: the parser context |
| 1905 | * @URL: the normalized URL |
| 1906 | * @ns: the inherited ns if any |
| 1907 | * |
| 1908 | * First lookup if the document is already loaded into the parser context, |
| 1909 | * check against recursion. If not found the resource is loaded and |
| 1910 | * the content is preprocessed before being returned back to the caller. |
| 1911 | * |
| 1912 | * Returns the xmlRelaxNGDocumentPtr or NULL in case of error |
| 1913 | */ |
| 1914 | static xmlRelaxNGDocumentPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1915 | xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt, |
| 1916 | const xmlChar * URL, const xmlChar * ns) |
| 1917 | { |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1918 | xmlRelaxNGDocumentPtr ret = NULL; |
| 1919 | xmlDocPtr doc; |
| 1920 | xmlNodePtr root; |
| 1921 | int i; |
| 1922 | |
| 1923 | /* |
| 1924 | * check against recursion in the stack |
| 1925 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1926 | for (i = 0; i < ctxt->docNr; i++) { |
| 1927 | if (xmlStrEqual(ctxt->docTab[i]->href, URL)) { |
| 1928 | xmlRngPErr(ctxt, NULL, XML_RNGP_EXTERNALREF_RECURSE, |
| 1929 | "Detected an externalRef recursion for %s\n", URL, |
| 1930 | NULL); |
| 1931 | return (NULL); |
| 1932 | } |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1933 | } |
| 1934 | |
| 1935 | /* |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1936 | * load the document |
| 1937 | */ |
Daniel Veillard | 87247e8 | 2004-01-13 20:42:02 +0000 | [diff] [blame] | 1938 | doc = xmlReadFile((const char *) URL,NULL,0); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1939 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1940 | xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR, |
| 1941 | "xmlRelaxNG: could not load %s\n", URL, NULL); |
| 1942 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1943 | } |
| 1944 | |
| 1945 | /* |
| 1946 | * Allocate the document structures and register it first. |
| 1947 | */ |
| 1948 | ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument)); |
| 1949 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1950 | xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_ERR_NO_MEMORY, |
| 1951 | "xmlRelaxNG: allocate memory for doc %s\n", URL, NULL); |
| 1952 | xmlFreeDoc(doc); |
| 1953 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1954 | } |
| 1955 | memset(ret, 0, sizeof(xmlRelaxNGDocument)); |
| 1956 | ret->doc = doc; |
| 1957 | ret->href = xmlStrdup(URL); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1958 | ret->next = ctxt->documents; |
| 1959 | ctxt->documents = ret; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1960 | |
| 1961 | /* |
| 1962 | * transmit the ns if needed |
| 1963 | */ |
| 1964 | if (ns != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1965 | root = xmlDocGetRootElement(doc); |
| 1966 | if (root != NULL) { |
| 1967 | if (xmlHasProp(root, BAD_CAST "ns") == NULL) { |
| 1968 | xmlSetProp(root, BAD_CAST "ns", ns); |
| 1969 | } |
| 1970 | } |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1971 | } |
| 1972 | |
| 1973 | /* |
| 1974 | * push it on the stack and register it in the hash table |
| 1975 | */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1976 | xmlRelaxNGDocumentPush(ctxt, ret); |
| 1977 | |
| 1978 | /* |
| 1979 | * Some preprocessing of the document content |
| 1980 | */ |
| 1981 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 1982 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1983 | ctxt->doc = NULL; |
| 1984 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1985 | } |
| 1986 | |
| 1987 | xmlRelaxNGDocumentPop(ctxt); |
| 1988 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1989 | return (ret); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1990 | } |
| 1991 | |
| 1992 | /************************************************************************ |
| 1993 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1994 | * Error functions * |
| 1995 | * * |
| 1996 | ************************************************************************/ |
| 1997 | |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1998 | #define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0); |
| 1999 | #define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0); |
| 2000 | #define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0); |
| 2001 | #define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1); |
| 2002 | #define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2003 | |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 2004 | static const char * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2005 | xmlRelaxNGDefName(xmlRelaxNGDefinePtr def) |
| 2006 | { |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 2007 | if (def == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2008 | return ("none"); |
| 2009 | switch (def->type) { |
| 2010 | case XML_RELAXNG_EMPTY: |
| 2011 | return ("empty"); |
| 2012 | case XML_RELAXNG_NOT_ALLOWED: |
| 2013 | return ("notAllowed"); |
| 2014 | case XML_RELAXNG_EXCEPT: |
| 2015 | return ("except"); |
| 2016 | case XML_RELAXNG_TEXT: |
| 2017 | return ("text"); |
| 2018 | case XML_RELAXNG_ELEMENT: |
| 2019 | return ("element"); |
| 2020 | case XML_RELAXNG_DATATYPE: |
| 2021 | return ("datatype"); |
| 2022 | case XML_RELAXNG_VALUE: |
| 2023 | return ("value"); |
| 2024 | case XML_RELAXNG_LIST: |
| 2025 | return ("list"); |
| 2026 | case XML_RELAXNG_ATTRIBUTE: |
| 2027 | return ("attribute"); |
| 2028 | case XML_RELAXNG_DEF: |
| 2029 | return ("def"); |
| 2030 | case XML_RELAXNG_REF: |
| 2031 | return ("ref"); |
| 2032 | case XML_RELAXNG_EXTERNALREF: |
| 2033 | return ("externalRef"); |
| 2034 | case XML_RELAXNG_PARENTREF: |
| 2035 | return ("parentRef"); |
| 2036 | case XML_RELAXNG_OPTIONAL: |
| 2037 | return ("optional"); |
| 2038 | case XML_RELAXNG_ZEROORMORE: |
| 2039 | return ("zeroOrMore"); |
| 2040 | case XML_RELAXNG_ONEORMORE: |
| 2041 | return ("oneOrMore"); |
| 2042 | case XML_RELAXNG_CHOICE: |
| 2043 | return ("choice"); |
| 2044 | case XML_RELAXNG_GROUP: |
| 2045 | return ("group"); |
| 2046 | case XML_RELAXNG_INTERLEAVE: |
| 2047 | return ("interleave"); |
| 2048 | case XML_RELAXNG_START: |
| 2049 | return ("start"); |
| 2050 | case XML_RELAXNG_NOOP: |
| 2051 | return ("noop"); |
| 2052 | case XML_RELAXNG_PARAM: |
| 2053 | return ("param"); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 2054 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2055 | return ("unknown"); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 2056 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 2057 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2058 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2059 | * xmlRelaxNGGetErrorString: |
| 2060 | * @err: the error code |
| 2061 | * @arg1: the first string argument |
| 2062 | * @arg2: the second string argument |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2063 | * |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2064 | * computes a formatted error string for the given error code and args |
| 2065 | * |
| 2066 | * Returns the error string, it must be deallocated by the caller |
| 2067 | */ |
| 2068 | static xmlChar * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2069 | xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar * arg1, |
| 2070 | const xmlChar * arg2) |
| 2071 | { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2072 | char msg[1000]; |
| 2073 | |
| 2074 | if (arg1 == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2075 | arg1 = BAD_CAST ""; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2076 | if (arg2 == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2077 | arg2 = BAD_CAST ""; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2078 | |
| 2079 | msg[0] = 0; |
| 2080 | switch (err) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2081 | case XML_RELAXNG_OK: |
| 2082 | return (NULL); |
| 2083 | case XML_RELAXNG_ERR_MEMORY: |
| 2084 | return (xmlCharStrdup("out of memory\n")); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2085 | case XML_RELAXNG_ERR_TYPE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2086 | snprintf(msg, 1000, "failed to validate type %s\n", arg1); |
| 2087 | break; |
| 2088 | case XML_RELAXNG_ERR_TYPEVAL: |
| 2089 | snprintf(msg, 1000, "Type %s doesn't allow value '%s'\n", arg1, |
| 2090 | arg2); |
| 2091 | break; |
| 2092 | case XML_RELAXNG_ERR_DUPID: |
| 2093 | snprintf(msg, 1000, "ID %s redefined\n", arg1); |
| 2094 | break; |
| 2095 | case XML_RELAXNG_ERR_TYPECMP: |
| 2096 | snprintf(msg, 1000, "failed to compare type %s\n", arg1); |
| 2097 | break; |
| 2098 | case XML_RELAXNG_ERR_NOSTATE: |
| 2099 | return (xmlCharStrdup("Internal error: no state\n")); |
| 2100 | case XML_RELAXNG_ERR_NODEFINE: |
| 2101 | return (xmlCharStrdup("Internal error: no define\n")); |
| 2102 | case XML_RELAXNG_ERR_INTERNAL: |
| 2103 | snprintf(msg, 1000, "Internal error: %s\n", arg1); |
| 2104 | break; |
| 2105 | case XML_RELAXNG_ERR_LISTEXTRA: |
| 2106 | snprintf(msg, 1000, "Extra data in list: %s\n", arg1); |
| 2107 | break; |
| 2108 | case XML_RELAXNG_ERR_INTERNODATA: |
| 2109 | return (xmlCharStrdup |
| 2110 | ("Internal: interleave block has no data\n")); |
| 2111 | case XML_RELAXNG_ERR_INTERSEQ: |
| 2112 | return (xmlCharStrdup("Invalid sequence in interleave\n")); |
| 2113 | case XML_RELAXNG_ERR_INTEREXTRA: |
| 2114 | snprintf(msg, 1000, "Extra element %s in interleave\n", arg1); |
| 2115 | break; |
| 2116 | case XML_RELAXNG_ERR_ELEMNAME: |
| 2117 | snprintf(msg, 1000, "Expecting element %s, got %s\n", arg1, |
| 2118 | arg2); |
| 2119 | break; |
| 2120 | case XML_RELAXNG_ERR_ELEMNONS: |
| 2121 | snprintf(msg, 1000, "Expecting a namespace for element %s\n", |
| 2122 | arg1); |
| 2123 | break; |
| 2124 | case XML_RELAXNG_ERR_ELEMWRONGNS: |
| 2125 | snprintf(msg, 1000, |
| 2126 | "Element %s has wrong namespace: expecting %s\n", arg1, |
| 2127 | arg2); |
| 2128 | break; |
| 2129 | case XML_RELAXNG_ERR_ELEMWRONG: |
| 2130 | snprintf(msg, 1000, "Did not expect element %s there\n", arg1); |
| 2131 | break; |
| 2132 | case XML_RELAXNG_ERR_TEXTWRONG: |
| 2133 | snprintf(msg, 1000, |
| 2134 | "Did not expect text in element %s content\n", arg1); |
| 2135 | break; |
| 2136 | case XML_RELAXNG_ERR_ELEMEXTRANS: |
| 2137 | snprintf(msg, 1000, "Expecting no namespace for element %s\n", |
| 2138 | arg1); |
| 2139 | break; |
| 2140 | case XML_RELAXNG_ERR_ELEMNOTEMPTY: |
| 2141 | snprintf(msg, 1000, "Expecting element %s to be empty\n", arg1); |
| 2142 | break; |
| 2143 | case XML_RELAXNG_ERR_NOELEM: |
| 2144 | snprintf(msg, 1000, "Expecting an element %s, got nothing\n", |
| 2145 | arg1); |
| 2146 | break; |
| 2147 | case XML_RELAXNG_ERR_NOTELEM: |
| 2148 | return (xmlCharStrdup("Expecting an element got text\n")); |
| 2149 | case XML_RELAXNG_ERR_ATTRVALID: |
| 2150 | snprintf(msg, 1000, "Element %s failed to validate attributes\n", |
| 2151 | arg1); |
| 2152 | break; |
| 2153 | case XML_RELAXNG_ERR_CONTENTVALID: |
| 2154 | snprintf(msg, 1000, "Element %s failed to validate content\n", |
| 2155 | arg1); |
| 2156 | break; |
| 2157 | case XML_RELAXNG_ERR_EXTRACONTENT: |
| 2158 | snprintf(msg, 1000, "Element %s has extra content: %s\n", |
| 2159 | arg1, arg2); |
| 2160 | break; |
| 2161 | case XML_RELAXNG_ERR_INVALIDATTR: |
| 2162 | snprintf(msg, 1000, "Invalid attribute %s for element %s\n", |
| 2163 | arg1, arg2); |
| 2164 | break; |
| 2165 | case XML_RELAXNG_ERR_LACKDATA: |
| 2166 | snprintf(msg, 1000, "Datatype element %s contains no data\n", |
| 2167 | arg1); |
| 2168 | break; |
| 2169 | case XML_RELAXNG_ERR_DATAELEM: |
| 2170 | snprintf(msg, 1000, "Datatype element %s has child elements\n", |
| 2171 | arg1); |
| 2172 | break; |
| 2173 | case XML_RELAXNG_ERR_VALELEM: |
| 2174 | snprintf(msg, 1000, "Value element %s has child elements\n", |
| 2175 | arg1); |
| 2176 | break; |
| 2177 | case XML_RELAXNG_ERR_LISTELEM: |
| 2178 | snprintf(msg, 1000, "List element %s has child elements\n", |
| 2179 | arg1); |
| 2180 | break; |
| 2181 | case XML_RELAXNG_ERR_DATATYPE: |
| 2182 | snprintf(msg, 1000, "Error validating datatype %s\n", arg1); |
| 2183 | break; |
| 2184 | case XML_RELAXNG_ERR_VALUE: |
| 2185 | snprintf(msg, 1000, "Error validating value %s\n", arg1); |
| 2186 | break; |
| 2187 | case XML_RELAXNG_ERR_LIST: |
| 2188 | return (xmlCharStrdup("Error validating list\n")); |
| 2189 | case XML_RELAXNG_ERR_NOGRAMMAR: |
| 2190 | return (xmlCharStrdup("No top grammar defined\n")); |
| 2191 | case XML_RELAXNG_ERR_EXTRADATA: |
| 2192 | return (xmlCharStrdup("Extra data in the document\n")); |
| 2193 | default: |
| 2194 | return (xmlCharStrdup("Unknown error !\n")); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2195 | } |
| 2196 | if (msg[0] == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2197 | snprintf(msg, 1000, "Unknown error code %d\n", err); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2198 | } |
Daniel Veillard | adbb0e6 | 2003-05-10 20:02:45 +0000 | [diff] [blame] | 2199 | msg[1000 - 1] = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2200 | return (xmlStrdup((xmlChar *) msg)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2201 | } |
| 2202 | |
| 2203 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2204 | * xmlRelaxNGShowValidError: |
| 2205 | * @ctxt: the validation context |
| 2206 | * @err: the error number |
| 2207 | * @node: the node |
| 2208 | * @child: the node child generating the problem. |
| 2209 | * @arg1: the first argument |
| 2210 | * @arg2: the second argument |
| 2211 | * |
| 2212 | * Show a validation error. |
| 2213 | */ |
| 2214 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2215 | xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt, |
| 2216 | xmlRelaxNGValidErr err, xmlNodePtr node, |
| 2217 | xmlNodePtr child, const xmlChar * arg1, |
| 2218 | const xmlChar * arg2) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2219 | { |
| 2220 | xmlChar *msg; |
| 2221 | |
| 2222 | if (ctxt->error == NULL) |
| 2223 | return; |
| 2224 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2225 | #ifdef DEBUG_ERROR |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2226 | xmlGenericError(xmlGenericErrorContext, "Show error %d\n", err); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2227 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2228 | msg = xmlRelaxNGGetErrorString(err, arg1, arg2); |
| 2229 | if (msg == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2230 | return; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2231 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2232 | if (ctxt->errNo == XML_RELAXNG_OK) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2233 | ctxt->errNo = err; |
| 2234 | xmlRngVErr(ctxt, (child == NULL ? node : child), err, |
| 2235 | (const char *) msg, arg1, arg2); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2236 | xmlFree(msg); |
| 2237 | } |
| 2238 | |
| 2239 | /** |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2240 | * xmlRelaxNGPopErrors: |
| 2241 | * @ctxt: the validation context |
| 2242 | * @level: the error level in the stack |
| 2243 | * |
| 2244 | * pop and discard all errors until the given level is reached |
| 2245 | */ |
| 2246 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2247 | xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level) |
| 2248 | { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2249 | int i; |
| 2250 | xmlRelaxNGValidErrorPtr err; |
| 2251 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2252 | #ifdef DEBUG_ERROR |
| 2253 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2254 | "Pop errors till level %d\n", level); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2255 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2256 | for (i = level; i < ctxt->errNr; i++) { |
| 2257 | err = &ctxt->errTab[i]; |
| 2258 | if (err->flags & ERROR_IS_DUP) { |
| 2259 | if (err->arg1 != NULL) |
| 2260 | xmlFree((xmlChar *) err->arg1); |
| 2261 | err->arg1 = NULL; |
| 2262 | if (err->arg2 != NULL) |
| 2263 | xmlFree((xmlChar *) err->arg2); |
| 2264 | err->arg2 = NULL; |
| 2265 | err->flags = 0; |
| 2266 | } |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2267 | } |
| 2268 | ctxt->errNr = level; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2269 | if (ctxt->errNr <= 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2270 | ctxt->err = NULL; |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2271 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2272 | |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2273 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2274 | * xmlRelaxNGDumpValidError: |
| 2275 | * @ctxt: the validation context |
| 2276 | * |
| 2277 | * Show all validation error over a given index. |
| 2278 | */ |
| 2279 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2280 | xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt) |
| 2281 | { |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 2282 | int i, j, k; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2283 | xmlRelaxNGValidErrorPtr err, dup; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2284 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2285 | #ifdef DEBUG_ERROR |
| 2286 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2287 | "Dumping error stack %d errors\n", ctxt->errNr); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2288 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2289 | for (i = 0, k = 0; i < ctxt->errNr; i++) { |
| 2290 | err = &ctxt->errTab[i]; |
| 2291 | if (k < MAX_ERROR) { |
| 2292 | for (j = 0; j < i; j++) { |
| 2293 | dup = &ctxt->errTab[j]; |
| 2294 | if ((err->err == dup->err) && (err->node == dup->node) && |
| 2295 | (xmlStrEqual(err->arg1, dup->arg1)) && |
| 2296 | (xmlStrEqual(err->arg2, dup->arg2))) { |
| 2297 | goto skip; |
| 2298 | } |
| 2299 | } |
| 2300 | xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq, |
| 2301 | err->arg1, err->arg2); |
| 2302 | k++; |
| 2303 | } |
| 2304 | skip: |
| 2305 | if (err->flags & ERROR_IS_DUP) { |
| 2306 | if (err->arg1 != NULL) |
| 2307 | xmlFree((xmlChar *) err->arg1); |
| 2308 | err->arg1 = NULL; |
| 2309 | if (err->arg2 != NULL) |
| 2310 | xmlFree((xmlChar *) err->arg2); |
| 2311 | err->arg2 = NULL; |
| 2312 | err->flags = 0; |
| 2313 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2314 | } |
| 2315 | ctxt->errNr = 0; |
| 2316 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2317 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2318 | /** |
| 2319 | * xmlRelaxNGAddValidError: |
| 2320 | * @ctxt: the validation context |
| 2321 | * @err: the error number |
| 2322 | * @arg1: the first argument |
| 2323 | * @arg2: the second argument |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2324 | * @dup: need to dup the args |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2325 | * |
| 2326 | * Register a validation error, either generating it if it's sure |
| 2327 | * or stacking it for later handling if unsure. |
| 2328 | */ |
| 2329 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2330 | xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt, |
| 2331 | xmlRelaxNGValidErr err, const xmlChar * arg1, |
| 2332 | const xmlChar * arg2, int dup) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2333 | { |
| 2334 | if ((ctxt == NULL) || (ctxt->error == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2335 | return; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2336 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2337 | #ifdef DEBUG_ERROR |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2338 | xmlGenericError(xmlGenericErrorContext, "Adding error %d\n", err); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2339 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2340 | /* |
| 2341 | * generate the error directly |
| 2342 | */ |
William M. Brack | 6092962 | 2004-03-27 17:54:18 +0000 | [diff] [blame] | 2343 | if (((ctxt->flags & FLAGS_IGNORABLE) == 0) || |
| 2344 | (ctxt->flags & FLAGS_NEGATIVE)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2345 | xmlNodePtr node, seq; |
| 2346 | |
| 2347 | /* |
| 2348 | * Flush first any stacked error which might be the |
| 2349 | * real cause of the problem. |
| 2350 | */ |
| 2351 | if (ctxt->errNr != 0) |
| 2352 | xmlRelaxNGDumpValidError(ctxt); |
| 2353 | if (ctxt->state != NULL) { |
| 2354 | node = ctxt->state->node; |
| 2355 | seq = ctxt->state->seq; |
| 2356 | } else { |
| 2357 | node = seq = NULL; |
| 2358 | } |
| 2359 | xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2360 | } |
| 2361 | /* |
| 2362 | * Stack the error for later processing if needed |
| 2363 | */ |
| 2364 | else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2365 | xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2366 | } |
| 2367 | } |
| 2368 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2369 | |
| 2370 | /************************************************************************ |
| 2371 | * * |
| 2372 | * Type library hooks * |
| 2373 | * * |
| 2374 | ************************************************************************/ |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2375 | static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2376 | const xmlChar * str); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2377 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2378 | /** |
| 2379 | * xmlRelaxNGSchemaTypeHave: |
| 2380 | * @data: data needed for the library |
| 2381 | * @type: the type name |
| 2382 | * |
| 2383 | * Check if the given type is provided by |
| 2384 | * the W3C XMLSchema Datatype library. |
| 2385 | * |
| 2386 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2387 | */ |
| 2388 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2389 | xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar * type) |
| 2390 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2391 | xmlSchemaTypePtr typ; |
| 2392 | |
| 2393 | if (type == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2394 | return (-1); |
| 2395 | typ = xmlSchemaGetPredefinedType(type, |
| 2396 | BAD_CAST |
| 2397 | "http://www.w3.org/2001/XMLSchema"); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2398 | if (typ == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2399 | return (0); |
| 2400 | return (1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2401 | } |
| 2402 | |
| 2403 | /** |
| 2404 | * xmlRelaxNGSchemaTypeCheck: |
| 2405 | * @data: data needed for the library |
| 2406 | * @type: the type name |
| 2407 | * @value: the value to check |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2408 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2409 | * |
| 2410 | * Check if the given type and value are validated by |
| 2411 | * the W3C XMLSchema Datatype library. |
| 2412 | * |
| 2413 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2414 | */ |
| 2415 | static int |
| 2416 | xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2417 | const xmlChar * type, |
| 2418 | const xmlChar * value, |
| 2419 | void **result, xmlNodePtr node) |
| 2420 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2421 | xmlSchemaTypePtr typ; |
| 2422 | int ret; |
| 2423 | |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2424 | if ((type == NULL) || (value == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2425 | return (-1); |
| 2426 | typ = xmlSchemaGetPredefinedType(type, |
| 2427 | BAD_CAST |
| 2428 | "http://www.w3.org/2001/XMLSchema"); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2429 | if (typ == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2430 | return (-1); |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2431 | ret = xmlSchemaValPredefTypeNode(typ, value, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2432 | (xmlSchemaValPtr *) result, node); |
| 2433 | if (ret == 2) /* special ID error code */ |
| 2434 | return (2); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2435 | if (ret == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2436 | return (1); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2437 | if (ret > 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2438 | return (0); |
| 2439 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2440 | } |
| 2441 | |
| 2442 | /** |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2443 | * xmlRelaxNGSchemaFacetCheck: |
| 2444 | * @data: data needed for the library |
| 2445 | * @type: the type name |
| 2446 | * @facet: the facet name |
| 2447 | * @val: the facet value |
| 2448 | * @strval: the string value |
| 2449 | * @value: the value to check |
| 2450 | * |
| 2451 | * Function provided by a type library to check a value facet |
| 2452 | * |
| 2453 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2454 | */ |
| 2455 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2456 | xmlRelaxNGSchemaFacetCheck(void *data ATTRIBUTE_UNUSED, |
| 2457 | const xmlChar * type, const xmlChar * facetname, |
| 2458 | const xmlChar * val, const xmlChar * strval, |
| 2459 | void *value) |
| 2460 | { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2461 | xmlSchemaFacetPtr facet; |
| 2462 | xmlSchemaTypePtr typ; |
| 2463 | int ret; |
| 2464 | |
| 2465 | if ((type == NULL) || (strval == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2466 | return (-1); |
| 2467 | typ = xmlSchemaGetPredefinedType(type, |
| 2468 | BAD_CAST |
| 2469 | "http://www.w3.org/2001/XMLSchema"); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2470 | if (typ == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2471 | return (-1); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2472 | |
| 2473 | facet = xmlSchemaNewFacet(); |
| 2474 | if (facet == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2475 | return (-1); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2476 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2477 | if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2478 | facet->type = XML_SCHEMA_FACET_MININCLUSIVE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2479 | } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2480 | facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2481 | } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2482 | facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2483 | } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2484 | facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2485 | } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2486 | facet->type = XML_SCHEMA_FACET_TOTALDIGITS; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2487 | } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2488 | facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2489 | } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2490 | facet->type = XML_SCHEMA_FACET_PATTERN; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2491 | } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2492 | facet->type = XML_SCHEMA_FACET_ENUMERATION; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2493 | } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2494 | facet->type = XML_SCHEMA_FACET_WHITESPACE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2495 | } else if (xmlStrEqual(facetname, BAD_CAST "length")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2496 | facet->type = XML_SCHEMA_FACET_LENGTH; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2497 | } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2498 | facet->type = XML_SCHEMA_FACET_MAXLENGTH; |
| 2499 | } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) { |
| 2500 | facet->type = XML_SCHEMA_FACET_MINLENGTH; |
| 2501 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2502 | xmlSchemaFreeFacet(facet); |
| 2503 | return (-1); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2504 | } |
Daniel Veillard | 6dc9196 | 2004-03-22 19:10:02 +0000 | [diff] [blame] | 2505 | facet->value = val; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2506 | ret = xmlSchemaCheckFacet(facet, typ, NULL, type); |
| 2507 | if (ret != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2508 | xmlSchemaFreeFacet(facet); |
| 2509 | return (-1); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2510 | } |
| 2511 | ret = xmlSchemaValidateFacet(typ, facet, strval, value); |
| 2512 | xmlSchemaFreeFacet(facet); |
| 2513 | if (ret != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2514 | return (-1); |
| 2515 | return (0); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2516 | } |
| 2517 | |
| 2518 | /** |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2519 | * xmlRelaxNGSchemaFreeValue: |
| 2520 | * @data: data needed for the library |
| 2521 | * @value: the value to free |
| 2522 | * |
| 2523 | * Function provided by a type library to free a Schemas value |
| 2524 | * |
| 2525 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2526 | */ |
| 2527 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2528 | xmlRelaxNGSchemaFreeValue(void *data ATTRIBUTE_UNUSED, void *value) |
| 2529 | { |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2530 | xmlSchemaFreeValue(value); |
| 2531 | } |
| 2532 | |
| 2533 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2534 | * xmlRelaxNGSchemaTypeCompare: |
| 2535 | * @data: data needed for the library |
| 2536 | * @type: the type name |
| 2537 | * @value1: the first value |
| 2538 | * @value2: the second value |
| 2539 | * |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2540 | * Compare two values for equality accordingly a type from the W3C XMLSchema |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2541 | * Datatype library. |
| 2542 | * |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2543 | * Returns 1 if equal, 0 if no and -1 in case of error. |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2544 | */ |
| 2545 | static int |
| 2546 | xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2547 | const xmlChar * type, |
| 2548 | const xmlChar * value1, |
| 2549 | xmlNodePtr ctxt1, |
| 2550 | void *comp1, |
| 2551 | const xmlChar * value2, xmlNodePtr ctxt2) |
| 2552 | { |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2553 | int ret; |
| 2554 | xmlSchemaTypePtr typ; |
| 2555 | xmlSchemaValPtr res1 = NULL, res2 = NULL; |
| 2556 | |
| 2557 | if ((type == NULL) || (value1 == NULL) || (value2 == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2558 | return (-1); |
| 2559 | typ = xmlSchemaGetPredefinedType(type, |
| 2560 | BAD_CAST |
| 2561 | "http://www.w3.org/2001/XMLSchema"); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2562 | if (typ == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2563 | return (-1); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2564 | if (comp1 == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2565 | ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1); |
| 2566 | if (ret != 0) |
| 2567 | return (-1); |
| 2568 | if (res1 == NULL) |
| 2569 | return (-1); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2570 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2571 | res1 = (xmlSchemaValPtr) comp1; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2572 | } |
| 2573 | ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2574 | if (ret != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2575 | xmlSchemaFreeValue(res1); |
| 2576 | return (-1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2577 | } |
| 2578 | if (res1 == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2579 | xmlSchemaFreeValue(res1); |
| 2580 | return (-1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2581 | } |
| 2582 | ret = xmlSchemaCompareValues(res1, res2); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2583 | if (res1 != (xmlSchemaValPtr) comp1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2584 | xmlSchemaFreeValue(res1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2585 | xmlSchemaFreeValue(res2); |
| 2586 | if (ret == -2) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2587 | return (-1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2588 | if (ret == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2589 | return (1); |
| 2590 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2591 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2592 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2593 | /** |
| 2594 | * xmlRelaxNGDefaultTypeHave: |
| 2595 | * @data: data needed for the library |
| 2596 | * @type: the type name |
| 2597 | * |
| 2598 | * Check if the given type is provided by |
| 2599 | * the default datatype library. |
| 2600 | * |
| 2601 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2602 | */ |
| 2603 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2604 | xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED, |
| 2605 | const xmlChar * type) |
| 2606 | { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2607 | if (type == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2608 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2609 | if (xmlStrEqual(type, BAD_CAST "string")) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2610 | return (1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2611 | if (xmlStrEqual(type, BAD_CAST "token")) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2612 | return (1); |
| 2613 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2614 | } |
| 2615 | |
| 2616 | /** |
| 2617 | * xmlRelaxNGDefaultTypeCheck: |
| 2618 | * @data: data needed for the library |
| 2619 | * @type: the type name |
| 2620 | * @value: the value to check |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2621 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2622 | * |
| 2623 | * Check if the given type and value are validated by |
| 2624 | * the default datatype library. |
| 2625 | * |
| 2626 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2627 | */ |
| 2628 | static int |
| 2629 | xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2630 | const xmlChar * type ATTRIBUTE_UNUSED, |
| 2631 | const xmlChar * value ATTRIBUTE_UNUSED, |
| 2632 | void **result ATTRIBUTE_UNUSED, |
| 2633 | xmlNodePtr node ATTRIBUTE_UNUSED) |
| 2634 | { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2635 | if (value == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2636 | return (-1); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2637 | if (xmlStrEqual(type, BAD_CAST "string")) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2638 | return (1); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2639 | if (xmlStrEqual(type, BAD_CAST "token")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2640 | return (1); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2641 | } |
| 2642 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2643 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2644 | } |
| 2645 | |
| 2646 | /** |
| 2647 | * xmlRelaxNGDefaultTypeCompare: |
| 2648 | * @data: data needed for the library |
| 2649 | * @type: the type name |
| 2650 | * @value1: the first value |
| 2651 | * @value2: the second value |
| 2652 | * |
| 2653 | * Compare two values accordingly a type from the default |
| 2654 | * datatype library. |
| 2655 | * |
| 2656 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2657 | */ |
| 2658 | static int |
| 2659 | xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2660 | const xmlChar * type, |
| 2661 | const xmlChar * value1, |
| 2662 | xmlNodePtr ctxt1 ATTRIBUTE_UNUSED, |
| 2663 | void *comp1 ATTRIBUTE_UNUSED, |
| 2664 | const xmlChar * value2, |
| 2665 | xmlNodePtr ctxt2 ATTRIBUTE_UNUSED) |
| 2666 | { |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2667 | int ret = -1; |
| 2668 | |
| 2669 | if (xmlStrEqual(type, BAD_CAST "string")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2670 | ret = xmlStrEqual(value1, value2); |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2671 | } else if (xmlStrEqual(type, BAD_CAST "token")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2672 | if (!xmlStrEqual(value1, value2)) { |
| 2673 | xmlChar *nval, *nvalue; |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2674 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2675 | /* |
| 2676 | * TODO: trivial optimizations are possible by |
| 2677 | * computing at compile-time |
| 2678 | */ |
| 2679 | nval = xmlRelaxNGNormalize(NULL, value1); |
| 2680 | nvalue = xmlRelaxNGNormalize(NULL, value2); |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2681 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2682 | if ((nval == NULL) || (nvalue == NULL)) |
| 2683 | ret = -1; |
| 2684 | else if (xmlStrEqual(nval, nvalue)) |
| 2685 | ret = 1; |
| 2686 | else |
| 2687 | ret = 0; |
| 2688 | if (nval != NULL) |
| 2689 | xmlFree(nval); |
| 2690 | if (nvalue != NULL) |
| 2691 | xmlFree(nvalue); |
| 2692 | } else |
| 2693 | ret = 1; |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2694 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2695 | return (ret); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2696 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2697 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2698 | static int xmlRelaxNGTypeInitialized = 0; |
| 2699 | static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL; |
| 2700 | |
| 2701 | /** |
| 2702 | * xmlRelaxNGFreeTypeLibrary: |
| 2703 | * @lib: the type library structure |
| 2704 | * @namespace: the URI bound to the library |
| 2705 | * |
| 2706 | * Free the structure associated to the type library |
| 2707 | */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2708 | static void |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2709 | xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2710 | const xmlChar * namespace ATTRIBUTE_UNUSED) |
| 2711 | { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2712 | if (lib == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2713 | return; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2714 | if (lib->namespace != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2715 | xmlFree((xmlChar *) lib->namespace); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2716 | xmlFree(lib); |
| 2717 | } |
| 2718 | |
| 2719 | /** |
| 2720 | * xmlRelaxNGRegisterTypeLibrary: |
| 2721 | * @namespace: the URI bound to the library |
| 2722 | * @data: data associated to the library |
| 2723 | * @have: the provide function |
| 2724 | * @check: the checking function |
| 2725 | * @comp: the comparison function |
| 2726 | * |
| 2727 | * Register a new type library |
| 2728 | * |
| 2729 | * Returns 0 in case of success and -1 in case of error. |
| 2730 | */ |
| 2731 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2732 | xmlRelaxNGRegisterTypeLibrary(const xmlChar * namespace, void *data, |
| 2733 | xmlRelaxNGTypeHave have, |
| 2734 | xmlRelaxNGTypeCheck check, |
| 2735 | xmlRelaxNGTypeCompare comp, |
| 2736 | xmlRelaxNGFacetCheck facet, |
| 2737 | xmlRelaxNGTypeFree freef) |
| 2738 | { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2739 | xmlRelaxNGTypeLibraryPtr lib; |
| 2740 | int ret; |
| 2741 | |
| 2742 | if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2743 | (check == NULL) || (comp == NULL)) |
| 2744 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2745 | if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2746 | xmlGenericError(xmlGenericErrorContext, |
| 2747 | "Relax-NG types library '%s' already registered\n", |
| 2748 | namespace); |
| 2749 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2750 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2751 | lib = |
| 2752 | (xmlRelaxNGTypeLibraryPtr) |
| 2753 | xmlMalloc(sizeof(xmlRelaxNGTypeLibrary)); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2754 | if (lib == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2755 | xmlRngVErrMemory(NULL, "adding types library\n"); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2756 | return (-1); |
| 2757 | } |
| 2758 | memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary)); |
| 2759 | lib->namespace = xmlStrdup(namespace); |
| 2760 | lib->data = data; |
| 2761 | lib->have = have; |
| 2762 | lib->comp = comp; |
| 2763 | lib->check = check; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2764 | lib->facet = facet; |
| 2765 | lib->freef = freef; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2766 | ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib); |
| 2767 | if (ret < 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2768 | xmlGenericError(xmlGenericErrorContext, |
| 2769 | "Relax-NG types library failed to register '%s'\n", |
| 2770 | namespace); |
| 2771 | xmlRelaxNGFreeTypeLibrary(lib, namespace); |
| 2772 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2773 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2774 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2775 | } |
| 2776 | |
| 2777 | /** |
| 2778 | * xmlRelaxNGInitTypes: |
| 2779 | * |
| 2780 | * Initilize the default type libraries. |
| 2781 | * |
| 2782 | * Returns 0 in case of success and -1 in case of error. |
| 2783 | */ |
| 2784 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2785 | xmlRelaxNGInitTypes(void) |
| 2786 | { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2787 | if (xmlRelaxNGTypeInitialized != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2788 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2789 | xmlRelaxNGRegisteredTypes = xmlHashCreate(10); |
| 2790 | if (xmlRelaxNGRegisteredTypes == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2791 | xmlGenericError(xmlGenericErrorContext, |
| 2792 | "Failed to allocate sh table for Relax-NG types\n"); |
| 2793 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2794 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2795 | xmlRelaxNGRegisterTypeLibrary(BAD_CAST |
| 2796 | "http://www.w3.org/2001/XMLSchema-datatypes", |
| 2797 | NULL, xmlRelaxNGSchemaTypeHave, |
| 2798 | xmlRelaxNGSchemaTypeCheck, |
| 2799 | xmlRelaxNGSchemaTypeCompare, |
| 2800 | xmlRelaxNGSchemaFacetCheck, |
| 2801 | xmlRelaxNGSchemaFreeValue); |
| 2802 | xmlRelaxNGRegisterTypeLibrary(xmlRelaxNGNs, NULL, |
| 2803 | xmlRelaxNGDefaultTypeHave, |
| 2804 | xmlRelaxNGDefaultTypeCheck, |
| 2805 | xmlRelaxNGDefaultTypeCompare, NULL, |
| 2806 | NULL); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2807 | xmlRelaxNGTypeInitialized = 1; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2808 | return (0); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2809 | } |
| 2810 | |
| 2811 | /** |
| 2812 | * xmlRelaxNGCleanupTypes: |
| 2813 | * |
| 2814 | * Cleanup the default Schemas type library associated to RelaxNG |
| 2815 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2816 | void |
| 2817 | xmlRelaxNGCleanupTypes(void) |
| 2818 | { |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 2819 | xmlSchemaCleanupTypes(); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2820 | if (xmlRelaxNGTypeInitialized == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2821 | return; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2822 | xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2823 | xmlRelaxNGFreeTypeLibrary); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2824 | xmlRelaxNGTypeInitialized = 0; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2825 | } |
| 2826 | |
| 2827 | /************************************************************************ |
| 2828 | * * |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2829 | * Compiling element content into regexp * |
| 2830 | * * |
| 2831 | * Sometime the element content can be compiled into a pure regexp, * |
| 2832 | * This allows a faster execution and streamability at that level * |
| 2833 | * * |
| 2834 | ************************************************************************/ |
| 2835 | |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2836 | static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, |
| 2837 | xmlRelaxNGDefinePtr def); |
| 2838 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2839 | /** |
| 2840 | * xmlRelaxNGIsCompileable: |
| 2841 | * @define: the definition to check |
| 2842 | * |
| 2843 | * Check if a definition is nullable. |
| 2844 | * |
| 2845 | * Returns 1 if yes, 0 if no and -1 in case of error |
| 2846 | */ |
| 2847 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2848 | xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def) |
| 2849 | { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2850 | int ret = -1; |
| 2851 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2852 | if (def == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2853 | return (-1); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2854 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2855 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 2856 | (def->dflags & IS_COMPILABLE)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2857 | return (1); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2858 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 2859 | (def->dflags & IS_NOT_COMPILABLE)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2860 | return (0); |
| 2861 | switch (def->type) { |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2862 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2863 | ret = xmlRelaxNGIsCompileable(def->content); |
| 2864 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2865 | case XML_RELAXNG_TEXT: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2866 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2867 | ret = 1; |
| 2868 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2869 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2870 | /* |
| 2871 | * Check if the element content is compileable |
| 2872 | */ |
| 2873 | if (((def->dflags & IS_NOT_COMPILABLE) == 0) && |
| 2874 | ((def->dflags & IS_COMPILABLE) == 0)) { |
| 2875 | xmlRelaxNGDefinePtr list; |
| 2876 | |
| 2877 | list = def->content; |
| 2878 | while (list != NULL) { |
| 2879 | ret = xmlRelaxNGIsCompileable(list); |
| 2880 | if (ret != 1) |
| 2881 | break; |
| 2882 | list = list->next; |
| 2883 | } |
William M. Brack | 6092962 | 2004-03-27 17:54:18 +0000 | [diff] [blame] | 2884 | /* |
| 2885 | * Because the routine is recursive, we must guard against |
| 2886 | * discovering both COMPILABLE and NOT_COMPILABLE |
| 2887 | */ |
| 2888 | if (ret == 0) { |
| 2889 | def->dflags &= ~IS_COMPILABLE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2890 | def->dflags |= IS_NOT_COMPILABLE; |
William M. Brack | 6092962 | 2004-03-27 17:54:18 +0000 | [diff] [blame] | 2891 | } |
| 2892 | if ((ret == 1) && !(def->dflags &= IS_NOT_COMPILABLE)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2893 | def->dflags |= IS_COMPILABLE; |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2894 | #ifdef DEBUG_COMPILE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2895 | if (ret == 1) { |
| 2896 | xmlGenericError(xmlGenericErrorContext, |
| 2897 | "element content for %s is compilable\n", |
| 2898 | def->name); |
| 2899 | } else if (ret == 0) { |
| 2900 | xmlGenericError(xmlGenericErrorContext, |
| 2901 | "element content for %s is not compilable\n", |
| 2902 | def->name); |
| 2903 | } else { |
| 2904 | xmlGenericError(xmlGenericErrorContext, |
| 2905 | "Problem in RelaxNGIsCompileable for element %s\n", |
| 2906 | def->name); |
| 2907 | } |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2908 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2909 | } |
| 2910 | /* |
| 2911 | * All elements return a compileable status unless they |
| 2912 | * are generic like anyName |
| 2913 | */ |
| 2914 | if ((def->nameClass != NULL) || (def->name == NULL)) |
| 2915 | ret = 0; |
| 2916 | else |
| 2917 | ret = 1; |
| 2918 | return (ret); |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2919 | case XML_RELAXNG_REF: |
| 2920 | case XML_RELAXNG_EXTERNALREF: |
| 2921 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2922 | if (def->depth == -20) { |
| 2923 | return (1); |
| 2924 | } else { |
| 2925 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2926 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2927 | def->depth = -20; |
| 2928 | list = def->content; |
| 2929 | while (list != NULL) { |
| 2930 | ret = xmlRelaxNGIsCompileable(list); |
| 2931 | if (ret != 1) |
| 2932 | break; |
| 2933 | list = list->next; |
| 2934 | } |
| 2935 | } |
| 2936 | break; |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2937 | case XML_RELAXNG_START: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2938 | case XML_RELAXNG_OPTIONAL: |
| 2939 | case XML_RELAXNG_ZEROORMORE: |
| 2940 | case XML_RELAXNG_ONEORMORE: |
| 2941 | case XML_RELAXNG_CHOICE: |
| 2942 | case XML_RELAXNG_GROUP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2943 | case XML_RELAXNG_DEF:{ |
| 2944 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2945 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2946 | list = def->content; |
| 2947 | while (list != NULL) { |
| 2948 | ret = xmlRelaxNGIsCompileable(list); |
| 2949 | if (ret != 1) |
| 2950 | break; |
| 2951 | list = list->next; |
| 2952 | } |
| 2953 | break; |
| 2954 | } |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2955 | case XML_RELAXNG_EXCEPT: |
| 2956 | case XML_RELAXNG_ATTRIBUTE: |
| 2957 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2958 | case XML_RELAXNG_DATATYPE: |
| 2959 | case XML_RELAXNG_LIST: |
| 2960 | case XML_RELAXNG_PARAM: |
| 2961 | case XML_RELAXNG_VALUE: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2962 | case XML_RELAXNG_NOT_ALLOWED: |
William M. Brack | 7e29c0a | 2004-04-02 09:07:22 +0000 | [diff] [blame] | 2963 | ret = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2964 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2965 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2966 | if (ret == 0) |
| 2967 | def->dflags |= IS_NOT_COMPILABLE; |
| 2968 | if (ret == 1) |
| 2969 | def->dflags |= IS_COMPILABLE; |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2970 | #ifdef DEBUG_COMPILE |
| 2971 | if (ret == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2972 | xmlGenericError(xmlGenericErrorContext, |
| 2973 | "RelaxNGIsCompileable %s : true\n", |
| 2974 | xmlRelaxNGDefName(def)); |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2975 | } else if (ret == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2976 | xmlGenericError(xmlGenericErrorContext, |
| 2977 | "RelaxNGIsCompileable %s : false\n", |
| 2978 | xmlRelaxNGDefName(def)); |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2979 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2980 | xmlGenericError(xmlGenericErrorContext, |
| 2981 | "Problem in RelaxNGIsCompileable %s\n", |
| 2982 | xmlRelaxNGDefName(def)); |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2983 | } |
| 2984 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2985 | return (ret); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2986 | } |
| 2987 | |
| 2988 | /** |
| 2989 | * xmlRelaxNGCompile: |
| 2990 | * ctxt: the RelaxNG parser context |
| 2991 | * @define: the definition tree to compile |
| 2992 | * |
| 2993 | * Compile the set of definitions, it works recursively, till the |
| 2994 | * element boundaries, where it tries to compile the content if possible |
| 2995 | * |
| 2996 | * Returns 0 if success and -1 in case of error |
| 2997 | */ |
| 2998 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2999 | xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) |
| 3000 | { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3001 | int ret = 0; |
| 3002 | xmlRelaxNGDefinePtr list; |
| 3003 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3004 | if ((ctxt == NULL) || (def == NULL)) |
| 3005 | return (-1); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3006 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3007 | switch (def->type) { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3008 | case XML_RELAXNG_START: |
| 3009 | if ((xmlRelaxNGIsCompileable(def) == 1) && (def->depth != -25)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3010 | xmlAutomataPtr oldam = ctxt->am; |
| 3011 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3012 | |
| 3013 | def->depth = -25; |
| 3014 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3015 | list = def->content; |
| 3016 | ctxt->am = xmlNewAutomata(); |
| 3017 | if (ctxt->am == NULL) |
| 3018 | return (-1); |
| 3019 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
| 3020 | while (list != NULL) { |
| 3021 | xmlRelaxNGCompile(ctxt, list); |
| 3022 | list = list->next; |
| 3023 | } |
| 3024 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
| 3025 | def->contModel = xmlAutomataCompile(ctxt->am); |
| 3026 | xmlRegexpIsDeterminist(def->contModel); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3027 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3028 | xmlFreeAutomata(ctxt->am); |
| 3029 | ctxt->state = oldstate; |
| 3030 | ctxt->am = oldam; |
| 3031 | } |
| 3032 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3033 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3034 | if ((ctxt->am != NULL) && (def->name != NULL)) { |
| 3035 | ctxt->state = xmlAutomataNewTransition2(ctxt->am, |
| 3036 | ctxt->state, NULL, |
| 3037 | def->name, def->ns, |
| 3038 | def); |
| 3039 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3040 | if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3041 | xmlAutomataPtr oldam = ctxt->am; |
| 3042 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3043 | |
| 3044 | def->depth = -25; |
| 3045 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3046 | list = def->content; |
| 3047 | ctxt->am = xmlNewAutomata(); |
| 3048 | if (ctxt->am == NULL) |
| 3049 | return (-1); |
| 3050 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
| 3051 | while (list != NULL) { |
| 3052 | xmlRelaxNGCompile(ctxt, list); |
| 3053 | list = list->next; |
| 3054 | } |
| 3055 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
| 3056 | def->contModel = xmlAutomataCompile(ctxt->am); |
| 3057 | if (!xmlRegexpIsDeterminist(def->contModel)) { |
| 3058 | /* |
| 3059 | * we can only use the automata if it is determinist |
| 3060 | */ |
| 3061 | xmlRegFreeRegexp(def->contModel); |
| 3062 | def->contModel = NULL; |
| 3063 | } |
| 3064 | xmlFreeAutomata(ctxt->am); |
| 3065 | ctxt->state = oldstate; |
| 3066 | ctxt->am = oldam; |
| 3067 | } else { |
| 3068 | xmlAutomataPtr oldam = ctxt->am; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3069 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3070 | /* |
| 3071 | * we can't build the content model for this element content |
| 3072 | * but it still might be possible to build it for some of its |
| 3073 | * children, recurse. |
| 3074 | */ |
| 3075 | ret = xmlRelaxNGTryCompile(ctxt, def); |
| 3076 | ctxt->am = oldam; |
| 3077 | } |
| 3078 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3079 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3080 | ret = xmlRelaxNGCompile(ctxt, def->content); |
| 3081 | break; |
| 3082 | case XML_RELAXNG_OPTIONAL:{ |
| 3083 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3084 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3085 | xmlRelaxNGCompile(ctxt, def->content); |
| 3086 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
| 3087 | break; |
| 3088 | } |
| 3089 | case XML_RELAXNG_ZEROORMORE:{ |
| 3090 | xmlAutomataStatePtr oldstate; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3091 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3092 | ctxt->state = |
| 3093 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 3094 | oldstate = ctxt->state; |
| 3095 | list = def->content; |
| 3096 | while (list != NULL) { |
| 3097 | xmlRelaxNGCompile(ctxt, list); |
| 3098 | list = list->next; |
| 3099 | } |
| 3100 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate); |
| 3101 | ctxt->state = |
| 3102 | xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 3103 | break; |
| 3104 | } |
| 3105 | case XML_RELAXNG_ONEORMORE:{ |
| 3106 | xmlAutomataStatePtr oldstate; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3107 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3108 | list = def->content; |
| 3109 | while (list != NULL) { |
| 3110 | xmlRelaxNGCompile(ctxt, list); |
| 3111 | list = list->next; |
| 3112 | } |
| 3113 | oldstate = ctxt->state; |
| 3114 | list = def->content; |
| 3115 | while (list != NULL) { |
| 3116 | xmlRelaxNGCompile(ctxt, list); |
| 3117 | list = list->next; |
| 3118 | } |
| 3119 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate); |
| 3120 | ctxt->state = |
| 3121 | xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 3122 | break; |
| 3123 | } |
| 3124 | case XML_RELAXNG_CHOICE:{ |
| 3125 | xmlAutomataStatePtr target = NULL; |
| 3126 | xmlAutomataStatePtr oldstate = ctxt->state; |
| 3127 | |
| 3128 | list = def->content; |
| 3129 | while (list != NULL) { |
| 3130 | ctxt->state = oldstate; |
| 3131 | ret = xmlRelaxNGCompile(ctxt, list); |
| 3132 | if (ret != 0) |
| 3133 | break; |
| 3134 | if (target == NULL) |
| 3135 | target = ctxt->state; |
| 3136 | else { |
| 3137 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, |
| 3138 | target); |
| 3139 | } |
| 3140 | list = list->next; |
| 3141 | } |
| 3142 | ctxt->state = target; |
| 3143 | |
| 3144 | break; |
| 3145 | } |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3146 | case XML_RELAXNG_REF: |
| 3147 | case XML_RELAXNG_EXTERNALREF: |
| 3148 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3149 | case XML_RELAXNG_GROUP: |
| 3150 | case XML_RELAXNG_DEF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3151 | list = def->content; |
| 3152 | while (list != NULL) { |
| 3153 | ret = xmlRelaxNGCompile(ctxt, list); |
| 3154 | if (ret != 0) |
| 3155 | break; |
| 3156 | list = list->next; |
| 3157 | } |
| 3158 | break; |
| 3159 | case XML_RELAXNG_TEXT:{ |
| 3160 | xmlAutomataStatePtr oldstate; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3161 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3162 | ctxt->state = |
| 3163 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 3164 | oldstate = ctxt->state; |
| 3165 | xmlRelaxNGCompile(ctxt, def->content); |
| 3166 | xmlAutomataNewTransition(ctxt->am, ctxt->state, |
| 3167 | ctxt->state, BAD_CAST "#text", |
| 3168 | NULL); |
| 3169 | ctxt->state = |
| 3170 | xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 3171 | break; |
| 3172 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3173 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3174 | ctxt->state = |
| 3175 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 3176 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3177 | case XML_RELAXNG_EXCEPT: |
| 3178 | case XML_RELAXNG_ATTRIBUTE: |
| 3179 | case XML_RELAXNG_INTERLEAVE: |
| 3180 | case XML_RELAXNG_NOT_ALLOWED: |
| 3181 | case XML_RELAXNG_DATATYPE: |
| 3182 | case XML_RELAXNG_LIST: |
| 3183 | case XML_RELAXNG_PARAM: |
| 3184 | case XML_RELAXNG_VALUE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3185 | /* This should not happen and generate an internal error */ |
| 3186 | fprintf(stderr, "RNG internal error trying to compile %s\n", |
| 3187 | xmlRelaxNGDefName(def)); |
| 3188 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3189 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3190 | return (ret); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3191 | } |
| 3192 | |
| 3193 | /** |
| 3194 | * xmlRelaxNGTryCompile: |
| 3195 | * ctxt: the RelaxNG parser context |
| 3196 | * @define: the definition tree to compile |
| 3197 | * |
| 3198 | * Try to compile the set of definitions, it works recursively, |
| 3199 | * possibly ignoring parts which cannot be compiled. |
| 3200 | * |
| 3201 | * Returns 0 if success and -1 in case of error |
| 3202 | */ |
| 3203 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3204 | xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) |
| 3205 | { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3206 | int ret = 0; |
| 3207 | xmlRelaxNGDefinePtr list; |
| 3208 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3209 | if ((ctxt == NULL) || (def == NULL)) |
| 3210 | return (-1); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3211 | |
| 3212 | if ((def->type == XML_RELAXNG_START) || |
| 3213 | (def->type == XML_RELAXNG_ELEMENT)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3214 | ret = xmlRelaxNGIsCompileable(def); |
| 3215 | if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) { |
| 3216 | ctxt->am = NULL; |
| 3217 | ret = xmlRelaxNGCompile(ctxt, def); |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3218 | #ifdef DEBUG_PROGRESSIVE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3219 | if (ret == 0) { |
| 3220 | if (def->type == XML_RELAXNG_START) |
| 3221 | xmlGenericError(xmlGenericErrorContext, |
| 3222 | "compiled the start\n"); |
| 3223 | else |
| 3224 | xmlGenericError(xmlGenericErrorContext, |
| 3225 | "compiled element %s\n", def->name); |
| 3226 | } else { |
| 3227 | if (def->type == XML_RELAXNG_START) |
| 3228 | xmlGenericError(xmlGenericErrorContext, |
| 3229 | "failed to compile the start\n"); |
| 3230 | else |
| 3231 | xmlGenericError(xmlGenericErrorContext, |
| 3232 | "failed to compile element %s\n", |
| 3233 | def->name); |
| 3234 | } |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3235 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3236 | return (ret); |
| 3237 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3238 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3239 | switch (def->type) { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3240 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3241 | ret = xmlRelaxNGTryCompile(ctxt, def->content); |
| 3242 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3243 | case XML_RELAXNG_TEXT: |
| 3244 | case XML_RELAXNG_DATATYPE: |
| 3245 | case XML_RELAXNG_LIST: |
| 3246 | case XML_RELAXNG_PARAM: |
| 3247 | case XML_RELAXNG_VALUE: |
| 3248 | case XML_RELAXNG_EMPTY: |
| 3249 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3250 | ret = 0; |
| 3251 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3252 | case XML_RELAXNG_OPTIONAL: |
| 3253 | case XML_RELAXNG_ZEROORMORE: |
| 3254 | case XML_RELAXNG_ONEORMORE: |
| 3255 | case XML_RELAXNG_CHOICE: |
| 3256 | case XML_RELAXNG_GROUP: |
| 3257 | case XML_RELAXNG_DEF: |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3258 | case XML_RELAXNG_START: |
| 3259 | case XML_RELAXNG_REF: |
| 3260 | case XML_RELAXNG_EXTERNALREF: |
| 3261 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3262 | list = def->content; |
| 3263 | while (list != NULL) { |
| 3264 | ret = xmlRelaxNGTryCompile(ctxt, list); |
| 3265 | if (ret != 0) |
| 3266 | break; |
| 3267 | list = list->next; |
| 3268 | } |
| 3269 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3270 | case XML_RELAXNG_EXCEPT: |
| 3271 | case XML_RELAXNG_ATTRIBUTE: |
| 3272 | case XML_RELAXNG_INTERLEAVE: |
| 3273 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3274 | ret = 0; |
| 3275 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3276 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3277 | return (ret); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 3278 | } |
| 3279 | |
| 3280 | /************************************************************************ |
| 3281 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3282 | * Parsing functions * |
| 3283 | * * |
| 3284 | ************************************************************************/ |
| 3285 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3286 | static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr |
| 3287 | ctxt, xmlNodePtr node); |
| 3288 | static xmlRelaxNGDefinePtr xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr |
| 3289 | ctxt, xmlNodePtr node); |
| 3290 | static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr |
| 3291 | ctxt, xmlNodePtr nodes, |
| 3292 | int group); |
| 3293 | static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr |
| 3294 | ctxt, xmlNodePtr node); |
| 3295 | static xmlRelaxNGPtr xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, |
| 3296 | xmlNodePtr node); |
| 3297 | static int xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt, |
| 3298 | xmlNodePtr nodes); |
| 3299 | static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr |
| 3300 | ctxt, xmlNodePtr node, |
| 3301 | xmlRelaxNGDefinePtr |
| 3302 | def); |
| 3303 | static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr |
| 3304 | ctxt, xmlNodePtr nodes); |
| 3305 | static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 3306 | xmlRelaxNGDefinePtr define, |
| 3307 | xmlNodePtr elem); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3308 | |
| 3309 | |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 3310 | #define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content)) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3311 | |
| 3312 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3313 | * xmlRelaxNGIsNullable: |
| 3314 | * @define: the definition to verify |
| 3315 | * |
| 3316 | * Check if a definition is nullable. |
| 3317 | * |
| 3318 | * Returns 1 if yes, 0 if no and -1 in case of error |
| 3319 | */ |
| 3320 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3321 | xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define) |
| 3322 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3323 | int ret; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3324 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3325 | if (define == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3326 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3327 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3328 | if (define->dflags & IS_NULLABLE) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3329 | return (1); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3330 | if (define->dflags & IS_NOT_NULLABLE) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3331 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3332 | switch (define->type) { |
| 3333 | case XML_RELAXNG_EMPTY: |
| 3334 | case XML_RELAXNG_TEXT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3335 | ret = 1; |
| 3336 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3337 | case XML_RELAXNG_NOOP: |
| 3338 | case XML_RELAXNG_DEF: |
| 3339 | case XML_RELAXNG_REF: |
| 3340 | case XML_RELAXNG_EXTERNALREF: |
| 3341 | case XML_RELAXNG_PARENTREF: |
| 3342 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3343 | ret = xmlRelaxNGIsNullable(define->content); |
| 3344 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3345 | case XML_RELAXNG_EXCEPT: |
| 3346 | case XML_RELAXNG_NOT_ALLOWED: |
| 3347 | case XML_RELAXNG_ELEMENT: |
| 3348 | case XML_RELAXNG_DATATYPE: |
| 3349 | case XML_RELAXNG_PARAM: |
| 3350 | case XML_RELAXNG_VALUE: |
| 3351 | case XML_RELAXNG_LIST: |
| 3352 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3353 | ret = 0; |
| 3354 | break; |
| 3355 | case XML_RELAXNG_CHOICE:{ |
| 3356 | xmlRelaxNGDefinePtr list = define->content; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3357 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3358 | while (list != NULL) { |
| 3359 | ret = xmlRelaxNGIsNullable(list); |
| 3360 | if (ret != 0) |
| 3361 | goto done; |
| 3362 | list = list->next; |
| 3363 | } |
| 3364 | ret = 0; |
| 3365 | break; |
| 3366 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3367 | case XML_RELAXNG_START: |
| 3368 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3369 | case XML_RELAXNG_GROUP:{ |
| 3370 | xmlRelaxNGDefinePtr list = define->content; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3371 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3372 | while (list != NULL) { |
| 3373 | ret = xmlRelaxNGIsNullable(list); |
| 3374 | if (ret != 1) |
| 3375 | goto done; |
| 3376 | list = list->next; |
| 3377 | } |
| 3378 | return (1); |
| 3379 | } |
| 3380 | default: |
| 3381 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3382 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3383 | done: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3384 | if (ret == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3385 | define->dflags |= IS_NOT_NULLABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3386 | if (ret == 1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3387 | define->dflags |= IS_NULLABLE; |
| 3388 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3389 | } |
| 3390 | |
| 3391 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3392 | * xmlRelaxNGIsBlank: |
| 3393 | * @str: a string |
| 3394 | * |
| 3395 | * Check if a string is ignorable c.f. 4.2. Whitespace |
| 3396 | * |
| 3397 | * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise |
| 3398 | */ |
| 3399 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3400 | xmlRelaxNGIsBlank(xmlChar * str) |
| 3401 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3402 | if (str == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3403 | return (1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3404 | while (*str != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3405 | if (!(IS_BLANK_CH(*str))) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3406 | return (0); |
| 3407 | str++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3408 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3409 | return (1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3410 | } |
| 3411 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3412 | /** |
| 3413 | * xmlRelaxNGGetDataTypeLibrary: |
| 3414 | * @ctxt: a Relax-NG parser context |
| 3415 | * @node: the current data or value element |
| 3416 | * |
| 3417 | * Applies algorithm from 4.3. datatypeLibrary attribute |
| 3418 | * |
| 3419 | * Returns the datatypeLibary value or NULL if not found |
| 3420 | */ |
| 3421 | static xmlChar * |
| 3422 | xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3423 | xmlNodePtr node) |
| 3424 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3425 | xmlChar *ret, *escape; |
| 3426 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3427 | if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3428 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
| 3429 | if (ret != NULL) { |
| 3430 | if (ret[0] == 0) { |
| 3431 | xmlFree(ret); |
| 3432 | return (NULL); |
| 3433 | } |
| 3434 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
| 3435 | if (escape == NULL) { |
| 3436 | return (ret); |
| 3437 | } |
| 3438 | xmlFree(ret); |
| 3439 | return (escape); |
| 3440 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3441 | } |
| 3442 | node = node->parent; |
| 3443 | while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3444 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
| 3445 | if (ret != NULL) { |
| 3446 | if (ret[0] == 0) { |
| 3447 | xmlFree(ret); |
| 3448 | return (NULL); |
| 3449 | } |
| 3450 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
| 3451 | if (escape == NULL) { |
| 3452 | return (ret); |
| 3453 | } |
| 3454 | xmlFree(ret); |
| 3455 | return (escape); |
| 3456 | } |
| 3457 | node = node->parent; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3458 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3459 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3460 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3461 | |
| 3462 | /** |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3463 | * xmlRelaxNGParseValue: |
| 3464 | * @ctxt: a Relax-NG parser context |
| 3465 | * @node: the data node. |
| 3466 | * |
| 3467 | * parse the content of a RelaxNG value node. |
| 3468 | * |
| 3469 | * Returns the definition pointer or NULL in case of error |
| 3470 | */ |
| 3471 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3472 | xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 3473 | { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3474 | xmlRelaxNGDefinePtr def = NULL; |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 3475 | xmlRelaxNGTypeLibraryPtr lib = NULL; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3476 | xmlChar *type; |
| 3477 | xmlChar *library; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3478 | int success = 0; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3479 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3480 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3481 | if (def == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3482 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3483 | def->type = XML_RELAXNG_VALUE; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3484 | |
| 3485 | type = xmlGetProp(node, BAD_CAST "type"); |
| 3486 | if (type != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3487 | xmlRelaxNGNormExtSpace(type); |
| 3488 | if (xmlValidateNCName(type, 0)) { |
| 3489 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE, |
| 3490 | "value type '%s' is not an NCName\n", type, NULL); |
| 3491 | } |
| 3492 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
| 3493 | if (library == NULL) |
| 3494 | library = |
| 3495 | xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3496 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3497 | def->name = type; |
| 3498 | def->ns = library; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3499 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3500 | lib = (xmlRelaxNGTypeLibraryPtr) |
| 3501 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
| 3502 | if (lib == NULL) { |
| 3503 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB, |
| 3504 | "Use of unregistered type library '%s'\n", library, |
| 3505 | NULL); |
| 3506 | def->data = NULL; |
| 3507 | } else { |
| 3508 | def->data = lib; |
| 3509 | if (lib->have == NULL) { |
| 3510 | xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB, |
| 3511 | "Internal error with type library '%s': no 'have'\n", |
| 3512 | library, NULL); |
| 3513 | } else { |
| 3514 | success = lib->have(lib->data, def->name); |
| 3515 | if (success != 1) { |
| 3516 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND, |
| 3517 | "Error type '%s' is not exported by type library '%s'\n", |
| 3518 | def->name, library); |
| 3519 | } |
| 3520 | } |
| 3521 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3522 | } |
| 3523 | if (node->children == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3524 | def->value = xmlStrdup(BAD_CAST ""); |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 3525 | } else if (((node->children->type != XML_TEXT_NODE) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3526 | (node->children->type != XML_CDATA_SECTION_NODE)) || |
| 3527 | (node->children->next != NULL)) { |
| 3528 | xmlRngPErr(ctxt, node, XML_RNGP_TEXT_EXPECTED, |
| 3529 | "Expecting a single text value for <value>content\n", |
| 3530 | NULL, NULL); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3531 | } else if (def != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3532 | def->value = xmlNodeGetContent(node); |
| 3533 | if (def->value == NULL) { |
| 3534 | xmlRngPErr(ctxt, node, XML_RNGP_VALUE_NO_CONTENT, |
| 3535 | "Element <value> has no content\n", NULL, NULL); |
| 3536 | } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) { |
| 3537 | void *val = NULL; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3538 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3539 | success = |
| 3540 | lib->check(lib->data, def->name, def->value, &val, node); |
| 3541 | if (success != 1) { |
| 3542 | xmlRngPErr(ctxt, node, XML_RNGP_INVALID_VALUE, |
| 3543 | "Value '%s' is not acceptable for type '%s'\n", |
| 3544 | def->value, def->name); |
| 3545 | } else { |
| 3546 | if (val != NULL) |
| 3547 | def->attrs = val; |
| 3548 | } |
| 3549 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3550 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3551 | return (def); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3552 | } |
| 3553 | |
| 3554 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3555 | * xmlRelaxNGParseData: |
| 3556 | * @ctxt: a Relax-NG parser context |
| 3557 | * @node: the data node. |
| 3558 | * |
| 3559 | * parse the content of a RelaxNG data node. |
| 3560 | * |
| 3561 | * Returns the definition pointer or NULL in case of error |
| 3562 | */ |
| 3563 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3564 | xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 3565 | { |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3566 | xmlRelaxNGDefinePtr def = NULL, except, last = NULL; |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 3567 | xmlRelaxNGDefinePtr param, lastparam = NULL; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3568 | xmlRelaxNGTypeLibraryPtr lib; |
| 3569 | xmlChar *type; |
| 3570 | xmlChar *library; |
| 3571 | xmlNodePtr content; |
| 3572 | int tmp; |
| 3573 | |
| 3574 | type = xmlGetProp(node, BAD_CAST "type"); |
| 3575 | if (type == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3576 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_MISSING, "data has no type\n", NULL, |
| 3577 | NULL); |
| 3578 | return (NULL); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3579 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3580 | xmlRelaxNGNormExtSpace(type); |
| 3581 | if (xmlValidateNCName(type, 0)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3582 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE, |
| 3583 | "data type '%s' is not an NCName\n", type, NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3584 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3585 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
| 3586 | if (library == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3587 | library = |
| 3588 | xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3589 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3590 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3591 | if (def == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3592 | xmlFree(type); |
| 3593 | return (NULL); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3594 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3595 | def->type = XML_RELAXNG_DATATYPE; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3596 | def->name = type; |
| 3597 | def->ns = library; |
| 3598 | |
| 3599 | lib = (xmlRelaxNGTypeLibraryPtr) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3600 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3601 | if (lib == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3602 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB, |
| 3603 | "Use of unregistered type library '%s'\n", library, |
| 3604 | NULL); |
| 3605 | def->data = NULL; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3606 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3607 | def->data = lib; |
| 3608 | if (lib->have == NULL) { |
| 3609 | xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB, |
| 3610 | "Internal error with type library '%s': no 'have'\n", |
| 3611 | library, NULL); |
| 3612 | } else { |
| 3613 | tmp = lib->have(lib->data, def->name); |
| 3614 | if (tmp != 1) { |
| 3615 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND, |
| 3616 | "Error type '%s' is not exported by type library '%s'\n", |
| 3617 | def->name, library); |
| 3618 | } else |
| 3619 | if ((xmlStrEqual |
| 3620 | (library, |
| 3621 | BAD_CAST |
| 3622 | "http://www.w3.org/2001/XMLSchema-datatypes")) |
| 3623 | && ((xmlStrEqual(def->name, BAD_CAST "IDREF")) |
| 3624 | || (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) { |
| 3625 | ctxt->idref = 1; |
| 3626 | } |
| 3627 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3628 | } |
| 3629 | content = node->children; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3630 | |
| 3631 | /* |
| 3632 | * Handle optional params |
| 3633 | */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3634 | while (content != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3635 | if (!xmlStrEqual(content->name, BAD_CAST "param")) |
| 3636 | break; |
| 3637 | if (xmlStrEqual(library, |
| 3638 | BAD_CAST "http://relaxng.org/ns/structure/1.0")) { |
| 3639 | xmlRngPErr(ctxt, node, XML_RNGP_PARAM_FORBIDDEN, |
| 3640 | "Type library '%s' does not allow type parameters\n", |
| 3641 | library, NULL); |
| 3642 | content = content->next; |
| 3643 | while ((content != NULL) && |
| 3644 | (xmlStrEqual(content->name, BAD_CAST "param"))) |
| 3645 | content = content->next; |
| 3646 | } else { |
| 3647 | param = xmlRelaxNGNewDefine(ctxt, node); |
| 3648 | if (param != NULL) { |
| 3649 | param->type = XML_RELAXNG_PARAM; |
| 3650 | param->name = xmlGetProp(content, BAD_CAST "name"); |
| 3651 | if (param->name == NULL) { |
| 3652 | xmlRngPErr(ctxt, node, XML_RNGP_PARAM_NAME_MISSING, |
| 3653 | "param has no name\n", NULL, NULL); |
| 3654 | } |
| 3655 | param->value = xmlNodeGetContent(content); |
| 3656 | if (lastparam == NULL) { |
| 3657 | def->attrs = lastparam = param; |
| 3658 | } else { |
| 3659 | lastparam->next = param; |
| 3660 | lastparam = param; |
| 3661 | } |
| 3662 | if (lib != NULL) { |
| 3663 | } |
| 3664 | } |
| 3665 | content = content->next; |
| 3666 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3667 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3668 | /* |
| 3669 | * Handle optional except |
| 3670 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3671 | if ((content != NULL) |
| 3672 | && (xmlStrEqual(content->name, BAD_CAST "except"))) { |
| 3673 | xmlNodePtr child; |
| 3674 | xmlRelaxNGDefinePtr tmp2, last2 = NULL; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3675 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3676 | except = xmlRelaxNGNewDefine(ctxt, node); |
| 3677 | if (except == NULL) { |
| 3678 | return (def); |
| 3679 | } |
| 3680 | except->type = XML_RELAXNG_EXCEPT; |
| 3681 | child = content->children; |
| 3682 | if (last == NULL) { |
| 3683 | def->content = except; |
| 3684 | } else { |
| 3685 | last->next = except; |
| 3686 | } |
| 3687 | if (child == NULL) { |
| 3688 | xmlRngPErr(ctxt, content, XML_RNGP_EXCEPT_NO_CONTENT, |
| 3689 | "except has no content\n", NULL, NULL); |
| 3690 | } |
| 3691 | while (child != NULL) { |
| 3692 | tmp2 = xmlRelaxNGParsePattern(ctxt, child); |
| 3693 | if (tmp2 != NULL) { |
| 3694 | if (last2 == NULL) { |
| 3695 | except->content = last2 = tmp2; |
| 3696 | } else { |
| 3697 | last2->next = tmp2; |
| 3698 | last2 = tmp2; |
| 3699 | } |
| 3700 | } |
| 3701 | child = child->next; |
| 3702 | } |
| 3703 | content = content->next; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3704 | } |
| 3705 | /* |
| 3706 | * Check there is no unhandled data |
| 3707 | */ |
| 3708 | if (content != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3709 | xmlRngPErr(ctxt, content, XML_RNGP_DATA_CONTENT, |
| 3710 | "Element data has unexpected content %s\n", |
| 3711 | content->name, NULL); |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3712 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3713 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3714 | return (def); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3715 | } |
| 3716 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3717 | static const xmlChar *invalidName = BAD_CAST "\1"; |
| 3718 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3719 | /** |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3720 | * xmlRelaxNGCompareNameClasses: |
| 3721 | * @defs1: the first element/attribute defs |
| 3722 | * @defs2: the second element/attribute defs |
| 3723 | * @name: the restriction on the name |
| 3724 | * @ns: the restriction on the namespace |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3725 | * |
| 3726 | * Compare the 2 lists of element definitions. The comparison is |
| 3727 | * that if both lists do not accept the same QNames, it returns 1 |
| 3728 | * If the 2 lists can accept the same QName the comparison returns 0 |
| 3729 | * |
| 3730 | * Returns 1 disttinct, 0 if equal |
| 3731 | */ |
| 3732 | static int |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3733 | xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3734 | xmlRelaxNGDefinePtr def2) |
| 3735 | { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3736 | int ret = 1; |
| 3737 | xmlNode node; |
| 3738 | xmlNs ns; |
| 3739 | xmlRelaxNGValidCtxt ctxt; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3740 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3741 | ctxt.flags = FLAGS_IGNORABLE; |
| 3742 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 3743 | memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt)); |
| 3744 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3745 | if ((def1->type == XML_RELAXNG_ELEMENT) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3746 | (def1->type == XML_RELAXNG_ATTRIBUTE)) { |
| 3747 | if (def2->type == XML_RELAXNG_TEXT) |
| 3748 | return (1); |
| 3749 | if (def1->name != NULL) { |
| 3750 | node.name = def1->name; |
| 3751 | } else { |
| 3752 | node.name = invalidName; |
| 3753 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3754 | if (def1->ns != NULL) { |
| 3755 | if (def1->ns[0] == 0) { |
| 3756 | node.ns = NULL; |
| 3757 | } else { |
William M. Brack | a74a6ff | 2004-04-02 14:03:22 +0000 | [diff] [blame] | 3758 | node.ns = &ns; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3759 | ns.href = def1->ns; |
| 3760 | } |
| 3761 | } else { |
William M. Brack | a74a6ff | 2004-04-02 14:03:22 +0000 | [diff] [blame] | 3762 | node.ns = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3763 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3764 | if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3765 | if (def1->nameClass != NULL) { |
| 3766 | ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2); |
| 3767 | } else { |
| 3768 | ret = 0; |
| 3769 | } |
| 3770 | } else { |
| 3771 | ret = 1; |
| 3772 | } |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3773 | } else if (def1->type == XML_RELAXNG_TEXT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3774 | if (def2->type == XML_RELAXNG_TEXT) |
| 3775 | return (0); |
| 3776 | return (1); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3777 | } else if (def1->type == XML_RELAXNG_EXCEPT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3778 | TODO ret = 0; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3779 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3780 | TODO ret = 0; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3781 | } |
| 3782 | if (ret == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3783 | return (ret); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3784 | if ((def2->type == XML_RELAXNG_ELEMENT) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3785 | (def2->type == XML_RELAXNG_ATTRIBUTE)) { |
| 3786 | if (def2->name != NULL) { |
| 3787 | node.name = def2->name; |
| 3788 | } else { |
| 3789 | node.name = invalidName; |
| 3790 | } |
| 3791 | node.ns = &ns; |
| 3792 | if (def2->ns != NULL) { |
| 3793 | if (def2->ns[0] == 0) { |
| 3794 | node.ns = NULL; |
| 3795 | } else { |
| 3796 | ns.href = def2->ns; |
| 3797 | } |
| 3798 | } else { |
| 3799 | ns.href = invalidName; |
| 3800 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3801 | if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3802 | if (def2->nameClass != NULL) { |
| 3803 | ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1); |
| 3804 | } else { |
| 3805 | ret = 0; |
| 3806 | } |
| 3807 | } else { |
| 3808 | ret = 1; |
| 3809 | } |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3810 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3811 | TODO ret = 0; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3812 | } |
| 3813 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3814 | return (ret); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3815 | } |
| 3816 | |
| 3817 | /** |
| 3818 | * xmlRelaxNGCompareElemDefLists: |
| 3819 | * @ctxt: a Relax-NG parser context |
| 3820 | * @defs1: the first list of element/attribute defs |
| 3821 | * @defs2: the second list of element/attribute defs |
| 3822 | * |
| 3823 | * Compare the 2 lists of element or attribute definitions. The comparison |
| 3824 | * is that if both lists do not accept the same QNames, it returns 1 |
| 3825 | * If the 2 lists can accept the same QName the comparison returns 0 |
| 3826 | * |
| 3827 | * Returns 1 disttinct, 0 if equal |
| 3828 | */ |
| 3829 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3830 | xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt |
| 3831 | ATTRIBUTE_UNUSED, xmlRelaxNGDefinePtr * def1, |
| 3832 | xmlRelaxNGDefinePtr * def2) |
| 3833 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3834 | xmlRelaxNGDefinePtr *basedef2 = def2; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3835 | |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3836 | if ((def1 == NULL) || (def2 == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3837 | return (1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3838 | if ((*def1 == NULL) || (*def2 == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3839 | return (1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3840 | while (*def1 != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3841 | while ((*def2) != NULL) { |
| 3842 | if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0) |
| 3843 | return (0); |
| 3844 | def2++; |
| 3845 | } |
| 3846 | def2 = basedef2; |
| 3847 | def1++; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3848 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3849 | return (1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3850 | } |
| 3851 | |
| 3852 | /** |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3853 | * xmlRelaxNGGenerateAttributes: |
| 3854 | * @ctxt: a Relax-NG parser context |
| 3855 | * @def: the definition definition |
| 3856 | * |
| 3857 | * Check if the definition can only generate attributes |
| 3858 | * |
| 3859 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 3860 | */ |
| 3861 | static int |
| 3862 | xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3863 | xmlRelaxNGDefinePtr def) |
| 3864 | { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3865 | xmlRelaxNGDefinePtr parent, cur, tmp; |
| 3866 | |
| 3867 | /* |
| 3868 | * Don't run that check in case of error. Infinite recursion |
| 3869 | * becomes possible. |
| 3870 | */ |
| 3871 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3872 | return (-1); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3873 | |
| 3874 | parent = NULL; |
| 3875 | cur = def; |
| 3876 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3877 | if ((cur->type == XML_RELAXNG_ELEMENT) || |
| 3878 | (cur->type == XML_RELAXNG_TEXT) || |
| 3879 | (cur->type == XML_RELAXNG_DATATYPE) || |
| 3880 | (cur->type == XML_RELAXNG_PARAM) || |
| 3881 | (cur->type == XML_RELAXNG_LIST) || |
| 3882 | (cur->type == XML_RELAXNG_VALUE) || |
| 3883 | (cur->type == XML_RELAXNG_EMPTY)) |
| 3884 | return (0); |
| 3885 | if ((cur->type == XML_RELAXNG_CHOICE) || |
| 3886 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
| 3887 | (cur->type == XML_RELAXNG_GROUP) || |
| 3888 | (cur->type == XML_RELAXNG_ONEORMORE) || |
| 3889 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
| 3890 | (cur->type == XML_RELAXNG_OPTIONAL) || |
| 3891 | (cur->type == XML_RELAXNG_PARENTREF) || |
| 3892 | (cur->type == XML_RELAXNG_EXTERNALREF) || |
| 3893 | (cur->type == XML_RELAXNG_REF) || |
| 3894 | (cur->type == XML_RELAXNG_DEF)) { |
| 3895 | if (cur->content != NULL) { |
| 3896 | parent = cur; |
| 3897 | cur = cur->content; |
| 3898 | tmp = cur; |
| 3899 | while (tmp != NULL) { |
| 3900 | tmp->parent = parent; |
| 3901 | tmp = tmp->next; |
| 3902 | } |
| 3903 | continue; |
| 3904 | } |
| 3905 | } |
| 3906 | if (cur == def) |
| 3907 | break; |
| 3908 | if (cur->next != NULL) { |
| 3909 | cur = cur->next; |
| 3910 | continue; |
| 3911 | } |
| 3912 | do { |
| 3913 | cur = cur->parent; |
| 3914 | if (cur == NULL) |
| 3915 | break; |
| 3916 | if (cur == def) |
| 3917 | return (1); |
| 3918 | if (cur->next != NULL) { |
| 3919 | cur = cur->next; |
| 3920 | break; |
| 3921 | } |
| 3922 | } while (cur != NULL); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3923 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3924 | return (1); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3925 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3926 | |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3927 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3928 | * xmlRelaxNGGetElements: |
| 3929 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3930 | * @def: the definition definition |
| 3931 | * @eora: gather elements (0) or attributes (1) |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3932 | * |
| 3933 | * Compute the list of top elements a definition can generate |
| 3934 | * |
| 3935 | * Returns a list of elements or NULL if none was found. |
| 3936 | */ |
| 3937 | static xmlRelaxNGDefinePtr * |
| 3938 | xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3939 | xmlRelaxNGDefinePtr def, int eora) |
| 3940 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3941 | xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3942 | int len = 0; |
| 3943 | int max = 0; |
| 3944 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3945 | /* |
| 3946 | * Don't run that check in case of error. Infinite recursion |
| 3947 | * becomes possible. |
| 3948 | */ |
| 3949 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3950 | return (NULL); |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3951 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3952 | parent = NULL; |
| 3953 | cur = def; |
| 3954 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3955 | if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) || |
| 3956 | (cur->type == XML_RELAXNG_TEXT))) || |
| 3957 | ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) { |
| 3958 | if (ret == NULL) { |
| 3959 | max = 10; |
| 3960 | ret = (xmlRelaxNGDefinePtr *) |
| 3961 | xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
| 3962 | if (ret == NULL) { |
| 3963 | xmlRngPErrMemory(ctxt, "getting element list\n"); |
| 3964 | return (NULL); |
| 3965 | } |
| 3966 | } else if (max <= len) { |
| 3967 | max *= 2; |
| 3968 | ret = |
| 3969 | xmlRealloc(ret, |
| 3970 | (max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
| 3971 | if (ret == NULL) { |
| 3972 | xmlRngPErrMemory(ctxt, "getting element list\n"); |
| 3973 | return (NULL); |
| 3974 | } |
| 3975 | } |
| 3976 | ret[len++] = cur; |
| 3977 | ret[len] = NULL; |
| 3978 | } else if ((cur->type == XML_RELAXNG_CHOICE) || |
| 3979 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
| 3980 | (cur->type == XML_RELAXNG_GROUP) || |
| 3981 | (cur->type == XML_RELAXNG_ONEORMORE) || |
| 3982 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
| 3983 | (cur->type == XML_RELAXNG_OPTIONAL) || |
| 3984 | (cur->type == XML_RELAXNG_PARENTREF) || |
| 3985 | (cur->type == XML_RELAXNG_REF) || |
William M. Brack | 236c8c0 | 2004-03-20 11:32:36 +0000 | [diff] [blame] | 3986 | (cur->type == XML_RELAXNG_DEF) || |
| 3987 | (cur->type == XML_RELAXNG_EXTERNALREF)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3988 | /* |
| 3989 | * Don't go within elements or attributes or string values. |
| 3990 | * Just gather the element top list |
| 3991 | */ |
| 3992 | if (cur->content != NULL) { |
| 3993 | parent = cur; |
| 3994 | cur = cur->content; |
| 3995 | tmp = cur; |
| 3996 | while (tmp != NULL) { |
| 3997 | tmp->parent = parent; |
| 3998 | tmp = tmp->next; |
| 3999 | } |
| 4000 | continue; |
| 4001 | } |
| 4002 | } |
| 4003 | if (cur == def) |
| 4004 | break; |
| 4005 | if (cur->next != NULL) { |
| 4006 | cur = cur->next; |
| 4007 | continue; |
| 4008 | } |
| 4009 | do { |
| 4010 | cur = cur->parent; |
| 4011 | if (cur == NULL) |
| 4012 | break; |
| 4013 | if (cur == def) |
| 4014 | return (ret); |
| 4015 | if (cur->next != NULL) { |
| 4016 | cur = cur->next; |
| 4017 | break; |
| 4018 | } |
| 4019 | } while (cur != NULL); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4020 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4021 | return (ret); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4022 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4023 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4024 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4025 | * xmlRelaxNGCheckChoiceDeterminism: |
| 4026 | * @ctxt: a Relax-NG parser context |
| 4027 | * @def: the choice definition |
| 4028 | * |
| 4029 | * Also used to find indeterministic pattern in choice |
| 4030 | */ |
| 4031 | static void |
| 4032 | xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4033 | xmlRelaxNGDefinePtr def) |
| 4034 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4035 | xmlRelaxNGDefinePtr **list; |
| 4036 | xmlRelaxNGDefinePtr cur; |
| 4037 | int nbchild = 0, i, j, ret; |
| 4038 | int is_nullable = 0; |
| 4039 | int is_indeterminist = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4040 | xmlHashTablePtr triage = NULL; |
| 4041 | int is_triable = 1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4042 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4043 | if ((def == NULL) || (def->type != XML_RELAXNG_CHOICE)) |
| 4044 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4045 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4046 | if (def->dflags & IS_PROCESSED) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4047 | return; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4048 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4049 | /* |
| 4050 | * Don't run that check in case of error. Infinite recursion |
| 4051 | * becomes possible. |
| 4052 | */ |
| 4053 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4054 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4055 | |
| 4056 | is_nullable = xmlRelaxNGIsNullable(def); |
| 4057 | |
| 4058 | cur = def->content; |
| 4059 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4060 | nbchild++; |
| 4061 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4062 | } |
| 4063 | |
| 4064 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4065 | sizeof(xmlRelaxNGDefinePtr |
| 4066 | *)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4067 | if (list == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4068 | xmlRngPErrMemory(ctxt, "building choice\n"); |
| 4069 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4070 | } |
| 4071 | i = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4072 | /* |
| 4073 | * a bit strong but safe |
| 4074 | */ |
| 4075 | if (is_nullable == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4076 | triage = xmlHashCreate(10); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4077 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4078 | is_triable = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4079 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4080 | cur = def->content; |
| 4081 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4082 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 0); |
| 4083 | if ((list[i] == NULL) || (list[i][0] == NULL)) { |
| 4084 | is_triable = 0; |
| 4085 | } else if (is_triable == 1) { |
| 4086 | xmlRelaxNGDefinePtr *tmp; |
| 4087 | int res; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4088 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4089 | tmp = list[i]; |
| 4090 | while ((*tmp != NULL) && (is_triable == 1)) { |
| 4091 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
| 4092 | res = xmlHashAddEntry2(triage, |
| 4093 | BAD_CAST "#text", NULL, |
| 4094 | (void *) cur); |
| 4095 | if (res != 0) |
| 4096 | is_triable = -1; |
| 4097 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
| 4098 | ((*tmp)->name != NULL)) { |
| 4099 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4100 | res = xmlHashAddEntry2(triage, |
| 4101 | (*tmp)->name, NULL, |
| 4102 | (void *) cur); |
| 4103 | else |
| 4104 | res = xmlHashAddEntry2(triage, |
| 4105 | (*tmp)->name, (*tmp)->ns, |
| 4106 | (void *) cur); |
| 4107 | if (res != 0) |
| 4108 | is_triable = -1; |
| 4109 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
| 4110 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4111 | res = xmlHashAddEntry2(triage, |
| 4112 | BAD_CAST "#any", NULL, |
| 4113 | (void *) cur); |
| 4114 | else |
| 4115 | res = xmlHashAddEntry2(triage, |
| 4116 | BAD_CAST "#any", (*tmp)->ns, |
| 4117 | (void *) cur); |
| 4118 | if (res != 0) |
| 4119 | is_triable = -1; |
| 4120 | } else { |
| 4121 | is_triable = -1; |
| 4122 | } |
| 4123 | tmp++; |
| 4124 | } |
| 4125 | } |
| 4126 | i++; |
| 4127 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4128 | } |
| 4129 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4130 | for (i = 0; i < nbchild; i++) { |
| 4131 | if (list[i] == NULL) |
| 4132 | continue; |
| 4133 | for (j = 0; j < i; j++) { |
| 4134 | if (list[j] == NULL) |
| 4135 | continue; |
| 4136 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
| 4137 | if (ret == 0) { |
| 4138 | is_indeterminist = 1; |
| 4139 | } |
| 4140 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4141 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4142 | for (i = 0; i < nbchild; i++) { |
| 4143 | if (list[i] != NULL) |
| 4144 | xmlFree(list[i]); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4145 | } |
| 4146 | |
| 4147 | xmlFree(list); |
| 4148 | if (is_indeterminist) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4149 | def->dflags |= IS_INDETERMINIST; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4150 | } |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4151 | if (is_triable == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4152 | def->dflags |= IS_TRIABLE; |
| 4153 | def->data = triage; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4154 | } else if (triage != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4155 | xmlHashFree(triage, NULL); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4156 | } |
| 4157 | def->dflags |= IS_PROCESSED; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4158 | } |
| 4159 | |
| 4160 | /** |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4161 | * xmlRelaxNGCheckGroupAttrs: |
| 4162 | * @ctxt: a Relax-NG parser context |
| 4163 | * @def: the group definition |
| 4164 | * |
| 4165 | * Detects violations of rule 7.3 |
| 4166 | */ |
| 4167 | static void |
| 4168 | xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4169 | xmlRelaxNGDefinePtr def) |
| 4170 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4171 | xmlRelaxNGDefinePtr **list; |
| 4172 | xmlRelaxNGDefinePtr cur; |
| 4173 | int nbchild = 0, i, j, ret; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4174 | |
| 4175 | if ((def == NULL) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4176 | ((def->type != XML_RELAXNG_GROUP) && |
| 4177 | (def->type != XML_RELAXNG_ELEMENT))) |
| 4178 | return; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4179 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4180 | if (def->dflags & IS_PROCESSED) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4181 | return; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4182 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4183 | /* |
| 4184 | * Don't run that check in case of error. Infinite recursion |
| 4185 | * becomes possible. |
| 4186 | */ |
| 4187 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4188 | return; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4189 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4190 | cur = def->attrs; |
| 4191 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4192 | nbchild++; |
| 4193 | cur = cur->next; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4194 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4195 | cur = def->content; |
| 4196 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4197 | nbchild++; |
| 4198 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4199 | } |
| 4200 | |
| 4201 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4202 | sizeof(xmlRelaxNGDefinePtr |
| 4203 | *)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4204 | if (list == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4205 | xmlRngPErrMemory(ctxt, "building group\n"); |
| 4206 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4207 | } |
| 4208 | i = 0; |
| 4209 | cur = def->attrs; |
| 4210 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4211 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4212 | i++; |
| 4213 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4214 | } |
| 4215 | cur = def->content; |
| 4216 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4217 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4218 | i++; |
| 4219 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4220 | } |
| 4221 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4222 | for (i = 0; i < nbchild; i++) { |
| 4223 | if (list[i] == NULL) |
| 4224 | continue; |
| 4225 | for (j = 0; j < i; j++) { |
| 4226 | if (list[j] == NULL) |
| 4227 | continue; |
| 4228 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
| 4229 | if (ret == 0) { |
| 4230 | xmlRngPErr(ctxt, def->node, XML_RNGP_GROUP_ATTR_CONFLICT, |
| 4231 | "Attributes conflicts in group\n", NULL, NULL); |
| 4232 | } |
| 4233 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4234 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4235 | for (i = 0; i < nbchild; i++) { |
| 4236 | if (list[i] != NULL) |
| 4237 | xmlFree(list[i]); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4238 | } |
| 4239 | |
| 4240 | xmlFree(list); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4241 | def->dflags |= IS_PROCESSED; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4242 | } |
| 4243 | |
| 4244 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4245 | * xmlRelaxNGComputeInterleaves: |
| 4246 | * @def: the interleave definition |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4247 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4248 | * @name: the definition name |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4249 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4250 | * A lot of work for preprocessing interleave definitions |
| 4251 | * is potentially needed to get a decent execution speed at runtime |
| 4252 | * - trying to get a total order on the element nodes generated |
| 4253 | * by the interleaves, order the list of interleave definitions |
| 4254 | * following that order. |
| 4255 | * - if <text/> is used to handle mixed content, it is better to |
| 4256 | * flag this in the define and simplify the runtime checking |
| 4257 | * algorithm |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4258 | */ |
| 4259 | static void |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4260 | xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4261 | xmlRelaxNGParserCtxtPtr ctxt, |
| 4262 | xmlChar * name ATTRIBUTE_UNUSED) |
| 4263 | { |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4264 | xmlRelaxNGDefinePtr cur, *tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4265 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4266 | xmlRelaxNGPartitionPtr partitions = NULL; |
| 4267 | xmlRelaxNGInterleaveGroupPtr *groups = NULL; |
| 4268 | xmlRelaxNGInterleaveGroupPtr group; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4269 | int i, j, ret, res; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4270 | int nbgroups = 0; |
| 4271 | int nbchild = 0; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 4272 | int is_mixed = 0; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4273 | int is_determinist = 1; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4274 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4275 | /* |
| 4276 | * Don't run that check in case of error. Infinite recursion |
| 4277 | * becomes possible. |
| 4278 | */ |
| 4279 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4280 | return; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4281 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4282 | #ifdef DEBUG_INTERLEAVE |
| 4283 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4284 | "xmlRelaxNGComputeInterleaves(%s)\n", name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4285 | #endif |
| 4286 | cur = def->content; |
| 4287 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4288 | nbchild++; |
| 4289 | cur = cur->next; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4290 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4291 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4292 | #ifdef DEBUG_INTERLEAVE |
| 4293 | xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild); |
| 4294 | #endif |
| 4295 | groups = (xmlRelaxNGInterleaveGroupPtr *) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4296 | xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4297 | if (groups == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4298 | goto error; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4299 | cur = def->content; |
| 4300 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4301 | groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr) |
| 4302 | xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup)); |
| 4303 | if (groups[nbgroups] == NULL) |
| 4304 | goto error; |
| 4305 | if (cur->type == XML_RELAXNG_TEXT) |
| 4306 | is_mixed++; |
| 4307 | groups[nbgroups]->rule = cur; |
| 4308 | groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0); |
| 4309 | groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4310 | nbgroups++; |
| 4311 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4312 | } |
| 4313 | #ifdef DEBUG_INTERLEAVE |
| 4314 | xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups); |
| 4315 | #endif |
| 4316 | |
| 4317 | /* |
| 4318 | * Let's check that all rules makes a partitions according to 7.4 |
| 4319 | */ |
| 4320 | partitions = (xmlRelaxNGPartitionPtr) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4321 | xmlMalloc(sizeof(xmlRelaxNGPartition)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4322 | if (partitions == NULL) |
| 4323 | goto error; |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 4324 | memset(partitions, 0, sizeof(xmlRelaxNGPartition)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4325 | partitions->nbgroups = nbgroups; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4326 | partitions->triage = xmlHashCreate(nbgroups); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4327 | for (i = 0; i < nbgroups; i++) { |
| 4328 | group = groups[i]; |
| 4329 | for (j = i + 1; j < nbgroups; j++) { |
| 4330 | if (groups[j] == NULL) |
| 4331 | continue; |
| 4332 | |
| 4333 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs, |
| 4334 | groups[j]->defs); |
| 4335 | if (ret == 0) { |
| 4336 | xmlRngPErr(ctxt, def->node, XML_RNGP_ELEM_TEXT_CONFLICT, |
| 4337 | "Element or text conflicts in interleave\n", |
| 4338 | NULL, NULL); |
| 4339 | } |
| 4340 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs, |
| 4341 | groups[j]->attrs); |
| 4342 | if (ret == 0) { |
| 4343 | xmlRngPErr(ctxt, def->node, XML_RNGP_ATTR_CONFLICT, |
| 4344 | "Attributes conflicts in interleave\n", NULL, |
| 4345 | NULL); |
| 4346 | } |
| 4347 | } |
| 4348 | tmp = group->defs; |
| 4349 | if ((tmp != NULL) && (*tmp != NULL)) { |
| 4350 | while (*tmp != NULL) { |
| 4351 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
| 4352 | res = xmlHashAddEntry2(partitions->triage, |
| 4353 | BAD_CAST "#text", NULL, |
| 4354 | (void *) (long) (i + 1)); |
| 4355 | if (res != 0) |
| 4356 | is_determinist = -1; |
| 4357 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
| 4358 | ((*tmp)->name != NULL)) { |
| 4359 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4360 | res = xmlHashAddEntry2(partitions->triage, |
| 4361 | (*tmp)->name, NULL, |
| 4362 | (void *) (long) (i + 1)); |
| 4363 | else |
| 4364 | res = xmlHashAddEntry2(partitions->triage, |
| 4365 | (*tmp)->name, (*tmp)->ns, |
| 4366 | (void *) (long) (i + 1)); |
| 4367 | if (res != 0) |
| 4368 | is_determinist = -1; |
| 4369 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
| 4370 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4371 | res = xmlHashAddEntry2(partitions->triage, |
| 4372 | BAD_CAST "#any", NULL, |
| 4373 | (void *) (long) (i + 1)); |
| 4374 | else |
| 4375 | res = xmlHashAddEntry2(partitions->triage, |
| 4376 | BAD_CAST "#any", (*tmp)->ns, |
| 4377 | (void *) (long) (i + 1)); |
| 4378 | if ((*tmp)->nameClass != NULL) |
| 4379 | is_determinist = 2; |
| 4380 | if (res != 0) |
| 4381 | is_determinist = -1; |
| 4382 | } else { |
| 4383 | is_determinist = -1; |
| 4384 | } |
| 4385 | tmp++; |
| 4386 | } |
| 4387 | } else { |
| 4388 | is_determinist = 0; |
| 4389 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4390 | } |
| 4391 | partitions->groups = groups; |
| 4392 | |
| 4393 | /* |
| 4394 | * and save the partition list back in the def |
| 4395 | */ |
| 4396 | def->data = partitions; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 4397 | if (is_mixed != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4398 | def->dflags |= IS_MIXED; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4399 | if (is_determinist == 1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4400 | partitions->flags = IS_DETERMINIST; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4401 | if (is_determinist == 2) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4402 | partitions->flags = IS_DETERMINIST | IS_NEEDCHECK; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4403 | return; |
| 4404 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4405 | error: |
| 4406 | xmlRngPErrMemory(ctxt, "in interleave computation\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4407 | if (groups != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4408 | for (i = 0; i < nbgroups; i++) |
| 4409 | if (groups[i] != NULL) { |
| 4410 | if (groups[i]->defs != NULL) |
| 4411 | xmlFree(groups[i]->defs); |
| 4412 | xmlFree(groups[i]); |
| 4413 | } |
| 4414 | xmlFree(groups); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4415 | } |
| 4416 | xmlRelaxNGFreePartition(partitions); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4417 | } |
| 4418 | |
| 4419 | /** |
| 4420 | * xmlRelaxNGParseInterleave: |
| 4421 | * @ctxt: a Relax-NG parser context |
| 4422 | * @node: the data node. |
| 4423 | * |
| 4424 | * parse the content of a RelaxNG interleave node. |
| 4425 | * |
| 4426 | * Returns the definition pointer or NULL in case of error |
| 4427 | */ |
| 4428 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4429 | xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4430 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4431 | xmlRelaxNGDefinePtr def = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4432 | xmlRelaxNGDefinePtr last = NULL, cur; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4433 | xmlNodePtr child; |
| 4434 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4435 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4436 | if (def == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4437 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4438 | } |
| 4439 | def->type = XML_RELAXNG_INTERLEAVE; |
| 4440 | |
| 4441 | if (ctxt->interleaves == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4442 | ctxt->interleaves = xmlHashCreate(10); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4443 | if (ctxt->interleaves == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4444 | xmlRngPErrMemory(ctxt, "create interleaves\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4445 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4446 | char name[32]; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4447 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4448 | snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++); |
| 4449 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) { |
| 4450 | xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_ADD, |
| 4451 | "Failed to add %s to hash table\n", |
| 4452 | (const xmlChar *) name, NULL); |
| 4453 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4454 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4455 | child = node->children; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4456 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4457 | xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_NO_CONTENT, |
| 4458 | "Element interleave is empty\n", NULL, NULL); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 4459 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4460 | while (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4461 | if (IS_RELAXNG(child, "element")) { |
| 4462 | cur = xmlRelaxNGParseElement(ctxt, child); |
| 4463 | } else { |
| 4464 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 4465 | } |
| 4466 | if (cur != NULL) { |
| 4467 | cur->parent = def; |
| 4468 | if (last == NULL) { |
| 4469 | def->content = last = cur; |
| 4470 | } else { |
| 4471 | last->next = cur; |
| 4472 | last = cur; |
| 4473 | } |
| 4474 | } |
| 4475 | child = child->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4476 | } |
| 4477 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4478 | return (def); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4479 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4480 | |
| 4481 | /** |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4482 | * xmlRelaxNGParseInclude: |
| 4483 | * @ctxt: a Relax-NG parser context |
| 4484 | * @node: the include node |
| 4485 | * |
| 4486 | * Integrate the content of an include node in the current grammar |
| 4487 | * |
| 4488 | * Returns 0 in case of success or -1 in case of error |
| 4489 | */ |
| 4490 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4491 | xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4492 | { |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4493 | xmlRelaxNGIncludePtr incl; |
| 4494 | xmlNodePtr root; |
| 4495 | int ret = 0, tmp; |
| 4496 | |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 4497 | incl = node->psvi; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4498 | if (incl == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4499 | xmlRngPErr(ctxt, node, XML_RNGP_INCLUDE_EMPTY, |
| 4500 | "Include node has no data\n", NULL, NULL); |
| 4501 | return (-1); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4502 | } |
| 4503 | root = xmlDocGetRootElement(incl->doc); |
| 4504 | if (root == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4505 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, "Include document is empty\n", |
| 4506 | NULL, NULL); |
| 4507 | return (-1); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4508 | } |
| 4509 | if (!xmlStrEqual(root->name, BAD_CAST "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4510 | xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING, |
| 4511 | "Include document root is not a grammar\n", NULL, NULL); |
| 4512 | return (-1); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4513 | } |
| 4514 | |
| 4515 | /* |
| 4516 | * Merge the definition from both the include and the internal list |
| 4517 | */ |
| 4518 | if (root->children != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4519 | tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children); |
| 4520 | if (tmp != 0) |
| 4521 | ret = -1; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4522 | } |
| 4523 | if (node->children != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4524 | tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children); |
| 4525 | if (tmp != 0) |
| 4526 | ret = -1; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4527 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4528 | return (ret); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4529 | } |
| 4530 | |
| 4531 | /** |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4532 | * xmlRelaxNGParseDefine: |
| 4533 | * @ctxt: a Relax-NG parser context |
| 4534 | * @node: the define node |
| 4535 | * |
| 4536 | * parse the content of a RelaxNG define element node. |
| 4537 | * |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4538 | * Returns 0 in case of success or -1 in case of error |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4539 | */ |
| 4540 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4541 | xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4542 | { |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4543 | xmlChar *name; |
| 4544 | int ret = 0, tmp; |
| 4545 | xmlRelaxNGDefinePtr def; |
| 4546 | const xmlChar *olddefine; |
| 4547 | |
| 4548 | name = xmlGetProp(node, BAD_CAST "name"); |
| 4549 | if (name == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4550 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_NAME_MISSING, |
| 4551 | "define has no name\n", NULL, NULL); |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4552 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4553 | xmlRelaxNGNormExtSpace(name); |
| 4554 | if (xmlValidateNCName(name, 0)) { |
| 4555 | xmlRngPErr(ctxt, node, XML_RNGP_INVALID_DEFINE_NAME, |
| 4556 | "define name '%s' is not an NCName\n", name, NULL); |
| 4557 | } |
| 4558 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4559 | if (def == NULL) { |
| 4560 | xmlFree(name); |
| 4561 | return (-1); |
| 4562 | } |
| 4563 | def->type = XML_RELAXNG_DEF; |
| 4564 | def->name = name; |
| 4565 | if (node->children == NULL) { |
| 4566 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_EMPTY, |
| 4567 | "define has no children\n", NULL, NULL); |
| 4568 | } else { |
| 4569 | olddefine = ctxt->define; |
| 4570 | ctxt->define = name; |
| 4571 | def->content = |
| 4572 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4573 | ctxt->define = olddefine; |
| 4574 | } |
| 4575 | if (ctxt->grammar->defs == NULL) |
| 4576 | ctxt->grammar->defs = xmlHashCreate(10); |
| 4577 | if (ctxt->grammar->defs == NULL) { |
| 4578 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED, |
| 4579 | "Could not create definition hash\n", NULL, NULL); |
| 4580 | ret = -1; |
| 4581 | } else { |
| 4582 | tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def); |
| 4583 | if (tmp < 0) { |
| 4584 | xmlRelaxNGDefinePtr prev; |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 4585 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4586 | prev = xmlHashLookup(ctxt->grammar->defs, name); |
| 4587 | if (prev == NULL) { |
| 4588 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED, |
| 4589 | "Internal error on define aggregation of %s\n", |
| 4590 | name, NULL); |
| 4591 | ret = -1; |
| 4592 | } else { |
| 4593 | while (prev->nextHash != NULL) |
| 4594 | prev = prev->nextHash; |
| 4595 | prev->nextHash = def; |
| 4596 | } |
| 4597 | } |
| 4598 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4599 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4600 | return (ret); |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4601 | } |
| 4602 | |
| 4603 | /** |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4604 | * xmlRelaxNGProcessExternalRef: |
| 4605 | * @ctxt: the parser context |
| 4606 | * @node: the externlRef node |
| 4607 | * |
| 4608 | * Process and compile an externlRef node |
| 4609 | * |
| 4610 | * Returns the xmlRelaxNGDefinePtr or NULL in case of error |
| 4611 | */ |
| 4612 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4613 | xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4614 | { |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4615 | xmlRelaxNGDocumentPtr docu; |
| 4616 | xmlNodePtr root, tmp; |
| 4617 | xmlChar *ns; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4618 | int newNs = 0, oldflags; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4619 | xmlRelaxNGDefinePtr def; |
| 4620 | |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 4621 | docu = node->psvi; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4622 | if (docu != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4623 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4624 | if (def == NULL) |
| 4625 | return (NULL); |
| 4626 | def->type = XML_RELAXNG_EXTERNALREF; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4627 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4628 | if (docu->content == NULL) { |
| 4629 | /* |
| 4630 | * Then do the parsing for good |
| 4631 | */ |
| 4632 | root = xmlDocGetRootElement(docu->doc); |
| 4633 | if (root == NULL) { |
| 4634 | xmlRngPErr(ctxt, node, XML_RNGP_EXTERNALREF_EMTPY, |
| 4635 | "xmlRelaxNGParse: %s is empty\n", ctxt->URL, |
| 4636 | NULL); |
| 4637 | return (NULL); |
| 4638 | } |
| 4639 | /* |
| 4640 | * ns transmission rules |
| 4641 | */ |
| 4642 | ns = xmlGetProp(root, BAD_CAST "ns"); |
| 4643 | if (ns == NULL) { |
| 4644 | tmp = node; |
| 4645 | while ((tmp != NULL) && (tmp->type == XML_ELEMENT_NODE)) { |
| 4646 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 4647 | if (ns != NULL) { |
| 4648 | break; |
| 4649 | } |
| 4650 | tmp = tmp->parent; |
| 4651 | } |
| 4652 | if (ns != NULL) { |
| 4653 | xmlSetProp(root, BAD_CAST "ns", ns); |
| 4654 | newNs = 1; |
| 4655 | xmlFree(ns); |
| 4656 | } |
| 4657 | } else { |
| 4658 | xmlFree(ns); |
| 4659 | } |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4660 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4661 | /* |
| 4662 | * Parsing to get a precompiled schemas. |
| 4663 | */ |
| 4664 | oldflags = ctxt->flags; |
| 4665 | ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF; |
| 4666 | docu->schema = xmlRelaxNGParseDocument(ctxt, root); |
| 4667 | ctxt->flags = oldflags; |
| 4668 | if ((docu->schema != NULL) && |
| 4669 | (docu->schema->topgrammar != NULL)) { |
| 4670 | docu->content = docu->schema->topgrammar->start; |
| 4671 | } |
| 4672 | |
| 4673 | /* |
| 4674 | * the externalRef may be reused in a different ns context |
| 4675 | */ |
| 4676 | if (newNs == 1) { |
| 4677 | xmlUnsetProp(root, BAD_CAST "ns"); |
| 4678 | } |
| 4679 | } |
| 4680 | def->content = docu->content; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4681 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4682 | def = NULL; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4683 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4684 | return (def); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4685 | } |
| 4686 | |
| 4687 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4688 | * xmlRelaxNGParsePattern: |
| 4689 | * @ctxt: a Relax-NG parser context |
| 4690 | * @node: the pattern node. |
| 4691 | * |
| 4692 | * parse the content of a RelaxNG pattern node. |
| 4693 | * |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4694 | * Returns the definition pointer or NULL in case of error or if no |
| 4695 | * pattern is generated. |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4696 | */ |
| 4697 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4698 | xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4699 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4700 | xmlRelaxNGDefinePtr def = NULL; |
| 4701 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4702 | if (node == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4703 | return (NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4704 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4705 | if (IS_RELAXNG(node, "element")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4706 | def = xmlRelaxNGParseElement(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4707 | } else if (IS_RELAXNG(node, "attribute")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4708 | def = xmlRelaxNGParseAttribute(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4709 | } else if (IS_RELAXNG(node, "empty")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4710 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4711 | if (def == NULL) |
| 4712 | return (NULL); |
| 4713 | def->type = XML_RELAXNG_EMPTY; |
| 4714 | if (node->children != NULL) { |
| 4715 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_NOT_EMPTY, |
| 4716 | "empty: had a child node\n", NULL, NULL); |
| 4717 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4718 | } else if (IS_RELAXNG(node, "text")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4719 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4720 | if (def == NULL) |
| 4721 | return (NULL); |
| 4722 | def->type = XML_RELAXNG_TEXT; |
| 4723 | if (node->children != NULL) { |
| 4724 | xmlRngPErr(ctxt, node, XML_RNGP_TEXT_HAS_CHILD, |
| 4725 | "text: had a child node\n", NULL, NULL); |
| 4726 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4727 | } else if (IS_RELAXNG(node, "zeroOrMore")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4728 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4729 | if (def == NULL) |
| 4730 | return (NULL); |
| 4731 | def->type = XML_RELAXNG_ZEROORMORE; |
| 4732 | if (node->children == NULL) { |
| 4733 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4734 | "Element %s is empty\n", node->name, NULL); |
| 4735 | } else { |
| 4736 | def->content = |
| 4737 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4738 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4739 | } else if (IS_RELAXNG(node, "oneOrMore")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4740 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4741 | if (def == NULL) |
| 4742 | return (NULL); |
| 4743 | def->type = XML_RELAXNG_ONEORMORE; |
| 4744 | if (node->children == NULL) { |
| 4745 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4746 | "Element %s is empty\n", node->name, NULL); |
| 4747 | } else { |
| 4748 | def->content = |
| 4749 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4750 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4751 | } else if (IS_RELAXNG(node, "optional")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4752 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4753 | if (def == NULL) |
| 4754 | return (NULL); |
| 4755 | def->type = XML_RELAXNG_OPTIONAL; |
| 4756 | if (node->children == NULL) { |
| 4757 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4758 | "Element %s is empty\n", node->name, NULL); |
| 4759 | } else { |
| 4760 | def->content = |
| 4761 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4762 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4763 | } else if (IS_RELAXNG(node, "choice")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4764 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4765 | if (def == NULL) |
| 4766 | return (NULL); |
| 4767 | def->type = XML_RELAXNG_CHOICE; |
| 4768 | if (node->children == NULL) { |
| 4769 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4770 | "Element %s is empty\n", node->name, NULL); |
| 4771 | } else { |
| 4772 | def->content = |
| 4773 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4774 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4775 | } else if (IS_RELAXNG(node, "group")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4776 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4777 | if (def == NULL) |
| 4778 | return (NULL); |
| 4779 | def->type = XML_RELAXNG_GROUP; |
| 4780 | if (node->children == NULL) { |
| 4781 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4782 | "Element %s is empty\n", node->name, NULL); |
| 4783 | } else { |
| 4784 | def->content = |
| 4785 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4786 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4787 | } else if (IS_RELAXNG(node, "ref")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4788 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4789 | if (def == NULL) |
| 4790 | return (NULL); |
| 4791 | def->type = XML_RELAXNG_REF; |
| 4792 | def->name = xmlGetProp(node, BAD_CAST "name"); |
| 4793 | if (def->name == NULL) { |
| 4794 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NO_NAME, "ref has no name\n", |
| 4795 | NULL, NULL); |
| 4796 | } else { |
| 4797 | xmlRelaxNGNormExtSpace(def->name); |
| 4798 | if (xmlValidateNCName(def->name, 0)) { |
| 4799 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NAME_INVALID, |
| 4800 | "ref name '%s' is not an NCName\n", def->name, |
| 4801 | NULL); |
| 4802 | } |
| 4803 | } |
| 4804 | if (node->children != NULL) { |
| 4805 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NOT_EMPTY, "ref is not empty\n", |
| 4806 | NULL, NULL); |
| 4807 | } |
| 4808 | if (ctxt->grammar->refs == NULL) |
| 4809 | ctxt->grammar->refs = xmlHashCreate(10); |
| 4810 | if (ctxt->grammar->refs == NULL) { |
| 4811 | xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED, |
| 4812 | "Could not create references hash\n", NULL, NULL); |
| 4813 | def = NULL; |
| 4814 | } else { |
| 4815 | int tmp; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4816 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4817 | tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def); |
| 4818 | if (tmp < 0) { |
| 4819 | xmlRelaxNGDefinePtr prev; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4820 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4821 | prev = (xmlRelaxNGDefinePtr) |
| 4822 | xmlHashLookup(ctxt->grammar->refs, def->name); |
| 4823 | if (prev == NULL) { |
| 4824 | if (def->name != NULL) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 4825 | xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED, |
| 4826 | "Error refs definitions '%s'\n", |
| 4827 | def->name, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4828 | } else { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 4829 | xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED, |
| 4830 | "Error refs definitions\n", |
| 4831 | NULL, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4832 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4833 | def = NULL; |
| 4834 | } else { |
| 4835 | def->nextHash = prev->nextHash; |
| 4836 | prev->nextHash = def; |
| 4837 | } |
| 4838 | } |
| 4839 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 4840 | } else if (IS_RELAXNG(node, "data")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4841 | def = xmlRelaxNGParseData(ctxt, node); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 4842 | } else if (IS_RELAXNG(node, "value")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4843 | def = xmlRelaxNGParseValue(ctxt, node); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 4844 | } else if (IS_RELAXNG(node, "list")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4845 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4846 | if (def == NULL) |
| 4847 | return (NULL); |
| 4848 | def->type = XML_RELAXNG_LIST; |
| 4849 | if (node->children == NULL) { |
| 4850 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4851 | "Element %s is empty\n", node->name, NULL); |
| 4852 | } else { |
| 4853 | def->content = |
| 4854 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4855 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4856 | } else if (IS_RELAXNG(node, "interleave")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4857 | def = xmlRelaxNGParseInterleave(ctxt, node); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4858 | } else if (IS_RELAXNG(node, "externalRef")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4859 | def = xmlRelaxNGProcessExternalRef(ctxt, node); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4860 | } else if (IS_RELAXNG(node, "notAllowed")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4861 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4862 | if (def == NULL) |
| 4863 | return (NULL); |
| 4864 | def->type = XML_RELAXNG_NOT_ALLOWED; |
| 4865 | if (node->children != NULL) { |
| 4866 | xmlRngPErr(ctxt, node, XML_RNGP_NOTALLOWED_NOT_EMPTY, |
| 4867 | "xmlRelaxNGParse: notAllowed element is not empty\n", |
| 4868 | NULL, NULL); |
| 4869 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4870 | } else if (IS_RELAXNG(node, "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4871 | xmlRelaxNGGrammarPtr grammar, old; |
| 4872 | xmlRelaxNGGrammarPtr oldparent; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4873 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4874 | #ifdef DEBUG_GRAMMAR |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4875 | xmlGenericError(xmlGenericErrorContext, |
| 4876 | "Found <grammar> pattern\n"); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4877 | #endif |
| 4878 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4879 | oldparent = ctxt->parentgrammar; |
| 4880 | old = ctxt->grammar; |
| 4881 | ctxt->parentgrammar = old; |
| 4882 | grammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
| 4883 | if (old != NULL) { |
| 4884 | ctxt->grammar = old; |
| 4885 | ctxt->parentgrammar = oldparent; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4886 | #if 0 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4887 | if (grammar != NULL) { |
| 4888 | grammar->next = old->next; |
| 4889 | old->next = grammar; |
| 4890 | } |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4891 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4892 | } |
| 4893 | if (grammar != NULL) |
| 4894 | def = grammar->start; |
| 4895 | else |
| 4896 | def = NULL; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4897 | } else if (IS_RELAXNG(node, "parentRef")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4898 | if (ctxt->parentgrammar == NULL) { |
| 4899 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_PARENT, |
| 4900 | "Use of parentRef without a parent grammar\n", NULL, |
| 4901 | NULL); |
| 4902 | return (NULL); |
| 4903 | } |
| 4904 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4905 | if (def == NULL) |
| 4906 | return (NULL); |
| 4907 | def->type = XML_RELAXNG_PARENTREF; |
| 4908 | def->name = xmlGetProp(node, BAD_CAST "name"); |
| 4909 | if (def->name == NULL) { |
| 4910 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_NAME, |
| 4911 | "parentRef has no name\n", NULL, NULL); |
| 4912 | } else { |
| 4913 | xmlRelaxNGNormExtSpace(def->name); |
| 4914 | if (xmlValidateNCName(def->name, 0)) { |
| 4915 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NAME_INVALID, |
| 4916 | "parentRef name '%s' is not an NCName\n", |
| 4917 | def->name, NULL); |
| 4918 | } |
| 4919 | } |
| 4920 | if (node->children != NULL) { |
| 4921 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NOT_EMPTY, |
| 4922 | "parentRef is not empty\n", NULL, NULL); |
| 4923 | } |
| 4924 | if (ctxt->parentgrammar->refs == NULL) |
| 4925 | ctxt->parentgrammar->refs = xmlHashCreate(10); |
| 4926 | if (ctxt->parentgrammar->refs == NULL) { |
| 4927 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED, |
| 4928 | "Could not create references hash\n", NULL, NULL); |
| 4929 | def = NULL; |
| 4930 | } else if (def->name != NULL) { |
| 4931 | int tmp; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4932 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4933 | tmp = |
| 4934 | xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def); |
| 4935 | if (tmp < 0) { |
| 4936 | xmlRelaxNGDefinePtr prev; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4937 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4938 | prev = (xmlRelaxNGDefinePtr) |
| 4939 | xmlHashLookup(ctxt->parentgrammar->refs, def->name); |
| 4940 | if (prev == NULL) { |
| 4941 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED, |
| 4942 | "Internal error parentRef definitions '%s'\n", |
| 4943 | def->name, NULL); |
| 4944 | def = NULL; |
| 4945 | } else { |
| 4946 | def->nextHash = prev->nextHash; |
| 4947 | prev->nextHash = def; |
| 4948 | } |
| 4949 | } |
| 4950 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4951 | } else if (IS_RELAXNG(node, "mixed")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4952 | if (node->children == NULL) { |
| 4953 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, "Mixed is empty\n", |
| 4954 | NULL, NULL); |
| 4955 | def = NULL; |
| 4956 | } else { |
| 4957 | def = xmlRelaxNGParseInterleave(ctxt, node); |
| 4958 | if (def != NULL) { |
| 4959 | xmlRelaxNGDefinePtr tmp; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4960 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4961 | if ((def->content != NULL) && (def->content->next != NULL)) { |
| 4962 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
| 4963 | if (tmp != NULL) { |
| 4964 | tmp->type = XML_RELAXNG_GROUP; |
| 4965 | tmp->content = def->content; |
| 4966 | def->content = tmp; |
| 4967 | } |
| 4968 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4969 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4970 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
| 4971 | if (tmp == NULL) |
| 4972 | return (def); |
| 4973 | tmp->type = XML_RELAXNG_TEXT; |
| 4974 | tmp->next = def->content; |
| 4975 | def->content = tmp; |
| 4976 | } |
| 4977 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4978 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4979 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_CONSTRUCT, |
| 4980 | "Unexpected node %s is not a pattern\n", node->name, |
| 4981 | NULL); |
| 4982 | def = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4983 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4984 | return (def); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4985 | } |
| 4986 | |
| 4987 | /** |
| 4988 | * xmlRelaxNGParseAttribute: |
| 4989 | * @ctxt: a Relax-NG parser context |
| 4990 | * @node: the element node |
| 4991 | * |
| 4992 | * parse the content of a RelaxNG attribute node. |
| 4993 | * |
| 4994 | * Returns the definition pointer or NULL in case of error. |
| 4995 | */ |
| 4996 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4997 | xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4998 | { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4999 | xmlRelaxNGDefinePtr ret, cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5000 | xmlNodePtr child; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5001 | int old_flags; |
| 5002 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5003 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5004 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5005 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5006 | ret->type = XML_RELAXNG_ATTRIBUTE; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5007 | ret->parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5008 | child = node->children; |
| 5009 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5010 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_EMPTY, |
| 5011 | "xmlRelaxNGParseattribute: attribute has no children\n", |
| 5012 | NULL, NULL); |
| 5013 | return (ret); |
| 5014 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5015 | old_flags = ctxt->flags; |
| 5016 | ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5017 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5018 | if (cur != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5019 | child = child->next; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5020 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5021 | if (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5022 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 5023 | if (cur != NULL) { |
| 5024 | switch (cur->type) { |
| 5025 | case XML_RELAXNG_EMPTY: |
| 5026 | case XML_RELAXNG_NOT_ALLOWED: |
| 5027 | case XML_RELAXNG_TEXT: |
| 5028 | case XML_RELAXNG_ELEMENT: |
| 5029 | case XML_RELAXNG_DATATYPE: |
| 5030 | case XML_RELAXNG_VALUE: |
| 5031 | case XML_RELAXNG_LIST: |
| 5032 | case XML_RELAXNG_REF: |
| 5033 | case XML_RELAXNG_PARENTREF: |
| 5034 | case XML_RELAXNG_EXTERNALREF: |
| 5035 | case XML_RELAXNG_DEF: |
| 5036 | case XML_RELAXNG_ONEORMORE: |
| 5037 | case XML_RELAXNG_ZEROORMORE: |
| 5038 | case XML_RELAXNG_OPTIONAL: |
| 5039 | case XML_RELAXNG_CHOICE: |
| 5040 | case XML_RELAXNG_GROUP: |
| 5041 | case XML_RELAXNG_INTERLEAVE: |
| 5042 | case XML_RELAXNG_ATTRIBUTE: |
| 5043 | ret->content = cur; |
| 5044 | cur->parent = ret; |
| 5045 | break; |
| 5046 | case XML_RELAXNG_START: |
| 5047 | case XML_RELAXNG_PARAM: |
| 5048 | case XML_RELAXNG_EXCEPT: |
| 5049 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CONTENT, |
| 5050 | "attribute has invalid content\n", NULL, |
| 5051 | NULL); |
| 5052 | break; |
| 5053 | case XML_RELAXNG_NOOP: |
| 5054 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_NOOP, |
| 5055 | "RNG Internal error, noop found in attribute\n", |
| 5056 | NULL, NULL); |
| 5057 | break; |
| 5058 | } |
| 5059 | } |
| 5060 | child = child->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5061 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5062 | if (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5063 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CHILDREN, |
| 5064 | "attribute has multiple children\n", NULL, NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5065 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5066 | ctxt->flags = old_flags; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5067 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5068 | } |
| 5069 | |
| 5070 | /** |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5071 | * xmlRelaxNGParseExceptNameClass: |
| 5072 | * @ctxt: a Relax-NG parser context |
| 5073 | * @node: the except node |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5074 | * @attr: 1 if within an attribute, 0 if within an element |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5075 | * |
| 5076 | * parse the content of a RelaxNG nameClass node. |
| 5077 | * |
| 5078 | * Returns the definition pointer or NULL in case of error. |
| 5079 | */ |
| 5080 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5081 | xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5082 | xmlNodePtr node, int attr) |
| 5083 | { |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5084 | xmlRelaxNGDefinePtr ret, cur, last = NULL; |
| 5085 | xmlNodePtr child; |
| 5086 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5087 | if (!IS_RELAXNG(node, "except")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5088 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MISSING, |
| 5089 | "Expecting an except node\n", NULL, NULL); |
| 5090 | return (NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5091 | } |
| 5092 | if (node->next != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5093 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MULTIPLE, |
| 5094 | "exceptNameClass allows only a single except node\n", |
| 5095 | NULL, NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5096 | } |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5097 | if (node->children == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5098 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_EMPTY, "except has no content\n", |
| 5099 | NULL, NULL); |
| 5100 | return (NULL); |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5101 | } |
| 5102 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5103 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5104 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5105 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5106 | ret->type = XML_RELAXNG_EXCEPT; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5107 | child = node->children; |
| 5108 | while (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5109 | cur = xmlRelaxNGNewDefine(ctxt, child); |
| 5110 | if (cur == NULL) |
| 5111 | break; |
| 5112 | if (attr) |
| 5113 | cur->type = XML_RELAXNG_ATTRIBUTE; |
| 5114 | else |
| 5115 | cur->type = XML_RELAXNG_ELEMENT; |
| 5116 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 5117 | if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5118 | if (last == NULL) { |
| 5119 | ret->content = cur; |
| 5120 | } else { |
| 5121 | last->next = cur; |
| 5122 | } |
| 5123 | last = cur; |
| 5124 | } |
| 5125 | child = child->next; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5126 | } |
| 5127 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5128 | return (ret); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5129 | } |
| 5130 | |
| 5131 | /** |
| 5132 | * xmlRelaxNGParseNameClass: |
| 5133 | * @ctxt: a Relax-NG parser context |
| 5134 | * @node: the nameClass node |
| 5135 | * @def: the current definition |
| 5136 | * |
| 5137 | * parse the content of a RelaxNG nameClass node. |
| 5138 | * |
| 5139 | * Returns the definition pointer or NULL in case of error. |
| 5140 | */ |
| 5141 | static xmlRelaxNGDefinePtr |
| 5142 | xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5143 | xmlRelaxNGDefinePtr def) |
| 5144 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5145 | xmlRelaxNGDefinePtr ret, tmp; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5146 | xmlChar *val; |
| 5147 | |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5148 | ret = def; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5149 | if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) || |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5150 | (IS_RELAXNG(node, "nsName"))) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5151 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 5152 | (def->type != XML_RELAXNG_ATTRIBUTE)) { |
| 5153 | ret = xmlRelaxNGNewDefine(ctxt, node); |
| 5154 | if (ret == NULL) |
| 5155 | return (NULL); |
| 5156 | ret->parent = def; |
| 5157 | if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) |
| 5158 | ret->type = XML_RELAXNG_ATTRIBUTE; |
| 5159 | else |
| 5160 | ret->type = XML_RELAXNG_ELEMENT; |
| 5161 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5162 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5163 | if (IS_RELAXNG(node, "name")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5164 | val = xmlNodeGetContent(node); |
| 5165 | xmlRelaxNGNormExtSpace(val); |
| 5166 | if (xmlValidateNCName(val, 0)) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5167 | if (node->parent != NULL) |
| 5168 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME, |
| 5169 | "Element %s name '%s' is not an NCName\n", |
| 5170 | node->parent->name, val); |
| 5171 | else |
| 5172 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME, |
| 5173 | "name '%s' is not an NCName\n", |
| 5174 | val, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5175 | } |
| 5176 | ret->name = val; |
| 5177 | val = xmlGetProp(node, BAD_CAST "ns"); |
| 5178 | ret->ns = val; |
| 5179 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5180 | (val != NULL) && |
| 5181 | (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5182 | xmlRngPErr(ctxt, node, XML_RNGP_XML_NS, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5183 | "Attribute with namespace '%s' is not allowed\n", |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5184 | val, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5185 | } |
| 5186 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5187 | (val != NULL) && |
| 5188 | (val[0] == 0) && (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5189 | xmlRngPErr(ctxt, node, XML_RNGP_XMLNS_NAME, |
| 5190 | "Attribute with QName 'xmlns' is not allowed\n", |
| 5191 | val, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5192 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5193 | } else if (IS_RELAXNG(node, "anyName")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5194 | ret->name = NULL; |
| 5195 | ret->ns = NULL; |
| 5196 | if (node->children != NULL) { |
| 5197 | ret->nameClass = |
| 5198 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
| 5199 | (def->type == |
| 5200 | XML_RELAXNG_ATTRIBUTE)); |
| 5201 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5202 | } else if (IS_RELAXNG(node, "nsName")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5203 | ret->name = NULL; |
| 5204 | ret->ns = xmlGetProp(node, BAD_CAST "ns"); |
| 5205 | if (ret->ns == NULL) { |
| 5206 | xmlRngPErr(ctxt, node, XML_RNGP_NSNAME_NO_NS, |
| 5207 | "nsName has no ns attribute\n", NULL, NULL); |
| 5208 | } |
| 5209 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5210 | (ret->ns != NULL) && |
| 5211 | (xmlStrEqual |
| 5212 | (ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
| 5213 | xmlRngPErr(ctxt, node, XML_RNGP_XML_NS, |
| 5214 | "Attribute with namespace '%s' is not allowed\n", |
| 5215 | ret->ns, NULL); |
| 5216 | } |
| 5217 | if (node->children != NULL) { |
| 5218 | ret->nameClass = |
| 5219 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
| 5220 | (def->type == |
| 5221 | XML_RELAXNG_ATTRIBUTE)); |
| 5222 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5223 | } else if (IS_RELAXNG(node, "choice")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5224 | xmlNodePtr child; |
| 5225 | xmlRelaxNGDefinePtr last = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5226 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5227 | ret = xmlRelaxNGNewDefine(ctxt, node); |
| 5228 | if (ret == NULL) |
| 5229 | return (NULL); |
| 5230 | ret->parent = def; |
| 5231 | ret->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5232 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5233 | if (node->children == NULL) { |
| 5234 | xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_EMPTY, |
| 5235 | "Element choice is empty\n", NULL, NULL); |
| 5236 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5237 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5238 | child = node->children; |
| 5239 | while (child != NULL) { |
| 5240 | tmp = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5241 | if (tmp != NULL) { |
| 5242 | if (last == NULL) { |
| 5243 | last = ret->nameClass = tmp; |
| 5244 | } else { |
| 5245 | last->next = tmp; |
| 5246 | last = tmp; |
| 5247 | } |
| 5248 | } |
| 5249 | child = child->next; |
| 5250 | } |
| 5251 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5252 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5253 | xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_CONTENT, |
| 5254 | "expecting name, anyName, nsName or choice : got %s\n", |
| 5255 | node->name, NULL); |
| 5256 | return (NULL); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5257 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5258 | if (ret != def) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5259 | if (def->nameClass == NULL) { |
| 5260 | def->nameClass = ret; |
| 5261 | } else { |
| 5262 | tmp = def->nameClass; |
| 5263 | while (tmp->next != NULL) { |
| 5264 | tmp = tmp->next; |
| 5265 | } |
| 5266 | tmp->next = ret; |
| 5267 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5268 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5269 | return (ret); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5270 | } |
| 5271 | |
| 5272 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5273 | * xmlRelaxNGParseElement: |
| 5274 | * @ctxt: a Relax-NG parser context |
| 5275 | * @node: the element node |
| 5276 | * |
| 5277 | * parse the content of a RelaxNG element node. |
| 5278 | * |
| 5279 | * Returns the definition pointer or NULL in case of error. |
| 5280 | */ |
| 5281 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5282 | xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 5283 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5284 | xmlRelaxNGDefinePtr ret, cur, last; |
| 5285 | xmlNodePtr child; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5286 | const xmlChar *olddefine; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5287 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5288 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5289 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5290 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5291 | ret->type = XML_RELAXNG_ELEMENT; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5292 | ret->parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5293 | child = node->children; |
| 5294 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5295 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_EMPTY, |
| 5296 | "xmlRelaxNGParseElement: element has no children\n", |
| 5297 | NULL, NULL); |
| 5298 | return (ret); |
| 5299 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5300 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5301 | if (cur != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5302 | child = child->next; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5303 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5304 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5305 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NO_CONTENT, |
| 5306 | "xmlRelaxNGParseElement: element has no content\n", |
| 5307 | NULL, NULL); |
| 5308 | return (ret); |
| 5309 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5310 | olddefine = ctxt->define; |
| 5311 | ctxt->define = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5312 | last = NULL; |
| 5313 | while (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5314 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 5315 | if (cur != NULL) { |
| 5316 | cur->parent = ret; |
| 5317 | switch (cur->type) { |
| 5318 | case XML_RELAXNG_EMPTY: |
| 5319 | case XML_RELAXNG_NOT_ALLOWED: |
| 5320 | case XML_RELAXNG_TEXT: |
| 5321 | case XML_RELAXNG_ELEMENT: |
| 5322 | case XML_RELAXNG_DATATYPE: |
| 5323 | case XML_RELAXNG_VALUE: |
| 5324 | case XML_RELAXNG_LIST: |
| 5325 | case XML_RELAXNG_REF: |
| 5326 | case XML_RELAXNG_PARENTREF: |
| 5327 | case XML_RELAXNG_EXTERNALREF: |
| 5328 | case XML_RELAXNG_DEF: |
| 5329 | case XML_RELAXNG_ZEROORMORE: |
| 5330 | case XML_RELAXNG_ONEORMORE: |
| 5331 | case XML_RELAXNG_OPTIONAL: |
| 5332 | case XML_RELAXNG_CHOICE: |
| 5333 | case XML_RELAXNG_GROUP: |
| 5334 | case XML_RELAXNG_INTERLEAVE: |
| 5335 | if (last == NULL) { |
| 5336 | ret->content = last = cur; |
| 5337 | } else { |
| 5338 | if ((last->type == XML_RELAXNG_ELEMENT) && |
| 5339 | (ret->content == last)) { |
| 5340 | ret->content = xmlRelaxNGNewDefine(ctxt, node); |
| 5341 | if (ret->content != NULL) { |
| 5342 | ret->content->type = XML_RELAXNG_GROUP; |
| 5343 | ret->content->content = last; |
| 5344 | } else { |
| 5345 | ret->content = last; |
| 5346 | } |
| 5347 | } |
| 5348 | last->next = cur; |
| 5349 | last = cur; |
| 5350 | } |
| 5351 | break; |
| 5352 | case XML_RELAXNG_ATTRIBUTE: |
| 5353 | cur->next = ret->attrs; |
| 5354 | ret->attrs = cur; |
| 5355 | break; |
| 5356 | case XML_RELAXNG_START: |
| 5357 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5358 | "RNG Internal error, start found in element\n", |
| 5359 | NULL, NULL); |
| 5360 | break; |
| 5361 | case XML_RELAXNG_PARAM: |
| 5362 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5363 | "RNG Internal error, param found in element\n", |
| 5364 | NULL, NULL); |
| 5365 | break; |
| 5366 | case XML_RELAXNG_EXCEPT: |
| 5367 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5368 | "RNG Internal error, except found in element\n", |
| 5369 | NULL, NULL); |
| 5370 | break; |
| 5371 | case XML_RELAXNG_NOOP: |
| 5372 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5373 | "RNG Internal error, noop found in element\n", |
| 5374 | NULL, NULL); |
| 5375 | break; |
| 5376 | } |
| 5377 | } |
| 5378 | child = child->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5379 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5380 | ctxt->define = olddefine; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5381 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5382 | } |
| 5383 | |
| 5384 | /** |
| 5385 | * xmlRelaxNGParsePatterns: |
| 5386 | * @ctxt: a Relax-NG parser context |
| 5387 | * @nodes: list of nodes |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5388 | * @group: use an implicit <group> for elements |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5389 | * |
| 5390 | * parse the content of a RelaxNG start node. |
| 5391 | * |
| 5392 | * Returns the definition pointer or NULL in case of error. |
| 5393 | */ |
| 5394 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5395 | xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5396 | int group) |
| 5397 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5398 | xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5399 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5400 | parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5401 | while (nodes != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5402 | if (IS_RELAXNG(nodes, "element")) { |
| 5403 | cur = xmlRelaxNGParseElement(ctxt, nodes); |
| 5404 | if (def == NULL) { |
| 5405 | def = last = cur; |
| 5406 | } else { |
| 5407 | if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) && |
| 5408 | (def == last)) { |
| 5409 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
| 5410 | def->type = XML_RELAXNG_GROUP; |
| 5411 | def->content = last; |
| 5412 | } |
| 5413 | last->next = cur; |
| 5414 | last = cur; |
| 5415 | } |
| 5416 | cur->parent = parent; |
| 5417 | } else { |
| 5418 | cur = xmlRelaxNGParsePattern(ctxt, nodes); |
| 5419 | if (cur != NULL) { |
| 5420 | if (def == NULL) { |
| 5421 | def = last = cur; |
| 5422 | } else { |
| 5423 | last->next = cur; |
| 5424 | last = cur; |
| 5425 | } |
| 5426 | } |
| 5427 | } |
| 5428 | nodes = nodes->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5429 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5430 | return (def); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5431 | } |
| 5432 | |
| 5433 | /** |
| 5434 | * xmlRelaxNGParseStart: |
| 5435 | * @ctxt: a Relax-NG parser context |
| 5436 | * @nodes: start children nodes |
| 5437 | * |
| 5438 | * parse the content of a RelaxNG start node. |
| 5439 | * |
| 5440 | * Returns 0 in case of success, -1 in case of error |
| 5441 | */ |
| 5442 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5443 | xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) |
| 5444 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5445 | int ret = 0; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5446 | xmlRelaxNGDefinePtr def = NULL, last; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5447 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5448 | if (nodes == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5449 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, "start has no children\n", |
| 5450 | NULL, NULL); |
| 5451 | return (-1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5452 | } |
| 5453 | if (IS_RELAXNG(nodes, "empty")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5454 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
| 5455 | if (def == NULL) |
| 5456 | return (-1); |
| 5457 | def->type = XML_RELAXNG_EMPTY; |
| 5458 | if (nodes->children != NULL) { |
| 5459 | xmlRngPErr(ctxt, nodes, XML_RNGP_EMPTY_CONTENT, |
| 5460 | "element empty is not empty\n", NULL, NULL); |
| 5461 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5462 | } else if (IS_RELAXNG(nodes, "notAllowed")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5463 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
| 5464 | if (def == NULL) |
| 5465 | return (-1); |
| 5466 | def->type = XML_RELAXNG_NOT_ALLOWED; |
| 5467 | if (nodes->children != NULL) { |
| 5468 | xmlRngPErr(ctxt, nodes, XML_RNGP_NOTALLOWED_NOT_EMPTY, |
| 5469 | "element notAllowed is not empty\n", NULL, NULL); |
| 5470 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5471 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5472 | def = xmlRelaxNGParsePatterns(ctxt, nodes, 1); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5473 | } |
| 5474 | if (ctxt->grammar->start != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5475 | last = ctxt->grammar->start; |
| 5476 | while (last->next != NULL) |
| 5477 | last = last->next; |
| 5478 | last->next = def; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5479 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5480 | ctxt->grammar->start = def; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5481 | } |
| 5482 | nodes = nodes->next; |
| 5483 | if (nodes != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5484 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_CONTENT, |
| 5485 | "start more than one children\n", NULL, NULL); |
| 5486 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5487 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5488 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5489 | } |
| 5490 | |
| 5491 | /** |
| 5492 | * xmlRelaxNGParseGrammarContent: |
| 5493 | * @ctxt: a Relax-NG parser context |
| 5494 | * @nodes: grammar children nodes |
| 5495 | * |
| 5496 | * parse the content of a RelaxNG grammar node. |
| 5497 | * |
| 5498 | * Returns 0 in case of success, -1 in case of error |
| 5499 | */ |
| 5500 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5501 | xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt, |
| 5502 | xmlNodePtr nodes) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5503 | { |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5504 | int ret = 0, tmp; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5505 | |
| 5506 | if (nodes == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5507 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_EMPTY, |
| 5508 | "grammar has no children\n", NULL, NULL); |
| 5509 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5510 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5511 | while (nodes != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5512 | if (IS_RELAXNG(nodes, "start")) { |
| 5513 | if (nodes->children == NULL) { |
| 5514 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, |
| 5515 | "start has no children\n", NULL, NULL); |
| 5516 | } else { |
| 5517 | tmp = xmlRelaxNGParseStart(ctxt, nodes->children); |
| 5518 | if (tmp != 0) |
| 5519 | ret = -1; |
| 5520 | } |
| 5521 | } else if (IS_RELAXNG(nodes, "define")) { |
| 5522 | tmp = xmlRelaxNGParseDefine(ctxt, nodes); |
| 5523 | if (tmp != 0) |
| 5524 | ret = -1; |
| 5525 | } else if (IS_RELAXNG(nodes, "include")) { |
| 5526 | tmp = xmlRelaxNGParseInclude(ctxt, nodes); |
| 5527 | if (tmp != 0) |
| 5528 | ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5529 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5530 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT, |
| 5531 | "grammar has unexpected child %s\n", nodes->name, |
| 5532 | NULL); |
| 5533 | ret = -1; |
| 5534 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5535 | nodes = nodes->next; |
| 5536 | } |
| 5537 | return (ret); |
| 5538 | } |
| 5539 | |
| 5540 | /** |
| 5541 | * xmlRelaxNGCheckReference: |
| 5542 | * @ref: the ref |
| 5543 | * @ctxt: a Relax-NG parser context |
| 5544 | * @name: the name associated to the defines |
| 5545 | * |
| 5546 | * Applies the 4.17. combine attribute rule for all the define |
| 5547 | * element of a given grammar using the same name. |
| 5548 | */ |
| 5549 | static void |
| 5550 | xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5551 | xmlRelaxNGParserCtxtPtr ctxt, |
| 5552 | const xmlChar * name) |
| 5553 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5554 | xmlRelaxNGGrammarPtr grammar; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5555 | xmlRelaxNGDefinePtr def, cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5556 | |
| 5557 | grammar = ctxt->grammar; |
| 5558 | if (grammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5559 | xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR, |
| 5560 | "Internal error: no grammar in CheckReference %s\n", |
| 5561 | name, NULL); |
| 5562 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5563 | } |
| 5564 | if (ref->content != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5565 | xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR, |
| 5566 | "Internal error: reference has content in CheckReference %s\n", |
| 5567 | name, NULL); |
| 5568 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5569 | } |
| 5570 | if (grammar->defs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5571 | def = xmlHashLookup(grammar->defs, name); |
| 5572 | if (def != NULL) { |
| 5573 | cur = ref; |
| 5574 | while (cur != NULL) { |
| 5575 | cur->content = def; |
| 5576 | cur = cur->nextHash; |
| 5577 | } |
| 5578 | } else { |
| 5579 | xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF, |
| 5580 | "Reference %s has no matching definition\n", name, |
| 5581 | NULL); |
| 5582 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5583 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5584 | xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF, |
| 5585 | "Reference %s has no matching definition\n", name, |
| 5586 | NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5587 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5588 | } |
| 5589 | |
| 5590 | /** |
| 5591 | * xmlRelaxNGCheckCombine: |
| 5592 | * @define: the define(s) list |
| 5593 | * @ctxt: a Relax-NG parser context |
| 5594 | * @name: the name associated to the defines |
| 5595 | * |
| 5596 | * Applies the 4.17. combine attribute rule for all the define |
| 5597 | * element of a given grammar using the same name. |
| 5598 | */ |
| 5599 | static void |
| 5600 | xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5601 | xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * name) |
| 5602 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5603 | xmlChar *combine; |
| 5604 | int choiceOrInterleave = -1; |
| 5605 | int missing = 0; |
| 5606 | xmlRelaxNGDefinePtr cur, last, tmp, tmp2; |
| 5607 | |
| 5608 | if (define->nextHash == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5609 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5610 | cur = define; |
| 5611 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5612 | combine = xmlGetProp(cur->node, BAD_CAST "combine"); |
| 5613 | if (combine != NULL) { |
| 5614 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
| 5615 | if (choiceOrInterleave == -1) |
| 5616 | choiceOrInterleave = 1; |
| 5617 | else if (choiceOrInterleave == 0) { |
| 5618 | xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE, |
| 5619 | "Defines for %s use both 'choice' and 'interleave'\n", |
| 5620 | name, NULL); |
| 5621 | } |
| 5622 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
| 5623 | if (choiceOrInterleave == -1) |
| 5624 | choiceOrInterleave = 0; |
| 5625 | else if (choiceOrInterleave == 1) { |
| 5626 | xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE, |
| 5627 | "Defines for %s use both 'choice' and 'interleave'\n", |
| 5628 | name, NULL); |
| 5629 | } |
| 5630 | } else { |
| 5631 | xmlRngPErr(ctxt, define->node, XML_RNGP_UNKNOWN_COMBINE, |
| 5632 | "Defines for %s use unknown combine value '%s''\n", |
| 5633 | name, combine); |
| 5634 | } |
| 5635 | xmlFree(combine); |
| 5636 | } else { |
| 5637 | if (missing == 0) |
| 5638 | missing = 1; |
| 5639 | else { |
| 5640 | xmlRngPErr(ctxt, define->node, XML_RNGP_NEED_COMBINE, |
| 5641 | "Some defines for %s needs the combine attribute\n", |
| 5642 | name, NULL); |
| 5643 | } |
| 5644 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5645 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5646 | cur = cur->nextHash; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5647 | } |
| 5648 | #ifdef DEBUG |
| 5649 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5650 | "xmlRelaxNGCheckCombine(): merging %s defines: %d\n", |
| 5651 | name, choiceOrInterleave); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5652 | #endif |
| 5653 | if (choiceOrInterleave == -1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5654 | choiceOrInterleave = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5655 | cur = xmlRelaxNGNewDefine(ctxt, define->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5656 | if (cur == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5657 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5658 | if (choiceOrInterleave == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5659 | cur->type = XML_RELAXNG_INTERLEAVE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5660 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5661 | cur->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5662 | tmp = define; |
| 5663 | last = NULL; |
| 5664 | while (tmp != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5665 | if (tmp->content != NULL) { |
| 5666 | if (tmp->content->next != NULL) { |
| 5667 | /* |
| 5668 | * we need first to create a wrapper. |
| 5669 | */ |
| 5670 | tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node); |
| 5671 | if (tmp2 == NULL) |
| 5672 | break; |
| 5673 | tmp2->type = XML_RELAXNG_GROUP; |
| 5674 | tmp2->content = tmp->content; |
| 5675 | } else { |
| 5676 | tmp2 = tmp->content; |
| 5677 | } |
| 5678 | if (last == NULL) { |
| 5679 | cur->content = tmp2; |
| 5680 | } else { |
| 5681 | last->next = tmp2; |
| 5682 | } |
| 5683 | last = tmp2; |
| 5684 | } |
| 5685 | tmp->content = cur; |
| 5686 | tmp = tmp->nextHash; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5687 | } |
| 5688 | define->content = cur; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5689 | if (choiceOrInterleave == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5690 | if (ctxt->interleaves == NULL) |
| 5691 | ctxt->interleaves = xmlHashCreate(10); |
| 5692 | if (ctxt->interleaves == NULL) { |
| 5693 | xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5694 | "Failed to create interleaves hash table\n", NULL, |
| 5695 | NULL); |
| 5696 | } else { |
| 5697 | char tmpname[32]; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5698 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5699 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
| 5700 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < |
| 5701 | 0) { |
| 5702 | xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5703 | "Failed to add %s to hash table\n", |
| 5704 | (const xmlChar *) tmpname, NULL); |
| 5705 | } |
| 5706 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5707 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5708 | } |
| 5709 | |
| 5710 | /** |
| 5711 | * xmlRelaxNGCombineStart: |
| 5712 | * @ctxt: a Relax-NG parser context |
| 5713 | * @grammar: the grammar |
| 5714 | * |
| 5715 | * Applies the 4.17. combine rule for all the start |
| 5716 | * element of a given grammar. |
| 5717 | */ |
| 5718 | static void |
| 5719 | xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5720 | xmlRelaxNGGrammarPtr grammar) |
| 5721 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5722 | xmlRelaxNGDefinePtr starts; |
| 5723 | xmlChar *combine; |
| 5724 | int choiceOrInterleave = -1; |
| 5725 | int missing = 0; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5726 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5727 | |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5728 | starts = grammar->start; |
| 5729 | if ((starts == NULL) || (starts->next == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5730 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5731 | cur = starts; |
| 5732 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5733 | if ((cur->node == NULL) || (cur->node->parent == NULL) || |
| 5734 | (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) { |
| 5735 | combine = NULL; |
| 5736 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_MISSING, |
| 5737 | "Internal error: start element not found\n", NULL, |
| 5738 | NULL); |
| 5739 | } else { |
| 5740 | combine = xmlGetProp(cur->node->parent, BAD_CAST "combine"); |
| 5741 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5742 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5743 | if (combine != NULL) { |
| 5744 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
| 5745 | if (choiceOrInterleave == -1) |
| 5746 | choiceOrInterleave = 1; |
| 5747 | else if (choiceOrInterleave == 0) { |
| 5748 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE, |
| 5749 | "<start> use both 'choice' and 'interleave'\n", |
| 5750 | NULL, NULL); |
| 5751 | } |
| 5752 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
| 5753 | if (choiceOrInterleave == -1) |
| 5754 | choiceOrInterleave = 0; |
| 5755 | else if (choiceOrInterleave == 1) { |
| 5756 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE, |
| 5757 | "<start> use both 'choice' and 'interleave'\n", |
| 5758 | NULL, NULL); |
| 5759 | } |
| 5760 | } else { |
| 5761 | xmlRngPErr(ctxt, cur->node, XML_RNGP_UNKNOWN_COMBINE, |
| 5762 | "<start> uses unknown combine value '%s''\n", |
| 5763 | combine, NULL); |
| 5764 | } |
| 5765 | xmlFree(combine); |
| 5766 | } else { |
| 5767 | if (missing == 0) |
| 5768 | missing = 1; |
| 5769 | else { |
| 5770 | xmlRngPErr(ctxt, cur->node, XML_RNGP_NEED_COMBINE, |
| 5771 | "Some <start> element miss the combine attribute\n", |
| 5772 | NULL, NULL); |
| 5773 | } |
| 5774 | } |
| 5775 | |
| 5776 | cur = cur->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5777 | } |
| 5778 | #ifdef DEBUG |
| 5779 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5780 | "xmlRelaxNGCombineStart(): merging <start>: %d\n", |
| 5781 | choiceOrInterleave); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5782 | #endif |
| 5783 | if (choiceOrInterleave == -1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5784 | choiceOrInterleave = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5785 | cur = xmlRelaxNGNewDefine(ctxt, starts->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5786 | if (cur == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5787 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5788 | if (choiceOrInterleave == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5789 | cur->type = XML_RELAXNG_INTERLEAVE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5790 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5791 | cur->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5792 | cur->content = grammar->start; |
| 5793 | grammar->start = cur; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5794 | if (choiceOrInterleave == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5795 | if (ctxt->interleaves == NULL) |
| 5796 | ctxt->interleaves = xmlHashCreate(10); |
| 5797 | if (ctxt->interleaves == NULL) { |
| 5798 | xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5799 | "Failed to create interleaves hash table\n", NULL, |
| 5800 | NULL); |
| 5801 | } else { |
| 5802 | char tmpname[32]; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5803 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5804 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
| 5805 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < |
| 5806 | 0) { |
| 5807 | xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5808 | "Failed to add %s to hash table\n", |
| 5809 | (const xmlChar *) tmpname, NULL); |
| 5810 | } |
| 5811 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5812 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5813 | } |
| 5814 | |
| 5815 | /** |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5816 | * xmlRelaxNGCheckCycles: |
| 5817 | * @ctxt: a Relax-NG parser context |
| 5818 | * @nodes: grammar children nodes |
| 5819 | * @depth: the counter |
| 5820 | * |
| 5821 | * Check for cycles. |
| 5822 | * |
| 5823 | * Returns 0 if check passed, and -1 in case of error |
| 5824 | */ |
| 5825 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5826 | xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt, |
| 5827 | xmlRelaxNGDefinePtr cur, int depth) |
| 5828 | { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5829 | int ret = 0; |
| 5830 | |
| 5831 | while ((ret == 0) && (cur != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5832 | if ((cur->type == XML_RELAXNG_REF) || |
| 5833 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5834 | if (cur->depth == -1) { |
| 5835 | cur->depth = depth; |
| 5836 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
| 5837 | cur->depth = -2; |
| 5838 | } else if (depth == cur->depth) { |
| 5839 | xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_CYCLE, |
| 5840 | "Detected a cycle in %s references\n", |
| 5841 | cur->name, NULL); |
| 5842 | return (-1); |
| 5843 | } |
| 5844 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
| 5845 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1); |
| 5846 | } else { |
| 5847 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
| 5848 | } |
| 5849 | cur = cur->next; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5850 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5851 | return (ret); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5852 | } |
| 5853 | |
| 5854 | /** |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5855 | * xmlRelaxNGTryUnlink: |
| 5856 | * @ctxt: a Relax-NG parser context |
| 5857 | * @cur: the definition to unlink |
| 5858 | * @parent: the parent definition |
| 5859 | * @prev: the previous sibling definition |
| 5860 | * |
| 5861 | * Try to unlink a definition. If not possble make it a NOOP |
| 5862 | * |
| 5863 | * Returns the new prev definition |
| 5864 | */ |
| 5865 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5866 | xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 5867 | xmlRelaxNGDefinePtr cur, |
| 5868 | xmlRelaxNGDefinePtr parent, xmlRelaxNGDefinePtr prev) |
| 5869 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5870 | if (prev != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5871 | prev->next = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5872 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5873 | if (parent != NULL) { |
| 5874 | if (parent->content == cur) |
| 5875 | parent->content = cur->next; |
| 5876 | else if (parent->attrs == cur) |
| 5877 | parent->attrs = cur->next; |
| 5878 | else if (parent->nameClass == cur) |
| 5879 | parent->nameClass = cur->next; |
| 5880 | } else { |
| 5881 | cur->type = XML_RELAXNG_NOOP; |
| 5882 | prev = cur; |
| 5883 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5884 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5885 | return (prev); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5886 | } |
| 5887 | |
| 5888 | /** |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5889 | * xmlRelaxNGSimplify: |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5890 | * @ctxt: a Relax-NG parser context |
| 5891 | * @nodes: grammar children nodes |
| 5892 | * |
| 5893 | * Check for simplification of empty and notAllowed |
| 5894 | */ |
| 5895 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5896 | xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt, |
| 5897 | xmlRelaxNGDefinePtr cur, xmlRelaxNGDefinePtr parent) |
| 5898 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5899 | xmlRelaxNGDefinePtr prev = NULL; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5900 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5901 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5902 | if ((cur->type == XML_RELAXNG_REF) || |
| 5903 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5904 | if (cur->depth != -3) { |
| 5905 | cur->depth = -3; |
| 5906 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
| 5907 | } |
| 5908 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
| 5909 | cur->parent = parent; |
| 5910 | if ((parent != NULL) && |
| 5911 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
| 5912 | (parent->type == XML_RELAXNG_LIST) || |
| 5913 | (parent->type == XML_RELAXNG_GROUP) || |
| 5914 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 5915 | (parent->type == XML_RELAXNG_ONEORMORE) || |
| 5916 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 5917 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
| 5918 | break; |
| 5919 | } |
| 5920 | if ((parent != NULL) && (parent->type == XML_RELAXNG_CHOICE)) { |
| 5921 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5922 | } else |
| 5923 | prev = cur; |
| 5924 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 5925 | cur->parent = parent; |
| 5926 | if ((parent != NULL) && |
| 5927 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
| 5928 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 5929 | parent->type = XML_RELAXNG_EMPTY; |
| 5930 | break; |
| 5931 | } |
| 5932 | if ((parent != NULL) && |
| 5933 | ((parent->type == XML_RELAXNG_GROUP) || |
| 5934 | (parent->type == XML_RELAXNG_INTERLEAVE))) { |
| 5935 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5936 | } else |
| 5937 | prev = cur; |
| 5938 | } else { |
| 5939 | cur->parent = parent; |
| 5940 | if (cur->content != NULL) |
| 5941 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
| 5942 | if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL)) |
| 5943 | xmlRelaxNGSimplify(ctxt, cur->attrs, cur); |
| 5944 | if (cur->nameClass != NULL) |
| 5945 | xmlRelaxNGSimplify(ctxt, cur->nameClass, cur); |
| 5946 | /* |
| 5947 | * On Elements, try to move attribute only generating rules on |
| 5948 | * the attrs rules. |
| 5949 | */ |
| 5950 | if (cur->type == XML_RELAXNG_ELEMENT) { |
| 5951 | int attronly; |
| 5952 | xmlRelaxNGDefinePtr tmp, pre; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 5953 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5954 | while (cur->content != NULL) { |
| 5955 | attronly = |
| 5956 | xmlRelaxNGGenerateAttributes(ctxt, cur->content); |
| 5957 | if (attronly == 1) { |
| 5958 | /* |
| 5959 | * migrate cur->content to attrs |
| 5960 | */ |
| 5961 | tmp = cur->content; |
| 5962 | cur->content = tmp->next; |
| 5963 | tmp->next = cur->attrs; |
| 5964 | cur->attrs = tmp; |
| 5965 | } else { |
| 5966 | /* |
| 5967 | * cur->content can generate elements or text |
| 5968 | */ |
| 5969 | break; |
| 5970 | } |
| 5971 | } |
| 5972 | pre = cur->content; |
| 5973 | while ((pre != NULL) && (pre->next != NULL)) { |
| 5974 | tmp = pre->next; |
| 5975 | attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp); |
| 5976 | if (attronly == 1) { |
| 5977 | /* |
| 5978 | * migrate tmp to attrs |
| 5979 | */ |
| 5980 | pre->next = tmp->next; |
| 5981 | tmp->next = cur->attrs; |
| 5982 | cur->attrs = tmp; |
| 5983 | } else { |
| 5984 | pre = tmp; |
| 5985 | } |
| 5986 | } |
| 5987 | } |
| 5988 | /* |
| 5989 | * This may result in a simplification |
| 5990 | */ |
| 5991 | if ((cur->type == XML_RELAXNG_GROUP) || |
| 5992 | (cur->type == XML_RELAXNG_INTERLEAVE)) { |
| 5993 | if (cur->content == NULL) |
| 5994 | cur->type = XML_RELAXNG_EMPTY; |
| 5995 | else if (cur->content->next == NULL) { |
| 5996 | if ((parent == NULL) && (prev == NULL)) { |
| 5997 | cur->type = XML_RELAXNG_NOOP; |
| 5998 | } else if (prev == NULL) { |
| 5999 | parent->content = cur->content; |
| 6000 | cur->content->next = cur->next; |
| 6001 | cur = cur->content; |
| 6002 | } else { |
| 6003 | cur->content->next = cur->next; |
| 6004 | prev->next = cur->content; |
| 6005 | cur = cur->content; |
| 6006 | } |
| 6007 | } |
| 6008 | } |
| 6009 | /* |
| 6010 | * the current node may have been transformed back |
| 6011 | */ |
| 6012 | if ((cur->type == XML_RELAXNG_EXCEPT) && |
| 6013 | (cur->content != NULL) && |
| 6014 | (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) { |
| 6015 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 6016 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
| 6017 | if ((parent != NULL) && |
| 6018 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
| 6019 | (parent->type == XML_RELAXNG_LIST) || |
| 6020 | (parent->type == XML_RELAXNG_GROUP) || |
| 6021 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 6022 | (parent->type == XML_RELAXNG_ONEORMORE) || |
| 6023 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 6024 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
| 6025 | break; |
| 6026 | } |
| 6027 | if ((parent != NULL) && |
| 6028 | (parent->type == XML_RELAXNG_CHOICE)) { |
| 6029 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 6030 | } else |
| 6031 | prev = cur; |
| 6032 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 6033 | if ((parent != NULL) && |
| 6034 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
| 6035 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 6036 | parent->type = XML_RELAXNG_EMPTY; |
| 6037 | break; |
| 6038 | } |
| 6039 | if ((parent != NULL) && |
| 6040 | ((parent->type == XML_RELAXNG_GROUP) || |
| 6041 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 6042 | (parent->type == XML_RELAXNG_CHOICE))) { |
| 6043 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 6044 | } else |
| 6045 | prev = cur; |
| 6046 | } else { |
| 6047 | prev = cur; |
| 6048 | } |
| 6049 | } |
| 6050 | cur = cur->next; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6051 | } |
| 6052 | } |
| 6053 | |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6054 | /** |
| 6055 | * xmlRelaxNGGroupContentType: |
| 6056 | * @ct1: the first content type |
| 6057 | * @ct2: the second content type |
| 6058 | * |
| 6059 | * Try to group 2 content types |
| 6060 | * |
| 6061 | * Returns the content type |
| 6062 | */ |
| 6063 | static xmlRelaxNGContentType |
| 6064 | xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6065 | xmlRelaxNGContentType ct2) |
| 6066 | { |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6067 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6068 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
| 6069 | return (XML_RELAXNG_CONTENT_ERROR); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6070 | if (ct1 == XML_RELAXNG_CONTENT_EMPTY) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6071 | return (ct2); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6072 | if (ct2 == XML_RELAXNG_CONTENT_EMPTY) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6073 | return (ct1); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6074 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6075 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
| 6076 | return (XML_RELAXNG_CONTENT_COMPLEX); |
| 6077 | return (XML_RELAXNG_CONTENT_ERROR); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6078 | } |
| 6079 | |
| 6080 | /** |
| 6081 | * xmlRelaxNGMaxContentType: |
| 6082 | * @ct1: the first content type |
| 6083 | * @ct2: the second content type |
| 6084 | * |
| 6085 | * Compute the max content-type |
| 6086 | * |
| 6087 | * Returns the content type |
| 6088 | */ |
| 6089 | static xmlRelaxNGContentType |
| 6090 | xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6091 | xmlRelaxNGContentType ct2) |
| 6092 | { |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6093 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6094 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
| 6095 | return (XML_RELAXNG_CONTENT_ERROR); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6096 | if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6097 | (ct2 == XML_RELAXNG_CONTENT_SIMPLE)) |
| 6098 | return (XML_RELAXNG_CONTENT_SIMPLE); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6099 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6100 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
| 6101 | return (XML_RELAXNG_CONTENT_COMPLEX); |
| 6102 | return (XML_RELAXNG_CONTENT_EMPTY); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6103 | } |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6104 | |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6105 | /** |
| 6106 | * xmlRelaxNGCheckRules: |
| 6107 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6108 | * @cur: the current definition |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6109 | * @flags: some accumulated flags |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6110 | * @ptype: the parent type |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6111 | * |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6112 | * Check for rules in section 7.1 and 7.2 |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6113 | * |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6114 | * Returns the content type of @cur |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6115 | */ |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6116 | static xmlRelaxNGContentType |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6117 | xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt, |
| 6118 | xmlRelaxNGDefinePtr cur, int flags, |
| 6119 | xmlRelaxNGType ptype) |
| 6120 | { |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6121 | int nflags = flags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6122 | xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6123 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6124 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6125 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6126 | if ((cur->type == XML_RELAXNG_REF) || |
| 6127 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 6128 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6129 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_REF, |
| 6130 | "Found forbidden pattern list//ref\n", NULL, |
| 6131 | NULL); |
| 6132 | } |
| 6133 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6134 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_REF, |
| 6135 | "Found forbidden pattern data/except//ref\n", |
| 6136 | NULL, NULL); |
| 6137 | } |
| 6138 | if (cur->depth > -4) { |
| 6139 | cur->depth = -4; |
| 6140 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, |
| 6141 | flags, cur->type); |
| 6142 | cur->depth = ret - 15; |
| 6143 | } else if (cur->depth == -4) { |
| 6144 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6145 | } else { |
| 6146 | ret = (xmlRelaxNGContentType) (cur->depth + 15); |
| 6147 | } |
| 6148 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
| 6149 | /* |
| 6150 | * The 7.3 Attribute derivation rule for groups is plugged there |
| 6151 | */ |
| 6152 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
| 6153 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6154 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ELEM, |
| 6155 | "Found forbidden pattern data/except//element(ref)\n", |
| 6156 | NULL, NULL); |
| 6157 | } |
| 6158 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6159 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ELEM, |
| 6160 | "Found forbidden pattern list//element(ref)\n", |
| 6161 | NULL, NULL); |
| 6162 | } |
| 6163 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6164 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM, |
| 6165 | "Found forbidden pattern attribute//element(ref)\n", |
| 6166 | NULL, NULL); |
| 6167 | } |
| 6168 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6169 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM, |
| 6170 | "Found forbidden pattern attribute//element(ref)\n", |
| 6171 | NULL, NULL); |
| 6172 | } |
| 6173 | /* |
| 6174 | * reset since in the simple form elements are only child |
| 6175 | * of grammar/define |
| 6176 | */ |
| 6177 | nflags = 0; |
| 6178 | ret = |
| 6179 | xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type); |
| 6180 | if (ret != XML_RELAXNG_CONTENT_EMPTY) { |
| 6181 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_EMPTY, |
| 6182 | "Element %s attributes have a content type error\n", |
| 6183 | cur->name, NULL); |
| 6184 | } |
| 6185 | ret = |
| 6186 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6187 | cur->type); |
| 6188 | if (ret == XML_RELAXNG_CONTENT_ERROR) { |
| 6189 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_ERROR, |
| 6190 | "Element %s has a content type error\n", |
| 6191 | cur->name, NULL); |
| 6192 | } else { |
| 6193 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6194 | } |
| 6195 | } else if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
| 6196 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6197 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ATTR, |
| 6198 | "Found forbidden pattern attribute//attribute\n", |
| 6199 | NULL, NULL); |
| 6200 | } |
| 6201 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6202 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ATTR, |
| 6203 | "Found forbidden pattern list//attribute\n", |
| 6204 | NULL, NULL); |
| 6205 | } |
| 6206 | if (flags & XML_RELAXNG_IN_OOMGROUP) { |
| 6207 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_GROUP_ATTR, |
| 6208 | "Found forbidden pattern oneOrMore//group//attribute\n", |
| 6209 | NULL, NULL); |
| 6210 | } |
| 6211 | if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) { |
| 6212 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_INTERLEAVE_ATTR, |
| 6213 | "Found forbidden pattern oneOrMore//interleave//attribute\n", |
| 6214 | NULL, NULL); |
| 6215 | } |
| 6216 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6217 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ATTR, |
| 6218 | "Found forbidden pattern data/except//attribute\n", |
| 6219 | NULL, NULL); |
| 6220 | } |
| 6221 | if (flags & XML_RELAXNG_IN_START) { |
| 6222 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ATTR, |
| 6223 | "Found forbidden pattern start//attribute\n", |
| 6224 | NULL, NULL); |
| 6225 | } |
| 6226 | if ((!(flags & XML_RELAXNG_IN_ONEORMORE)) |
| 6227 | && (cur->name == NULL)) { |
| 6228 | if (cur->ns == NULL) { |
| 6229 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ANYNAME_ATTR_ANCESTOR, |
| 6230 | "Found anyName attribute without oneOrMore ancestor\n", |
| 6231 | NULL, NULL); |
| 6232 | } else { |
| 6233 | xmlRngPErr(ctxt, cur->node, XML_RNGP_NSNAME_ATTR_ANCESTOR, |
| 6234 | "Found nsName attribute without oneOrMore ancestor\n", |
| 6235 | NULL, NULL); |
| 6236 | } |
| 6237 | } |
| 6238 | nflags = flags | XML_RELAXNG_IN_ATTRIBUTE; |
| 6239 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6240 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6241 | } else if ((cur->type == XML_RELAXNG_ONEORMORE) || |
| 6242 | (cur->type == XML_RELAXNG_ZEROORMORE)) { |
| 6243 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6244 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ONEMORE, |
| 6245 | "Found forbidden pattern data/except//oneOrMore\n", |
| 6246 | NULL, NULL); |
| 6247 | } |
| 6248 | if (flags & XML_RELAXNG_IN_START) { |
| 6249 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ONEMORE, |
| 6250 | "Found forbidden pattern start//oneOrMore\n", |
| 6251 | NULL, NULL); |
| 6252 | } |
| 6253 | nflags = flags | XML_RELAXNG_IN_ONEORMORE; |
| 6254 | ret = |
| 6255 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6256 | cur->type); |
| 6257 | ret = xmlRelaxNGGroupContentType(ret, ret); |
| 6258 | } else if (cur->type == XML_RELAXNG_LIST) { |
| 6259 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6260 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_LIST, |
| 6261 | "Found forbidden pattern list//list\n", NULL, |
| 6262 | NULL); |
| 6263 | } |
| 6264 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6265 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_LIST, |
| 6266 | "Found forbidden pattern data/except//list\n", |
| 6267 | NULL, NULL); |
| 6268 | } |
| 6269 | if (flags & XML_RELAXNG_IN_START) { |
| 6270 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_LIST, |
| 6271 | "Found forbidden pattern start//list\n", NULL, |
| 6272 | NULL); |
| 6273 | } |
| 6274 | nflags = flags | XML_RELAXNG_IN_LIST; |
| 6275 | ret = |
| 6276 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6277 | cur->type); |
| 6278 | } else if (cur->type == XML_RELAXNG_GROUP) { |
| 6279 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6280 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_GROUP, |
| 6281 | "Found forbidden pattern data/except//group\n", |
| 6282 | NULL, NULL); |
| 6283 | } |
| 6284 | if (flags & XML_RELAXNG_IN_START) { |
| 6285 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_GROUP, |
| 6286 | "Found forbidden pattern start//group\n", NULL, |
| 6287 | NULL); |
| 6288 | } |
| 6289 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
| 6290 | nflags = flags | XML_RELAXNG_IN_OOMGROUP; |
| 6291 | else |
| 6292 | nflags = flags; |
| 6293 | ret = |
| 6294 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6295 | cur->type); |
| 6296 | /* |
| 6297 | * The 7.3 Attribute derivation rule for groups is plugged there |
| 6298 | */ |
| 6299 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
| 6300 | } else if (cur->type == XML_RELAXNG_INTERLEAVE) { |
| 6301 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6302 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_INTERLEAVE, |
| 6303 | "Found forbidden pattern list//interleave\n", |
| 6304 | NULL, NULL); |
| 6305 | } |
| 6306 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6307 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE, |
| 6308 | "Found forbidden pattern data/except//interleave\n", |
| 6309 | NULL, NULL); |
| 6310 | } |
| 6311 | if (flags & XML_RELAXNG_IN_START) { |
| 6312 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE, |
| 6313 | "Found forbidden pattern start//interleave\n", |
| 6314 | NULL, NULL); |
| 6315 | } |
| 6316 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
| 6317 | nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE; |
| 6318 | else |
| 6319 | nflags = flags; |
| 6320 | ret = |
| 6321 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6322 | cur->type); |
| 6323 | } else if (cur->type == XML_RELAXNG_EXCEPT) { |
| 6324 | if ((cur->parent != NULL) && |
| 6325 | (cur->parent->type == XML_RELAXNG_DATATYPE)) |
| 6326 | nflags = flags | XML_RELAXNG_IN_DATAEXCEPT; |
| 6327 | else |
| 6328 | nflags = flags; |
| 6329 | ret = |
| 6330 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6331 | cur->type); |
| 6332 | } else if (cur->type == XML_RELAXNG_DATATYPE) { |
| 6333 | if (flags & XML_RELAXNG_IN_START) { |
| 6334 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_DATA, |
| 6335 | "Found forbidden pattern start//data\n", NULL, |
| 6336 | NULL); |
| 6337 | } |
| 6338 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6339 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
| 6340 | } else if (cur->type == XML_RELAXNG_VALUE) { |
| 6341 | if (flags & XML_RELAXNG_IN_START) { |
| 6342 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_VALUE, |
| 6343 | "Found forbidden pattern start//value\n", NULL, |
| 6344 | NULL); |
| 6345 | } |
| 6346 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6347 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
| 6348 | } else if (cur->type == XML_RELAXNG_TEXT) { |
| 6349 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6350 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_TEXT, |
| 6351 | "Found forbidden pattern list//text\n", NULL, |
| 6352 | NULL); |
| 6353 | } |
| 6354 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6355 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_TEXT, |
| 6356 | "Found forbidden pattern data/except//text\n", |
| 6357 | NULL, NULL); |
| 6358 | } |
| 6359 | if (flags & XML_RELAXNG_IN_START) { |
| 6360 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_TEXT, |
| 6361 | "Found forbidden pattern start//text\n", NULL, |
| 6362 | NULL); |
| 6363 | } |
| 6364 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6365 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 6366 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6367 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_EMPTY, |
| 6368 | "Found forbidden pattern data/except//empty\n", |
| 6369 | NULL, NULL); |
| 6370 | } |
| 6371 | if (flags & XML_RELAXNG_IN_START) { |
| 6372 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_EMPTY, |
| 6373 | "Found forbidden pattern start//empty\n", NULL, |
| 6374 | NULL); |
| 6375 | } |
| 6376 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6377 | } else if (cur->type == XML_RELAXNG_CHOICE) { |
| 6378 | xmlRelaxNGCheckChoiceDeterminism(ctxt, cur); |
| 6379 | ret = |
| 6380 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6381 | } else { |
| 6382 | ret = |
| 6383 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6384 | } |
| 6385 | cur = cur->next; |
| 6386 | if (ptype == XML_RELAXNG_GROUP) { |
| 6387 | val = xmlRelaxNGGroupContentType(val, ret); |
| 6388 | } else if (ptype == XML_RELAXNG_INTERLEAVE) { |
| 6389 | tmp = xmlRelaxNGGroupContentType(val, ret); |
| 6390 | if (tmp != XML_RELAXNG_CONTENT_ERROR) |
| 6391 | tmp = xmlRelaxNGMaxContentType(val, ret); |
| 6392 | } else if (ptype == XML_RELAXNG_CHOICE) { |
| 6393 | val = xmlRelaxNGMaxContentType(val, ret); |
| 6394 | } else if (ptype == XML_RELAXNG_LIST) { |
| 6395 | val = XML_RELAXNG_CONTENT_SIMPLE; |
| 6396 | } else if (ptype == XML_RELAXNG_EXCEPT) { |
| 6397 | if (ret == XML_RELAXNG_CONTENT_ERROR) |
| 6398 | val = XML_RELAXNG_CONTENT_ERROR; |
| 6399 | else |
| 6400 | val = XML_RELAXNG_CONTENT_SIMPLE; |
| 6401 | } else { |
| 6402 | val = xmlRelaxNGGroupContentType(val, ret); |
| 6403 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6404 | |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6405 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6406 | return (val); |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6407 | } |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6408 | |
| 6409 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6410 | * xmlRelaxNGParseGrammar: |
| 6411 | * @ctxt: a Relax-NG parser context |
| 6412 | * @nodes: grammar children nodes |
| 6413 | * |
| 6414 | * parse a Relax-NG <grammar> node |
| 6415 | * |
| 6416 | * Returns the internal xmlRelaxNGGrammarPtr built or |
| 6417 | * NULL in case of error |
| 6418 | */ |
| 6419 | static xmlRelaxNGGrammarPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6420 | xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) |
| 6421 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6422 | xmlRelaxNGGrammarPtr ret, tmp, old; |
| 6423 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6424 | #ifdef DEBUG_GRAMMAR |
| 6425 | xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n"); |
| 6426 | #endif |
| 6427 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6428 | ret = xmlRelaxNGNewGrammar(ctxt); |
| 6429 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6430 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6431 | |
| 6432 | /* |
| 6433 | * Link the new grammar in the tree |
| 6434 | */ |
| 6435 | ret->parent = ctxt->grammar; |
| 6436 | if (ctxt->grammar != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6437 | tmp = ctxt->grammar->children; |
| 6438 | if (tmp == NULL) { |
| 6439 | ctxt->grammar->children = ret; |
| 6440 | } else { |
| 6441 | while (tmp->next != NULL) |
| 6442 | tmp = tmp->next; |
| 6443 | tmp->next = ret; |
| 6444 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6445 | } |
| 6446 | |
| 6447 | old = ctxt->grammar; |
| 6448 | ctxt->grammar = ret; |
| 6449 | xmlRelaxNGParseGrammarContent(ctxt, nodes); |
| 6450 | ctxt->grammar = ret; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6451 | if (ctxt->grammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6452 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT, |
| 6453 | "Failed to parse <grammar> content\n", NULL, NULL); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6454 | } else if (ctxt->grammar->start == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6455 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_NO_START, |
| 6456 | "Element <grammar> has no <start>\n", NULL, NULL); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6457 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6458 | |
| 6459 | /* |
| 6460 | * Apply 4.17 mergingd rules to defines and starts |
| 6461 | */ |
| 6462 | xmlRelaxNGCombineStart(ctxt, ret); |
| 6463 | if (ret->defs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6464 | xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine, |
| 6465 | ctxt); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6466 | } |
| 6467 | |
| 6468 | /* |
| 6469 | * link together defines and refs in this grammar |
| 6470 | */ |
| 6471 | if (ret->refs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6472 | xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference, |
| 6473 | ctxt); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6474 | } |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 6475 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6476 | ctxt->grammar = old; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6477 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6478 | } |
| 6479 | |
| 6480 | /** |
| 6481 | * xmlRelaxNGParseDocument: |
| 6482 | * @ctxt: a Relax-NG parser context |
| 6483 | * @node: the root node of the RelaxNG schema |
| 6484 | * |
| 6485 | * parse a Relax-NG definition resource and build an internal |
| 6486 | * xmlRelaxNG struture which can be used to validate instances. |
| 6487 | * |
| 6488 | * Returns the internal XML RelaxNG structure built or |
| 6489 | * NULL in case of error |
| 6490 | */ |
| 6491 | static xmlRelaxNGPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6492 | xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 6493 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6494 | xmlRelaxNGPtr schema = NULL; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6495 | const xmlChar *olddefine; |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 6496 | xmlRelaxNGGrammarPtr old; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6497 | |
| 6498 | if ((ctxt == NULL) || (node == NULL)) |
| 6499 | return (NULL); |
| 6500 | |
| 6501 | schema = xmlRelaxNGNewRelaxNG(ctxt); |
| 6502 | if (schema == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6503 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6504 | |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6505 | olddefine = ctxt->define; |
| 6506 | ctxt->define = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6507 | if (IS_RELAXNG(node, "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6508 | schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6509 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6510 | xmlRelaxNGGrammarPtr tmp, ret; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6511 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6512 | schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt); |
| 6513 | if (schema->topgrammar == NULL) { |
| 6514 | return (schema); |
| 6515 | } |
| 6516 | /* |
| 6517 | * Link the new grammar in the tree |
| 6518 | */ |
| 6519 | ret->parent = ctxt->grammar; |
| 6520 | if (ctxt->grammar != NULL) { |
| 6521 | tmp = ctxt->grammar->children; |
| 6522 | if (tmp == NULL) { |
| 6523 | ctxt->grammar->children = ret; |
| 6524 | } else { |
| 6525 | while (tmp->next != NULL) |
| 6526 | tmp = tmp->next; |
| 6527 | tmp->next = ret; |
| 6528 | } |
| 6529 | } |
| 6530 | old = ctxt->grammar; |
| 6531 | ctxt->grammar = ret; |
| 6532 | xmlRelaxNGParseStart(ctxt, node); |
| 6533 | if (old != NULL) |
| 6534 | ctxt->grammar = old; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6535 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6536 | ctxt->define = olddefine; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 6537 | if (schema->topgrammar->start != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6538 | xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0); |
| 6539 | if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) { |
| 6540 | xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL); |
| 6541 | while ((schema->topgrammar->start != NULL) && |
| 6542 | (schema->topgrammar->start->type == XML_RELAXNG_NOOP) && |
| 6543 | (schema->topgrammar->start->next != NULL)) |
| 6544 | schema->topgrammar->start = |
| 6545 | schema->topgrammar->start->content; |
| 6546 | xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start, |
| 6547 | XML_RELAXNG_IN_START, XML_RELAXNG_NOOP); |
| 6548 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 6549 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6550 | #ifdef DEBUG |
| 6551 | if (schema == NULL) |
| 6552 | xmlGenericError(xmlGenericErrorContext, |
| 6553 | "xmlRelaxNGParseDocument() failed\n"); |
| 6554 | #endif |
| 6555 | |
| 6556 | return (schema); |
| 6557 | } |
| 6558 | |
| 6559 | /************************************************************************ |
| 6560 | * * |
| 6561 | * Reading RelaxNGs * |
| 6562 | * * |
| 6563 | ************************************************************************/ |
| 6564 | |
| 6565 | /** |
| 6566 | * xmlRelaxNGNewParserCtxt: |
| 6567 | * @URL: the location of the schema |
| 6568 | * |
| 6569 | * Create an XML RelaxNGs parse context for that file/resource expected |
| 6570 | * to contain an XML RelaxNGs file. |
| 6571 | * |
| 6572 | * Returns the parser context or NULL in case of error |
| 6573 | */ |
| 6574 | xmlRelaxNGParserCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6575 | xmlRelaxNGNewParserCtxt(const char *URL) |
| 6576 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6577 | xmlRelaxNGParserCtxtPtr ret; |
| 6578 | |
| 6579 | if (URL == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6580 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6581 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6582 | ret = |
| 6583 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6584 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6585 | xmlRngPErrMemory(NULL, "building parser\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6586 | return (NULL); |
| 6587 | } |
| 6588 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6589 | ret->URL = xmlStrdup((const xmlChar *) URL); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 6590 | ret->error = xmlGenericError; |
| 6591 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6592 | return (ret); |
| 6593 | } |
| 6594 | |
| 6595 | /** |
| 6596 | * xmlRelaxNGNewMemParserCtxt: |
| 6597 | * @buffer: a pointer to a char array containing the schemas |
| 6598 | * @size: the size of the array |
| 6599 | * |
| 6600 | * Create an XML RelaxNGs parse context for that memory buffer expected |
| 6601 | * to contain an XML RelaxNGs file. |
| 6602 | * |
| 6603 | * Returns the parser context or NULL in case of error |
| 6604 | */ |
| 6605 | xmlRelaxNGParserCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6606 | xmlRelaxNGNewMemParserCtxt(const char *buffer, int size) |
| 6607 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6608 | xmlRelaxNGParserCtxtPtr ret; |
| 6609 | |
| 6610 | if ((buffer == NULL) || (size <= 0)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6611 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6612 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6613 | ret = |
| 6614 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6615 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6616 | xmlRngPErrMemory(NULL, "building parser\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6617 | return (NULL); |
| 6618 | } |
| 6619 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6620 | ret->buffer = buffer; |
| 6621 | ret->size = size; |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 6622 | ret->error = xmlGenericError; |
| 6623 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6624 | return (ret); |
| 6625 | } |
| 6626 | |
| 6627 | /** |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6628 | * xmlRelaxNGNewDocParserCtxt: |
| 6629 | * @doc: a preparsed document tree |
| 6630 | * |
| 6631 | * Create an XML RelaxNGs parser context for that document. |
| 6632 | * Note: since the process of compiling a RelaxNG schemas modifies the |
| 6633 | * document, the @doc parameter is duplicated internally. |
| 6634 | * |
| 6635 | * Returns the parser context or NULL in case of error |
| 6636 | */ |
| 6637 | xmlRelaxNGParserCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6638 | xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc) |
| 6639 | { |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6640 | xmlRelaxNGParserCtxtPtr ret; |
| 6641 | xmlDocPtr copy; |
| 6642 | |
| 6643 | if (doc == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6644 | return (NULL); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6645 | copy = xmlCopyDoc(doc, 1); |
| 6646 | if (copy == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6647 | return (NULL); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6648 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6649 | ret = |
| 6650 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6651 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6652 | xmlRngPErrMemory(NULL, "building parser\n"); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6653 | return (NULL); |
| 6654 | } |
| 6655 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6656 | ret->document = copy; |
| 6657 | ret->userData = xmlGenericErrorContext; |
| 6658 | return (ret); |
| 6659 | } |
| 6660 | |
| 6661 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6662 | * xmlRelaxNGFreeParserCtxt: |
| 6663 | * @ctxt: the schema parser context |
| 6664 | * |
| 6665 | * Free the resources associated to the schema parser context |
| 6666 | */ |
| 6667 | void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6668 | xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt) |
| 6669 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6670 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6671 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6672 | if (ctxt->URL != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6673 | xmlFree(ctxt->URL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6674 | if (ctxt->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6675 | xmlRelaxNGFreeDocument(ctxt->doc); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 6676 | if (ctxt->interleaves != NULL) |
| 6677 | xmlHashFree(ctxt->interleaves, NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6678 | if (ctxt->documents != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6679 | xmlRelaxNGFreeDocumentList(ctxt->documents); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 6680 | if (ctxt->includes != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6681 | xmlRelaxNGFreeIncludeList(ctxt->includes); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6682 | if (ctxt->docTab != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6683 | xmlFree(ctxt->docTab); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 6684 | if (ctxt->incTab != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6685 | xmlFree(ctxt->incTab); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6686 | if (ctxt->defTab != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6687 | int i; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6688 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6689 | for (i = 0; i < ctxt->defNr; i++) |
| 6690 | xmlRelaxNGFreeDefine(ctxt->defTab[i]); |
| 6691 | xmlFree(ctxt->defTab); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6692 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6693 | xmlFree(ctxt); |
| 6694 | } |
| 6695 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6696 | /** |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6697 | * xmlRelaxNGNormExtSpace: |
| 6698 | * @value: a value |
| 6699 | * |
| 6700 | * Removes the leading and ending spaces of the value |
| 6701 | * The string is modified "in situ" |
| 6702 | */ |
| 6703 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6704 | xmlRelaxNGNormExtSpace(xmlChar * value) |
| 6705 | { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6706 | xmlChar *start = value; |
| 6707 | xmlChar *cur = value; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6708 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6709 | if (value == NULL) |
| 6710 | return; |
| 6711 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6712 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6713 | cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6714 | if (cur == start) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6715 | do { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6716 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6717 | cur++; |
| 6718 | if (*cur == 0) |
| 6719 | return; |
| 6720 | start = cur; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6721 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6722 | cur++; |
| 6723 | if (*cur == 0) { |
| 6724 | *start = 0; |
| 6725 | return; |
| 6726 | } |
| 6727 | } while (1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6728 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6729 | do { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6730 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6731 | *start++ = *cur++; |
| 6732 | if (*cur == 0) { |
| 6733 | *start = 0; |
| 6734 | return; |
| 6735 | } |
| 6736 | /* don't try to normalize the inner spaces */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6737 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6738 | cur++; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6739 | if (*cur == 0) { |
| 6740 | *start = 0; |
| 6741 | return; |
| 6742 | } |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 6743 | *start++ = *cur++; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6744 | } while (1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6745 | } |
| 6746 | } |
| 6747 | |
| 6748 | /** |
| 6749 | * xmlRelaxNGCheckAttributes: |
| 6750 | * @ctxt: a Relax-NG parser context |
| 6751 | * @node: a Relax-NG node |
| 6752 | * |
| 6753 | * Check all the attributes on the given node |
| 6754 | */ |
| 6755 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6756 | xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 6757 | { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6758 | xmlAttrPtr cur, next; |
| 6759 | |
| 6760 | cur = node->properties; |
| 6761 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6762 | next = cur->next; |
| 6763 | if ((cur->ns == NULL) || |
| 6764 | (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
| 6765 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
| 6766 | if ((!xmlStrEqual(node->name, BAD_CAST "element")) && |
| 6767 | (!xmlStrEqual(node->name, BAD_CAST "attribute")) && |
| 6768 | (!xmlStrEqual(node->name, BAD_CAST "ref")) && |
| 6769 | (!xmlStrEqual(node->name, BAD_CAST "parentRef")) && |
| 6770 | (!xmlStrEqual(node->name, BAD_CAST "param")) && |
| 6771 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
| 6772 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6773 | "Attribute %s is not allowed on %s\n", |
| 6774 | cur->name, node->name); |
| 6775 | } |
| 6776 | } else if (xmlStrEqual(cur->name, BAD_CAST "type")) { |
| 6777 | if ((!xmlStrEqual(node->name, BAD_CAST "value")) && |
| 6778 | (!xmlStrEqual(node->name, BAD_CAST "data"))) { |
| 6779 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6780 | "Attribute %s is not allowed on %s\n", |
| 6781 | cur->name, node->name); |
| 6782 | } |
| 6783 | } else if (xmlStrEqual(cur->name, BAD_CAST "href")) { |
| 6784 | if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) && |
| 6785 | (!xmlStrEqual(node->name, BAD_CAST "include"))) { |
| 6786 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6787 | "Attribute %s is not allowed on %s\n", |
| 6788 | cur->name, node->name); |
| 6789 | } |
| 6790 | } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) { |
| 6791 | if ((!xmlStrEqual(node->name, BAD_CAST "start")) && |
| 6792 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
| 6793 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6794 | "Attribute %s is not allowed on %s\n", |
| 6795 | cur->name, node->name); |
| 6796 | } |
| 6797 | } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) { |
| 6798 | xmlChar *val; |
| 6799 | xmlURIPtr uri; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6800 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6801 | val = xmlNodeListGetString(node->doc, cur->children, 1); |
| 6802 | if (val != NULL) { |
| 6803 | if (val[0] != 0) { |
| 6804 | uri = xmlParseURI((const char *) val); |
| 6805 | if (uri == NULL) { |
| 6806 | xmlRngPErr(ctxt, node, XML_RNGP_INVALID_URI, |
| 6807 | "Attribute %s contains invalid URI %s\n", |
| 6808 | cur->name, val); |
| 6809 | } else { |
| 6810 | if (uri->scheme == NULL) { |
| 6811 | xmlRngPErr(ctxt, node, XML_RNGP_URI_NOT_ABSOLUTE, |
| 6812 | "Attribute %s URI %s is not absolute\n", |
| 6813 | cur->name, val); |
| 6814 | } |
| 6815 | if (uri->fragment != NULL) { |
| 6816 | xmlRngPErr(ctxt, node, XML_RNGP_URI_FRAGMENT, |
| 6817 | "Attribute %s URI %s has a fragment ID\n", |
| 6818 | cur->name, val); |
| 6819 | } |
| 6820 | xmlFreeURI(uri); |
| 6821 | } |
| 6822 | } |
| 6823 | xmlFree(val); |
| 6824 | } |
| 6825 | } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) { |
| 6826 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_ATTRIBUTE, |
| 6827 | "Unknown attribute %s on %s\n", cur->name, |
| 6828 | node->name); |
| 6829 | } |
| 6830 | } |
| 6831 | cur = next; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6832 | } |
| 6833 | } |
| 6834 | |
| 6835 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6836 | * xmlRelaxNGCleanupTree: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6837 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6838 | * @root: an xmlNodePtr subtree |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6839 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6840 | * Cleanup the subtree from unwanted nodes for parsing, resolve |
| 6841 | * Include and externalRef lookups. |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6842 | */ |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6843 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6844 | xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root) |
| 6845 | { |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6846 | xmlNodePtr cur, delete; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6847 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6848 | delete = NULL; |
| 6849 | cur = root; |
| 6850 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6851 | if (delete != NULL) { |
| 6852 | xmlUnlinkNode(delete); |
| 6853 | xmlFreeNode(delete); |
| 6854 | delete = NULL; |
| 6855 | } |
| 6856 | if (cur->type == XML_ELEMENT_NODE) { |
| 6857 | /* |
| 6858 | * Simplification 4.1. Annotations |
| 6859 | */ |
| 6860 | if ((cur->ns == NULL) || |
| 6861 | (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
| 6862 | if ((cur->parent != NULL) && |
| 6863 | (cur->parent->type == XML_ELEMENT_NODE) && |
| 6864 | ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) || |
| 6865 | (xmlStrEqual(cur->parent->name, BAD_CAST "value")) || |
| 6866 | (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) { |
| 6867 | xmlRngPErr(ctxt, cur, XML_RNGP_FOREIGN_ELEMENT, |
| 6868 | "element %s doesn't allow foreign elements\n", |
| 6869 | cur->parent->name, NULL); |
| 6870 | } |
| 6871 | delete = cur; |
| 6872 | goto skip_children; |
| 6873 | } else { |
| 6874 | xmlRelaxNGCleanupAttributes(ctxt, cur); |
| 6875 | if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) { |
| 6876 | xmlChar *href, *ns, *base, *URL; |
| 6877 | xmlRelaxNGDocumentPtr docu; |
| 6878 | xmlNodePtr tmp; |
Daniel Veillard | 6dc9196 | 2004-03-22 19:10:02 +0000 | [diff] [blame] | 6879 | xmlURIPtr uri; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6880 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6881 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6882 | if (ns == NULL) { |
| 6883 | tmp = cur->parent; |
| 6884 | while ((tmp != NULL) && |
| 6885 | (tmp->type == XML_ELEMENT_NODE)) { |
| 6886 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 6887 | if (ns != NULL) |
| 6888 | break; |
| 6889 | tmp = tmp->parent; |
| 6890 | } |
| 6891 | } |
| 6892 | href = xmlGetProp(cur, BAD_CAST "href"); |
| 6893 | if (href == NULL) { |
| 6894 | xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF, |
| 6895 | "xmlRelaxNGParse: externalRef has no href attribute\n", |
| 6896 | NULL, NULL); |
| 6897 | delete = cur; |
| 6898 | goto skip_children; |
| 6899 | } |
Daniel Veillard | 6dc9196 | 2004-03-22 19:10:02 +0000 | [diff] [blame] | 6900 | uri = xmlParseURI((const char *) href); |
| 6901 | if (uri == NULL) { |
| 6902 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
| 6903 | "Incorrect URI for externalRef %s\n", |
| 6904 | href, NULL); |
| 6905 | if (href != NULL) |
| 6906 | xmlFree(href); |
| 6907 | delete = cur; |
| 6908 | goto skip_children; |
| 6909 | } |
| 6910 | if (uri->fragment != NULL) { |
| 6911 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
| 6912 | "Fragment forbidden in URI for externalRef %s\n", |
| 6913 | href, NULL); |
| 6914 | xmlFreeURI(uri); |
| 6915 | if (href != NULL) |
| 6916 | xmlFree(href); |
| 6917 | delete = cur; |
| 6918 | goto skip_children; |
| 6919 | } |
| 6920 | xmlFreeURI(uri); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6921 | base = xmlNodeGetBase(cur->doc, cur); |
| 6922 | URL = xmlBuildURI(href, base); |
| 6923 | if (URL == NULL) { |
| 6924 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
| 6925 | "Failed to compute URL for externalRef %s\n", |
| 6926 | href, NULL); |
| 6927 | if (href != NULL) |
| 6928 | xmlFree(href); |
| 6929 | if (base != NULL) |
| 6930 | xmlFree(base); |
| 6931 | delete = cur; |
| 6932 | goto skip_children; |
| 6933 | } |
| 6934 | if (href != NULL) |
| 6935 | xmlFree(href); |
| 6936 | if (base != NULL) |
| 6937 | xmlFree(base); |
| 6938 | docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns); |
| 6939 | if (docu == NULL) { |
| 6940 | xmlRngPErr(ctxt, cur, XML_RNGP_EXTERNAL_REF_FAILURE, |
| 6941 | "Failed to load externalRef %s\n", URL, |
| 6942 | NULL); |
| 6943 | xmlFree(URL); |
| 6944 | delete = cur; |
| 6945 | goto skip_children; |
| 6946 | } |
| 6947 | if (ns != NULL) |
| 6948 | xmlFree(ns); |
| 6949 | xmlFree(URL); |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 6950 | cur->psvi = docu; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6951 | } else if (xmlStrEqual(cur->name, BAD_CAST "include")) { |
| 6952 | xmlChar *href, *ns, *base, *URL; |
| 6953 | xmlRelaxNGIncludePtr incl; |
| 6954 | xmlNodePtr tmp; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6955 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6956 | href = xmlGetProp(cur, BAD_CAST "href"); |
| 6957 | if (href == NULL) { |
| 6958 | xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF, |
| 6959 | "xmlRelaxNGParse: include has no href attribute\n", |
| 6960 | NULL, NULL); |
| 6961 | delete = cur; |
| 6962 | goto skip_children; |
| 6963 | } |
| 6964 | base = xmlNodeGetBase(cur->doc, cur); |
| 6965 | URL = xmlBuildURI(href, base); |
| 6966 | if (URL == NULL) { |
| 6967 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
| 6968 | "Failed to compute URL for include %s\n", |
| 6969 | href, NULL); |
| 6970 | if (href != NULL) |
| 6971 | xmlFree(href); |
| 6972 | if (base != NULL) |
| 6973 | xmlFree(base); |
| 6974 | delete = cur; |
| 6975 | goto skip_children; |
| 6976 | } |
| 6977 | if (href != NULL) |
| 6978 | xmlFree(href); |
| 6979 | if (base != NULL) |
| 6980 | xmlFree(base); |
| 6981 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6982 | if (ns == NULL) { |
| 6983 | tmp = cur->parent; |
| 6984 | while ((tmp != NULL) && |
| 6985 | (tmp->type == XML_ELEMENT_NODE)) { |
| 6986 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 6987 | if (ns != NULL) |
| 6988 | break; |
| 6989 | tmp = tmp->parent; |
| 6990 | } |
| 6991 | } |
| 6992 | incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns); |
| 6993 | if (ns != NULL) |
| 6994 | xmlFree(ns); |
| 6995 | if (incl == NULL) { |
| 6996 | xmlRngPErr(ctxt, cur, XML_RNGP_INCLUDE_FAILURE, |
| 6997 | "Failed to load include %s\n", URL, |
| 6998 | NULL); |
| 6999 | xmlFree(URL); |
| 7000 | delete = cur; |
| 7001 | goto skip_children; |
| 7002 | } |
| 7003 | xmlFree(URL); |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 7004 | cur->psvi = incl; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7005 | } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) || |
| 7006 | (xmlStrEqual(cur->name, BAD_CAST "attribute"))) |
| 7007 | { |
| 7008 | xmlChar *name, *ns; |
| 7009 | xmlNodePtr text = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7010 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7011 | /* |
| 7012 | * Simplification 4.8. name attribute of element |
| 7013 | * and attribute elements |
| 7014 | */ |
| 7015 | name = xmlGetProp(cur, BAD_CAST "name"); |
| 7016 | if (name != NULL) { |
| 7017 | if (cur->children == NULL) { |
| 7018 | text = |
| 7019 | xmlNewChild(cur, cur->ns, BAD_CAST "name", |
| 7020 | name); |
| 7021 | } else { |
| 7022 | xmlNodePtr node; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7023 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7024 | node = xmlNewNode(cur->ns, BAD_CAST "name"); |
| 7025 | if (node != NULL) { |
| 7026 | xmlAddPrevSibling(cur->children, node); |
| 7027 | text = xmlNewText(name); |
| 7028 | xmlAddChild(node, text); |
| 7029 | text = node; |
| 7030 | } |
| 7031 | } |
| 7032 | if (text == NULL) { |
| 7033 | xmlRngPErr(ctxt, cur, XML_RNGP_CREATE_FAILURE, |
| 7034 | "Failed to create a name %s element\n", |
| 7035 | name, NULL); |
| 7036 | } |
| 7037 | xmlUnsetProp(cur, BAD_CAST "name"); |
| 7038 | xmlFree(name); |
| 7039 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 7040 | if (ns != NULL) { |
| 7041 | if (text != NULL) { |
| 7042 | xmlSetProp(text, BAD_CAST "ns", ns); |
| 7043 | /* xmlUnsetProp(cur, BAD_CAST "ns"); */ |
| 7044 | } |
| 7045 | xmlFree(ns); |
| 7046 | } else if (xmlStrEqual(cur->name, |
| 7047 | BAD_CAST "attribute")) { |
| 7048 | xmlSetProp(text, BAD_CAST "ns", BAD_CAST ""); |
| 7049 | } |
| 7050 | } |
| 7051 | } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) || |
| 7052 | (xmlStrEqual(cur->name, BAD_CAST "nsName")) || |
| 7053 | (xmlStrEqual(cur->name, BAD_CAST "value"))) { |
| 7054 | /* |
| 7055 | * Simplification 4.8. name attribute of element |
| 7056 | * and attribute elements |
| 7057 | */ |
| 7058 | if (xmlHasProp(cur, BAD_CAST "ns") == NULL) { |
| 7059 | xmlNodePtr node; |
| 7060 | xmlChar *ns = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7061 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7062 | node = cur->parent; |
| 7063 | while ((node != NULL) && |
| 7064 | (node->type == XML_ELEMENT_NODE)) { |
| 7065 | ns = xmlGetProp(node, BAD_CAST "ns"); |
| 7066 | if (ns != NULL) { |
| 7067 | break; |
| 7068 | } |
| 7069 | node = node->parent; |
| 7070 | } |
| 7071 | if (ns == NULL) { |
| 7072 | xmlSetProp(cur, BAD_CAST "ns", BAD_CAST ""); |
| 7073 | } else { |
| 7074 | xmlSetProp(cur, BAD_CAST "ns", ns); |
| 7075 | xmlFree(ns); |
| 7076 | } |
| 7077 | } |
| 7078 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
| 7079 | xmlChar *name, *local, *prefix; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7080 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7081 | /* |
| 7082 | * Simplification: 4.10. QNames |
| 7083 | */ |
| 7084 | name = xmlNodeGetContent(cur); |
| 7085 | if (name != NULL) { |
| 7086 | local = xmlSplitQName2(name, &prefix); |
| 7087 | if (local != NULL) { |
| 7088 | xmlNsPtr ns; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7089 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7090 | ns = xmlSearchNs(cur->doc, cur, prefix); |
| 7091 | if (ns == NULL) { |
| 7092 | xmlRngPErr(ctxt, cur, |
| 7093 | XML_RNGP_PREFIX_UNDEFINED, |
| 7094 | "xmlRelaxNGParse: no namespace for prefix %s\n", |
| 7095 | prefix, NULL); |
| 7096 | } else { |
| 7097 | xmlSetProp(cur, BAD_CAST "ns", |
| 7098 | ns->href); |
| 7099 | xmlNodeSetContent(cur, local); |
| 7100 | } |
| 7101 | xmlFree(local); |
| 7102 | xmlFree(prefix); |
| 7103 | } |
| 7104 | xmlFree(name); |
| 7105 | } |
| 7106 | } |
| 7107 | /* |
| 7108 | * 4.16 |
| 7109 | */ |
| 7110 | if (xmlStrEqual(cur->name, BAD_CAST "nsName")) { |
| 7111 | if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
| 7112 | xmlRngPErr(ctxt, cur, |
| 7113 | XML_RNGP_PAT_NSNAME_EXCEPT_NSNAME, |
| 7114 | "Found nsName/except//nsName forbidden construct\n", |
| 7115 | NULL, NULL); |
| 7116 | } |
| 7117 | } |
| 7118 | } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) && |
| 7119 | (cur != root)) { |
| 7120 | int oldflags = ctxt->flags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7121 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7122 | /* |
| 7123 | * 4.16 |
| 7124 | */ |
| 7125 | if ((cur->parent != NULL) && |
| 7126 | (xmlStrEqual |
| 7127 | (cur->parent->name, BAD_CAST "anyName"))) { |
| 7128 | ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT; |
| 7129 | xmlRelaxNGCleanupTree(ctxt, cur); |
| 7130 | ctxt->flags = oldflags; |
| 7131 | goto skip_children; |
| 7132 | } else if ((cur->parent != NULL) && |
| 7133 | (xmlStrEqual |
| 7134 | (cur->parent->name, BAD_CAST "nsName"))) { |
| 7135 | ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT; |
| 7136 | xmlRelaxNGCleanupTree(ctxt, cur); |
| 7137 | ctxt->flags = oldflags; |
| 7138 | goto skip_children; |
| 7139 | } |
| 7140 | } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) { |
| 7141 | /* |
| 7142 | * 4.16 |
| 7143 | */ |
| 7144 | if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) { |
| 7145 | xmlRngPErr(ctxt, cur, |
| 7146 | XML_RNGP_PAT_ANYNAME_EXCEPT_ANYNAME, |
| 7147 | "Found anyName/except//anyName forbidden construct\n", |
| 7148 | NULL, NULL); |
| 7149 | } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
| 7150 | xmlRngPErr(ctxt, cur, |
| 7151 | XML_RNGP_PAT_NSNAME_EXCEPT_ANYNAME, |
| 7152 | "Found nsName/except//anyName forbidden construct\n", |
| 7153 | NULL, NULL); |
| 7154 | } |
| 7155 | } |
| 7156 | /* |
| 7157 | * Thisd is not an else since "include" is transformed |
| 7158 | * into a div |
| 7159 | */ |
| 7160 | if (xmlStrEqual(cur->name, BAD_CAST "div")) { |
| 7161 | xmlChar *ns; |
| 7162 | xmlNodePtr child, ins, tmp; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7163 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7164 | /* |
| 7165 | * implements rule 4.11 |
| 7166 | */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7167 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7168 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 7169 | |
| 7170 | child = cur->children; |
| 7171 | ins = cur; |
| 7172 | while (child != NULL) { |
| 7173 | if (ns != NULL) { |
| 7174 | if (!xmlHasProp(child, BAD_CAST "ns")) { |
| 7175 | xmlSetProp(child, BAD_CAST "ns", ns); |
| 7176 | } |
| 7177 | } |
| 7178 | tmp = child->next; |
| 7179 | xmlUnlinkNode(child); |
| 7180 | ins = xmlAddNextSibling(ins, child); |
| 7181 | child = tmp; |
| 7182 | } |
| 7183 | if (ns != NULL) |
| 7184 | xmlFree(ns); |
William M. Brack | 8eabb05 | 2004-06-07 14:15:54 +0000 | [diff] [blame] | 7185 | /* |
| 7186 | * Since we are about to delete cur, if it's nsDef is non-NULL we |
| 7187 | * need to preserve it (it contains the ns definitions for the |
| 7188 | * children we just moved). We'll just stick it on to the end |
| 7189 | * of cur->parent's list, since it's never going to be re-serialized |
| 7190 | * (bug 143738). |
| 7191 | */ |
| 7192 | if (cur->nsDef != NULL) { |
| 7193 | xmlNsPtr parDef = (xmlNsPtr)&cur->parent->nsDef; |
| 7194 | while (parDef->next != NULL) |
| 7195 | parDef = parDef->next; |
| 7196 | parDef->next = cur->nsDef; |
| 7197 | cur->nsDef = NULL; |
| 7198 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7199 | delete = cur; |
| 7200 | goto skip_children; |
| 7201 | } |
| 7202 | } |
| 7203 | } |
| 7204 | /* |
| 7205 | * Simplification 4.2 whitespaces |
| 7206 | */ |
| 7207 | else if ((cur->type == XML_TEXT_NODE) || |
| 7208 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 7209 | if (IS_BLANK_NODE(cur)) { |
| 7210 | if (cur->parent->type == XML_ELEMENT_NODE) { |
| 7211 | if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value")) |
| 7212 | && |
| 7213 | (!xmlStrEqual |
| 7214 | (cur->parent->name, BAD_CAST "param"))) |
| 7215 | delete = cur; |
| 7216 | } else { |
| 7217 | delete = cur; |
| 7218 | goto skip_children; |
| 7219 | } |
| 7220 | } |
| 7221 | } else { |
| 7222 | delete = cur; |
| 7223 | goto skip_children; |
| 7224 | } |
| 7225 | |
| 7226 | /* |
| 7227 | * Skip to next node |
| 7228 | */ |
| 7229 | if (cur->children != NULL) { |
| 7230 | if ((cur->children->type != XML_ENTITY_DECL) && |
| 7231 | (cur->children->type != XML_ENTITY_REF_NODE) && |
| 7232 | (cur->children->type != XML_ENTITY_NODE)) { |
| 7233 | cur = cur->children; |
| 7234 | continue; |
| 7235 | } |
| 7236 | } |
| 7237 | skip_children: |
| 7238 | if (cur->next != NULL) { |
| 7239 | cur = cur->next; |
| 7240 | continue; |
| 7241 | } |
| 7242 | |
| 7243 | do { |
| 7244 | cur = cur->parent; |
| 7245 | if (cur == NULL) |
| 7246 | break; |
| 7247 | if (cur == root) { |
| 7248 | cur = NULL; |
| 7249 | break; |
| 7250 | } |
| 7251 | if (cur->next != NULL) { |
| 7252 | cur = cur->next; |
| 7253 | break; |
| 7254 | } |
| 7255 | } while (cur != NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7256 | } |
| 7257 | if (delete != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7258 | xmlUnlinkNode(delete); |
| 7259 | xmlFreeNode(delete); |
| 7260 | delete = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7261 | } |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7262 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7263 | |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7264 | /** |
| 7265 | * xmlRelaxNGCleanupDoc: |
| 7266 | * @ctxt: a Relax-NG parser context |
| 7267 | * @doc: an xmldocPtr document pointer |
| 7268 | * |
| 7269 | * Cleanup the document from unwanted nodes for parsing, resolve |
| 7270 | * Include and externalRef lookups. |
| 7271 | * |
| 7272 | * Returns the cleaned up document or NULL in case of error |
| 7273 | */ |
| 7274 | static xmlDocPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7275 | xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc) |
| 7276 | { |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7277 | xmlNodePtr root; |
| 7278 | |
| 7279 | /* |
| 7280 | * Extract the root |
| 7281 | */ |
| 7282 | root = xmlDocGetRootElement(doc); |
| 7283 | if (root == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7284 | xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n", |
| 7285 | ctxt->URL, NULL); |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7286 | return (NULL); |
| 7287 | } |
| 7288 | xmlRelaxNGCleanupTree(ctxt, root); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7289 | return (doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7290 | } |
| 7291 | |
| 7292 | /** |
| 7293 | * xmlRelaxNGParse: |
| 7294 | * @ctxt: a Relax-NG parser context |
| 7295 | * |
| 7296 | * parse a schema definition resource and build an internal |
| 7297 | * XML Shema struture which can be used to validate instances. |
| 7298 | * *WARNING* this interface is highly subject to change |
| 7299 | * |
| 7300 | * Returns the internal XML RelaxNG structure built from the resource or |
| 7301 | * NULL in case of error |
| 7302 | */ |
| 7303 | xmlRelaxNGPtr |
| 7304 | xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt) |
| 7305 | { |
| 7306 | xmlRelaxNGPtr ret = NULL; |
| 7307 | xmlDocPtr doc; |
| 7308 | xmlNodePtr root; |
| 7309 | |
| 7310 | xmlRelaxNGInitTypes(); |
| 7311 | |
| 7312 | if (ctxt == NULL) |
| 7313 | return (NULL); |
| 7314 | |
| 7315 | /* |
| 7316 | * First step is to parse the input document into an DOM/Infoset |
| 7317 | */ |
| 7318 | if (ctxt->URL != NULL) { |
Daniel Veillard | 87247e8 | 2004-01-13 20:42:02 +0000 | [diff] [blame] | 7319 | doc = xmlReadFile((const char *) ctxt->URL,NULL,0); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7320 | if (doc == NULL) { |
| 7321 | xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR, |
| 7322 | "xmlRelaxNGParse: could not load %s\n", ctxt->URL, |
| 7323 | NULL); |
| 7324 | return (NULL); |
| 7325 | } |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7326 | } else if (ctxt->buffer != NULL) { |
Daniel Veillard | 87247e8 | 2004-01-13 20:42:02 +0000 | [diff] [blame] | 7327 | doc = xmlReadMemory(ctxt->buffer, ctxt->size,NULL,NULL,0); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7328 | if (doc == NULL) { |
| 7329 | xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR, |
| 7330 | "xmlRelaxNGParse: could not parse schemas\n", NULL, |
| 7331 | NULL); |
| 7332 | return (NULL); |
| 7333 | } |
| 7334 | doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
| 7335 | ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 7336 | } else if (ctxt->document != NULL) { |
| 7337 | doc = ctxt->document; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7338 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7339 | xmlRngPErr(ctxt, NULL, XML_RNGP_EMPTY, |
| 7340 | "xmlRelaxNGParse: nothing to parse\n", NULL, NULL); |
| 7341 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7342 | } |
| 7343 | ctxt->document = doc; |
| 7344 | |
| 7345 | /* |
| 7346 | * Some preprocessing of the document content |
| 7347 | */ |
| 7348 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 7349 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7350 | xmlFreeDoc(ctxt->document); |
| 7351 | ctxt->document = NULL; |
| 7352 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7353 | } |
| 7354 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7355 | /* |
| 7356 | * Then do the parsing for good |
| 7357 | */ |
| 7358 | root = xmlDocGetRootElement(doc); |
| 7359 | if (root == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7360 | xmlRngPErr(ctxt, (xmlNodePtr) doc, |
| 7361 | XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n", |
| 7362 | ctxt->URL, NULL); |
| 7363 | xmlFreeDoc(doc); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7364 | return (NULL); |
| 7365 | } |
| 7366 | ret = xmlRelaxNGParseDocument(ctxt, root); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7367 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7368 | xmlFreeDoc(doc); |
| 7369 | return (NULL); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7370 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7371 | |
| 7372 | /* |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7373 | * Check the ref/defines links |
| 7374 | */ |
| 7375 | /* |
| 7376 | * try to preprocess interleaves |
| 7377 | */ |
| 7378 | if (ctxt->interleaves != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7379 | xmlHashScan(ctxt->interleaves, |
| 7380 | (xmlHashScanner) xmlRelaxNGComputeInterleaves, ctxt); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7381 | } |
| 7382 | |
| 7383 | /* |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7384 | * if there was a parsing error return NULL |
| 7385 | */ |
| 7386 | if (ctxt->nbErrors > 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7387 | xmlRelaxNGFree(ret); |
| 7388 | ctxt->document = NULL; |
| 7389 | xmlFreeDoc(doc); |
| 7390 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7391 | } |
| 7392 | |
| 7393 | /* |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 7394 | * try to compile (parts of) the schemas |
| 7395 | */ |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7396 | if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) { |
| 7397 | if (ret->topgrammar->start->type != XML_RELAXNG_START) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7398 | xmlRelaxNGDefinePtr def; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7399 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7400 | def = xmlRelaxNGNewDefine(ctxt, NULL); |
| 7401 | if (def != NULL) { |
| 7402 | def->type = XML_RELAXNG_START; |
| 7403 | def->content = ret->topgrammar->start; |
| 7404 | ret->topgrammar->start = def; |
| 7405 | } |
| 7406 | } |
| 7407 | xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7408 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 7409 | |
| 7410 | /* |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7411 | * Transfer the pointer for cleanup at the schema level. |
| 7412 | */ |
| 7413 | ret->doc = doc; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7414 | ctxt->document = NULL; |
| 7415 | ret->documents = ctxt->documents; |
| 7416 | ctxt->documents = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7417 | |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 7418 | ret->includes = ctxt->includes; |
| 7419 | ctxt->includes = NULL; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7420 | ret->defNr = ctxt->defNr; |
| 7421 | ret->defTab = ctxt->defTab; |
| 7422 | ctxt->defTab = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7423 | if (ctxt->idref == 1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7424 | ret->idref = 1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7425 | |
| 7426 | return (ret); |
| 7427 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7428 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7429 | /** |
| 7430 | * xmlRelaxNGSetParserErrors: |
| 7431 | * @ctxt: a Relax-NG validation context |
| 7432 | * @err: the error callback |
| 7433 | * @warn: the warning callback |
| 7434 | * @ctx: contextual data for the callbacks |
| 7435 | * |
| 7436 | * Set the callback functions used to handle errors for a validation context |
| 7437 | */ |
| 7438 | void |
| 7439 | xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7440 | xmlRelaxNGValidityErrorFunc err, |
| 7441 | xmlRelaxNGValidityWarningFunc warn, void *ctx) |
| 7442 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7443 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7444 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7445 | ctxt->error = err; |
| 7446 | ctxt->warning = warn; |
| 7447 | ctxt->userData = ctx; |
| 7448 | } |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 7449 | |
| 7450 | /** |
| 7451 | * xmlRelaxNGGetParserErrors: |
| 7452 | * @ctxt: a Relax-NG validation context |
| 7453 | * @err: the error callback result |
| 7454 | * @warn: the warning callback result |
| 7455 | * @ctx: contextual data for the callbacks result |
| 7456 | * |
| 7457 | * Get the callback information used to handle errors for a validation context |
| 7458 | * |
| 7459 | * Returns -1 in case of failure, 0 otherwise. |
| 7460 | */ |
| 7461 | int |
| 7462 | xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7463 | xmlRelaxNGValidityErrorFunc * err, |
| 7464 | xmlRelaxNGValidityWarningFunc * warn, void **ctx) |
| 7465 | { |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 7466 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7467 | return (-1); |
| 7468 | if (err != NULL) |
| 7469 | *err = ctxt->error; |
| 7470 | if (warn != NULL) |
| 7471 | *warn = ctxt->warning; |
| 7472 | if (ctx != NULL) |
| 7473 | *ctx = ctxt->userData; |
| 7474 | return (0); |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 7475 | } |
| 7476 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 7477 | #ifdef LIBXML_OUTPUT_ENABLED |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7478 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7479 | /************************************************************************ |
| 7480 | * * |
| 7481 | * Dump back a compiled form * |
| 7482 | * * |
| 7483 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7484 | static void xmlRelaxNGDumpDefine(FILE * output, |
| 7485 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7486 | |
| 7487 | /** |
| 7488 | * xmlRelaxNGDumpDefines: |
| 7489 | * @output: the file output |
| 7490 | * @defines: a list of define structures |
| 7491 | * |
| 7492 | * Dump a RelaxNG structure back |
| 7493 | */ |
| 7494 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7495 | xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines) |
| 7496 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7497 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7498 | xmlRelaxNGDumpDefine(output, defines); |
| 7499 | defines = defines->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7500 | } |
| 7501 | } |
| 7502 | |
| 7503 | /** |
| 7504 | * xmlRelaxNGDumpDefine: |
| 7505 | * @output: the file output |
| 7506 | * @define: a define structure |
| 7507 | * |
| 7508 | * Dump a RelaxNG structure back |
| 7509 | */ |
| 7510 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7511 | xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define) |
| 7512 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7513 | if (define == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7514 | return; |
| 7515 | switch (define->type) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7516 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7517 | fprintf(output, "<empty/>\n"); |
| 7518 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7519 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7520 | fprintf(output, "<notAllowed/>\n"); |
| 7521 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7522 | case XML_RELAXNG_TEXT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7523 | fprintf(output, "<text/>\n"); |
| 7524 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7525 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7526 | fprintf(output, "<element>\n"); |
| 7527 | if (define->name != NULL) { |
| 7528 | fprintf(output, "<name"); |
| 7529 | if (define->ns != NULL) |
| 7530 | fprintf(output, " ns=\"%s\"", define->ns); |
| 7531 | fprintf(output, ">%s</name>\n", define->name); |
| 7532 | } |
| 7533 | xmlRelaxNGDumpDefines(output, define->attrs); |
| 7534 | xmlRelaxNGDumpDefines(output, define->content); |
| 7535 | fprintf(output, "</element>\n"); |
| 7536 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7537 | case XML_RELAXNG_LIST: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7538 | fprintf(output, "<list>\n"); |
| 7539 | xmlRelaxNGDumpDefines(output, define->content); |
| 7540 | fprintf(output, "</list>\n"); |
| 7541 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7542 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7543 | fprintf(output, "<oneOrMore>\n"); |
| 7544 | xmlRelaxNGDumpDefines(output, define->content); |
| 7545 | fprintf(output, "</oneOrMore>\n"); |
| 7546 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7547 | case XML_RELAXNG_ZEROORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7548 | fprintf(output, "<zeroOrMore>\n"); |
| 7549 | xmlRelaxNGDumpDefines(output, define->content); |
| 7550 | fprintf(output, "</zeroOrMore>\n"); |
| 7551 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7552 | case XML_RELAXNG_CHOICE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7553 | fprintf(output, "<choice>\n"); |
| 7554 | xmlRelaxNGDumpDefines(output, define->content); |
| 7555 | fprintf(output, "</choice>\n"); |
| 7556 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7557 | case XML_RELAXNG_GROUP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7558 | fprintf(output, "<group>\n"); |
| 7559 | xmlRelaxNGDumpDefines(output, define->content); |
| 7560 | fprintf(output, "</group>\n"); |
| 7561 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7562 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7563 | fprintf(output, "<interleave>\n"); |
| 7564 | xmlRelaxNGDumpDefines(output, define->content); |
| 7565 | fprintf(output, "</interleave>\n"); |
| 7566 | break; |
| 7567 | case XML_RELAXNG_OPTIONAL: |
| 7568 | fprintf(output, "<optional>\n"); |
| 7569 | xmlRelaxNGDumpDefines(output, define->content); |
| 7570 | fprintf(output, "</optional>\n"); |
| 7571 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7572 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7573 | fprintf(output, "<attribute>\n"); |
| 7574 | xmlRelaxNGDumpDefines(output, define->content); |
| 7575 | fprintf(output, "</attribute>\n"); |
| 7576 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7577 | case XML_RELAXNG_DEF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7578 | fprintf(output, "<define"); |
| 7579 | if (define->name != NULL) |
| 7580 | fprintf(output, " name=\"%s\"", define->name); |
| 7581 | fprintf(output, ">\n"); |
| 7582 | xmlRelaxNGDumpDefines(output, define->content); |
| 7583 | fprintf(output, "</define>\n"); |
| 7584 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7585 | case XML_RELAXNG_REF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7586 | fprintf(output, "<ref"); |
| 7587 | if (define->name != NULL) |
| 7588 | fprintf(output, " name=\"%s\"", define->name); |
| 7589 | fprintf(output, ">\n"); |
| 7590 | xmlRelaxNGDumpDefines(output, define->content); |
| 7591 | fprintf(output, "</ref>\n"); |
| 7592 | break; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7593 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7594 | fprintf(output, "<parentRef"); |
| 7595 | if (define->name != NULL) |
| 7596 | fprintf(output, " name=\"%s\"", define->name); |
| 7597 | fprintf(output, ">\n"); |
| 7598 | xmlRelaxNGDumpDefines(output, define->content); |
| 7599 | fprintf(output, "</parentRef>\n"); |
| 7600 | break; |
| 7601 | case XML_RELAXNG_EXTERNALREF: |
| 7602 | fprintf(output, "<externalRef>"); |
| 7603 | xmlRelaxNGDumpDefines(output, define->content); |
| 7604 | fprintf(output, "</externalRef>\n"); |
| 7605 | break; |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 7606 | case XML_RELAXNG_DATATYPE: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7607 | case XML_RELAXNG_VALUE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7608 | TODO break; |
| 7609 | case XML_RELAXNG_START: |
| 7610 | case XML_RELAXNG_EXCEPT: |
| 7611 | case XML_RELAXNG_PARAM: |
| 7612 | TODO break; |
| 7613 | case XML_RELAXNG_NOOP: |
| 7614 | xmlRelaxNGDumpDefines(output, define->content); |
| 7615 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7616 | } |
| 7617 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7618 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7619 | /** |
| 7620 | * xmlRelaxNGDumpGrammar: |
| 7621 | * @output: the file output |
| 7622 | * @grammar: a grammar structure |
| 7623 | * @top: is this a top grammar |
| 7624 | * |
| 7625 | * Dump a RelaxNG structure back |
| 7626 | */ |
| 7627 | static void |
| 7628 | xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top) |
| 7629 | { |
| 7630 | if (grammar == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7631 | return; |
| 7632 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7633 | fprintf(output, "<grammar"); |
| 7634 | if (top) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7635 | fprintf(output, " xmlns=\"http://relaxng.org/ns/structure/1.0\""); |
| 7636 | switch (grammar->combine) { |
| 7637 | case XML_RELAXNG_COMBINE_UNDEFINED: |
| 7638 | break; |
| 7639 | case XML_RELAXNG_COMBINE_CHOICE: |
| 7640 | fprintf(output, " combine=\"choice\""); |
| 7641 | break; |
| 7642 | case XML_RELAXNG_COMBINE_INTERLEAVE: |
| 7643 | fprintf(output, " combine=\"interleave\""); |
| 7644 | break; |
| 7645 | default: |
| 7646 | fprintf(output, " <!-- invalid combine value -->"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7647 | } |
| 7648 | fprintf(output, ">\n"); |
| 7649 | if (grammar->start == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7650 | fprintf(output, " <!-- grammar had no start -->"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7651 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7652 | fprintf(output, "<start>\n"); |
| 7653 | xmlRelaxNGDumpDefine(output, grammar->start); |
| 7654 | fprintf(output, "</start>\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7655 | } |
| 7656 | /* TODO ? Dump the defines ? */ |
| 7657 | fprintf(output, "</grammar>\n"); |
| 7658 | } |
| 7659 | |
| 7660 | /** |
| 7661 | * xmlRelaxNGDump: |
| 7662 | * @output: the file output |
| 7663 | * @schema: a schema structure |
| 7664 | * |
| 7665 | * Dump a RelaxNG structure back |
| 7666 | */ |
| 7667 | void |
| 7668 | xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema) |
| 7669 | { |
| 7670 | if (schema == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7671 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
| 7672 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7673 | } |
| 7674 | fprintf(output, "RelaxNG: "); |
| 7675 | if (schema->doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7676 | fprintf(output, "no document\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7677 | } else if (schema->doc->URL != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7678 | fprintf(output, "%s\n", schema->doc->URL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7679 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7680 | fprintf(output, "\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7681 | } |
| 7682 | if (schema->topgrammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7683 | fprintf(output, "RelaxNG has no top grammar\n"); |
| 7684 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7685 | } |
| 7686 | xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1); |
| 7687 | } |
| 7688 | |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7689 | /** |
| 7690 | * xmlRelaxNGDumpTree: |
| 7691 | * @output: the file output |
| 7692 | * @schema: a schema structure |
| 7693 | * |
| 7694 | * Dump the transformed RelaxNG tree. |
| 7695 | */ |
| 7696 | void |
| 7697 | xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema) |
| 7698 | { |
| 7699 | if (schema == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7700 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
| 7701 | return; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7702 | } |
| 7703 | if (schema->doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7704 | fprintf(output, "no document\n"); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7705 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7706 | xmlDocDump(output, schema->doc); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7707 | } |
| 7708 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 7709 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7710 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7711 | /************************************************************************ |
| 7712 | * * |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7713 | * Validation of compiled content * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7714 | * * |
| 7715 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7716 | static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
| 7717 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7718 | |
| 7719 | /** |
| 7720 | * xmlRelaxNGValidateCompiledCallback: |
| 7721 | * @exec: the regular expression instance |
| 7722 | * @token: the token which matched |
| 7723 | * @transdata: callback data, the define for the subelement if available |
| 7724 | @ @inputdata: callback data, the Relax NG validation context |
| 7725 | * |
| 7726 | * Handle the callback and if needed validate the element children. |
| 7727 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7728 | static void |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7729 | xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7730 | const xmlChar * token, |
| 7731 | void *transdata, void *inputdata) |
| 7732 | { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7733 | xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata; |
| 7734 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata; |
| 7735 | int ret; |
| 7736 | |
| 7737 | #ifdef DEBUG_COMPILE |
| 7738 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7739 | "Compiled callback for: '%s'\n", token); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7740 | #endif |
| 7741 | if (ctxt == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7742 | fprintf(stderr, "callback on %s missing context\n", token); |
| 7743 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7744 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7745 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7746 | } |
| 7747 | if (define == NULL) { |
| 7748 | if (token[0] == '#') |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7749 | return; |
| 7750 | fprintf(stderr, "callback on %s missing define\n", token); |
| 7751 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7752 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7753 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7754 | } |
| 7755 | if ((ctxt == NULL) || (define == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7756 | fprintf(stderr, "callback on %s missing info\n", token); |
| 7757 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7758 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7759 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7760 | } else if (define->type != XML_RELAXNG_ELEMENT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7761 | fprintf(stderr, "callback on %s define is not element\n", token); |
| 7762 | if (ctxt->errNo == XML_RELAXNG_OK) |
| 7763 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7764 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7765 | } |
| 7766 | ret = xmlRelaxNGValidateDefinition(ctxt, define); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7767 | if (ret != 0) |
| 7768 | ctxt->perr = ret; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7769 | } |
| 7770 | |
| 7771 | /** |
| 7772 | * xmlRelaxNGValidateCompiledContent: |
| 7773 | * @ctxt: the RelaxNG validation context |
| 7774 | * @regexp: the regular expression as compiled |
| 7775 | * @content: list of children to test against the regexp |
| 7776 | * |
| 7777 | * Validate the content model of an element or start using the regexp |
| 7778 | * |
| 7779 | * Returns 0 in case of success, -1 in case of error. |
| 7780 | */ |
| 7781 | static int |
| 7782 | xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7783 | xmlRegexpPtr regexp, xmlNodePtr content) |
| 7784 | { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7785 | xmlRegExecCtxtPtr exec; |
| 7786 | xmlNodePtr cur; |
Daniel Veillard | 6216360 | 2003-04-17 09:36:38 +0000 | [diff] [blame] | 7787 | int ret = 0; |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7788 | int oldperr = ctxt->perr; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7789 | |
| 7790 | if ((ctxt == NULL) || (regexp == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7791 | return (-1); |
| 7792 | exec = xmlRegNewExecCtxt(regexp, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7793 | xmlRelaxNGValidateCompiledCallback, ctxt); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7794 | ctxt->perr = 0; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7795 | cur = content; |
| 7796 | while (cur != NULL) { |
| 7797 | ctxt->state->seq = cur; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7798 | switch (cur->type) { |
| 7799 | case XML_TEXT_NODE: |
| 7800 | case XML_CDATA_SECTION_NODE: |
| 7801 | if (xmlIsBlankNode(cur)) |
| 7802 | break; |
| 7803 | ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt); |
| 7804 | if (ret < 0) { |
| 7805 | VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, |
| 7806 | cur->parent->name); |
| 7807 | } |
| 7808 | break; |
| 7809 | case XML_ELEMENT_NODE: |
| 7810 | if (cur->ns != NULL) { |
| 7811 | ret = xmlRegExecPushString2(exec, cur->name, |
| 7812 | cur->ns->href, ctxt); |
| 7813 | } else { |
| 7814 | ret = xmlRegExecPushString(exec, cur->name, ctxt); |
| 7815 | } |
| 7816 | if (ret < 0) { |
| 7817 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name); |
| 7818 | } |
| 7819 | break; |
| 7820 | default: |
| 7821 | break; |
| 7822 | } |
| 7823 | if (ret < 0) |
| 7824 | break; |
| 7825 | /* |
| 7826 | * Switch to next element |
| 7827 | */ |
| 7828 | cur = cur->next; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7829 | } |
| 7830 | ret = xmlRegExecPushString(exec, NULL, NULL); |
| 7831 | if (ret == 1) { |
| 7832 | ret = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7833 | ctxt->state->seq = NULL; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7834 | } else if (ret == 0) { |
| 7835 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7836 | * TODO: get some of the names needed to exit the current state of exec |
| 7837 | */ |
| 7838 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST ""); |
| 7839 | ret = -1; |
| 7840 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 7841 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7842 | } else { |
| 7843 | ret = -1; |
| 7844 | } |
| 7845 | xmlRegFreeExecCtxt(exec); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7846 | /* |
| 7847 | * There might be content model errors outside of the pure |
| 7848 | * regexp validation, e.g. for attribute values. |
| 7849 | */ |
| 7850 | if ((ret == 0) && (ctxt->perr != 0)) { |
| 7851 | ret = ctxt->perr; |
| 7852 | } |
| 7853 | ctxt->perr = oldperr; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7854 | return (ret); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7855 | } |
| 7856 | |
| 7857 | /************************************************************************ |
| 7858 | * * |
| 7859 | * Progressive validation of when possible * |
| 7860 | * * |
| 7861 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7862 | static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
| 7863 | xmlRelaxNGDefinePtr defines); |
| 7864 | static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 7865 | int dolog); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 7866 | static void xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7867 | |
| 7868 | /** |
| 7869 | * xmlRelaxNGElemPush: |
| 7870 | * @ctxt: the validation context |
| 7871 | * @exec: the regexp runtime for the new content model |
| 7872 | * |
| 7873 | * Push a new regexp for the current node content model on the stack |
| 7874 | * |
| 7875 | * Returns 0 in case of success and -1 in case of error. |
| 7876 | */ |
| 7877 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7878 | xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec) |
| 7879 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7880 | if (ctxt->elemTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7881 | ctxt->elemMax = 10; |
| 7882 | ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax * |
| 7883 | sizeof |
| 7884 | (xmlRegExecCtxtPtr)); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7885 | if (ctxt->elemTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7886 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 7887 | return (-1); |
| 7888 | } |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7889 | } |
| 7890 | if (ctxt->elemNr >= ctxt->elemMax) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7891 | ctxt->elemMax *= 2; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7892 | ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7893 | ctxt->elemMax * |
| 7894 | sizeof |
| 7895 | (xmlRegExecCtxtPtr)); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7896 | if (ctxt->elemTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7897 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 7898 | return (-1); |
| 7899 | } |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7900 | } |
| 7901 | ctxt->elemTab[ctxt->elemNr++] = exec; |
| 7902 | ctxt->elem = exec; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7903 | return (0); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7904 | } |
| 7905 | |
| 7906 | /** |
| 7907 | * xmlRelaxNGElemPop: |
| 7908 | * @ctxt: the validation context |
| 7909 | * |
| 7910 | * Pop the regexp of the current node content model from the stack |
| 7911 | * |
| 7912 | * Returns the exec or NULL if empty |
| 7913 | */ |
| 7914 | static xmlRegExecCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7915 | xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt) |
| 7916 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7917 | xmlRegExecCtxtPtr ret; |
| 7918 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7919 | if (ctxt->elemNr <= 0) |
| 7920 | return (NULL); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7921 | ctxt->elemNr--; |
| 7922 | ret = ctxt->elemTab[ctxt->elemNr]; |
| 7923 | ctxt->elemTab[ctxt->elemNr] = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7924 | if (ctxt->elemNr > 0) |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7925 | ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1]; |
| 7926 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7927 | ctxt->elem = NULL; |
| 7928 | return (ret); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7929 | } |
| 7930 | |
| 7931 | /** |
| 7932 | * xmlRelaxNGValidateProgressiveCallback: |
| 7933 | * @exec: the regular expression instance |
| 7934 | * @token: the token which matched |
| 7935 | * @transdata: callback data, the define for the subelement if available |
| 7936 | @ @inputdata: callback data, the Relax NG validation context |
| 7937 | * |
| 7938 | * Handle the callback and if needed validate the element children. |
| 7939 | * some of the in/out informations are passed via the context in @inputdata. |
| 7940 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7941 | static void |
| 7942 | xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec |
| 7943 | ATTRIBUTE_UNUSED, |
| 7944 | const xmlChar * token, |
| 7945 | void *transdata, void *inputdata) |
| 7946 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7947 | xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata; |
| 7948 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7949 | xmlRelaxNGValidStatePtr state, oldstate; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7950 | xmlNodePtr node = ctxt->pnode; |
Daniel Veillard | c3ca5ba | 2003-05-09 22:26:28 +0000 | [diff] [blame] | 7951 | int ret = 0, oldflags; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7952 | |
| 7953 | #ifdef DEBUG_PROGRESSIVE |
| 7954 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7955 | "Progressive callback for: '%s'\n", token); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7956 | #endif |
| 7957 | if (ctxt == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7958 | fprintf(stderr, "callback on %s missing context\n", token); |
| 7959 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7960 | } |
| 7961 | ctxt->pstate = 1; |
| 7962 | if (define == NULL) { |
| 7963 | if (token[0] == '#') |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7964 | return; |
| 7965 | fprintf(stderr, "callback on %s missing define\n", token); |
| 7966 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7967 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7968 | ctxt->pstate = -1; |
| 7969 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7970 | } |
| 7971 | if ((ctxt == NULL) || (define == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7972 | fprintf(stderr, "callback on %s missing info\n", token); |
| 7973 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7974 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7975 | ctxt->pstate = -1; |
| 7976 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7977 | } else if (define->type != XML_RELAXNG_ELEMENT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7978 | fprintf(stderr, "callback on %s define is not element\n", token); |
| 7979 | if (ctxt->errNo == XML_RELAXNG_OK) |
| 7980 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7981 | ctxt->pstate = -1; |
| 7982 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7983 | } |
| 7984 | if (node->type != XML_ELEMENT_NODE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7985 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
| 7986 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 7987 | xmlRelaxNGDumpValidError(ctxt); |
| 7988 | ctxt->pstate = -1; |
| 7989 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7990 | } |
| 7991 | if (define->contModel == NULL) { |
| 7992 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7993 | * this node cannot be validated in a streamable fashion |
| 7994 | */ |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7995 | #ifdef DEBUG_PROGRESSIVE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7996 | xmlGenericError(xmlGenericErrorContext, |
| 7997 | "Element '%s' validation is not streamable\n", |
| 7998 | token); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7999 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8000 | ctxt->pstate = 0; |
| 8001 | ctxt->pdef = define; |
| 8002 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8003 | } |
| 8004 | exec = xmlRegNewExecCtxt(define->contModel, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8005 | xmlRelaxNGValidateProgressiveCallback, ctxt); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8006 | if (exec == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8007 | ctxt->pstate = -1; |
| 8008 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8009 | } |
| 8010 | xmlRelaxNGElemPush(ctxt, exec); |
| 8011 | |
| 8012 | /* |
| 8013 | * Validate the attributes part of the content. |
| 8014 | */ |
| 8015 | state = xmlRelaxNGNewValidState(ctxt, node); |
| 8016 | if (state == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8017 | ctxt->pstate = -1; |
| 8018 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8019 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8020 | oldstate = ctxt->state; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8021 | ctxt->state = state; |
| 8022 | if (define->attrs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8023 | ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
| 8024 | if (ret != 0) { |
| 8025 | ctxt->pstate = -1; |
| 8026 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
| 8027 | } |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8028 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8029 | if (ctxt->state != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8030 | ctxt->state->seq = NULL; |
| 8031 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
| 8032 | if (ret != 0) { |
| 8033 | ctxt->pstate = -1; |
| 8034 | } |
| 8035 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8036 | } else if (ctxt->states != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8037 | int tmp = -1, i; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8038 | |
| 8039 | oldflags = ctxt->flags; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8040 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8041 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 8042 | state = ctxt->states->tabState[i]; |
| 8043 | ctxt->state = state; |
| 8044 | ctxt->state->seq = NULL; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8045 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8046 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
| 8047 | tmp = 0; |
| 8048 | break; |
| 8049 | } |
| 8050 | } |
| 8051 | if (tmp != 0) { |
| 8052 | /* |
| 8053 | * validation error, log the message for the "best" one |
| 8054 | */ |
| 8055 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8056 | xmlRelaxNGLogBestError(ctxt); |
| 8057 | } |
| 8058 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 8059 | xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[i]); |
| 8060 | } |
| 8061 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 8062 | ctxt->states = NULL; |
| 8063 | if ((ret == 0) && (tmp == -1)) |
| 8064 | ctxt->pstate = -1; |
| 8065 | ctxt->flags = oldflags; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8066 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8067 | if (ctxt->pstate == -1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8068 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 8069 | xmlRelaxNGDumpValidError(ctxt); |
| 8070 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8071 | } |
| 8072 | ctxt->state = oldstate; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8073 | } |
| 8074 | |
| 8075 | /** |
| 8076 | * xmlRelaxNGValidatePushElement: |
| 8077 | * @ctxt: the validation context |
| 8078 | * @doc: a document instance |
| 8079 | * @elem: an element instance |
| 8080 | * |
| 8081 | * Push a new element start on the RelaxNG validation stack. |
| 8082 | * |
| 8083 | * returns 1 if no validation problem was found or 0 if validating the |
| 8084 | * element requires a full node, and -1 in case of error. |
| 8085 | */ |
| 8086 | int |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8087 | xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8088 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8089 | xmlNodePtr elem) |
| 8090 | { |
| 8091 | int ret = 1; |
| 8092 | |
| 8093 | if ((ctxt == NULL) || (elem == NULL)) |
| 8094 | return (-1); |
| 8095 | |
| 8096 | #ifdef DEBUG_PROGRESSIVE |
| 8097 | xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name); |
| 8098 | #endif |
| 8099 | if (ctxt->elem == 0) { |
| 8100 | xmlRelaxNGPtr schema; |
| 8101 | xmlRelaxNGGrammarPtr grammar; |
| 8102 | xmlRegExecCtxtPtr exec; |
| 8103 | xmlRelaxNGDefinePtr define; |
| 8104 | |
| 8105 | schema = ctxt->schema; |
| 8106 | if (schema == NULL) { |
| 8107 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 8108 | return (-1); |
| 8109 | } |
| 8110 | grammar = schema->topgrammar; |
| 8111 | if ((grammar == NULL) || (grammar->start == NULL)) { |
| 8112 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 8113 | return (-1); |
| 8114 | } |
| 8115 | define = grammar->start; |
| 8116 | if (define->contModel == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8117 | ctxt->pdef = define; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8118 | return (0); |
| 8119 | } |
| 8120 | exec = xmlRegNewExecCtxt(define->contModel, |
| 8121 | xmlRelaxNGValidateProgressiveCallback, |
| 8122 | ctxt); |
| 8123 | if (exec == NULL) { |
| 8124 | return (-1); |
| 8125 | } |
| 8126 | xmlRelaxNGElemPush(ctxt, exec); |
| 8127 | } |
| 8128 | ctxt->pnode = elem; |
| 8129 | ctxt->pstate = 0; |
| 8130 | if (elem->ns != NULL) { |
| 8131 | ret = |
| 8132 | xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href, |
| 8133 | ctxt); |
| 8134 | } else { |
| 8135 | ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt); |
| 8136 | } |
| 8137 | if (ret < 0) { |
| 8138 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name); |
| 8139 | } else { |
| 8140 | if (ctxt->pstate == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8141 | ret = 0; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8142 | else if (ctxt->pstate < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8143 | ret = -1; |
| 8144 | else |
| 8145 | ret = 1; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8146 | } |
| 8147 | #ifdef DEBUG_PROGRESSIVE |
| 8148 | if (ret < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8149 | xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n", |
| 8150 | elem->name); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8151 | #endif |
| 8152 | return (ret); |
| 8153 | } |
| 8154 | |
| 8155 | /** |
| 8156 | * xmlRelaxNGValidatePushCData: |
| 8157 | * @ctxt: the RelaxNG validation context |
| 8158 | * @data: some character data read |
| 8159 | * @len: the lenght of the data |
| 8160 | * |
| 8161 | * check the CData parsed for validation in the current stack |
| 8162 | * |
| 8163 | * returns 1 if no validation problem was found or -1 otherwise |
| 8164 | */ |
| 8165 | int |
| 8166 | xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8167 | const xmlChar * data, int len ATTRIBUTE_UNUSED) |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8168 | { |
| 8169 | int ret = 1; |
| 8170 | |
| 8171 | if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL)) |
| 8172 | return (-1); |
| 8173 | |
| 8174 | #ifdef DEBUG_PROGRESSIVE |
| 8175 | xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len); |
| 8176 | #endif |
| 8177 | |
| 8178 | while (*data != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8179 | if (!IS_BLANK_CH(*data)) |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8180 | break; |
| 8181 | data++; |
| 8182 | } |
| 8183 | if (*data == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8184 | return (1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8185 | |
| 8186 | ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt); |
| 8187 | if (ret < 0) { |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8188 | VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO "); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8189 | #ifdef DEBUG_PROGRESSIVE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8190 | xmlGenericError(xmlGenericErrorContext, "CDATA failed\n"); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8191 | #endif |
| 8192 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8193 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8194 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8195 | return (1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8196 | } |
| 8197 | |
| 8198 | /** |
| 8199 | * xmlRelaxNGValidatePopElement: |
| 8200 | * @ctxt: the RelaxNG validation context |
| 8201 | * @doc: a document instance |
| 8202 | * @elem: an element instance |
| 8203 | * |
| 8204 | * Pop the element end from the RelaxNG validation stack. |
| 8205 | * |
| 8206 | * returns 1 if no validation problem was found or 0 otherwise |
| 8207 | */ |
| 8208 | int |
| 8209 | xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8210 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8211 | xmlNodePtr elem) |
| 8212 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8213 | int ret; |
| 8214 | xmlRegExecCtxtPtr exec; |
| 8215 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8216 | if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL)) |
| 8217 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8218 | #ifdef DEBUG_PROGRESSIVE |
| 8219 | xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name); |
| 8220 | #endif |
| 8221 | /* |
| 8222 | * verify that we reached a terminal state of the content model. |
| 8223 | */ |
| 8224 | exec = xmlRelaxNGElemPop(ctxt); |
| 8225 | ret = xmlRegExecPushString(exec, NULL, NULL); |
| 8226 | if (ret == 0) { |
| 8227 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8228 | * TODO: get some of the names needed to exit the current state of exec |
| 8229 | */ |
| 8230 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST ""); |
| 8231 | ret = -1; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8232 | } else if (ret < 0) { |
| 8233 | ret = -1; |
| 8234 | } else { |
| 8235 | ret = 1; |
| 8236 | } |
| 8237 | xmlRegFreeExecCtxt(exec); |
| 8238 | #ifdef DEBUG_PROGRESSIVE |
| 8239 | if (ret < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8240 | xmlGenericError(xmlGenericErrorContext, "PopElem %s failed\n", |
| 8241 | elem->name); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8242 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8243 | return (ret); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8244 | } |
| 8245 | |
| 8246 | /** |
| 8247 | * xmlRelaxNGValidateFullElement: |
| 8248 | * @ctxt: the validation context |
| 8249 | * @doc: a document instance |
| 8250 | * @elem: an element instance |
| 8251 | * |
| 8252 | * Validate a full subtree when xmlRelaxNGValidatePushElement() returned |
| 8253 | * 0 and the content of the node has been expanded. |
| 8254 | * |
| 8255 | * returns 1 if no validation problem was found or -1 in case of error. |
| 8256 | */ |
| 8257 | int |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8258 | xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8259 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8260 | xmlNodePtr elem) |
| 8261 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8262 | int ret; |
| 8263 | xmlRelaxNGValidStatePtr state; |
| 8264 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8265 | if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL)) |
| 8266 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8267 | #ifdef DEBUG_PROGRESSIVE |
| 8268 | xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name); |
| 8269 | #endif |
| 8270 | state = xmlRelaxNGNewValidState(ctxt, elem->parent); |
| 8271 | if (state == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8272 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8273 | } |
| 8274 | state->seq = elem; |
| 8275 | ctxt->state = state; |
| 8276 | ctxt->errNo = XML_RELAXNG_OK; |
| 8277 | ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8278 | if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK)) |
| 8279 | ret = -1; |
| 8280 | else |
| 8281 | ret = 1; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8282 | xmlRelaxNGFreeValidState(ctxt, state); |
| 8283 | ctxt->state = NULL; |
| 8284 | #ifdef DEBUG_PROGRESSIVE |
| 8285 | if (ret < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8286 | xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n", |
| 8287 | elem->name); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8288 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8289 | return (ret); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8290 | } |
| 8291 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 8292 | /************************************************************************ |
| 8293 | * * |
| 8294 | * Generic interpreted validation implementation * |
| 8295 | * * |
| 8296 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8297 | static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
| 8298 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8299 | |
| 8300 | /** |
| 8301 | * xmlRelaxNGSkipIgnored: |
| 8302 | * @ctxt: a schema validation context |
| 8303 | * @node: the top node. |
| 8304 | * |
| 8305 | * Skip ignorable nodes in that context |
| 8306 | * |
| 8307 | * Returns the new sibling or NULL in case of error. |
| 8308 | */ |
| 8309 | static xmlNodePtr |
| 8310 | xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8311 | xmlNodePtr node) |
| 8312 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8313 | /* |
| 8314 | * TODO complete and handle entities |
| 8315 | */ |
| 8316 | while ((node != NULL) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8317 | ((node->type == XML_COMMENT_NODE) || |
| 8318 | (node->type == XML_PI_NODE) || |
William M. Brack | 7217c86 | 2004-03-15 02:43:56 +0000 | [diff] [blame] | 8319 | (node->type == XML_XINCLUDE_START) || |
| 8320 | (node->type == XML_XINCLUDE_END) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8321 | (((node->type == XML_TEXT_NODE) || |
| 8322 | (node->type == XML_CDATA_SECTION_NODE)) && |
| 8323 | ((ctxt->flags & FLAGS_MIXED_CONTENT) || |
| 8324 | (IS_BLANK_NODE(node)))))) { |
| 8325 | node = node->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8326 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8327 | return (node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8328 | } |
| 8329 | |
| 8330 | /** |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8331 | * xmlRelaxNGNormalize: |
| 8332 | * @ctxt: a schema validation context |
| 8333 | * @str: the string to normalize |
| 8334 | * |
| 8335 | * Implements the normalizeWhiteSpace( s ) function from |
| 8336 | * section 6.2.9 of the spec |
| 8337 | * |
| 8338 | * Returns the new string or NULL in case of error. |
| 8339 | */ |
| 8340 | static xmlChar * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8341 | xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar * str) |
| 8342 | { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8343 | xmlChar *ret, *p; |
| 8344 | const xmlChar *tmp; |
| 8345 | int len; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8346 | |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8347 | if (str == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8348 | return (NULL); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8349 | tmp = str; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8350 | while (*tmp != 0) |
| 8351 | tmp++; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8352 | len = tmp - str; |
| 8353 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 8354 | ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar)); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8355 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8356 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 8357 | return (NULL); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8358 | } |
| 8359 | p = ret; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8360 | while (IS_BLANK_CH(*str)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8361 | str++; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8362 | while (*str != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8363 | if (IS_BLANK_CH(*str)) { |
| 8364 | while (IS_BLANK_CH(*str)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8365 | str++; |
| 8366 | if (*str == 0) |
| 8367 | break; |
| 8368 | *p++ = ' '; |
| 8369 | } else |
| 8370 | *p++ = *str++; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8371 | } |
| 8372 | *p = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8373 | return (ret); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8374 | } |
| 8375 | |
| 8376 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8377 | * xmlRelaxNGValidateDatatype: |
| 8378 | * @ctxt: a Relax-NG validation context |
| 8379 | * @value: the string value |
| 8380 | * @type: the datatype definition |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8381 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8382 | * |
| 8383 | * Validate the given value against the dataype |
| 8384 | * |
| 8385 | * Returns 0 if the validation succeeded or an error code. |
| 8386 | */ |
| 8387 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8388 | xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt, |
| 8389 | const xmlChar * value, |
| 8390 | xmlRelaxNGDefinePtr define, xmlNodePtr node) |
| 8391 | { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8392 | int ret, tmp; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8393 | xmlRelaxNGTypeLibraryPtr lib; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8394 | void *result = NULL; |
| 8395 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8396 | |
| 8397 | if ((define == NULL) || (define->data == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8398 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8399 | } |
| 8400 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8401 | if (lib->check != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8402 | if ((define->attrs != NULL) && |
| 8403 | (define->attrs->type == XML_RELAXNG_PARAM)) { |
| 8404 | ret = |
| 8405 | lib->check(lib->data, define->name, value, &result, node); |
| 8406 | } else { |
| 8407 | ret = lib->check(lib->data, define->name, value, NULL, node); |
| 8408 | } |
| 8409 | } else |
| 8410 | ret = -1; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8411 | if (ret < 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8412 | VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name); |
| 8413 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
| 8414 | lib->freef(lib->data, result); |
| 8415 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8416 | } else if (ret == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8417 | ret = 0; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8418 | } else if (ret == 2) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8419 | VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8420 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8421 | VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value); |
| 8422 | ret = -1; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8423 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8424 | cur = define->attrs; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8425 | while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8426 | if (lib->facet != NULL) { |
| 8427 | tmp = lib->facet(lib->data, define->name, cur->name, |
| 8428 | cur->value, value, result); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8429 | if (tmp != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8430 | ret = -1; |
| 8431 | } |
| 8432 | cur = cur->next; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8433 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8434 | if ((ret == 0) && (define->content != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8435 | const xmlChar *oldvalue, *oldendvalue; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8436 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8437 | oldvalue = ctxt->state->value; |
| 8438 | oldendvalue = ctxt->state->endvalue; |
| 8439 | ctxt->state->value = (xmlChar *) value; |
| 8440 | ctxt->state->endvalue = NULL; |
| 8441 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 8442 | ctxt->state->value = (xmlChar *) oldvalue; |
| 8443 | ctxt->state->endvalue = (xmlChar *) oldendvalue; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8444 | } |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8445 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8446 | lib->freef(lib->data, result); |
| 8447 | return (ret); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8448 | } |
| 8449 | |
| 8450 | /** |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8451 | * xmlRelaxNGNextValue: |
| 8452 | * @ctxt: a Relax-NG validation context |
| 8453 | * |
| 8454 | * Skip to the next value when validating within a list |
| 8455 | * |
| 8456 | * Returns 0 if the operation succeeded or an error code. |
| 8457 | */ |
| 8458 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8459 | xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt) |
| 8460 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8461 | xmlChar *cur; |
| 8462 | |
| 8463 | cur = ctxt->state->value; |
| 8464 | if ((cur == NULL) || (ctxt->state->endvalue == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8465 | ctxt->state->value = NULL; |
| 8466 | ctxt->state->endvalue = NULL; |
| 8467 | return (0); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8468 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8469 | while (*cur != 0) |
| 8470 | cur++; |
| 8471 | while ((cur != ctxt->state->endvalue) && (*cur == 0)) |
| 8472 | cur++; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8473 | if (cur == ctxt->state->endvalue) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8474 | ctxt->state->value = NULL; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8475 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8476 | ctxt->state->value = cur; |
| 8477 | return (0); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8478 | } |
| 8479 | |
| 8480 | /** |
| 8481 | * xmlRelaxNGValidateValueList: |
| 8482 | * @ctxt: a Relax-NG validation context |
| 8483 | * @defines: the list of definitions to verify |
| 8484 | * |
| 8485 | * Validate the given set of definitions for the current value |
| 8486 | * |
| 8487 | * Returns 0 if the validation succeeded or an error code. |
| 8488 | */ |
| 8489 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8490 | xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt, |
| 8491 | xmlRelaxNGDefinePtr defines) |
| 8492 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8493 | int ret = 0; |
| 8494 | |
| 8495 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8496 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
| 8497 | if (ret != 0) |
| 8498 | break; |
| 8499 | defines = defines->next; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8500 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8501 | return (ret); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8502 | } |
| 8503 | |
| 8504 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8505 | * xmlRelaxNGValidateValue: |
| 8506 | * @ctxt: a Relax-NG validation context |
| 8507 | * @define: the definition to verify |
| 8508 | * |
| 8509 | * Validate the given definition for the current value |
| 8510 | * |
| 8511 | * Returns 0 if the validation succeeded or an error code. |
| 8512 | */ |
| 8513 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8514 | xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
| 8515 | xmlRelaxNGDefinePtr define) |
| 8516 | { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8517 | int ret = 0, oldflags; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8518 | xmlChar *value; |
| 8519 | |
| 8520 | value = ctxt->state->value; |
| 8521 | switch (define->type) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8522 | case XML_RELAXNG_EMPTY:{ |
| 8523 | if ((value != NULL) && (value[0] != 0)) { |
| 8524 | int idx = 0; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 8525 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8526 | while (IS_BLANK_CH(value[idx])) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8527 | idx++; |
| 8528 | if (value[idx] != 0) |
| 8529 | ret = -1; |
| 8530 | } |
| 8531 | break; |
| 8532 | } |
| 8533 | case XML_RELAXNG_TEXT: |
| 8534 | break; |
| 8535 | case XML_RELAXNG_VALUE:{ |
| 8536 | if (!xmlStrEqual(value, define->value)) { |
| 8537 | if (define->name != NULL) { |
| 8538 | xmlRelaxNGTypeLibraryPtr lib; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8539 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8540 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
| 8541 | if ((lib != NULL) && (lib->comp != NULL)) { |
| 8542 | ret = lib->comp(lib->data, define->name, |
| 8543 | define->value, define->node, |
| 8544 | (void *) define->attrs, |
| 8545 | value, ctxt->state->node); |
| 8546 | } else |
| 8547 | ret = -1; |
| 8548 | if (ret < 0) { |
| 8549 | VALID_ERR2(XML_RELAXNG_ERR_TYPECMP, |
| 8550 | define->name); |
| 8551 | return (-1); |
| 8552 | } else if (ret == 1) { |
| 8553 | ret = 0; |
| 8554 | } else { |
| 8555 | ret = -1; |
| 8556 | } |
| 8557 | } else { |
| 8558 | xmlChar *nval, *nvalue; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8559 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8560 | /* |
| 8561 | * TODO: trivial optimizations are possible by |
| 8562 | * computing at compile-time |
| 8563 | */ |
| 8564 | nval = xmlRelaxNGNormalize(ctxt, define->value); |
| 8565 | nvalue = xmlRelaxNGNormalize(ctxt, value); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8566 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8567 | if ((nval == NULL) || (nvalue == NULL) || |
| 8568 | (!xmlStrEqual(nval, nvalue))) |
| 8569 | ret = -1; |
| 8570 | if (nval != NULL) |
| 8571 | xmlFree(nval); |
| 8572 | if (nvalue != NULL) |
| 8573 | xmlFree(nvalue); |
| 8574 | } |
| 8575 | } |
| 8576 | if (ret == 0) |
| 8577 | xmlRelaxNGNextValue(ctxt); |
| 8578 | break; |
| 8579 | } |
| 8580 | case XML_RELAXNG_DATATYPE:{ |
| 8581 | ret = xmlRelaxNGValidateDatatype(ctxt, value, define, |
| 8582 | ctxt->state->seq); |
| 8583 | if (ret == 0) |
| 8584 | xmlRelaxNGNextValue(ctxt); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8585 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8586 | break; |
| 8587 | } |
| 8588 | case XML_RELAXNG_CHOICE:{ |
| 8589 | xmlRelaxNGDefinePtr list = define->content; |
| 8590 | xmlChar *oldvalue; |
| 8591 | |
| 8592 | oldflags = ctxt->flags; |
| 8593 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8594 | |
| 8595 | oldvalue = ctxt->state->value; |
| 8596 | while (list != NULL) { |
| 8597 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8598 | if (ret == 0) { |
| 8599 | break; |
| 8600 | } |
| 8601 | ctxt->state->value = oldvalue; |
| 8602 | list = list->next; |
| 8603 | } |
| 8604 | ctxt->flags = oldflags; |
| 8605 | if (ret != 0) { |
| 8606 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8607 | xmlRelaxNGDumpValidError(ctxt); |
| 8608 | } else { |
| 8609 | if (ctxt->errNr > 0) |
| 8610 | xmlRelaxNGPopErrors(ctxt, 0); |
| 8611 | } |
| 8612 | if (ret == 0) |
| 8613 | xmlRelaxNGNextValue(ctxt); |
| 8614 | break; |
| 8615 | } |
| 8616 | case XML_RELAXNG_LIST:{ |
| 8617 | xmlRelaxNGDefinePtr list = define->content; |
| 8618 | xmlChar *oldvalue, *oldend, *val, *cur; |
| 8619 | |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8620 | #ifdef DEBUG_LIST |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8621 | int nb_values = 0; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8622 | #endif |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8623 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8624 | oldvalue = ctxt->state->value; |
| 8625 | oldend = ctxt->state->endvalue; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8626 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8627 | val = xmlStrdup(oldvalue); |
| 8628 | if (val == NULL) { |
| 8629 | val = xmlStrdup(BAD_CAST ""); |
| 8630 | } |
| 8631 | if (val == NULL) { |
| 8632 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 8633 | return (-1); |
| 8634 | } |
| 8635 | cur = val; |
| 8636 | while (*cur != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8637 | if (IS_BLANK_CH(*cur)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8638 | *cur = 0; |
| 8639 | cur++; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8640 | #ifdef DEBUG_LIST |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8641 | nb_values++; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8642 | #endif |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8643 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8644 | *cur++ = 0; |
| 8645 | } else |
| 8646 | cur++; |
| 8647 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8648 | #ifdef DEBUG_LIST |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8649 | xmlGenericError(xmlGenericErrorContext, |
| 8650 | "list value: '%s' found %d items\n", |
| 8651 | oldvalue, nb_values); |
| 8652 | nb_values = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8653 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8654 | ctxt->state->endvalue = cur; |
| 8655 | cur = val; |
| 8656 | while ((*cur == 0) && (cur != ctxt->state->endvalue)) |
| 8657 | cur++; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8658 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8659 | ctxt->state->value = cur; |
| 8660 | |
| 8661 | while (list != NULL) { |
| 8662 | if (ctxt->state->value == ctxt->state->endvalue) |
| 8663 | ctxt->state->value = NULL; |
| 8664 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8665 | if (ret != 0) { |
| 8666 | #ifdef DEBUG_LIST |
| 8667 | xmlGenericError(xmlGenericErrorContext, |
| 8668 | "Failed to validate value: '%s' with %d rule\n", |
| 8669 | ctxt->state->value, nb_values); |
| 8670 | #endif |
| 8671 | break; |
| 8672 | } |
| 8673 | #ifdef DEBUG_LIST |
| 8674 | nb_values++; |
| 8675 | #endif |
| 8676 | list = list->next; |
| 8677 | } |
| 8678 | |
| 8679 | if ((ret == 0) && (ctxt->state->value != NULL) && |
| 8680 | (ctxt->state->value != ctxt->state->endvalue)) { |
| 8681 | VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA, |
| 8682 | ctxt->state->value); |
| 8683 | ret = -1; |
| 8684 | } |
| 8685 | xmlFree(val); |
| 8686 | ctxt->state->value = oldvalue; |
| 8687 | ctxt->state->endvalue = oldend; |
| 8688 | break; |
| 8689 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8690 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8691 | ret = xmlRelaxNGValidateValueList(ctxt, define->content); |
| 8692 | if (ret != 0) { |
| 8693 | break; |
| 8694 | } |
| 8695 | /* no break on purpose */ |
| 8696 | case XML_RELAXNG_ZEROORMORE:{ |
| 8697 | xmlChar *cur, *temp; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8698 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8699 | oldflags = ctxt->flags; |
| 8700 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8701 | cur = ctxt->state->value; |
| 8702 | temp = NULL; |
| 8703 | while ((cur != NULL) && (cur != ctxt->state->endvalue) && |
| 8704 | (temp != cur)) { |
| 8705 | temp = cur; |
| 8706 | ret = |
| 8707 | xmlRelaxNGValidateValueList(ctxt, define->content); |
| 8708 | if (ret != 0) { |
| 8709 | ctxt->state->value = temp; |
| 8710 | ret = 0; |
| 8711 | break; |
| 8712 | } |
| 8713 | cur = ctxt->state->value; |
| 8714 | } |
| 8715 | ctxt->flags = oldflags; |
| 8716 | if (ret != 0) { |
| 8717 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8718 | xmlRelaxNGDumpValidError(ctxt); |
| 8719 | } else { |
| 8720 | if (ctxt->errNr > 0) |
| 8721 | xmlRelaxNGPopErrors(ctxt, 0); |
| 8722 | } |
| 8723 | break; |
| 8724 | } |
| 8725 | case XML_RELAXNG_EXCEPT:{ |
| 8726 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8727 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8728 | list = define->content; |
| 8729 | while (list != NULL) { |
| 8730 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8731 | if (ret == 0) { |
| 8732 | ret = -1; |
| 8733 | break; |
| 8734 | } else |
| 8735 | ret = 0; |
| 8736 | list = list->next; |
| 8737 | } |
| 8738 | break; |
| 8739 | } |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 8740 | case XML_RELAXNG_DEF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8741 | case XML_RELAXNG_GROUP:{ |
| 8742 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8743 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8744 | list = define->content; |
| 8745 | while (list != NULL) { |
| 8746 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8747 | if (ret != 0) { |
| 8748 | ret = -1; |
| 8749 | break; |
| 8750 | } else |
| 8751 | ret = 0; |
| 8752 | list = list->next; |
| 8753 | } |
| 8754 | break; |
| 8755 | } |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 8756 | case XML_RELAXNG_REF: |
| 8757 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8758 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 8759 | break; |
| 8760 | default: |
| 8761 | TODO ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8762 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8763 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8764 | } |
| 8765 | |
| 8766 | /** |
| 8767 | * xmlRelaxNGValidateValueContent: |
| 8768 | * @ctxt: a Relax-NG validation context |
| 8769 | * @defines: the list of definitions to verify |
| 8770 | * |
| 8771 | * Validate the given definitions for the current value |
| 8772 | * |
| 8773 | * Returns 0 if the validation succeeded or an error code. |
| 8774 | */ |
| 8775 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8776 | xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt, |
| 8777 | xmlRelaxNGDefinePtr defines) |
| 8778 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8779 | int ret = 0; |
| 8780 | |
| 8781 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8782 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
| 8783 | if (ret != 0) |
| 8784 | break; |
| 8785 | defines = defines->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8786 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8787 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8788 | } |
| 8789 | |
| 8790 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8791 | * xmlRelaxNGAttributeMatch: |
| 8792 | * @ctxt: a Relax-NG validation context |
| 8793 | * @define: the definition to check |
| 8794 | * @prop: the attribute |
| 8795 | * |
| 8796 | * Check if the attribute matches the definition nameClass |
| 8797 | * |
| 8798 | * Returns 1 if the attribute matches, 0 if no, or -1 in case of error |
| 8799 | */ |
| 8800 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8801 | xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 8802 | xmlRelaxNGDefinePtr define, xmlAttrPtr prop) |
| 8803 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8804 | int ret; |
| 8805 | |
| 8806 | if (define->name != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8807 | if (!xmlStrEqual(define->name, prop->name)) |
| 8808 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8809 | } |
| 8810 | if (define->ns != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8811 | if (define->ns[0] == 0) { |
| 8812 | if (prop->ns != NULL) |
| 8813 | return (0); |
| 8814 | } else { |
| 8815 | if ((prop->ns == NULL) || |
| 8816 | (!xmlStrEqual(define->ns, prop->ns->href))) |
| 8817 | return (0); |
| 8818 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8819 | } |
| 8820 | if (define->nameClass == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8821 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8822 | define = define->nameClass; |
| 8823 | if (define->type == XML_RELAXNG_EXCEPT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8824 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8825 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8826 | list = define->content; |
| 8827 | while (list != NULL) { |
| 8828 | ret = xmlRelaxNGAttributeMatch(ctxt, list, prop); |
| 8829 | if (ret == 1) |
| 8830 | return (0); |
| 8831 | if (ret < 0) |
| 8832 | return (ret); |
| 8833 | list = list->next; |
| 8834 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8835 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8836 | TODO} |
| 8837 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8838 | } |
| 8839 | |
| 8840 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8841 | * xmlRelaxNGValidateAttribute: |
| 8842 | * @ctxt: a Relax-NG validation context |
| 8843 | * @define: the definition to verify |
| 8844 | * |
| 8845 | * Validate the given attribute definition for that node |
| 8846 | * |
| 8847 | * Returns 0 if the validation succeeded or an error code. |
| 8848 | */ |
| 8849 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8850 | xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt, |
| 8851 | xmlRelaxNGDefinePtr define) |
| 8852 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8853 | int ret = 0, i; |
| 8854 | xmlChar *value, *oldvalue; |
| 8855 | xmlAttrPtr prop = NULL, tmp; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8856 | xmlNodePtr oldseq; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8857 | |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 8858 | if (ctxt->state->nbAttrLeft <= 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8859 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8860 | if (define->name != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8861 | for (i = 0; i < ctxt->state->nbAttrs; i++) { |
| 8862 | tmp = ctxt->state->attrs[i]; |
| 8863 | if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) { |
| 8864 | if ((((define->ns == NULL) || (define->ns[0] == 0)) && |
| 8865 | (tmp->ns == NULL)) || |
| 8866 | ((tmp->ns != NULL) && |
| 8867 | (xmlStrEqual(define->ns, tmp->ns->href)))) { |
| 8868 | prop = tmp; |
| 8869 | break; |
| 8870 | } |
| 8871 | } |
| 8872 | } |
| 8873 | if (prop != NULL) { |
| 8874 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 8875 | oldvalue = ctxt->state->value; |
| 8876 | oldseq = ctxt->state->seq; |
| 8877 | ctxt->state->seq = (xmlNodePtr) prop; |
| 8878 | ctxt->state->value = value; |
| 8879 | ctxt->state->endvalue = NULL; |
| 8880 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
| 8881 | if (ctxt->state->value != NULL) |
| 8882 | value = ctxt->state->value; |
| 8883 | if (value != NULL) |
| 8884 | xmlFree(value); |
| 8885 | ctxt->state->value = oldvalue; |
| 8886 | ctxt->state->seq = oldseq; |
| 8887 | if (ret == 0) { |
| 8888 | /* |
| 8889 | * flag the attribute as processed |
| 8890 | */ |
| 8891 | ctxt->state->attrs[i] = NULL; |
| 8892 | ctxt->state->nbAttrLeft--; |
| 8893 | } |
| 8894 | } else { |
| 8895 | ret = -1; |
| 8896 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8897 | #ifdef DEBUG |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8898 | xmlGenericError(xmlGenericErrorContext, |
| 8899 | "xmlRelaxNGValidateAttribute(%s): %d\n", |
| 8900 | define->name, ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8901 | #endif |
| 8902 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8903 | for (i = 0; i < ctxt->state->nbAttrs; i++) { |
| 8904 | tmp = ctxt->state->attrs[i]; |
| 8905 | if ((tmp != NULL) && |
| 8906 | (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) { |
| 8907 | prop = tmp; |
| 8908 | break; |
| 8909 | } |
| 8910 | } |
| 8911 | if (prop != NULL) { |
| 8912 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 8913 | oldvalue = ctxt->state->value; |
| 8914 | oldseq = ctxt->state->seq; |
| 8915 | ctxt->state->seq = (xmlNodePtr) prop; |
| 8916 | ctxt->state->value = value; |
| 8917 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
| 8918 | if (ctxt->state->value != NULL) |
| 8919 | value = ctxt->state->value; |
| 8920 | if (value != NULL) |
| 8921 | xmlFree(value); |
| 8922 | ctxt->state->value = oldvalue; |
| 8923 | ctxt->state->seq = oldseq; |
| 8924 | if (ret == 0) { |
| 8925 | /* |
| 8926 | * flag the attribute as processed |
| 8927 | */ |
| 8928 | ctxt->state->attrs[i] = NULL; |
| 8929 | ctxt->state->nbAttrLeft--; |
| 8930 | } |
| 8931 | } else { |
| 8932 | ret = -1; |
| 8933 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8934 | #ifdef DEBUG |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8935 | if (define->ns != NULL) { |
| 8936 | xmlGenericError(xmlGenericErrorContext, |
| 8937 | "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n", |
| 8938 | define->ns, ret); |
| 8939 | } else { |
| 8940 | xmlGenericError(xmlGenericErrorContext, |
| 8941 | "xmlRelaxNGValidateAttribute(anyName): %d\n", |
| 8942 | ret); |
| 8943 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8944 | #endif |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8945 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8946 | |
| 8947 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8948 | } |
| 8949 | |
| 8950 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8951 | * xmlRelaxNGValidateAttributeList: |
| 8952 | * @ctxt: a Relax-NG validation context |
| 8953 | * @define: the list of definition to verify |
| 8954 | * |
| 8955 | * Validate the given node against the list of attribute definitions |
| 8956 | * |
| 8957 | * Returns 0 if the validation succeeded or an error code. |
| 8958 | */ |
| 8959 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8960 | xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
| 8961 | xmlRelaxNGDefinePtr defines) |
| 8962 | { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8963 | int ret = 0, res; |
| 8964 | int needmore = 0; |
| 8965 | xmlRelaxNGDefinePtr cur; |
| 8966 | |
| 8967 | cur = defines; |
| 8968 | while (cur != NULL) { |
| 8969 | if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8970 | if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0) |
| 8971 | ret = -1; |
| 8972 | } else |
| 8973 | needmore = 1; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8974 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8975 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8976 | if (!needmore) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8977 | return (ret); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8978 | cur = defines; |
| 8979 | while (cur != NULL) { |
| 8980 | if (cur->type != XML_RELAXNG_ATTRIBUTE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8981 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
| 8982 | res = xmlRelaxNGValidateDefinition(ctxt, cur); |
| 8983 | if (res < 0) |
| 8984 | ret = -1; |
| 8985 | } else { |
| 8986 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 8987 | return (-1); |
| 8988 | } |
| 8989 | if (res == -1) /* continues on -2 */ |
| 8990 | break; |
| 8991 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8992 | cur = cur->next; |
| 8993 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8994 | |
| 8995 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8996 | } |
| 8997 | |
| 8998 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8999 | * xmlRelaxNGNodeMatchesList: |
| 9000 | * @node: the node |
| 9001 | * @list: a NULL terminated array of definitions |
| 9002 | * |
| 9003 | * Check if a node can be matched by one of the definitions |
| 9004 | * |
| 9005 | * Returns 1 if matches 0 otherwise |
| 9006 | */ |
| 9007 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9008 | xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr * list) |
| 9009 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 9010 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9011 | int i = 0, tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 9012 | |
| 9013 | if ((node == NULL) || (list == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9014 | return (0); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 9015 | |
| 9016 | cur = list[i++]; |
| 9017 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9018 | if ((node->type == XML_ELEMENT_NODE) && |
| 9019 | (cur->type == XML_RELAXNG_ELEMENT)) { |
| 9020 | tmp = xmlRelaxNGElementMatch(NULL, cur, node); |
| 9021 | if (tmp == 1) |
| 9022 | return (1); |
| 9023 | } else if (((node->type == XML_TEXT_NODE) || |
| 9024 | (node->type == XML_CDATA_SECTION_NODE)) && |
| 9025 | (cur->type == XML_RELAXNG_TEXT)) { |
| 9026 | return (1); |
| 9027 | } |
| 9028 | cur = list[i++]; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 9029 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9030 | return (0); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 9031 | } |
| 9032 | |
| 9033 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9034 | * xmlRelaxNGValidateInterleave: |
| 9035 | * @ctxt: a Relax-NG validation context |
| 9036 | * @define: the definition to verify |
| 9037 | * |
| 9038 | * Validate an interleave definition for a node. |
| 9039 | * |
| 9040 | * Returns 0 if the validation succeeded or an error code. |
| 9041 | */ |
| 9042 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9043 | xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt, |
| 9044 | xmlRelaxNGDefinePtr define) |
| 9045 | { |
William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 9046 | int ret = 0, i, nbgroups; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9047 | int errNr = ctxt->errNr; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9048 | int oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9049 | |
| 9050 | xmlRelaxNGValidStatePtr oldstate; |
| 9051 | xmlRelaxNGPartitionPtr partitions; |
| 9052 | xmlRelaxNGInterleaveGroupPtr group = NULL; |
| 9053 | xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem; |
| 9054 | xmlNodePtr *list = NULL, *lasts = NULL; |
| 9055 | |
| 9056 | if (define->data != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9057 | partitions = (xmlRelaxNGPartitionPtr) define->data; |
| 9058 | nbgroups = partitions->nbgroups; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9059 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9060 | VALID_ERR(XML_RELAXNG_ERR_INTERNODATA); |
| 9061 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9062 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9063 | /* |
| 9064 | * Optimizations for MIXED |
| 9065 | */ |
| 9066 | oldflags = ctxt->flags; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 9067 | if (define->dflags & IS_MIXED) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9068 | ctxt->flags |= FLAGS_MIXED_CONTENT; |
| 9069 | if (nbgroups == 2) { |
| 9070 | /* |
| 9071 | * this is a pure <mixed> case |
| 9072 | */ |
| 9073 | if (ctxt->state != NULL) |
| 9074 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
| 9075 | ctxt->state->seq); |
| 9076 | if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT) |
| 9077 | ret = xmlRelaxNGValidateDefinition(ctxt, |
| 9078 | partitions->groups[1]-> |
| 9079 | rule); |
| 9080 | else |
| 9081 | ret = xmlRelaxNGValidateDefinition(ctxt, |
| 9082 | partitions->groups[0]-> |
| 9083 | rule); |
| 9084 | if (ret == 0) { |
| 9085 | if (ctxt->state != NULL) |
| 9086 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
| 9087 | ctxt->state-> |
| 9088 | seq); |
| 9089 | } |
| 9090 | ctxt->flags = oldflags; |
| 9091 | return (ret); |
| 9092 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9093 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9094 | |
| 9095 | /* |
| 9096 | * Build arrays to store the first and last node of the chain |
| 9097 | * pertaining to each group |
| 9098 | */ |
| 9099 | list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
| 9100 | if (list == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9101 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 9102 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9103 | } |
| 9104 | memset(list, 0, nbgroups * sizeof(xmlNodePtr)); |
| 9105 | lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
| 9106 | if (lasts == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9107 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 9108 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9109 | } |
| 9110 | memset(lasts, 0, nbgroups * sizeof(xmlNodePtr)); |
| 9111 | |
| 9112 | /* |
| 9113 | * Walk the sequence of children finding the right group and |
| 9114 | * sorting them in sequences. |
| 9115 | */ |
| 9116 | cur = ctxt->state->seq; |
| 9117 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 9118 | start = cur; |
| 9119 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9120 | ctxt->state->seq = cur; |
| 9121 | if ((partitions->triage != NULL) && |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9122 | (partitions->flags & IS_DETERMINIST)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9123 | void *tmp = NULL; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9124 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9125 | if ((cur->type == XML_TEXT_NODE) || |
| 9126 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 9127 | tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text", |
| 9128 | NULL); |
| 9129 | } else if (cur->type == XML_ELEMENT_NODE) { |
| 9130 | if (cur->ns != NULL) { |
| 9131 | tmp = xmlHashLookup2(partitions->triage, cur->name, |
| 9132 | cur->ns->href); |
| 9133 | if (tmp == NULL) |
| 9134 | tmp = xmlHashLookup2(partitions->triage, |
| 9135 | BAD_CAST "#any", |
| 9136 | cur->ns->href); |
| 9137 | } else |
| 9138 | tmp = |
| 9139 | xmlHashLookup2(partitions->triage, cur->name, |
| 9140 | NULL); |
| 9141 | if (tmp == NULL) |
| 9142 | tmp = |
| 9143 | xmlHashLookup2(partitions->triage, BAD_CAST "#any", |
| 9144 | NULL); |
| 9145 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9146 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9147 | if (tmp == NULL) { |
| 9148 | i = nbgroups; |
| 9149 | } else { |
| 9150 | i = ((long) tmp) - 1; |
| 9151 | if (partitions->flags & IS_NEEDCHECK) { |
| 9152 | group = partitions->groups[i]; |
| 9153 | if (!xmlRelaxNGNodeMatchesList(cur, group->defs)) |
| 9154 | i = nbgroups; |
| 9155 | } |
| 9156 | } |
| 9157 | } else { |
| 9158 | for (i = 0; i < nbgroups; i++) { |
| 9159 | group = partitions->groups[i]; |
| 9160 | if (group == NULL) |
| 9161 | continue; |
| 9162 | if (xmlRelaxNGNodeMatchesList(cur, group->defs)) |
| 9163 | break; |
| 9164 | } |
| 9165 | } |
| 9166 | /* |
| 9167 | * We break as soon as an element not matched is found |
| 9168 | */ |
| 9169 | if (i >= nbgroups) { |
| 9170 | break; |
| 9171 | } |
| 9172 | if (lasts[i] != NULL) { |
| 9173 | lasts[i]->next = cur; |
| 9174 | lasts[i] = cur; |
| 9175 | } else { |
| 9176 | list[i] = cur; |
| 9177 | lasts[i] = cur; |
| 9178 | } |
| 9179 | if (cur->next != NULL) |
| 9180 | lastchg = cur->next; |
| 9181 | else |
| 9182 | lastchg = cur; |
| 9183 | cur = xmlRelaxNGSkipIgnored(ctxt, cur->next); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9184 | } |
| 9185 | if (ret != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9186 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
| 9187 | ret = -1; |
| 9188 | goto done; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9189 | } |
| 9190 | lastelem = cur; |
| 9191 | oldstate = ctxt->state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9192 | for (i = 0; i < nbgroups; i++) { |
| 9193 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate); |
| 9194 | group = partitions->groups[i]; |
| 9195 | if (lasts[i] != NULL) { |
| 9196 | last = lasts[i]->next; |
| 9197 | lasts[i]->next = NULL; |
| 9198 | } |
| 9199 | ctxt->state->seq = list[i]; |
| 9200 | ret = xmlRelaxNGValidateDefinition(ctxt, group->rule); |
| 9201 | if (ret != 0) |
| 9202 | break; |
| 9203 | if (ctxt->state != NULL) { |
| 9204 | cur = ctxt->state->seq; |
| 9205 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 9206 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 9207 | oldstate = ctxt->state; |
| 9208 | ctxt->state = NULL; |
| 9209 | if (cur != NULL) { |
| 9210 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
| 9211 | ret = -1; |
| 9212 | ctxt->state = oldstate; |
| 9213 | goto done; |
| 9214 | } |
| 9215 | } else if (ctxt->states != NULL) { |
| 9216 | int j; |
| 9217 | int found = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9218 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9219 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 9220 | cur = ctxt->states->tabState[j]->seq; |
| 9221 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 9222 | if (cur == NULL) { |
| 9223 | found = 1; |
| 9224 | break; |
| 9225 | } |
| 9226 | } |
| 9227 | if (ctxt->states->nbState > 0) { |
| 9228 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 9229 | oldstate = |
| 9230 | ctxt->states->tabState[ctxt->states->nbState - 1]; |
| 9231 | } |
| 9232 | for (j = 0; j < ctxt->states->nbState - 1; j++) { |
| 9233 | xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[j]); |
| 9234 | } |
| 9235 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9236 | ctxt->states = NULL; |
| 9237 | if (found == 0) { |
| 9238 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
| 9239 | ret = -1; |
| 9240 | ctxt->state = oldstate; |
| 9241 | goto done; |
| 9242 | } |
| 9243 | } else { |
| 9244 | ret = -1; |
| 9245 | break; |
| 9246 | } |
| 9247 | if (lasts[i] != NULL) { |
| 9248 | lasts[i]->next = last; |
| 9249 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9250 | } |
| 9251 | if (ctxt->state != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9252 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9253 | ctxt->state = oldstate; |
| 9254 | ctxt->state->seq = lastelem; |
| 9255 | if (ret != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9256 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
| 9257 | ret = -1; |
| 9258 | goto done; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9259 | } |
| 9260 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9261 | done: |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9262 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9263 | /* |
| 9264 | * builds the next links chain from the prev one |
| 9265 | */ |
| 9266 | cur = lastchg; |
| 9267 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9268 | if ((cur == start) || (cur->prev == NULL)) |
| 9269 | break; |
| 9270 | cur->prev->next = cur; |
| 9271 | cur = cur->prev; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9272 | } |
| 9273 | if (ret == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9274 | if (ctxt->errNr > errNr) |
| 9275 | xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9276 | } |
| 9277 | |
| 9278 | xmlFree(list); |
| 9279 | xmlFree(lasts); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9280 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9281 | } |
| 9282 | |
| 9283 | /** |
| 9284 | * xmlRelaxNGValidateDefinitionList: |
| 9285 | * @ctxt: a Relax-NG validation context |
| 9286 | * @define: the list of definition to verify |
| 9287 | * |
| 9288 | * Validate the given node content against the (list) of definitions |
| 9289 | * |
| 9290 | * Returns 0 if the validation succeeded or an error code. |
| 9291 | */ |
| 9292 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9293 | xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt, |
| 9294 | xmlRelaxNGDefinePtr defines) |
| 9295 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9296 | int ret = 0, res; |
| 9297 | |
| 9298 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 9299 | if (defines == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9300 | VALID_ERR2(XML_RELAXNG_ERR_INTERNAL, |
| 9301 | BAD_CAST "NULL definition list"); |
| 9302 | return (-1); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 9303 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9304 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9305 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
| 9306 | res = xmlRelaxNGValidateDefinition(ctxt, defines); |
| 9307 | if (res < 0) |
| 9308 | ret = -1; |
| 9309 | } else { |
| 9310 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 9311 | return (-1); |
| 9312 | } |
| 9313 | if (res == -1) /* continues on -2 */ |
| 9314 | break; |
| 9315 | defines = defines->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9316 | } |
| 9317 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9318 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9319 | } |
| 9320 | |
| 9321 | /** |
| 9322 | * xmlRelaxNGElementMatch: |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9323 | * @ctxt: a Relax-NG validation context |
| 9324 | * @define: the definition to check |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9325 | * @elem: the element |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9326 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9327 | * Check if the element matches the definition nameClass |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9328 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9329 | * Returns 1 if the element matches, 0 if no, or -1 in case of error |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9330 | */ |
| 9331 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9332 | xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 9333 | xmlRelaxNGDefinePtr define, xmlNodePtr elem) |
| 9334 | { |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 9335 | int ret = 0, oldflags = 0; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9336 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9337 | if (define->name != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9338 | if (!xmlStrEqual(elem->name, define->name)) { |
| 9339 | VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name); |
| 9340 | return (0); |
| 9341 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9342 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9343 | if ((define->ns != NULL) && (define->ns[0] != 0)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9344 | if (elem->ns == NULL) { |
| 9345 | VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, elem->name); |
| 9346 | return (0); |
| 9347 | } else if (!xmlStrEqual(elem->ns->href, define->ns)) { |
| 9348 | VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS, |
| 9349 | elem->name, define->ns); |
| 9350 | return (0); |
| 9351 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9352 | } else if ((elem->ns != NULL) && (define->ns != NULL) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9353 | (define->name == NULL)) { |
| 9354 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, elem->name); |
| 9355 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9356 | } else if ((elem->ns != NULL) && (define->name != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9357 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, define->name); |
| 9358 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9359 | } |
| 9360 | |
| 9361 | if (define->nameClass == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9362 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9363 | |
| 9364 | define = define->nameClass; |
| 9365 | if (define->type == XML_RELAXNG_EXCEPT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9366 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9367 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9368 | if (ctxt != NULL) { |
| 9369 | oldflags = ctxt->flags; |
| 9370 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9371 | } |
| 9372 | |
| 9373 | list = define->content; |
| 9374 | while (list != NULL) { |
| 9375 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
| 9376 | if (ret == 1) { |
| 9377 | if (ctxt != NULL) |
| 9378 | ctxt->flags = oldflags; |
| 9379 | return (0); |
| 9380 | } |
| 9381 | if (ret < 0) { |
| 9382 | if (ctxt != NULL) |
| 9383 | ctxt->flags = oldflags; |
| 9384 | return (ret); |
| 9385 | } |
| 9386 | list = list->next; |
| 9387 | } |
| 9388 | ret = 1; |
| 9389 | if (ctxt != NULL) { |
| 9390 | ctxt->flags = oldflags; |
| 9391 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9392 | } else if (define->type == XML_RELAXNG_CHOICE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9393 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9394 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9395 | if (ctxt != NULL) { |
| 9396 | oldflags = ctxt->flags; |
| 9397 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9398 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9399 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9400 | list = define->nameClass; |
| 9401 | while (list != NULL) { |
| 9402 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
| 9403 | if (ret == 1) { |
| 9404 | if (ctxt != NULL) |
| 9405 | ctxt->flags = oldflags; |
| 9406 | return (1); |
| 9407 | } |
| 9408 | if (ret < 0) { |
| 9409 | if (ctxt != NULL) |
| 9410 | ctxt->flags = oldflags; |
| 9411 | return (ret); |
| 9412 | } |
| 9413 | list = list->next; |
| 9414 | } |
| 9415 | if (ctxt != NULL) { |
| 9416 | if (ret != 0) { |
| 9417 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9418 | xmlRelaxNGDumpValidError(ctxt); |
| 9419 | } else { |
| 9420 | if (ctxt->errNr > 0) |
| 9421 | xmlRelaxNGPopErrors(ctxt, 0); |
| 9422 | } |
| 9423 | } |
| 9424 | ret = 0; |
| 9425 | if (ctxt != NULL) { |
| 9426 | ctxt->flags = oldflags; |
| 9427 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9428 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9429 | TODO ret = -1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9430 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9431 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9432 | } |
| 9433 | |
| 9434 | /** |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9435 | * xmlRelaxNGBestState: |
| 9436 | * @ctxt: a Relax-NG validation context |
| 9437 | * |
| 9438 | * Find the "best" state in the ctxt->states list of states to report |
| 9439 | * errors about. I.e. a state with no element left in the child list |
| 9440 | * or the one with the less attributes left. |
| 9441 | * This is called only if a falidation error was detected |
| 9442 | * |
| 9443 | * Returns the index of the "best" state or -1 in case of error |
| 9444 | */ |
| 9445 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9446 | xmlRelaxNGBestState(xmlRelaxNGValidCtxtPtr ctxt) |
| 9447 | { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9448 | xmlRelaxNGValidStatePtr state; |
| 9449 | int i, tmp; |
| 9450 | int best = -1; |
| 9451 | int value = 1000000; |
| 9452 | |
| 9453 | if ((ctxt == NULL) || (ctxt->states == NULL) || |
| 9454 | (ctxt->states->nbState <= 0)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9455 | return (-1); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9456 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9457 | for (i = 0; i < ctxt->states->nbState; i++) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9458 | state = ctxt->states->tabState[i]; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9459 | if (state == NULL) |
| 9460 | continue; |
| 9461 | if (state->seq != NULL) { |
| 9462 | if ((best == -1) || (value > 100000)) { |
| 9463 | value = 100000; |
| 9464 | best = i; |
| 9465 | } |
| 9466 | } else { |
| 9467 | tmp = state->nbAttrLeft; |
| 9468 | if ((best == -1) || (value > tmp)) { |
| 9469 | value = tmp; |
| 9470 | best = i; |
| 9471 | } |
| 9472 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9473 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9474 | return (best); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9475 | } |
| 9476 | |
| 9477 | /** |
| 9478 | * xmlRelaxNGLogBestError: |
| 9479 | * @ctxt: a Relax-NG validation context |
| 9480 | * |
| 9481 | * Find the "best" state in the ctxt->states list of states to report |
| 9482 | * errors about and log it. |
| 9483 | */ |
| 9484 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9485 | xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt) |
| 9486 | { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9487 | int best; |
| 9488 | |
| 9489 | if ((ctxt == NULL) || (ctxt->states == NULL) || |
| 9490 | (ctxt->states->nbState <= 0)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9491 | return; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9492 | |
| 9493 | best = xmlRelaxNGBestState(ctxt); |
| 9494 | if ((best >= 0) && (best < ctxt->states->nbState)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9495 | ctxt->state = ctxt->states->tabState[best]; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9496 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9497 | xmlRelaxNGValidateElementEnd(ctxt, 1); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9498 | } |
| 9499 | } |
| 9500 | |
| 9501 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9502 | * xmlRelaxNGValidateElementEnd: |
| 9503 | * @ctxt: a Relax-NG validation context |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 9504 | * @dolog: indicate that error logging should be done |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9505 | * |
| 9506 | * Validate the end of the element, implements check that |
| 9507 | * there is nothing left not consumed in the element content |
| 9508 | * or in the attribute list. |
| 9509 | * |
| 9510 | * Returns 0 if the validation succeeded or an error code. |
| 9511 | */ |
| 9512 | static int |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 9513 | xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int dolog) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9514 | { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9515 | int i; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9516 | xmlRelaxNGValidStatePtr state; |
| 9517 | |
| 9518 | state = ctxt->state; |
| 9519 | if (state->seq != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9520 | state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq); |
| 9521 | if (state->seq != NULL) { |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 9522 | if (dolog) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9523 | VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT, |
| 9524 | state->node->name, state->seq->name); |
| 9525 | } |
| 9526 | return (-1); |
| 9527 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9528 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9529 | for (i = 0; i < state->nbAttrs; i++) { |
| 9530 | if (state->attrs[i] != NULL) { |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 9531 | if (dolog) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9532 | VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR, |
| 9533 | state->attrs[i]->name, state->node->name); |
| 9534 | } |
| 9535 | return (-1 - i); |
| 9536 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9537 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9538 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9539 | } |
| 9540 | |
| 9541 | /** |
| 9542 | * xmlRelaxNGValidateState: |
| 9543 | * @ctxt: a Relax-NG validation context |
| 9544 | * @define: the definition to verify |
| 9545 | * |
| 9546 | * Validate the current state against the definition |
| 9547 | * |
| 9548 | * Returns 0 if the validation succeeded or an error code. |
| 9549 | */ |
| 9550 | static int |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9551 | xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt, |
| 9552 | xmlRelaxNGDefinePtr define) |
| 9553 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9554 | xmlNodePtr node; |
| 9555 | int ret = 0, i, tmp, oldflags, errNr; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9556 | xmlRelaxNGValidStatePtr oldstate = NULL, state; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9557 | |
| 9558 | if (define == NULL) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9559 | VALID_ERR(XML_RELAXNG_ERR_NODEFINE); |
| 9560 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9561 | } |
| 9562 | |
| 9563 | if (ctxt->state != NULL) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9564 | node = ctxt->state->seq; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9565 | } else { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9566 | node = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9567 | } |
| 9568 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9569 | for (i = 0; i < ctxt->depth; i++) |
| 9570 | xmlGenericError(xmlGenericErrorContext, " "); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9571 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9572 | "Start validating %s ", xmlRelaxNGDefName(define)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9573 | if (define->name != NULL) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9574 | xmlGenericError(xmlGenericErrorContext, "%s ", define->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9575 | if ((node != NULL) && (node->name != NULL)) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9576 | xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9577 | else |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9578 | xmlGenericError(xmlGenericErrorContext, "\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9579 | #endif |
| 9580 | ctxt->depth++; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9581 | switch (define->type) { |
| 9582 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9583 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 9584 | ret = 0; |
| 9585 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9586 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9587 | ret = -1; |
| 9588 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9589 | case XML_RELAXNG_TEXT: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9590 | while ((node != NULL) && |
| 9591 | ((node->type == XML_TEXT_NODE) || |
| 9592 | (node->type == XML_COMMENT_NODE) || |
| 9593 | (node->type == XML_PI_NODE) || |
| 9594 | (node->type == XML_CDATA_SECTION_NODE))) |
| 9595 | node = node->next; |
| 9596 | ctxt->state->seq = node; |
| 9597 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9598 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9599 | errNr = ctxt->errNr; |
| 9600 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 9601 | if (node == NULL) { |
| 9602 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name); |
| 9603 | ret = -1; |
| 9604 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9605 | xmlRelaxNGDumpValidError(ctxt); |
| 9606 | break; |
| 9607 | } |
| 9608 | if (node->type != XML_ELEMENT_NODE) { |
| 9609 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
| 9610 | ret = -1; |
| 9611 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9612 | xmlRelaxNGDumpValidError(ctxt); |
| 9613 | break; |
| 9614 | } |
| 9615 | /* |
| 9616 | * This node was already validated successfully against |
| 9617 | * this definition. |
| 9618 | */ |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 9619 | if (node->psvi == define) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9620 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
| 9621 | if (ctxt->errNr > errNr) |
| 9622 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9623 | if (ctxt->errNr != 0) { |
| 9624 | while ((ctxt->err != NULL) && |
| 9625 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) |
| 9626 | && (xmlStrEqual(ctxt->err->arg2, node->name))) |
| 9627 | || |
| 9628 | ((ctxt->err->err == |
| 9629 | XML_RELAXNG_ERR_ELEMEXTRANS) |
| 9630 | && (xmlStrEqual(ctxt->err->arg1, node->name))) |
| 9631 | || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) |
| 9632 | || (ctxt->err->err == |
| 9633 | XML_RELAXNG_ERR_NOTELEM))) |
| 9634 | xmlRelaxNGValidErrorPop(ctxt); |
| 9635 | } |
| 9636 | break; |
| 9637 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9638 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9639 | ret = xmlRelaxNGElementMatch(ctxt, define, node); |
| 9640 | if (ret <= 0) { |
| 9641 | ret = -1; |
| 9642 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9643 | xmlRelaxNGDumpValidError(ctxt); |
| 9644 | break; |
| 9645 | } |
| 9646 | ret = 0; |
| 9647 | if (ctxt->errNr != 0) { |
| 9648 | if (ctxt->errNr > errNr) |
| 9649 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9650 | while ((ctxt->err != NULL) && |
| 9651 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) && |
| 9652 | (xmlStrEqual(ctxt->err->arg2, node->name))) || |
| 9653 | ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) && |
| 9654 | (xmlStrEqual(ctxt->err->arg1, node->name))) || |
| 9655 | (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) || |
| 9656 | (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM))) |
| 9657 | xmlRelaxNGValidErrorPop(ctxt); |
| 9658 | } |
| 9659 | errNr = ctxt->errNr; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9660 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9661 | oldflags = ctxt->flags; |
| 9662 | if (ctxt->flags & FLAGS_MIXED_CONTENT) { |
| 9663 | ctxt->flags -= FLAGS_MIXED_CONTENT; |
| 9664 | } |
| 9665 | state = xmlRelaxNGNewValidState(ctxt, node); |
| 9666 | if (state == NULL) { |
| 9667 | ret = -1; |
| 9668 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9669 | xmlRelaxNGDumpValidError(ctxt); |
| 9670 | break; |
| 9671 | } |
Daniel Veillard | 7fe1f3a | 2003-03-31 22:13:33 +0000 | [diff] [blame] | 9672 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9673 | oldstate = ctxt->state; |
| 9674 | ctxt->state = state; |
| 9675 | if (define->attrs != NULL) { |
| 9676 | tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
| 9677 | if (tmp != 0) { |
| 9678 | ret = -1; |
| 9679 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
| 9680 | } |
| 9681 | } |
| 9682 | if (define->contModel != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9683 | xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state; |
| 9684 | xmlRelaxNGStatesPtr tmpstates = ctxt->states; |
| 9685 | xmlNodePtr nseq; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9686 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9687 | nstate = xmlRelaxNGNewValidState(ctxt, node); |
| 9688 | ctxt->state = nstate; |
| 9689 | ctxt->states = NULL; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9690 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9691 | tmp = xmlRelaxNGValidateCompiledContent(ctxt, |
| 9692 | define->contModel, |
| 9693 | ctxt->state->seq); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9694 | nseq = ctxt->state->seq; |
| 9695 | ctxt->state = tmpstate; |
| 9696 | ctxt->states = tmpstates; |
| 9697 | xmlRelaxNGFreeValidState(ctxt, nstate); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9698 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9699 | #ifdef DEBUG_COMPILE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9700 | xmlGenericError(xmlGenericErrorContext, |
| 9701 | "Validating content of '%s' : %d\n", |
| 9702 | define->name, tmp); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9703 | #endif |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9704 | if (tmp != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9705 | ret = -1; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9706 | |
| 9707 | if (ctxt->states != NULL) { |
| 9708 | tmp = -1; |
| 9709 | |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9710 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9711 | state = ctxt->states->tabState[i]; |
| 9712 | ctxt->state = state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9713 | ctxt->state->seq = nseq; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9714 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9715 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9716 | tmp = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9717 | break; |
| 9718 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9719 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9720 | if (tmp != 0) { |
| 9721 | /* |
| 9722 | * validation error, log the message for the "best" one |
| 9723 | */ |
| 9724 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9725 | xmlRelaxNGLogBestError(ctxt); |
| 9726 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9727 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9728 | xmlRelaxNGFreeValidState(ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9729 | ctxt->states-> |
| 9730 | tabState[i]); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9731 | } |
| 9732 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9733 | ctxt->flags = oldflags; |
| 9734 | ctxt->states = NULL; |
| 9735 | if ((ret == 0) && (tmp == -1)) |
| 9736 | ret = -1; |
| 9737 | } else { |
| 9738 | state = ctxt->state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9739 | ctxt->state->seq = nseq; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9740 | if (ret == 0) |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9741 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9742 | xmlRelaxNGFreeValidState(ctxt, state); |
| 9743 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9744 | } else { |
| 9745 | if (define->content != NULL) { |
| 9746 | tmp = xmlRelaxNGValidateDefinitionList(ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9747 | define-> |
| 9748 | content); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9749 | if (tmp != 0) { |
| 9750 | ret = -1; |
| 9751 | if (ctxt->state == NULL) { |
| 9752 | ctxt->state = oldstate; |
| 9753 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, |
| 9754 | node->name); |
| 9755 | ctxt->state = NULL; |
| 9756 | } else { |
| 9757 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, |
| 9758 | node->name); |
| 9759 | } |
| 9760 | |
| 9761 | } |
| 9762 | } |
| 9763 | if (ctxt->states != NULL) { |
| 9764 | tmp = -1; |
| 9765 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9766 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9767 | state = ctxt->states->tabState[i]; |
| 9768 | ctxt->state = state; |
| 9769 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9770 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9771 | tmp = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9772 | break; |
| 9773 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9774 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9775 | if (tmp != 0) { |
| 9776 | /* |
| 9777 | * validation error, log the message for the "best" one |
| 9778 | */ |
| 9779 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9780 | xmlRelaxNGLogBestError(ctxt); |
| 9781 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9782 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9783 | xmlRelaxNGFreeValidState(ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9784 | ctxt->states-> |
| 9785 | tabState[i]); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9786 | } |
| 9787 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9788 | ctxt->flags = oldflags; |
| 9789 | ctxt->states = NULL; |
| 9790 | if ((ret == 0) && (tmp == -1)) |
| 9791 | ret = -1; |
| 9792 | } else { |
| 9793 | state = ctxt->state; |
| 9794 | if (ret == 0) |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9795 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9796 | xmlRelaxNGFreeValidState(ctxt, state); |
| 9797 | } |
| 9798 | } |
| 9799 | if (ret == 0) { |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 9800 | node->psvi = define; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9801 | } |
| 9802 | ctxt->flags = oldflags; |
| 9803 | ctxt->state = oldstate; |
| 9804 | if (oldstate != NULL) |
| 9805 | oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
| 9806 | if (ret != 0) { |
| 9807 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 9808 | xmlRelaxNGDumpValidError(ctxt); |
| 9809 | ret = 0; |
| 9810 | } else { |
| 9811 | ret = -2; |
| 9812 | } |
| 9813 | } else { |
| 9814 | if (ctxt->errNr > errNr) |
| 9815 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9816 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9817 | |
| 9818 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9819 | xmlGenericError(xmlGenericErrorContext, |
| 9820 | "xmlRelaxNGValidateDefinition(): validated %s : %d", |
| 9821 | node->name, ret); |
| 9822 | if (oldstate == NULL) |
| 9823 | xmlGenericError(xmlGenericErrorContext, ": no state\n"); |
| 9824 | else if (oldstate->seq == NULL) |
| 9825 | xmlGenericError(xmlGenericErrorContext, ": done\n"); |
| 9826 | else if (oldstate->seq->type == XML_ELEMENT_NODE) |
| 9827 | xmlGenericError(xmlGenericErrorContext, ": next elem %s\n", |
| 9828 | oldstate->seq->name); |
| 9829 | else |
| 9830 | xmlGenericError(xmlGenericErrorContext, ": next %s %d\n", |
| 9831 | oldstate->seq->name, oldstate->seq->type); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9832 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9833 | break; |
| 9834 | case XML_RELAXNG_OPTIONAL:{ |
| 9835 | errNr = ctxt->errNr; |
| 9836 | oldflags = ctxt->flags; |
| 9837 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9838 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
| 9839 | ret = |
| 9840 | xmlRelaxNGValidateDefinitionList(ctxt, |
| 9841 | define->content); |
| 9842 | if (ret != 0) { |
| 9843 | if (ctxt->state != NULL) |
| 9844 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 9845 | ctxt->state = oldstate; |
| 9846 | ctxt->flags = oldflags; |
| 9847 | ret = 0; |
| 9848 | if (ctxt->errNr > errNr) |
| 9849 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9850 | break; |
| 9851 | } |
| 9852 | if (ctxt->states != NULL) { |
| 9853 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
| 9854 | } else { |
| 9855 | ctxt->states = xmlRelaxNGNewStates(ctxt, 1); |
| 9856 | if (ctxt->states == NULL) { |
| 9857 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 9858 | ctxt->flags = oldflags; |
| 9859 | ret = -1; |
| 9860 | if (ctxt->errNr > errNr) |
| 9861 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9862 | break; |
| 9863 | } |
| 9864 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
| 9865 | xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state); |
| 9866 | ctxt->state = NULL; |
| 9867 | } |
| 9868 | ctxt->flags = oldflags; |
| 9869 | ret = 0; |
| 9870 | if (ctxt->errNr > errNr) |
| 9871 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9872 | break; |
| 9873 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9874 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9875 | errNr = ctxt->errNr; |
| 9876 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 9877 | if (ret != 0) { |
| 9878 | break; |
| 9879 | } |
| 9880 | if (ctxt->errNr > errNr) |
| 9881 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9882 | /* no break on purpose */ |
| 9883 | case XML_RELAXNG_ZEROORMORE:{ |
| 9884 | int progress; |
| 9885 | xmlRelaxNGStatesPtr states = NULL, res = NULL; |
| 9886 | int base, j; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9887 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9888 | errNr = ctxt->errNr; |
| 9889 | res = xmlRelaxNGNewStates(ctxt, 1); |
| 9890 | if (res == NULL) { |
| 9891 | ret = -1; |
| 9892 | break; |
| 9893 | } |
| 9894 | /* |
| 9895 | * All the input states are also exit states |
| 9896 | */ |
| 9897 | if (ctxt->state != NULL) { |
| 9898 | xmlRelaxNGAddStates(ctxt, res, |
| 9899 | xmlRelaxNGCopyValidState(ctxt, |
| 9900 | ctxt-> |
| 9901 | state)); |
| 9902 | } else { |
| 9903 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 9904 | xmlRelaxNGAddStates(ctxt, res, |
| 9905 | xmlRelaxNGCopyValidState(ctxt, |
| 9906 | ctxt-> |
| 9907 | states-> |
| 9908 | tabState |
| 9909 | [j])); |
| 9910 | } |
| 9911 | } |
| 9912 | oldflags = ctxt->flags; |
| 9913 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9914 | do { |
| 9915 | progress = 0; |
| 9916 | base = res->nbState; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9917 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9918 | if (ctxt->states != NULL) { |
| 9919 | states = ctxt->states; |
| 9920 | for (i = 0; i < states->nbState; i++) { |
| 9921 | ctxt->state = states->tabState[i]; |
| 9922 | ctxt->states = NULL; |
| 9923 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
| 9924 | define-> |
| 9925 | content); |
| 9926 | if (ret == 0) { |
| 9927 | if (ctxt->state != NULL) { |
| 9928 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 9929 | ctxt->state); |
| 9930 | ctxt->state = NULL; |
| 9931 | if (tmp == 1) |
| 9932 | progress = 1; |
| 9933 | } else if (ctxt->states != NULL) { |
| 9934 | for (j = 0; j < ctxt->states->nbState; |
| 9935 | j++) { |
| 9936 | tmp = |
| 9937 | xmlRelaxNGAddStates(ctxt, res, |
| 9938 | ctxt-> |
| 9939 | states-> |
| 9940 | tabState |
| 9941 | [j]); |
| 9942 | if (tmp == 1) |
| 9943 | progress = 1; |
| 9944 | } |
| 9945 | xmlRelaxNGFreeStates(ctxt, |
| 9946 | ctxt->states); |
| 9947 | ctxt->states = NULL; |
| 9948 | } |
| 9949 | } else { |
| 9950 | if (ctxt->state != NULL) { |
| 9951 | xmlRelaxNGFreeValidState(ctxt, |
| 9952 | ctxt->state); |
| 9953 | ctxt->state = NULL; |
| 9954 | } |
| 9955 | } |
| 9956 | } |
| 9957 | } else { |
| 9958 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
| 9959 | define-> |
| 9960 | content); |
| 9961 | if (ret != 0) { |
| 9962 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 9963 | ctxt->state = NULL; |
| 9964 | } else { |
| 9965 | base = res->nbState; |
| 9966 | if (ctxt->state != NULL) { |
| 9967 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 9968 | ctxt->state); |
| 9969 | ctxt->state = NULL; |
| 9970 | if (tmp == 1) |
| 9971 | progress = 1; |
| 9972 | } else if (ctxt->states != NULL) { |
| 9973 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 9974 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 9975 | ctxt-> |
| 9976 | states-> |
| 9977 | tabState[j]); |
| 9978 | if (tmp == 1) |
| 9979 | progress = 1; |
| 9980 | } |
| 9981 | if (states == NULL) { |
| 9982 | states = ctxt->states; |
| 9983 | } else { |
| 9984 | xmlRelaxNGFreeStates(ctxt, |
| 9985 | ctxt->states); |
| 9986 | } |
| 9987 | ctxt->states = NULL; |
| 9988 | } |
| 9989 | } |
| 9990 | } |
| 9991 | if (progress) { |
| 9992 | /* |
| 9993 | * Collect all the new nodes added at that step |
| 9994 | * and make them the new node set |
| 9995 | */ |
| 9996 | if (res->nbState - base == 1) { |
| 9997 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, |
| 9998 | res-> |
| 9999 | tabState |
| 10000 | [base]); |
| 10001 | } else { |
| 10002 | if (states == NULL) { |
| 10003 | xmlRelaxNGNewStates(ctxt, |
| 10004 | res->nbState - base); |
| 10005 | } |
| 10006 | states->nbState = 0; |
| 10007 | for (i = base; i < res->nbState; i++) |
| 10008 | xmlRelaxNGAddStates(ctxt, states, |
| 10009 | xmlRelaxNGCopyValidState |
| 10010 | (ctxt, |
| 10011 | res->tabState[i])); |
| 10012 | ctxt->states = states; |
| 10013 | } |
| 10014 | } |
| 10015 | } while (progress == 1); |
| 10016 | if (states != NULL) { |
| 10017 | xmlRelaxNGFreeStates(ctxt, states); |
| 10018 | } |
| 10019 | ctxt->states = res; |
| 10020 | ctxt->flags = oldflags; |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 10021 | #if 0 |
| 10022 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10023 | * errors may have to be propagated back... |
| 10024 | */ |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10025 | if (ctxt->errNr > errNr) |
| 10026 | xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 10027 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10028 | ret = 0; |
| 10029 | break; |
| 10030 | } |
| 10031 | case XML_RELAXNG_CHOICE:{ |
| 10032 | xmlRelaxNGDefinePtr list = NULL; |
| 10033 | xmlRelaxNGStatesPtr states = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10034 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10035 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10036 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10037 | errNr = ctxt->errNr; |
| 10038 | if ((define->dflags & IS_TRIABLE) |
| 10039 | && (define->data != NULL)) { |
| 10040 | xmlHashTablePtr triage = |
| 10041 | (xmlHashTablePtr) define->data; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 10042 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10043 | /* |
| 10044 | * Something we can optimize cleanly there is only one |
| 10045 | * possble branch out ! |
| 10046 | */ |
| 10047 | if (node == NULL) { |
| 10048 | ret = -1; |
| 10049 | break; |
| 10050 | } |
| 10051 | if ((node->type == XML_TEXT_NODE) || |
| 10052 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 10053 | list = |
| 10054 | xmlHashLookup2(triage, BAD_CAST "#text", NULL); |
| 10055 | } else if (node->type == XML_ELEMENT_NODE) { |
| 10056 | if (node->ns != NULL) { |
| 10057 | list = xmlHashLookup2(triage, node->name, |
| 10058 | node->ns->href); |
| 10059 | if (list == NULL) |
| 10060 | list = |
| 10061 | xmlHashLookup2(triage, BAD_CAST "#any", |
| 10062 | node->ns->href); |
| 10063 | } else |
| 10064 | list = |
| 10065 | xmlHashLookup2(triage, node->name, NULL); |
| 10066 | if (list == NULL) |
| 10067 | list = |
| 10068 | xmlHashLookup2(triage, BAD_CAST "#any", |
| 10069 | NULL); |
| 10070 | } |
| 10071 | if (list == NULL) { |
| 10072 | ret = -1; |
William M. Brack | 2f07606 | 2004-03-21 11:21:14 +0000 | [diff] [blame] | 10073 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, node->name); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10074 | break; |
| 10075 | } |
| 10076 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
| 10077 | if (ret == 0) { |
| 10078 | } |
| 10079 | break; |
| 10080 | } |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 10081 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10082 | list = define->content; |
| 10083 | oldflags = ctxt->flags; |
| 10084 | ctxt->flags |= FLAGS_IGNORABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10085 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10086 | while (list != NULL) { |
| 10087 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
| 10088 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
| 10089 | if (ret == 0) { |
| 10090 | if (states == NULL) { |
| 10091 | states = xmlRelaxNGNewStates(ctxt, 1); |
| 10092 | } |
| 10093 | if (ctxt->state != NULL) { |
| 10094 | xmlRelaxNGAddStates(ctxt, states, ctxt->state); |
| 10095 | } else if (ctxt->states != NULL) { |
| 10096 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 10097 | xmlRelaxNGAddStates(ctxt, states, |
| 10098 | ctxt->states-> |
| 10099 | tabState[i]); |
| 10100 | } |
| 10101 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10102 | ctxt->states = NULL; |
| 10103 | } |
| 10104 | } else { |
| 10105 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10106 | } |
| 10107 | ctxt->state = oldstate; |
| 10108 | list = list->next; |
| 10109 | } |
| 10110 | if (states != NULL) { |
| 10111 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 10112 | ctxt->states = states; |
| 10113 | ctxt->state = NULL; |
| 10114 | ret = 0; |
| 10115 | } else { |
| 10116 | ctxt->states = NULL; |
| 10117 | } |
| 10118 | ctxt->flags = oldflags; |
| 10119 | if (ret != 0) { |
| 10120 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 10121 | xmlRelaxNGDumpValidError(ctxt); |
| 10122 | } |
| 10123 | } else { |
| 10124 | if (ctxt->errNr > errNr) |
| 10125 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 10126 | } |
| 10127 | break; |
| 10128 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10129 | case XML_RELAXNG_DEF: |
| 10130 | case XML_RELAXNG_GROUP: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10131 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 10132 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10133 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10134 | ret = xmlRelaxNGValidateInterleave(ctxt, define); |
| 10135 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10136 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10137 | ret = xmlRelaxNGValidateAttribute(ctxt, define); |
| 10138 | break; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 10139 | case XML_RELAXNG_START: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10140 | case XML_RELAXNG_NOOP: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10141 | case XML_RELAXNG_REF: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10142 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 10143 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10144 | ret = xmlRelaxNGValidateDefinition(ctxt, define->content); |
| 10145 | break; |
| 10146 | case XML_RELAXNG_DATATYPE:{ |
| 10147 | xmlNodePtr child; |
| 10148 | xmlChar *content = NULL; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10149 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10150 | child = node; |
| 10151 | while (child != NULL) { |
| 10152 | if (child->type == XML_ELEMENT_NODE) { |
| 10153 | VALID_ERR2(XML_RELAXNG_ERR_DATAELEM, |
| 10154 | node->parent->name); |
| 10155 | ret = -1; |
| 10156 | break; |
| 10157 | } else if ((child->type == XML_TEXT_NODE) || |
| 10158 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 10159 | content = xmlStrcat(content, child->content); |
| 10160 | } |
| 10161 | /* TODO: handle entities ... */ |
| 10162 | child = child->next; |
| 10163 | } |
| 10164 | if (ret == -1) { |
| 10165 | if (content != NULL) |
| 10166 | xmlFree(content); |
| 10167 | break; |
| 10168 | } |
| 10169 | if (content == NULL) { |
| 10170 | content = xmlStrdup(BAD_CAST ""); |
| 10171 | if (content == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10172 | xmlRngVErrMemory(ctxt, "validating\n"); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10173 | ret = -1; |
| 10174 | break; |
| 10175 | } |
| 10176 | } |
| 10177 | ret = xmlRelaxNGValidateDatatype(ctxt, content, define, |
| 10178 | ctxt->state->seq); |
| 10179 | if (ret == -1) { |
| 10180 | VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name); |
| 10181 | } else if (ret == 0) { |
| 10182 | ctxt->state->seq = NULL; |
| 10183 | } |
| 10184 | if (content != NULL) |
| 10185 | xmlFree(content); |
| 10186 | break; |
| 10187 | } |
| 10188 | case XML_RELAXNG_VALUE:{ |
| 10189 | xmlChar *content = NULL; |
| 10190 | xmlChar *oldvalue; |
| 10191 | xmlNodePtr child; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10192 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10193 | child = node; |
| 10194 | while (child != NULL) { |
| 10195 | if (child->type == XML_ELEMENT_NODE) { |
| 10196 | VALID_ERR2(XML_RELAXNG_ERR_VALELEM, |
| 10197 | node->parent->name); |
| 10198 | ret = -1; |
| 10199 | break; |
| 10200 | } else if ((child->type == XML_TEXT_NODE) || |
| 10201 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 10202 | content = xmlStrcat(content, child->content); |
| 10203 | } |
| 10204 | /* TODO: handle entities ... */ |
| 10205 | child = child->next; |
| 10206 | } |
| 10207 | if (ret == -1) { |
| 10208 | if (content != NULL) |
| 10209 | xmlFree(content); |
| 10210 | break; |
| 10211 | } |
| 10212 | if (content == NULL) { |
| 10213 | content = xmlStrdup(BAD_CAST ""); |
| 10214 | if (content == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10215 | xmlRngVErrMemory(ctxt, "validating\n"); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10216 | ret = -1; |
| 10217 | break; |
| 10218 | } |
| 10219 | } |
| 10220 | oldvalue = ctxt->state->value; |
| 10221 | ctxt->state->value = content; |
| 10222 | ret = xmlRelaxNGValidateValue(ctxt, define); |
| 10223 | ctxt->state->value = oldvalue; |
| 10224 | if (ret == -1) { |
| 10225 | VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name); |
| 10226 | } else if (ret == 0) { |
| 10227 | ctxt->state->seq = NULL; |
| 10228 | } |
| 10229 | if (content != NULL) |
| 10230 | xmlFree(content); |
| 10231 | break; |
| 10232 | } |
| 10233 | case XML_RELAXNG_LIST:{ |
| 10234 | xmlChar *content; |
| 10235 | xmlNodePtr child; |
| 10236 | xmlChar *oldvalue, *oldendvalue; |
| 10237 | int len; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10238 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10239 | /* |
| 10240 | * Make sure it's only text nodes |
| 10241 | */ |
| 10242 | |
| 10243 | content = NULL; |
| 10244 | child = node; |
| 10245 | while (child != NULL) { |
| 10246 | if (child->type == XML_ELEMENT_NODE) { |
| 10247 | VALID_ERR2(XML_RELAXNG_ERR_LISTELEM, |
| 10248 | node->parent->name); |
| 10249 | ret = -1; |
| 10250 | break; |
| 10251 | } else if ((child->type == XML_TEXT_NODE) || |
| 10252 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 10253 | content = xmlStrcat(content, child->content); |
| 10254 | } |
| 10255 | /* TODO: handle entities ... */ |
| 10256 | child = child->next; |
| 10257 | } |
| 10258 | if (ret == -1) { |
| 10259 | if (content != NULL) |
| 10260 | xmlFree(content); |
| 10261 | break; |
| 10262 | } |
| 10263 | if (content == NULL) { |
| 10264 | content = xmlStrdup(BAD_CAST ""); |
| 10265 | if (content == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10266 | xmlRngVErrMemory(ctxt, "validating\n"); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10267 | ret = -1; |
| 10268 | break; |
| 10269 | } |
| 10270 | } |
| 10271 | len = xmlStrlen(content); |
| 10272 | oldvalue = ctxt->state->value; |
| 10273 | oldendvalue = ctxt->state->endvalue; |
| 10274 | ctxt->state->value = content; |
| 10275 | ctxt->state->endvalue = content + len; |
| 10276 | ret = xmlRelaxNGValidateValue(ctxt, define); |
| 10277 | ctxt->state->value = oldvalue; |
| 10278 | ctxt->state->endvalue = oldendvalue; |
| 10279 | if (ret == -1) { |
| 10280 | VALID_ERR(XML_RELAXNG_ERR_LIST); |
| 10281 | } else if ((ret == 0) && (node != NULL)) { |
| 10282 | ctxt->state->seq = node->next; |
| 10283 | } |
| 10284 | if (content != NULL) |
| 10285 | xmlFree(content); |
| 10286 | break; |
| 10287 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10288 | case XML_RELAXNG_EXCEPT: |
| 10289 | case XML_RELAXNG_PARAM: |
| 10290 | TODO ret = -1; |
| 10291 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10292 | } |
| 10293 | ctxt->depth--; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10294 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10295 | for (i = 0; i < ctxt->depth; i++) |
| 10296 | xmlGenericError(xmlGenericErrorContext, " "); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10297 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10298 | "Validating %s ", xmlRelaxNGDefName(define)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10299 | if (define->name != NULL) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10300 | xmlGenericError(xmlGenericErrorContext, "%s ", define->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10301 | if (ret == 0) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10302 | xmlGenericError(xmlGenericErrorContext, "suceeded\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10303 | else |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10304 | xmlGenericError(xmlGenericErrorContext, "failed\n"); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10305 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10306 | return (ret); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10307 | } |
| 10308 | |
| 10309 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10310 | * xmlRelaxNGValidateDefinition: |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10311 | * @ctxt: a Relax-NG validation context |
| 10312 | * @define: the definition to verify |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10313 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10314 | * Validate the current node lists against the definition |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10315 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10316 | * Returns 0 if the validation succeeded or an error code. |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10317 | */ |
| 10318 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10319 | xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
| 10320 | xmlRelaxNGDefinePtr define) |
| 10321 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10322 | xmlRelaxNGStatesPtr states, res; |
| 10323 | int i, j, k, ret, oldflags; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10324 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10325 | /* |
| 10326 | * We should NOT have both ctxt->state and ctxt->states |
| 10327 | */ |
| 10328 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10329 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10330 | ctxt->state = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10331 | } |
| 10332 | |
| 10333 | if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10334 | if (ctxt->states != NULL) { |
| 10335 | ctxt->state = ctxt->states->tabState[0]; |
| 10336 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10337 | ctxt->states = NULL; |
| 10338 | } |
| 10339 | ret = xmlRelaxNGValidateState(ctxt, define); |
| 10340 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10341 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10342 | ctxt->state = NULL; |
| 10343 | } |
| 10344 | if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) { |
| 10345 | ctxt->state = ctxt->states->tabState[0]; |
| 10346 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10347 | ctxt->states = NULL; |
| 10348 | } |
| 10349 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10350 | } |
| 10351 | |
| 10352 | states = ctxt->states; |
| 10353 | ctxt->states = NULL; |
| 10354 | res = NULL; |
| 10355 | j = 0; |
| 10356 | oldflags = ctxt->flags; |
| 10357 | ctxt->flags |= FLAGS_IGNORABLE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10358 | for (i = 0; i < states->nbState; i++) { |
| 10359 | ctxt->state = states->tabState[i]; |
| 10360 | ctxt->states = NULL; |
| 10361 | ret = xmlRelaxNGValidateState(ctxt, define); |
| 10362 | /* |
| 10363 | * We should NOT have both ctxt->state and ctxt->states |
| 10364 | */ |
| 10365 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10366 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10367 | ctxt->state = NULL; |
| 10368 | } |
| 10369 | if (ret == 0) { |
| 10370 | if (ctxt->states == NULL) { |
| 10371 | if (res != NULL) { |
| 10372 | /* add the state to the container */ |
| 10373 | xmlRelaxNGAddStates(ctxt, res, ctxt->state); |
| 10374 | ctxt->state = NULL; |
| 10375 | } else { |
| 10376 | /* add the state directly in states */ |
| 10377 | states->tabState[j++] = ctxt->state; |
| 10378 | ctxt->state = NULL; |
| 10379 | } |
| 10380 | } else { |
| 10381 | if (res == NULL) { |
| 10382 | /* make it the new container and copy other results */ |
| 10383 | res = ctxt->states; |
| 10384 | ctxt->states = NULL; |
| 10385 | for (k = 0; k < j; k++) |
| 10386 | xmlRelaxNGAddStates(ctxt, res, |
| 10387 | states->tabState[k]); |
| 10388 | } else { |
| 10389 | /* add all the new results to res and reff the container */ |
| 10390 | for (k = 0; k < ctxt->states->nbState; k++) |
| 10391 | xmlRelaxNGAddStates(ctxt, res, |
| 10392 | ctxt->states->tabState[k]); |
| 10393 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10394 | ctxt->states = NULL; |
| 10395 | } |
| 10396 | } |
| 10397 | } else { |
| 10398 | if (ctxt->state != NULL) { |
| 10399 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10400 | ctxt->state = NULL; |
| 10401 | } else if (ctxt->states != NULL) { |
| 10402 | for (k = 0; k < ctxt->states->nbState; k++) |
| 10403 | xmlRelaxNGFreeValidState(ctxt, |
| 10404 | ctxt->states->tabState[k]); |
| 10405 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10406 | ctxt->states = NULL; |
| 10407 | } |
| 10408 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10409 | } |
| 10410 | ctxt->flags = oldflags; |
| 10411 | if (res != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10412 | xmlRelaxNGFreeStates(ctxt, states); |
| 10413 | ctxt->states = res; |
| 10414 | ret = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10415 | } else if (j > 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10416 | states->nbState = j; |
| 10417 | ctxt->states = states; |
| 10418 | ret = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10419 | } else if (j == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10420 | ctxt->state = states->tabState[0]; |
| 10421 | xmlRelaxNGFreeStates(ctxt, states); |
| 10422 | ret = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10423 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10424 | ret = -1; |
| 10425 | xmlRelaxNGFreeStates(ctxt, states); |
| 10426 | if (ctxt->states != NULL) { |
| 10427 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10428 | ctxt->states = NULL; |
| 10429 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10430 | } |
| 10431 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10432 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10433 | ctxt->state = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10434 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10435 | return (ret); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10436 | } |
| 10437 | |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10438 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10439 | * xmlRelaxNGValidateDocument: |
| 10440 | * @ctxt: a Relax-NG validation context |
| 10441 | * @doc: the document |
| 10442 | * |
| 10443 | * Validate the given document |
| 10444 | * |
| 10445 | * Returns 0 if the validation succeeded or an error code. |
| 10446 | */ |
| 10447 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10448 | xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) |
| 10449 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10450 | int ret; |
| 10451 | xmlRelaxNGPtr schema; |
| 10452 | xmlRelaxNGGrammarPtr grammar; |
| 10453 | xmlRelaxNGValidStatePtr state; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10454 | xmlNodePtr node; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10455 | |
| 10456 | if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10457 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10458 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10459 | ctxt->errNo = XML_RELAXNG_OK; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10460 | schema = ctxt->schema; |
| 10461 | grammar = schema->topgrammar; |
| 10462 | if (grammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10463 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 10464 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10465 | } |
| 10466 | state = xmlRelaxNGNewValidState(ctxt, NULL); |
| 10467 | ctxt->state = state; |
| 10468 | ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10469 | if ((ctxt->state != NULL) && (state->seq != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10470 | state = ctxt->state; |
| 10471 | node = state->seq; |
| 10472 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 10473 | if (node != NULL) { |
| 10474 | if (ret != -1) { |
| 10475 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
| 10476 | ret = -1; |
| 10477 | } |
| 10478 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10479 | } else if (ctxt->states != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10480 | int i; |
| 10481 | int tmp = -1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10482 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10483 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 10484 | state = ctxt->states->tabState[i]; |
| 10485 | node = state->seq; |
| 10486 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 10487 | if (node == NULL) |
| 10488 | tmp = 0; |
| 10489 | xmlRelaxNGFreeValidState(ctxt, state); |
| 10490 | } |
| 10491 | if (tmp == -1) { |
| 10492 | if (ret != -1) { |
| 10493 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
| 10494 | ret = -1; |
| 10495 | } |
| 10496 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10497 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 10498 | if (ctxt->state != NULL) { |
| 10499 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10500 | ctxt->state = NULL; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 10501 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10502 | if (ret != 0) |
| 10503 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 10504 | #ifdef DEBUG |
| 10505 | else if (ctxt->errNr != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10506 | ctxt->error(ctxt->userData, |
| 10507 | "%d Extra error messages left on stack !\n", |
| 10508 | ctxt->errNr); |
| 10509 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 10510 | } |
| 10511 | #endif |
Daniel Veillard | f54cd53 | 2004-02-25 11:52:31 +0000 | [diff] [blame] | 10512 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10513 | if (ctxt->idref == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10514 | xmlValidCtxt vctxt; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10515 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10516 | memset(&vctxt, 0, sizeof(xmlValidCtxt)); |
| 10517 | vctxt.valid = 1; |
| 10518 | vctxt.error = ctxt->error; |
| 10519 | vctxt.warning = ctxt->warning; |
| 10520 | vctxt.userData = ctxt->userData; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10521 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10522 | if (xmlValidateDocumentFinal(&vctxt, doc) != 1) |
| 10523 | ret = -1; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10524 | } |
Daniel Veillard | f54cd53 | 2004-02-25 11:52:31 +0000 | [diff] [blame] | 10525 | #endif /* LIBXML_VALID_ENABLED */ |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10526 | if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10527 | ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10528 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10529 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10530 | } |
| 10531 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10532 | /************************************************************************ |
| 10533 | * * |
| 10534 | * Validation interfaces * |
| 10535 | * * |
| 10536 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10537 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10538 | /** |
| 10539 | * xmlRelaxNGNewValidCtxt: |
| 10540 | * @schema: a precompiled XML RelaxNGs |
| 10541 | * |
| 10542 | * Create an XML RelaxNGs validation context based on the given schema |
| 10543 | * |
| 10544 | * Returns the validation context or NULL in case of error |
| 10545 | */ |
| 10546 | xmlRelaxNGValidCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10547 | xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema) |
| 10548 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10549 | xmlRelaxNGValidCtxtPtr ret; |
| 10550 | |
| 10551 | ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt)); |
| 10552 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10553 | xmlRngVErrMemory(NULL, "building context\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10554 | return (NULL); |
| 10555 | } |
| 10556 | memset(ret, 0, sizeof(xmlRelaxNGValidCtxt)); |
| 10557 | ret->schema = schema; |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 10558 | ret->error = xmlGenericError; |
| 10559 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 10560 | ret->errNr = 0; |
| 10561 | ret->errMax = 0; |
| 10562 | ret->err = NULL; |
| 10563 | ret->errTab = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10564 | ret->idref = schema->idref; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10565 | ret->states = NULL; |
| 10566 | ret->freeState = NULL; |
| 10567 | ret->freeStates = NULL; |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10568 | ret->errNo = XML_RELAXNG_OK; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10569 | return (ret); |
| 10570 | } |
| 10571 | |
| 10572 | /** |
| 10573 | * xmlRelaxNGFreeValidCtxt: |
| 10574 | * @ctxt: the schema validation context |
| 10575 | * |
| 10576 | * Free the resources associated to the schema validation context |
| 10577 | */ |
| 10578 | void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10579 | xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt) |
| 10580 | { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10581 | int k; |
| 10582 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10583 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10584 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10585 | if (ctxt->states != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10586 | xmlRelaxNGFreeStates(NULL, ctxt->states); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10587 | if (ctxt->freeState != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10588 | for (k = 0; k < ctxt->freeState->nbState; k++) { |
| 10589 | xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]); |
| 10590 | } |
| 10591 | xmlRelaxNGFreeStates(NULL, ctxt->freeState); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10592 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10593 | if (ctxt->freeStates != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10594 | for (k = 0; k < ctxt->freeStatesNr; k++) { |
| 10595 | xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]); |
| 10596 | } |
| 10597 | xmlFree(ctxt->freeStates); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10598 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 10599 | if (ctxt->errTab != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10600 | xmlFree(ctxt->errTab); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 10601 | if (ctxt->elemTab != NULL) { |
| 10602 | xmlRegExecCtxtPtr exec; |
| 10603 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10604 | exec = xmlRelaxNGElemPop(ctxt); |
| 10605 | while (exec != NULL) { |
| 10606 | xmlRegFreeExecCtxt(exec); |
| 10607 | exec = xmlRelaxNGElemPop(ctxt); |
| 10608 | } |
| 10609 | xmlFree(ctxt->elemTab); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 10610 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10611 | xmlFree(ctxt); |
| 10612 | } |
| 10613 | |
| 10614 | /** |
| 10615 | * xmlRelaxNGSetValidErrors: |
| 10616 | * @ctxt: a Relax-NG validation context |
| 10617 | * @err: the error function |
| 10618 | * @warn: the warning function |
| 10619 | * @ctx: the functions context |
| 10620 | * |
| 10621 | * Set the error and warning callback informations |
| 10622 | */ |
| 10623 | void |
| 10624 | xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10625 | xmlRelaxNGValidityErrorFunc err, |
| 10626 | xmlRelaxNGValidityWarningFunc warn, void *ctx) |
| 10627 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10628 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10629 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10630 | ctxt->error = err; |
| 10631 | ctxt->warning = warn; |
| 10632 | ctxt->userData = ctx; |
| 10633 | } |
| 10634 | |
| 10635 | /** |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 10636 | * xmlRelaxNGGetValidErrors: |
| 10637 | * @ctxt: a Relax-NG validation context |
| 10638 | * @err: the error function result |
| 10639 | * @warn: the warning function result |
| 10640 | * @ctx: the functions context result |
| 10641 | * |
| 10642 | * Get the error and warning callback informations |
| 10643 | * |
| 10644 | * Returns -1 in case of error and 0 otherwise |
| 10645 | */ |
| 10646 | int |
| 10647 | xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10648 | xmlRelaxNGValidityErrorFunc * err, |
| 10649 | xmlRelaxNGValidityWarningFunc * warn, void **ctx) |
| 10650 | { |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 10651 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10652 | return (-1); |
| 10653 | if (err != NULL) |
| 10654 | *err = ctxt->error; |
| 10655 | if (warn != NULL) |
| 10656 | *warn = ctxt->warning; |
| 10657 | if (ctx != NULL) |
| 10658 | *ctx = ctxt->userData; |
| 10659 | return (0); |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 10660 | } |
| 10661 | |
| 10662 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10663 | * xmlRelaxNGValidateDoc: |
| 10664 | * @ctxt: a Relax-NG validation context |
| 10665 | * @doc: a parsed document tree |
| 10666 | * |
| 10667 | * Validate a document tree in memory. |
| 10668 | * |
| 10669 | * Returns 0 if the document is valid, a positive error code |
| 10670 | * number otherwise and -1 in case of internal or API error. |
| 10671 | */ |
| 10672 | int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10673 | xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) |
| 10674 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10675 | int ret; |
| 10676 | |
| 10677 | if ((ctxt == NULL) || (doc == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10678 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10679 | |
| 10680 | ctxt->doc = doc; |
| 10681 | |
| 10682 | ret = xmlRelaxNGValidateDocument(ctxt, doc); |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 10683 | /* |
| 10684 | * TODO: build error codes |
| 10685 | */ |
| 10686 | if (ret == -1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10687 | return (1); |
| 10688 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10689 | } |
| 10690 | |
| 10691 | #endif /* LIBXML_SCHEMAS_ENABLED */ |