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 | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 249 | }; |
| 250 | |
| 251 | #define FLAGS_IGNORABLE 1 |
| 252 | #define FLAGS_NEGATIVE 2 |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 253 | #define FLAGS_MIXED_CONTENT 4 |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 254 | |
| 255 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 256 | * xmlRelaxNGInterleaveGroup: |
| 257 | * |
| 258 | * A RelaxNGs partition set associated to lists of definitions |
| 259 | */ |
| 260 | typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup; |
| 261 | typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr; |
| 262 | struct _xmlRelaxNGInterleaveGroup { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 263 | xmlRelaxNGDefinePtr rule; /* the rule to satisfy */ |
| 264 | xmlRelaxNGDefinePtr *defs; /* the array of element definitions */ |
| 265 | xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 266 | }; |
| 267 | |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 268 | #define IS_DETERMINIST 1 |
| 269 | #define IS_NEEDCHECK 2 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 270 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 271 | /** |
| 272 | * xmlRelaxNGPartitions: |
| 273 | * |
| 274 | * A RelaxNGs partition associated to an interleave group |
| 275 | */ |
| 276 | typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition; |
| 277 | typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr; |
| 278 | struct _xmlRelaxNGPartition { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 279 | int nbgroups; /* number of groups in the partitions */ |
| 280 | xmlHashTablePtr triage; /* hash table used to direct nodes to the |
| 281 | * right group when possible */ |
| 282 | int flags; /* determinist ? */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 283 | xmlRelaxNGInterleaveGroupPtr *groups; |
| 284 | }; |
| 285 | |
| 286 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 287 | * xmlRelaxNGValidState: |
| 288 | * |
| 289 | * A RelaxNGs validation state |
| 290 | */ |
| 291 | #define MAX_ATTR 20 |
| 292 | typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState; |
| 293 | typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr; |
| 294 | struct _xmlRelaxNGValidState { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 295 | xmlNodePtr node; /* the current node */ |
| 296 | xmlNodePtr seq; /* the sequence of children left to validate */ |
| 297 | int nbAttrs; /* the number of attributes */ |
| 298 | int maxAttrs; /* the size of attrs */ |
| 299 | int nbAttrLeft; /* the number of attributes left to validate */ |
| 300 | xmlChar *value; /* the value when operating on string */ |
| 301 | xmlChar *endvalue; /* the end value when operating on string */ |
| 302 | xmlAttrPtr *attrs; /* the array of attributes */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 303 | }; |
| 304 | |
| 305 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 306 | * xmlRelaxNGStates: |
| 307 | * |
| 308 | * A RelaxNGs container for validation state |
| 309 | */ |
| 310 | typedef struct _xmlRelaxNGStates xmlRelaxNGStates; |
| 311 | typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr; |
| 312 | struct _xmlRelaxNGStates { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 313 | int nbState; /* the number of states */ |
| 314 | int maxState; /* the size of the array */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 315 | xmlRelaxNGValidStatePtr *tabState; |
| 316 | }; |
| 317 | |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 318 | #define ERROR_IS_DUP 1 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 319 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 320 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 321 | * xmlRelaxNGValidError: |
| 322 | * |
| 323 | * A RelaxNGs validation error |
| 324 | */ |
| 325 | typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError; |
| 326 | typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr; |
| 327 | struct _xmlRelaxNGValidError { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 328 | xmlRelaxNGValidErr err; /* the error number */ |
| 329 | int flags; /* flags */ |
| 330 | xmlNodePtr node; /* the current node */ |
| 331 | xmlNodePtr seq; /* the current child */ |
| 332 | const xmlChar *arg1; /* first arg */ |
| 333 | const xmlChar *arg2; /* second arg */ |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 334 | }; |
| 335 | |
| 336 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 337 | * xmlRelaxNGValidCtxt: |
| 338 | * |
| 339 | * A RelaxNGs validation context |
| 340 | */ |
| 341 | |
| 342 | struct _xmlRelaxNGValidCtxt { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 343 | void *userData; /* user specific data block */ |
| 344 | xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */ |
| 345 | xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */ |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 346 | xmlStructuredErrorFunc serror; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 347 | int nbErrors; /* number of errors in validation */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 348 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 349 | xmlRelaxNGPtr schema; /* The schema in use */ |
| 350 | xmlDocPtr doc; /* the document being validated */ |
| 351 | int flags; /* validation flags */ |
| 352 | int depth; /* validation depth */ |
| 353 | int idref; /* requires idref checking */ |
| 354 | int errNo; /* the first error found */ |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 355 | |
| 356 | /* |
| 357 | * Errors accumulated in branches may have to be stacked to be |
| 358 | * provided back when it's sure they affect validation. |
| 359 | */ |
| 360 | xmlRelaxNGValidErrorPtr err; /* Last error */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 361 | int errNr; /* Depth of the error stack */ |
| 362 | int errMax; /* Max depth of the error stack */ |
| 363 | xmlRelaxNGValidErrorPtr errTab; /* stack of errors */ |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 364 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 365 | xmlRelaxNGValidStatePtr state; /* the current validation state */ |
| 366 | xmlRelaxNGStatesPtr states; /* the accumulated state list */ |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 367 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 368 | xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */ |
| 369 | int freeStatesNr; |
| 370 | int freeStatesMax; |
| 371 | xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */ |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 372 | |
| 373 | /* |
| 374 | * This is used for "progressive" validation |
| 375 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 376 | xmlRegExecCtxtPtr elem; /* the current element regexp */ |
| 377 | int elemNr; /* the number of element validated */ |
| 378 | int elemMax; /* the max depth of elements */ |
| 379 | xmlRegExecCtxtPtr *elemTab; /* the stack of regexp runtime */ |
| 380 | int pstate; /* progressive state */ |
| 381 | xmlNodePtr pnode; /* the current node */ |
| 382 | xmlRelaxNGDefinePtr pdef; /* the non-streamable definition */ |
| 383 | int perr; /* signal error in content model |
| 384 | * outside the regexp */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 385 | }; |
| 386 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 387 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 388 | * xmlRelaxNGInclude: |
| 389 | * |
| 390 | * Structure associated to a RelaxNGs document element |
| 391 | */ |
| 392 | struct _xmlRelaxNGInclude { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 393 | xmlRelaxNGIncludePtr next; /* keep a chain of includes */ |
| 394 | xmlChar *href; /* the normalized href value */ |
| 395 | xmlDocPtr doc; /* the associated XML document */ |
| 396 | xmlRelaxNGDefinePtr content; /* the definitions */ |
| 397 | xmlRelaxNGPtr schema; /* the schema */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 398 | }; |
| 399 | |
| 400 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 401 | * xmlRelaxNGDocument: |
| 402 | * |
| 403 | * Structure associated to a RelaxNGs document element |
| 404 | */ |
| 405 | struct _xmlRelaxNGDocument { |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 406 | xmlRelaxNGDocumentPtr next; /* keep a chain of documents */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 407 | xmlChar *href; /* the normalized href value */ |
| 408 | xmlDocPtr doc; /* the associated XML document */ |
| 409 | xmlRelaxNGDefinePtr content; /* the definitions */ |
| 410 | xmlRelaxNGPtr schema; /* the schema */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 411 | }; |
| 412 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 413 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 414 | /************************************************************************ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 415 | * * |
| 416 | * Some factorized error routines * |
| 417 | * * |
| 418 | ************************************************************************/ |
| 419 | |
| 420 | /** |
| 421 | * xmlRngPErrMemory: |
| 422 | * @ctxt: an Relax-NG parser context |
| 423 | * @extra: extra informations |
| 424 | * |
| 425 | * Handle a redefinition of attribute error |
| 426 | */ |
| 427 | static void |
| 428 | xmlRngPErrMemory(xmlRelaxNGParserCtxtPtr ctxt, const char *extra) |
| 429 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 430 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 431 | xmlGenericErrorFunc channel = NULL; |
| 432 | void *data = NULL; |
| 433 | |
| 434 | if (ctxt != NULL) { |
| 435 | channel = ctxt->error; |
| 436 | data = ctxt->userData; |
| 437 | ctxt->nbErrors++; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 438 | schannel = ctxt->serror; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 439 | } |
| 440 | if (extra) |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 441 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 442 | NULL, NULL, XML_FROM_RELAXNGP, |
| 443 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra, |
| 444 | NULL, NULL, 0, 0, |
| 445 | "Memory allocation failed : %s\n", extra); |
| 446 | else |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 447 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 448 | NULL, NULL, XML_FROM_RELAXNGP, |
| 449 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL, |
| 450 | NULL, NULL, 0, 0, "Memory allocation failed\n"); |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * xmlRngVErrMemory: |
| 455 | * @ctxt: a Relax-NG validation context |
| 456 | * @extra: extra informations |
| 457 | * |
| 458 | * Handle a redefinition of attribute error |
| 459 | */ |
| 460 | static void |
| 461 | xmlRngVErrMemory(xmlRelaxNGValidCtxtPtr ctxt, const char *extra) |
| 462 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 463 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 464 | xmlGenericErrorFunc channel = NULL; |
| 465 | void *data = NULL; |
| 466 | |
| 467 | if (ctxt != NULL) { |
| 468 | channel = ctxt->error; |
| 469 | data = ctxt->userData; |
| 470 | ctxt->nbErrors++; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 471 | schannel = ctxt->serror; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 472 | } |
| 473 | if (extra) |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 474 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 475 | NULL, NULL, XML_FROM_RELAXNGV, |
| 476 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra, |
| 477 | NULL, NULL, 0, 0, |
| 478 | "Memory allocation failed : %s\n", extra); |
| 479 | else |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 480 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 481 | NULL, NULL, XML_FROM_RELAXNGV, |
| 482 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL, |
| 483 | NULL, NULL, 0, 0, "Memory allocation failed\n"); |
| 484 | } |
| 485 | |
| 486 | /** |
| 487 | * xmlRngPErr: |
| 488 | * @ctxt: a Relax-NG parser context |
| 489 | * @node: the node raising the error |
| 490 | * @error: the error code |
| 491 | * @msg: message |
| 492 | * @str1: extra info |
| 493 | * @str2: extra info |
| 494 | * |
| 495 | * Handle a Relax NG Parsing error |
| 496 | */ |
| 497 | static void |
| 498 | xmlRngPErr(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, int error, |
| 499 | const char *msg, const xmlChar * str1, const xmlChar * str2) |
| 500 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 501 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 502 | xmlGenericErrorFunc channel = NULL; |
| 503 | void *data = NULL; |
| 504 | |
| 505 | if (ctxt != NULL) { |
| 506 | channel = ctxt->error; |
| 507 | data = ctxt->userData; |
| 508 | ctxt->nbErrors++; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 509 | schannel = ctxt->serror; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 510 | } |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 511 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 512 | NULL, node, XML_FROM_RELAXNGP, |
| 513 | error, XML_ERR_ERROR, NULL, 0, |
| 514 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
| 515 | msg, str1, str2); |
| 516 | } |
| 517 | |
| 518 | /** |
| 519 | * xmlRngVErr: |
| 520 | * @ctxt: a Relax-NG validation context |
| 521 | * @node: the node raising the error |
| 522 | * @error: the error code |
| 523 | * @msg: message |
| 524 | * @str1: extra info |
| 525 | * @str2: extra info |
| 526 | * |
| 527 | * Handle a Relax NG Validation error |
| 528 | */ |
| 529 | static void |
| 530 | xmlRngVErr(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node, int error, |
| 531 | const char *msg, const xmlChar * str1, const xmlChar * str2) |
| 532 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 533 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 534 | xmlGenericErrorFunc channel = NULL; |
| 535 | void *data = NULL; |
| 536 | |
| 537 | if (ctxt != NULL) { |
| 538 | channel = ctxt->error; |
| 539 | data = ctxt->userData; |
| 540 | ctxt->nbErrors++; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 541 | schannel = ctxt->serror; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 542 | } |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 543 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 544 | NULL, node, XML_FROM_RELAXNGV, |
| 545 | error, XML_ERR_ERROR, NULL, 0, |
| 546 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
| 547 | msg, str1, str2); |
| 548 | } |
| 549 | |
| 550 | /************************************************************************ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 551 | * * |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 552 | * Preliminary type checking interfaces * |
| 553 | * * |
| 554 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 555 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 556 | /** |
| 557 | * xmlRelaxNGTypeHave: |
| 558 | * @data: data needed for the library |
| 559 | * @type: the type name |
| 560 | * @value: the value to check |
| 561 | * |
| 562 | * Function provided by a type library to check if a type is exported |
| 563 | * |
| 564 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 565 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 566 | typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar * type); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 567 | |
| 568 | /** |
| 569 | * xmlRelaxNGTypeCheck: |
| 570 | * @data: data needed for the library |
| 571 | * @type: the type name |
| 572 | * @value: the value to check |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 573 | * @result: place to store the result if needed |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 574 | * |
| 575 | * Function provided by a type library to check if a value match a type |
| 576 | * |
| 577 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 578 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 579 | typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar * type, |
| 580 | const xmlChar * value, void **result, |
| 581 | xmlNodePtr node); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 582 | |
| 583 | /** |
| 584 | * xmlRelaxNGFacetCheck: |
| 585 | * @data: data needed for the library |
| 586 | * @type: the type name |
| 587 | * @facet: the facet name |
| 588 | * @val: the facet value |
| 589 | * @strval: the string value |
| 590 | * @value: the value to check |
| 591 | * |
| 592 | * Function provided by a type library to check a value facet |
| 593 | * |
| 594 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 595 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 596 | typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar * type, |
| 597 | const xmlChar * facet, |
| 598 | const xmlChar * val, |
| 599 | const xmlChar * strval, void *value); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 600 | |
| 601 | /** |
| 602 | * xmlRelaxNGTypeFree: |
| 603 | * @data: data needed for the library |
| 604 | * @result: the value to free |
| 605 | * |
| 606 | * Function provided by a type library to free a returned result |
| 607 | */ |
| 608 | typedef void (*xmlRelaxNGTypeFree) (void *data, void *result); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 609 | |
| 610 | /** |
| 611 | * xmlRelaxNGTypeCompare: |
| 612 | * @data: data needed for the library |
| 613 | * @type: the type name |
| 614 | * @value1: the first value |
| 615 | * @value2: the second value |
| 616 | * |
| 617 | * Function provided by a type library to compare two values accordingly |
| 618 | * to a type. |
| 619 | * |
| 620 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 621 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 622 | typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar * type, |
| 623 | const xmlChar * value1, |
| 624 | xmlNodePtr ctxt1, |
| 625 | void *comp1, |
| 626 | const xmlChar * value2, |
| 627 | xmlNodePtr ctxt2); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 628 | typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary; |
| 629 | typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr; |
| 630 | struct _xmlRelaxNGTypeLibrary { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 631 | const xmlChar *namespace; /* the datatypeLibrary value */ |
| 632 | void *data; /* data needed for the library */ |
| 633 | xmlRelaxNGTypeHave have; /* the export function */ |
| 634 | xmlRelaxNGTypeCheck check; /* the checking function */ |
| 635 | xmlRelaxNGTypeCompare comp; /* the compare function */ |
| 636 | xmlRelaxNGFacetCheck facet; /* the facet check function */ |
| 637 | xmlRelaxNGTypeFree freef; /* the freeing function */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 638 | }; |
| 639 | |
| 640 | /************************************************************************ |
| 641 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 642 | * Allocation functions * |
| 643 | * * |
| 644 | ************************************************************************/ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 645 | static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar); |
| 646 | static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 647 | static void xmlRelaxNGNormExtSpace(xmlChar * value); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 648 | static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 649 | static int xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt |
| 650 | ATTRIBUTE_UNUSED, |
| 651 | xmlRelaxNGValidStatePtr state1, |
| 652 | xmlRelaxNGValidStatePtr state2); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 653 | static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 654 | xmlRelaxNGValidStatePtr state); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 655 | |
| 656 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 657 | * xmlRelaxNGFreeDocument: |
| 658 | * @docu: a document structure |
| 659 | * |
| 660 | * Deallocate a RelaxNG document structure. |
| 661 | */ |
| 662 | static void |
| 663 | xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu) |
| 664 | { |
| 665 | if (docu == NULL) |
| 666 | return; |
| 667 | |
| 668 | if (docu->href != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 669 | xmlFree(docu->href); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 670 | if (docu->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 671 | xmlFreeDoc(docu->doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 672 | if (docu->schema != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 673 | xmlRelaxNGFreeInnerSchema(docu->schema); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 674 | xmlFree(docu); |
| 675 | } |
| 676 | |
| 677 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 678 | * xmlRelaxNGFreeDocumentList: |
| 679 | * @docu: a list of document structure |
| 680 | * |
| 681 | * Deallocate a RelaxNG document structures. |
| 682 | */ |
| 683 | static void |
| 684 | xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu) |
| 685 | { |
| 686 | xmlRelaxNGDocumentPtr next; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 687 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 688 | while (docu != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 689 | next = docu->next; |
| 690 | xmlRelaxNGFreeDocument(docu); |
| 691 | docu = next; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 692 | } |
| 693 | } |
| 694 | |
| 695 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 696 | * xmlRelaxNGFreeInclude: |
| 697 | * @incl: a include structure |
| 698 | * |
| 699 | * Deallocate a RelaxNG include structure. |
| 700 | */ |
| 701 | static void |
| 702 | xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl) |
| 703 | { |
| 704 | if (incl == NULL) |
| 705 | return; |
| 706 | |
| 707 | if (incl->href != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 708 | xmlFree(incl->href); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 709 | if (incl->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 710 | xmlFreeDoc(incl->doc); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 711 | if (incl->schema != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 712 | xmlRelaxNGFree(incl->schema); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 713 | xmlFree(incl); |
| 714 | } |
| 715 | |
| 716 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 717 | * xmlRelaxNGFreeIncludeList: |
| 718 | * @incl: a include structure list |
| 719 | * |
| 720 | * Deallocate a RelaxNG include structure. |
| 721 | */ |
| 722 | static void |
| 723 | xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl) |
| 724 | { |
| 725 | xmlRelaxNGIncludePtr next; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 726 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 727 | while (incl != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 728 | next = incl->next; |
| 729 | xmlRelaxNGFreeInclude(incl); |
| 730 | incl = next; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 731 | } |
| 732 | } |
| 733 | |
| 734 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 735 | * xmlRelaxNGNewRelaxNG: |
| 736 | * @ctxt: a Relax-NG validation context (optional) |
| 737 | * |
| 738 | * Allocate a new RelaxNG structure. |
| 739 | * |
| 740 | * Returns the newly allocated structure or NULL in case or error |
| 741 | */ |
| 742 | static xmlRelaxNGPtr |
| 743 | xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt) |
| 744 | { |
| 745 | xmlRelaxNGPtr ret; |
| 746 | |
| 747 | ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG)); |
| 748 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 749 | xmlRngPErrMemory(ctxt, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 750 | return (NULL); |
| 751 | } |
| 752 | memset(ret, 0, sizeof(xmlRelaxNG)); |
| 753 | |
| 754 | return (ret); |
| 755 | } |
| 756 | |
| 757 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 758 | * xmlRelaxNGFreeInnerSchema: |
| 759 | * @schema: a schema structure |
| 760 | * |
| 761 | * Deallocate a RelaxNG schema structure. |
| 762 | */ |
| 763 | static void |
| 764 | xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema) |
| 765 | { |
| 766 | if (schema == NULL) |
| 767 | return; |
| 768 | |
| 769 | if (schema->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 770 | xmlFreeDoc(schema->doc); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 771 | if (schema->defTab != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 772 | int i; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 773 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 774 | for (i = 0; i < schema->defNr; i++) |
| 775 | xmlRelaxNGFreeDefine(schema->defTab[i]); |
| 776 | xmlFree(schema->defTab); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | xmlFree(schema); |
| 780 | } |
| 781 | |
| 782 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 783 | * xmlRelaxNGFree: |
| 784 | * @schema: a schema structure |
| 785 | * |
| 786 | * Deallocate a RelaxNG structure. |
| 787 | */ |
| 788 | void |
| 789 | xmlRelaxNGFree(xmlRelaxNGPtr schema) |
| 790 | { |
| 791 | if (schema == NULL) |
| 792 | return; |
| 793 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 794 | if (schema->topgrammar != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 795 | xmlRelaxNGFreeGrammar(schema->topgrammar); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 796 | if (schema->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 797 | xmlFreeDoc(schema->doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 798 | if (schema->documents != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 799 | xmlRelaxNGFreeDocumentList(schema->documents); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 800 | if (schema->includes != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 801 | xmlRelaxNGFreeIncludeList(schema->includes); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 802 | if (schema->defTab != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 803 | int i; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 804 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 805 | for (i = 0; i < schema->defNr; i++) |
| 806 | xmlRelaxNGFreeDefine(schema->defTab[i]); |
| 807 | xmlFree(schema->defTab); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 808 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 809 | |
| 810 | xmlFree(schema); |
| 811 | } |
| 812 | |
| 813 | /** |
| 814 | * xmlRelaxNGNewGrammar: |
| 815 | * @ctxt: a Relax-NG validation context (optional) |
| 816 | * |
| 817 | * Allocate a new RelaxNG grammar. |
| 818 | * |
| 819 | * Returns the newly allocated structure or NULL in case or error |
| 820 | */ |
| 821 | static xmlRelaxNGGrammarPtr |
| 822 | xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt) |
| 823 | { |
| 824 | xmlRelaxNGGrammarPtr ret; |
| 825 | |
| 826 | ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar)); |
| 827 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 828 | xmlRngPErrMemory(ctxt, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 829 | return (NULL); |
| 830 | } |
| 831 | memset(ret, 0, sizeof(xmlRelaxNGGrammar)); |
| 832 | |
| 833 | return (ret); |
| 834 | } |
| 835 | |
| 836 | /** |
| 837 | * xmlRelaxNGFreeGrammar: |
| 838 | * @grammar: a grammar structure |
| 839 | * |
| 840 | * Deallocate a RelaxNG grammar structure. |
| 841 | */ |
| 842 | static void |
| 843 | xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar) |
| 844 | { |
| 845 | if (grammar == NULL) |
| 846 | return; |
| 847 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 848 | if (grammar->children != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 849 | xmlRelaxNGFreeGrammar(grammar->children); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 850 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 851 | if (grammar->next != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 852 | xmlRelaxNGFreeGrammar(grammar->next); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 853 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 854 | if (grammar->refs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 855 | xmlHashFree(grammar->refs, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 856 | } |
| 857 | if (grammar->defs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 858 | xmlHashFree(grammar->defs, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | xmlFree(grammar); |
| 862 | } |
| 863 | |
| 864 | /** |
| 865 | * xmlRelaxNGNewDefine: |
| 866 | * @ctxt: a Relax-NG validation context |
| 867 | * @node: the node in the input document. |
| 868 | * |
| 869 | * Allocate a new RelaxNG define. |
| 870 | * |
| 871 | * Returns the newly allocated structure or NULL in case or error |
| 872 | */ |
| 873 | static xmlRelaxNGDefinePtr |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 874 | xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 875 | { |
| 876 | xmlRelaxNGDefinePtr ret; |
| 877 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 878 | if (ctxt->defMax == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 879 | ctxt->defMax = 16; |
| 880 | ctxt->defNr = 0; |
| 881 | ctxt->defTab = (xmlRelaxNGDefinePtr *) |
| 882 | xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr)); |
| 883 | if (ctxt->defTab == NULL) { |
| 884 | xmlRngPErrMemory(ctxt, "allocating define\n"); |
| 885 | return (NULL); |
| 886 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 887 | } else if (ctxt->defMax <= ctxt->defNr) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 888 | xmlRelaxNGDefinePtr *tmp; |
| 889 | |
| 890 | ctxt->defMax *= 2; |
| 891 | tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab, |
| 892 | ctxt->defMax * |
| 893 | sizeof |
| 894 | (xmlRelaxNGDefinePtr)); |
| 895 | if (tmp == NULL) { |
| 896 | xmlRngPErrMemory(ctxt, "allocating define\n"); |
| 897 | return (NULL); |
| 898 | } |
| 899 | ctxt->defTab = tmp; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 900 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 901 | ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine)); |
| 902 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 903 | xmlRngPErrMemory(ctxt, "allocating define\n"); |
| 904 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 905 | } |
| 906 | memset(ret, 0, sizeof(xmlRelaxNGDefine)); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 907 | ctxt->defTab[ctxt->defNr++] = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 908 | ret->node = node; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 909 | ret->depth = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 910 | return (ret); |
| 911 | } |
| 912 | |
| 913 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 914 | * xmlRelaxNGFreePartition: |
| 915 | * @partitions: a partition set structure |
| 916 | * |
| 917 | * Deallocate RelaxNG partition set structures. |
| 918 | */ |
| 919 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 920 | xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions) |
| 921 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 922 | xmlRelaxNGInterleaveGroupPtr group; |
| 923 | int j; |
| 924 | |
| 925 | if (partitions != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 926 | if (partitions->groups != NULL) { |
| 927 | for (j = 0; j < partitions->nbgroups; j++) { |
| 928 | group = partitions->groups[j]; |
| 929 | if (group != NULL) { |
| 930 | if (group->defs != NULL) |
| 931 | xmlFree(group->defs); |
| 932 | if (group->attrs != NULL) |
| 933 | xmlFree(group->attrs); |
| 934 | xmlFree(group); |
| 935 | } |
| 936 | } |
| 937 | xmlFree(partitions->groups); |
| 938 | } |
| 939 | if (partitions->triage != NULL) { |
| 940 | xmlHashFree(partitions->triage, NULL); |
| 941 | } |
| 942 | xmlFree(partitions); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 943 | } |
| 944 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 945 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 946 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 947 | * xmlRelaxNGFreeDefine: |
| 948 | * @define: a define structure |
| 949 | * |
| 950 | * Deallocate a RelaxNG define structure. |
| 951 | */ |
| 952 | static void |
| 953 | xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define) |
| 954 | { |
| 955 | if (define == NULL) |
| 956 | return; |
| 957 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 958 | if ((define->type == XML_RELAXNG_VALUE) && (define->attrs != NULL)) { |
| 959 | xmlRelaxNGTypeLibraryPtr lib; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 960 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 961 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
| 962 | if ((lib != NULL) && (lib->freef != NULL)) |
| 963 | lib->freef(lib->data, (void *) define->attrs); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 964 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 965 | if ((define->data != NULL) && (define->type == XML_RELAXNG_INTERLEAVE)) |
| 966 | xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data); |
| 967 | if ((define->data != NULL) && (define->type == XML_RELAXNG_CHOICE)) |
| 968 | xmlHashFree((xmlHashTablePtr) define->data, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 969 | if (define->name != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 970 | xmlFree(define->name); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 971 | if (define->ns != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 972 | xmlFree(define->ns); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 973 | if (define->value != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 974 | xmlFree(define->value); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 975 | if (define->contModel != NULL) |
| 976 | xmlRegFreeRegexp(define->contModel); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 977 | xmlFree(define); |
| 978 | } |
| 979 | |
| 980 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 981 | * xmlRelaxNGNewStates: |
| 982 | * @ctxt: a Relax-NG validation context |
| 983 | * @size: the default size for the container |
| 984 | * |
| 985 | * Allocate a new RelaxNG validation state container |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 986 | * |
| 987 | * Returns the newly allocated structure or NULL in case or error |
| 988 | */ |
| 989 | static xmlRelaxNGStatesPtr |
| 990 | xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size) |
| 991 | { |
| 992 | xmlRelaxNGStatesPtr ret; |
| 993 | |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 994 | if ((ctxt != NULL) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 995 | (ctxt->freeState != NULL) && (ctxt->freeStatesNr > 0)) { |
| 996 | ctxt->freeStatesNr--; |
| 997 | ret = ctxt->freeStates[ctxt->freeStatesNr]; |
| 998 | ret->nbState = 0; |
| 999 | return (ret); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1000 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1001 | if (size < 16) |
| 1002 | size = 16; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1003 | |
| 1004 | ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) + |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1005 | (size - |
| 1006 | 1) * |
| 1007 | sizeof(xmlRelaxNGValidStatePtr)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1008 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1009 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1010 | return (NULL); |
| 1011 | } |
| 1012 | ret->nbState = 0; |
| 1013 | ret->maxState = size; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1014 | ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc((size) * |
| 1015 | sizeof |
| 1016 | (xmlRelaxNGValidStatePtr)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1017 | if (ret->tabState == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1018 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1019 | xmlFree(ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1020 | return (NULL); |
| 1021 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1022 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | /** |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1026 | * xmlRelaxNGAddStateUniq: |
| 1027 | * @ctxt: a Relax-NG validation context |
| 1028 | * @states: the states container |
| 1029 | * @state: the validation state |
| 1030 | * |
| 1031 | * Add a RelaxNG validation state to the container without checking |
| 1032 | * for unicity. |
| 1033 | * |
| 1034 | * Return 1 in case of success and 0 if this is a duplicate and -1 on error |
| 1035 | */ |
| 1036 | static int |
| 1037 | xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1038 | xmlRelaxNGStatesPtr states, |
| 1039 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1040 | { |
| 1041 | if (state == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1042 | return (-1); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1043 | } |
| 1044 | if (states->nbState >= states->maxState) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1045 | xmlRelaxNGValidStatePtr *tmp; |
| 1046 | int size; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1047 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1048 | size = states->maxState * 2; |
| 1049 | tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState, |
| 1050 | (size) * |
| 1051 | sizeof |
| 1052 | (xmlRelaxNGValidStatePtr)); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1053 | if (tmp == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1054 | xmlRngVErrMemory(ctxt, "adding states\n"); |
| 1055 | return (-1); |
| 1056 | } |
| 1057 | states->tabState = tmp; |
| 1058 | states->maxState = size; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1059 | } |
| 1060 | states->tabState[states->nbState++] = state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1061 | return (1); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1065 | * xmlRelaxNGAddState: |
| 1066 | * @ctxt: a Relax-NG validation context |
| 1067 | * @states: the states container |
| 1068 | * @state: the validation state |
| 1069 | * |
| 1070 | * Add a RelaxNG validation state to the container |
| 1071 | * |
| 1072 | * Return 1 in case of success and 0 if this is a duplicate and -1 on error |
| 1073 | */ |
| 1074 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1075 | xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt, |
| 1076 | xmlRelaxNGStatesPtr states, |
| 1077 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1078 | { |
| 1079 | int i; |
| 1080 | |
| 1081 | if (state == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1082 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1083 | } |
| 1084 | if (states->nbState >= states->maxState) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1085 | xmlRelaxNGValidStatePtr *tmp; |
| 1086 | int size; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1087 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1088 | size = states->maxState * 2; |
| 1089 | tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState, |
| 1090 | (size) * |
| 1091 | sizeof |
| 1092 | (xmlRelaxNGValidStatePtr)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1093 | if (tmp == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1094 | xmlRngVErrMemory(ctxt, "adding states\n"); |
| 1095 | return (-1); |
| 1096 | } |
| 1097 | states->tabState = tmp; |
| 1098 | states->maxState = size; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1099 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1100 | for (i = 0; i < states->nbState; i++) { |
| 1101 | if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) { |
| 1102 | xmlRelaxNGFreeValidState(ctxt, state); |
| 1103 | return (0); |
| 1104 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1105 | } |
| 1106 | states->tabState[states->nbState++] = state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1107 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | /** |
| 1111 | * xmlRelaxNGFreeStates: |
| 1112 | * @ctxt: a Relax-NG validation context |
| 1113 | * @states: teh container |
| 1114 | * |
| 1115 | * Free a RelaxNG validation state container |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1116 | */ |
| 1117 | static void |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1118 | xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1119 | xmlRelaxNGStatesPtr states) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1120 | { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1121 | if (states == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1122 | return; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1123 | if ((ctxt != NULL) && (ctxt->freeStates == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1124 | ctxt->freeStatesMax = 40; |
| 1125 | ctxt->freeStatesNr = 0; |
| 1126 | ctxt->freeStates = (xmlRelaxNGStatesPtr *) |
| 1127 | xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr)); |
| 1128 | if (ctxt->freeStates == NULL) { |
| 1129 | xmlRngVErrMemory(ctxt, "storing states\n"); |
| 1130 | } |
| 1131 | } else if ((ctxt != NULL) |
| 1132 | && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) { |
| 1133 | xmlRelaxNGStatesPtr *tmp; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1134 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1135 | tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates, |
| 1136 | 2 * ctxt->freeStatesMax * |
| 1137 | sizeof |
| 1138 | (xmlRelaxNGStatesPtr)); |
| 1139 | if (tmp == NULL) { |
| 1140 | xmlRngVErrMemory(ctxt, "storing states\n"); |
| 1141 | xmlFree(states->tabState); |
| 1142 | xmlFree(states); |
| 1143 | return; |
| 1144 | } |
| 1145 | ctxt->freeStates = tmp; |
| 1146 | ctxt->freeStatesMax *= 2; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1147 | } |
| 1148 | if ((ctxt == NULL) || (ctxt->freeState == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1149 | xmlFree(states->tabState); |
| 1150 | xmlFree(states); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1151 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1152 | ctxt->freeStates[ctxt->freeStatesNr++] = states; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1153 | } |
| 1154 | } |
| 1155 | |
| 1156 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1157 | * xmlRelaxNGNewValidState: |
| 1158 | * @ctxt: a Relax-NG validation context |
| 1159 | * @node: the current node or NULL for the document |
| 1160 | * |
| 1161 | * Allocate a new RelaxNG validation state |
| 1162 | * |
| 1163 | * Returns the newly allocated structure or NULL in case or error |
| 1164 | */ |
| 1165 | static xmlRelaxNGValidStatePtr |
| 1166 | xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node) |
| 1167 | { |
| 1168 | xmlRelaxNGValidStatePtr ret; |
| 1169 | xmlAttrPtr attr; |
| 1170 | xmlAttrPtr attrs[MAX_ATTR]; |
| 1171 | int nbAttrs = 0; |
| 1172 | xmlNodePtr root = NULL; |
| 1173 | |
| 1174 | if (node == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1175 | root = xmlDocGetRootElement(ctxt->doc); |
| 1176 | if (root == NULL) |
| 1177 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1178 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1179 | attr = node->properties; |
| 1180 | while (attr != NULL) { |
| 1181 | if (nbAttrs < MAX_ATTR) |
| 1182 | attrs[nbAttrs++] = attr; |
| 1183 | else |
| 1184 | nbAttrs++; |
| 1185 | attr = attr->next; |
| 1186 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1187 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1188 | if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) { |
| 1189 | ctxt->freeState->nbState--; |
| 1190 | ret = ctxt->freeState->tabState[ctxt->freeState->nbState]; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1191 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1192 | ret = |
| 1193 | (xmlRelaxNGValidStatePtr) |
| 1194 | xmlMalloc(sizeof(xmlRelaxNGValidState)); |
| 1195 | if (ret == NULL) { |
| 1196 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1197 | return (NULL); |
| 1198 | } |
| 1199 | memset(ret, 0, sizeof(xmlRelaxNGValidState)); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1200 | } |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 1201 | ret->value = NULL; |
| 1202 | ret->endvalue = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1203 | if (node == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1204 | ret->node = (xmlNodePtr) ctxt->doc; |
| 1205 | ret->seq = root; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1206 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1207 | ret->node = node; |
| 1208 | ret->seq = node->children; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1209 | } |
| 1210 | ret->nbAttrs = 0; |
| 1211 | if (nbAttrs > 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1212 | if (ret->attrs == NULL) { |
| 1213 | if (nbAttrs < 4) |
| 1214 | ret->maxAttrs = 4; |
| 1215 | else |
| 1216 | ret->maxAttrs = nbAttrs; |
| 1217 | ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs * |
| 1218 | sizeof(xmlAttrPtr)); |
| 1219 | if (ret->attrs == NULL) { |
| 1220 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1221 | return (ret); |
| 1222 | } |
| 1223 | } else if (ret->maxAttrs < nbAttrs) { |
| 1224 | xmlAttrPtr *tmp; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1225 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1226 | tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs * |
| 1227 | sizeof(xmlAttrPtr)); |
| 1228 | if (tmp == NULL) { |
| 1229 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1230 | return (ret); |
| 1231 | } |
| 1232 | ret->attrs = tmp; |
| 1233 | ret->maxAttrs = nbAttrs; |
| 1234 | } |
| 1235 | ret->nbAttrs = nbAttrs; |
| 1236 | if (nbAttrs < MAX_ATTR) { |
| 1237 | memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs); |
| 1238 | } else { |
| 1239 | attr = node->properties; |
| 1240 | nbAttrs = 0; |
| 1241 | while (attr != NULL) { |
| 1242 | ret->attrs[nbAttrs++] = attr; |
| 1243 | attr = attr->next; |
| 1244 | } |
| 1245 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1246 | } |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 1247 | ret->nbAttrLeft = ret->nbAttrs; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1248 | return (ret); |
| 1249 | } |
| 1250 | |
| 1251 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1252 | * xmlRelaxNGCopyValidState: |
| 1253 | * @ctxt: a Relax-NG validation context |
| 1254 | * @state: a validation state |
| 1255 | * |
| 1256 | * Copy the validation state |
| 1257 | * |
| 1258 | * Returns the newly allocated structure or NULL in case or error |
| 1259 | */ |
| 1260 | static xmlRelaxNGValidStatePtr |
| 1261 | xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1262 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1263 | { |
| 1264 | xmlRelaxNGValidStatePtr ret; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1265 | unsigned int maxAttrs; |
| 1266 | xmlAttrPtr *attrs; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1267 | |
| 1268 | if (state == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1269 | return (NULL); |
| 1270 | if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) { |
| 1271 | ctxt->freeState->nbState--; |
| 1272 | ret = ctxt->freeState->tabState[ctxt->freeState->nbState]; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1273 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1274 | ret = |
| 1275 | (xmlRelaxNGValidStatePtr) |
| 1276 | xmlMalloc(sizeof(xmlRelaxNGValidState)); |
| 1277 | if (ret == NULL) { |
| 1278 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1279 | return (NULL); |
| 1280 | } |
| 1281 | memset(ret, 0, sizeof(xmlRelaxNGValidState)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1282 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1283 | attrs = ret->attrs; |
| 1284 | maxAttrs = ret->maxAttrs; |
| 1285 | memcpy(ret, state, sizeof(xmlRelaxNGValidState)); |
| 1286 | ret->attrs = attrs; |
| 1287 | ret->maxAttrs = maxAttrs; |
| 1288 | if (state->nbAttrs > 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1289 | if (ret->attrs == NULL) { |
| 1290 | ret->maxAttrs = state->maxAttrs; |
| 1291 | ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs * |
| 1292 | sizeof(xmlAttrPtr)); |
| 1293 | if (ret->attrs == NULL) { |
| 1294 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1295 | ret->nbAttrs = 0; |
| 1296 | return (ret); |
| 1297 | } |
| 1298 | } else if (ret->maxAttrs < state->nbAttrs) { |
| 1299 | xmlAttrPtr *tmp; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1300 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1301 | tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs * |
| 1302 | sizeof(xmlAttrPtr)); |
| 1303 | if (tmp == NULL) { |
| 1304 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1305 | ret->nbAttrs = 0; |
| 1306 | return (ret); |
| 1307 | } |
| 1308 | ret->maxAttrs = state->maxAttrs; |
| 1309 | ret->attrs = tmp; |
| 1310 | } |
| 1311 | memcpy(ret->attrs, state->attrs, |
| 1312 | state->nbAttrs * sizeof(xmlAttrPtr)); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1313 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1314 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
| 1317 | /** |
| 1318 | * xmlRelaxNGEqualValidState: |
| 1319 | * @ctxt: a Relax-NG validation context |
| 1320 | * @state1: a validation state |
| 1321 | * @state2: a validation state |
| 1322 | * |
| 1323 | * Compare the validation states for equality |
| 1324 | * |
| 1325 | * Returns 1 if equald, 0 otherwise |
| 1326 | */ |
| 1327 | static int |
| 1328 | xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1329 | xmlRelaxNGValidStatePtr state1, |
| 1330 | xmlRelaxNGValidStatePtr state2) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1331 | { |
| 1332 | int i; |
| 1333 | |
| 1334 | if ((state1 == NULL) || (state2 == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1335 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1336 | if (state1 == state2) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1337 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1338 | if (state1->node != state2->node) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1339 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1340 | if (state1->seq != state2->seq) |
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->nbAttrLeft != state2->nbAttrLeft) |
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->nbAttrs != state2->nbAttrs) |
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->endvalue != state2->endvalue) |
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->value != state2->value) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1349 | (!xmlStrEqual(state1->value, state2->value))) |
| 1350 | return (0); |
| 1351 | for (i = 0; i < state1->nbAttrs; i++) { |
| 1352 | if (state1->attrs[i] != state2->attrs[i]) |
| 1353 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1354 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1355 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1359 | * xmlRelaxNGFreeValidState: |
| 1360 | * @state: a validation state structure |
| 1361 | * |
| 1362 | * Deallocate a RelaxNG validation state structure. |
| 1363 | */ |
| 1364 | static void |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1365 | xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1366 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1367 | { |
| 1368 | if (state == NULL) |
| 1369 | return; |
| 1370 | |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1371 | if ((ctxt != NULL) && (ctxt->freeState == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1372 | ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1373 | } |
| 1374 | if ((ctxt == NULL) || (ctxt->freeState == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1375 | if (state->attrs != NULL) |
| 1376 | xmlFree(state->attrs); |
| 1377 | xmlFree(state); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1378 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1379 | xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1380 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
| 1383 | /************************************************************************ |
| 1384 | * * |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1385 | * Document functions * |
| 1386 | * * |
| 1387 | ************************************************************************/ |
| 1388 | static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1389 | xmlDocPtr doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1390 | |
| 1391 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1392 | * xmlRelaxNGIncludePush: |
| 1393 | * @ctxt: the parser context |
| 1394 | * @value: the element doc |
| 1395 | * |
| 1396 | * Pushes a new include on top of the include stack |
| 1397 | * |
| 1398 | * Returns 0 in case of error, the index in the stack otherwise |
| 1399 | */ |
| 1400 | static int |
| 1401 | xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1402 | xmlRelaxNGIncludePtr value) |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1403 | { |
| 1404 | if (ctxt->incTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1405 | ctxt->incMax = 4; |
| 1406 | ctxt->incNr = 0; |
| 1407 | ctxt->incTab = |
| 1408 | (xmlRelaxNGIncludePtr *) xmlMalloc(ctxt->incMax * |
| 1409 | sizeof(ctxt->incTab[0])); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1410 | if (ctxt->incTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1411 | xmlRngPErrMemory(ctxt, "allocating include\n"); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1412 | return (0); |
| 1413 | } |
| 1414 | } |
| 1415 | if (ctxt->incNr >= ctxt->incMax) { |
| 1416 | ctxt->incMax *= 2; |
| 1417 | ctxt->incTab = |
| 1418 | (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1419 | ctxt->incMax * |
| 1420 | sizeof(ctxt->incTab[0])); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1421 | if (ctxt->incTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1422 | xmlRngPErrMemory(ctxt, "allocating include\n"); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1423 | return (0); |
| 1424 | } |
| 1425 | } |
| 1426 | ctxt->incTab[ctxt->incNr] = value; |
| 1427 | ctxt->inc = value; |
| 1428 | return (ctxt->incNr++); |
| 1429 | } |
| 1430 | |
| 1431 | /** |
| 1432 | * xmlRelaxNGIncludePop: |
| 1433 | * @ctxt: the parser context |
| 1434 | * |
| 1435 | * Pops the top include from the include stack |
| 1436 | * |
| 1437 | * Returns the include just removed |
| 1438 | */ |
| 1439 | static xmlRelaxNGIncludePtr |
| 1440 | xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt) |
| 1441 | { |
| 1442 | xmlRelaxNGIncludePtr ret; |
| 1443 | |
| 1444 | if (ctxt->incNr <= 0) |
| 1445 | return (0); |
| 1446 | ctxt->incNr--; |
| 1447 | if (ctxt->incNr > 0) |
| 1448 | ctxt->inc = ctxt->incTab[ctxt->incNr - 1]; |
| 1449 | else |
| 1450 | ctxt->inc = NULL; |
| 1451 | ret = ctxt->incTab[ctxt->incNr]; |
| 1452 | ctxt->incTab[ctxt->incNr] = 0; |
| 1453 | return (ret); |
| 1454 | } |
| 1455 | |
| 1456 | /** |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1457 | * xmlRelaxNGRemoveRedefine: |
| 1458 | * @ctxt: the parser context |
| 1459 | * @URL: the normalized URL |
| 1460 | * @target: the included target |
| 1461 | * @name: the define name to eliminate |
| 1462 | * |
| 1463 | * Applies the elimination algorithm of 4.7 |
| 1464 | * |
| 1465 | * Returns 0 in case of error, 1 in case of success. |
| 1466 | */ |
| 1467 | static int |
| 1468 | xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1469 | const xmlChar * URL ATTRIBUTE_UNUSED, |
| 1470 | xmlNodePtr target, const xmlChar * name) |
| 1471 | { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1472 | int found = 0; |
| 1473 | xmlNodePtr tmp, tmp2; |
| 1474 | xmlChar *name2; |
| 1475 | |
| 1476 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 1477 | if (name == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1478 | xmlGenericError(xmlGenericErrorContext, |
| 1479 | "Elimination of <include> start from %s\n", URL); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 1480 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1481 | xmlGenericError(xmlGenericErrorContext, |
| 1482 | "Elimination of <include> define %s from %s\n", |
| 1483 | name, URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1484 | #endif |
| 1485 | tmp = target; |
| 1486 | while (tmp != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1487 | tmp2 = tmp->next; |
| 1488 | if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) { |
| 1489 | found = 1; |
| 1490 | xmlUnlinkNode(tmp); |
| 1491 | xmlFreeNode(tmp); |
| 1492 | } else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) { |
| 1493 | name2 = xmlGetProp(tmp, BAD_CAST "name"); |
| 1494 | xmlRelaxNGNormExtSpace(name2); |
| 1495 | if (name2 != NULL) { |
| 1496 | if (xmlStrEqual(name, name2)) { |
| 1497 | found = 1; |
| 1498 | xmlUnlinkNode(tmp); |
| 1499 | xmlFreeNode(tmp); |
| 1500 | } |
| 1501 | xmlFree(name2); |
| 1502 | } |
| 1503 | } else if (IS_RELAXNG(tmp, "include")) { |
| 1504 | xmlChar *href = NULL; |
| 1505 | xmlRelaxNGDocumentPtr inc = tmp->_private; |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1506 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1507 | if ((inc != NULL) && (inc->doc != NULL) && |
| 1508 | (inc->doc->children != NULL)) { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1509 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1510 | if (xmlStrEqual |
| 1511 | (inc->doc->children->name, BAD_CAST "grammar")) { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1512 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1513 | href = xmlGetProp(tmp, BAD_CAST "href"); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1514 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1515 | if (xmlRelaxNGRemoveRedefine(ctxt, href, |
| 1516 | inc->doc->children-> |
| 1517 | children, name) == 1) { |
| 1518 | found = 1; |
| 1519 | } |
| 1520 | if (href != NULL) |
| 1521 | xmlFree(href); |
| 1522 | } |
| 1523 | } |
| 1524 | } |
| 1525 | tmp = tmp2; |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1526 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1527 | return (found); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
| 1530 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1531 | * xmlRelaxNGLoadInclude: |
| 1532 | * @ctxt: the parser context |
| 1533 | * @URL: the normalized URL |
| 1534 | * @node: the include node. |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1535 | * @ns: the namespace passed from the context. |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1536 | * |
| 1537 | * First lookup if the document is already loaded into the parser context, |
| 1538 | * check against recursion. If not found the resource is loaded and |
| 1539 | * the content is preprocessed before being returned back to the caller. |
| 1540 | * |
| 1541 | * Returns the xmlRelaxNGIncludePtr or NULL in case of error |
| 1542 | */ |
| 1543 | static xmlRelaxNGIncludePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1544 | xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * URL, |
| 1545 | xmlNodePtr node, const xmlChar * ns) |
| 1546 | { |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1547 | xmlRelaxNGIncludePtr ret = NULL; |
| 1548 | xmlDocPtr doc; |
| 1549 | int i; |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1550 | xmlNodePtr root, cur; |
| 1551 | |
| 1552 | #ifdef DEBUG_INCLUDE |
| 1553 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1554 | "xmlRelaxNGLoadInclude(%s)\n", URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1555 | #endif |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1556 | |
| 1557 | /* |
| 1558 | * check against recursion in the stack |
| 1559 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1560 | for (i = 0; i < ctxt->incNr; i++) { |
| 1561 | if (xmlStrEqual(ctxt->incTab[i]->href, URL)) { |
| 1562 | xmlRngPErr(ctxt, NULL, XML_RNGP_INCLUDE_RECURSE, |
| 1563 | "Detected an Include recursion for %s\n", URL, |
| 1564 | NULL); |
| 1565 | return (NULL); |
| 1566 | } |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1567 | } |
| 1568 | |
| 1569 | /* |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1570 | * load the document |
| 1571 | */ |
| 1572 | doc = xmlParseFile((const char *) URL); |
| 1573 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1574 | xmlRngPErr(ctxt, node, XML_RNGP_PARSE_ERROR, |
| 1575 | "xmlRelaxNG: could not load %s\n", URL, NULL); |
| 1576 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1577 | } |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1578 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1579 | xmlGenericError(xmlGenericErrorContext, "Parsed %s Okay\n", URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1580 | #endif |
| 1581 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1582 | /* |
| 1583 | * Allocate the document structures and register it first. |
| 1584 | */ |
| 1585 | ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude)); |
| 1586 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1587 | xmlRngPErrMemory(ctxt, "allocating include\n"); |
| 1588 | xmlFreeDoc(doc); |
| 1589 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1590 | } |
| 1591 | memset(ret, 0, sizeof(xmlRelaxNGInclude)); |
| 1592 | ret->doc = doc; |
| 1593 | ret->href = xmlStrdup(URL); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1594 | ret->next = ctxt->includes; |
| 1595 | ctxt->includes = ret; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1596 | |
| 1597 | /* |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1598 | * transmit the ns if needed |
| 1599 | */ |
| 1600 | if (ns != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1601 | root = xmlDocGetRootElement(doc); |
| 1602 | if (root != NULL) { |
| 1603 | if (xmlHasProp(root, BAD_CAST "ns") == NULL) { |
| 1604 | xmlSetProp(root, BAD_CAST "ns", ns); |
| 1605 | } |
| 1606 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1607 | } |
| 1608 | |
| 1609 | /* |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1610 | * push it on the stack |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1611 | */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1612 | xmlRelaxNGIncludePush(ctxt, ret); |
| 1613 | |
| 1614 | /* |
| 1615 | * Some preprocessing of the document content, this include recursing |
| 1616 | * in the include stack. |
| 1617 | */ |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1618 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1619 | xmlGenericError(xmlGenericErrorContext, "cleanup of %s\n", URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1620 | #endif |
| 1621 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1622 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 1623 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1624 | ctxt->inc = NULL; |
| 1625 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | /* |
| 1629 | * Pop up the include from the stack |
| 1630 | */ |
| 1631 | xmlRelaxNGIncludePop(ctxt); |
| 1632 | |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1633 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1634 | xmlGenericError(xmlGenericErrorContext, "Checking of %s\n", URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1635 | #endif |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1636 | /* |
| 1637 | * Check that the top element is a grammar |
| 1638 | */ |
| 1639 | root = xmlDocGetRootElement(doc); |
| 1640 | if (root == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1641 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, |
| 1642 | "xmlRelaxNG: included document is empty %s\n", URL, |
| 1643 | NULL); |
| 1644 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1645 | } |
| 1646 | if (!IS_RELAXNG(root, "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1647 | xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING, |
| 1648 | "xmlRelaxNG: included document %s root is not a grammar\n", |
| 1649 | URL, NULL); |
| 1650 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1651 | } |
| 1652 | |
| 1653 | /* |
| 1654 | * Elimination of redefined rules in the include. |
| 1655 | */ |
| 1656 | cur = node->children; |
| 1657 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1658 | if (IS_RELAXNG(cur, "start")) { |
| 1659 | int found = 0; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1660 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1661 | found = |
| 1662 | xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL); |
| 1663 | if (!found) { |
| 1664 | xmlRngPErr(ctxt, node, XML_RNGP_START_MISSING, |
| 1665 | "xmlRelaxNG: include %s has a start but not the included grammar\n", |
| 1666 | URL, NULL); |
| 1667 | } |
| 1668 | } else if (IS_RELAXNG(cur, "define")) { |
| 1669 | xmlChar *name; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1670 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1671 | name = xmlGetProp(cur, BAD_CAST "name"); |
| 1672 | if (name == NULL) { |
| 1673 | xmlRngPErr(ctxt, node, XML_RNGP_NAME_MISSING, |
| 1674 | "xmlRelaxNG: include %s has define without name\n", |
| 1675 | URL, NULL); |
| 1676 | } else { |
| 1677 | int found; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1678 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1679 | xmlRelaxNGNormExtSpace(name); |
| 1680 | found = xmlRelaxNGRemoveRedefine(ctxt, URL, |
| 1681 | root->children, name); |
| 1682 | if (!found) { |
| 1683 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_MISSING, |
| 1684 | "xmlRelaxNG: include %s has a define %s but not the included grammar\n", |
| 1685 | URL, name); |
| 1686 | } |
| 1687 | xmlFree(name); |
| 1688 | } |
| 1689 | } |
| 1690 | cur = cur->next; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1691 | } |
| 1692 | |
| 1693 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1694 | return (ret); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1695 | } |
| 1696 | |
| 1697 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1698 | * xmlRelaxNGValidErrorPush: |
| 1699 | * @ctxt: the validation context |
| 1700 | * @err: the error code |
| 1701 | * @arg1: the first string argument |
| 1702 | * @arg2: the second string argument |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1703 | * @dup: arg need to be duplicated |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1704 | * |
| 1705 | * Pushes a new error on top of the error stack |
| 1706 | * |
| 1707 | * Returns 0 in case of error, the index in the stack otherwise |
| 1708 | */ |
| 1709 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1710 | xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt, |
| 1711 | xmlRelaxNGValidErr err, const xmlChar * arg1, |
| 1712 | const xmlChar * arg2, int dup) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1713 | { |
| 1714 | xmlRelaxNGValidErrorPtr cur; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1715 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 1716 | #ifdef DEBUG_ERROR |
| 1717 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1718 | "Pushing error %d at %d on stack\n", err, ctxt->errNr); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 1719 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1720 | if (ctxt->errTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1721 | ctxt->errMax = 8; |
| 1722 | ctxt->errNr = 0; |
| 1723 | ctxt->errTab = |
| 1724 | (xmlRelaxNGValidErrorPtr) xmlMalloc(ctxt->errMax * |
| 1725 | sizeof |
| 1726 | (xmlRelaxNGValidError)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1727 | if (ctxt->errTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1728 | xmlRngVErrMemory(ctxt, "pushing error\n"); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1729 | return (0); |
| 1730 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1731 | ctxt->err = NULL; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1732 | } |
| 1733 | if (ctxt->errNr >= ctxt->errMax) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1734 | ctxt->errMax *= 2; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1735 | ctxt->errTab = |
| 1736 | (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1737 | ctxt->errMax * |
| 1738 | sizeof |
| 1739 | (xmlRelaxNGValidError)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1740 | if (ctxt->errTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1741 | xmlRngVErrMemory(ctxt, "pushing error\n"); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1742 | return (0); |
| 1743 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1744 | ctxt->err = &ctxt->errTab[ctxt->errNr - 1]; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1745 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 1746 | if ((ctxt->err != NULL) && (ctxt->state != NULL) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1747 | (ctxt->err->node == ctxt->state->node) && (ctxt->err->err == err)) |
| 1748 | return (ctxt->errNr); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1749 | cur = &ctxt->errTab[ctxt->errNr]; |
| 1750 | cur->err = err; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1751 | if (dup) { |
| 1752 | cur->arg1 = xmlStrdup(arg1); |
| 1753 | cur->arg2 = xmlStrdup(arg2); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1754 | cur->flags = ERROR_IS_DUP; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1755 | } else { |
| 1756 | cur->arg1 = arg1; |
| 1757 | cur->arg2 = arg2; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1758 | cur->flags = 0; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1759 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1760 | if (ctxt->state != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1761 | cur->node = ctxt->state->node; |
| 1762 | cur->seq = ctxt->state->seq; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1763 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1764 | cur->node = NULL; |
| 1765 | cur->seq = NULL; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1766 | } |
| 1767 | ctxt->err = cur; |
| 1768 | return (ctxt->errNr++); |
| 1769 | } |
| 1770 | |
| 1771 | /** |
| 1772 | * xmlRelaxNGValidErrorPop: |
| 1773 | * @ctxt: the validation context |
| 1774 | * |
| 1775 | * Pops the top error from the error stack |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1776 | */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1777 | static void |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1778 | xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt) |
| 1779 | { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1780 | xmlRelaxNGValidErrorPtr cur; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1781 | |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1782 | if (ctxt->errNr <= 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1783 | ctxt->err = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1784 | return; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1785 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1786 | ctxt->errNr--; |
| 1787 | if (ctxt->errNr > 0) |
| 1788 | ctxt->err = &ctxt->errTab[ctxt->errNr - 1]; |
| 1789 | else |
| 1790 | ctxt->err = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1791 | cur = &ctxt->errTab[ctxt->errNr]; |
| 1792 | if (cur->flags & ERROR_IS_DUP) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1793 | if (cur->arg1 != NULL) |
| 1794 | xmlFree((xmlChar *) cur->arg1); |
| 1795 | cur->arg1 = NULL; |
| 1796 | if (cur->arg2 != NULL) |
| 1797 | xmlFree((xmlChar *) cur->arg2); |
| 1798 | cur->arg2 = NULL; |
| 1799 | cur->flags = 0; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1800 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1801 | } |
| 1802 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1803 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1804 | * xmlRelaxNGDocumentPush: |
| 1805 | * @ctxt: the parser context |
| 1806 | * @value: the element doc |
| 1807 | * |
| 1808 | * Pushes a new doc on top of the doc stack |
| 1809 | * |
| 1810 | * Returns 0 in case of error, the index in the stack otherwise |
| 1811 | */ |
| 1812 | static int |
| 1813 | xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1814 | xmlRelaxNGDocumentPtr value) |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1815 | { |
| 1816 | if (ctxt->docTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1817 | ctxt->docMax = 4; |
| 1818 | ctxt->docNr = 0; |
| 1819 | ctxt->docTab = |
| 1820 | (xmlRelaxNGDocumentPtr *) xmlMalloc(ctxt->docMax * |
| 1821 | sizeof(ctxt->docTab[0])); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1822 | if (ctxt->docTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1823 | xmlRngPErrMemory(ctxt, "adding document\n"); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1824 | return (0); |
| 1825 | } |
| 1826 | } |
| 1827 | if (ctxt->docNr >= ctxt->docMax) { |
| 1828 | ctxt->docMax *= 2; |
| 1829 | ctxt->docTab = |
| 1830 | (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1831 | ctxt->docMax * |
| 1832 | sizeof(ctxt->docTab[0])); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1833 | if (ctxt->docTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1834 | xmlRngPErrMemory(ctxt, "adding document\n"); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1835 | return (0); |
| 1836 | } |
| 1837 | } |
| 1838 | ctxt->docTab[ctxt->docNr] = value; |
| 1839 | ctxt->doc = value; |
| 1840 | return (ctxt->docNr++); |
| 1841 | } |
| 1842 | |
| 1843 | /** |
| 1844 | * xmlRelaxNGDocumentPop: |
| 1845 | * @ctxt: the parser context |
| 1846 | * |
| 1847 | * Pops the top doc from the doc stack |
| 1848 | * |
| 1849 | * Returns the doc just removed |
| 1850 | */ |
| 1851 | static xmlRelaxNGDocumentPtr |
| 1852 | xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt) |
| 1853 | { |
| 1854 | xmlRelaxNGDocumentPtr ret; |
| 1855 | |
| 1856 | if (ctxt->docNr <= 0) |
| 1857 | return (0); |
| 1858 | ctxt->docNr--; |
| 1859 | if (ctxt->docNr > 0) |
| 1860 | ctxt->doc = ctxt->docTab[ctxt->docNr - 1]; |
| 1861 | else |
| 1862 | ctxt->doc = NULL; |
| 1863 | ret = ctxt->docTab[ctxt->docNr]; |
| 1864 | ctxt->docTab[ctxt->docNr] = 0; |
| 1865 | return (ret); |
| 1866 | } |
| 1867 | |
| 1868 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1869 | * xmlRelaxNGLoadExternalRef: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1870 | * @ctxt: the parser context |
| 1871 | * @URL: the normalized URL |
| 1872 | * @ns: the inherited ns if any |
| 1873 | * |
| 1874 | * First lookup if the document is already loaded into the parser context, |
| 1875 | * check against recursion. If not found the resource is loaded and |
| 1876 | * the content is preprocessed before being returned back to the caller. |
| 1877 | * |
| 1878 | * Returns the xmlRelaxNGDocumentPtr or NULL in case of error |
| 1879 | */ |
| 1880 | static xmlRelaxNGDocumentPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1881 | xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt, |
| 1882 | const xmlChar * URL, const xmlChar * ns) |
| 1883 | { |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1884 | xmlRelaxNGDocumentPtr ret = NULL; |
| 1885 | xmlDocPtr doc; |
| 1886 | xmlNodePtr root; |
| 1887 | int i; |
| 1888 | |
| 1889 | /* |
| 1890 | * check against recursion in the stack |
| 1891 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1892 | for (i = 0; i < ctxt->docNr; i++) { |
| 1893 | if (xmlStrEqual(ctxt->docTab[i]->href, URL)) { |
| 1894 | xmlRngPErr(ctxt, NULL, XML_RNGP_EXTERNALREF_RECURSE, |
| 1895 | "Detected an externalRef recursion for %s\n", URL, |
| 1896 | NULL); |
| 1897 | return (NULL); |
| 1898 | } |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1899 | } |
| 1900 | |
| 1901 | /* |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1902 | * load the document |
| 1903 | */ |
| 1904 | doc = xmlParseFile((const char *) URL); |
| 1905 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1906 | xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR, |
| 1907 | "xmlRelaxNG: could not load %s\n", URL, NULL); |
| 1908 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1909 | } |
| 1910 | |
| 1911 | /* |
| 1912 | * Allocate the document structures and register it first. |
| 1913 | */ |
| 1914 | ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument)); |
| 1915 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1916 | xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_ERR_NO_MEMORY, |
| 1917 | "xmlRelaxNG: allocate memory for doc %s\n", URL, NULL); |
| 1918 | xmlFreeDoc(doc); |
| 1919 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1920 | } |
| 1921 | memset(ret, 0, sizeof(xmlRelaxNGDocument)); |
| 1922 | ret->doc = doc; |
| 1923 | ret->href = xmlStrdup(URL); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1924 | ret->next = ctxt->documents; |
| 1925 | ctxt->documents = ret; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1926 | |
| 1927 | /* |
| 1928 | * transmit the ns if needed |
| 1929 | */ |
| 1930 | if (ns != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1931 | root = xmlDocGetRootElement(doc); |
| 1932 | if (root != NULL) { |
| 1933 | if (xmlHasProp(root, BAD_CAST "ns") == NULL) { |
| 1934 | xmlSetProp(root, BAD_CAST "ns", ns); |
| 1935 | } |
| 1936 | } |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1937 | } |
| 1938 | |
| 1939 | /* |
| 1940 | * push it on the stack and register it in the hash table |
| 1941 | */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1942 | xmlRelaxNGDocumentPush(ctxt, ret); |
| 1943 | |
| 1944 | /* |
| 1945 | * Some preprocessing of the document content |
| 1946 | */ |
| 1947 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 1948 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1949 | ctxt->doc = NULL; |
| 1950 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1951 | } |
| 1952 | |
| 1953 | xmlRelaxNGDocumentPop(ctxt); |
| 1954 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1955 | return (ret); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1956 | } |
| 1957 | |
| 1958 | /************************************************************************ |
| 1959 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1960 | * Error functions * |
| 1961 | * * |
| 1962 | ************************************************************************/ |
| 1963 | |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1964 | #define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0); |
| 1965 | #define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0); |
| 1966 | #define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0); |
| 1967 | #define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1); |
| 1968 | #define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1969 | |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 1970 | static const char * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1971 | xmlRelaxNGDefName(xmlRelaxNGDefinePtr def) |
| 1972 | { |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 1973 | if (def == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1974 | return ("none"); |
| 1975 | switch (def->type) { |
| 1976 | case XML_RELAXNG_EMPTY: |
| 1977 | return ("empty"); |
| 1978 | case XML_RELAXNG_NOT_ALLOWED: |
| 1979 | return ("notAllowed"); |
| 1980 | case XML_RELAXNG_EXCEPT: |
| 1981 | return ("except"); |
| 1982 | case XML_RELAXNG_TEXT: |
| 1983 | return ("text"); |
| 1984 | case XML_RELAXNG_ELEMENT: |
| 1985 | return ("element"); |
| 1986 | case XML_RELAXNG_DATATYPE: |
| 1987 | return ("datatype"); |
| 1988 | case XML_RELAXNG_VALUE: |
| 1989 | return ("value"); |
| 1990 | case XML_RELAXNG_LIST: |
| 1991 | return ("list"); |
| 1992 | case XML_RELAXNG_ATTRIBUTE: |
| 1993 | return ("attribute"); |
| 1994 | case XML_RELAXNG_DEF: |
| 1995 | return ("def"); |
| 1996 | case XML_RELAXNG_REF: |
| 1997 | return ("ref"); |
| 1998 | case XML_RELAXNG_EXTERNALREF: |
| 1999 | return ("externalRef"); |
| 2000 | case XML_RELAXNG_PARENTREF: |
| 2001 | return ("parentRef"); |
| 2002 | case XML_RELAXNG_OPTIONAL: |
| 2003 | return ("optional"); |
| 2004 | case XML_RELAXNG_ZEROORMORE: |
| 2005 | return ("zeroOrMore"); |
| 2006 | case XML_RELAXNG_ONEORMORE: |
| 2007 | return ("oneOrMore"); |
| 2008 | case XML_RELAXNG_CHOICE: |
| 2009 | return ("choice"); |
| 2010 | case XML_RELAXNG_GROUP: |
| 2011 | return ("group"); |
| 2012 | case XML_RELAXNG_INTERLEAVE: |
| 2013 | return ("interleave"); |
| 2014 | case XML_RELAXNG_START: |
| 2015 | return ("start"); |
| 2016 | case XML_RELAXNG_NOOP: |
| 2017 | return ("noop"); |
| 2018 | case XML_RELAXNG_PARAM: |
| 2019 | return ("param"); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 2020 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2021 | return ("unknown"); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 2022 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 2023 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2024 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2025 | * xmlRelaxNGGetErrorString: |
| 2026 | * @err: the error code |
| 2027 | * @arg1: the first string argument |
| 2028 | * @arg2: the second string argument |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2029 | * |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2030 | * computes a formatted error string for the given error code and args |
| 2031 | * |
| 2032 | * Returns the error string, it must be deallocated by the caller |
| 2033 | */ |
| 2034 | static xmlChar * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2035 | xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar * arg1, |
| 2036 | const xmlChar * arg2) |
| 2037 | { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2038 | char msg[1000]; |
| 2039 | |
| 2040 | if (arg1 == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2041 | arg1 = BAD_CAST ""; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2042 | if (arg2 == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2043 | arg2 = BAD_CAST ""; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2044 | |
| 2045 | msg[0] = 0; |
| 2046 | switch (err) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2047 | case XML_RELAXNG_OK: |
| 2048 | return (NULL); |
| 2049 | case XML_RELAXNG_ERR_MEMORY: |
| 2050 | return (xmlCharStrdup("out of memory\n")); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2051 | case XML_RELAXNG_ERR_TYPE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2052 | snprintf(msg, 1000, "failed to validate type %s\n", arg1); |
| 2053 | break; |
| 2054 | case XML_RELAXNG_ERR_TYPEVAL: |
| 2055 | snprintf(msg, 1000, "Type %s doesn't allow value '%s'\n", arg1, |
| 2056 | arg2); |
| 2057 | break; |
| 2058 | case XML_RELAXNG_ERR_DUPID: |
| 2059 | snprintf(msg, 1000, "ID %s redefined\n", arg1); |
| 2060 | break; |
| 2061 | case XML_RELAXNG_ERR_TYPECMP: |
| 2062 | snprintf(msg, 1000, "failed to compare type %s\n", arg1); |
| 2063 | break; |
| 2064 | case XML_RELAXNG_ERR_NOSTATE: |
| 2065 | return (xmlCharStrdup("Internal error: no state\n")); |
| 2066 | case XML_RELAXNG_ERR_NODEFINE: |
| 2067 | return (xmlCharStrdup("Internal error: no define\n")); |
| 2068 | case XML_RELAXNG_ERR_INTERNAL: |
| 2069 | snprintf(msg, 1000, "Internal error: %s\n", arg1); |
| 2070 | break; |
| 2071 | case XML_RELAXNG_ERR_LISTEXTRA: |
| 2072 | snprintf(msg, 1000, "Extra data in list: %s\n", arg1); |
| 2073 | break; |
| 2074 | case XML_RELAXNG_ERR_INTERNODATA: |
| 2075 | return (xmlCharStrdup |
| 2076 | ("Internal: interleave block has no data\n")); |
| 2077 | case XML_RELAXNG_ERR_INTERSEQ: |
| 2078 | return (xmlCharStrdup("Invalid sequence in interleave\n")); |
| 2079 | case XML_RELAXNG_ERR_INTEREXTRA: |
| 2080 | snprintf(msg, 1000, "Extra element %s in interleave\n", arg1); |
| 2081 | break; |
| 2082 | case XML_RELAXNG_ERR_ELEMNAME: |
| 2083 | snprintf(msg, 1000, "Expecting element %s, got %s\n", arg1, |
| 2084 | arg2); |
| 2085 | break; |
| 2086 | case XML_RELAXNG_ERR_ELEMNONS: |
| 2087 | snprintf(msg, 1000, "Expecting a namespace for element %s\n", |
| 2088 | arg1); |
| 2089 | break; |
| 2090 | case XML_RELAXNG_ERR_ELEMWRONGNS: |
| 2091 | snprintf(msg, 1000, |
| 2092 | "Element %s has wrong namespace: expecting %s\n", arg1, |
| 2093 | arg2); |
| 2094 | break; |
| 2095 | case XML_RELAXNG_ERR_ELEMWRONG: |
| 2096 | snprintf(msg, 1000, "Did not expect element %s there\n", arg1); |
| 2097 | break; |
| 2098 | case XML_RELAXNG_ERR_TEXTWRONG: |
| 2099 | snprintf(msg, 1000, |
| 2100 | "Did not expect text in element %s content\n", arg1); |
| 2101 | break; |
| 2102 | case XML_RELAXNG_ERR_ELEMEXTRANS: |
| 2103 | snprintf(msg, 1000, "Expecting no namespace for element %s\n", |
| 2104 | arg1); |
| 2105 | break; |
| 2106 | case XML_RELAXNG_ERR_ELEMNOTEMPTY: |
| 2107 | snprintf(msg, 1000, "Expecting element %s to be empty\n", arg1); |
| 2108 | break; |
| 2109 | case XML_RELAXNG_ERR_NOELEM: |
| 2110 | snprintf(msg, 1000, "Expecting an element %s, got nothing\n", |
| 2111 | arg1); |
| 2112 | break; |
| 2113 | case XML_RELAXNG_ERR_NOTELEM: |
| 2114 | return (xmlCharStrdup("Expecting an element got text\n")); |
| 2115 | case XML_RELAXNG_ERR_ATTRVALID: |
| 2116 | snprintf(msg, 1000, "Element %s failed to validate attributes\n", |
| 2117 | arg1); |
| 2118 | break; |
| 2119 | case XML_RELAXNG_ERR_CONTENTVALID: |
| 2120 | snprintf(msg, 1000, "Element %s failed to validate content\n", |
| 2121 | arg1); |
| 2122 | break; |
| 2123 | case XML_RELAXNG_ERR_EXTRACONTENT: |
| 2124 | snprintf(msg, 1000, "Element %s has extra content: %s\n", |
| 2125 | arg1, arg2); |
| 2126 | break; |
| 2127 | case XML_RELAXNG_ERR_INVALIDATTR: |
| 2128 | snprintf(msg, 1000, "Invalid attribute %s for element %s\n", |
| 2129 | arg1, arg2); |
| 2130 | break; |
| 2131 | case XML_RELAXNG_ERR_LACKDATA: |
| 2132 | snprintf(msg, 1000, "Datatype element %s contains no data\n", |
| 2133 | arg1); |
| 2134 | break; |
| 2135 | case XML_RELAXNG_ERR_DATAELEM: |
| 2136 | snprintf(msg, 1000, "Datatype element %s has child elements\n", |
| 2137 | arg1); |
| 2138 | break; |
| 2139 | case XML_RELAXNG_ERR_VALELEM: |
| 2140 | snprintf(msg, 1000, "Value element %s has child elements\n", |
| 2141 | arg1); |
| 2142 | break; |
| 2143 | case XML_RELAXNG_ERR_LISTELEM: |
| 2144 | snprintf(msg, 1000, "List element %s has child elements\n", |
| 2145 | arg1); |
| 2146 | break; |
| 2147 | case XML_RELAXNG_ERR_DATATYPE: |
| 2148 | snprintf(msg, 1000, "Error validating datatype %s\n", arg1); |
| 2149 | break; |
| 2150 | case XML_RELAXNG_ERR_VALUE: |
| 2151 | snprintf(msg, 1000, "Error validating value %s\n", arg1); |
| 2152 | break; |
| 2153 | case XML_RELAXNG_ERR_LIST: |
| 2154 | return (xmlCharStrdup("Error validating list\n")); |
| 2155 | case XML_RELAXNG_ERR_NOGRAMMAR: |
| 2156 | return (xmlCharStrdup("No top grammar defined\n")); |
| 2157 | case XML_RELAXNG_ERR_EXTRADATA: |
| 2158 | return (xmlCharStrdup("Extra data in the document\n")); |
| 2159 | default: |
| 2160 | return (xmlCharStrdup("Unknown error !\n")); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2161 | } |
| 2162 | if (msg[0] == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2163 | snprintf(msg, 1000, "Unknown error code %d\n", err); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2164 | } |
Daniel Veillard | adbb0e6 | 2003-05-10 20:02:45 +0000 | [diff] [blame] | 2165 | msg[1000 - 1] = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2166 | return (xmlStrdup((xmlChar *) msg)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2167 | } |
| 2168 | |
| 2169 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2170 | * xmlRelaxNGShowValidError: |
| 2171 | * @ctxt: the validation context |
| 2172 | * @err: the error number |
| 2173 | * @node: the node |
| 2174 | * @child: the node child generating the problem. |
| 2175 | * @arg1: the first argument |
| 2176 | * @arg2: the second argument |
| 2177 | * |
| 2178 | * Show a validation error. |
| 2179 | */ |
| 2180 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2181 | xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt, |
| 2182 | xmlRelaxNGValidErr err, xmlNodePtr node, |
| 2183 | xmlNodePtr child, const xmlChar * arg1, |
| 2184 | const xmlChar * arg2) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2185 | { |
| 2186 | xmlChar *msg; |
| 2187 | |
| 2188 | if (ctxt->error == NULL) |
| 2189 | return; |
| 2190 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2191 | #ifdef DEBUG_ERROR |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2192 | xmlGenericError(xmlGenericErrorContext, "Show error %d\n", err); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2193 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2194 | msg = xmlRelaxNGGetErrorString(err, arg1, arg2); |
| 2195 | if (msg == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2196 | return; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2197 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2198 | if (ctxt->errNo == XML_RELAXNG_OK) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2199 | ctxt->errNo = err; |
| 2200 | xmlRngVErr(ctxt, (child == NULL ? node : child), err, |
| 2201 | (const char *) msg, arg1, arg2); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2202 | xmlFree(msg); |
| 2203 | } |
| 2204 | |
| 2205 | /** |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2206 | * xmlRelaxNGPopErrors: |
| 2207 | * @ctxt: the validation context |
| 2208 | * @level: the error level in the stack |
| 2209 | * |
| 2210 | * pop and discard all errors until the given level is reached |
| 2211 | */ |
| 2212 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2213 | xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level) |
| 2214 | { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2215 | int i; |
| 2216 | xmlRelaxNGValidErrorPtr err; |
| 2217 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2218 | #ifdef DEBUG_ERROR |
| 2219 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2220 | "Pop errors till level %d\n", level); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2221 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2222 | for (i = level; i < ctxt->errNr; i++) { |
| 2223 | err = &ctxt->errTab[i]; |
| 2224 | if (err->flags & ERROR_IS_DUP) { |
| 2225 | if (err->arg1 != NULL) |
| 2226 | xmlFree((xmlChar *) err->arg1); |
| 2227 | err->arg1 = NULL; |
| 2228 | if (err->arg2 != NULL) |
| 2229 | xmlFree((xmlChar *) err->arg2); |
| 2230 | err->arg2 = NULL; |
| 2231 | err->flags = 0; |
| 2232 | } |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2233 | } |
| 2234 | ctxt->errNr = level; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2235 | if (ctxt->errNr <= 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2236 | ctxt->err = NULL; |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2237 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2238 | |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2239 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2240 | * xmlRelaxNGDumpValidError: |
| 2241 | * @ctxt: the validation context |
| 2242 | * |
| 2243 | * Show all validation error over a given index. |
| 2244 | */ |
| 2245 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2246 | xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt) |
| 2247 | { |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 2248 | int i, j, k; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2249 | xmlRelaxNGValidErrorPtr err, dup; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2250 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2251 | #ifdef DEBUG_ERROR |
| 2252 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2253 | "Dumping error stack %d errors\n", ctxt->errNr); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2254 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2255 | for (i = 0, k = 0; i < ctxt->errNr; i++) { |
| 2256 | err = &ctxt->errTab[i]; |
| 2257 | if (k < MAX_ERROR) { |
| 2258 | for (j = 0; j < i; j++) { |
| 2259 | dup = &ctxt->errTab[j]; |
| 2260 | if ((err->err == dup->err) && (err->node == dup->node) && |
| 2261 | (xmlStrEqual(err->arg1, dup->arg1)) && |
| 2262 | (xmlStrEqual(err->arg2, dup->arg2))) { |
| 2263 | goto skip; |
| 2264 | } |
| 2265 | } |
| 2266 | xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq, |
| 2267 | err->arg1, err->arg2); |
| 2268 | k++; |
| 2269 | } |
| 2270 | skip: |
| 2271 | if (err->flags & ERROR_IS_DUP) { |
| 2272 | if (err->arg1 != NULL) |
| 2273 | xmlFree((xmlChar *) err->arg1); |
| 2274 | err->arg1 = NULL; |
| 2275 | if (err->arg2 != NULL) |
| 2276 | xmlFree((xmlChar *) err->arg2); |
| 2277 | err->arg2 = NULL; |
| 2278 | err->flags = 0; |
| 2279 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2280 | } |
| 2281 | ctxt->errNr = 0; |
| 2282 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2283 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2284 | /** |
| 2285 | * xmlRelaxNGAddValidError: |
| 2286 | * @ctxt: the validation context |
| 2287 | * @err: the error number |
| 2288 | * @arg1: the first argument |
| 2289 | * @arg2: the second argument |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2290 | * @dup: need to dup the args |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2291 | * |
| 2292 | * Register a validation error, either generating it if it's sure |
| 2293 | * or stacking it for later handling if unsure. |
| 2294 | */ |
| 2295 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2296 | xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt, |
| 2297 | xmlRelaxNGValidErr err, const xmlChar * arg1, |
| 2298 | const xmlChar * arg2, int dup) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2299 | { |
| 2300 | if ((ctxt == NULL) || (ctxt->error == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2301 | return; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2302 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2303 | #ifdef DEBUG_ERROR |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2304 | xmlGenericError(xmlGenericErrorContext, "Adding error %d\n", err); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2305 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2306 | /* |
| 2307 | * generate the error directly |
| 2308 | */ |
| 2309 | if (((ctxt->flags & 1) == 0) || (ctxt->flags & 2)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2310 | xmlNodePtr node, seq; |
| 2311 | |
| 2312 | /* |
| 2313 | * Flush first any stacked error which might be the |
| 2314 | * real cause of the problem. |
| 2315 | */ |
| 2316 | if (ctxt->errNr != 0) |
| 2317 | xmlRelaxNGDumpValidError(ctxt); |
| 2318 | if (ctxt->state != NULL) { |
| 2319 | node = ctxt->state->node; |
| 2320 | seq = ctxt->state->seq; |
| 2321 | } else { |
| 2322 | node = seq = NULL; |
| 2323 | } |
| 2324 | xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2325 | } |
| 2326 | /* |
| 2327 | * Stack the error for later processing if needed |
| 2328 | */ |
| 2329 | else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2330 | xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2331 | } |
| 2332 | } |
| 2333 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2334 | |
| 2335 | /************************************************************************ |
| 2336 | * * |
| 2337 | * Type library hooks * |
| 2338 | * * |
| 2339 | ************************************************************************/ |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2340 | static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2341 | const xmlChar * str); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2342 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2343 | /** |
| 2344 | * xmlRelaxNGSchemaTypeHave: |
| 2345 | * @data: data needed for the library |
| 2346 | * @type: the type name |
| 2347 | * |
| 2348 | * Check if the given type is provided by |
| 2349 | * the W3C XMLSchema Datatype library. |
| 2350 | * |
| 2351 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2352 | */ |
| 2353 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2354 | xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar * type) |
| 2355 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2356 | xmlSchemaTypePtr typ; |
| 2357 | |
| 2358 | if (type == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2359 | return (-1); |
| 2360 | typ = xmlSchemaGetPredefinedType(type, |
| 2361 | BAD_CAST |
| 2362 | "http://www.w3.org/2001/XMLSchema"); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2363 | if (typ == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2364 | return (0); |
| 2365 | return (1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2366 | } |
| 2367 | |
| 2368 | /** |
| 2369 | * xmlRelaxNGSchemaTypeCheck: |
| 2370 | * @data: data needed for the library |
| 2371 | * @type: the type name |
| 2372 | * @value: the value to check |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2373 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2374 | * |
| 2375 | * Check if the given type and value are validated by |
| 2376 | * the W3C XMLSchema Datatype library. |
| 2377 | * |
| 2378 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2379 | */ |
| 2380 | static int |
| 2381 | xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2382 | const xmlChar * type, |
| 2383 | const xmlChar * value, |
| 2384 | void **result, xmlNodePtr node) |
| 2385 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2386 | xmlSchemaTypePtr typ; |
| 2387 | int ret; |
| 2388 | |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2389 | if ((type == NULL) || (value == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2390 | return (-1); |
| 2391 | typ = xmlSchemaGetPredefinedType(type, |
| 2392 | BAD_CAST |
| 2393 | "http://www.w3.org/2001/XMLSchema"); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2394 | if (typ == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2395 | return (-1); |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2396 | ret = xmlSchemaValPredefTypeNode(typ, value, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2397 | (xmlSchemaValPtr *) result, node); |
| 2398 | if (ret == 2) /* special ID error code */ |
| 2399 | return (2); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2400 | if (ret == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2401 | return (1); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2402 | if (ret > 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2403 | return (0); |
| 2404 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2405 | } |
| 2406 | |
| 2407 | /** |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2408 | * xmlRelaxNGSchemaFacetCheck: |
| 2409 | * @data: data needed for the library |
| 2410 | * @type: the type name |
| 2411 | * @facet: the facet name |
| 2412 | * @val: the facet value |
| 2413 | * @strval: the string value |
| 2414 | * @value: the value to check |
| 2415 | * |
| 2416 | * Function provided by a type library to check a value facet |
| 2417 | * |
| 2418 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2419 | */ |
| 2420 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2421 | xmlRelaxNGSchemaFacetCheck(void *data ATTRIBUTE_UNUSED, |
| 2422 | const xmlChar * type, const xmlChar * facetname, |
| 2423 | const xmlChar * val, const xmlChar * strval, |
| 2424 | void *value) |
| 2425 | { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2426 | xmlSchemaFacetPtr facet; |
| 2427 | xmlSchemaTypePtr typ; |
| 2428 | int ret; |
| 2429 | |
| 2430 | if ((type == NULL) || (strval == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2431 | return (-1); |
| 2432 | typ = xmlSchemaGetPredefinedType(type, |
| 2433 | BAD_CAST |
| 2434 | "http://www.w3.org/2001/XMLSchema"); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2435 | if (typ == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2436 | return (-1); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2437 | |
| 2438 | facet = xmlSchemaNewFacet(); |
| 2439 | if (facet == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2440 | return (-1); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2441 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2442 | if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2443 | facet->type = XML_SCHEMA_FACET_MININCLUSIVE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2444 | } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2445 | facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2446 | } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2447 | facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2448 | } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2449 | facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2450 | } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2451 | facet->type = XML_SCHEMA_FACET_TOTALDIGITS; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2452 | } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2453 | facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2454 | } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2455 | facet->type = XML_SCHEMA_FACET_PATTERN; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2456 | } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2457 | facet->type = XML_SCHEMA_FACET_ENUMERATION; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2458 | } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2459 | facet->type = XML_SCHEMA_FACET_WHITESPACE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2460 | } else if (xmlStrEqual(facetname, BAD_CAST "length")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2461 | facet->type = XML_SCHEMA_FACET_LENGTH; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2462 | } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2463 | facet->type = XML_SCHEMA_FACET_MAXLENGTH; |
| 2464 | } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) { |
| 2465 | facet->type = XML_SCHEMA_FACET_MINLENGTH; |
| 2466 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2467 | xmlSchemaFreeFacet(facet); |
| 2468 | return (-1); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2469 | } |
| 2470 | facet->value = xmlStrdup(val); |
| 2471 | ret = xmlSchemaCheckFacet(facet, typ, NULL, type); |
| 2472 | if (ret != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2473 | xmlSchemaFreeFacet(facet); |
| 2474 | return (-1); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2475 | } |
| 2476 | ret = xmlSchemaValidateFacet(typ, facet, strval, value); |
| 2477 | xmlSchemaFreeFacet(facet); |
| 2478 | if (ret != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2479 | return (-1); |
| 2480 | return (0); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2481 | } |
| 2482 | |
| 2483 | /** |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2484 | * xmlRelaxNGSchemaFreeValue: |
| 2485 | * @data: data needed for the library |
| 2486 | * @value: the value to free |
| 2487 | * |
| 2488 | * Function provided by a type library to free a Schemas value |
| 2489 | * |
| 2490 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2491 | */ |
| 2492 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2493 | xmlRelaxNGSchemaFreeValue(void *data ATTRIBUTE_UNUSED, void *value) |
| 2494 | { |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2495 | xmlSchemaFreeValue(value); |
| 2496 | } |
| 2497 | |
| 2498 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2499 | * xmlRelaxNGSchemaTypeCompare: |
| 2500 | * @data: data needed for the library |
| 2501 | * @type: the type name |
| 2502 | * @value1: the first value |
| 2503 | * @value2: the second value |
| 2504 | * |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2505 | * Compare two values for equality accordingly a type from the W3C XMLSchema |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2506 | * Datatype library. |
| 2507 | * |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2508 | * 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] | 2509 | */ |
| 2510 | static int |
| 2511 | xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2512 | const xmlChar * type, |
| 2513 | const xmlChar * value1, |
| 2514 | xmlNodePtr ctxt1, |
| 2515 | void *comp1, |
| 2516 | const xmlChar * value2, xmlNodePtr ctxt2) |
| 2517 | { |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2518 | int ret; |
| 2519 | xmlSchemaTypePtr typ; |
| 2520 | xmlSchemaValPtr res1 = NULL, res2 = NULL; |
| 2521 | |
| 2522 | if ((type == NULL) || (value1 == NULL) || (value2 == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2523 | return (-1); |
| 2524 | typ = xmlSchemaGetPredefinedType(type, |
| 2525 | BAD_CAST |
| 2526 | "http://www.w3.org/2001/XMLSchema"); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2527 | if (typ == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2528 | return (-1); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2529 | if (comp1 == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2530 | ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1); |
| 2531 | if (ret != 0) |
| 2532 | return (-1); |
| 2533 | if (res1 == NULL) |
| 2534 | return (-1); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2535 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2536 | res1 = (xmlSchemaValPtr) comp1; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2537 | } |
| 2538 | ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2539 | if (ret != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2540 | xmlSchemaFreeValue(res1); |
| 2541 | return (-1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2542 | } |
| 2543 | if (res1 == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2544 | xmlSchemaFreeValue(res1); |
| 2545 | return (-1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2546 | } |
| 2547 | ret = xmlSchemaCompareValues(res1, res2); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2548 | if (res1 != (xmlSchemaValPtr) comp1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2549 | xmlSchemaFreeValue(res1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2550 | xmlSchemaFreeValue(res2); |
| 2551 | if (ret == -2) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2552 | return (-1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2553 | if (ret == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2554 | return (1); |
| 2555 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2556 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2557 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2558 | /** |
| 2559 | * xmlRelaxNGDefaultTypeHave: |
| 2560 | * @data: data needed for the library |
| 2561 | * @type: the type name |
| 2562 | * |
| 2563 | * Check if the given type is provided by |
| 2564 | * the default datatype library. |
| 2565 | * |
| 2566 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2567 | */ |
| 2568 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2569 | xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED, |
| 2570 | const xmlChar * type) |
| 2571 | { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2572 | if (type == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2573 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2574 | if (xmlStrEqual(type, BAD_CAST "string")) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2575 | return (1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2576 | if (xmlStrEqual(type, BAD_CAST "token")) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2577 | return (1); |
| 2578 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2579 | } |
| 2580 | |
| 2581 | /** |
| 2582 | * xmlRelaxNGDefaultTypeCheck: |
| 2583 | * @data: data needed for the library |
| 2584 | * @type: the type name |
| 2585 | * @value: the value to check |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2586 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2587 | * |
| 2588 | * Check if the given type and value are validated by |
| 2589 | * the default datatype library. |
| 2590 | * |
| 2591 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2592 | */ |
| 2593 | static int |
| 2594 | xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2595 | const xmlChar * type ATTRIBUTE_UNUSED, |
| 2596 | const xmlChar * value ATTRIBUTE_UNUSED, |
| 2597 | void **result ATTRIBUTE_UNUSED, |
| 2598 | xmlNodePtr node ATTRIBUTE_UNUSED) |
| 2599 | { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2600 | if (value == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2601 | return (-1); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2602 | if (xmlStrEqual(type, BAD_CAST "string")) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2603 | return (1); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2604 | if (xmlStrEqual(type, BAD_CAST "token")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2605 | return (1); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2606 | } |
| 2607 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2608 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2609 | } |
| 2610 | |
| 2611 | /** |
| 2612 | * xmlRelaxNGDefaultTypeCompare: |
| 2613 | * @data: data needed for the library |
| 2614 | * @type: the type name |
| 2615 | * @value1: the first value |
| 2616 | * @value2: the second value |
| 2617 | * |
| 2618 | * Compare two values accordingly a type from the default |
| 2619 | * datatype library. |
| 2620 | * |
| 2621 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2622 | */ |
| 2623 | static int |
| 2624 | xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2625 | const xmlChar * type, |
| 2626 | const xmlChar * value1, |
| 2627 | xmlNodePtr ctxt1 ATTRIBUTE_UNUSED, |
| 2628 | void *comp1 ATTRIBUTE_UNUSED, |
| 2629 | const xmlChar * value2, |
| 2630 | xmlNodePtr ctxt2 ATTRIBUTE_UNUSED) |
| 2631 | { |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2632 | int ret = -1; |
| 2633 | |
| 2634 | if (xmlStrEqual(type, BAD_CAST "string")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2635 | ret = xmlStrEqual(value1, value2); |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2636 | } else if (xmlStrEqual(type, BAD_CAST "token")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2637 | if (!xmlStrEqual(value1, value2)) { |
| 2638 | xmlChar *nval, *nvalue; |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2639 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2640 | /* |
| 2641 | * TODO: trivial optimizations are possible by |
| 2642 | * computing at compile-time |
| 2643 | */ |
| 2644 | nval = xmlRelaxNGNormalize(NULL, value1); |
| 2645 | nvalue = xmlRelaxNGNormalize(NULL, value2); |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2646 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2647 | if ((nval == NULL) || (nvalue == NULL)) |
| 2648 | ret = -1; |
| 2649 | else if (xmlStrEqual(nval, nvalue)) |
| 2650 | ret = 1; |
| 2651 | else |
| 2652 | ret = 0; |
| 2653 | if (nval != NULL) |
| 2654 | xmlFree(nval); |
| 2655 | if (nvalue != NULL) |
| 2656 | xmlFree(nvalue); |
| 2657 | } else |
| 2658 | ret = 1; |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2659 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2660 | return (ret); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2661 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2662 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2663 | static int xmlRelaxNGTypeInitialized = 0; |
| 2664 | static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL; |
| 2665 | |
| 2666 | /** |
| 2667 | * xmlRelaxNGFreeTypeLibrary: |
| 2668 | * @lib: the type library structure |
| 2669 | * @namespace: the URI bound to the library |
| 2670 | * |
| 2671 | * Free the structure associated to the type library |
| 2672 | */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2673 | static void |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2674 | xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2675 | const xmlChar * namespace ATTRIBUTE_UNUSED) |
| 2676 | { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2677 | if (lib == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2678 | return; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2679 | if (lib->namespace != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2680 | xmlFree((xmlChar *) lib->namespace); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2681 | xmlFree(lib); |
| 2682 | } |
| 2683 | |
| 2684 | /** |
| 2685 | * xmlRelaxNGRegisterTypeLibrary: |
| 2686 | * @namespace: the URI bound to the library |
| 2687 | * @data: data associated to the library |
| 2688 | * @have: the provide function |
| 2689 | * @check: the checking function |
| 2690 | * @comp: the comparison function |
| 2691 | * |
| 2692 | * Register a new type library |
| 2693 | * |
| 2694 | * Returns 0 in case of success and -1 in case of error. |
| 2695 | */ |
| 2696 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2697 | xmlRelaxNGRegisterTypeLibrary(const xmlChar * namespace, void *data, |
| 2698 | xmlRelaxNGTypeHave have, |
| 2699 | xmlRelaxNGTypeCheck check, |
| 2700 | xmlRelaxNGTypeCompare comp, |
| 2701 | xmlRelaxNGFacetCheck facet, |
| 2702 | xmlRelaxNGTypeFree freef) |
| 2703 | { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2704 | xmlRelaxNGTypeLibraryPtr lib; |
| 2705 | int ret; |
| 2706 | |
| 2707 | if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2708 | (check == NULL) || (comp == NULL)) |
| 2709 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2710 | if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2711 | xmlGenericError(xmlGenericErrorContext, |
| 2712 | "Relax-NG types library '%s' already registered\n", |
| 2713 | namespace); |
| 2714 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2715 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2716 | lib = |
| 2717 | (xmlRelaxNGTypeLibraryPtr) |
| 2718 | xmlMalloc(sizeof(xmlRelaxNGTypeLibrary)); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2719 | if (lib == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2720 | xmlRngVErrMemory(NULL, "adding types library\n"); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2721 | return (-1); |
| 2722 | } |
| 2723 | memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary)); |
| 2724 | lib->namespace = xmlStrdup(namespace); |
| 2725 | lib->data = data; |
| 2726 | lib->have = have; |
| 2727 | lib->comp = comp; |
| 2728 | lib->check = check; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2729 | lib->facet = facet; |
| 2730 | lib->freef = freef; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2731 | ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib); |
| 2732 | if (ret < 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2733 | xmlGenericError(xmlGenericErrorContext, |
| 2734 | "Relax-NG types library failed to register '%s'\n", |
| 2735 | namespace); |
| 2736 | xmlRelaxNGFreeTypeLibrary(lib, namespace); |
| 2737 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2738 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2739 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2740 | } |
| 2741 | |
| 2742 | /** |
| 2743 | * xmlRelaxNGInitTypes: |
| 2744 | * |
| 2745 | * Initilize the default type libraries. |
| 2746 | * |
| 2747 | * Returns 0 in case of success and -1 in case of error. |
| 2748 | */ |
| 2749 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2750 | xmlRelaxNGInitTypes(void) |
| 2751 | { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2752 | if (xmlRelaxNGTypeInitialized != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2753 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2754 | xmlRelaxNGRegisteredTypes = xmlHashCreate(10); |
| 2755 | if (xmlRelaxNGRegisteredTypes == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2756 | xmlGenericError(xmlGenericErrorContext, |
| 2757 | "Failed to allocate sh table for Relax-NG types\n"); |
| 2758 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2759 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2760 | xmlRelaxNGRegisterTypeLibrary(BAD_CAST |
| 2761 | "http://www.w3.org/2001/XMLSchema-datatypes", |
| 2762 | NULL, xmlRelaxNGSchemaTypeHave, |
| 2763 | xmlRelaxNGSchemaTypeCheck, |
| 2764 | xmlRelaxNGSchemaTypeCompare, |
| 2765 | xmlRelaxNGSchemaFacetCheck, |
| 2766 | xmlRelaxNGSchemaFreeValue); |
| 2767 | xmlRelaxNGRegisterTypeLibrary(xmlRelaxNGNs, NULL, |
| 2768 | xmlRelaxNGDefaultTypeHave, |
| 2769 | xmlRelaxNGDefaultTypeCheck, |
| 2770 | xmlRelaxNGDefaultTypeCompare, NULL, |
| 2771 | NULL); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2772 | xmlRelaxNGTypeInitialized = 1; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2773 | return (0); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2774 | } |
| 2775 | |
| 2776 | /** |
| 2777 | * xmlRelaxNGCleanupTypes: |
| 2778 | * |
| 2779 | * Cleanup the default Schemas type library associated to RelaxNG |
| 2780 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2781 | void |
| 2782 | xmlRelaxNGCleanupTypes(void) |
| 2783 | { |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 2784 | xmlSchemaCleanupTypes(); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2785 | if (xmlRelaxNGTypeInitialized == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2786 | return; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2787 | xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2788 | xmlRelaxNGFreeTypeLibrary); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2789 | xmlRelaxNGTypeInitialized = 0; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2790 | } |
| 2791 | |
| 2792 | /************************************************************************ |
| 2793 | * * |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2794 | * Compiling element content into regexp * |
| 2795 | * * |
| 2796 | * Sometime the element content can be compiled into a pure regexp, * |
| 2797 | * This allows a faster execution and streamability at that level * |
| 2798 | * * |
| 2799 | ************************************************************************/ |
| 2800 | |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2801 | static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, |
| 2802 | xmlRelaxNGDefinePtr def); |
| 2803 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2804 | /** |
| 2805 | * xmlRelaxNGIsCompileable: |
| 2806 | * @define: the definition to check |
| 2807 | * |
| 2808 | * Check if a definition is nullable. |
| 2809 | * |
| 2810 | * Returns 1 if yes, 0 if no and -1 in case of error |
| 2811 | */ |
| 2812 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2813 | xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def) |
| 2814 | { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2815 | int ret = -1; |
| 2816 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2817 | if (def == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2818 | return (-1); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2819 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2820 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 2821 | (def->dflags & IS_COMPILABLE)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2822 | return (1); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2823 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 2824 | (def->dflags & IS_NOT_COMPILABLE)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2825 | return (0); |
| 2826 | switch (def->type) { |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2827 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2828 | ret = xmlRelaxNGIsCompileable(def->content); |
| 2829 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2830 | case XML_RELAXNG_TEXT: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2831 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2832 | ret = 1; |
| 2833 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2834 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2835 | /* |
| 2836 | * Check if the element content is compileable |
| 2837 | */ |
| 2838 | if (((def->dflags & IS_NOT_COMPILABLE) == 0) && |
| 2839 | ((def->dflags & IS_COMPILABLE) == 0)) { |
| 2840 | xmlRelaxNGDefinePtr list; |
| 2841 | |
| 2842 | list = def->content; |
| 2843 | while (list != NULL) { |
| 2844 | ret = xmlRelaxNGIsCompileable(list); |
| 2845 | if (ret != 1) |
| 2846 | break; |
| 2847 | list = list->next; |
| 2848 | } |
| 2849 | if (ret == 0) |
| 2850 | def->dflags |= IS_NOT_COMPILABLE; |
| 2851 | if (ret == 1) |
| 2852 | def->dflags |= IS_COMPILABLE; |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2853 | #ifdef DEBUG_COMPILE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2854 | if (ret == 1) { |
| 2855 | xmlGenericError(xmlGenericErrorContext, |
| 2856 | "element content for %s is compilable\n", |
| 2857 | def->name); |
| 2858 | } else if (ret == 0) { |
| 2859 | xmlGenericError(xmlGenericErrorContext, |
| 2860 | "element content for %s is not compilable\n", |
| 2861 | def->name); |
| 2862 | } else { |
| 2863 | xmlGenericError(xmlGenericErrorContext, |
| 2864 | "Problem in RelaxNGIsCompileable for element %s\n", |
| 2865 | def->name); |
| 2866 | } |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2867 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2868 | } |
| 2869 | /* |
| 2870 | * All elements return a compileable status unless they |
| 2871 | * are generic like anyName |
| 2872 | */ |
| 2873 | if ((def->nameClass != NULL) || (def->name == NULL)) |
| 2874 | ret = 0; |
| 2875 | else |
| 2876 | ret = 1; |
| 2877 | return (ret); |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2878 | case XML_RELAXNG_REF: |
| 2879 | case XML_RELAXNG_EXTERNALREF: |
| 2880 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2881 | if (def->depth == -20) { |
| 2882 | return (1); |
| 2883 | } else { |
| 2884 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2885 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2886 | def->depth = -20; |
| 2887 | list = def->content; |
| 2888 | while (list != NULL) { |
| 2889 | ret = xmlRelaxNGIsCompileable(list); |
| 2890 | if (ret != 1) |
| 2891 | break; |
| 2892 | list = list->next; |
| 2893 | } |
| 2894 | } |
| 2895 | break; |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2896 | case XML_RELAXNG_START: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2897 | case XML_RELAXNG_OPTIONAL: |
| 2898 | case XML_RELAXNG_ZEROORMORE: |
| 2899 | case XML_RELAXNG_ONEORMORE: |
| 2900 | case XML_RELAXNG_CHOICE: |
| 2901 | case XML_RELAXNG_GROUP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2902 | case XML_RELAXNG_DEF:{ |
| 2903 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2904 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2905 | list = def->content; |
| 2906 | while (list != NULL) { |
| 2907 | ret = xmlRelaxNGIsCompileable(list); |
| 2908 | if (ret != 1) |
| 2909 | break; |
| 2910 | list = list->next; |
| 2911 | } |
| 2912 | break; |
| 2913 | } |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2914 | case XML_RELAXNG_EXCEPT: |
| 2915 | case XML_RELAXNG_ATTRIBUTE: |
| 2916 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2917 | case XML_RELAXNG_DATATYPE: |
| 2918 | case XML_RELAXNG_LIST: |
| 2919 | case XML_RELAXNG_PARAM: |
| 2920 | case XML_RELAXNG_VALUE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2921 | ret = 0; |
| 2922 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2923 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2924 | ret = -1; |
| 2925 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2926 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2927 | if (ret == 0) |
| 2928 | def->dflags |= IS_NOT_COMPILABLE; |
| 2929 | if (ret == 1) |
| 2930 | def->dflags |= IS_COMPILABLE; |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2931 | #ifdef DEBUG_COMPILE |
| 2932 | if (ret == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2933 | xmlGenericError(xmlGenericErrorContext, |
| 2934 | "RelaxNGIsCompileable %s : true\n", |
| 2935 | xmlRelaxNGDefName(def)); |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2936 | } else if (ret == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2937 | xmlGenericError(xmlGenericErrorContext, |
| 2938 | "RelaxNGIsCompileable %s : false\n", |
| 2939 | xmlRelaxNGDefName(def)); |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2940 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2941 | xmlGenericError(xmlGenericErrorContext, |
| 2942 | "Problem in RelaxNGIsCompileable %s\n", |
| 2943 | xmlRelaxNGDefName(def)); |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2944 | } |
| 2945 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2946 | return (ret); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2947 | } |
| 2948 | |
| 2949 | /** |
| 2950 | * xmlRelaxNGCompile: |
| 2951 | * ctxt: the RelaxNG parser context |
| 2952 | * @define: the definition tree to compile |
| 2953 | * |
| 2954 | * Compile the set of definitions, it works recursively, till the |
| 2955 | * element boundaries, where it tries to compile the content if possible |
| 2956 | * |
| 2957 | * Returns 0 if success and -1 in case of error |
| 2958 | */ |
| 2959 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2960 | xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) |
| 2961 | { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2962 | int ret = 0; |
| 2963 | xmlRelaxNGDefinePtr list; |
| 2964 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2965 | if ((ctxt == NULL) || (def == NULL)) |
| 2966 | return (-1); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2967 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2968 | switch (def->type) { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2969 | case XML_RELAXNG_START: |
| 2970 | if ((xmlRelaxNGIsCompileable(def) == 1) && (def->depth != -25)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2971 | xmlAutomataPtr oldam = ctxt->am; |
| 2972 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2973 | |
| 2974 | def->depth = -25; |
| 2975 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2976 | list = def->content; |
| 2977 | ctxt->am = xmlNewAutomata(); |
| 2978 | if (ctxt->am == NULL) |
| 2979 | return (-1); |
| 2980 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
| 2981 | while (list != NULL) { |
| 2982 | xmlRelaxNGCompile(ctxt, list); |
| 2983 | list = list->next; |
| 2984 | } |
| 2985 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
| 2986 | def->contModel = xmlAutomataCompile(ctxt->am); |
| 2987 | xmlRegexpIsDeterminist(def->contModel); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2988 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2989 | xmlFreeAutomata(ctxt->am); |
| 2990 | ctxt->state = oldstate; |
| 2991 | ctxt->am = oldam; |
| 2992 | } |
| 2993 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2994 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2995 | if ((ctxt->am != NULL) && (def->name != NULL)) { |
| 2996 | ctxt->state = xmlAutomataNewTransition2(ctxt->am, |
| 2997 | ctxt->state, NULL, |
| 2998 | def->name, def->ns, |
| 2999 | def); |
| 3000 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3001 | if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3002 | xmlAutomataPtr oldam = ctxt->am; |
| 3003 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3004 | |
| 3005 | def->depth = -25; |
| 3006 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3007 | list = def->content; |
| 3008 | ctxt->am = xmlNewAutomata(); |
| 3009 | if (ctxt->am == NULL) |
| 3010 | return (-1); |
| 3011 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
| 3012 | while (list != NULL) { |
| 3013 | xmlRelaxNGCompile(ctxt, list); |
| 3014 | list = list->next; |
| 3015 | } |
| 3016 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
| 3017 | def->contModel = xmlAutomataCompile(ctxt->am); |
| 3018 | if (!xmlRegexpIsDeterminist(def->contModel)) { |
| 3019 | /* |
| 3020 | * we can only use the automata if it is determinist |
| 3021 | */ |
| 3022 | xmlRegFreeRegexp(def->contModel); |
| 3023 | def->contModel = NULL; |
| 3024 | } |
| 3025 | xmlFreeAutomata(ctxt->am); |
| 3026 | ctxt->state = oldstate; |
| 3027 | ctxt->am = oldam; |
| 3028 | } else { |
| 3029 | xmlAutomataPtr oldam = ctxt->am; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3030 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3031 | /* |
| 3032 | * we can't build the content model for this element content |
| 3033 | * but it still might be possible to build it for some of its |
| 3034 | * children, recurse. |
| 3035 | */ |
| 3036 | ret = xmlRelaxNGTryCompile(ctxt, def); |
| 3037 | ctxt->am = oldam; |
| 3038 | } |
| 3039 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3040 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3041 | ret = xmlRelaxNGCompile(ctxt, def->content); |
| 3042 | break; |
| 3043 | case XML_RELAXNG_OPTIONAL:{ |
| 3044 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3045 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3046 | xmlRelaxNGCompile(ctxt, def->content); |
| 3047 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
| 3048 | break; |
| 3049 | } |
| 3050 | case XML_RELAXNG_ZEROORMORE:{ |
| 3051 | xmlAutomataStatePtr oldstate; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3052 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3053 | ctxt->state = |
| 3054 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 3055 | oldstate = ctxt->state; |
| 3056 | list = def->content; |
| 3057 | while (list != NULL) { |
| 3058 | xmlRelaxNGCompile(ctxt, list); |
| 3059 | list = list->next; |
| 3060 | } |
| 3061 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate); |
| 3062 | ctxt->state = |
| 3063 | xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 3064 | break; |
| 3065 | } |
| 3066 | case XML_RELAXNG_ONEORMORE:{ |
| 3067 | xmlAutomataStatePtr oldstate; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3068 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3069 | list = def->content; |
| 3070 | while (list != NULL) { |
| 3071 | xmlRelaxNGCompile(ctxt, list); |
| 3072 | list = list->next; |
| 3073 | } |
| 3074 | oldstate = ctxt->state; |
| 3075 | list = def->content; |
| 3076 | while (list != NULL) { |
| 3077 | xmlRelaxNGCompile(ctxt, list); |
| 3078 | list = list->next; |
| 3079 | } |
| 3080 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate); |
| 3081 | ctxt->state = |
| 3082 | xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 3083 | break; |
| 3084 | } |
| 3085 | case XML_RELAXNG_CHOICE:{ |
| 3086 | xmlAutomataStatePtr target = NULL; |
| 3087 | xmlAutomataStatePtr oldstate = ctxt->state; |
| 3088 | |
| 3089 | list = def->content; |
| 3090 | while (list != NULL) { |
| 3091 | ctxt->state = oldstate; |
| 3092 | ret = xmlRelaxNGCompile(ctxt, list); |
| 3093 | if (ret != 0) |
| 3094 | break; |
| 3095 | if (target == NULL) |
| 3096 | target = ctxt->state; |
| 3097 | else { |
| 3098 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, |
| 3099 | target); |
| 3100 | } |
| 3101 | list = list->next; |
| 3102 | } |
| 3103 | ctxt->state = target; |
| 3104 | |
| 3105 | break; |
| 3106 | } |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3107 | case XML_RELAXNG_REF: |
| 3108 | case XML_RELAXNG_EXTERNALREF: |
| 3109 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3110 | case XML_RELAXNG_GROUP: |
| 3111 | case XML_RELAXNG_DEF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3112 | list = def->content; |
| 3113 | while (list != NULL) { |
| 3114 | ret = xmlRelaxNGCompile(ctxt, list); |
| 3115 | if (ret != 0) |
| 3116 | break; |
| 3117 | list = list->next; |
| 3118 | } |
| 3119 | break; |
| 3120 | case XML_RELAXNG_TEXT:{ |
| 3121 | xmlAutomataStatePtr oldstate; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3122 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3123 | ctxt->state = |
| 3124 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 3125 | oldstate = ctxt->state; |
| 3126 | xmlRelaxNGCompile(ctxt, def->content); |
| 3127 | xmlAutomataNewTransition(ctxt->am, ctxt->state, |
| 3128 | ctxt->state, BAD_CAST "#text", |
| 3129 | NULL); |
| 3130 | ctxt->state = |
| 3131 | xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 3132 | break; |
| 3133 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3134 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3135 | ctxt->state = |
| 3136 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 3137 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3138 | case XML_RELAXNG_EXCEPT: |
| 3139 | case XML_RELAXNG_ATTRIBUTE: |
| 3140 | case XML_RELAXNG_INTERLEAVE: |
| 3141 | case XML_RELAXNG_NOT_ALLOWED: |
| 3142 | case XML_RELAXNG_DATATYPE: |
| 3143 | case XML_RELAXNG_LIST: |
| 3144 | case XML_RELAXNG_PARAM: |
| 3145 | case XML_RELAXNG_VALUE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3146 | /* This should not happen and generate an internal error */ |
| 3147 | fprintf(stderr, "RNG internal error trying to compile %s\n", |
| 3148 | xmlRelaxNGDefName(def)); |
| 3149 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3150 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3151 | return (ret); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3152 | } |
| 3153 | |
| 3154 | /** |
| 3155 | * xmlRelaxNGTryCompile: |
| 3156 | * ctxt: the RelaxNG parser context |
| 3157 | * @define: the definition tree to compile |
| 3158 | * |
| 3159 | * Try to compile the set of definitions, it works recursively, |
| 3160 | * possibly ignoring parts which cannot be compiled. |
| 3161 | * |
| 3162 | * Returns 0 if success and -1 in case of error |
| 3163 | */ |
| 3164 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3165 | xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) |
| 3166 | { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3167 | int ret = 0; |
| 3168 | xmlRelaxNGDefinePtr list; |
| 3169 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3170 | if ((ctxt == NULL) || (def == NULL)) |
| 3171 | return (-1); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3172 | |
| 3173 | if ((def->type == XML_RELAXNG_START) || |
| 3174 | (def->type == XML_RELAXNG_ELEMENT)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3175 | ret = xmlRelaxNGIsCompileable(def); |
| 3176 | if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) { |
| 3177 | ctxt->am = NULL; |
| 3178 | ret = xmlRelaxNGCompile(ctxt, def); |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3179 | #ifdef DEBUG_PROGRESSIVE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3180 | if (ret == 0) { |
| 3181 | if (def->type == XML_RELAXNG_START) |
| 3182 | xmlGenericError(xmlGenericErrorContext, |
| 3183 | "compiled the start\n"); |
| 3184 | else |
| 3185 | xmlGenericError(xmlGenericErrorContext, |
| 3186 | "compiled element %s\n", def->name); |
| 3187 | } else { |
| 3188 | if (def->type == XML_RELAXNG_START) |
| 3189 | xmlGenericError(xmlGenericErrorContext, |
| 3190 | "failed to compile the start\n"); |
| 3191 | else |
| 3192 | xmlGenericError(xmlGenericErrorContext, |
| 3193 | "failed to compile element %s\n", |
| 3194 | def->name); |
| 3195 | } |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3196 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3197 | return (ret); |
| 3198 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3199 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3200 | switch (def->type) { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3201 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3202 | ret = xmlRelaxNGTryCompile(ctxt, def->content); |
| 3203 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3204 | case XML_RELAXNG_TEXT: |
| 3205 | case XML_RELAXNG_DATATYPE: |
| 3206 | case XML_RELAXNG_LIST: |
| 3207 | case XML_RELAXNG_PARAM: |
| 3208 | case XML_RELAXNG_VALUE: |
| 3209 | case XML_RELAXNG_EMPTY: |
| 3210 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3211 | ret = 0; |
| 3212 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3213 | case XML_RELAXNG_OPTIONAL: |
| 3214 | case XML_RELAXNG_ZEROORMORE: |
| 3215 | case XML_RELAXNG_ONEORMORE: |
| 3216 | case XML_RELAXNG_CHOICE: |
| 3217 | case XML_RELAXNG_GROUP: |
| 3218 | case XML_RELAXNG_DEF: |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3219 | case XML_RELAXNG_START: |
| 3220 | case XML_RELAXNG_REF: |
| 3221 | case XML_RELAXNG_EXTERNALREF: |
| 3222 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3223 | list = def->content; |
| 3224 | while (list != NULL) { |
| 3225 | ret = xmlRelaxNGTryCompile(ctxt, list); |
| 3226 | if (ret != 0) |
| 3227 | break; |
| 3228 | list = list->next; |
| 3229 | } |
| 3230 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3231 | case XML_RELAXNG_EXCEPT: |
| 3232 | case XML_RELAXNG_ATTRIBUTE: |
| 3233 | case XML_RELAXNG_INTERLEAVE: |
| 3234 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3235 | ret = 0; |
| 3236 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3237 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3238 | return (ret); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 3239 | } |
| 3240 | |
| 3241 | /************************************************************************ |
| 3242 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3243 | * Parsing functions * |
| 3244 | * * |
| 3245 | ************************************************************************/ |
| 3246 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3247 | static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr |
| 3248 | ctxt, xmlNodePtr node); |
| 3249 | static xmlRelaxNGDefinePtr xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr |
| 3250 | ctxt, xmlNodePtr node); |
| 3251 | static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr |
| 3252 | ctxt, xmlNodePtr nodes, |
| 3253 | int group); |
| 3254 | static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr |
| 3255 | ctxt, xmlNodePtr node); |
| 3256 | static xmlRelaxNGPtr xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, |
| 3257 | xmlNodePtr node); |
| 3258 | static int xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt, |
| 3259 | xmlNodePtr nodes); |
| 3260 | static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr |
| 3261 | ctxt, xmlNodePtr node, |
| 3262 | xmlRelaxNGDefinePtr |
| 3263 | def); |
| 3264 | static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr |
| 3265 | ctxt, xmlNodePtr nodes); |
| 3266 | static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 3267 | xmlRelaxNGDefinePtr define, |
| 3268 | xmlNodePtr elem); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3269 | |
| 3270 | |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 3271 | #define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content)) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3272 | |
| 3273 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3274 | * xmlRelaxNGIsNullable: |
| 3275 | * @define: the definition to verify |
| 3276 | * |
| 3277 | * Check if a definition is nullable. |
| 3278 | * |
| 3279 | * Returns 1 if yes, 0 if no and -1 in case of error |
| 3280 | */ |
| 3281 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3282 | xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define) |
| 3283 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3284 | int ret; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3285 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3286 | if (define == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3287 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3288 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3289 | if (define->dflags & IS_NULLABLE) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3290 | return (1); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3291 | if (define->dflags & IS_NOT_NULLABLE) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3292 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3293 | switch (define->type) { |
| 3294 | case XML_RELAXNG_EMPTY: |
| 3295 | case XML_RELAXNG_TEXT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3296 | ret = 1; |
| 3297 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3298 | case XML_RELAXNG_NOOP: |
| 3299 | case XML_RELAXNG_DEF: |
| 3300 | case XML_RELAXNG_REF: |
| 3301 | case XML_RELAXNG_EXTERNALREF: |
| 3302 | case XML_RELAXNG_PARENTREF: |
| 3303 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3304 | ret = xmlRelaxNGIsNullable(define->content); |
| 3305 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3306 | case XML_RELAXNG_EXCEPT: |
| 3307 | case XML_RELAXNG_NOT_ALLOWED: |
| 3308 | case XML_RELAXNG_ELEMENT: |
| 3309 | case XML_RELAXNG_DATATYPE: |
| 3310 | case XML_RELAXNG_PARAM: |
| 3311 | case XML_RELAXNG_VALUE: |
| 3312 | case XML_RELAXNG_LIST: |
| 3313 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3314 | ret = 0; |
| 3315 | break; |
| 3316 | case XML_RELAXNG_CHOICE:{ |
| 3317 | xmlRelaxNGDefinePtr list = define->content; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3318 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3319 | while (list != NULL) { |
| 3320 | ret = xmlRelaxNGIsNullable(list); |
| 3321 | if (ret != 0) |
| 3322 | goto done; |
| 3323 | list = list->next; |
| 3324 | } |
| 3325 | ret = 0; |
| 3326 | break; |
| 3327 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3328 | case XML_RELAXNG_START: |
| 3329 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3330 | case XML_RELAXNG_GROUP:{ |
| 3331 | xmlRelaxNGDefinePtr list = define->content; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3332 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3333 | while (list != NULL) { |
| 3334 | ret = xmlRelaxNGIsNullable(list); |
| 3335 | if (ret != 1) |
| 3336 | goto done; |
| 3337 | list = list->next; |
| 3338 | } |
| 3339 | return (1); |
| 3340 | } |
| 3341 | default: |
| 3342 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3343 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3344 | done: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3345 | if (ret == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3346 | define->dflags |= IS_NOT_NULLABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3347 | if (ret == 1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3348 | define->dflags |= IS_NULLABLE; |
| 3349 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3350 | } |
| 3351 | |
| 3352 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3353 | * xmlRelaxNGIsBlank: |
| 3354 | * @str: a string |
| 3355 | * |
| 3356 | * Check if a string is ignorable c.f. 4.2. Whitespace |
| 3357 | * |
| 3358 | * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise |
| 3359 | */ |
| 3360 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3361 | xmlRelaxNGIsBlank(xmlChar * str) |
| 3362 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3363 | if (str == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3364 | return (1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3365 | while (*str != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3366 | if (!(IS_BLANK_CH(*str))) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3367 | return (0); |
| 3368 | str++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3369 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3370 | return (1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3371 | } |
| 3372 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3373 | /** |
| 3374 | * xmlRelaxNGGetDataTypeLibrary: |
| 3375 | * @ctxt: a Relax-NG parser context |
| 3376 | * @node: the current data or value element |
| 3377 | * |
| 3378 | * Applies algorithm from 4.3. datatypeLibrary attribute |
| 3379 | * |
| 3380 | * Returns the datatypeLibary value or NULL if not found |
| 3381 | */ |
| 3382 | static xmlChar * |
| 3383 | xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3384 | xmlNodePtr node) |
| 3385 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3386 | xmlChar *ret, *escape; |
| 3387 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3388 | if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3389 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
| 3390 | if (ret != NULL) { |
| 3391 | if (ret[0] == 0) { |
| 3392 | xmlFree(ret); |
| 3393 | return (NULL); |
| 3394 | } |
| 3395 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
| 3396 | if (escape == NULL) { |
| 3397 | return (ret); |
| 3398 | } |
| 3399 | xmlFree(ret); |
| 3400 | return (escape); |
| 3401 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3402 | } |
| 3403 | node = node->parent; |
| 3404 | while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3405 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
| 3406 | if (ret != NULL) { |
| 3407 | if (ret[0] == 0) { |
| 3408 | xmlFree(ret); |
| 3409 | return (NULL); |
| 3410 | } |
| 3411 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
| 3412 | if (escape == NULL) { |
| 3413 | return (ret); |
| 3414 | } |
| 3415 | xmlFree(ret); |
| 3416 | return (escape); |
| 3417 | } |
| 3418 | node = node->parent; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3419 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3420 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3421 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3422 | |
| 3423 | /** |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3424 | * xmlRelaxNGParseValue: |
| 3425 | * @ctxt: a Relax-NG parser context |
| 3426 | * @node: the data node. |
| 3427 | * |
| 3428 | * parse the content of a RelaxNG value node. |
| 3429 | * |
| 3430 | * Returns the definition pointer or NULL in case of error |
| 3431 | */ |
| 3432 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3433 | xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 3434 | { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3435 | xmlRelaxNGDefinePtr def = NULL; |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 3436 | xmlRelaxNGTypeLibraryPtr lib = NULL; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3437 | xmlChar *type; |
| 3438 | xmlChar *library; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3439 | int success = 0; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3440 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3441 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3442 | if (def == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3443 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3444 | def->type = XML_RELAXNG_VALUE; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3445 | |
| 3446 | type = xmlGetProp(node, BAD_CAST "type"); |
| 3447 | if (type != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3448 | xmlRelaxNGNormExtSpace(type); |
| 3449 | if (xmlValidateNCName(type, 0)) { |
| 3450 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE, |
| 3451 | "value type '%s' is not an NCName\n", type, NULL); |
| 3452 | } |
| 3453 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
| 3454 | if (library == NULL) |
| 3455 | library = |
| 3456 | xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3457 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3458 | def->name = type; |
| 3459 | def->ns = library; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3460 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3461 | lib = (xmlRelaxNGTypeLibraryPtr) |
| 3462 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
| 3463 | if (lib == NULL) { |
| 3464 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB, |
| 3465 | "Use of unregistered type library '%s'\n", library, |
| 3466 | NULL); |
| 3467 | def->data = NULL; |
| 3468 | } else { |
| 3469 | def->data = lib; |
| 3470 | if (lib->have == NULL) { |
| 3471 | xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB, |
| 3472 | "Internal error with type library '%s': no 'have'\n", |
| 3473 | library, NULL); |
| 3474 | } else { |
| 3475 | success = lib->have(lib->data, def->name); |
| 3476 | if (success != 1) { |
| 3477 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND, |
| 3478 | "Error type '%s' is not exported by type library '%s'\n", |
| 3479 | def->name, library); |
| 3480 | } |
| 3481 | } |
| 3482 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3483 | } |
| 3484 | if (node->children == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3485 | def->value = xmlStrdup(BAD_CAST ""); |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 3486 | } else if (((node->children->type != XML_TEXT_NODE) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3487 | (node->children->type != XML_CDATA_SECTION_NODE)) || |
| 3488 | (node->children->next != NULL)) { |
| 3489 | xmlRngPErr(ctxt, node, XML_RNGP_TEXT_EXPECTED, |
| 3490 | "Expecting a single text value for <value>content\n", |
| 3491 | NULL, NULL); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3492 | } else if (def != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3493 | def->value = xmlNodeGetContent(node); |
| 3494 | if (def->value == NULL) { |
| 3495 | xmlRngPErr(ctxt, node, XML_RNGP_VALUE_NO_CONTENT, |
| 3496 | "Element <value> has no content\n", NULL, NULL); |
| 3497 | } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) { |
| 3498 | void *val = NULL; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3499 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3500 | success = |
| 3501 | lib->check(lib->data, def->name, def->value, &val, node); |
| 3502 | if (success != 1) { |
| 3503 | xmlRngPErr(ctxt, node, XML_RNGP_INVALID_VALUE, |
| 3504 | "Value '%s' is not acceptable for type '%s'\n", |
| 3505 | def->value, def->name); |
| 3506 | } else { |
| 3507 | if (val != NULL) |
| 3508 | def->attrs = val; |
| 3509 | } |
| 3510 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3511 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3512 | return (def); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3513 | } |
| 3514 | |
| 3515 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3516 | * xmlRelaxNGParseData: |
| 3517 | * @ctxt: a Relax-NG parser context |
| 3518 | * @node: the data node. |
| 3519 | * |
| 3520 | * parse the content of a RelaxNG data node. |
| 3521 | * |
| 3522 | * Returns the definition pointer or NULL in case of error |
| 3523 | */ |
| 3524 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3525 | xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 3526 | { |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3527 | xmlRelaxNGDefinePtr def = NULL, except, last = NULL; |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 3528 | xmlRelaxNGDefinePtr param, lastparam = NULL; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3529 | xmlRelaxNGTypeLibraryPtr lib; |
| 3530 | xmlChar *type; |
| 3531 | xmlChar *library; |
| 3532 | xmlNodePtr content; |
| 3533 | int tmp; |
| 3534 | |
| 3535 | type = xmlGetProp(node, BAD_CAST "type"); |
| 3536 | if (type == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3537 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_MISSING, "data has no type\n", NULL, |
| 3538 | NULL); |
| 3539 | return (NULL); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3540 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3541 | xmlRelaxNGNormExtSpace(type); |
| 3542 | if (xmlValidateNCName(type, 0)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3543 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE, |
| 3544 | "data type '%s' is not an NCName\n", type, NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3545 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3546 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
| 3547 | if (library == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3548 | library = |
| 3549 | xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3550 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3551 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3552 | if (def == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3553 | xmlFree(type); |
| 3554 | return (NULL); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3555 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3556 | def->type = XML_RELAXNG_DATATYPE; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3557 | def->name = type; |
| 3558 | def->ns = library; |
| 3559 | |
| 3560 | lib = (xmlRelaxNGTypeLibraryPtr) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3561 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3562 | if (lib == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3563 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB, |
| 3564 | "Use of unregistered type library '%s'\n", library, |
| 3565 | NULL); |
| 3566 | def->data = NULL; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3567 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3568 | def->data = lib; |
| 3569 | if (lib->have == NULL) { |
| 3570 | xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB, |
| 3571 | "Internal error with type library '%s': no 'have'\n", |
| 3572 | library, NULL); |
| 3573 | } else { |
| 3574 | tmp = lib->have(lib->data, def->name); |
| 3575 | if (tmp != 1) { |
| 3576 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND, |
| 3577 | "Error type '%s' is not exported by type library '%s'\n", |
| 3578 | def->name, library); |
| 3579 | } else |
| 3580 | if ((xmlStrEqual |
| 3581 | (library, |
| 3582 | BAD_CAST |
| 3583 | "http://www.w3.org/2001/XMLSchema-datatypes")) |
| 3584 | && ((xmlStrEqual(def->name, BAD_CAST "IDREF")) |
| 3585 | || (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) { |
| 3586 | ctxt->idref = 1; |
| 3587 | } |
| 3588 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3589 | } |
| 3590 | content = node->children; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3591 | |
| 3592 | /* |
| 3593 | * Handle optional params |
| 3594 | */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3595 | while (content != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3596 | if (!xmlStrEqual(content->name, BAD_CAST "param")) |
| 3597 | break; |
| 3598 | if (xmlStrEqual(library, |
| 3599 | BAD_CAST "http://relaxng.org/ns/structure/1.0")) { |
| 3600 | xmlRngPErr(ctxt, node, XML_RNGP_PARAM_FORBIDDEN, |
| 3601 | "Type library '%s' does not allow type parameters\n", |
| 3602 | library, NULL); |
| 3603 | content = content->next; |
| 3604 | while ((content != NULL) && |
| 3605 | (xmlStrEqual(content->name, BAD_CAST "param"))) |
| 3606 | content = content->next; |
| 3607 | } else { |
| 3608 | param = xmlRelaxNGNewDefine(ctxt, node); |
| 3609 | if (param != NULL) { |
| 3610 | param->type = XML_RELAXNG_PARAM; |
| 3611 | param->name = xmlGetProp(content, BAD_CAST "name"); |
| 3612 | if (param->name == NULL) { |
| 3613 | xmlRngPErr(ctxt, node, XML_RNGP_PARAM_NAME_MISSING, |
| 3614 | "param has no name\n", NULL, NULL); |
| 3615 | } |
| 3616 | param->value = xmlNodeGetContent(content); |
| 3617 | if (lastparam == NULL) { |
| 3618 | def->attrs = lastparam = param; |
| 3619 | } else { |
| 3620 | lastparam->next = param; |
| 3621 | lastparam = param; |
| 3622 | } |
| 3623 | if (lib != NULL) { |
| 3624 | } |
| 3625 | } |
| 3626 | content = content->next; |
| 3627 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3628 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3629 | /* |
| 3630 | * Handle optional except |
| 3631 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3632 | if ((content != NULL) |
| 3633 | && (xmlStrEqual(content->name, BAD_CAST "except"))) { |
| 3634 | xmlNodePtr child; |
| 3635 | xmlRelaxNGDefinePtr tmp2, last2 = NULL; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3636 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3637 | except = xmlRelaxNGNewDefine(ctxt, node); |
| 3638 | if (except == NULL) { |
| 3639 | return (def); |
| 3640 | } |
| 3641 | except->type = XML_RELAXNG_EXCEPT; |
| 3642 | child = content->children; |
| 3643 | if (last == NULL) { |
| 3644 | def->content = except; |
| 3645 | } else { |
| 3646 | last->next = except; |
| 3647 | } |
| 3648 | if (child == NULL) { |
| 3649 | xmlRngPErr(ctxt, content, XML_RNGP_EXCEPT_NO_CONTENT, |
| 3650 | "except has no content\n", NULL, NULL); |
| 3651 | } |
| 3652 | while (child != NULL) { |
| 3653 | tmp2 = xmlRelaxNGParsePattern(ctxt, child); |
| 3654 | if (tmp2 != NULL) { |
| 3655 | if (last2 == NULL) { |
| 3656 | except->content = last2 = tmp2; |
| 3657 | } else { |
| 3658 | last2->next = tmp2; |
| 3659 | last2 = tmp2; |
| 3660 | } |
| 3661 | } |
| 3662 | child = child->next; |
| 3663 | } |
| 3664 | content = content->next; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3665 | } |
| 3666 | /* |
| 3667 | * Check there is no unhandled data |
| 3668 | */ |
| 3669 | if (content != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3670 | xmlRngPErr(ctxt, content, XML_RNGP_DATA_CONTENT, |
| 3671 | "Element data has unexpected content %s\n", |
| 3672 | content->name, NULL); |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3673 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3674 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3675 | return (def); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3676 | } |
| 3677 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3678 | static const xmlChar *invalidName = BAD_CAST "\1"; |
| 3679 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3680 | /** |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3681 | * xmlRelaxNGCompareNameClasses: |
| 3682 | * @defs1: the first element/attribute defs |
| 3683 | * @defs2: the second element/attribute defs |
| 3684 | * @name: the restriction on the name |
| 3685 | * @ns: the restriction on the namespace |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3686 | * |
| 3687 | * Compare the 2 lists of element definitions. The comparison is |
| 3688 | * that if both lists do not accept the same QNames, it returns 1 |
| 3689 | * If the 2 lists can accept the same QName the comparison returns 0 |
| 3690 | * |
| 3691 | * Returns 1 disttinct, 0 if equal |
| 3692 | */ |
| 3693 | static int |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3694 | xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3695 | xmlRelaxNGDefinePtr def2) |
| 3696 | { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3697 | int ret = 1; |
| 3698 | xmlNode node; |
| 3699 | xmlNs ns; |
| 3700 | xmlRelaxNGValidCtxt ctxt; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3701 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3702 | ctxt.flags = FLAGS_IGNORABLE; |
| 3703 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 3704 | memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt)); |
| 3705 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3706 | if ((def1->type == XML_RELAXNG_ELEMENT) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3707 | (def1->type == XML_RELAXNG_ATTRIBUTE)) { |
| 3708 | if (def2->type == XML_RELAXNG_TEXT) |
| 3709 | return (1); |
| 3710 | if (def1->name != NULL) { |
| 3711 | node.name = def1->name; |
| 3712 | } else { |
| 3713 | node.name = invalidName; |
| 3714 | } |
| 3715 | node.ns = &ns; |
| 3716 | if (def1->ns != NULL) { |
| 3717 | if (def1->ns[0] == 0) { |
| 3718 | node.ns = NULL; |
| 3719 | } else { |
| 3720 | ns.href = def1->ns; |
| 3721 | } |
| 3722 | } else { |
| 3723 | ns.href = invalidName; |
| 3724 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3725 | if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3726 | if (def1->nameClass != NULL) { |
| 3727 | ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2); |
| 3728 | } else { |
| 3729 | ret = 0; |
| 3730 | } |
| 3731 | } else { |
| 3732 | ret = 1; |
| 3733 | } |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3734 | } else if (def1->type == XML_RELAXNG_TEXT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3735 | if (def2->type == XML_RELAXNG_TEXT) |
| 3736 | return (0); |
| 3737 | return (1); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3738 | } else if (def1->type == XML_RELAXNG_EXCEPT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3739 | TODO ret = 0; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3740 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3741 | TODO ret = 0; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3742 | } |
| 3743 | if (ret == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3744 | return (ret); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3745 | if ((def2->type == XML_RELAXNG_ELEMENT) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3746 | (def2->type == XML_RELAXNG_ATTRIBUTE)) { |
| 3747 | if (def2->name != NULL) { |
| 3748 | node.name = def2->name; |
| 3749 | } else { |
| 3750 | node.name = invalidName; |
| 3751 | } |
| 3752 | node.ns = &ns; |
| 3753 | if (def2->ns != NULL) { |
| 3754 | if (def2->ns[0] == 0) { |
| 3755 | node.ns = NULL; |
| 3756 | } else { |
| 3757 | ns.href = def2->ns; |
| 3758 | } |
| 3759 | } else { |
| 3760 | ns.href = invalidName; |
| 3761 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3762 | if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3763 | if (def2->nameClass != NULL) { |
| 3764 | ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1); |
| 3765 | } else { |
| 3766 | ret = 0; |
| 3767 | } |
| 3768 | } else { |
| 3769 | ret = 1; |
| 3770 | } |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3771 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3772 | TODO ret = 0; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3773 | } |
| 3774 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3775 | return (ret); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3776 | } |
| 3777 | |
| 3778 | /** |
| 3779 | * xmlRelaxNGCompareElemDefLists: |
| 3780 | * @ctxt: a Relax-NG parser context |
| 3781 | * @defs1: the first list of element/attribute defs |
| 3782 | * @defs2: the second list of element/attribute defs |
| 3783 | * |
| 3784 | * Compare the 2 lists of element or attribute definitions. The comparison |
| 3785 | * is that if both lists do not accept the same QNames, it returns 1 |
| 3786 | * If the 2 lists can accept the same QName the comparison returns 0 |
| 3787 | * |
| 3788 | * Returns 1 disttinct, 0 if equal |
| 3789 | */ |
| 3790 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3791 | xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt |
| 3792 | ATTRIBUTE_UNUSED, xmlRelaxNGDefinePtr * def1, |
| 3793 | xmlRelaxNGDefinePtr * def2) |
| 3794 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3795 | xmlRelaxNGDefinePtr *basedef2 = def2; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3796 | |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3797 | if ((def1 == NULL) || (def2 == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3798 | return (1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3799 | if ((*def1 == NULL) || (*def2 == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3800 | return (1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3801 | while (*def1 != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3802 | while ((*def2) != NULL) { |
| 3803 | if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0) |
| 3804 | return (0); |
| 3805 | def2++; |
| 3806 | } |
| 3807 | def2 = basedef2; |
| 3808 | def1++; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3809 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3810 | return (1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3811 | } |
| 3812 | |
| 3813 | /** |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3814 | * xmlRelaxNGGenerateAttributes: |
| 3815 | * @ctxt: a Relax-NG parser context |
| 3816 | * @def: the definition definition |
| 3817 | * |
| 3818 | * Check if the definition can only generate attributes |
| 3819 | * |
| 3820 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 3821 | */ |
| 3822 | static int |
| 3823 | xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3824 | xmlRelaxNGDefinePtr def) |
| 3825 | { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3826 | xmlRelaxNGDefinePtr parent, cur, tmp; |
| 3827 | |
| 3828 | /* |
| 3829 | * Don't run that check in case of error. Infinite recursion |
| 3830 | * becomes possible. |
| 3831 | */ |
| 3832 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3833 | return (-1); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3834 | |
| 3835 | parent = NULL; |
| 3836 | cur = def; |
| 3837 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3838 | if ((cur->type == XML_RELAXNG_ELEMENT) || |
| 3839 | (cur->type == XML_RELAXNG_TEXT) || |
| 3840 | (cur->type == XML_RELAXNG_DATATYPE) || |
| 3841 | (cur->type == XML_RELAXNG_PARAM) || |
| 3842 | (cur->type == XML_RELAXNG_LIST) || |
| 3843 | (cur->type == XML_RELAXNG_VALUE) || |
| 3844 | (cur->type == XML_RELAXNG_EMPTY)) |
| 3845 | return (0); |
| 3846 | if ((cur->type == XML_RELAXNG_CHOICE) || |
| 3847 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
| 3848 | (cur->type == XML_RELAXNG_GROUP) || |
| 3849 | (cur->type == XML_RELAXNG_ONEORMORE) || |
| 3850 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
| 3851 | (cur->type == XML_RELAXNG_OPTIONAL) || |
| 3852 | (cur->type == XML_RELAXNG_PARENTREF) || |
| 3853 | (cur->type == XML_RELAXNG_EXTERNALREF) || |
| 3854 | (cur->type == XML_RELAXNG_REF) || |
| 3855 | (cur->type == XML_RELAXNG_DEF)) { |
| 3856 | if (cur->content != NULL) { |
| 3857 | parent = cur; |
| 3858 | cur = cur->content; |
| 3859 | tmp = cur; |
| 3860 | while (tmp != NULL) { |
| 3861 | tmp->parent = parent; |
| 3862 | tmp = tmp->next; |
| 3863 | } |
| 3864 | continue; |
| 3865 | } |
| 3866 | } |
| 3867 | if (cur == def) |
| 3868 | break; |
| 3869 | if (cur->next != NULL) { |
| 3870 | cur = cur->next; |
| 3871 | continue; |
| 3872 | } |
| 3873 | do { |
| 3874 | cur = cur->parent; |
| 3875 | if (cur == NULL) |
| 3876 | break; |
| 3877 | if (cur == def) |
| 3878 | return (1); |
| 3879 | if (cur->next != NULL) { |
| 3880 | cur = cur->next; |
| 3881 | break; |
| 3882 | } |
| 3883 | } while (cur != NULL); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3884 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3885 | return (1); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3886 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3887 | |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3888 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3889 | * xmlRelaxNGGetElements: |
| 3890 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3891 | * @def: the definition definition |
| 3892 | * @eora: gather elements (0) or attributes (1) |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3893 | * |
| 3894 | * Compute the list of top elements a definition can generate |
| 3895 | * |
| 3896 | * Returns a list of elements or NULL if none was found. |
| 3897 | */ |
| 3898 | static xmlRelaxNGDefinePtr * |
| 3899 | xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3900 | xmlRelaxNGDefinePtr def, int eora) |
| 3901 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3902 | xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3903 | int len = 0; |
| 3904 | int max = 0; |
| 3905 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3906 | /* |
| 3907 | * Don't run that check in case of error. Infinite recursion |
| 3908 | * becomes possible. |
| 3909 | */ |
| 3910 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3911 | return (NULL); |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3912 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3913 | parent = NULL; |
| 3914 | cur = def; |
| 3915 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3916 | if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) || |
| 3917 | (cur->type == XML_RELAXNG_TEXT))) || |
| 3918 | ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) { |
| 3919 | if (ret == NULL) { |
| 3920 | max = 10; |
| 3921 | ret = (xmlRelaxNGDefinePtr *) |
| 3922 | xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
| 3923 | if (ret == NULL) { |
| 3924 | xmlRngPErrMemory(ctxt, "getting element list\n"); |
| 3925 | return (NULL); |
| 3926 | } |
| 3927 | } else if (max <= len) { |
| 3928 | max *= 2; |
| 3929 | ret = |
| 3930 | xmlRealloc(ret, |
| 3931 | (max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
| 3932 | if (ret == NULL) { |
| 3933 | xmlRngPErrMemory(ctxt, "getting element list\n"); |
| 3934 | return (NULL); |
| 3935 | } |
| 3936 | } |
| 3937 | ret[len++] = cur; |
| 3938 | ret[len] = NULL; |
| 3939 | } else if ((cur->type == XML_RELAXNG_CHOICE) || |
| 3940 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
| 3941 | (cur->type == XML_RELAXNG_GROUP) || |
| 3942 | (cur->type == XML_RELAXNG_ONEORMORE) || |
| 3943 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
| 3944 | (cur->type == XML_RELAXNG_OPTIONAL) || |
| 3945 | (cur->type == XML_RELAXNG_PARENTREF) || |
| 3946 | (cur->type == XML_RELAXNG_REF) || |
| 3947 | (cur->type == XML_RELAXNG_DEF)) { |
| 3948 | /* |
| 3949 | * Don't go within elements or attributes or string values. |
| 3950 | * Just gather the element top list |
| 3951 | */ |
| 3952 | if (cur->content != NULL) { |
| 3953 | parent = cur; |
| 3954 | cur = cur->content; |
| 3955 | tmp = cur; |
| 3956 | while (tmp != NULL) { |
| 3957 | tmp->parent = parent; |
| 3958 | tmp = tmp->next; |
| 3959 | } |
| 3960 | continue; |
| 3961 | } |
| 3962 | } |
| 3963 | if (cur == def) |
| 3964 | break; |
| 3965 | if (cur->next != NULL) { |
| 3966 | cur = cur->next; |
| 3967 | continue; |
| 3968 | } |
| 3969 | do { |
| 3970 | cur = cur->parent; |
| 3971 | if (cur == NULL) |
| 3972 | break; |
| 3973 | if (cur == def) |
| 3974 | return (ret); |
| 3975 | if (cur->next != NULL) { |
| 3976 | cur = cur->next; |
| 3977 | break; |
| 3978 | } |
| 3979 | } while (cur != NULL); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3980 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3981 | return (ret); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3982 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3983 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3984 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3985 | * xmlRelaxNGCheckChoiceDeterminism: |
| 3986 | * @ctxt: a Relax-NG parser context |
| 3987 | * @def: the choice definition |
| 3988 | * |
| 3989 | * Also used to find indeterministic pattern in choice |
| 3990 | */ |
| 3991 | static void |
| 3992 | xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3993 | xmlRelaxNGDefinePtr def) |
| 3994 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3995 | xmlRelaxNGDefinePtr **list; |
| 3996 | xmlRelaxNGDefinePtr cur; |
| 3997 | int nbchild = 0, i, j, ret; |
| 3998 | int is_nullable = 0; |
| 3999 | int is_indeterminist = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4000 | xmlHashTablePtr triage = NULL; |
| 4001 | int is_triable = 1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4002 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4003 | if ((def == NULL) || (def->type != XML_RELAXNG_CHOICE)) |
| 4004 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4005 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4006 | if (def->dflags & IS_PROCESSED) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4007 | return; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4008 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4009 | /* |
| 4010 | * Don't run that check in case of error. Infinite recursion |
| 4011 | * becomes possible. |
| 4012 | */ |
| 4013 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4014 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4015 | |
| 4016 | is_nullable = xmlRelaxNGIsNullable(def); |
| 4017 | |
| 4018 | cur = def->content; |
| 4019 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4020 | nbchild++; |
| 4021 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4022 | } |
| 4023 | |
| 4024 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4025 | sizeof(xmlRelaxNGDefinePtr |
| 4026 | *)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4027 | if (list == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4028 | xmlRngPErrMemory(ctxt, "building choice\n"); |
| 4029 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4030 | } |
| 4031 | i = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4032 | /* |
| 4033 | * a bit strong but safe |
| 4034 | */ |
| 4035 | if (is_nullable == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4036 | triage = xmlHashCreate(10); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4037 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4038 | is_triable = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4039 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4040 | cur = def->content; |
| 4041 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4042 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 0); |
| 4043 | if ((list[i] == NULL) || (list[i][0] == NULL)) { |
| 4044 | is_triable = 0; |
| 4045 | } else if (is_triable == 1) { |
| 4046 | xmlRelaxNGDefinePtr *tmp; |
| 4047 | int res; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4048 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4049 | tmp = list[i]; |
| 4050 | while ((*tmp != NULL) && (is_triable == 1)) { |
| 4051 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
| 4052 | res = xmlHashAddEntry2(triage, |
| 4053 | BAD_CAST "#text", NULL, |
| 4054 | (void *) cur); |
| 4055 | if (res != 0) |
| 4056 | is_triable = -1; |
| 4057 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
| 4058 | ((*tmp)->name != NULL)) { |
| 4059 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4060 | res = xmlHashAddEntry2(triage, |
| 4061 | (*tmp)->name, NULL, |
| 4062 | (void *) cur); |
| 4063 | else |
| 4064 | res = xmlHashAddEntry2(triage, |
| 4065 | (*tmp)->name, (*tmp)->ns, |
| 4066 | (void *) cur); |
| 4067 | if (res != 0) |
| 4068 | is_triable = -1; |
| 4069 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
| 4070 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4071 | res = xmlHashAddEntry2(triage, |
| 4072 | BAD_CAST "#any", NULL, |
| 4073 | (void *) cur); |
| 4074 | else |
| 4075 | res = xmlHashAddEntry2(triage, |
| 4076 | BAD_CAST "#any", (*tmp)->ns, |
| 4077 | (void *) cur); |
| 4078 | if (res != 0) |
| 4079 | is_triable = -1; |
| 4080 | } else { |
| 4081 | is_triable = -1; |
| 4082 | } |
| 4083 | tmp++; |
| 4084 | } |
| 4085 | } |
| 4086 | i++; |
| 4087 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4088 | } |
| 4089 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4090 | for (i = 0; i < nbchild; i++) { |
| 4091 | if (list[i] == NULL) |
| 4092 | continue; |
| 4093 | for (j = 0; j < i; j++) { |
| 4094 | if (list[j] == NULL) |
| 4095 | continue; |
| 4096 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
| 4097 | if (ret == 0) { |
| 4098 | is_indeterminist = 1; |
| 4099 | } |
| 4100 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4101 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4102 | for (i = 0; i < nbchild; i++) { |
| 4103 | if (list[i] != NULL) |
| 4104 | xmlFree(list[i]); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4105 | } |
| 4106 | |
| 4107 | xmlFree(list); |
| 4108 | if (is_indeterminist) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4109 | def->dflags |= IS_INDETERMINIST; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4110 | } |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4111 | if (is_triable == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4112 | def->dflags |= IS_TRIABLE; |
| 4113 | def->data = triage; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4114 | } else if (triage != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4115 | xmlHashFree(triage, NULL); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4116 | } |
| 4117 | def->dflags |= IS_PROCESSED; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4118 | } |
| 4119 | |
| 4120 | /** |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4121 | * xmlRelaxNGCheckGroupAttrs: |
| 4122 | * @ctxt: a Relax-NG parser context |
| 4123 | * @def: the group definition |
| 4124 | * |
| 4125 | * Detects violations of rule 7.3 |
| 4126 | */ |
| 4127 | static void |
| 4128 | xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4129 | xmlRelaxNGDefinePtr def) |
| 4130 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4131 | xmlRelaxNGDefinePtr **list; |
| 4132 | xmlRelaxNGDefinePtr cur; |
| 4133 | int nbchild = 0, i, j, ret; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4134 | |
| 4135 | if ((def == NULL) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4136 | ((def->type != XML_RELAXNG_GROUP) && |
| 4137 | (def->type != XML_RELAXNG_ELEMENT))) |
| 4138 | return; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4139 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4140 | if (def->dflags & IS_PROCESSED) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4141 | return; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4142 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4143 | /* |
| 4144 | * Don't run that check in case of error. Infinite recursion |
| 4145 | * becomes possible. |
| 4146 | */ |
| 4147 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4148 | return; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4149 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4150 | cur = def->attrs; |
| 4151 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4152 | nbchild++; |
| 4153 | cur = cur->next; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4154 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4155 | cur = def->content; |
| 4156 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4157 | nbchild++; |
| 4158 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4159 | } |
| 4160 | |
| 4161 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4162 | sizeof(xmlRelaxNGDefinePtr |
| 4163 | *)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4164 | if (list == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4165 | xmlRngPErrMemory(ctxt, "building group\n"); |
| 4166 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4167 | } |
| 4168 | i = 0; |
| 4169 | cur = def->attrs; |
| 4170 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4171 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4172 | i++; |
| 4173 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4174 | } |
| 4175 | cur = def->content; |
| 4176 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4177 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4178 | i++; |
| 4179 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4180 | } |
| 4181 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4182 | for (i = 0; i < nbchild; i++) { |
| 4183 | if (list[i] == NULL) |
| 4184 | continue; |
| 4185 | for (j = 0; j < i; j++) { |
| 4186 | if (list[j] == NULL) |
| 4187 | continue; |
| 4188 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
| 4189 | if (ret == 0) { |
| 4190 | xmlRngPErr(ctxt, def->node, XML_RNGP_GROUP_ATTR_CONFLICT, |
| 4191 | "Attributes conflicts in group\n", NULL, NULL); |
| 4192 | } |
| 4193 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4194 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4195 | for (i = 0; i < nbchild; i++) { |
| 4196 | if (list[i] != NULL) |
| 4197 | xmlFree(list[i]); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4198 | } |
| 4199 | |
| 4200 | xmlFree(list); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4201 | def->dflags |= IS_PROCESSED; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4202 | } |
| 4203 | |
| 4204 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4205 | * xmlRelaxNGComputeInterleaves: |
| 4206 | * @def: the interleave definition |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4207 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4208 | * @name: the definition name |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4209 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4210 | * A lot of work for preprocessing interleave definitions |
| 4211 | * is potentially needed to get a decent execution speed at runtime |
| 4212 | * - trying to get a total order on the element nodes generated |
| 4213 | * by the interleaves, order the list of interleave definitions |
| 4214 | * following that order. |
| 4215 | * - if <text/> is used to handle mixed content, it is better to |
| 4216 | * flag this in the define and simplify the runtime checking |
| 4217 | * algorithm |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4218 | */ |
| 4219 | static void |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4220 | xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4221 | xmlRelaxNGParserCtxtPtr ctxt, |
| 4222 | xmlChar * name ATTRIBUTE_UNUSED) |
| 4223 | { |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4224 | xmlRelaxNGDefinePtr cur, *tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4225 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4226 | xmlRelaxNGPartitionPtr partitions = NULL; |
| 4227 | xmlRelaxNGInterleaveGroupPtr *groups = NULL; |
| 4228 | xmlRelaxNGInterleaveGroupPtr group; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4229 | int i, j, ret, res; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4230 | int nbgroups = 0; |
| 4231 | int nbchild = 0; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 4232 | int is_mixed = 0; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4233 | int is_determinist = 1; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4234 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4235 | /* |
| 4236 | * Don't run that check in case of error. Infinite recursion |
| 4237 | * becomes possible. |
| 4238 | */ |
| 4239 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4240 | return; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4241 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4242 | #ifdef DEBUG_INTERLEAVE |
| 4243 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4244 | "xmlRelaxNGComputeInterleaves(%s)\n", name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4245 | #endif |
| 4246 | cur = def->content; |
| 4247 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4248 | nbchild++; |
| 4249 | cur = cur->next; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4250 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4251 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4252 | #ifdef DEBUG_INTERLEAVE |
| 4253 | xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild); |
| 4254 | #endif |
| 4255 | groups = (xmlRelaxNGInterleaveGroupPtr *) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4256 | xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4257 | if (groups == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4258 | goto error; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4259 | cur = def->content; |
| 4260 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4261 | groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr) |
| 4262 | xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup)); |
| 4263 | if (groups[nbgroups] == NULL) |
| 4264 | goto error; |
| 4265 | if (cur->type == XML_RELAXNG_TEXT) |
| 4266 | is_mixed++; |
| 4267 | groups[nbgroups]->rule = cur; |
| 4268 | groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0); |
| 4269 | groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4270 | nbgroups++; |
| 4271 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4272 | } |
| 4273 | #ifdef DEBUG_INTERLEAVE |
| 4274 | xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups); |
| 4275 | #endif |
| 4276 | |
| 4277 | /* |
| 4278 | * Let's check that all rules makes a partitions according to 7.4 |
| 4279 | */ |
| 4280 | partitions = (xmlRelaxNGPartitionPtr) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4281 | xmlMalloc(sizeof(xmlRelaxNGPartition)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4282 | if (partitions == NULL) |
| 4283 | goto error; |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 4284 | memset(partitions, 0, sizeof(xmlRelaxNGPartition)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4285 | partitions->nbgroups = nbgroups; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4286 | partitions->triage = xmlHashCreate(nbgroups); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4287 | for (i = 0; i < nbgroups; i++) { |
| 4288 | group = groups[i]; |
| 4289 | for (j = i + 1; j < nbgroups; j++) { |
| 4290 | if (groups[j] == NULL) |
| 4291 | continue; |
| 4292 | |
| 4293 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs, |
| 4294 | groups[j]->defs); |
| 4295 | if (ret == 0) { |
| 4296 | xmlRngPErr(ctxt, def->node, XML_RNGP_ELEM_TEXT_CONFLICT, |
| 4297 | "Element or text conflicts in interleave\n", |
| 4298 | NULL, NULL); |
| 4299 | } |
| 4300 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs, |
| 4301 | groups[j]->attrs); |
| 4302 | if (ret == 0) { |
| 4303 | xmlRngPErr(ctxt, def->node, XML_RNGP_ATTR_CONFLICT, |
| 4304 | "Attributes conflicts in interleave\n", NULL, |
| 4305 | NULL); |
| 4306 | } |
| 4307 | } |
| 4308 | tmp = group->defs; |
| 4309 | if ((tmp != NULL) && (*tmp != NULL)) { |
| 4310 | while (*tmp != NULL) { |
| 4311 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
| 4312 | res = xmlHashAddEntry2(partitions->triage, |
| 4313 | BAD_CAST "#text", NULL, |
| 4314 | (void *) (long) (i + 1)); |
| 4315 | if (res != 0) |
| 4316 | is_determinist = -1; |
| 4317 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
| 4318 | ((*tmp)->name != NULL)) { |
| 4319 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4320 | res = xmlHashAddEntry2(partitions->triage, |
| 4321 | (*tmp)->name, NULL, |
| 4322 | (void *) (long) (i + 1)); |
| 4323 | else |
| 4324 | res = xmlHashAddEntry2(partitions->triage, |
| 4325 | (*tmp)->name, (*tmp)->ns, |
| 4326 | (void *) (long) (i + 1)); |
| 4327 | if (res != 0) |
| 4328 | is_determinist = -1; |
| 4329 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
| 4330 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4331 | res = xmlHashAddEntry2(partitions->triage, |
| 4332 | BAD_CAST "#any", NULL, |
| 4333 | (void *) (long) (i + 1)); |
| 4334 | else |
| 4335 | res = xmlHashAddEntry2(partitions->triage, |
| 4336 | BAD_CAST "#any", (*tmp)->ns, |
| 4337 | (void *) (long) (i + 1)); |
| 4338 | if ((*tmp)->nameClass != NULL) |
| 4339 | is_determinist = 2; |
| 4340 | if (res != 0) |
| 4341 | is_determinist = -1; |
| 4342 | } else { |
| 4343 | is_determinist = -1; |
| 4344 | } |
| 4345 | tmp++; |
| 4346 | } |
| 4347 | } else { |
| 4348 | is_determinist = 0; |
| 4349 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4350 | } |
| 4351 | partitions->groups = groups; |
| 4352 | |
| 4353 | /* |
| 4354 | * and save the partition list back in the def |
| 4355 | */ |
| 4356 | def->data = partitions; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 4357 | if (is_mixed != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4358 | def->dflags |= IS_MIXED; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4359 | if (is_determinist == 1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4360 | partitions->flags = IS_DETERMINIST; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4361 | if (is_determinist == 2) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4362 | partitions->flags = IS_DETERMINIST | IS_NEEDCHECK; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4363 | return; |
| 4364 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4365 | error: |
| 4366 | xmlRngPErrMemory(ctxt, "in interleave computation\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4367 | if (groups != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4368 | for (i = 0; i < nbgroups; i++) |
| 4369 | if (groups[i] != NULL) { |
| 4370 | if (groups[i]->defs != NULL) |
| 4371 | xmlFree(groups[i]->defs); |
| 4372 | xmlFree(groups[i]); |
| 4373 | } |
| 4374 | xmlFree(groups); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4375 | } |
| 4376 | xmlRelaxNGFreePartition(partitions); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4377 | } |
| 4378 | |
| 4379 | /** |
| 4380 | * xmlRelaxNGParseInterleave: |
| 4381 | * @ctxt: a Relax-NG parser context |
| 4382 | * @node: the data node. |
| 4383 | * |
| 4384 | * parse the content of a RelaxNG interleave node. |
| 4385 | * |
| 4386 | * Returns the definition pointer or NULL in case of error |
| 4387 | */ |
| 4388 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4389 | xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4390 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4391 | xmlRelaxNGDefinePtr def = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4392 | xmlRelaxNGDefinePtr last = NULL, cur; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4393 | xmlNodePtr child; |
| 4394 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4395 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4396 | if (def == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4397 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4398 | } |
| 4399 | def->type = XML_RELAXNG_INTERLEAVE; |
| 4400 | |
| 4401 | if (ctxt->interleaves == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4402 | ctxt->interleaves = xmlHashCreate(10); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4403 | if (ctxt->interleaves == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4404 | xmlRngPErrMemory(ctxt, "create interleaves\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4405 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4406 | char name[32]; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4407 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4408 | snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++); |
| 4409 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) { |
| 4410 | xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_ADD, |
| 4411 | "Failed to add %s to hash table\n", |
| 4412 | (const xmlChar *) name, NULL); |
| 4413 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4414 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4415 | child = node->children; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4416 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4417 | xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_NO_CONTENT, |
| 4418 | "Element interleave is empty\n", NULL, NULL); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 4419 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4420 | while (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4421 | if (IS_RELAXNG(child, "element")) { |
| 4422 | cur = xmlRelaxNGParseElement(ctxt, child); |
| 4423 | } else { |
| 4424 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 4425 | } |
| 4426 | if (cur != NULL) { |
| 4427 | cur->parent = def; |
| 4428 | if (last == NULL) { |
| 4429 | def->content = last = cur; |
| 4430 | } else { |
| 4431 | last->next = cur; |
| 4432 | last = cur; |
| 4433 | } |
| 4434 | } |
| 4435 | child = child->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4436 | } |
| 4437 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4438 | return (def); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4439 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4440 | |
| 4441 | /** |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4442 | * xmlRelaxNGParseInclude: |
| 4443 | * @ctxt: a Relax-NG parser context |
| 4444 | * @node: the include node |
| 4445 | * |
| 4446 | * Integrate the content of an include node in the current grammar |
| 4447 | * |
| 4448 | * Returns 0 in case of success or -1 in case of error |
| 4449 | */ |
| 4450 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4451 | xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4452 | { |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4453 | xmlRelaxNGIncludePtr incl; |
| 4454 | xmlNodePtr root; |
| 4455 | int ret = 0, tmp; |
| 4456 | |
| 4457 | incl = node->_private; |
| 4458 | if (incl == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4459 | xmlRngPErr(ctxt, node, XML_RNGP_INCLUDE_EMPTY, |
| 4460 | "Include node has no data\n", NULL, NULL); |
| 4461 | return (-1); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4462 | } |
| 4463 | root = xmlDocGetRootElement(incl->doc); |
| 4464 | if (root == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4465 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, "Include document is empty\n", |
| 4466 | NULL, NULL); |
| 4467 | return (-1); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4468 | } |
| 4469 | if (!xmlStrEqual(root->name, BAD_CAST "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4470 | xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING, |
| 4471 | "Include document root is not a grammar\n", NULL, NULL); |
| 4472 | return (-1); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4473 | } |
| 4474 | |
| 4475 | /* |
| 4476 | * Merge the definition from both the include and the internal list |
| 4477 | */ |
| 4478 | if (root->children != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4479 | tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children); |
| 4480 | if (tmp != 0) |
| 4481 | ret = -1; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4482 | } |
| 4483 | if (node->children != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4484 | tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children); |
| 4485 | if (tmp != 0) |
| 4486 | ret = -1; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4487 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4488 | return (ret); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4489 | } |
| 4490 | |
| 4491 | /** |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4492 | * xmlRelaxNGParseDefine: |
| 4493 | * @ctxt: a Relax-NG parser context |
| 4494 | * @node: the define node |
| 4495 | * |
| 4496 | * parse the content of a RelaxNG define element node. |
| 4497 | * |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4498 | * Returns 0 in case of success or -1 in case of error |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4499 | */ |
| 4500 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4501 | xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4502 | { |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4503 | xmlChar *name; |
| 4504 | int ret = 0, tmp; |
| 4505 | xmlRelaxNGDefinePtr def; |
| 4506 | const xmlChar *olddefine; |
| 4507 | |
| 4508 | name = xmlGetProp(node, BAD_CAST "name"); |
| 4509 | if (name == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4510 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_NAME_MISSING, |
| 4511 | "define has no name\n", NULL, NULL); |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4512 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4513 | xmlRelaxNGNormExtSpace(name); |
| 4514 | if (xmlValidateNCName(name, 0)) { |
| 4515 | xmlRngPErr(ctxt, node, XML_RNGP_INVALID_DEFINE_NAME, |
| 4516 | "define name '%s' is not an NCName\n", name, NULL); |
| 4517 | } |
| 4518 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4519 | if (def == NULL) { |
| 4520 | xmlFree(name); |
| 4521 | return (-1); |
| 4522 | } |
| 4523 | def->type = XML_RELAXNG_DEF; |
| 4524 | def->name = name; |
| 4525 | if (node->children == NULL) { |
| 4526 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_EMPTY, |
| 4527 | "define has no children\n", NULL, NULL); |
| 4528 | } else { |
| 4529 | olddefine = ctxt->define; |
| 4530 | ctxt->define = name; |
| 4531 | def->content = |
| 4532 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4533 | ctxt->define = olddefine; |
| 4534 | } |
| 4535 | if (ctxt->grammar->defs == NULL) |
| 4536 | ctxt->grammar->defs = xmlHashCreate(10); |
| 4537 | if (ctxt->grammar->defs == NULL) { |
| 4538 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED, |
| 4539 | "Could not create definition hash\n", NULL, NULL); |
| 4540 | ret = -1; |
| 4541 | } else { |
| 4542 | tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def); |
| 4543 | if (tmp < 0) { |
| 4544 | xmlRelaxNGDefinePtr prev; |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 4545 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4546 | prev = xmlHashLookup(ctxt->grammar->defs, name); |
| 4547 | if (prev == NULL) { |
| 4548 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED, |
| 4549 | "Internal error on define aggregation of %s\n", |
| 4550 | name, NULL); |
| 4551 | ret = -1; |
| 4552 | } else { |
| 4553 | while (prev->nextHash != NULL) |
| 4554 | prev = prev->nextHash; |
| 4555 | prev->nextHash = def; |
| 4556 | } |
| 4557 | } |
| 4558 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4559 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4560 | return (ret); |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4561 | } |
| 4562 | |
| 4563 | /** |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4564 | * xmlRelaxNGProcessExternalRef: |
| 4565 | * @ctxt: the parser context |
| 4566 | * @node: the externlRef node |
| 4567 | * |
| 4568 | * Process and compile an externlRef node |
| 4569 | * |
| 4570 | * Returns the xmlRelaxNGDefinePtr or NULL in case of error |
| 4571 | */ |
| 4572 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4573 | xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4574 | { |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4575 | xmlRelaxNGDocumentPtr docu; |
| 4576 | xmlNodePtr root, tmp; |
| 4577 | xmlChar *ns; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4578 | int newNs = 0, oldflags; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4579 | xmlRelaxNGDefinePtr def; |
| 4580 | |
| 4581 | docu = node->_private; |
| 4582 | if (docu != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4583 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4584 | if (def == NULL) |
| 4585 | return (NULL); |
| 4586 | def->type = XML_RELAXNG_EXTERNALREF; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4587 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4588 | if (docu->content == NULL) { |
| 4589 | /* |
| 4590 | * Then do the parsing for good |
| 4591 | */ |
| 4592 | root = xmlDocGetRootElement(docu->doc); |
| 4593 | if (root == NULL) { |
| 4594 | xmlRngPErr(ctxt, node, XML_RNGP_EXTERNALREF_EMTPY, |
| 4595 | "xmlRelaxNGParse: %s is empty\n", ctxt->URL, |
| 4596 | NULL); |
| 4597 | return (NULL); |
| 4598 | } |
| 4599 | /* |
| 4600 | * ns transmission rules |
| 4601 | */ |
| 4602 | ns = xmlGetProp(root, BAD_CAST "ns"); |
| 4603 | if (ns == NULL) { |
| 4604 | tmp = node; |
| 4605 | while ((tmp != NULL) && (tmp->type == XML_ELEMENT_NODE)) { |
| 4606 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 4607 | if (ns != NULL) { |
| 4608 | break; |
| 4609 | } |
| 4610 | tmp = tmp->parent; |
| 4611 | } |
| 4612 | if (ns != NULL) { |
| 4613 | xmlSetProp(root, BAD_CAST "ns", ns); |
| 4614 | newNs = 1; |
| 4615 | xmlFree(ns); |
| 4616 | } |
| 4617 | } else { |
| 4618 | xmlFree(ns); |
| 4619 | } |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4620 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4621 | /* |
| 4622 | * Parsing to get a precompiled schemas. |
| 4623 | */ |
| 4624 | oldflags = ctxt->flags; |
| 4625 | ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF; |
| 4626 | docu->schema = xmlRelaxNGParseDocument(ctxt, root); |
| 4627 | ctxt->flags = oldflags; |
| 4628 | if ((docu->schema != NULL) && |
| 4629 | (docu->schema->topgrammar != NULL)) { |
| 4630 | docu->content = docu->schema->topgrammar->start; |
| 4631 | } |
| 4632 | |
| 4633 | /* |
| 4634 | * the externalRef may be reused in a different ns context |
| 4635 | */ |
| 4636 | if (newNs == 1) { |
| 4637 | xmlUnsetProp(root, BAD_CAST "ns"); |
| 4638 | } |
| 4639 | } |
| 4640 | def->content = docu->content; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4641 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4642 | def = NULL; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4643 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4644 | return (def); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4645 | } |
| 4646 | |
| 4647 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4648 | * xmlRelaxNGParsePattern: |
| 4649 | * @ctxt: a Relax-NG parser context |
| 4650 | * @node: the pattern node. |
| 4651 | * |
| 4652 | * parse the content of a RelaxNG pattern node. |
| 4653 | * |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4654 | * Returns the definition pointer or NULL in case of error or if no |
| 4655 | * pattern is generated. |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4656 | */ |
| 4657 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4658 | xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4659 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4660 | xmlRelaxNGDefinePtr def = NULL; |
| 4661 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4662 | if (node == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4663 | return (NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4664 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4665 | if (IS_RELAXNG(node, "element")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4666 | def = xmlRelaxNGParseElement(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4667 | } else if (IS_RELAXNG(node, "attribute")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4668 | def = xmlRelaxNGParseAttribute(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4669 | } else if (IS_RELAXNG(node, "empty")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4670 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4671 | if (def == NULL) |
| 4672 | return (NULL); |
| 4673 | def->type = XML_RELAXNG_EMPTY; |
| 4674 | if (node->children != NULL) { |
| 4675 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_NOT_EMPTY, |
| 4676 | "empty: had a child node\n", NULL, NULL); |
| 4677 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4678 | } else if (IS_RELAXNG(node, "text")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4679 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4680 | if (def == NULL) |
| 4681 | return (NULL); |
| 4682 | def->type = XML_RELAXNG_TEXT; |
| 4683 | if (node->children != NULL) { |
| 4684 | xmlRngPErr(ctxt, node, XML_RNGP_TEXT_HAS_CHILD, |
| 4685 | "text: had a child node\n", NULL, NULL); |
| 4686 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4687 | } else if (IS_RELAXNG(node, "zeroOrMore")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4688 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4689 | if (def == NULL) |
| 4690 | return (NULL); |
| 4691 | def->type = XML_RELAXNG_ZEROORMORE; |
| 4692 | if (node->children == NULL) { |
| 4693 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4694 | "Element %s is empty\n", node->name, NULL); |
| 4695 | } else { |
| 4696 | def->content = |
| 4697 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4698 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4699 | } else if (IS_RELAXNG(node, "oneOrMore")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4700 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4701 | if (def == NULL) |
| 4702 | return (NULL); |
| 4703 | def->type = XML_RELAXNG_ONEORMORE; |
| 4704 | if (node->children == NULL) { |
| 4705 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4706 | "Element %s is empty\n", node->name, NULL); |
| 4707 | } else { |
| 4708 | def->content = |
| 4709 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4710 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4711 | } else if (IS_RELAXNG(node, "optional")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4712 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4713 | if (def == NULL) |
| 4714 | return (NULL); |
| 4715 | def->type = XML_RELAXNG_OPTIONAL; |
| 4716 | if (node->children == NULL) { |
| 4717 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4718 | "Element %s is empty\n", node->name, NULL); |
| 4719 | } else { |
| 4720 | def->content = |
| 4721 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4722 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4723 | } else if (IS_RELAXNG(node, "choice")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4724 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4725 | if (def == NULL) |
| 4726 | return (NULL); |
| 4727 | def->type = XML_RELAXNG_CHOICE; |
| 4728 | if (node->children == NULL) { |
| 4729 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4730 | "Element %s is empty\n", node->name, NULL); |
| 4731 | } else { |
| 4732 | def->content = |
| 4733 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4734 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4735 | } else if (IS_RELAXNG(node, "group")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4736 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4737 | if (def == NULL) |
| 4738 | return (NULL); |
| 4739 | def->type = XML_RELAXNG_GROUP; |
| 4740 | if (node->children == NULL) { |
| 4741 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4742 | "Element %s is empty\n", node->name, NULL); |
| 4743 | } else { |
| 4744 | def->content = |
| 4745 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4746 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4747 | } else if (IS_RELAXNG(node, "ref")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4748 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4749 | if (def == NULL) |
| 4750 | return (NULL); |
| 4751 | def->type = XML_RELAXNG_REF; |
| 4752 | def->name = xmlGetProp(node, BAD_CAST "name"); |
| 4753 | if (def->name == NULL) { |
| 4754 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NO_NAME, "ref has no name\n", |
| 4755 | NULL, NULL); |
| 4756 | } else { |
| 4757 | xmlRelaxNGNormExtSpace(def->name); |
| 4758 | if (xmlValidateNCName(def->name, 0)) { |
| 4759 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NAME_INVALID, |
| 4760 | "ref name '%s' is not an NCName\n", def->name, |
| 4761 | NULL); |
| 4762 | } |
| 4763 | } |
| 4764 | if (node->children != NULL) { |
| 4765 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NOT_EMPTY, "ref is not empty\n", |
| 4766 | NULL, NULL); |
| 4767 | } |
| 4768 | if (ctxt->grammar->refs == NULL) |
| 4769 | ctxt->grammar->refs = xmlHashCreate(10); |
| 4770 | if (ctxt->grammar->refs == NULL) { |
| 4771 | xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED, |
| 4772 | "Could not create references hash\n", NULL, NULL); |
| 4773 | def = NULL; |
| 4774 | } else { |
| 4775 | int tmp; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4776 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4777 | tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def); |
| 4778 | if (tmp < 0) { |
| 4779 | xmlRelaxNGDefinePtr prev; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4780 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4781 | prev = (xmlRelaxNGDefinePtr) |
| 4782 | xmlHashLookup(ctxt->grammar->refs, def->name); |
| 4783 | if (prev == NULL) { |
| 4784 | if (def->name != NULL) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 4785 | xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED, |
| 4786 | "Error refs definitions '%s'\n", |
| 4787 | def->name, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4788 | } else { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 4789 | xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED, |
| 4790 | "Error refs definitions\n", |
| 4791 | NULL, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4792 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4793 | def = NULL; |
| 4794 | } else { |
| 4795 | def->nextHash = prev->nextHash; |
| 4796 | prev->nextHash = def; |
| 4797 | } |
| 4798 | } |
| 4799 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 4800 | } else if (IS_RELAXNG(node, "data")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4801 | def = xmlRelaxNGParseData(ctxt, node); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 4802 | } else if (IS_RELAXNG(node, "value")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4803 | def = xmlRelaxNGParseValue(ctxt, node); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 4804 | } else if (IS_RELAXNG(node, "list")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4805 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4806 | if (def == NULL) |
| 4807 | return (NULL); |
| 4808 | def->type = XML_RELAXNG_LIST; |
| 4809 | if (node->children == NULL) { |
| 4810 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4811 | "Element %s is empty\n", node->name, NULL); |
| 4812 | } else { |
| 4813 | def->content = |
| 4814 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4815 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4816 | } else if (IS_RELAXNG(node, "interleave")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4817 | def = xmlRelaxNGParseInterleave(ctxt, node); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4818 | } else if (IS_RELAXNG(node, "externalRef")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4819 | def = xmlRelaxNGProcessExternalRef(ctxt, node); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4820 | } else if (IS_RELAXNG(node, "notAllowed")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4821 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4822 | if (def == NULL) |
| 4823 | return (NULL); |
| 4824 | def->type = XML_RELAXNG_NOT_ALLOWED; |
| 4825 | if (node->children != NULL) { |
| 4826 | xmlRngPErr(ctxt, node, XML_RNGP_NOTALLOWED_NOT_EMPTY, |
| 4827 | "xmlRelaxNGParse: notAllowed element is not empty\n", |
| 4828 | NULL, NULL); |
| 4829 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4830 | } else if (IS_RELAXNG(node, "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4831 | xmlRelaxNGGrammarPtr grammar, old; |
| 4832 | xmlRelaxNGGrammarPtr oldparent; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4833 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4834 | #ifdef DEBUG_GRAMMAR |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4835 | xmlGenericError(xmlGenericErrorContext, |
| 4836 | "Found <grammar> pattern\n"); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4837 | #endif |
| 4838 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4839 | oldparent = ctxt->parentgrammar; |
| 4840 | old = ctxt->grammar; |
| 4841 | ctxt->parentgrammar = old; |
| 4842 | grammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
| 4843 | if (old != NULL) { |
| 4844 | ctxt->grammar = old; |
| 4845 | ctxt->parentgrammar = oldparent; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4846 | #if 0 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4847 | if (grammar != NULL) { |
| 4848 | grammar->next = old->next; |
| 4849 | old->next = grammar; |
| 4850 | } |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4851 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4852 | } |
| 4853 | if (grammar != NULL) |
| 4854 | def = grammar->start; |
| 4855 | else |
| 4856 | def = NULL; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4857 | } else if (IS_RELAXNG(node, "parentRef")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4858 | if (ctxt->parentgrammar == NULL) { |
| 4859 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_PARENT, |
| 4860 | "Use of parentRef without a parent grammar\n", NULL, |
| 4861 | NULL); |
| 4862 | return (NULL); |
| 4863 | } |
| 4864 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4865 | if (def == NULL) |
| 4866 | return (NULL); |
| 4867 | def->type = XML_RELAXNG_PARENTREF; |
| 4868 | def->name = xmlGetProp(node, BAD_CAST "name"); |
| 4869 | if (def->name == NULL) { |
| 4870 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_NAME, |
| 4871 | "parentRef has no name\n", NULL, NULL); |
| 4872 | } else { |
| 4873 | xmlRelaxNGNormExtSpace(def->name); |
| 4874 | if (xmlValidateNCName(def->name, 0)) { |
| 4875 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NAME_INVALID, |
| 4876 | "parentRef name '%s' is not an NCName\n", |
| 4877 | def->name, NULL); |
| 4878 | } |
| 4879 | } |
| 4880 | if (node->children != NULL) { |
| 4881 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NOT_EMPTY, |
| 4882 | "parentRef is not empty\n", NULL, NULL); |
| 4883 | } |
| 4884 | if (ctxt->parentgrammar->refs == NULL) |
| 4885 | ctxt->parentgrammar->refs = xmlHashCreate(10); |
| 4886 | if (ctxt->parentgrammar->refs == NULL) { |
| 4887 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED, |
| 4888 | "Could not create references hash\n", NULL, NULL); |
| 4889 | def = NULL; |
| 4890 | } else if (def->name != NULL) { |
| 4891 | int tmp; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4892 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4893 | tmp = |
| 4894 | xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def); |
| 4895 | if (tmp < 0) { |
| 4896 | xmlRelaxNGDefinePtr prev; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4897 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4898 | prev = (xmlRelaxNGDefinePtr) |
| 4899 | xmlHashLookup(ctxt->parentgrammar->refs, def->name); |
| 4900 | if (prev == NULL) { |
| 4901 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED, |
| 4902 | "Internal error parentRef definitions '%s'\n", |
| 4903 | def->name, NULL); |
| 4904 | def = NULL; |
| 4905 | } else { |
| 4906 | def->nextHash = prev->nextHash; |
| 4907 | prev->nextHash = def; |
| 4908 | } |
| 4909 | } |
| 4910 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4911 | } else if (IS_RELAXNG(node, "mixed")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4912 | if (node->children == NULL) { |
| 4913 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, "Mixed is empty\n", |
| 4914 | NULL, NULL); |
| 4915 | def = NULL; |
| 4916 | } else { |
| 4917 | def = xmlRelaxNGParseInterleave(ctxt, node); |
| 4918 | if (def != NULL) { |
| 4919 | xmlRelaxNGDefinePtr tmp; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4920 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4921 | if ((def->content != NULL) && (def->content->next != NULL)) { |
| 4922 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
| 4923 | if (tmp != NULL) { |
| 4924 | tmp->type = XML_RELAXNG_GROUP; |
| 4925 | tmp->content = def->content; |
| 4926 | def->content = tmp; |
| 4927 | } |
| 4928 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4929 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4930 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
| 4931 | if (tmp == NULL) |
| 4932 | return (def); |
| 4933 | tmp->type = XML_RELAXNG_TEXT; |
| 4934 | tmp->next = def->content; |
| 4935 | def->content = tmp; |
| 4936 | } |
| 4937 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4938 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4939 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_CONSTRUCT, |
| 4940 | "Unexpected node %s is not a pattern\n", node->name, |
| 4941 | NULL); |
| 4942 | def = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4943 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4944 | return (def); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4945 | } |
| 4946 | |
| 4947 | /** |
| 4948 | * xmlRelaxNGParseAttribute: |
| 4949 | * @ctxt: a Relax-NG parser context |
| 4950 | * @node: the element node |
| 4951 | * |
| 4952 | * parse the content of a RelaxNG attribute node. |
| 4953 | * |
| 4954 | * Returns the definition pointer or NULL in case of error. |
| 4955 | */ |
| 4956 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4957 | xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4958 | { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4959 | xmlRelaxNGDefinePtr ret, cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4960 | xmlNodePtr child; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4961 | int old_flags; |
| 4962 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4963 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4964 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4965 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4966 | ret->type = XML_RELAXNG_ATTRIBUTE; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4967 | ret->parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4968 | child = node->children; |
| 4969 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4970 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_EMPTY, |
| 4971 | "xmlRelaxNGParseattribute: attribute has no children\n", |
| 4972 | NULL, NULL); |
| 4973 | return (ret); |
| 4974 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4975 | old_flags = ctxt->flags; |
| 4976 | ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4977 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 4978 | if (cur != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4979 | child = child->next; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4980 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4981 | if (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4982 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 4983 | if (cur != NULL) { |
| 4984 | switch (cur->type) { |
| 4985 | case XML_RELAXNG_EMPTY: |
| 4986 | case XML_RELAXNG_NOT_ALLOWED: |
| 4987 | case XML_RELAXNG_TEXT: |
| 4988 | case XML_RELAXNG_ELEMENT: |
| 4989 | case XML_RELAXNG_DATATYPE: |
| 4990 | case XML_RELAXNG_VALUE: |
| 4991 | case XML_RELAXNG_LIST: |
| 4992 | case XML_RELAXNG_REF: |
| 4993 | case XML_RELAXNG_PARENTREF: |
| 4994 | case XML_RELAXNG_EXTERNALREF: |
| 4995 | case XML_RELAXNG_DEF: |
| 4996 | case XML_RELAXNG_ONEORMORE: |
| 4997 | case XML_RELAXNG_ZEROORMORE: |
| 4998 | case XML_RELAXNG_OPTIONAL: |
| 4999 | case XML_RELAXNG_CHOICE: |
| 5000 | case XML_RELAXNG_GROUP: |
| 5001 | case XML_RELAXNG_INTERLEAVE: |
| 5002 | case XML_RELAXNG_ATTRIBUTE: |
| 5003 | ret->content = cur; |
| 5004 | cur->parent = ret; |
| 5005 | break; |
| 5006 | case XML_RELAXNG_START: |
| 5007 | case XML_RELAXNG_PARAM: |
| 5008 | case XML_RELAXNG_EXCEPT: |
| 5009 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CONTENT, |
| 5010 | "attribute has invalid content\n", NULL, |
| 5011 | NULL); |
| 5012 | break; |
| 5013 | case XML_RELAXNG_NOOP: |
| 5014 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_NOOP, |
| 5015 | "RNG Internal error, noop found in attribute\n", |
| 5016 | NULL, NULL); |
| 5017 | break; |
| 5018 | } |
| 5019 | } |
| 5020 | child = child->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5021 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5022 | if (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5023 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CHILDREN, |
| 5024 | "attribute has multiple children\n", NULL, NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5025 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5026 | ctxt->flags = old_flags; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5027 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5028 | } |
| 5029 | |
| 5030 | /** |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5031 | * xmlRelaxNGParseExceptNameClass: |
| 5032 | * @ctxt: a Relax-NG parser context |
| 5033 | * @node: the except node |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5034 | * @attr: 1 if within an attribute, 0 if within an element |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5035 | * |
| 5036 | * parse the content of a RelaxNG nameClass node. |
| 5037 | * |
| 5038 | * Returns the definition pointer or NULL in case of error. |
| 5039 | */ |
| 5040 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5041 | xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5042 | xmlNodePtr node, int attr) |
| 5043 | { |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5044 | xmlRelaxNGDefinePtr ret, cur, last = NULL; |
| 5045 | xmlNodePtr child; |
| 5046 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5047 | if (!IS_RELAXNG(node, "except")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5048 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MISSING, |
| 5049 | "Expecting an except node\n", NULL, NULL); |
| 5050 | return (NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5051 | } |
| 5052 | if (node->next != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5053 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MULTIPLE, |
| 5054 | "exceptNameClass allows only a single except node\n", |
| 5055 | NULL, NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5056 | } |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5057 | if (node->children == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5058 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_EMPTY, "except has no content\n", |
| 5059 | NULL, NULL); |
| 5060 | return (NULL); |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5061 | } |
| 5062 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5063 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5064 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5065 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5066 | ret->type = XML_RELAXNG_EXCEPT; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5067 | child = node->children; |
| 5068 | while (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5069 | cur = xmlRelaxNGNewDefine(ctxt, child); |
| 5070 | if (cur == NULL) |
| 5071 | break; |
| 5072 | if (attr) |
| 5073 | cur->type = XML_RELAXNG_ATTRIBUTE; |
| 5074 | else |
| 5075 | cur->type = XML_RELAXNG_ELEMENT; |
| 5076 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 5077 | if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5078 | if (last == NULL) { |
| 5079 | ret->content = cur; |
| 5080 | } else { |
| 5081 | last->next = cur; |
| 5082 | } |
| 5083 | last = cur; |
| 5084 | } |
| 5085 | child = child->next; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5086 | } |
| 5087 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5088 | return (ret); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5089 | } |
| 5090 | |
| 5091 | /** |
| 5092 | * xmlRelaxNGParseNameClass: |
| 5093 | * @ctxt: a Relax-NG parser context |
| 5094 | * @node: the nameClass node |
| 5095 | * @def: the current definition |
| 5096 | * |
| 5097 | * parse the content of a RelaxNG nameClass node. |
| 5098 | * |
| 5099 | * Returns the definition pointer or NULL in case of error. |
| 5100 | */ |
| 5101 | static xmlRelaxNGDefinePtr |
| 5102 | xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5103 | xmlRelaxNGDefinePtr def) |
| 5104 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5105 | xmlRelaxNGDefinePtr ret, tmp; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5106 | xmlChar *val; |
| 5107 | |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5108 | ret = def; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5109 | if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) || |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5110 | (IS_RELAXNG(node, "nsName"))) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5111 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 5112 | (def->type != XML_RELAXNG_ATTRIBUTE)) { |
| 5113 | ret = xmlRelaxNGNewDefine(ctxt, node); |
| 5114 | if (ret == NULL) |
| 5115 | return (NULL); |
| 5116 | ret->parent = def; |
| 5117 | if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) |
| 5118 | ret->type = XML_RELAXNG_ATTRIBUTE; |
| 5119 | else |
| 5120 | ret->type = XML_RELAXNG_ELEMENT; |
| 5121 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5122 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5123 | if (IS_RELAXNG(node, "name")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5124 | val = xmlNodeGetContent(node); |
| 5125 | xmlRelaxNGNormExtSpace(val); |
| 5126 | if (xmlValidateNCName(val, 0)) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5127 | if (node->parent != NULL) |
| 5128 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME, |
| 5129 | "Element %s name '%s' is not an NCName\n", |
| 5130 | node->parent->name, val); |
| 5131 | else |
| 5132 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME, |
| 5133 | "name '%s' is not an NCName\n", |
| 5134 | val, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5135 | } |
| 5136 | ret->name = val; |
| 5137 | val = xmlGetProp(node, BAD_CAST "ns"); |
| 5138 | ret->ns = val; |
| 5139 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5140 | (val != NULL) && |
| 5141 | (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5142 | xmlRngPErr(ctxt, node, XML_RNGP_XML_NS, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5143 | "Attribute with namespace '%s' is not allowed\n", |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5144 | val, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5145 | } |
| 5146 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5147 | (val != NULL) && |
| 5148 | (val[0] == 0) && (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5149 | xmlRngPErr(ctxt, node, XML_RNGP_XMLNS_NAME, |
| 5150 | "Attribute with QName 'xmlns' is not allowed\n", |
| 5151 | val, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5152 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5153 | } else if (IS_RELAXNG(node, "anyName")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5154 | ret->name = NULL; |
| 5155 | ret->ns = NULL; |
| 5156 | if (node->children != NULL) { |
| 5157 | ret->nameClass = |
| 5158 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
| 5159 | (def->type == |
| 5160 | XML_RELAXNG_ATTRIBUTE)); |
| 5161 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5162 | } else if (IS_RELAXNG(node, "nsName")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5163 | ret->name = NULL; |
| 5164 | ret->ns = xmlGetProp(node, BAD_CAST "ns"); |
| 5165 | if (ret->ns == NULL) { |
| 5166 | xmlRngPErr(ctxt, node, XML_RNGP_NSNAME_NO_NS, |
| 5167 | "nsName has no ns attribute\n", NULL, NULL); |
| 5168 | } |
| 5169 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5170 | (ret->ns != NULL) && |
| 5171 | (xmlStrEqual |
| 5172 | (ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
| 5173 | xmlRngPErr(ctxt, node, XML_RNGP_XML_NS, |
| 5174 | "Attribute with namespace '%s' is not allowed\n", |
| 5175 | ret->ns, NULL); |
| 5176 | } |
| 5177 | if (node->children != NULL) { |
| 5178 | ret->nameClass = |
| 5179 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
| 5180 | (def->type == |
| 5181 | XML_RELAXNG_ATTRIBUTE)); |
| 5182 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5183 | } else if (IS_RELAXNG(node, "choice")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5184 | xmlNodePtr child; |
| 5185 | xmlRelaxNGDefinePtr last = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5186 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5187 | ret = xmlRelaxNGNewDefine(ctxt, node); |
| 5188 | if (ret == NULL) |
| 5189 | return (NULL); |
| 5190 | ret->parent = def; |
| 5191 | ret->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5192 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5193 | if (node->children == NULL) { |
| 5194 | xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_EMPTY, |
| 5195 | "Element choice is empty\n", NULL, NULL); |
| 5196 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5197 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5198 | child = node->children; |
| 5199 | while (child != NULL) { |
| 5200 | tmp = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5201 | if (tmp != NULL) { |
| 5202 | if (last == NULL) { |
| 5203 | last = ret->nameClass = tmp; |
| 5204 | } else { |
| 5205 | last->next = tmp; |
| 5206 | last = tmp; |
| 5207 | } |
| 5208 | } |
| 5209 | child = child->next; |
| 5210 | } |
| 5211 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5212 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5213 | xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_CONTENT, |
| 5214 | "expecting name, anyName, nsName or choice : got %s\n", |
| 5215 | node->name, NULL); |
| 5216 | return (NULL); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5217 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5218 | if (ret != def) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5219 | if (def->nameClass == NULL) { |
| 5220 | def->nameClass = ret; |
| 5221 | } else { |
| 5222 | tmp = def->nameClass; |
| 5223 | while (tmp->next != NULL) { |
| 5224 | tmp = tmp->next; |
| 5225 | } |
| 5226 | tmp->next = ret; |
| 5227 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5228 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5229 | return (ret); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5230 | } |
| 5231 | |
| 5232 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5233 | * xmlRelaxNGParseElement: |
| 5234 | * @ctxt: a Relax-NG parser context |
| 5235 | * @node: the element node |
| 5236 | * |
| 5237 | * parse the content of a RelaxNG element node. |
| 5238 | * |
| 5239 | * Returns the definition pointer or NULL in case of error. |
| 5240 | */ |
| 5241 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5242 | xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 5243 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5244 | xmlRelaxNGDefinePtr ret, cur, last; |
| 5245 | xmlNodePtr child; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5246 | const xmlChar *olddefine; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5247 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5248 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5249 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5250 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5251 | ret->type = XML_RELAXNG_ELEMENT; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5252 | ret->parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5253 | child = node->children; |
| 5254 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5255 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_EMPTY, |
| 5256 | "xmlRelaxNGParseElement: element has no children\n", |
| 5257 | NULL, NULL); |
| 5258 | return (ret); |
| 5259 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5260 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5261 | if (cur != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5262 | child = child->next; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5263 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5264 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5265 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NO_CONTENT, |
| 5266 | "xmlRelaxNGParseElement: element has no content\n", |
| 5267 | NULL, NULL); |
| 5268 | return (ret); |
| 5269 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5270 | olddefine = ctxt->define; |
| 5271 | ctxt->define = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5272 | last = NULL; |
| 5273 | while (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5274 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 5275 | if (cur != NULL) { |
| 5276 | cur->parent = ret; |
| 5277 | switch (cur->type) { |
| 5278 | case XML_RELAXNG_EMPTY: |
| 5279 | case XML_RELAXNG_NOT_ALLOWED: |
| 5280 | case XML_RELAXNG_TEXT: |
| 5281 | case XML_RELAXNG_ELEMENT: |
| 5282 | case XML_RELAXNG_DATATYPE: |
| 5283 | case XML_RELAXNG_VALUE: |
| 5284 | case XML_RELAXNG_LIST: |
| 5285 | case XML_RELAXNG_REF: |
| 5286 | case XML_RELAXNG_PARENTREF: |
| 5287 | case XML_RELAXNG_EXTERNALREF: |
| 5288 | case XML_RELAXNG_DEF: |
| 5289 | case XML_RELAXNG_ZEROORMORE: |
| 5290 | case XML_RELAXNG_ONEORMORE: |
| 5291 | case XML_RELAXNG_OPTIONAL: |
| 5292 | case XML_RELAXNG_CHOICE: |
| 5293 | case XML_RELAXNG_GROUP: |
| 5294 | case XML_RELAXNG_INTERLEAVE: |
| 5295 | if (last == NULL) { |
| 5296 | ret->content = last = cur; |
| 5297 | } else { |
| 5298 | if ((last->type == XML_RELAXNG_ELEMENT) && |
| 5299 | (ret->content == last)) { |
| 5300 | ret->content = xmlRelaxNGNewDefine(ctxt, node); |
| 5301 | if (ret->content != NULL) { |
| 5302 | ret->content->type = XML_RELAXNG_GROUP; |
| 5303 | ret->content->content = last; |
| 5304 | } else { |
| 5305 | ret->content = last; |
| 5306 | } |
| 5307 | } |
| 5308 | last->next = cur; |
| 5309 | last = cur; |
| 5310 | } |
| 5311 | break; |
| 5312 | case XML_RELAXNG_ATTRIBUTE: |
| 5313 | cur->next = ret->attrs; |
| 5314 | ret->attrs = cur; |
| 5315 | break; |
| 5316 | case XML_RELAXNG_START: |
| 5317 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5318 | "RNG Internal error, start found in element\n", |
| 5319 | NULL, NULL); |
| 5320 | break; |
| 5321 | case XML_RELAXNG_PARAM: |
| 5322 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5323 | "RNG Internal error, param found in element\n", |
| 5324 | NULL, NULL); |
| 5325 | break; |
| 5326 | case XML_RELAXNG_EXCEPT: |
| 5327 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5328 | "RNG Internal error, except found in element\n", |
| 5329 | NULL, NULL); |
| 5330 | break; |
| 5331 | case XML_RELAXNG_NOOP: |
| 5332 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5333 | "RNG Internal error, noop found in element\n", |
| 5334 | NULL, NULL); |
| 5335 | break; |
| 5336 | } |
| 5337 | } |
| 5338 | child = child->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5339 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5340 | ctxt->define = olddefine; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5341 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5342 | } |
| 5343 | |
| 5344 | /** |
| 5345 | * xmlRelaxNGParsePatterns: |
| 5346 | * @ctxt: a Relax-NG parser context |
| 5347 | * @nodes: list of nodes |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5348 | * @group: use an implicit <group> for elements |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5349 | * |
| 5350 | * parse the content of a RelaxNG start node. |
| 5351 | * |
| 5352 | * Returns the definition pointer or NULL in case of error. |
| 5353 | */ |
| 5354 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5355 | xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5356 | int group) |
| 5357 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5358 | xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5359 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5360 | parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5361 | while (nodes != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5362 | if (IS_RELAXNG(nodes, "element")) { |
| 5363 | cur = xmlRelaxNGParseElement(ctxt, nodes); |
| 5364 | if (def == NULL) { |
| 5365 | def = last = cur; |
| 5366 | } else { |
| 5367 | if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) && |
| 5368 | (def == last)) { |
| 5369 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
| 5370 | def->type = XML_RELAXNG_GROUP; |
| 5371 | def->content = last; |
| 5372 | } |
| 5373 | last->next = cur; |
| 5374 | last = cur; |
| 5375 | } |
| 5376 | cur->parent = parent; |
| 5377 | } else { |
| 5378 | cur = xmlRelaxNGParsePattern(ctxt, nodes); |
| 5379 | if (cur != NULL) { |
| 5380 | if (def == NULL) { |
| 5381 | def = last = cur; |
| 5382 | } else { |
| 5383 | last->next = cur; |
| 5384 | last = cur; |
| 5385 | } |
| 5386 | } |
| 5387 | } |
| 5388 | nodes = nodes->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5389 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5390 | return (def); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5391 | } |
| 5392 | |
| 5393 | /** |
| 5394 | * xmlRelaxNGParseStart: |
| 5395 | * @ctxt: a Relax-NG parser context |
| 5396 | * @nodes: start children nodes |
| 5397 | * |
| 5398 | * parse the content of a RelaxNG start node. |
| 5399 | * |
| 5400 | * Returns 0 in case of success, -1 in case of error |
| 5401 | */ |
| 5402 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5403 | xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) |
| 5404 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5405 | int ret = 0; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5406 | xmlRelaxNGDefinePtr def = NULL, last; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5407 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5408 | if (nodes == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5409 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, "start has no children\n", |
| 5410 | NULL, NULL); |
| 5411 | return (-1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5412 | } |
| 5413 | if (IS_RELAXNG(nodes, "empty")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5414 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
| 5415 | if (def == NULL) |
| 5416 | return (-1); |
| 5417 | def->type = XML_RELAXNG_EMPTY; |
| 5418 | if (nodes->children != NULL) { |
| 5419 | xmlRngPErr(ctxt, nodes, XML_RNGP_EMPTY_CONTENT, |
| 5420 | "element empty is not empty\n", NULL, NULL); |
| 5421 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5422 | } else if (IS_RELAXNG(nodes, "notAllowed")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5423 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
| 5424 | if (def == NULL) |
| 5425 | return (-1); |
| 5426 | def->type = XML_RELAXNG_NOT_ALLOWED; |
| 5427 | if (nodes->children != NULL) { |
| 5428 | xmlRngPErr(ctxt, nodes, XML_RNGP_NOTALLOWED_NOT_EMPTY, |
| 5429 | "element notAllowed is not empty\n", NULL, NULL); |
| 5430 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5431 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5432 | def = xmlRelaxNGParsePatterns(ctxt, nodes, 1); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5433 | } |
| 5434 | if (ctxt->grammar->start != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5435 | last = ctxt->grammar->start; |
| 5436 | while (last->next != NULL) |
| 5437 | last = last->next; |
| 5438 | last->next = def; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5439 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5440 | ctxt->grammar->start = def; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5441 | } |
| 5442 | nodes = nodes->next; |
| 5443 | if (nodes != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5444 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_CONTENT, |
| 5445 | "start more than one children\n", NULL, NULL); |
| 5446 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5447 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5448 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5449 | } |
| 5450 | |
| 5451 | /** |
| 5452 | * xmlRelaxNGParseGrammarContent: |
| 5453 | * @ctxt: a Relax-NG parser context |
| 5454 | * @nodes: grammar children nodes |
| 5455 | * |
| 5456 | * parse the content of a RelaxNG grammar node. |
| 5457 | * |
| 5458 | * Returns 0 in case of success, -1 in case of error |
| 5459 | */ |
| 5460 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5461 | xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt, |
| 5462 | xmlNodePtr nodes) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5463 | { |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5464 | int ret = 0, tmp; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5465 | |
| 5466 | if (nodes == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5467 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_EMPTY, |
| 5468 | "grammar has no children\n", NULL, NULL); |
| 5469 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5470 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5471 | while (nodes != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5472 | if (IS_RELAXNG(nodes, "start")) { |
| 5473 | if (nodes->children == NULL) { |
| 5474 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, |
| 5475 | "start has no children\n", NULL, NULL); |
| 5476 | } else { |
| 5477 | tmp = xmlRelaxNGParseStart(ctxt, nodes->children); |
| 5478 | if (tmp != 0) |
| 5479 | ret = -1; |
| 5480 | } |
| 5481 | } else if (IS_RELAXNG(nodes, "define")) { |
| 5482 | tmp = xmlRelaxNGParseDefine(ctxt, nodes); |
| 5483 | if (tmp != 0) |
| 5484 | ret = -1; |
| 5485 | } else if (IS_RELAXNG(nodes, "include")) { |
| 5486 | tmp = xmlRelaxNGParseInclude(ctxt, nodes); |
| 5487 | if (tmp != 0) |
| 5488 | ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5489 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5490 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT, |
| 5491 | "grammar has unexpected child %s\n", nodes->name, |
| 5492 | NULL); |
| 5493 | ret = -1; |
| 5494 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5495 | nodes = nodes->next; |
| 5496 | } |
| 5497 | return (ret); |
| 5498 | } |
| 5499 | |
| 5500 | /** |
| 5501 | * xmlRelaxNGCheckReference: |
| 5502 | * @ref: the ref |
| 5503 | * @ctxt: a Relax-NG parser context |
| 5504 | * @name: the name associated to the defines |
| 5505 | * |
| 5506 | * Applies the 4.17. combine attribute rule for all the define |
| 5507 | * element of a given grammar using the same name. |
| 5508 | */ |
| 5509 | static void |
| 5510 | xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5511 | xmlRelaxNGParserCtxtPtr ctxt, |
| 5512 | const xmlChar * name) |
| 5513 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5514 | xmlRelaxNGGrammarPtr grammar; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5515 | xmlRelaxNGDefinePtr def, cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5516 | |
| 5517 | grammar = ctxt->grammar; |
| 5518 | if (grammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5519 | xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR, |
| 5520 | "Internal error: no grammar in CheckReference %s\n", |
| 5521 | name, NULL); |
| 5522 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5523 | } |
| 5524 | if (ref->content != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5525 | xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR, |
| 5526 | "Internal error: reference has content in CheckReference %s\n", |
| 5527 | name, NULL); |
| 5528 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5529 | } |
| 5530 | if (grammar->defs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5531 | def = xmlHashLookup(grammar->defs, name); |
| 5532 | if (def != NULL) { |
| 5533 | cur = ref; |
| 5534 | while (cur != NULL) { |
| 5535 | cur->content = def; |
| 5536 | cur = cur->nextHash; |
| 5537 | } |
| 5538 | } else { |
| 5539 | xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF, |
| 5540 | "Reference %s has no matching definition\n", name, |
| 5541 | NULL); |
| 5542 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5543 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5544 | xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF, |
| 5545 | "Reference %s has no matching definition\n", name, |
| 5546 | NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5547 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5548 | } |
| 5549 | |
| 5550 | /** |
| 5551 | * xmlRelaxNGCheckCombine: |
| 5552 | * @define: the define(s) list |
| 5553 | * @ctxt: a Relax-NG parser context |
| 5554 | * @name: the name associated to the defines |
| 5555 | * |
| 5556 | * Applies the 4.17. combine attribute rule for all the define |
| 5557 | * element of a given grammar using the same name. |
| 5558 | */ |
| 5559 | static void |
| 5560 | xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5561 | xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * name) |
| 5562 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5563 | xmlChar *combine; |
| 5564 | int choiceOrInterleave = -1; |
| 5565 | int missing = 0; |
| 5566 | xmlRelaxNGDefinePtr cur, last, tmp, tmp2; |
| 5567 | |
| 5568 | if (define->nextHash == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5569 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5570 | cur = define; |
| 5571 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5572 | combine = xmlGetProp(cur->node, BAD_CAST "combine"); |
| 5573 | if (combine != NULL) { |
| 5574 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
| 5575 | if (choiceOrInterleave == -1) |
| 5576 | choiceOrInterleave = 1; |
| 5577 | else if (choiceOrInterleave == 0) { |
| 5578 | xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE, |
| 5579 | "Defines for %s use both 'choice' and 'interleave'\n", |
| 5580 | name, NULL); |
| 5581 | } |
| 5582 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
| 5583 | if (choiceOrInterleave == -1) |
| 5584 | choiceOrInterleave = 0; |
| 5585 | else if (choiceOrInterleave == 1) { |
| 5586 | xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE, |
| 5587 | "Defines for %s use both 'choice' and 'interleave'\n", |
| 5588 | name, NULL); |
| 5589 | } |
| 5590 | } else { |
| 5591 | xmlRngPErr(ctxt, define->node, XML_RNGP_UNKNOWN_COMBINE, |
| 5592 | "Defines for %s use unknown combine value '%s''\n", |
| 5593 | name, combine); |
| 5594 | } |
| 5595 | xmlFree(combine); |
| 5596 | } else { |
| 5597 | if (missing == 0) |
| 5598 | missing = 1; |
| 5599 | else { |
| 5600 | xmlRngPErr(ctxt, define->node, XML_RNGP_NEED_COMBINE, |
| 5601 | "Some defines for %s needs the combine attribute\n", |
| 5602 | name, NULL); |
| 5603 | } |
| 5604 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5605 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5606 | cur = cur->nextHash; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5607 | } |
| 5608 | #ifdef DEBUG |
| 5609 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5610 | "xmlRelaxNGCheckCombine(): merging %s defines: %d\n", |
| 5611 | name, choiceOrInterleave); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5612 | #endif |
| 5613 | if (choiceOrInterleave == -1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5614 | choiceOrInterleave = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5615 | cur = xmlRelaxNGNewDefine(ctxt, define->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5616 | if (cur == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5617 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5618 | if (choiceOrInterleave == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5619 | cur->type = XML_RELAXNG_INTERLEAVE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5620 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5621 | cur->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5622 | tmp = define; |
| 5623 | last = NULL; |
| 5624 | while (tmp != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5625 | if (tmp->content != NULL) { |
| 5626 | if (tmp->content->next != NULL) { |
| 5627 | /* |
| 5628 | * we need first to create a wrapper. |
| 5629 | */ |
| 5630 | tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node); |
| 5631 | if (tmp2 == NULL) |
| 5632 | break; |
| 5633 | tmp2->type = XML_RELAXNG_GROUP; |
| 5634 | tmp2->content = tmp->content; |
| 5635 | } else { |
| 5636 | tmp2 = tmp->content; |
| 5637 | } |
| 5638 | if (last == NULL) { |
| 5639 | cur->content = tmp2; |
| 5640 | } else { |
| 5641 | last->next = tmp2; |
| 5642 | } |
| 5643 | last = tmp2; |
| 5644 | } |
| 5645 | tmp->content = cur; |
| 5646 | tmp = tmp->nextHash; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5647 | } |
| 5648 | define->content = cur; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5649 | if (choiceOrInterleave == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5650 | if (ctxt->interleaves == NULL) |
| 5651 | ctxt->interleaves = xmlHashCreate(10); |
| 5652 | if (ctxt->interleaves == NULL) { |
| 5653 | xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5654 | "Failed to create interleaves hash table\n", NULL, |
| 5655 | NULL); |
| 5656 | } else { |
| 5657 | char tmpname[32]; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5658 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5659 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
| 5660 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < |
| 5661 | 0) { |
| 5662 | xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5663 | "Failed to add %s to hash table\n", |
| 5664 | (const xmlChar *) tmpname, NULL); |
| 5665 | } |
| 5666 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5667 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5668 | } |
| 5669 | |
| 5670 | /** |
| 5671 | * xmlRelaxNGCombineStart: |
| 5672 | * @ctxt: a Relax-NG parser context |
| 5673 | * @grammar: the grammar |
| 5674 | * |
| 5675 | * Applies the 4.17. combine rule for all the start |
| 5676 | * element of a given grammar. |
| 5677 | */ |
| 5678 | static void |
| 5679 | xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5680 | xmlRelaxNGGrammarPtr grammar) |
| 5681 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5682 | xmlRelaxNGDefinePtr starts; |
| 5683 | xmlChar *combine; |
| 5684 | int choiceOrInterleave = -1; |
| 5685 | int missing = 0; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5686 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5687 | |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5688 | starts = grammar->start; |
| 5689 | if ((starts == NULL) || (starts->next == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5690 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5691 | cur = starts; |
| 5692 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5693 | if ((cur->node == NULL) || (cur->node->parent == NULL) || |
| 5694 | (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) { |
| 5695 | combine = NULL; |
| 5696 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_MISSING, |
| 5697 | "Internal error: start element not found\n", NULL, |
| 5698 | NULL); |
| 5699 | } else { |
| 5700 | combine = xmlGetProp(cur->node->parent, BAD_CAST "combine"); |
| 5701 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5702 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5703 | if (combine != NULL) { |
| 5704 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
| 5705 | if (choiceOrInterleave == -1) |
| 5706 | choiceOrInterleave = 1; |
| 5707 | else if (choiceOrInterleave == 0) { |
| 5708 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE, |
| 5709 | "<start> use both 'choice' and 'interleave'\n", |
| 5710 | NULL, NULL); |
| 5711 | } |
| 5712 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
| 5713 | if (choiceOrInterleave == -1) |
| 5714 | choiceOrInterleave = 0; |
| 5715 | else if (choiceOrInterleave == 1) { |
| 5716 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE, |
| 5717 | "<start> use both 'choice' and 'interleave'\n", |
| 5718 | NULL, NULL); |
| 5719 | } |
| 5720 | } else { |
| 5721 | xmlRngPErr(ctxt, cur->node, XML_RNGP_UNKNOWN_COMBINE, |
| 5722 | "<start> uses unknown combine value '%s''\n", |
| 5723 | combine, NULL); |
| 5724 | } |
| 5725 | xmlFree(combine); |
| 5726 | } else { |
| 5727 | if (missing == 0) |
| 5728 | missing = 1; |
| 5729 | else { |
| 5730 | xmlRngPErr(ctxt, cur->node, XML_RNGP_NEED_COMBINE, |
| 5731 | "Some <start> element miss the combine attribute\n", |
| 5732 | NULL, NULL); |
| 5733 | } |
| 5734 | } |
| 5735 | |
| 5736 | cur = cur->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5737 | } |
| 5738 | #ifdef DEBUG |
| 5739 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5740 | "xmlRelaxNGCombineStart(): merging <start>: %d\n", |
| 5741 | choiceOrInterleave); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5742 | #endif |
| 5743 | if (choiceOrInterleave == -1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5744 | choiceOrInterleave = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5745 | cur = xmlRelaxNGNewDefine(ctxt, starts->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5746 | if (cur == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5747 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5748 | if (choiceOrInterleave == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5749 | cur->type = XML_RELAXNG_INTERLEAVE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5750 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5751 | cur->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5752 | cur->content = grammar->start; |
| 5753 | grammar->start = cur; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5754 | if (choiceOrInterleave == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5755 | if (ctxt->interleaves == NULL) |
| 5756 | ctxt->interleaves = xmlHashCreate(10); |
| 5757 | if (ctxt->interleaves == NULL) { |
| 5758 | xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5759 | "Failed to create interleaves hash table\n", NULL, |
| 5760 | NULL); |
| 5761 | } else { |
| 5762 | char tmpname[32]; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5763 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5764 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
| 5765 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < |
| 5766 | 0) { |
| 5767 | xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5768 | "Failed to add %s to hash table\n", |
| 5769 | (const xmlChar *) tmpname, NULL); |
| 5770 | } |
| 5771 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5772 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5773 | } |
| 5774 | |
| 5775 | /** |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5776 | * xmlRelaxNGCheckCycles: |
| 5777 | * @ctxt: a Relax-NG parser context |
| 5778 | * @nodes: grammar children nodes |
| 5779 | * @depth: the counter |
| 5780 | * |
| 5781 | * Check for cycles. |
| 5782 | * |
| 5783 | * Returns 0 if check passed, and -1 in case of error |
| 5784 | */ |
| 5785 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5786 | xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt, |
| 5787 | xmlRelaxNGDefinePtr cur, int depth) |
| 5788 | { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5789 | int ret = 0; |
| 5790 | |
| 5791 | while ((ret == 0) && (cur != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5792 | if ((cur->type == XML_RELAXNG_REF) || |
| 5793 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5794 | if (cur->depth == -1) { |
| 5795 | cur->depth = depth; |
| 5796 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
| 5797 | cur->depth = -2; |
| 5798 | } else if (depth == cur->depth) { |
| 5799 | xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_CYCLE, |
| 5800 | "Detected a cycle in %s references\n", |
| 5801 | cur->name, NULL); |
| 5802 | return (-1); |
| 5803 | } |
| 5804 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
| 5805 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1); |
| 5806 | } else { |
| 5807 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
| 5808 | } |
| 5809 | cur = cur->next; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5810 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5811 | return (ret); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5812 | } |
| 5813 | |
| 5814 | /** |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5815 | * xmlRelaxNGTryUnlink: |
| 5816 | * @ctxt: a Relax-NG parser context |
| 5817 | * @cur: the definition to unlink |
| 5818 | * @parent: the parent definition |
| 5819 | * @prev: the previous sibling definition |
| 5820 | * |
| 5821 | * Try to unlink a definition. If not possble make it a NOOP |
| 5822 | * |
| 5823 | * Returns the new prev definition |
| 5824 | */ |
| 5825 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5826 | xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 5827 | xmlRelaxNGDefinePtr cur, |
| 5828 | xmlRelaxNGDefinePtr parent, xmlRelaxNGDefinePtr prev) |
| 5829 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5830 | if (prev != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5831 | prev->next = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5832 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5833 | if (parent != NULL) { |
| 5834 | if (parent->content == cur) |
| 5835 | parent->content = cur->next; |
| 5836 | else if (parent->attrs == cur) |
| 5837 | parent->attrs = cur->next; |
| 5838 | else if (parent->nameClass == cur) |
| 5839 | parent->nameClass = cur->next; |
| 5840 | } else { |
| 5841 | cur->type = XML_RELAXNG_NOOP; |
| 5842 | prev = cur; |
| 5843 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5844 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5845 | return (prev); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5846 | } |
| 5847 | |
| 5848 | /** |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5849 | * xmlRelaxNGSimplify: |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5850 | * @ctxt: a Relax-NG parser context |
| 5851 | * @nodes: grammar children nodes |
| 5852 | * |
| 5853 | * Check for simplification of empty and notAllowed |
| 5854 | */ |
| 5855 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5856 | xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt, |
| 5857 | xmlRelaxNGDefinePtr cur, xmlRelaxNGDefinePtr parent) |
| 5858 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5859 | xmlRelaxNGDefinePtr prev = NULL; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5860 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5861 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5862 | if ((cur->type == XML_RELAXNG_REF) || |
| 5863 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5864 | if (cur->depth != -3) { |
| 5865 | cur->depth = -3; |
| 5866 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
| 5867 | } |
| 5868 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
| 5869 | cur->parent = parent; |
| 5870 | if ((parent != NULL) && |
| 5871 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
| 5872 | (parent->type == XML_RELAXNG_LIST) || |
| 5873 | (parent->type == XML_RELAXNG_GROUP) || |
| 5874 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 5875 | (parent->type == XML_RELAXNG_ONEORMORE) || |
| 5876 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 5877 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
| 5878 | break; |
| 5879 | } |
| 5880 | if ((parent != NULL) && (parent->type == XML_RELAXNG_CHOICE)) { |
| 5881 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5882 | } else |
| 5883 | prev = cur; |
| 5884 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 5885 | cur->parent = parent; |
| 5886 | if ((parent != NULL) && |
| 5887 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
| 5888 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 5889 | parent->type = XML_RELAXNG_EMPTY; |
| 5890 | break; |
| 5891 | } |
| 5892 | if ((parent != NULL) && |
| 5893 | ((parent->type == XML_RELAXNG_GROUP) || |
| 5894 | (parent->type == XML_RELAXNG_INTERLEAVE))) { |
| 5895 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5896 | } else |
| 5897 | prev = cur; |
| 5898 | } else { |
| 5899 | cur->parent = parent; |
| 5900 | if (cur->content != NULL) |
| 5901 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
| 5902 | if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL)) |
| 5903 | xmlRelaxNGSimplify(ctxt, cur->attrs, cur); |
| 5904 | if (cur->nameClass != NULL) |
| 5905 | xmlRelaxNGSimplify(ctxt, cur->nameClass, cur); |
| 5906 | /* |
| 5907 | * On Elements, try to move attribute only generating rules on |
| 5908 | * the attrs rules. |
| 5909 | */ |
| 5910 | if (cur->type == XML_RELAXNG_ELEMENT) { |
| 5911 | int attronly; |
| 5912 | xmlRelaxNGDefinePtr tmp, pre; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 5913 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5914 | while (cur->content != NULL) { |
| 5915 | attronly = |
| 5916 | xmlRelaxNGGenerateAttributes(ctxt, cur->content); |
| 5917 | if (attronly == 1) { |
| 5918 | /* |
| 5919 | * migrate cur->content to attrs |
| 5920 | */ |
| 5921 | tmp = cur->content; |
| 5922 | cur->content = tmp->next; |
| 5923 | tmp->next = cur->attrs; |
| 5924 | cur->attrs = tmp; |
| 5925 | } else { |
| 5926 | /* |
| 5927 | * cur->content can generate elements or text |
| 5928 | */ |
| 5929 | break; |
| 5930 | } |
| 5931 | } |
| 5932 | pre = cur->content; |
| 5933 | while ((pre != NULL) && (pre->next != NULL)) { |
| 5934 | tmp = pre->next; |
| 5935 | attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp); |
| 5936 | if (attronly == 1) { |
| 5937 | /* |
| 5938 | * migrate tmp to attrs |
| 5939 | */ |
| 5940 | pre->next = tmp->next; |
| 5941 | tmp->next = cur->attrs; |
| 5942 | cur->attrs = tmp; |
| 5943 | } else { |
| 5944 | pre = tmp; |
| 5945 | } |
| 5946 | } |
| 5947 | } |
| 5948 | /* |
| 5949 | * This may result in a simplification |
| 5950 | */ |
| 5951 | if ((cur->type == XML_RELAXNG_GROUP) || |
| 5952 | (cur->type == XML_RELAXNG_INTERLEAVE)) { |
| 5953 | if (cur->content == NULL) |
| 5954 | cur->type = XML_RELAXNG_EMPTY; |
| 5955 | else if (cur->content->next == NULL) { |
| 5956 | if ((parent == NULL) && (prev == NULL)) { |
| 5957 | cur->type = XML_RELAXNG_NOOP; |
| 5958 | } else if (prev == NULL) { |
| 5959 | parent->content = cur->content; |
| 5960 | cur->content->next = cur->next; |
| 5961 | cur = cur->content; |
| 5962 | } else { |
| 5963 | cur->content->next = cur->next; |
| 5964 | prev->next = cur->content; |
| 5965 | cur = cur->content; |
| 5966 | } |
| 5967 | } |
| 5968 | } |
| 5969 | /* |
| 5970 | * the current node may have been transformed back |
| 5971 | */ |
| 5972 | if ((cur->type == XML_RELAXNG_EXCEPT) && |
| 5973 | (cur->content != NULL) && |
| 5974 | (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) { |
| 5975 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5976 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
| 5977 | if ((parent != NULL) && |
| 5978 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
| 5979 | (parent->type == XML_RELAXNG_LIST) || |
| 5980 | (parent->type == XML_RELAXNG_GROUP) || |
| 5981 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 5982 | (parent->type == XML_RELAXNG_ONEORMORE) || |
| 5983 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 5984 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
| 5985 | break; |
| 5986 | } |
| 5987 | if ((parent != NULL) && |
| 5988 | (parent->type == XML_RELAXNG_CHOICE)) { |
| 5989 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5990 | } else |
| 5991 | prev = cur; |
| 5992 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 5993 | if ((parent != NULL) && |
| 5994 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
| 5995 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 5996 | parent->type = XML_RELAXNG_EMPTY; |
| 5997 | break; |
| 5998 | } |
| 5999 | if ((parent != NULL) && |
| 6000 | ((parent->type == XML_RELAXNG_GROUP) || |
| 6001 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 6002 | (parent->type == XML_RELAXNG_CHOICE))) { |
| 6003 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 6004 | } else |
| 6005 | prev = cur; |
| 6006 | } else { |
| 6007 | prev = cur; |
| 6008 | } |
| 6009 | } |
| 6010 | cur = cur->next; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6011 | } |
| 6012 | } |
| 6013 | |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6014 | /** |
| 6015 | * xmlRelaxNGGroupContentType: |
| 6016 | * @ct1: the first content type |
| 6017 | * @ct2: the second content type |
| 6018 | * |
| 6019 | * Try to group 2 content types |
| 6020 | * |
| 6021 | * Returns the content type |
| 6022 | */ |
| 6023 | static xmlRelaxNGContentType |
| 6024 | xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6025 | xmlRelaxNGContentType ct2) |
| 6026 | { |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6027 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6028 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
| 6029 | return (XML_RELAXNG_CONTENT_ERROR); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6030 | if (ct1 == XML_RELAXNG_CONTENT_EMPTY) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6031 | return (ct2); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6032 | if (ct2 == XML_RELAXNG_CONTENT_EMPTY) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6033 | return (ct1); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6034 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6035 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
| 6036 | return (XML_RELAXNG_CONTENT_COMPLEX); |
| 6037 | return (XML_RELAXNG_CONTENT_ERROR); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6038 | } |
| 6039 | |
| 6040 | /** |
| 6041 | * xmlRelaxNGMaxContentType: |
| 6042 | * @ct1: the first content type |
| 6043 | * @ct2: the second content type |
| 6044 | * |
| 6045 | * Compute the max content-type |
| 6046 | * |
| 6047 | * Returns the content type |
| 6048 | */ |
| 6049 | static xmlRelaxNGContentType |
| 6050 | xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6051 | xmlRelaxNGContentType ct2) |
| 6052 | { |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6053 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6054 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
| 6055 | return (XML_RELAXNG_CONTENT_ERROR); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6056 | if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6057 | (ct2 == XML_RELAXNG_CONTENT_SIMPLE)) |
| 6058 | return (XML_RELAXNG_CONTENT_SIMPLE); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6059 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6060 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
| 6061 | return (XML_RELAXNG_CONTENT_COMPLEX); |
| 6062 | return (XML_RELAXNG_CONTENT_EMPTY); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6063 | } |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6064 | |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6065 | /** |
| 6066 | * xmlRelaxNGCheckRules: |
| 6067 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6068 | * @cur: the current definition |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6069 | * @flags: some accumulated flags |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6070 | * @ptype: the parent type |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6071 | * |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6072 | * Check for rules in section 7.1 and 7.2 |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6073 | * |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6074 | * Returns the content type of @cur |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6075 | */ |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6076 | static xmlRelaxNGContentType |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6077 | xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt, |
| 6078 | xmlRelaxNGDefinePtr cur, int flags, |
| 6079 | xmlRelaxNGType ptype) |
| 6080 | { |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6081 | int nflags = flags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6082 | xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6083 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6084 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6085 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6086 | if ((cur->type == XML_RELAXNG_REF) || |
| 6087 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 6088 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6089 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_REF, |
| 6090 | "Found forbidden pattern list//ref\n", NULL, |
| 6091 | NULL); |
| 6092 | } |
| 6093 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6094 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_REF, |
| 6095 | "Found forbidden pattern data/except//ref\n", |
| 6096 | NULL, NULL); |
| 6097 | } |
| 6098 | if (cur->depth > -4) { |
| 6099 | cur->depth = -4; |
| 6100 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, |
| 6101 | flags, cur->type); |
| 6102 | cur->depth = ret - 15; |
| 6103 | } else if (cur->depth == -4) { |
| 6104 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6105 | } else { |
| 6106 | ret = (xmlRelaxNGContentType) (cur->depth + 15); |
| 6107 | } |
| 6108 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
| 6109 | /* |
| 6110 | * The 7.3 Attribute derivation rule for groups is plugged there |
| 6111 | */ |
| 6112 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
| 6113 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6114 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ELEM, |
| 6115 | "Found forbidden pattern data/except//element(ref)\n", |
| 6116 | NULL, NULL); |
| 6117 | } |
| 6118 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6119 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ELEM, |
| 6120 | "Found forbidden pattern list//element(ref)\n", |
| 6121 | NULL, NULL); |
| 6122 | } |
| 6123 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6124 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM, |
| 6125 | "Found forbidden pattern attribute//element(ref)\n", |
| 6126 | NULL, NULL); |
| 6127 | } |
| 6128 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6129 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM, |
| 6130 | "Found forbidden pattern attribute//element(ref)\n", |
| 6131 | NULL, NULL); |
| 6132 | } |
| 6133 | /* |
| 6134 | * reset since in the simple form elements are only child |
| 6135 | * of grammar/define |
| 6136 | */ |
| 6137 | nflags = 0; |
| 6138 | ret = |
| 6139 | xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type); |
| 6140 | if (ret != XML_RELAXNG_CONTENT_EMPTY) { |
| 6141 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_EMPTY, |
| 6142 | "Element %s attributes have a content type error\n", |
| 6143 | cur->name, NULL); |
| 6144 | } |
| 6145 | ret = |
| 6146 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6147 | cur->type); |
| 6148 | if (ret == XML_RELAXNG_CONTENT_ERROR) { |
| 6149 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_ERROR, |
| 6150 | "Element %s has a content type error\n", |
| 6151 | cur->name, NULL); |
| 6152 | } else { |
| 6153 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6154 | } |
| 6155 | } else if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
| 6156 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6157 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ATTR, |
| 6158 | "Found forbidden pattern attribute//attribute\n", |
| 6159 | NULL, NULL); |
| 6160 | } |
| 6161 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6162 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ATTR, |
| 6163 | "Found forbidden pattern list//attribute\n", |
| 6164 | NULL, NULL); |
| 6165 | } |
| 6166 | if (flags & XML_RELAXNG_IN_OOMGROUP) { |
| 6167 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_GROUP_ATTR, |
| 6168 | "Found forbidden pattern oneOrMore//group//attribute\n", |
| 6169 | NULL, NULL); |
| 6170 | } |
| 6171 | if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) { |
| 6172 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_INTERLEAVE_ATTR, |
| 6173 | "Found forbidden pattern oneOrMore//interleave//attribute\n", |
| 6174 | NULL, NULL); |
| 6175 | } |
| 6176 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6177 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ATTR, |
| 6178 | "Found forbidden pattern data/except//attribute\n", |
| 6179 | NULL, NULL); |
| 6180 | } |
| 6181 | if (flags & XML_RELAXNG_IN_START) { |
| 6182 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ATTR, |
| 6183 | "Found forbidden pattern start//attribute\n", |
| 6184 | NULL, NULL); |
| 6185 | } |
| 6186 | if ((!(flags & XML_RELAXNG_IN_ONEORMORE)) |
| 6187 | && (cur->name == NULL)) { |
| 6188 | if (cur->ns == NULL) { |
| 6189 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ANYNAME_ATTR_ANCESTOR, |
| 6190 | "Found anyName attribute without oneOrMore ancestor\n", |
| 6191 | NULL, NULL); |
| 6192 | } else { |
| 6193 | xmlRngPErr(ctxt, cur->node, XML_RNGP_NSNAME_ATTR_ANCESTOR, |
| 6194 | "Found nsName attribute without oneOrMore ancestor\n", |
| 6195 | NULL, NULL); |
| 6196 | } |
| 6197 | } |
| 6198 | nflags = flags | XML_RELAXNG_IN_ATTRIBUTE; |
| 6199 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6200 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6201 | } else if ((cur->type == XML_RELAXNG_ONEORMORE) || |
| 6202 | (cur->type == XML_RELAXNG_ZEROORMORE)) { |
| 6203 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6204 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ONEMORE, |
| 6205 | "Found forbidden pattern data/except//oneOrMore\n", |
| 6206 | NULL, NULL); |
| 6207 | } |
| 6208 | if (flags & XML_RELAXNG_IN_START) { |
| 6209 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ONEMORE, |
| 6210 | "Found forbidden pattern start//oneOrMore\n", |
| 6211 | NULL, NULL); |
| 6212 | } |
| 6213 | nflags = flags | XML_RELAXNG_IN_ONEORMORE; |
| 6214 | ret = |
| 6215 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6216 | cur->type); |
| 6217 | ret = xmlRelaxNGGroupContentType(ret, ret); |
| 6218 | } else if (cur->type == XML_RELAXNG_LIST) { |
| 6219 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6220 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_LIST, |
| 6221 | "Found forbidden pattern list//list\n", NULL, |
| 6222 | NULL); |
| 6223 | } |
| 6224 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6225 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_LIST, |
| 6226 | "Found forbidden pattern data/except//list\n", |
| 6227 | NULL, NULL); |
| 6228 | } |
| 6229 | if (flags & XML_RELAXNG_IN_START) { |
| 6230 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_LIST, |
| 6231 | "Found forbidden pattern start//list\n", NULL, |
| 6232 | NULL); |
| 6233 | } |
| 6234 | nflags = flags | XML_RELAXNG_IN_LIST; |
| 6235 | ret = |
| 6236 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6237 | cur->type); |
| 6238 | } else if (cur->type == XML_RELAXNG_GROUP) { |
| 6239 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6240 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_GROUP, |
| 6241 | "Found forbidden pattern data/except//group\n", |
| 6242 | NULL, NULL); |
| 6243 | } |
| 6244 | if (flags & XML_RELAXNG_IN_START) { |
| 6245 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_GROUP, |
| 6246 | "Found forbidden pattern start//group\n", NULL, |
| 6247 | NULL); |
| 6248 | } |
| 6249 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
| 6250 | nflags = flags | XML_RELAXNG_IN_OOMGROUP; |
| 6251 | else |
| 6252 | nflags = flags; |
| 6253 | ret = |
| 6254 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6255 | cur->type); |
| 6256 | /* |
| 6257 | * The 7.3 Attribute derivation rule for groups is plugged there |
| 6258 | */ |
| 6259 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
| 6260 | } else if (cur->type == XML_RELAXNG_INTERLEAVE) { |
| 6261 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6262 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_INTERLEAVE, |
| 6263 | "Found forbidden pattern list//interleave\n", |
| 6264 | NULL, NULL); |
| 6265 | } |
| 6266 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6267 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE, |
| 6268 | "Found forbidden pattern data/except//interleave\n", |
| 6269 | NULL, NULL); |
| 6270 | } |
| 6271 | if (flags & XML_RELAXNG_IN_START) { |
| 6272 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE, |
| 6273 | "Found forbidden pattern start//interleave\n", |
| 6274 | NULL, NULL); |
| 6275 | } |
| 6276 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
| 6277 | nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE; |
| 6278 | else |
| 6279 | nflags = flags; |
| 6280 | ret = |
| 6281 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6282 | cur->type); |
| 6283 | } else if (cur->type == XML_RELAXNG_EXCEPT) { |
| 6284 | if ((cur->parent != NULL) && |
| 6285 | (cur->parent->type == XML_RELAXNG_DATATYPE)) |
| 6286 | nflags = flags | XML_RELAXNG_IN_DATAEXCEPT; |
| 6287 | else |
| 6288 | nflags = flags; |
| 6289 | ret = |
| 6290 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6291 | cur->type); |
| 6292 | } else if (cur->type == XML_RELAXNG_DATATYPE) { |
| 6293 | if (flags & XML_RELAXNG_IN_START) { |
| 6294 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_DATA, |
| 6295 | "Found forbidden pattern start//data\n", NULL, |
| 6296 | NULL); |
| 6297 | } |
| 6298 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6299 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
| 6300 | } else if (cur->type == XML_RELAXNG_VALUE) { |
| 6301 | if (flags & XML_RELAXNG_IN_START) { |
| 6302 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_VALUE, |
| 6303 | "Found forbidden pattern start//value\n", NULL, |
| 6304 | NULL); |
| 6305 | } |
| 6306 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6307 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
| 6308 | } else if (cur->type == XML_RELAXNG_TEXT) { |
| 6309 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6310 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_TEXT, |
| 6311 | "Found forbidden pattern list//text\n", NULL, |
| 6312 | NULL); |
| 6313 | } |
| 6314 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6315 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_TEXT, |
| 6316 | "Found forbidden pattern data/except//text\n", |
| 6317 | NULL, NULL); |
| 6318 | } |
| 6319 | if (flags & XML_RELAXNG_IN_START) { |
| 6320 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_TEXT, |
| 6321 | "Found forbidden pattern start//text\n", NULL, |
| 6322 | NULL); |
| 6323 | } |
| 6324 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6325 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 6326 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6327 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_EMPTY, |
| 6328 | "Found forbidden pattern data/except//empty\n", |
| 6329 | NULL, NULL); |
| 6330 | } |
| 6331 | if (flags & XML_RELAXNG_IN_START) { |
| 6332 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_EMPTY, |
| 6333 | "Found forbidden pattern start//empty\n", NULL, |
| 6334 | NULL); |
| 6335 | } |
| 6336 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6337 | } else if (cur->type == XML_RELAXNG_CHOICE) { |
| 6338 | xmlRelaxNGCheckChoiceDeterminism(ctxt, cur); |
| 6339 | ret = |
| 6340 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6341 | } else { |
| 6342 | ret = |
| 6343 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6344 | } |
| 6345 | cur = cur->next; |
| 6346 | if (ptype == XML_RELAXNG_GROUP) { |
| 6347 | val = xmlRelaxNGGroupContentType(val, ret); |
| 6348 | } else if (ptype == XML_RELAXNG_INTERLEAVE) { |
| 6349 | tmp = xmlRelaxNGGroupContentType(val, ret); |
| 6350 | if (tmp != XML_RELAXNG_CONTENT_ERROR) |
| 6351 | tmp = xmlRelaxNGMaxContentType(val, ret); |
| 6352 | } else if (ptype == XML_RELAXNG_CHOICE) { |
| 6353 | val = xmlRelaxNGMaxContentType(val, ret); |
| 6354 | } else if (ptype == XML_RELAXNG_LIST) { |
| 6355 | val = XML_RELAXNG_CONTENT_SIMPLE; |
| 6356 | } else if (ptype == XML_RELAXNG_EXCEPT) { |
| 6357 | if (ret == XML_RELAXNG_CONTENT_ERROR) |
| 6358 | val = XML_RELAXNG_CONTENT_ERROR; |
| 6359 | else |
| 6360 | val = XML_RELAXNG_CONTENT_SIMPLE; |
| 6361 | } else { |
| 6362 | val = xmlRelaxNGGroupContentType(val, ret); |
| 6363 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6364 | |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6365 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6366 | return (val); |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6367 | } |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6368 | |
| 6369 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6370 | * xmlRelaxNGParseGrammar: |
| 6371 | * @ctxt: a Relax-NG parser context |
| 6372 | * @nodes: grammar children nodes |
| 6373 | * |
| 6374 | * parse a Relax-NG <grammar> node |
| 6375 | * |
| 6376 | * Returns the internal xmlRelaxNGGrammarPtr built or |
| 6377 | * NULL in case of error |
| 6378 | */ |
| 6379 | static xmlRelaxNGGrammarPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6380 | xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) |
| 6381 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6382 | xmlRelaxNGGrammarPtr ret, tmp, old; |
| 6383 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6384 | #ifdef DEBUG_GRAMMAR |
| 6385 | xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n"); |
| 6386 | #endif |
| 6387 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6388 | ret = xmlRelaxNGNewGrammar(ctxt); |
| 6389 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6390 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6391 | |
| 6392 | /* |
| 6393 | * Link the new grammar in the tree |
| 6394 | */ |
| 6395 | ret->parent = ctxt->grammar; |
| 6396 | if (ctxt->grammar != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6397 | tmp = ctxt->grammar->children; |
| 6398 | if (tmp == NULL) { |
| 6399 | ctxt->grammar->children = ret; |
| 6400 | } else { |
| 6401 | while (tmp->next != NULL) |
| 6402 | tmp = tmp->next; |
| 6403 | tmp->next = ret; |
| 6404 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6405 | } |
| 6406 | |
| 6407 | old = ctxt->grammar; |
| 6408 | ctxt->grammar = ret; |
| 6409 | xmlRelaxNGParseGrammarContent(ctxt, nodes); |
| 6410 | ctxt->grammar = ret; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6411 | if (ctxt->grammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6412 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT, |
| 6413 | "Failed to parse <grammar> content\n", NULL, NULL); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6414 | } else if (ctxt->grammar->start == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6415 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_NO_START, |
| 6416 | "Element <grammar> has no <start>\n", NULL, NULL); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6417 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6418 | |
| 6419 | /* |
| 6420 | * Apply 4.17 mergingd rules to defines and starts |
| 6421 | */ |
| 6422 | xmlRelaxNGCombineStart(ctxt, ret); |
| 6423 | if (ret->defs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6424 | xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine, |
| 6425 | ctxt); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6426 | } |
| 6427 | |
| 6428 | /* |
| 6429 | * link together defines and refs in this grammar |
| 6430 | */ |
| 6431 | if (ret->refs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6432 | xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference, |
| 6433 | ctxt); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6434 | } |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 6435 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6436 | ctxt->grammar = old; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6437 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6438 | } |
| 6439 | |
| 6440 | /** |
| 6441 | * xmlRelaxNGParseDocument: |
| 6442 | * @ctxt: a Relax-NG parser context |
| 6443 | * @node: the root node of the RelaxNG schema |
| 6444 | * |
| 6445 | * parse a Relax-NG definition resource and build an internal |
| 6446 | * xmlRelaxNG struture which can be used to validate instances. |
| 6447 | * |
| 6448 | * Returns the internal XML RelaxNG structure built or |
| 6449 | * NULL in case of error |
| 6450 | */ |
| 6451 | static xmlRelaxNGPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6452 | xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 6453 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6454 | xmlRelaxNGPtr schema = NULL; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6455 | const xmlChar *olddefine; |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 6456 | xmlRelaxNGGrammarPtr old; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6457 | |
| 6458 | if ((ctxt == NULL) || (node == NULL)) |
| 6459 | return (NULL); |
| 6460 | |
| 6461 | schema = xmlRelaxNGNewRelaxNG(ctxt); |
| 6462 | if (schema == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6463 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6464 | |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6465 | olddefine = ctxt->define; |
| 6466 | ctxt->define = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6467 | if (IS_RELAXNG(node, "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6468 | schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6469 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6470 | xmlRelaxNGGrammarPtr tmp, ret; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6471 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6472 | schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt); |
| 6473 | if (schema->topgrammar == NULL) { |
| 6474 | return (schema); |
| 6475 | } |
| 6476 | /* |
| 6477 | * Link the new grammar in the tree |
| 6478 | */ |
| 6479 | ret->parent = ctxt->grammar; |
| 6480 | if (ctxt->grammar != NULL) { |
| 6481 | tmp = ctxt->grammar->children; |
| 6482 | if (tmp == NULL) { |
| 6483 | ctxt->grammar->children = ret; |
| 6484 | } else { |
| 6485 | while (tmp->next != NULL) |
| 6486 | tmp = tmp->next; |
| 6487 | tmp->next = ret; |
| 6488 | } |
| 6489 | } |
| 6490 | old = ctxt->grammar; |
| 6491 | ctxt->grammar = ret; |
| 6492 | xmlRelaxNGParseStart(ctxt, node); |
| 6493 | if (old != NULL) |
| 6494 | ctxt->grammar = old; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6495 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6496 | ctxt->define = olddefine; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 6497 | if (schema->topgrammar->start != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6498 | xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0); |
| 6499 | if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) { |
| 6500 | xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL); |
| 6501 | while ((schema->topgrammar->start != NULL) && |
| 6502 | (schema->topgrammar->start->type == XML_RELAXNG_NOOP) && |
| 6503 | (schema->topgrammar->start->next != NULL)) |
| 6504 | schema->topgrammar->start = |
| 6505 | schema->topgrammar->start->content; |
| 6506 | xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start, |
| 6507 | XML_RELAXNG_IN_START, XML_RELAXNG_NOOP); |
| 6508 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 6509 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6510 | #ifdef DEBUG |
| 6511 | if (schema == NULL) |
| 6512 | xmlGenericError(xmlGenericErrorContext, |
| 6513 | "xmlRelaxNGParseDocument() failed\n"); |
| 6514 | #endif |
| 6515 | |
| 6516 | return (schema); |
| 6517 | } |
| 6518 | |
| 6519 | /************************************************************************ |
| 6520 | * * |
| 6521 | * Reading RelaxNGs * |
| 6522 | * * |
| 6523 | ************************************************************************/ |
| 6524 | |
| 6525 | /** |
| 6526 | * xmlRelaxNGNewParserCtxt: |
| 6527 | * @URL: the location of the schema |
| 6528 | * |
| 6529 | * Create an XML RelaxNGs parse context for that file/resource expected |
| 6530 | * to contain an XML RelaxNGs file. |
| 6531 | * |
| 6532 | * Returns the parser context or NULL in case of error |
| 6533 | */ |
| 6534 | xmlRelaxNGParserCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6535 | xmlRelaxNGNewParserCtxt(const char *URL) |
| 6536 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6537 | xmlRelaxNGParserCtxtPtr ret; |
| 6538 | |
| 6539 | if (URL == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6540 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6541 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6542 | ret = |
| 6543 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6544 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6545 | xmlRngPErrMemory(NULL, "building parser\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6546 | return (NULL); |
| 6547 | } |
| 6548 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6549 | ret->URL = xmlStrdup((const xmlChar *) URL); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 6550 | ret->error = xmlGenericError; |
| 6551 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6552 | return (ret); |
| 6553 | } |
| 6554 | |
| 6555 | /** |
| 6556 | * xmlRelaxNGNewMemParserCtxt: |
| 6557 | * @buffer: a pointer to a char array containing the schemas |
| 6558 | * @size: the size of the array |
| 6559 | * |
| 6560 | * Create an XML RelaxNGs parse context for that memory buffer expected |
| 6561 | * to contain an XML RelaxNGs file. |
| 6562 | * |
| 6563 | * Returns the parser context or NULL in case of error |
| 6564 | */ |
| 6565 | xmlRelaxNGParserCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6566 | xmlRelaxNGNewMemParserCtxt(const char *buffer, int size) |
| 6567 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6568 | xmlRelaxNGParserCtxtPtr ret; |
| 6569 | |
| 6570 | if ((buffer == NULL) || (size <= 0)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6571 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6572 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6573 | ret = |
| 6574 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6575 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6576 | xmlRngPErrMemory(NULL, "building parser\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6577 | return (NULL); |
| 6578 | } |
| 6579 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6580 | ret->buffer = buffer; |
| 6581 | ret->size = size; |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 6582 | ret->error = xmlGenericError; |
| 6583 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6584 | return (ret); |
| 6585 | } |
| 6586 | |
| 6587 | /** |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6588 | * xmlRelaxNGNewDocParserCtxt: |
| 6589 | * @doc: a preparsed document tree |
| 6590 | * |
| 6591 | * Create an XML RelaxNGs parser context for that document. |
| 6592 | * Note: since the process of compiling a RelaxNG schemas modifies the |
| 6593 | * document, the @doc parameter is duplicated internally. |
| 6594 | * |
| 6595 | * Returns the parser context or NULL in case of error |
| 6596 | */ |
| 6597 | xmlRelaxNGParserCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6598 | xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc) |
| 6599 | { |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6600 | xmlRelaxNGParserCtxtPtr ret; |
| 6601 | xmlDocPtr copy; |
| 6602 | |
| 6603 | if (doc == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6604 | return (NULL); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6605 | copy = xmlCopyDoc(doc, 1); |
| 6606 | if (copy == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6607 | return (NULL); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6608 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6609 | ret = |
| 6610 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6611 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6612 | xmlRngPErrMemory(NULL, "building parser\n"); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6613 | return (NULL); |
| 6614 | } |
| 6615 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6616 | ret->document = copy; |
| 6617 | ret->userData = xmlGenericErrorContext; |
| 6618 | return (ret); |
| 6619 | } |
| 6620 | |
| 6621 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6622 | * xmlRelaxNGFreeParserCtxt: |
| 6623 | * @ctxt: the schema parser context |
| 6624 | * |
| 6625 | * Free the resources associated to the schema parser context |
| 6626 | */ |
| 6627 | void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6628 | xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt) |
| 6629 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6630 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6631 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6632 | if (ctxt->URL != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6633 | xmlFree(ctxt->URL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6634 | if (ctxt->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6635 | xmlRelaxNGFreeDocument(ctxt->doc); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 6636 | if (ctxt->interleaves != NULL) |
| 6637 | xmlHashFree(ctxt->interleaves, NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6638 | if (ctxt->documents != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6639 | xmlRelaxNGFreeDocumentList(ctxt->documents); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 6640 | if (ctxt->includes != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6641 | xmlRelaxNGFreeIncludeList(ctxt->includes); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6642 | if (ctxt->docTab != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6643 | xmlFree(ctxt->docTab); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 6644 | if (ctxt->incTab != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6645 | xmlFree(ctxt->incTab); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6646 | if (ctxt->defTab != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6647 | int i; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6648 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6649 | for (i = 0; i < ctxt->defNr; i++) |
| 6650 | xmlRelaxNGFreeDefine(ctxt->defTab[i]); |
| 6651 | xmlFree(ctxt->defTab); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6652 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6653 | xmlFree(ctxt); |
| 6654 | } |
| 6655 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6656 | /** |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6657 | * xmlRelaxNGNormExtSpace: |
| 6658 | * @value: a value |
| 6659 | * |
| 6660 | * Removes the leading and ending spaces of the value |
| 6661 | * The string is modified "in situ" |
| 6662 | */ |
| 6663 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6664 | xmlRelaxNGNormExtSpace(xmlChar * value) |
| 6665 | { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6666 | xmlChar *start = value; |
| 6667 | xmlChar *cur = value; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6668 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6669 | if (value == NULL) |
| 6670 | return; |
| 6671 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6672 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6673 | cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6674 | if (cur == start) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6675 | do { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6676 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6677 | cur++; |
| 6678 | if (*cur == 0) |
| 6679 | return; |
| 6680 | start = cur; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6681 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6682 | cur++; |
| 6683 | if (*cur == 0) { |
| 6684 | *start = 0; |
| 6685 | return; |
| 6686 | } |
| 6687 | } while (1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6688 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6689 | do { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6690 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6691 | *start++ = *cur++; |
| 6692 | if (*cur == 0) { |
| 6693 | *start = 0; |
| 6694 | return; |
| 6695 | } |
| 6696 | /* don't try to normalize the inner spaces */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6697 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6698 | cur++; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6699 | if (*cur == 0) { |
| 6700 | *start = 0; |
| 6701 | return; |
| 6702 | } |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 6703 | *start++ = *cur++; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6704 | } while (1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6705 | } |
| 6706 | } |
| 6707 | |
| 6708 | /** |
| 6709 | * xmlRelaxNGCheckAttributes: |
| 6710 | * @ctxt: a Relax-NG parser context |
| 6711 | * @node: a Relax-NG node |
| 6712 | * |
| 6713 | * Check all the attributes on the given node |
| 6714 | */ |
| 6715 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6716 | xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 6717 | { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6718 | xmlAttrPtr cur, next; |
| 6719 | |
| 6720 | cur = node->properties; |
| 6721 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6722 | next = cur->next; |
| 6723 | if ((cur->ns == NULL) || |
| 6724 | (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
| 6725 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
| 6726 | if ((!xmlStrEqual(node->name, BAD_CAST "element")) && |
| 6727 | (!xmlStrEqual(node->name, BAD_CAST "attribute")) && |
| 6728 | (!xmlStrEqual(node->name, BAD_CAST "ref")) && |
| 6729 | (!xmlStrEqual(node->name, BAD_CAST "parentRef")) && |
| 6730 | (!xmlStrEqual(node->name, BAD_CAST "param")) && |
| 6731 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
| 6732 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6733 | "Attribute %s is not allowed on %s\n", |
| 6734 | cur->name, node->name); |
| 6735 | } |
| 6736 | } else if (xmlStrEqual(cur->name, BAD_CAST "type")) { |
| 6737 | if ((!xmlStrEqual(node->name, BAD_CAST "value")) && |
| 6738 | (!xmlStrEqual(node->name, BAD_CAST "data"))) { |
| 6739 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6740 | "Attribute %s is not allowed on %s\n", |
| 6741 | cur->name, node->name); |
| 6742 | } |
| 6743 | } else if (xmlStrEqual(cur->name, BAD_CAST "href")) { |
| 6744 | if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) && |
| 6745 | (!xmlStrEqual(node->name, BAD_CAST "include"))) { |
| 6746 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6747 | "Attribute %s is not allowed on %s\n", |
| 6748 | cur->name, node->name); |
| 6749 | } |
| 6750 | } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) { |
| 6751 | if ((!xmlStrEqual(node->name, BAD_CAST "start")) && |
| 6752 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
| 6753 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6754 | "Attribute %s is not allowed on %s\n", |
| 6755 | cur->name, node->name); |
| 6756 | } |
| 6757 | } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) { |
| 6758 | xmlChar *val; |
| 6759 | xmlURIPtr uri; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6760 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6761 | val = xmlNodeListGetString(node->doc, cur->children, 1); |
| 6762 | if (val != NULL) { |
| 6763 | if (val[0] != 0) { |
| 6764 | uri = xmlParseURI((const char *) val); |
| 6765 | if (uri == NULL) { |
| 6766 | xmlRngPErr(ctxt, node, XML_RNGP_INVALID_URI, |
| 6767 | "Attribute %s contains invalid URI %s\n", |
| 6768 | cur->name, val); |
| 6769 | } else { |
| 6770 | if (uri->scheme == NULL) { |
| 6771 | xmlRngPErr(ctxt, node, XML_RNGP_URI_NOT_ABSOLUTE, |
| 6772 | "Attribute %s URI %s is not absolute\n", |
| 6773 | cur->name, val); |
| 6774 | } |
| 6775 | if (uri->fragment != NULL) { |
| 6776 | xmlRngPErr(ctxt, node, XML_RNGP_URI_FRAGMENT, |
| 6777 | "Attribute %s URI %s has a fragment ID\n", |
| 6778 | cur->name, val); |
| 6779 | } |
| 6780 | xmlFreeURI(uri); |
| 6781 | } |
| 6782 | } |
| 6783 | xmlFree(val); |
| 6784 | } |
| 6785 | } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) { |
| 6786 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_ATTRIBUTE, |
| 6787 | "Unknown attribute %s on %s\n", cur->name, |
| 6788 | node->name); |
| 6789 | } |
| 6790 | } |
| 6791 | cur = next; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6792 | } |
| 6793 | } |
| 6794 | |
| 6795 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6796 | * xmlRelaxNGCleanupTree: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6797 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6798 | * @root: an xmlNodePtr subtree |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6799 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6800 | * Cleanup the subtree from unwanted nodes for parsing, resolve |
| 6801 | * Include and externalRef lookups. |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6802 | */ |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6803 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6804 | xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root) |
| 6805 | { |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6806 | xmlNodePtr cur, delete; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6807 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6808 | delete = NULL; |
| 6809 | cur = root; |
| 6810 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6811 | if (delete != NULL) { |
| 6812 | xmlUnlinkNode(delete); |
| 6813 | xmlFreeNode(delete); |
| 6814 | delete = NULL; |
| 6815 | } |
| 6816 | if (cur->type == XML_ELEMENT_NODE) { |
| 6817 | /* |
| 6818 | * Simplification 4.1. Annotations |
| 6819 | */ |
| 6820 | if ((cur->ns == NULL) || |
| 6821 | (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
| 6822 | if ((cur->parent != NULL) && |
| 6823 | (cur->parent->type == XML_ELEMENT_NODE) && |
| 6824 | ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) || |
| 6825 | (xmlStrEqual(cur->parent->name, BAD_CAST "value")) || |
| 6826 | (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) { |
| 6827 | xmlRngPErr(ctxt, cur, XML_RNGP_FOREIGN_ELEMENT, |
| 6828 | "element %s doesn't allow foreign elements\n", |
| 6829 | cur->parent->name, NULL); |
| 6830 | } |
| 6831 | delete = cur; |
| 6832 | goto skip_children; |
| 6833 | } else { |
| 6834 | xmlRelaxNGCleanupAttributes(ctxt, cur); |
| 6835 | if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) { |
| 6836 | xmlChar *href, *ns, *base, *URL; |
| 6837 | xmlRelaxNGDocumentPtr docu; |
| 6838 | xmlNodePtr tmp; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6839 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6840 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6841 | if (ns == NULL) { |
| 6842 | tmp = cur->parent; |
| 6843 | while ((tmp != NULL) && |
| 6844 | (tmp->type == XML_ELEMENT_NODE)) { |
| 6845 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 6846 | if (ns != NULL) |
| 6847 | break; |
| 6848 | tmp = tmp->parent; |
| 6849 | } |
| 6850 | } |
| 6851 | href = xmlGetProp(cur, BAD_CAST "href"); |
| 6852 | if (href == NULL) { |
| 6853 | xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF, |
| 6854 | "xmlRelaxNGParse: externalRef has no href attribute\n", |
| 6855 | NULL, NULL); |
| 6856 | delete = cur; |
| 6857 | goto skip_children; |
| 6858 | } |
| 6859 | base = xmlNodeGetBase(cur->doc, cur); |
| 6860 | URL = xmlBuildURI(href, base); |
| 6861 | if (URL == NULL) { |
| 6862 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
| 6863 | "Failed to compute URL for externalRef %s\n", |
| 6864 | href, NULL); |
| 6865 | if (href != NULL) |
| 6866 | xmlFree(href); |
| 6867 | if (base != NULL) |
| 6868 | xmlFree(base); |
| 6869 | delete = cur; |
| 6870 | goto skip_children; |
| 6871 | } |
| 6872 | if (href != NULL) |
| 6873 | xmlFree(href); |
| 6874 | if (base != NULL) |
| 6875 | xmlFree(base); |
| 6876 | docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns); |
| 6877 | if (docu == NULL) { |
| 6878 | xmlRngPErr(ctxt, cur, XML_RNGP_EXTERNAL_REF_FAILURE, |
| 6879 | "Failed to load externalRef %s\n", URL, |
| 6880 | NULL); |
| 6881 | xmlFree(URL); |
| 6882 | delete = cur; |
| 6883 | goto skip_children; |
| 6884 | } |
| 6885 | if (ns != NULL) |
| 6886 | xmlFree(ns); |
| 6887 | xmlFree(URL); |
| 6888 | cur->_private = docu; |
| 6889 | } else if (xmlStrEqual(cur->name, BAD_CAST "include")) { |
| 6890 | xmlChar *href, *ns, *base, *URL; |
| 6891 | xmlRelaxNGIncludePtr incl; |
| 6892 | xmlNodePtr tmp; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6893 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6894 | href = xmlGetProp(cur, BAD_CAST "href"); |
| 6895 | if (href == NULL) { |
| 6896 | xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF, |
| 6897 | "xmlRelaxNGParse: include has no href attribute\n", |
| 6898 | NULL, NULL); |
| 6899 | delete = cur; |
| 6900 | goto skip_children; |
| 6901 | } |
| 6902 | base = xmlNodeGetBase(cur->doc, cur); |
| 6903 | URL = xmlBuildURI(href, base); |
| 6904 | if (URL == NULL) { |
| 6905 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
| 6906 | "Failed to compute URL for include %s\n", |
| 6907 | href, NULL); |
| 6908 | if (href != NULL) |
| 6909 | xmlFree(href); |
| 6910 | if (base != NULL) |
| 6911 | xmlFree(base); |
| 6912 | delete = cur; |
| 6913 | goto skip_children; |
| 6914 | } |
| 6915 | if (href != NULL) |
| 6916 | xmlFree(href); |
| 6917 | if (base != NULL) |
| 6918 | xmlFree(base); |
| 6919 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6920 | if (ns == NULL) { |
| 6921 | tmp = cur->parent; |
| 6922 | while ((tmp != NULL) && |
| 6923 | (tmp->type == XML_ELEMENT_NODE)) { |
| 6924 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 6925 | if (ns != NULL) |
| 6926 | break; |
| 6927 | tmp = tmp->parent; |
| 6928 | } |
| 6929 | } |
| 6930 | incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns); |
| 6931 | if (ns != NULL) |
| 6932 | xmlFree(ns); |
| 6933 | if (incl == NULL) { |
| 6934 | xmlRngPErr(ctxt, cur, XML_RNGP_INCLUDE_FAILURE, |
| 6935 | "Failed to load include %s\n", URL, |
| 6936 | NULL); |
| 6937 | xmlFree(URL); |
| 6938 | delete = cur; |
| 6939 | goto skip_children; |
| 6940 | } |
| 6941 | xmlFree(URL); |
| 6942 | cur->_private = incl; |
| 6943 | } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) || |
| 6944 | (xmlStrEqual(cur->name, BAD_CAST "attribute"))) |
| 6945 | { |
| 6946 | xmlChar *name, *ns; |
| 6947 | xmlNodePtr text = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6948 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6949 | /* |
| 6950 | * Simplification 4.8. name attribute of element |
| 6951 | * and attribute elements |
| 6952 | */ |
| 6953 | name = xmlGetProp(cur, BAD_CAST "name"); |
| 6954 | if (name != NULL) { |
| 6955 | if (cur->children == NULL) { |
| 6956 | text = |
| 6957 | xmlNewChild(cur, cur->ns, BAD_CAST "name", |
| 6958 | name); |
| 6959 | } else { |
| 6960 | xmlNodePtr node; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6961 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6962 | node = xmlNewNode(cur->ns, BAD_CAST "name"); |
| 6963 | if (node != NULL) { |
| 6964 | xmlAddPrevSibling(cur->children, node); |
| 6965 | text = xmlNewText(name); |
| 6966 | xmlAddChild(node, text); |
| 6967 | text = node; |
| 6968 | } |
| 6969 | } |
| 6970 | if (text == NULL) { |
| 6971 | xmlRngPErr(ctxt, cur, XML_RNGP_CREATE_FAILURE, |
| 6972 | "Failed to create a name %s element\n", |
| 6973 | name, NULL); |
| 6974 | } |
| 6975 | xmlUnsetProp(cur, BAD_CAST "name"); |
| 6976 | xmlFree(name); |
| 6977 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6978 | if (ns != NULL) { |
| 6979 | if (text != NULL) { |
| 6980 | xmlSetProp(text, BAD_CAST "ns", ns); |
| 6981 | /* xmlUnsetProp(cur, BAD_CAST "ns"); */ |
| 6982 | } |
| 6983 | xmlFree(ns); |
| 6984 | } else if (xmlStrEqual(cur->name, |
| 6985 | BAD_CAST "attribute")) { |
| 6986 | xmlSetProp(text, BAD_CAST "ns", BAD_CAST ""); |
| 6987 | } |
| 6988 | } |
| 6989 | } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) || |
| 6990 | (xmlStrEqual(cur->name, BAD_CAST "nsName")) || |
| 6991 | (xmlStrEqual(cur->name, BAD_CAST "value"))) { |
| 6992 | /* |
| 6993 | * Simplification 4.8. name attribute of element |
| 6994 | * and attribute elements |
| 6995 | */ |
| 6996 | if (xmlHasProp(cur, BAD_CAST "ns") == NULL) { |
| 6997 | xmlNodePtr node; |
| 6998 | xmlChar *ns = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6999 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7000 | node = cur->parent; |
| 7001 | while ((node != NULL) && |
| 7002 | (node->type == XML_ELEMENT_NODE)) { |
| 7003 | ns = xmlGetProp(node, BAD_CAST "ns"); |
| 7004 | if (ns != NULL) { |
| 7005 | break; |
| 7006 | } |
| 7007 | node = node->parent; |
| 7008 | } |
| 7009 | if (ns == NULL) { |
| 7010 | xmlSetProp(cur, BAD_CAST "ns", BAD_CAST ""); |
| 7011 | } else { |
| 7012 | xmlSetProp(cur, BAD_CAST "ns", ns); |
| 7013 | xmlFree(ns); |
| 7014 | } |
| 7015 | } |
| 7016 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
| 7017 | xmlChar *name, *local, *prefix; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7018 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7019 | /* |
| 7020 | * Simplification: 4.10. QNames |
| 7021 | */ |
| 7022 | name = xmlNodeGetContent(cur); |
| 7023 | if (name != NULL) { |
| 7024 | local = xmlSplitQName2(name, &prefix); |
| 7025 | if (local != NULL) { |
| 7026 | xmlNsPtr ns; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7027 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7028 | ns = xmlSearchNs(cur->doc, cur, prefix); |
| 7029 | if (ns == NULL) { |
| 7030 | xmlRngPErr(ctxt, cur, |
| 7031 | XML_RNGP_PREFIX_UNDEFINED, |
| 7032 | "xmlRelaxNGParse: no namespace for prefix %s\n", |
| 7033 | prefix, NULL); |
| 7034 | } else { |
| 7035 | xmlSetProp(cur, BAD_CAST "ns", |
| 7036 | ns->href); |
| 7037 | xmlNodeSetContent(cur, local); |
| 7038 | } |
| 7039 | xmlFree(local); |
| 7040 | xmlFree(prefix); |
| 7041 | } |
| 7042 | xmlFree(name); |
| 7043 | } |
| 7044 | } |
| 7045 | /* |
| 7046 | * 4.16 |
| 7047 | */ |
| 7048 | if (xmlStrEqual(cur->name, BAD_CAST "nsName")) { |
| 7049 | if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
| 7050 | xmlRngPErr(ctxt, cur, |
| 7051 | XML_RNGP_PAT_NSNAME_EXCEPT_NSNAME, |
| 7052 | "Found nsName/except//nsName forbidden construct\n", |
| 7053 | NULL, NULL); |
| 7054 | } |
| 7055 | } |
| 7056 | } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) && |
| 7057 | (cur != root)) { |
| 7058 | int oldflags = ctxt->flags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7059 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7060 | /* |
| 7061 | * 4.16 |
| 7062 | */ |
| 7063 | if ((cur->parent != NULL) && |
| 7064 | (xmlStrEqual |
| 7065 | (cur->parent->name, BAD_CAST "anyName"))) { |
| 7066 | ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT; |
| 7067 | xmlRelaxNGCleanupTree(ctxt, cur); |
| 7068 | ctxt->flags = oldflags; |
| 7069 | goto skip_children; |
| 7070 | } else if ((cur->parent != NULL) && |
| 7071 | (xmlStrEqual |
| 7072 | (cur->parent->name, BAD_CAST "nsName"))) { |
| 7073 | ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT; |
| 7074 | xmlRelaxNGCleanupTree(ctxt, cur); |
| 7075 | ctxt->flags = oldflags; |
| 7076 | goto skip_children; |
| 7077 | } |
| 7078 | } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) { |
| 7079 | /* |
| 7080 | * 4.16 |
| 7081 | */ |
| 7082 | if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) { |
| 7083 | xmlRngPErr(ctxt, cur, |
| 7084 | XML_RNGP_PAT_ANYNAME_EXCEPT_ANYNAME, |
| 7085 | "Found anyName/except//anyName forbidden construct\n", |
| 7086 | NULL, NULL); |
| 7087 | } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
| 7088 | xmlRngPErr(ctxt, cur, |
| 7089 | XML_RNGP_PAT_NSNAME_EXCEPT_ANYNAME, |
| 7090 | "Found nsName/except//anyName forbidden construct\n", |
| 7091 | NULL, NULL); |
| 7092 | } |
| 7093 | } |
| 7094 | /* |
| 7095 | * Thisd is not an else since "include" is transformed |
| 7096 | * into a div |
| 7097 | */ |
| 7098 | if (xmlStrEqual(cur->name, BAD_CAST "div")) { |
| 7099 | xmlChar *ns; |
| 7100 | xmlNodePtr child, ins, tmp; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7101 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7102 | /* |
| 7103 | * implements rule 4.11 |
| 7104 | */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7105 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7106 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 7107 | |
| 7108 | child = cur->children; |
| 7109 | ins = cur; |
| 7110 | while (child != NULL) { |
| 7111 | if (ns != NULL) { |
| 7112 | if (!xmlHasProp(child, BAD_CAST "ns")) { |
| 7113 | xmlSetProp(child, BAD_CAST "ns", ns); |
| 7114 | } |
| 7115 | } |
| 7116 | tmp = child->next; |
| 7117 | xmlUnlinkNode(child); |
| 7118 | ins = xmlAddNextSibling(ins, child); |
| 7119 | child = tmp; |
| 7120 | } |
| 7121 | if (ns != NULL) |
| 7122 | xmlFree(ns); |
| 7123 | delete = cur; |
| 7124 | goto skip_children; |
| 7125 | } |
| 7126 | } |
| 7127 | } |
| 7128 | /* |
| 7129 | * Simplification 4.2 whitespaces |
| 7130 | */ |
| 7131 | else if ((cur->type == XML_TEXT_NODE) || |
| 7132 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 7133 | if (IS_BLANK_NODE(cur)) { |
| 7134 | if (cur->parent->type == XML_ELEMENT_NODE) { |
| 7135 | if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value")) |
| 7136 | && |
| 7137 | (!xmlStrEqual |
| 7138 | (cur->parent->name, BAD_CAST "param"))) |
| 7139 | delete = cur; |
| 7140 | } else { |
| 7141 | delete = cur; |
| 7142 | goto skip_children; |
| 7143 | } |
| 7144 | } |
| 7145 | } else { |
| 7146 | delete = cur; |
| 7147 | goto skip_children; |
| 7148 | } |
| 7149 | |
| 7150 | /* |
| 7151 | * Skip to next node |
| 7152 | */ |
| 7153 | if (cur->children != NULL) { |
| 7154 | if ((cur->children->type != XML_ENTITY_DECL) && |
| 7155 | (cur->children->type != XML_ENTITY_REF_NODE) && |
| 7156 | (cur->children->type != XML_ENTITY_NODE)) { |
| 7157 | cur = cur->children; |
| 7158 | continue; |
| 7159 | } |
| 7160 | } |
| 7161 | skip_children: |
| 7162 | if (cur->next != NULL) { |
| 7163 | cur = cur->next; |
| 7164 | continue; |
| 7165 | } |
| 7166 | |
| 7167 | do { |
| 7168 | cur = cur->parent; |
| 7169 | if (cur == NULL) |
| 7170 | break; |
| 7171 | if (cur == root) { |
| 7172 | cur = NULL; |
| 7173 | break; |
| 7174 | } |
| 7175 | if (cur->next != NULL) { |
| 7176 | cur = cur->next; |
| 7177 | break; |
| 7178 | } |
| 7179 | } while (cur != NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7180 | } |
| 7181 | if (delete != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7182 | xmlUnlinkNode(delete); |
| 7183 | xmlFreeNode(delete); |
| 7184 | delete = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7185 | } |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7186 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7187 | |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7188 | /** |
| 7189 | * xmlRelaxNGCleanupDoc: |
| 7190 | * @ctxt: a Relax-NG parser context |
| 7191 | * @doc: an xmldocPtr document pointer |
| 7192 | * |
| 7193 | * Cleanup the document from unwanted nodes for parsing, resolve |
| 7194 | * Include and externalRef lookups. |
| 7195 | * |
| 7196 | * Returns the cleaned up document or NULL in case of error |
| 7197 | */ |
| 7198 | static xmlDocPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7199 | xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc) |
| 7200 | { |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7201 | xmlNodePtr root; |
| 7202 | |
| 7203 | /* |
| 7204 | * Extract the root |
| 7205 | */ |
| 7206 | root = xmlDocGetRootElement(doc); |
| 7207 | if (root == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7208 | xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n", |
| 7209 | ctxt->URL, NULL); |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7210 | return (NULL); |
| 7211 | } |
| 7212 | xmlRelaxNGCleanupTree(ctxt, root); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7213 | return (doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7214 | } |
| 7215 | |
| 7216 | /** |
| 7217 | * xmlRelaxNGParse: |
| 7218 | * @ctxt: a Relax-NG parser context |
| 7219 | * |
| 7220 | * parse a schema definition resource and build an internal |
| 7221 | * XML Shema struture which can be used to validate instances. |
| 7222 | * *WARNING* this interface is highly subject to change |
| 7223 | * |
| 7224 | * Returns the internal XML RelaxNG structure built from the resource or |
| 7225 | * NULL in case of error |
| 7226 | */ |
| 7227 | xmlRelaxNGPtr |
| 7228 | xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt) |
| 7229 | { |
| 7230 | xmlRelaxNGPtr ret = NULL; |
| 7231 | xmlDocPtr doc; |
| 7232 | xmlNodePtr root; |
| 7233 | |
| 7234 | xmlRelaxNGInitTypes(); |
| 7235 | |
| 7236 | if (ctxt == NULL) |
| 7237 | return (NULL); |
| 7238 | |
| 7239 | /* |
| 7240 | * First step is to parse the input document into an DOM/Infoset |
| 7241 | */ |
| 7242 | if (ctxt->URL != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7243 | doc = xmlParseFile((const char *) ctxt->URL); |
| 7244 | if (doc == NULL) { |
| 7245 | xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR, |
| 7246 | "xmlRelaxNGParse: could not load %s\n", ctxt->URL, |
| 7247 | NULL); |
| 7248 | return (NULL); |
| 7249 | } |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7250 | } else if (ctxt->buffer != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7251 | doc = xmlParseMemory(ctxt->buffer, ctxt->size); |
| 7252 | if (doc == NULL) { |
| 7253 | xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR, |
| 7254 | "xmlRelaxNGParse: could not parse schemas\n", NULL, |
| 7255 | NULL); |
| 7256 | return (NULL); |
| 7257 | } |
| 7258 | doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
| 7259 | ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 7260 | } else if (ctxt->document != NULL) { |
| 7261 | doc = ctxt->document; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7262 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7263 | xmlRngPErr(ctxt, NULL, XML_RNGP_EMPTY, |
| 7264 | "xmlRelaxNGParse: nothing to parse\n", NULL, NULL); |
| 7265 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7266 | } |
| 7267 | ctxt->document = doc; |
| 7268 | |
| 7269 | /* |
| 7270 | * Some preprocessing of the document content |
| 7271 | */ |
| 7272 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 7273 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7274 | xmlFreeDoc(ctxt->document); |
| 7275 | ctxt->document = NULL; |
| 7276 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7277 | } |
| 7278 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7279 | /* |
| 7280 | * Then do the parsing for good |
| 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, |
| 7285 | XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n", |
| 7286 | ctxt->URL, NULL); |
| 7287 | xmlFreeDoc(doc); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7288 | return (NULL); |
| 7289 | } |
| 7290 | ret = xmlRelaxNGParseDocument(ctxt, root); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7291 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7292 | xmlFreeDoc(doc); |
| 7293 | return (NULL); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7294 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7295 | |
| 7296 | /* |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7297 | * Check the ref/defines links |
| 7298 | */ |
| 7299 | /* |
| 7300 | * try to preprocess interleaves |
| 7301 | */ |
| 7302 | if (ctxt->interleaves != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7303 | xmlHashScan(ctxt->interleaves, |
| 7304 | (xmlHashScanner) xmlRelaxNGComputeInterleaves, ctxt); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7305 | } |
| 7306 | |
| 7307 | /* |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7308 | * if there was a parsing error return NULL |
| 7309 | */ |
| 7310 | if (ctxt->nbErrors > 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7311 | xmlRelaxNGFree(ret); |
| 7312 | ctxt->document = NULL; |
| 7313 | xmlFreeDoc(doc); |
| 7314 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7315 | } |
| 7316 | |
| 7317 | /* |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 7318 | * try to compile (parts of) the schemas |
| 7319 | */ |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7320 | if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) { |
| 7321 | if (ret->topgrammar->start->type != XML_RELAXNG_START) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7322 | xmlRelaxNGDefinePtr def; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7323 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7324 | def = xmlRelaxNGNewDefine(ctxt, NULL); |
| 7325 | if (def != NULL) { |
| 7326 | def->type = XML_RELAXNG_START; |
| 7327 | def->content = ret->topgrammar->start; |
| 7328 | ret->topgrammar->start = def; |
| 7329 | } |
| 7330 | } |
| 7331 | xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7332 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 7333 | |
| 7334 | /* |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7335 | * Transfer the pointer for cleanup at the schema level. |
| 7336 | */ |
| 7337 | ret->doc = doc; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7338 | ctxt->document = NULL; |
| 7339 | ret->documents = ctxt->documents; |
| 7340 | ctxt->documents = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7341 | |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 7342 | ret->includes = ctxt->includes; |
| 7343 | ctxt->includes = NULL; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7344 | ret->defNr = ctxt->defNr; |
| 7345 | ret->defTab = ctxt->defTab; |
| 7346 | ctxt->defTab = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7347 | if (ctxt->idref == 1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7348 | ret->idref = 1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7349 | |
| 7350 | return (ret); |
| 7351 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7352 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7353 | /** |
| 7354 | * xmlRelaxNGSetParserErrors: |
| 7355 | * @ctxt: a Relax-NG validation context |
| 7356 | * @err: the error callback |
| 7357 | * @warn: the warning callback |
| 7358 | * @ctx: contextual data for the callbacks |
| 7359 | * |
| 7360 | * Set the callback functions used to handle errors for a validation context |
| 7361 | */ |
| 7362 | void |
| 7363 | xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7364 | xmlRelaxNGValidityErrorFunc err, |
| 7365 | xmlRelaxNGValidityWarningFunc warn, void *ctx) |
| 7366 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7367 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7368 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7369 | ctxt->error = err; |
| 7370 | ctxt->warning = warn; |
| 7371 | ctxt->userData = ctx; |
| 7372 | } |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 7373 | |
| 7374 | /** |
| 7375 | * xmlRelaxNGGetParserErrors: |
| 7376 | * @ctxt: a Relax-NG validation context |
| 7377 | * @err: the error callback result |
| 7378 | * @warn: the warning callback result |
| 7379 | * @ctx: contextual data for the callbacks result |
| 7380 | * |
| 7381 | * Get the callback information used to handle errors for a validation context |
| 7382 | * |
| 7383 | * Returns -1 in case of failure, 0 otherwise. |
| 7384 | */ |
| 7385 | int |
| 7386 | xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7387 | xmlRelaxNGValidityErrorFunc * err, |
| 7388 | xmlRelaxNGValidityWarningFunc * warn, void **ctx) |
| 7389 | { |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 7390 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7391 | return (-1); |
| 7392 | if (err != NULL) |
| 7393 | *err = ctxt->error; |
| 7394 | if (warn != NULL) |
| 7395 | *warn = ctxt->warning; |
| 7396 | if (ctx != NULL) |
| 7397 | *ctx = ctxt->userData; |
| 7398 | return (0); |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 7399 | } |
| 7400 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 7401 | #ifdef LIBXML_OUTPUT_ENABLED |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7402 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7403 | /************************************************************************ |
| 7404 | * * |
| 7405 | * Dump back a compiled form * |
| 7406 | * * |
| 7407 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7408 | static void xmlRelaxNGDumpDefine(FILE * output, |
| 7409 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7410 | |
| 7411 | /** |
| 7412 | * xmlRelaxNGDumpDefines: |
| 7413 | * @output: the file output |
| 7414 | * @defines: a list of define structures |
| 7415 | * |
| 7416 | * Dump a RelaxNG structure back |
| 7417 | */ |
| 7418 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7419 | xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines) |
| 7420 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7421 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7422 | xmlRelaxNGDumpDefine(output, defines); |
| 7423 | defines = defines->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7424 | } |
| 7425 | } |
| 7426 | |
| 7427 | /** |
| 7428 | * xmlRelaxNGDumpDefine: |
| 7429 | * @output: the file output |
| 7430 | * @define: a define structure |
| 7431 | * |
| 7432 | * Dump a RelaxNG structure back |
| 7433 | */ |
| 7434 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7435 | xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define) |
| 7436 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7437 | if (define == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7438 | return; |
| 7439 | switch (define->type) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7440 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7441 | fprintf(output, "<empty/>\n"); |
| 7442 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7443 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7444 | fprintf(output, "<notAllowed/>\n"); |
| 7445 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7446 | case XML_RELAXNG_TEXT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7447 | fprintf(output, "<text/>\n"); |
| 7448 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7449 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7450 | fprintf(output, "<element>\n"); |
| 7451 | if (define->name != NULL) { |
| 7452 | fprintf(output, "<name"); |
| 7453 | if (define->ns != NULL) |
| 7454 | fprintf(output, " ns=\"%s\"", define->ns); |
| 7455 | fprintf(output, ">%s</name>\n", define->name); |
| 7456 | } |
| 7457 | xmlRelaxNGDumpDefines(output, define->attrs); |
| 7458 | xmlRelaxNGDumpDefines(output, define->content); |
| 7459 | fprintf(output, "</element>\n"); |
| 7460 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7461 | case XML_RELAXNG_LIST: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7462 | fprintf(output, "<list>\n"); |
| 7463 | xmlRelaxNGDumpDefines(output, define->content); |
| 7464 | fprintf(output, "</list>\n"); |
| 7465 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7466 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7467 | fprintf(output, "<oneOrMore>\n"); |
| 7468 | xmlRelaxNGDumpDefines(output, define->content); |
| 7469 | fprintf(output, "</oneOrMore>\n"); |
| 7470 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7471 | case XML_RELAXNG_ZEROORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7472 | fprintf(output, "<zeroOrMore>\n"); |
| 7473 | xmlRelaxNGDumpDefines(output, define->content); |
| 7474 | fprintf(output, "</zeroOrMore>\n"); |
| 7475 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7476 | case XML_RELAXNG_CHOICE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7477 | fprintf(output, "<choice>\n"); |
| 7478 | xmlRelaxNGDumpDefines(output, define->content); |
| 7479 | fprintf(output, "</choice>\n"); |
| 7480 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7481 | case XML_RELAXNG_GROUP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7482 | fprintf(output, "<group>\n"); |
| 7483 | xmlRelaxNGDumpDefines(output, define->content); |
| 7484 | fprintf(output, "</group>\n"); |
| 7485 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7486 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7487 | fprintf(output, "<interleave>\n"); |
| 7488 | xmlRelaxNGDumpDefines(output, define->content); |
| 7489 | fprintf(output, "</interleave>\n"); |
| 7490 | break; |
| 7491 | case XML_RELAXNG_OPTIONAL: |
| 7492 | fprintf(output, "<optional>\n"); |
| 7493 | xmlRelaxNGDumpDefines(output, define->content); |
| 7494 | fprintf(output, "</optional>\n"); |
| 7495 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7496 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7497 | fprintf(output, "<attribute>\n"); |
| 7498 | xmlRelaxNGDumpDefines(output, define->content); |
| 7499 | fprintf(output, "</attribute>\n"); |
| 7500 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7501 | case XML_RELAXNG_DEF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7502 | fprintf(output, "<define"); |
| 7503 | if (define->name != NULL) |
| 7504 | fprintf(output, " name=\"%s\"", define->name); |
| 7505 | fprintf(output, ">\n"); |
| 7506 | xmlRelaxNGDumpDefines(output, define->content); |
| 7507 | fprintf(output, "</define>\n"); |
| 7508 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7509 | case XML_RELAXNG_REF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7510 | fprintf(output, "<ref"); |
| 7511 | if (define->name != NULL) |
| 7512 | fprintf(output, " name=\"%s\"", define->name); |
| 7513 | fprintf(output, ">\n"); |
| 7514 | xmlRelaxNGDumpDefines(output, define->content); |
| 7515 | fprintf(output, "</ref>\n"); |
| 7516 | break; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7517 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7518 | fprintf(output, "<parentRef"); |
| 7519 | if (define->name != NULL) |
| 7520 | fprintf(output, " name=\"%s\"", define->name); |
| 7521 | fprintf(output, ">\n"); |
| 7522 | xmlRelaxNGDumpDefines(output, define->content); |
| 7523 | fprintf(output, "</parentRef>\n"); |
| 7524 | break; |
| 7525 | case XML_RELAXNG_EXTERNALREF: |
| 7526 | fprintf(output, "<externalRef>"); |
| 7527 | xmlRelaxNGDumpDefines(output, define->content); |
| 7528 | fprintf(output, "</externalRef>\n"); |
| 7529 | break; |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 7530 | case XML_RELAXNG_DATATYPE: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7531 | case XML_RELAXNG_VALUE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7532 | TODO break; |
| 7533 | case XML_RELAXNG_START: |
| 7534 | case XML_RELAXNG_EXCEPT: |
| 7535 | case XML_RELAXNG_PARAM: |
| 7536 | TODO break; |
| 7537 | case XML_RELAXNG_NOOP: |
| 7538 | xmlRelaxNGDumpDefines(output, define->content); |
| 7539 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7540 | } |
| 7541 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7542 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7543 | /** |
| 7544 | * xmlRelaxNGDumpGrammar: |
| 7545 | * @output: the file output |
| 7546 | * @grammar: a grammar structure |
| 7547 | * @top: is this a top grammar |
| 7548 | * |
| 7549 | * Dump a RelaxNG structure back |
| 7550 | */ |
| 7551 | static void |
| 7552 | xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top) |
| 7553 | { |
| 7554 | if (grammar == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7555 | return; |
| 7556 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7557 | fprintf(output, "<grammar"); |
| 7558 | if (top) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7559 | fprintf(output, " xmlns=\"http://relaxng.org/ns/structure/1.0\""); |
| 7560 | switch (grammar->combine) { |
| 7561 | case XML_RELAXNG_COMBINE_UNDEFINED: |
| 7562 | break; |
| 7563 | case XML_RELAXNG_COMBINE_CHOICE: |
| 7564 | fprintf(output, " combine=\"choice\""); |
| 7565 | break; |
| 7566 | case XML_RELAXNG_COMBINE_INTERLEAVE: |
| 7567 | fprintf(output, " combine=\"interleave\""); |
| 7568 | break; |
| 7569 | default: |
| 7570 | fprintf(output, " <!-- invalid combine value -->"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7571 | } |
| 7572 | fprintf(output, ">\n"); |
| 7573 | if (grammar->start == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7574 | fprintf(output, " <!-- grammar had no start -->"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7575 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7576 | fprintf(output, "<start>\n"); |
| 7577 | xmlRelaxNGDumpDefine(output, grammar->start); |
| 7578 | fprintf(output, "</start>\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7579 | } |
| 7580 | /* TODO ? Dump the defines ? */ |
| 7581 | fprintf(output, "</grammar>\n"); |
| 7582 | } |
| 7583 | |
| 7584 | /** |
| 7585 | * xmlRelaxNGDump: |
| 7586 | * @output: the file output |
| 7587 | * @schema: a schema structure |
| 7588 | * |
| 7589 | * Dump a RelaxNG structure back |
| 7590 | */ |
| 7591 | void |
| 7592 | xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema) |
| 7593 | { |
| 7594 | if (schema == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7595 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
| 7596 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7597 | } |
| 7598 | fprintf(output, "RelaxNG: "); |
| 7599 | if (schema->doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7600 | fprintf(output, "no document\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7601 | } else if (schema->doc->URL != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7602 | fprintf(output, "%s\n", schema->doc->URL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7603 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7604 | fprintf(output, "\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7605 | } |
| 7606 | if (schema->topgrammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7607 | fprintf(output, "RelaxNG has no top grammar\n"); |
| 7608 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7609 | } |
| 7610 | xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1); |
| 7611 | } |
| 7612 | |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7613 | /** |
| 7614 | * xmlRelaxNGDumpTree: |
| 7615 | * @output: the file output |
| 7616 | * @schema: a schema structure |
| 7617 | * |
| 7618 | * Dump the transformed RelaxNG tree. |
| 7619 | */ |
| 7620 | void |
| 7621 | xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema) |
| 7622 | { |
| 7623 | if (schema == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7624 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
| 7625 | return; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7626 | } |
| 7627 | if (schema->doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7628 | fprintf(output, "no document\n"); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7629 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7630 | xmlDocDump(output, schema->doc); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7631 | } |
| 7632 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 7633 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7634 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7635 | /************************************************************************ |
| 7636 | * * |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7637 | * Validation of compiled content * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7638 | * * |
| 7639 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7640 | static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
| 7641 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7642 | |
| 7643 | /** |
| 7644 | * xmlRelaxNGValidateCompiledCallback: |
| 7645 | * @exec: the regular expression instance |
| 7646 | * @token: the token which matched |
| 7647 | * @transdata: callback data, the define for the subelement if available |
| 7648 | @ @inputdata: callback data, the Relax NG validation context |
| 7649 | * |
| 7650 | * Handle the callback and if needed validate the element children. |
| 7651 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7652 | static void |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7653 | xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7654 | const xmlChar * token, |
| 7655 | void *transdata, void *inputdata) |
| 7656 | { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7657 | xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata; |
| 7658 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata; |
| 7659 | int ret; |
| 7660 | |
| 7661 | #ifdef DEBUG_COMPILE |
| 7662 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7663 | "Compiled callback for: '%s'\n", token); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7664 | #endif |
| 7665 | if (ctxt == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7666 | fprintf(stderr, "callback on %s missing context\n", token); |
| 7667 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7668 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7669 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7670 | } |
| 7671 | if (define == NULL) { |
| 7672 | if (token[0] == '#') |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7673 | return; |
| 7674 | fprintf(stderr, "callback on %s missing define\n", token); |
| 7675 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7676 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7677 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7678 | } |
| 7679 | if ((ctxt == NULL) || (define == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7680 | fprintf(stderr, "callback on %s missing info\n", token); |
| 7681 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7682 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7683 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7684 | } else if (define->type != XML_RELAXNG_ELEMENT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7685 | fprintf(stderr, "callback on %s define is not element\n", token); |
| 7686 | if (ctxt->errNo == XML_RELAXNG_OK) |
| 7687 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7688 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7689 | } |
| 7690 | ret = xmlRelaxNGValidateDefinition(ctxt, define); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7691 | if (ret != 0) |
| 7692 | ctxt->perr = ret; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7693 | } |
| 7694 | |
| 7695 | /** |
| 7696 | * xmlRelaxNGValidateCompiledContent: |
| 7697 | * @ctxt: the RelaxNG validation context |
| 7698 | * @regexp: the regular expression as compiled |
| 7699 | * @content: list of children to test against the regexp |
| 7700 | * |
| 7701 | * Validate the content model of an element or start using the regexp |
| 7702 | * |
| 7703 | * Returns 0 in case of success, -1 in case of error. |
| 7704 | */ |
| 7705 | static int |
| 7706 | xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7707 | xmlRegexpPtr regexp, xmlNodePtr content) |
| 7708 | { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7709 | xmlRegExecCtxtPtr exec; |
| 7710 | xmlNodePtr cur; |
Daniel Veillard | 6216360 | 2003-04-17 09:36:38 +0000 | [diff] [blame] | 7711 | int ret = 0; |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7712 | int oldperr = ctxt->perr; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7713 | |
| 7714 | if ((ctxt == NULL) || (regexp == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7715 | return (-1); |
| 7716 | exec = xmlRegNewExecCtxt(regexp, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7717 | xmlRelaxNGValidateCompiledCallback, ctxt); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7718 | ctxt->perr = 0; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7719 | cur = content; |
| 7720 | while (cur != NULL) { |
| 7721 | ctxt->state->seq = cur; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7722 | switch (cur->type) { |
| 7723 | case XML_TEXT_NODE: |
| 7724 | case XML_CDATA_SECTION_NODE: |
| 7725 | if (xmlIsBlankNode(cur)) |
| 7726 | break; |
| 7727 | ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt); |
| 7728 | if (ret < 0) { |
| 7729 | VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, |
| 7730 | cur->parent->name); |
| 7731 | } |
| 7732 | break; |
| 7733 | case XML_ELEMENT_NODE: |
| 7734 | if (cur->ns != NULL) { |
| 7735 | ret = xmlRegExecPushString2(exec, cur->name, |
| 7736 | cur->ns->href, ctxt); |
| 7737 | } else { |
| 7738 | ret = xmlRegExecPushString(exec, cur->name, ctxt); |
| 7739 | } |
| 7740 | if (ret < 0) { |
| 7741 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name); |
| 7742 | } |
| 7743 | break; |
| 7744 | default: |
| 7745 | break; |
| 7746 | } |
| 7747 | if (ret < 0) |
| 7748 | break; |
| 7749 | /* |
| 7750 | * Switch to next element |
| 7751 | */ |
| 7752 | cur = cur->next; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7753 | } |
| 7754 | ret = xmlRegExecPushString(exec, NULL, NULL); |
| 7755 | if (ret == 1) { |
| 7756 | ret = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7757 | ctxt->state->seq = NULL; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7758 | } else if (ret == 0) { |
| 7759 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7760 | * TODO: get some of the names needed to exit the current state of exec |
| 7761 | */ |
| 7762 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST ""); |
| 7763 | ret = -1; |
| 7764 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 7765 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7766 | } else { |
| 7767 | ret = -1; |
| 7768 | } |
| 7769 | xmlRegFreeExecCtxt(exec); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7770 | /* |
| 7771 | * There might be content model errors outside of the pure |
| 7772 | * regexp validation, e.g. for attribute values. |
| 7773 | */ |
| 7774 | if ((ret == 0) && (ctxt->perr != 0)) { |
| 7775 | ret = ctxt->perr; |
| 7776 | } |
| 7777 | ctxt->perr = oldperr; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7778 | return (ret); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7779 | } |
| 7780 | |
| 7781 | /************************************************************************ |
| 7782 | * * |
| 7783 | * Progressive validation of when possible * |
| 7784 | * * |
| 7785 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7786 | static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
| 7787 | xmlRelaxNGDefinePtr defines); |
| 7788 | static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 7789 | int dolog); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 7790 | static void xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7791 | |
| 7792 | /** |
| 7793 | * xmlRelaxNGElemPush: |
| 7794 | * @ctxt: the validation context |
| 7795 | * @exec: the regexp runtime for the new content model |
| 7796 | * |
| 7797 | * Push a new regexp for the current node content model on the stack |
| 7798 | * |
| 7799 | * Returns 0 in case of success and -1 in case of error. |
| 7800 | */ |
| 7801 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7802 | xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec) |
| 7803 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7804 | if (ctxt->elemTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7805 | ctxt->elemMax = 10; |
| 7806 | ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax * |
| 7807 | sizeof |
| 7808 | (xmlRegExecCtxtPtr)); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7809 | if (ctxt->elemTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7810 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 7811 | return (-1); |
| 7812 | } |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7813 | } |
| 7814 | if (ctxt->elemNr >= ctxt->elemMax) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7815 | ctxt->elemMax *= 2; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7816 | ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7817 | ctxt->elemMax * |
| 7818 | sizeof |
| 7819 | (xmlRegExecCtxtPtr)); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7820 | if (ctxt->elemTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7821 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 7822 | return (-1); |
| 7823 | } |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7824 | } |
| 7825 | ctxt->elemTab[ctxt->elemNr++] = exec; |
| 7826 | ctxt->elem = exec; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7827 | return (0); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7828 | } |
| 7829 | |
| 7830 | /** |
| 7831 | * xmlRelaxNGElemPop: |
| 7832 | * @ctxt: the validation context |
| 7833 | * |
| 7834 | * Pop the regexp of the current node content model from the stack |
| 7835 | * |
| 7836 | * Returns the exec or NULL if empty |
| 7837 | */ |
| 7838 | static xmlRegExecCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7839 | xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt) |
| 7840 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7841 | xmlRegExecCtxtPtr ret; |
| 7842 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7843 | if (ctxt->elemNr <= 0) |
| 7844 | return (NULL); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7845 | ctxt->elemNr--; |
| 7846 | ret = ctxt->elemTab[ctxt->elemNr]; |
| 7847 | ctxt->elemTab[ctxt->elemNr] = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7848 | if (ctxt->elemNr > 0) |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7849 | ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1]; |
| 7850 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7851 | ctxt->elem = NULL; |
| 7852 | return (ret); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7853 | } |
| 7854 | |
| 7855 | /** |
| 7856 | * xmlRelaxNGValidateProgressiveCallback: |
| 7857 | * @exec: the regular expression instance |
| 7858 | * @token: the token which matched |
| 7859 | * @transdata: callback data, the define for the subelement if available |
| 7860 | @ @inputdata: callback data, the Relax NG validation context |
| 7861 | * |
| 7862 | * Handle the callback and if needed validate the element children. |
| 7863 | * some of the in/out informations are passed via the context in @inputdata. |
| 7864 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7865 | static void |
| 7866 | xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec |
| 7867 | ATTRIBUTE_UNUSED, |
| 7868 | const xmlChar * token, |
| 7869 | void *transdata, void *inputdata) |
| 7870 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7871 | xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata; |
| 7872 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7873 | xmlRelaxNGValidStatePtr state, oldstate; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7874 | xmlNodePtr node = ctxt->pnode; |
Daniel Veillard | c3ca5ba | 2003-05-09 22:26:28 +0000 | [diff] [blame] | 7875 | int ret = 0, oldflags; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7876 | |
| 7877 | #ifdef DEBUG_PROGRESSIVE |
| 7878 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7879 | "Progressive callback for: '%s'\n", token); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7880 | #endif |
| 7881 | if (ctxt == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7882 | fprintf(stderr, "callback on %s missing context\n", token); |
| 7883 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7884 | } |
| 7885 | ctxt->pstate = 1; |
| 7886 | if (define == NULL) { |
| 7887 | if (token[0] == '#') |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7888 | return; |
| 7889 | fprintf(stderr, "callback on %s missing define\n", token); |
| 7890 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7891 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7892 | ctxt->pstate = -1; |
| 7893 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7894 | } |
| 7895 | if ((ctxt == NULL) || (define == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7896 | fprintf(stderr, "callback on %s missing info\n", token); |
| 7897 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7898 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7899 | ctxt->pstate = -1; |
| 7900 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7901 | } else if (define->type != XML_RELAXNG_ELEMENT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7902 | fprintf(stderr, "callback on %s define is not element\n", token); |
| 7903 | if (ctxt->errNo == XML_RELAXNG_OK) |
| 7904 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7905 | ctxt->pstate = -1; |
| 7906 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7907 | } |
| 7908 | if (node->type != XML_ELEMENT_NODE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7909 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
| 7910 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 7911 | xmlRelaxNGDumpValidError(ctxt); |
| 7912 | ctxt->pstate = -1; |
| 7913 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7914 | } |
| 7915 | if (define->contModel == NULL) { |
| 7916 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7917 | * this node cannot be validated in a streamable fashion |
| 7918 | */ |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7919 | #ifdef DEBUG_PROGRESSIVE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7920 | xmlGenericError(xmlGenericErrorContext, |
| 7921 | "Element '%s' validation is not streamable\n", |
| 7922 | token); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7923 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7924 | ctxt->pstate = 0; |
| 7925 | ctxt->pdef = define; |
| 7926 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7927 | } |
| 7928 | exec = xmlRegNewExecCtxt(define->contModel, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7929 | xmlRelaxNGValidateProgressiveCallback, ctxt); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7930 | if (exec == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7931 | ctxt->pstate = -1; |
| 7932 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7933 | } |
| 7934 | xmlRelaxNGElemPush(ctxt, exec); |
| 7935 | |
| 7936 | /* |
| 7937 | * Validate the attributes part of the content. |
| 7938 | */ |
| 7939 | state = xmlRelaxNGNewValidState(ctxt, node); |
| 7940 | if (state == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7941 | ctxt->pstate = -1; |
| 7942 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7943 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7944 | oldstate = ctxt->state; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7945 | ctxt->state = state; |
| 7946 | if (define->attrs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7947 | ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
| 7948 | if (ret != 0) { |
| 7949 | ctxt->pstate = -1; |
| 7950 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
| 7951 | } |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7952 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7953 | if (ctxt->state != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7954 | ctxt->state->seq = NULL; |
| 7955 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
| 7956 | if (ret != 0) { |
| 7957 | ctxt->pstate = -1; |
| 7958 | } |
| 7959 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7960 | } else if (ctxt->states != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7961 | int tmp = -1, i; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7962 | |
| 7963 | oldflags = ctxt->flags; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7964 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7965 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 7966 | state = ctxt->states->tabState[i]; |
| 7967 | ctxt->state = state; |
| 7968 | ctxt->state->seq = NULL; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7969 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7970 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
| 7971 | tmp = 0; |
| 7972 | break; |
| 7973 | } |
| 7974 | } |
| 7975 | if (tmp != 0) { |
| 7976 | /* |
| 7977 | * validation error, log the message for the "best" one |
| 7978 | */ |
| 7979 | ctxt->flags |= FLAGS_IGNORABLE; |
| 7980 | xmlRelaxNGLogBestError(ctxt); |
| 7981 | } |
| 7982 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 7983 | xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[i]); |
| 7984 | } |
| 7985 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 7986 | ctxt->states = NULL; |
| 7987 | if ((ret == 0) && (tmp == -1)) |
| 7988 | ctxt->pstate = -1; |
| 7989 | ctxt->flags = oldflags; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7990 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7991 | if (ctxt->pstate == -1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7992 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 7993 | xmlRelaxNGDumpValidError(ctxt); |
| 7994 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7995 | } |
| 7996 | ctxt->state = oldstate; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7997 | } |
| 7998 | |
| 7999 | /** |
| 8000 | * xmlRelaxNGValidatePushElement: |
| 8001 | * @ctxt: the validation context |
| 8002 | * @doc: a document instance |
| 8003 | * @elem: an element instance |
| 8004 | * |
| 8005 | * Push a new element start on the RelaxNG validation stack. |
| 8006 | * |
| 8007 | * returns 1 if no validation problem was found or 0 if validating the |
| 8008 | * element requires a full node, and -1 in case of error. |
| 8009 | */ |
| 8010 | int |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8011 | xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8012 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8013 | xmlNodePtr elem) |
| 8014 | { |
| 8015 | int ret = 1; |
| 8016 | |
| 8017 | if ((ctxt == NULL) || (elem == NULL)) |
| 8018 | return (-1); |
| 8019 | |
| 8020 | #ifdef DEBUG_PROGRESSIVE |
| 8021 | xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name); |
| 8022 | #endif |
| 8023 | if (ctxt->elem == 0) { |
| 8024 | xmlRelaxNGPtr schema; |
| 8025 | xmlRelaxNGGrammarPtr grammar; |
| 8026 | xmlRegExecCtxtPtr exec; |
| 8027 | xmlRelaxNGDefinePtr define; |
| 8028 | |
| 8029 | schema = ctxt->schema; |
| 8030 | if (schema == NULL) { |
| 8031 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 8032 | return (-1); |
| 8033 | } |
| 8034 | grammar = schema->topgrammar; |
| 8035 | if ((grammar == NULL) || (grammar->start == NULL)) { |
| 8036 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 8037 | return (-1); |
| 8038 | } |
| 8039 | define = grammar->start; |
| 8040 | if (define->contModel == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8041 | ctxt->pdef = define; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8042 | return (0); |
| 8043 | } |
| 8044 | exec = xmlRegNewExecCtxt(define->contModel, |
| 8045 | xmlRelaxNGValidateProgressiveCallback, |
| 8046 | ctxt); |
| 8047 | if (exec == NULL) { |
| 8048 | return (-1); |
| 8049 | } |
| 8050 | xmlRelaxNGElemPush(ctxt, exec); |
| 8051 | } |
| 8052 | ctxt->pnode = elem; |
| 8053 | ctxt->pstate = 0; |
| 8054 | if (elem->ns != NULL) { |
| 8055 | ret = |
| 8056 | xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href, |
| 8057 | ctxt); |
| 8058 | } else { |
| 8059 | ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt); |
| 8060 | } |
| 8061 | if (ret < 0) { |
| 8062 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name); |
| 8063 | } else { |
| 8064 | if (ctxt->pstate == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8065 | ret = 0; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8066 | else if (ctxt->pstate < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8067 | ret = -1; |
| 8068 | else |
| 8069 | ret = 1; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8070 | } |
| 8071 | #ifdef DEBUG_PROGRESSIVE |
| 8072 | if (ret < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8073 | xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n", |
| 8074 | elem->name); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8075 | #endif |
| 8076 | return (ret); |
| 8077 | } |
| 8078 | |
| 8079 | /** |
| 8080 | * xmlRelaxNGValidatePushCData: |
| 8081 | * @ctxt: the RelaxNG validation context |
| 8082 | * @data: some character data read |
| 8083 | * @len: the lenght of the data |
| 8084 | * |
| 8085 | * check the CData parsed for validation in the current stack |
| 8086 | * |
| 8087 | * returns 1 if no validation problem was found or -1 otherwise |
| 8088 | */ |
| 8089 | int |
| 8090 | xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8091 | const xmlChar * data, int len ATTRIBUTE_UNUSED) |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8092 | { |
| 8093 | int ret = 1; |
| 8094 | |
| 8095 | if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL)) |
| 8096 | return (-1); |
| 8097 | |
| 8098 | #ifdef DEBUG_PROGRESSIVE |
| 8099 | xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len); |
| 8100 | #endif |
| 8101 | |
| 8102 | while (*data != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8103 | if (!IS_BLANK_CH(*data)) |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8104 | break; |
| 8105 | data++; |
| 8106 | } |
| 8107 | if (*data == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8108 | return (1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8109 | |
| 8110 | ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt); |
| 8111 | if (ret < 0) { |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8112 | VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO "); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8113 | #ifdef DEBUG_PROGRESSIVE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8114 | xmlGenericError(xmlGenericErrorContext, "CDATA failed\n"); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8115 | #endif |
| 8116 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8117 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8118 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8119 | return (1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8120 | } |
| 8121 | |
| 8122 | /** |
| 8123 | * xmlRelaxNGValidatePopElement: |
| 8124 | * @ctxt: the RelaxNG validation context |
| 8125 | * @doc: a document instance |
| 8126 | * @elem: an element instance |
| 8127 | * |
| 8128 | * Pop the element end from the RelaxNG validation stack. |
| 8129 | * |
| 8130 | * returns 1 if no validation problem was found or 0 otherwise |
| 8131 | */ |
| 8132 | int |
| 8133 | xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8134 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8135 | xmlNodePtr elem) |
| 8136 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8137 | int ret; |
| 8138 | xmlRegExecCtxtPtr exec; |
| 8139 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8140 | if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL)) |
| 8141 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8142 | #ifdef DEBUG_PROGRESSIVE |
| 8143 | xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name); |
| 8144 | #endif |
| 8145 | /* |
| 8146 | * verify that we reached a terminal state of the content model. |
| 8147 | */ |
| 8148 | exec = xmlRelaxNGElemPop(ctxt); |
| 8149 | ret = xmlRegExecPushString(exec, NULL, NULL); |
| 8150 | if (ret == 0) { |
| 8151 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8152 | * TODO: get some of the names needed to exit the current state of exec |
| 8153 | */ |
| 8154 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST ""); |
| 8155 | ret = -1; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8156 | } else if (ret < 0) { |
| 8157 | ret = -1; |
| 8158 | } else { |
| 8159 | ret = 1; |
| 8160 | } |
| 8161 | xmlRegFreeExecCtxt(exec); |
| 8162 | #ifdef DEBUG_PROGRESSIVE |
| 8163 | if (ret < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8164 | xmlGenericError(xmlGenericErrorContext, "PopElem %s failed\n", |
| 8165 | elem->name); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8166 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8167 | return (ret); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8168 | } |
| 8169 | |
| 8170 | /** |
| 8171 | * xmlRelaxNGValidateFullElement: |
| 8172 | * @ctxt: the validation context |
| 8173 | * @doc: a document instance |
| 8174 | * @elem: an element instance |
| 8175 | * |
| 8176 | * Validate a full subtree when xmlRelaxNGValidatePushElement() returned |
| 8177 | * 0 and the content of the node has been expanded. |
| 8178 | * |
| 8179 | * returns 1 if no validation problem was found or -1 in case of error. |
| 8180 | */ |
| 8181 | int |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8182 | xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8183 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8184 | xmlNodePtr elem) |
| 8185 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8186 | int ret; |
| 8187 | xmlRelaxNGValidStatePtr state; |
| 8188 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8189 | if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL)) |
| 8190 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8191 | #ifdef DEBUG_PROGRESSIVE |
| 8192 | xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name); |
| 8193 | #endif |
| 8194 | state = xmlRelaxNGNewValidState(ctxt, elem->parent); |
| 8195 | if (state == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8196 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8197 | } |
| 8198 | state->seq = elem; |
| 8199 | ctxt->state = state; |
| 8200 | ctxt->errNo = XML_RELAXNG_OK; |
| 8201 | ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8202 | if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK)) |
| 8203 | ret = -1; |
| 8204 | else |
| 8205 | ret = 1; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8206 | xmlRelaxNGFreeValidState(ctxt, state); |
| 8207 | ctxt->state = NULL; |
| 8208 | #ifdef DEBUG_PROGRESSIVE |
| 8209 | if (ret < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8210 | xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n", |
| 8211 | elem->name); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8212 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8213 | return (ret); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8214 | } |
| 8215 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 8216 | /************************************************************************ |
| 8217 | * * |
| 8218 | * Generic interpreted validation implementation * |
| 8219 | * * |
| 8220 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8221 | static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
| 8222 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8223 | |
| 8224 | /** |
| 8225 | * xmlRelaxNGSkipIgnored: |
| 8226 | * @ctxt: a schema validation context |
| 8227 | * @node: the top node. |
| 8228 | * |
| 8229 | * Skip ignorable nodes in that context |
| 8230 | * |
| 8231 | * Returns the new sibling or NULL in case of error. |
| 8232 | */ |
| 8233 | static xmlNodePtr |
| 8234 | xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8235 | xmlNodePtr node) |
| 8236 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8237 | /* |
| 8238 | * TODO complete and handle entities |
| 8239 | */ |
| 8240 | while ((node != NULL) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8241 | ((node->type == XML_COMMENT_NODE) || |
| 8242 | (node->type == XML_PI_NODE) || |
| 8243 | (((node->type == XML_TEXT_NODE) || |
| 8244 | (node->type == XML_CDATA_SECTION_NODE)) && |
| 8245 | ((ctxt->flags & FLAGS_MIXED_CONTENT) || |
| 8246 | (IS_BLANK_NODE(node)))))) { |
| 8247 | node = node->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8248 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8249 | return (node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8250 | } |
| 8251 | |
| 8252 | /** |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8253 | * xmlRelaxNGNormalize: |
| 8254 | * @ctxt: a schema validation context |
| 8255 | * @str: the string to normalize |
| 8256 | * |
| 8257 | * Implements the normalizeWhiteSpace( s ) function from |
| 8258 | * section 6.2.9 of the spec |
| 8259 | * |
| 8260 | * Returns the new string or NULL in case of error. |
| 8261 | */ |
| 8262 | static xmlChar * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8263 | xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar * str) |
| 8264 | { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8265 | xmlChar *ret, *p; |
| 8266 | const xmlChar *tmp; |
| 8267 | int len; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8268 | |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8269 | if (str == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8270 | return (NULL); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8271 | tmp = str; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8272 | while (*tmp != 0) |
| 8273 | tmp++; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8274 | len = tmp - str; |
| 8275 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 8276 | ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar)); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8277 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8278 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 8279 | return (NULL); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8280 | } |
| 8281 | p = ret; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8282 | while (IS_BLANK_CH(*str)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8283 | str++; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8284 | while (*str != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8285 | if (IS_BLANK_CH(*str)) { |
| 8286 | while (IS_BLANK_CH(*str)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8287 | str++; |
| 8288 | if (*str == 0) |
| 8289 | break; |
| 8290 | *p++ = ' '; |
| 8291 | } else |
| 8292 | *p++ = *str++; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8293 | } |
| 8294 | *p = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8295 | return (ret); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8296 | } |
| 8297 | |
| 8298 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8299 | * xmlRelaxNGValidateDatatype: |
| 8300 | * @ctxt: a Relax-NG validation context |
| 8301 | * @value: the string value |
| 8302 | * @type: the datatype definition |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8303 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8304 | * |
| 8305 | * Validate the given value against the dataype |
| 8306 | * |
| 8307 | * Returns 0 if the validation succeeded or an error code. |
| 8308 | */ |
| 8309 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8310 | xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt, |
| 8311 | const xmlChar * value, |
| 8312 | xmlRelaxNGDefinePtr define, xmlNodePtr node) |
| 8313 | { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8314 | int ret, tmp; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8315 | xmlRelaxNGTypeLibraryPtr lib; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8316 | void *result = NULL; |
| 8317 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8318 | |
| 8319 | if ((define == NULL) || (define->data == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8320 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8321 | } |
| 8322 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8323 | if (lib->check != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8324 | if ((define->attrs != NULL) && |
| 8325 | (define->attrs->type == XML_RELAXNG_PARAM)) { |
| 8326 | ret = |
| 8327 | lib->check(lib->data, define->name, value, &result, node); |
| 8328 | } else { |
| 8329 | ret = lib->check(lib->data, define->name, value, NULL, node); |
| 8330 | } |
| 8331 | } else |
| 8332 | ret = -1; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8333 | if (ret < 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8334 | VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name); |
| 8335 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
| 8336 | lib->freef(lib->data, result); |
| 8337 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8338 | } else if (ret == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8339 | ret = 0; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8340 | } else if (ret == 2) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8341 | VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8342 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8343 | VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value); |
| 8344 | ret = -1; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8345 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8346 | cur = define->attrs; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8347 | while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8348 | if (lib->facet != NULL) { |
| 8349 | tmp = lib->facet(lib->data, define->name, cur->name, |
| 8350 | cur->value, value, result); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8351 | if (tmp != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8352 | ret = -1; |
| 8353 | } |
| 8354 | cur = cur->next; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8355 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8356 | if ((ret == 0) && (define->content != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8357 | const xmlChar *oldvalue, *oldendvalue; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8358 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8359 | oldvalue = ctxt->state->value; |
| 8360 | oldendvalue = ctxt->state->endvalue; |
| 8361 | ctxt->state->value = (xmlChar *) value; |
| 8362 | ctxt->state->endvalue = NULL; |
| 8363 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 8364 | ctxt->state->value = (xmlChar *) oldvalue; |
| 8365 | ctxt->state->endvalue = (xmlChar *) oldendvalue; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8366 | } |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8367 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8368 | lib->freef(lib->data, result); |
| 8369 | return (ret); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8370 | } |
| 8371 | |
| 8372 | /** |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8373 | * xmlRelaxNGNextValue: |
| 8374 | * @ctxt: a Relax-NG validation context |
| 8375 | * |
| 8376 | * Skip to the next value when validating within a list |
| 8377 | * |
| 8378 | * Returns 0 if the operation succeeded or an error code. |
| 8379 | */ |
| 8380 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8381 | xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt) |
| 8382 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8383 | xmlChar *cur; |
| 8384 | |
| 8385 | cur = ctxt->state->value; |
| 8386 | if ((cur == NULL) || (ctxt->state->endvalue == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8387 | ctxt->state->value = NULL; |
| 8388 | ctxt->state->endvalue = NULL; |
| 8389 | return (0); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8390 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8391 | while (*cur != 0) |
| 8392 | cur++; |
| 8393 | while ((cur != ctxt->state->endvalue) && (*cur == 0)) |
| 8394 | cur++; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8395 | if (cur == ctxt->state->endvalue) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8396 | ctxt->state->value = NULL; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8397 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8398 | ctxt->state->value = cur; |
| 8399 | return (0); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8400 | } |
| 8401 | |
| 8402 | /** |
| 8403 | * xmlRelaxNGValidateValueList: |
| 8404 | * @ctxt: a Relax-NG validation context |
| 8405 | * @defines: the list of definitions to verify |
| 8406 | * |
| 8407 | * Validate the given set of definitions for the current value |
| 8408 | * |
| 8409 | * Returns 0 if the validation succeeded or an error code. |
| 8410 | */ |
| 8411 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8412 | xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt, |
| 8413 | xmlRelaxNGDefinePtr defines) |
| 8414 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8415 | int ret = 0; |
| 8416 | |
| 8417 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8418 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
| 8419 | if (ret != 0) |
| 8420 | break; |
| 8421 | defines = defines->next; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8422 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8423 | return (ret); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8424 | } |
| 8425 | |
| 8426 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8427 | * xmlRelaxNGValidateValue: |
| 8428 | * @ctxt: a Relax-NG validation context |
| 8429 | * @define: the definition to verify |
| 8430 | * |
| 8431 | * Validate the given definition for the current value |
| 8432 | * |
| 8433 | * Returns 0 if the validation succeeded or an error code. |
| 8434 | */ |
| 8435 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8436 | xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
| 8437 | xmlRelaxNGDefinePtr define) |
| 8438 | { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8439 | int ret = 0, oldflags; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8440 | xmlChar *value; |
| 8441 | |
| 8442 | value = ctxt->state->value; |
| 8443 | switch (define->type) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8444 | case XML_RELAXNG_EMPTY:{ |
| 8445 | if ((value != NULL) && (value[0] != 0)) { |
| 8446 | int idx = 0; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 8447 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8448 | while (IS_BLANK_CH(value[idx])) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8449 | idx++; |
| 8450 | if (value[idx] != 0) |
| 8451 | ret = -1; |
| 8452 | } |
| 8453 | break; |
| 8454 | } |
| 8455 | case XML_RELAXNG_TEXT: |
| 8456 | break; |
| 8457 | case XML_RELAXNG_VALUE:{ |
| 8458 | if (!xmlStrEqual(value, define->value)) { |
| 8459 | if (define->name != NULL) { |
| 8460 | xmlRelaxNGTypeLibraryPtr lib; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8461 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8462 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
| 8463 | if ((lib != NULL) && (lib->comp != NULL)) { |
| 8464 | ret = lib->comp(lib->data, define->name, |
| 8465 | define->value, define->node, |
| 8466 | (void *) define->attrs, |
| 8467 | value, ctxt->state->node); |
| 8468 | } else |
| 8469 | ret = -1; |
| 8470 | if (ret < 0) { |
| 8471 | VALID_ERR2(XML_RELAXNG_ERR_TYPECMP, |
| 8472 | define->name); |
| 8473 | return (-1); |
| 8474 | } else if (ret == 1) { |
| 8475 | ret = 0; |
| 8476 | } else { |
| 8477 | ret = -1; |
| 8478 | } |
| 8479 | } else { |
| 8480 | xmlChar *nval, *nvalue; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8481 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8482 | /* |
| 8483 | * TODO: trivial optimizations are possible by |
| 8484 | * computing at compile-time |
| 8485 | */ |
| 8486 | nval = xmlRelaxNGNormalize(ctxt, define->value); |
| 8487 | nvalue = xmlRelaxNGNormalize(ctxt, value); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8488 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8489 | if ((nval == NULL) || (nvalue == NULL) || |
| 8490 | (!xmlStrEqual(nval, nvalue))) |
| 8491 | ret = -1; |
| 8492 | if (nval != NULL) |
| 8493 | xmlFree(nval); |
| 8494 | if (nvalue != NULL) |
| 8495 | xmlFree(nvalue); |
| 8496 | } |
| 8497 | } |
| 8498 | if (ret == 0) |
| 8499 | xmlRelaxNGNextValue(ctxt); |
| 8500 | break; |
| 8501 | } |
| 8502 | case XML_RELAXNG_DATATYPE:{ |
| 8503 | ret = xmlRelaxNGValidateDatatype(ctxt, value, define, |
| 8504 | ctxt->state->seq); |
| 8505 | if (ret == 0) |
| 8506 | xmlRelaxNGNextValue(ctxt); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8507 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8508 | break; |
| 8509 | } |
| 8510 | case XML_RELAXNG_CHOICE:{ |
| 8511 | xmlRelaxNGDefinePtr list = define->content; |
| 8512 | xmlChar *oldvalue; |
| 8513 | |
| 8514 | oldflags = ctxt->flags; |
| 8515 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8516 | |
| 8517 | oldvalue = ctxt->state->value; |
| 8518 | while (list != NULL) { |
| 8519 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8520 | if (ret == 0) { |
| 8521 | break; |
| 8522 | } |
| 8523 | ctxt->state->value = oldvalue; |
| 8524 | list = list->next; |
| 8525 | } |
| 8526 | ctxt->flags = oldflags; |
| 8527 | if (ret != 0) { |
| 8528 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8529 | xmlRelaxNGDumpValidError(ctxt); |
| 8530 | } else { |
| 8531 | if (ctxt->errNr > 0) |
| 8532 | xmlRelaxNGPopErrors(ctxt, 0); |
| 8533 | } |
| 8534 | if (ret == 0) |
| 8535 | xmlRelaxNGNextValue(ctxt); |
| 8536 | break; |
| 8537 | } |
| 8538 | case XML_RELAXNG_LIST:{ |
| 8539 | xmlRelaxNGDefinePtr list = define->content; |
| 8540 | xmlChar *oldvalue, *oldend, *val, *cur; |
| 8541 | |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8542 | #ifdef DEBUG_LIST |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8543 | int nb_values = 0; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8544 | #endif |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8545 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8546 | oldvalue = ctxt->state->value; |
| 8547 | oldend = ctxt->state->endvalue; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8548 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8549 | val = xmlStrdup(oldvalue); |
| 8550 | if (val == NULL) { |
| 8551 | val = xmlStrdup(BAD_CAST ""); |
| 8552 | } |
| 8553 | if (val == NULL) { |
| 8554 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 8555 | return (-1); |
| 8556 | } |
| 8557 | cur = val; |
| 8558 | while (*cur != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8559 | if (IS_BLANK_CH(*cur)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8560 | *cur = 0; |
| 8561 | cur++; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8562 | #ifdef DEBUG_LIST |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8563 | nb_values++; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8564 | #endif |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8565 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8566 | *cur++ = 0; |
| 8567 | } else |
| 8568 | cur++; |
| 8569 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8570 | #ifdef DEBUG_LIST |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8571 | xmlGenericError(xmlGenericErrorContext, |
| 8572 | "list value: '%s' found %d items\n", |
| 8573 | oldvalue, nb_values); |
| 8574 | nb_values = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8575 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8576 | ctxt->state->endvalue = cur; |
| 8577 | cur = val; |
| 8578 | while ((*cur == 0) && (cur != ctxt->state->endvalue)) |
| 8579 | cur++; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8580 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8581 | ctxt->state->value = cur; |
| 8582 | |
| 8583 | while (list != NULL) { |
| 8584 | if (ctxt->state->value == ctxt->state->endvalue) |
| 8585 | ctxt->state->value = NULL; |
| 8586 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8587 | if (ret != 0) { |
| 8588 | #ifdef DEBUG_LIST |
| 8589 | xmlGenericError(xmlGenericErrorContext, |
| 8590 | "Failed to validate value: '%s' with %d rule\n", |
| 8591 | ctxt->state->value, nb_values); |
| 8592 | #endif |
| 8593 | break; |
| 8594 | } |
| 8595 | #ifdef DEBUG_LIST |
| 8596 | nb_values++; |
| 8597 | #endif |
| 8598 | list = list->next; |
| 8599 | } |
| 8600 | |
| 8601 | if ((ret == 0) && (ctxt->state->value != NULL) && |
| 8602 | (ctxt->state->value != ctxt->state->endvalue)) { |
| 8603 | VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA, |
| 8604 | ctxt->state->value); |
| 8605 | ret = -1; |
| 8606 | } |
| 8607 | xmlFree(val); |
| 8608 | ctxt->state->value = oldvalue; |
| 8609 | ctxt->state->endvalue = oldend; |
| 8610 | break; |
| 8611 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8612 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8613 | ret = xmlRelaxNGValidateValueList(ctxt, define->content); |
| 8614 | if (ret != 0) { |
| 8615 | break; |
| 8616 | } |
| 8617 | /* no break on purpose */ |
| 8618 | case XML_RELAXNG_ZEROORMORE:{ |
| 8619 | xmlChar *cur, *temp; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8620 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8621 | oldflags = ctxt->flags; |
| 8622 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8623 | cur = ctxt->state->value; |
| 8624 | temp = NULL; |
| 8625 | while ((cur != NULL) && (cur != ctxt->state->endvalue) && |
| 8626 | (temp != cur)) { |
| 8627 | temp = cur; |
| 8628 | ret = |
| 8629 | xmlRelaxNGValidateValueList(ctxt, define->content); |
| 8630 | if (ret != 0) { |
| 8631 | ctxt->state->value = temp; |
| 8632 | ret = 0; |
| 8633 | break; |
| 8634 | } |
| 8635 | cur = ctxt->state->value; |
| 8636 | } |
| 8637 | ctxt->flags = oldflags; |
| 8638 | if (ret != 0) { |
| 8639 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8640 | xmlRelaxNGDumpValidError(ctxt); |
| 8641 | } else { |
| 8642 | if (ctxt->errNr > 0) |
| 8643 | xmlRelaxNGPopErrors(ctxt, 0); |
| 8644 | } |
| 8645 | break; |
| 8646 | } |
| 8647 | case XML_RELAXNG_EXCEPT:{ |
| 8648 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8649 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8650 | list = define->content; |
| 8651 | while (list != NULL) { |
| 8652 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8653 | if (ret == 0) { |
| 8654 | ret = -1; |
| 8655 | break; |
| 8656 | } else |
| 8657 | ret = 0; |
| 8658 | list = list->next; |
| 8659 | } |
| 8660 | break; |
| 8661 | } |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 8662 | case XML_RELAXNG_DEF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8663 | case XML_RELAXNG_GROUP:{ |
| 8664 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8665 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8666 | list = define->content; |
| 8667 | while (list != NULL) { |
| 8668 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8669 | if (ret != 0) { |
| 8670 | ret = -1; |
| 8671 | break; |
| 8672 | } else |
| 8673 | ret = 0; |
| 8674 | list = list->next; |
| 8675 | } |
| 8676 | break; |
| 8677 | } |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 8678 | case XML_RELAXNG_REF: |
| 8679 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8680 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 8681 | break; |
| 8682 | default: |
| 8683 | TODO ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8684 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8685 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8686 | } |
| 8687 | |
| 8688 | /** |
| 8689 | * xmlRelaxNGValidateValueContent: |
| 8690 | * @ctxt: a Relax-NG validation context |
| 8691 | * @defines: the list of definitions to verify |
| 8692 | * |
| 8693 | * Validate the given definitions for the current value |
| 8694 | * |
| 8695 | * Returns 0 if the validation succeeded or an error code. |
| 8696 | */ |
| 8697 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8698 | xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt, |
| 8699 | xmlRelaxNGDefinePtr defines) |
| 8700 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8701 | int ret = 0; |
| 8702 | |
| 8703 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8704 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
| 8705 | if (ret != 0) |
| 8706 | break; |
| 8707 | defines = defines->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8708 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8709 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8710 | } |
| 8711 | |
| 8712 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8713 | * xmlRelaxNGAttributeMatch: |
| 8714 | * @ctxt: a Relax-NG validation context |
| 8715 | * @define: the definition to check |
| 8716 | * @prop: the attribute |
| 8717 | * |
| 8718 | * Check if the attribute matches the definition nameClass |
| 8719 | * |
| 8720 | * Returns 1 if the attribute matches, 0 if no, or -1 in case of error |
| 8721 | */ |
| 8722 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8723 | xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 8724 | xmlRelaxNGDefinePtr define, xmlAttrPtr prop) |
| 8725 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8726 | int ret; |
| 8727 | |
| 8728 | if (define->name != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8729 | if (!xmlStrEqual(define->name, prop->name)) |
| 8730 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8731 | } |
| 8732 | if (define->ns != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8733 | if (define->ns[0] == 0) { |
| 8734 | if (prop->ns != NULL) |
| 8735 | return (0); |
| 8736 | } else { |
| 8737 | if ((prop->ns == NULL) || |
| 8738 | (!xmlStrEqual(define->ns, prop->ns->href))) |
| 8739 | return (0); |
| 8740 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8741 | } |
| 8742 | if (define->nameClass == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8743 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8744 | define = define->nameClass; |
| 8745 | if (define->type == XML_RELAXNG_EXCEPT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8746 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8747 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8748 | list = define->content; |
| 8749 | while (list != NULL) { |
| 8750 | ret = xmlRelaxNGAttributeMatch(ctxt, list, prop); |
| 8751 | if (ret == 1) |
| 8752 | return (0); |
| 8753 | if (ret < 0) |
| 8754 | return (ret); |
| 8755 | list = list->next; |
| 8756 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8757 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8758 | TODO} |
| 8759 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8760 | } |
| 8761 | |
| 8762 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8763 | * xmlRelaxNGValidateAttribute: |
| 8764 | * @ctxt: a Relax-NG validation context |
| 8765 | * @define: the definition to verify |
| 8766 | * |
| 8767 | * Validate the given attribute definition for that node |
| 8768 | * |
| 8769 | * Returns 0 if the validation succeeded or an error code. |
| 8770 | */ |
| 8771 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8772 | xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt, |
| 8773 | xmlRelaxNGDefinePtr define) |
| 8774 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8775 | int ret = 0, i; |
| 8776 | xmlChar *value, *oldvalue; |
| 8777 | xmlAttrPtr prop = NULL, tmp; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8778 | xmlNodePtr oldseq; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8779 | |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 8780 | if (ctxt->state->nbAttrLeft <= 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8781 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8782 | if (define->name != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8783 | for (i = 0; i < ctxt->state->nbAttrs; i++) { |
| 8784 | tmp = ctxt->state->attrs[i]; |
| 8785 | if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) { |
| 8786 | if ((((define->ns == NULL) || (define->ns[0] == 0)) && |
| 8787 | (tmp->ns == NULL)) || |
| 8788 | ((tmp->ns != NULL) && |
| 8789 | (xmlStrEqual(define->ns, tmp->ns->href)))) { |
| 8790 | prop = tmp; |
| 8791 | break; |
| 8792 | } |
| 8793 | } |
| 8794 | } |
| 8795 | if (prop != NULL) { |
| 8796 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 8797 | oldvalue = ctxt->state->value; |
| 8798 | oldseq = ctxt->state->seq; |
| 8799 | ctxt->state->seq = (xmlNodePtr) prop; |
| 8800 | ctxt->state->value = value; |
| 8801 | ctxt->state->endvalue = NULL; |
| 8802 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
| 8803 | if (ctxt->state->value != NULL) |
| 8804 | value = ctxt->state->value; |
| 8805 | if (value != NULL) |
| 8806 | xmlFree(value); |
| 8807 | ctxt->state->value = oldvalue; |
| 8808 | ctxt->state->seq = oldseq; |
| 8809 | if (ret == 0) { |
| 8810 | /* |
| 8811 | * flag the attribute as processed |
| 8812 | */ |
| 8813 | ctxt->state->attrs[i] = NULL; |
| 8814 | ctxt->state->nbAttrLeft--; |
| 8815 | } |
| 8816 | } else { |
| 8817 | ret = -1; |
| 8818 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8819 | #ifdef DEBUG |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8820 | xmlGenericError(xmlGenericErrorContext, |
| 8821 | "xmlRelaxNGValidateAttribute(%s): %d\n", |
| 8822 | define->name, ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8823 | #endif |
| 8824 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8825 | for (i = 0; i < ctxt->state->nbAttrs; i++) { |
| 8826 | tmp = ctxt->state->attrs[i]; |
| 8827 | if ((tmp != NULL) && |
| 8828 | (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) { |
| 8829 | prop = tmp; |
| 8830 | break; |
| 8831 | } |
| 8832 | } |
| 8833 | if (prop != NULL) { |
| 8834 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 8835 | oldvalue = ctxt->state->value; |
| 8836 | oldseq = ctxt->state->seq; |
| 8837 | ctxt->state->seq = (xmlNodePtr) prop; |
| 8838 | ctxt->state->value = value; |
| 8839 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
| 8840 | if (ctxt->state->value != NULL) |
| 8841 | value = ctxt->state->value; |
| 8842 | if (value != NULL) |
| 8843 | xmlFree(value); |
| 8844 | ctxt->state->value = oldvalue; |
| 8845 | ctxt->state->seq = oldseq; |
| 8846 | if (ret == 0) { |
| 8847 | /* |
| 8848 | * flag the attribute as processed |
| 8849 | */ |
| 8850 | ctxt->state->attrs[i] = NULL; |
| 8851 | ctxt->state->nbAttrLeft--; |
| 8852 | } |
| 8853 | } else { |
| 8854 | ret = -1; |
| 8855 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8856 | #ifdef DEBUG |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8857 | if (define->ns != NULL) { |
| 8858 | xmlGenericError(xmlGenericErrorContext, |
| 8859 | "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n", |
| 8860 | define->ns, ret); |
| 8861 | } else { |
| 8862 | xmlGenericError(xmlGenericErrorContext, |
| 8863 | "xmlRelaxNGValidateAttribute(anyName): %d\n", |
| 8864 | ret); |
| 8865 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8866 | #endif |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8867 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8868 | |
| 8869 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8870 | } |
| 8871 | |
| 8872 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8873 | * xmlRelaxNGValidateAttributeList: |
| 8874 | * @ctxt: a Relax-NG validation context |
| 8875 | * @define: the list of definition to verify |
| 8876 | * |
| 8877 | * Validate the given node against the list of attribute definitions |
| 8878 | * |
| 8879 | * Returns 0 if the validation succeeded or an error code. |
| 8880 | */ |
| 8881 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8882 | xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
| 8883 | xmlRelaxNGDefinePtr defines) |
| 8884 | { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8885 | int ret = 0, res; |
| 8886 | int needmore = 0; |
| 8887 | xmlRelaxNGDefinePtr cur; |
| 8888 | |
| 8889 | cur = defines; |
| 8890 | while (cur != NULL) { |
| 8891 | if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8892 | if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0) |
| 8893 | ret = -1; |
| 8894 | } else |
| 8895 | needmore = 1; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8896 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8897 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8898 | if (!needmore) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8899 | return (ret); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8900 | cur = defines; |
| 8901 | while (cur != NULL) { |
| 8902 | if (cur->type != XML_RELAXNG_ATTRIBUTE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8903 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
| 8904 | res = xmlRelaxNGValidateDefinition(ctxt, cur); |
| 8905 | if (res < 0) |
| 8906 | ret = -1; |
| 8907 | } else { |
| 8908 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 8909 | return (-1); |
| 8910 | } |
| 8911 | if (res == -1) /* continues on -2 */ |
| 8912 | break; |
| 8913 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8914 | cur = cur->next; |
| 8915 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8916 | |
| 8917 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8918 | } |
| 8919 | |
| 8920 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8921 | * xmlRelaxNGNodeMatchesList: |
| 8922 | * @node: the node |
| 8923 | * @list: a NULL terminated array of definitions |
| 8924 | * |
| 8925 | * Check if a node can be matched by one of the definitions |
| 8926 | * |
| 8927 | * Returns 1 if matches 0 otherwise |
| 8928 | */ |
| 8929 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8930 | xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr * list) |
| 8931 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8932 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8933 | int i = 0, tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8934 | |
| 8935 | if ((node == NULL) || (list == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8936 | return (0); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8937 | |
| 8938 | cur = list[i++]; |
| 8939 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8940 | if ((node->type == XML_ELEMENT_NODE) && |
| 8941 | (cur->type == XML_RELAXNG_ELEMENT)) { |
| 8942 | tmp = xmlRelaxNGElementMatch(NULL, cur, node); |
| 8943 | if (tmp == 1) |
| 8944 | return (1); |
| 8945 | } else if (((node->type == XML_TEXT_NODE) || |
| 8946 | (node->type == XML_CDATA_SECTION_NODE)) && |
| 8947 | (cur->type == XML_RELAXNG_TEXT)) { |
| 8948 | return (1); |
| 8949 | } |
| 8950 | cur = list[i++]; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8951 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8952 | return (0); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8953 | } |
| 8954 | |
| 8955 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8956 | * xmlRelaxNGValidateInterleave: |
| 8957 | * @ctxt: a Relax-NG validation context |
| 8958 | * @define: the definition to verify |
| 8959 | * |
| 8960 | * Validate an interleave definition for a node. |
| 8961 | * |
| 8962 | * Returns 0 if the validation succeeded or an error code. |
| 8963 | */ |
| 8964 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8965 | xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt, |
| 8966 | xmlRelaxNGDefinePtr define) |
| 8967 | { |
William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 8968 | int ret = 0, i, nbgroups; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8969 | int errNr = ctxt->errNr; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8970 | int oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8971 | |
| 8972 | xmlRelaxNGValidStatePtr oldstate; |
| 8973 | xmlRelaxNGPartitionPtr partitions; |
| 8974 | xmlRelaxNGInterleaveGroupPtr group = NULL; |
| 8975 | xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem; |
| 8976 | xmlNodePtr *list = NULL, *lasts = NULL; |
| 8977 | |
| 8978 | if (define->data != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8979 | partitions = (xmlRelaxNGPartitionPtr) define->data; |
| 8980 | nbgroups = partitions->nbgroups; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8981 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8982 | VALID_ERR(XML_RELAXNG_ERR_INTERNODATA); |
| 8983 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8984 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8985 | /* |
| 8986 | * Optimizations for MIXED |
| 8987 | */ |
| 8988 | oldflags = ctxt->flags; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 8989 | if (define->dflags & IS_MIXED) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8990 | ctxt->flags |= FLAGS_MIXED_CONTENT; |
| 8991 | if (nbgroups == 2) { |
| 8992 | /* |
| 8993 | * this is a pure <mixed> case |
| 8994 | */ |
| 8995 | if (ctxt->state != NULL) |
| 8996 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
| 8997 | ctxt->state->seq); |
| 8998 | if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT) |
| 8999 | ret = xmlRelaxNGValidateDefinition(ctxt, |
| 9000 | partitions->groups[1]-> |
| 9001 | rule); |
| 9002 | else |
| 9003 | ret = xmlRelaxNGValidateDefinition(ctxt, |
| 9004 | partitions->groups[0]-> |
| 9005 | rule); |
| 9006 | if (ret == 0) { |
| 9007 | if (ctxt->state != NULL) |
| 9008 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
| 9009 | ctxt->state-> |
| 9010 | seq); |
| 9011 | } |
| 9012 | ctxt->flags = oldflags; |
| 9013 | return (ret); |
| 9014 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9015 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9016 | |
| 9017 | /* |
| 9018 | * Build arrays to store the first and last node of the chain |
| 9019 | * pertaining to each group |
| 9020 | */ |
| 9021 | list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
| 9022 | if (list == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9023 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 9024 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9025 | } |
| 9026 | memset(list, 0, nbgroups * sizeof(xmlNodePtr)); |
| 9027 | lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
| 9028 | if (lasts == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9029 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 9030 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9031 | } |
| 9032 | memset(lasts, 0, nbgroups * sizeof(xmlNodePtr)); |
| 9033 | |
| 9034 | /* |
| 9035 | * Walk the sequence of children finding the right group and |
| 9036 | * sorting them in sequences. |
| 9037 | */ |
| 9038 | cur = ctxt->state->seq; |
| 9039 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 9040 | start = cur; |
| 9041 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9042 | ctxt->state->seq = cur; |
| 9043 | if ((partitions->triage != NULL) && |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9044 | (partitions->flags & IS_DETERMINIST)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9045 | void *tmp = NULL; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9046 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9047 | if ((cur->type == XML_TEXT_NODE) || |
| 9048 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 9049 | tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text", |
| 9050 | NULL); |
| 9051 | } else if (cur->type == XML_ELEMENT_NODE) { |
| 9052 | if (cur->ns != NULL) { |
| 9053 | tmp = xmlHashLookup2(partitions->triage, cur->name, |
| 9054 | cur->ns->href); |
| 9055 | if (tmp == NULL) |
| 9056 | tmp = xmlHashLookup2(partitions->triage, |
| 9057 | BAD_CAST "#any", |
| 9058 | cur->ns->href); |
| 9059 | } else |
| 9060 | tmp = |
| 9061 | xmlHashLookup2(partitions->triage, cur->name, |
| 9062 | NULL); |
| 9063 | if (tmp == NULL) |
| 9064 | tmp = |
| 9065 | xmlHashLookup2(partitions->triage, BAD_CAST "#any", |
| 9066 | NULL); |
| 9067 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9068 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9069 | if (tmp == NULL) { |
| 9070 | i = nbgroups; |
| 9071 | } else { |
| 9072 | i = ((long) tmp) - 1; |
| 9073 | if (partitions->flags & IS_NEEDCHECK) { |
| 9074 | group = partitions->groups[i]; |
| 9075 | if (!xmlRelaxNGNodeMatchesList(cur, group->defs)) |
| 9076 | i = nbgroups; |
| 9077 | } |
| 9078 | } |
| 9079 | } else { |
| 9080 | for (i = 0; i < nbgroups; i++) { |
| 9081 | group = partitions->groups[i]; |
| 9082 | if (group == NULL) |
| 9083 | continue; |
| 9084 | if (xmlRelaxNGNodeMatchesList(cur, group->defs)) |
| 9085 | break; |
| 9086 | } |
| 9087 | } |
| 9088 | /* |
| 9089 | * We break as soon as an element not matched is found |
| 9090 | */ |
| 9091 | if (i >= nbgroups) { |
| 9092 | break; |
| 9093 | } |
| 9094 | if (lasts[i] != NULL) { |
| 9095 | lasts[i]->next = cur; |
| 9096 | lasts[i] = cur; |
| 9097 | } else { |
| 9098 | list[i] = cur; |
| 9099 | lasts[i] = cur; |
| 9100 | } |
| 9101 | if (cur->next != NULL) |
| 9102 | lastchg = cur->next; |
| 9103 | else |
| 9104 | lastchg = cur; |
| 9105 | cur = xmlRelaxNGSkipIgnored(ctxt, cur->next); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9106 | } |
| 9107 | if (ret != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9108 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
| 9109 | ret = -1; |
| 9110 | goto done; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9111 | } |
| 9112 | lastelem = cur; |
| 9113 | oldstate = ctxt->state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9114 | for (i = 0; i < nbgroups; i++) { |
| 9115 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate); |
| 9116 | group = partitions->groups[i]; |
| 9117 | if (lasts[i] != NULL) { |
| 9118 | last = lasts[i]->next; |
| 9119 | lasts[i]->next = NULL; |
| 9120 | } |
| 9121 | ctxt->state->seq = list[i]; |
| 9122 | ret = xmlRelaxNGValidateDefinition(ctxt, group->rule); |
| 9123 | if (ret != 0) |
| 9124 | break; |
| 9125 | if (ctxt->state != NULL) { |
| 9126 | cur = ctxt->state->seq; |
| 9127 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 9128 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 9129 | oldstate = ctxt->state; |
| 9130 | ctxt->state = NULL; |
| 9131 | if (cur != NULL) { |
| 9132 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
| 9133 | ret = -1; |
| 9134 | ctxt->state = oldstate; |
| 9135 | goto done; |
| 9136 | } |
| 9137 | } else if (ctxt->states != NULL) { |
| 9138 | int j; |
| 9139 | int found = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9140 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9141 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 9142 | cur = ctxt->states->tabState[j]->seq; |
| 9143 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 9144 | if (cur == NULL) { |
| 9145 | found = 1; |
| 9146 | break; |
| 9147 | } |
| 9148 | } |
| 9149 | if (ctxt->states->nbState > 0) { |
| 9150 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 9151 | oldstate = |
| 9152 | ctxt->states->tabState[ctxt->states->nbState - 1]; |
| 9153 | } |
| 9154 | for (j = 0; j < ctxt->states->nbState - 1; j++) { |
| 9155 | xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[j]); |
| 9156 | } |
| 9157 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9158 | ctxt->states = NULL; |
| 9159 | if (found == 0) { |
| 9160 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
| 9161 | ret = -1; |
| 9162 | ctxt->state = oldstate; |
| 9163 | goto done; |
| 9164 | } |
| 9165 | } else { |
| 9166 | ret = -1; |
| 9167 | break; |
| 9168 | } |
| 9169 | if (lasts[i] != NULL) { |
| 9170 | lasts[i]->next = last; |
| 9171 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9172 | } |
| 9173 | if (ctxt->state != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9174 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9175 | ctxt->state = oldstate; |
| 9176 | ctxt->state->seq = lastelem; |
| 9177 | if (ret != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9178 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
| 9179 | ret = -1; |
| 9180 | goto done; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9181 | } |
| 9182 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9183 | done: |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9184 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9185 | /* |
| 9186 | * builds the next links chain from the prev one |
| 9187 | */ |
| 9188 | cur = lastchg; |
| 9189 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9190 | if ((cur == start) || (cur->prev == NULL)) |
| 9191 | break; |
| 9192 | cur->prev->next = cur; |
| 9193 | cur = cur->prev; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9194 | } |
| 9195 | if (ret == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9196 | if (ctxt->errNr > errNr) |
| 9197 | xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9198 | } |
| 9199 | |
| 9200 | xmlFree(list); |
| 9201 | xmlFree(lasts); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9202 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9203 | } |
| 9204 | |
| 9205 | /** |
| 9206 | * xmlRelaxNGValidateDefinitionList: |
| 9207 | * @ctxt: a Relax-NG validation context |
| 9208 | * @define: the list of definition to verify |
| 9209 | * |
| 9210 | * Validate the given node content against the (list) of definitions |
| 9211 | * |
| 9212 | * Returns 0 if the validation succeeded or an error code. |
| 9213 | */ |
| 9214 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9215 | xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt, |
| 9216 | xmlRelaxNGDefinePtr defines) |
| 9217 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9218 | int ret = 0, res; |
| 9219 | |
| 9220 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 9221 | if (defines == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9222 | VALID_ERR2(XML_RELAXNG_ERR_INTERNAL, |
| 9223 | BAD_CAST "NULL definition list"); |
| 9224 | return (-1); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 9225 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9226 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9227 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
| 9228 | res = xmlRelaxNGValidateDefinition(ctxt, defines); |
| 9229 | if (res < 0) |
| 9230 | ret = -1; |
| 9231 | } else { |
| 9232 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 9233 | return (-1); |
| 9234 | } |
| 9235 | if (res == -1) /* continues on -2 */ |
| 9236 | break; |
| 9237 | defines = defines->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9238 | } |
| 9239 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9240 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9241 | } |
| 9242 | |
| 9243 | /** |
| 9244 | * xmlRelaxNGElementMatch: |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9245 | * @ctxt: a Relax-NG validation context |
| 9246 | * @define: the definition to check |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9247 | * @elem: the element |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9248 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9249 | * Check if the element matches the definition nameClass |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9250 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9251 | * 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] | 9252 | */ |
| 9253 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9254 | xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 9255 | xmlRelaxNGDefinePtr define, xmlNodePtr elem) |
| 9256 | { |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 9257 | int ret = 0, oldflags = 0; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9258 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9259 | if (define->name != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9260 | if (!xmlStrEqual(elem->name, define->name)) { |
| 9261 | VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name); |
| 9262 | return (0); |
| 9263 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9264 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9265 | if ((define->ns != NULL) && (define->ns[0] != 0)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9266 | if (elem->ns == NULL) { |
| 9267 | VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, elem->name); |
| 9268 | return (0); |
| 9269 | } else if (!xmlStrEqual(elem->ns->href, define->ns)) { |
| 9270 | VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS, |
| 9271 | elem->name, define->ns); |
| 9272 | return (0); |
| 9273 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9274 | } else if ((elem->ns != NULL) && (define->ns != NULL) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9275 | (define->name == NULL)) { |
| 9276 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, elem->name); |
| 9277 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9278 | } else if ((elem->ns != NULL) && (define->name != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9279 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, define->name); |
| 9280 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9281 | } |
| 9282 | |
| 9283 | if (define->nameClass == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9284 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9285 | |
| 9286 | define = define->nameClass; |
| 9287 | if (define->type == XML_RELAXNG_EXCEPT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9288 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9289 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9290 | if (ctxt != NULL) { |
| 9291 | oldflags = ctxt->flags; |
| 9292 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9293 | } |
| 9294 | |
| 9295 | list = define->content; |
| 9296 | while (list != NULL) { |
| 9297 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
| 9298 | if (ret == 1) { |
| 9299 | if (ctxt != NULL) |
| 9300 | ctxt->flags = oldflags; |
| 9301 | return (0); |
| 9302 | } |
| 9303 | if (ret < 0) { |
| 9304 | if (ctxt != NULL) |
| 9305 | ctxt->flags = oldflags; |
| 9306 | return (ret); |
| 9307 | } |
| 9308 | list = list->next; |
| 9309 | } |
| 9310 | ret = 1; |
| 9311 | if (ctxt != NULL) { |
| 9312 | ctxt->flags = oldflags; |
| 9313 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9314 | } else if (define->type == XML_RELAXNG_CHOICE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9315 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9316 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9317 | if (ctxt != NULL) { |
| 9318 | oldflags = ctxt->flags; |
| 9319 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9320 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9321 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9322 | list = define->nameClass; |
| 9323 | while (list != NULL) { |
| 9324 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
| 9325 | if (ret == 1) { |
| 9326 | if (ctxt != NULL) |
| 9327 | ctxt->flags = oldflags; |
| 9328 | return (1); |
| 9329 | } |
| 9330 | if (ret < 0) { |
| 9331 | if (ctxt != NULL) |
| 9332 | ctxt->flags = oldflags; |
| 9333 | return (ret); |
| 9334 | } |
| 9335 | list = list->next; |
| 9336 | } |
| 9337 | if (ctxt != NULL) { |
| 9338 | if (ret != 0) { |
| 9339 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9340 | xmlRelaxNGDumpValidError(ctxt); |
| 9341 | } else { |
| 9342 | if (ctxt->errNr > 0) |
| 9343 | xmlRelaxNGPopErrors(ctxt, 0); |
| 9344 | } |
| 9345 | } |
| 9346 | ret = 0; |
| 9347 | if (ctxt != NULL) { |
| 9348 | ctxt->flags = oldflags; |
| 9349 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9350 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9351 | TODO ret = -1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9352 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9353 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9354 | } |
| 9355 | |
| 9356 | /** |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9357 | * xmlRelaxNGBestState: |
| 9358 | * @ctxt: a Relax-NG validation context |
| 9359 | * |
| 9360 | * Find the "best" state in the ctxt->states list of states to report |
| 9361 | * errors about. I.e. a state with no element left in the child list |
| 9362 | * or the one with the less attributes left. |
| 9363 | * This is called only if a falidation error was detected |
| 9364 | * |
| 9365 | * Returns the index of the "best" state or -1 in case of error |
| 9366 | */ |
| 9367 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9368 | xmlRelaxNGBestState(xmlRelaxNGValidCtxtPtr ctxt) |
| 9369 | { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9370 | xmlRelaxNGValidStatePtr state; |
| 9371 | int i, tmp; |
| 9372 | int best = -1; |
| 9373 | int value = 1000000; |
| 9374 | |
| 9375 | if ((ctxt == NULL) || (ctxt->states == NULL) || |
| 9376 | (ctxt->states->nbState <= 0)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9377 | return (-1); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9378 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9379 | for (i = 0; i < ctxt->states->nbState; i++) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9380 | state = ctxt->states->tabState[i]; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9381 | if (state == NULL) |
| 9382 | continue; |
| 9383 | if (state->seq != NULL) { |
| 9384 | if ((best == -1) || (value > 100000)) { |
| 9385 | value = 100000; |
| 9386 | best = i; |
| 9387 | } |
| 9388 | } else { |
| 9389 | tmp = state->nbAttrLeft; |
| 9390 | if ((best == -1) || (value > tmp)) { |
| 9391 | value = tmp; |
| 9392 | best = i; |
| 9393 | } |
| 9394 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9395 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9396 | return (best); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9397 | } |
| 9398 | |
| 9399 | /** |
| 9400 | * xmlRelaxNGLogBestError: |
| 9401 | * @ctxt: a Relax-NG validation context |
| 9402 | * |
| 9403 | * Find the "best" state in the ctxt->states list of states to report |
| 9404 | * errors about and log it. |
| 9405 | */ |
| 9406 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9407 | xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt) |
| 9408 | { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9409 | int best; |
| 9410 | |
| 9411 | if ((ctxt == NULL) || (ctxt->states == NULL) || |
| 9412 | (ctxt->states->nbState <= 0)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9413 | return; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9414 | |
| 9415 | best = xmlRelaxNGBestState(ctxt); |
| 9416 | if ((best >= 0) && (best < ctxt->states->nbState)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9417 | ctxt->state = ctxt->states->tabState[best]; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9418 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9419 | xmlRelaxNGValidateElementEnd(ctxt, 1); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9420 | } |
| 9421 | } |
| 9422 | |
| 9423 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9424 | * xmlRelaxNGValidateElementEnd: |
| 9425 | * @ctxt: a Relax-NG validation context |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 9426 | * @dolog: indicate that error logging should be done |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9427 | * |
| 9428 | * Validate the end of the element, implements check that |
| 9429 | * there is nothing left not consumed in the element content |
| 9430 | * or in the attribute list. |
| 9431 | * |
| 9432 | * Returns 0 if the validation succeeded or an error code. |
| 9433 | */ |
| 9434 | static int |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 9435 | xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int dolog) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9436 | { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9437 | int i; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9438 | xmlRelaxNGValidStatePtr state; |
| 9439 | |
| 9440 | state = ctxt->state; |
| 9441 | if (state->seq != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9442 | state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq); |
| 9443 | if (state->seq != NULL) { |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 9444 | if (dolog) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9445 | VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT, |
| 9446 | state->node->name, state->seq->name); |
| 9447 | } |
| 9448 | return (-1); |
| 9449 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9450 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9451 | for (i = 0; i < state->nbAttrs; i++) { |
| 9452 | if (state->attrs[i] != NULL) { |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 9453 | if (dolog) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9454 | VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR, |
| 9455 | state->attrs[i]->name, state->node->name); |
| 9456 | } |
| 9457 | return (-1 - i); |
| 9458 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9459 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9460 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9461 | } |
| 9462 | |
| 9463 | /** |
| 9464 | * xmlRelaxNGValidateState: |
| 9465 | * @ctxt: a Relax-NG validation context |
| 9466 | * @define: the definition to verify |
| 9467 | * |
| 9468 | * Validate the current state against the definition |
| 9469 | * |
| 9470 | * Returns 0 if the validation succeeded or an error code. |
| 9471 | */ |
| 9472 | static int |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9473 | xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt, |
| 9474 | xmlRelaxNGDefinePtr define) |
| 9475 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9476 | xmlNodePtr node; |
| 9477 | int ret = 0, i, tmp, oldflags, errNr; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9478 | xmlRelaxNGValidStatePtr oldstate = NULL, state; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9479 | |
| 9480 | if (define == NULL) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9481 | VALID_ERR(XML_RELAXNG_ERR_NODEFINE); |
| 9482 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9483 | } |
| 9484 | |
| 9485 | if (ctxt->state != NULL) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9486 | node = ctxt->state->seq; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9487 | } else { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9488 | node = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9489 | } |
| 9490 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9491 | for (i = 0; i < ctxt->depth; i++) |
| 9492 | xmlGenericError(xmlGenericErrorContext, " "); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9493 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9494 | "Start validating %s ", xmlRelaxNGDefName(define)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9495 | if (define->name != NULL) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9496 | xmlGenericError(xmlGenericErrorContext, "%s ", define->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9497 | if ((node != NULL) && (node->name != NULL)) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9498 | xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9499 | else |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9500 | xmlGenericError(xmlGenericErrorContext, "\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9501 | #endif |
| 9502 | ctxt->depth++; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9503 | switch (define->type) { |
| 9504 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9505 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 9506 | ret = 0; |
| 9507 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9508 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9509 | ret = -1; |
| 9510 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9511 | case XML_RELAXNG_TEXT: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9512 | while ((node != NULL) && |
| 9513 | ((node->type == XML_TEXT_NODE) || |
| 9514 | (node->type == XML_COMMENT_NODE) || |
| 9515 | (node->type == XML_PI_NODE) || |
| 9516 | (node->type == XML_CDATA_SECTION_NODE))) |
| 9517 | node = node->next; |
| 9518 | ctxt->state->seq = node; |
| 9519 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9520 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9521 | errNr = ctxt->errNr; |
| 9522 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 9523 | if (node == NULL) { |
| 9524 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name); |
| 9525 | ret = -1; |
| 9526 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9527 | xmlRelaxNGDumpValidError(ctxt); |
| 9528 | break; |
| 9529 | } |
| 9530 | if (node->type != XML_ELEMENT_NODE) { |
| 9531 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
| 9532 | ret = -1; |
| 9533 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9534 | xmlRelaxNGDumpValidError(ctxt); |
| 9535 | break; |
| 9536 | } |
| 9537 | /* |
| 9538 | * This node was already validated successfully against |
| 9539 | * this definition. |
| 9540 | */ |
| 9541 | if (node->_private == define) { |
| 9542 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
| 9543 | if (ctxt->errNr > errNr) |
| 9544 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9545 | if (ctxt->errNr != 0) { |
| 9546 | while ((ctxt->err != NULL) && |
| 9547 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) |
| 9548 | && (xmlStrEqual(ctxt->err->arg2, node->name))) |
| 9549 | || |
| 9550 | ((ctxt->err->err == |
| 9551 | XML_RELAXNG_ERR_ELEMEXTRANS) |
| 9552 | && (xmlStrEqual(ctxt->err->arg1, node->name))) |
| 9553 | || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) |
| 9554 | || (ctxt->err->err == |
| 9555 | XML_RELAXNG_ERR_NOTELEM))) |
| 9556 | xmlRelaxNGValidErrorPop(ctxt); |
| 9557 | } |
| 9558 | break; |
| 9559 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9560 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9561 | ret = xmlRelaxNGElementMatch(ctxt, define, node); |
| 9562 | if (ret <= 0) { |
| 9563 | ret = -1; |
| 9564 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9565 | xmlRelaxNGDumpValidError(ctxt); |
| 9566 | break; |
| 9567 | } |
| 9568 | ret = 0; |
| 9569 | if (ctxt->errNr != 0) { |
| 9570 | if (ctxt->errNr > errNr) |
| 9571 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9572 | while ((ctxt->err != NULL) && |
| 9573 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) && |
| 9574 | (xmlStrEqual(ctxt->err->arg2, node->name))) || |
| 9575 | ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) && |
| 9576 | (xmlStrEqual(ctxt->err->arg1, node->name))) || |
| 9577 | (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) || |
| 9578 | (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM))) |
| 9579 | xmlRelaxNGValidErrorPop(ctxt); |
| 9580 | } |
| 9581 | errNr = ctxt->errNr; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9582 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9583 | oldflags = ctxt->flags; |
| 9584 | if (ctxt->flags & FLAGS_MIXED_CONTENT) { |
| 9585 | ctxt->flags -= FLAGS_MIXED_CONTENT; |
| 9586 | } |
| 9587 | state = xmlRelaxNGNewValidState(ctxt, node); |
| 9588 | if (state == NULL) { |
| 9589 | ret = -1; |
| 9590 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9591 | xmlRelaxNGDumpValidError(ctxt); |
| 9592 | break; |
| 9593 | } |
Daniel Veillard | 7fe1f3a | 2003-03-31 22:13:33 +0000 | [diff] [blame] | 9594 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9595 | oldstate = ctxt->state; |
| 9596 | ctxt->state = state; |
| 9597 | if (define->attrs != NULL) { |
| 9598 | tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
| 9599 | if (tmp != 0) { |
| 9600 | ret = -1; |
| 9601 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
| 9602 | } |
| 9603 | } |
| 9604 | if (define->contModel != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9605 | xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state; |
| 9606 | xmlRelaxNGStatesPtr tmpstates = ctxt->states; |
| 9607 | xmlNodePtr nseq; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9608 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9609 | nstate = xmlRelaxNGNewValidState(ctxt, node); |
| 9610 | ctxt->state = nstate; |
| 9611 | ctxt->states = NULL; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9612 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9613 | tmp = xmlRelaxNGValidateCompiledContent(ctxt, |
| 9614 | define->contModel, |
| 9615 | ctxt->state->seq); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9616 | nseq = ctxt->state->seq; |
| 9617 | ctxt->state = tmpstate; |
| 9618 | ctxt->states = tmpstates; |
| 9619 | xmlRelaxNGFreeValidState(ctxt, nstate); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9620 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9621 | #ifdef DEBUG_COMPILE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9622 | xmlGenericError(xmlGenericErrorContext, |
| 9623 | "Validating content of '%s' : %d\n", |
| 9624 | define->name, tmp); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9625 | #endif |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9626 | if (tmp != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9627 | ret = -1; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9628 | |
| 9629 | if (ctxt->states != NULL) { |
| 9630 | tmp = -1; |
| 9631 | |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9632 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9633 | state = ctxt->states->tabState[i]; |
| 9634 | ctxt->state = state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9635 | ctxt->state->seq = nseq; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9636 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9637 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9638 | tmp = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9639 | break; |
| 9640 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9641 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9642 | if (tmp != 0) { |
| 9643 | /* |
| 9644 | * validation error, log the message for the "best" one |
| 9645 | */ |
| 9646 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9647 | xmlRelaxNGLogBestError(ctxt); |
| 9648 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9649 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9650 | xmlRelaxNGFreeValidState(ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9651 | ctxt->states-> |
| 9652 | tabState[i]); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9653 | } |
| 9654 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9655 | ctxt->flags = oldflags; |
| 9656 | ctxt->states = NULL; |
| 9657 | if ((ret == 0) && (tmp == -1)) |
| 9658 | ret = -1; |
| 9659 | } else { |
| 9660 | state = ctxt->state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9661 | ctxt->state->seq = nseq; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9662 | if (ret == 0) |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9663 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9664 | xmlRelaxNGFreeValidState(ctxt, state); |
| 9665 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9666 | } else { |
| 9667 | if (define->content != NULL) { |
| 9668 | tmp = xmlRelaxNGValidateDefinitionList(ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9669 | define-> |
| 9670 | content); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9671 | if (tmp != 0) { |
| 9672 | ret = -1; |
| 9673 | if (ctxt->state == NULL) { |
| 9674 | ctxt->state = oldstate; |
| 9675 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, |
| 9676 | node->name); |
| 9677 | ctxt->state = NULL; |
| 9678 | } else { |
| 9679 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, |
| 9680 | node->name); |
| 9681 | } |
| 9682 | |
| 9683 | } |
| 9684 | } |
| 9685 | if (ctxt->states != NULL) { |
| 9686 | tmp = -1; |
| 9687 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9688 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9689 | state = ctxt->states->tabState[i]; |
| 9690 | ctxt->state = state; |
| 9691 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9692 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9693 | tmp = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9694 | break; |
| 9695 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9696 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9697 | if (tmp != 0) { |
| 9698 | /* |
| 9699 | * validation error, log the message for the "best" one |
| 9700 | */ |
| 9701 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9702 | xmlRelaxNGLogBestError(ctxt); |
| 9703 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9704 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9705 | xmlRelaxNGFreeValidState(ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9706 | ctxt->states-> |
| 9707 | tabState[i]); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9708 | } |
| 9709 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9710 | ctxt->flags = oldflags; |
| 9711 | ctxt->states = NULL; |
| 9712 | if ((ret == 0) && (tmp == -1)) |
| 9713 | ret = -1; |
| 9714 | } else { |
| 9715 | state = ctxt->state; |
| 9716 | if (ret == 0) |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9717 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9718 | xmlRelaxNGFreeValidState(ctxt, state); |
| 9719 | } |
| 9720 | } |
| 9721 | if (ret == 0) { |
| 9722 | node->_private = define; |
| 9723 | } |
| 9724 | ctxt->flags = oldflags; |
| 9725 | ctxt->state = oldstate; |
| 9726 | if (oldstate != NULL) |
| 9727 | oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
| 9728 | if (ret != 0) { |
| 9729 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 9730 | xmlRelaxNGDumpValidError(ctxt); |
| 9731 | ret = 0; |
| 9732 | } else { |
| 9733 | ret = -2; |
| 9734 | } |
| 9735 | } else { |
| 9736 | if (ctxt->errNr > errNr) |
| 9737 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9738 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9739 | |
| 9740 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9741 | xmlGenericError(xmlGenericErrorContext, |
| 9742 | "xmlRelaxNGValidateDefinition(): validated %s : %d", |
| 9743 | node->name, ret); |
| 9744 | if (oldstate == NULL) |
| 9745 | xmlGenericError(xmlGenericErrorContext, ": no state\n"); |
| 9746 | else if (oldstate->seq == NULL) |
| 9747 | xmlGenericError(xmlGenericErrorContext, ": done\n"); |
| 9748 | else if (oldstate->seq->type == XML_ELEMENT_NODE) |
| 9749 | xmlGenericError(xmlGenericErrorContext, ": next elem %s\n", |
| 9750 | oldstate->seq->name); |
| 9751 | else |
| 9752 | xmlGenericError(xmlGenericErrorContext, ": next %s %d\n", |
| 9753 | oldstate->seq->name, oldstate->seq->type); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9754 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9755 | break; |
| 9756 | case XML_RELAXNG_OPTIONAL:{ |
| 9757 | errNr = ctxt->errNr; |
| 9758 | oldflags = ctxt->flags; |
| 9759 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9760 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
| 9761 | ret = |
| 9762 | xmlRelaxNGValidateDefinitionList(ctxt, |
| 9763 | define->content); |
| 9764 | if (ret != 0) { |
| 9765 | if (ctxt->state != NULL) |
| 9766 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 9767 | ctxt->state = oldstate; |
| 9768 | ctxt->flags = oldflags; |
| 9769 | ret = 0; |
| 9770 | if (ctxt->errNr > errNr) |
| 9771 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9772 | break; |
| 9773 | } |
| 9774 | if (ctxt->states != NULL) { |
| 9775 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
| 9776 | } else { |
| 9777 | ctxt->states = xmlRelaxNGNewStates(ctxt, 1); |
| 9778 | if (ctxt->states == NULL) { |
| 9779 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 9780 | ctxt->flags = oldflags; |
| 9781 | ret = -1; |
| 9782 | if (ctxt->errNr > errNr) |
| 9783 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9784 | break; |
| 9785 | } |
| 9786 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
| 9787 | xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state); |
| 9788 | ctxt->state = NULL; |
| 9789 | } |
| 9790 | ctxt->flags = oldflags; |
| 9791 | ret = 0; |
| 9792 | if (ctxt->errNr > errNr) |
| 9793 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9794 | break; |
| 9795 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9796 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9797 | errNr = ctxt->errNr; |
| 9798 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 9799 | if (ret != 0) { |
| 9800 | break; |
| 9801 | } |
| 9802 | if (ctxt->errNr > errNr) |
| 9803 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9804 | /* no break on purpose */ |
| 9805 | case XML_RELAXNG_ZEROORMORE:{ |
| 9806 | int progress; |
| 9807 | xmlRelaxNGStatesPtr states = NULL, res = NULL; |
| 9808 | int base, j; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9809 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9810 | errNr = ctxt->errNr; |
| 9811 | res = xmlRelaxNGNewStates(ctxt, 1); |
| 9812 | if (res == NULL) { |
| 9813 | ret = -1; |
| 9814 | break; |
| 9815 | } |
| 9816 | /* |
| 9817 | * All the input states are also exit states |
| 9818 | */ |
| 9819 | if (ctxt->state != NULL) { |
| 9820 | xmlRelaxNGAddStates(ctxt, res, |
| 9821 | xmlRelaxNGCopyValidState(ctxt, |
| 9822 | ctxt-> |
| 9823 | state)); |
| 9824 | } else { |
| 9825 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 9826 | xmlRelaxNGAddStates(ctxt, res, |
| 9827 | xmlRelaxNGCopyValidState(ctxt, |
| 9828 | ctxt-> |
| 9829 | states-> |
| 9830 | tabState |
| 9831 | [j])); |
| 9832 | } |
| 9833 | } |
| 9834 | oldflags = ctxt->flags; |
| 9835 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9836 | do { |
| 9837 | progress = 0; |
| 9838 | base = res->nbState; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9839 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9840 | if (ctxt->states != NULL) { |
| 9841 | states = ctxt->states; |
| 9842 | for (i = 0; i < states->nbState; i++) { |
| 9843 | ctxt->state = states->tabState[i]; |
| 9844 | ctxt->states = NULL; |
| 9845 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
| 9846 | define-> |
| 9847 | content); |
| 9848 | if (ret == 0) { |
| 9849 | if (ctxt->state != NULL) { |
| 9850 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 9851 | ctxt->state); |
| 9852 | ctxt->state = NULL; |
| 9853 | if (tmp == 1) |
| 9854 | progress = 1; |
| 9855 | } else if (ctxt->states != NULL) { |
| 9856 | for (j = 0; j < ctxt->states->nbState; |
| 9857 | j++) { |
| 9858 | tmp = |
| 9859 | xmlRelaxNGAddStates(ctxt, res, |
| 9860 | ctxt-> |
| 9861 | states-> |
| 9862 | tabState |
| 9863 | [j]); |
| 9864 | if (tmp == 1) |
| 9865 | progress = 1; |
| 9866 | } |
| 9867 | xmlRelaxNGFreeStates(ctxt, |
| 9868 | ctxt->states); |
| 9869 | ctxt->states = NULL; |
| 9870 | } |
| 9871 | } else { |
| 9872 | if (ctxt->state != NULL) { |
| 9873 | xmlRelaxNGFreeValidState(ctxt, |
| 9874 | ctxt->state); |
| 9875 | ctxt->state = NULL; |
| 9876 | } |
| 9877 | } |
| 9878 | } |
| 9879 | } else { |
| 9880 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
| 9881 | define-> |
| 9882 | content); |
| 9883 | if (ret != 0) { |
| 9884 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 9885 | ctxt->state = NULL; |
| 9886 | } else { |
| 9887 | base = res->nbState; |
| 9888 | if (ctxt->state != NULL) { |
| 9889 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 9890 | ctxt->state); |
| 9891 | ctxt->state = NULL; |
| 9892 | if (tmp == 1) |
| 9893 | progress = 1; |
| 9894 | } else if (ctxt->states != NULL) { |
| 9895 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 9896 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 9897 | ctxt-> |
| 9898 | states-> |
| 9899 | tabState[j]); |
| 9900 | if (tmp == 1) |
| 9901 | progress = 1; |
| 9902 | } |
| 9903 | if (states == NULL) { |
| 9904 | states = ctxt->states; |
| 9905 | } else { |
| 9906 | xmlRelaxNGFreeStates(ctxt, |
| 9907 | ctxt->states); |
| 9908 | } |
| 9909 | ctxt->states = NULL; |
| 9910 | } |
| 9911 | } |
| 9912 | } |
| 9913 | if (progress) { |
| 9914 | /* |
| 9915 | * Collect all the new nodes added at that step |
| 9916 | * and make them the new node set |
| 9917 | */ |
| 9918 | if (res->nbState - base == 1) { |
| 9919 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, |
| 9920 | res-> |
| 9921 | tabState |
| 9922 | [base]); |
| 9923 | } else { |
| 9924 | if (states == NULL) { |
| 9925 | xmlRelaxNGNewStates(ctxt, |
| 9926 | res->nbState - base); |
| 9927 | } |
| 9928 | states->nbState = 0; |
| 9929 | for (i = base; i < res->nbState; i++) |
| 9930 | xmlRelaxNGAddStates(ctxt, states, |
| 9931 | xmlRelaxNGCopyValidState |
| 9932 | (ctxt, |
| 9933 | res->tabState[i])); |
| 9934 | ctxt->states = states; |
| 9935 | } |
| 9936 | } |
| 9937 | } while (progress == 1); |
| 9938 | if (states != NULL) { |
| 9939 | xmlRelaxNGFreeStates(ctxt, states); |
| 9940 | } |
| 9941 | ctxt->states = res; |
| 9942 | ctxt->flags = oldflags; |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 9943 | #if 0 |
| 9944 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9945 | * errors may have to be propagated back... |
| 9946 | */ |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9947 | if (ctxt->errNr > errNr) |
| 9948 | xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 9949 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9950 | ret = 0; |
| 9951 | break; |
| 9952 | } |
| 9953 | case XML_RELAXNG_CHOICE:{ |
| 9954 | xmlRelaxNGDefinePtr list = NULL; |
| 9955 | xmlRelaxNGStatesPtr states = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9956 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9957 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9958 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9959 | errNr = ctxt->errNr; |
| 9960 | if ((define->dflags & IS_TRIABLE) |
| 9961 | && (define->data != NULL)) { |
| 9962 | xmlHashTablePtr triage = |
| 9963 | (xmlHashTablePtr) define->data; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 9964 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9965 | /* |
| 9966 | * Something we can optimize cleanly there is only one |
| 9967 | * possble branch out ! |
| 9968 | */ |
| 9969 | if (node == NULL) { |
| 9970 | ret = -1; |
| 9971 | break; |
| 9972 | } |
| 9973 | if ((node->type == XML_TEXT_NODE) || |
| 9974 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 9975 | list = |
| 9976 | xmlHashLookup2(triage, BAD_CAST "#text", NULL); |
| 9977 | } else if (node->type == XML_ELEMENT_NODE) { |
| 9978 | if (node->ns != NULL) { |
| 9979 | list = xmlHashLookup2(triage, node->name, |
| 9980 | node->ns->href); |
| 9981 | if (list == NULL) |
| 9982 | list = |
| 9983 | xmlHashLookup2(triage, BAD_CAST "#any", |
| 9984 | node->ns->href); |
| 9985 | } else |
| 9986 | list = |
| 9987 | xmlHashLookup2(triage, node->name, NULL); |
| 9988 | if (list == NULL) |
| 9989 | list = |
| 9990 | xmlHashLookup2(triage, BAD_CAST "#any", |
| 9991 | NULL); |
| 9992 | } |
| 9993 | if (list == NULL) { |
| 9994 | ret = -1; |
| 9995 | break; |
| 9996 | } |
| 9997 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
| 9998 | if (ret == 0) { |
| 9999 | } |
| 10000 | break; |
| 10001 | } |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 10002 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10003 | list = define->content; |
| 10004 | oldflags = ctxt->flags; |
| 10005 | ctxt->flags |= FLAGS_IGNORABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10006 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10007 | while (list != NULL) { |
| 10008 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
| 10009 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
| 10010 | if (ret == 0) { |
| 10011 | if (states == NULL) { |
| 10012 | states = xmlRelaxNGNewStates(ctxt, 1); |
| 10013 | } |
| 10014 | if (ctxt->state != NULL) { |
| 10015 | xmlRelaxNGAddStates(ctxt, states, ctxt->state); |
| 10016 | } else if (ctxt->states != NULL) { |
| 10017 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 10018 | xmlRelaxNGAddStates(ctxt, states, |
| 10019 | ctxt->states-> |
| 10020 | tabState[i]); |
| 10021 | } |
| 10022 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10023 | ctxt->states = NULL; |
| 10024 | } |
| 10025 | } else { |
| 10026 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10027 | } |
| 10028 | ctxt->state = oldstate; |
| 10029 | list = list->next; |
| 10030 | } |
| 10031 | if (states != NULL) { |
| 10032 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 10033 | ctxt->states = states; |
| 10034 | ctxt->state = NULL; |
| 10035 | ret = 0; |
| 10036 | } else { |
| 10037 | ctxt->states = NULL; |
| 10038 | } |
| 10039 | ctxt->flags = oldflags; |
| 10040 | if (ret != 0) { |
| 10041 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 10042 | xmlRelaxNGDumpValidError(ctxt); |
| 10043 | } |
| 10044 | } else { |
| 10045 | if (ctxt->errNr > errNr) |
| 10046 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 10047 | } |
| 10048 | break; |
| 10049 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10050 | case XML_RELAXNG_DEF: |
| 10051 | case XML_RELAXNG_GROUP: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10052 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 10053 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10054 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10055 | ret = xmlRelaxNGValidateInterleave(ctxt, define); |
| 10056 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10057 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10058 | ret = xmlRelaxNGValidateAttribute(ctxt, define); |
| 10059 | break; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 10060 | case XML_RELAXNG_START: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10061 | case XML_RELAXNG_NOOP: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10062 | case XML_RELAXNG_REF: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10063 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 10064 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10065 | ret = xmlRelaxNGValidateDefinition(ctxt, define->content); |
| 10066 | break; |
| 10067 | case XML_RELAXNG_DATATYPE:{ |
| 10068 | xmlNodePtr child; |
| 10069 | xmlChar *content = NULL; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10070 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10071 | child = node; |
| 10072 | while (child != NULL) { |
| 10073 | if (child->type == XML_ELEMENT_NODE) { |
| 10074 | VALID_ERR2(XML_RELAXNG_ERR_DATAELEM, |
| 10075 | node->parent->name); |
| 10076 | ret = -1; |
| 10077 | break; |
| 10078 | } else if ((child->type == XML_TEXT_NODE) || |
| 10079 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 10080 | content = xmlStrcat(content, child->content); |
| 10081 | } |
| 10082 | /* TODO: handle entities ... */ |
| 10083 | child = child->next; |
| 10084 | } |
| 10085 | if (ret == -1) { |
| 10086 | if (content != NULL) |
| 10087 | xmlFree(content); |
| 10088 | break; |
| 10089 | } |
| 10090 | if (content == NULL) { |
| 10091 | content = xmlStrdup(BAD_CAST ""); |
| 10092 | if (content == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10093 | xmlRngVErrMemory(ctxt, "validating\n"); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10094 | ret = -1; |
| 10095 | break; |
| 10096 | } |
| 10097 | } |
| 10098 | ret = xmlRelaxNGValidateDatatype(ctxt, content, define, |
| 10099 | ctxt->state->seq); |
| 10100 | if (ret == -1) { |
| 10101 | VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name); |
| 10102 | } else if (ret == 0) { |
| 10103 | ctxt->state->seq = NULL; |
| 10104 | } |
| 10105 | if (content != NULL) |
| 10106 | xmlFree(content); |
| 10107 | break; |
| 10108 | } |
| 10109 | case XML_RELAXNG_VALUE:{ |
| 10110 | xmlChar *content = NULL; |
| 10111 | xmlChar *oldvalue; |
| 10112 | xmlNodePtr child; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10113 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10114 | child = node; |
| 10115 | while (child != NULL) { |
| 10116 | if (child->type == XML_ELEMENT_NODE) { |
| 10117 | VALID_ERR2(XML_RELAXNG_ERR_VALELEM, |
| 10118 | node->parent->name); |
| 10119 | ret = -1; |
| 10120 | break; |
| 10121 | } else if ((child->type == XML_TEXT_NODE) || |
| 10122 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 10123 | content = xmlStrcat(content, child->content); |
| 10124 | } |
| 10125 | /* TODO: handle entities ... */ |
| 10126 | child = child->next; |
| 10127 | } |
| 10128 | if (ret == -1) { |
| 10129 | if (content != NULL) |
| 10130 | xmlFree(content); |
| 10131 | break; |
| 10132 | } |
| 10133 | if (content == NULL) { |
| 10134 | content = xmlStrdup(BAD_CAST ""); |
| 10135 | if (content == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10136 | xmlRngVErrMemory(ctxt, "validating\n"); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10137 | ret = -1; |
| 10138 | break; |
| 10139 | } |
| 10140 | } |
| 10141 | oldvalue = ctxt->state->value; |
| 10142 | ctxt->state->value = content; |
| 10143 | ret = xmlRelaxNGValidateValue(ctxt, define); |
| 10144 | ctxt->state->value = oldvalue; |
| 10145 | if (ret == -1) { |
| 10146 | VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name); |
| 10147 | } else if (ret == 0) { |
| 10148 | ctxt->state->seq = NULL; |
| 10149 | } |
| 10150 | if (content != NULL) |
| 10151 | xmlFree(content); |
| 10152 | break; |
| 10153 | } |
| 10154 | case XML_RELAXNG_LIST:{ |
| 10155 | xmlChar *content; |
| 10156 | xmlNodePtr child; |
| 10157 | xmlChar *oldvalue, *oldendvalue; |
| 10158 | int len; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10159 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10160 | /* |
| 10161 | * Make sure it's only text nodes |
| 10162 | */ |
| 10163 | |
| 10164 | content = NULL; |
| 10165 | child = node; |
| 10166 | while (child != NULL) { |
| 10167 | if (child->type == XML_ELEMENT_NODE) { |
| 10168 | VALID_ERR2(XML_RELAXNG_ERR_LISTELEM, |
| 10169 | node->parent->name); |
| 10170 | ret = -1; |
| 10171 | break; |
| 10172 | } else if ((child->type == XML_TEXT_NODE) || |
| 10173 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 10174 | content = xmlStrcat(content, child->content); |
| 10175 | } |
| 10176 | /* TODO: handle entities ... */ |
| 10177 | child = child->next; |
| 10178 | } |
| 10179 | if (ret == -1) { |
| 10180 | if (content != NULL) |
| 10181 | xmlFree(content); |
| 10182 | break; |
| 10183 | } |
| 10184 | if (content == NULL) { |
| 10185 | content = xmlStrdup(BAD_CAST ""); |
| 10186 | if (content == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10187 | xmlRngVErrMemory(ctxt, "validating\n"); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10188 | ret = -1; |
| 10189 | break; |
| 10190 | } |
| 10191 | } |
| 10192 | len = xmlStrlen(content); |
| 10193 | oldvalue = ctxt->state->value; |
| 10194 | oldendvalue = ctxt->state->endvalue; |
| 10195 | ctxt->state->value = content; |
| 10196 | ctxt->state->endvalue = content + len; |
| 10197 | ret = xmlRelaxNGValidateValue(ctxt, define); |
| 10198 | ctxt->state->value = oldvalue; |
| 10199 | ctxt->state->endvalue = oldendvalue; |
| 10200 | if (ret == -1) { |
| 10201 | VALID_ERR(XML_RELAXNG_ERR_LIST); |
| 10202 | } else if ((ret == 0) && (node != NULL)) { |
| 10203 | ctxt->state->seq = node->next; |
| 10204 | } |
| 10205 | if (content != NULL) |
| 10206 | xmlFree(content); |
| 10207 | break; |
| 10208 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10209 | case XML_RELAXNG_EXCEPT: |
| 10210 | case XML_RELAXNG_PARAM: |
| 10211 | TODO ret = -1; |
| 10212 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10213 | } |
| 10214 | ctxt->depth--; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10215 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10216 | for (i = 0; i < ctxt->depth; i++) |
| 10217 | xmlGenericError(xmlGenericErrorContext, " "); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10218 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10219 | "Validating %s ", xmlRelaxNGDefName(define)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10220 | if (define->name != NULL) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10221 | xmlGenericError(xmlGenericErrorContext, "%s ", define->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10222 | if (ret == 0) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10223 | xmlGenericError(xmlGenericErrorContext, "suceeded\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10224 | else |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10225 | xmlGenericError(xmlGenericErrorContext, "failed\n"); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10226 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10227 | return (ret); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10228 | } |
| 10229 | |
| 10230 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10231 | * xmlRelaxNGValidateDefinition: |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10232 | * @ctxt: a Relax-NG validation context |
| 10233 | * @define: the definition to verify |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10234 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10235 | * Validate the current node lists against the definition |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10236 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10237 | * Returns 0 if the validation succeeded or an error code. |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10238 | */ |
| 10239 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10240 | xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
| 10241 | xmlRelaxNGDefinePtr define) |
| 10242 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10243 | xmlRelaxNGStatesPtr states, res; |
| 10244 | int i, j, k, ret, oldflags; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10245 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10246 | /* |
| 10247 | * We should NOT have both ctxt->state and ctxt->states |
| 10248 | */ |
| 10249 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10250 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10251 | ctxt->state = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10252 | } |
| 10253 | |
| 10254 | if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10255 | if (ctxt->states != NULL) { |
| 10256 | ctxt->state = ctxt->states->tabState[0]; |
| 10257 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10258 | ctxt->states = NULL; |
| 10259 | } |
| 10260 | ret = xmlRelaxNGValidateState(ctxt, define); |
| 10261 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10262 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10263 | ctxt->state = NULL; |
| 10264 | } |
| 10265 | if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) { |
| 10266 | ctxt->state = ctxt->states->tabState[0]; |
| 10267 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10268 | ctxt->states = NULL; |
| 10269 | } |
| 10270 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10271 | } |
| 10272 | |
| 10273 | states = ctxt->states; |
| 10274 | ctxt->states = NULL; |
| 10275 | res = NULL; |
| 10276 | j = 0; |
| 10277 | oldflags = ctxt->flags; |
| 10278 | ctxt->flags |= FLAGS_IGNORABLE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10279 | for (i = 0; i < states->nbState; i++) { |
| 10280 | ctxt->state = states->tabState[i]; |
| 10281 | ctxt->states = NULL; |
| 10282 | ret = xmlRelaxNGValidateState(ctxt, define); |
| 10283 | /* |
| 10284 | * We should NOT have both ctxt->state and ctxt->states |
| 10285 | */ |
| 10286 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10287 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10288 | ctxt->state = NULL; |
| 10289 | } |
| 10290 | if (ret == 0) { |
| 10291 | if (ctxt->states == NULL) { |
| 10292 | if (res != NULL) { |
| 10293 | /* add the state to the container */ |
| 10294 | xmlRelaxNGAddStates(ctxt, res, ctxt->state); |
| 10295 | ctxt->state = NULL; |
| 10296 | } else { |
| 10297 | /* add the state directly in states */ |
| 10298 | states->tabState[j++] = ctxt->state; |
| 10299 | ctxt->state = NULL; |
| 10300 | } |
| 10301 | } else { |
| 10302 | if (res == NULL) { |
| 10303 | /* make it the new container and copy other results */ |
| 10304 | res = ctxt->states; |
| 10305 | ctxt->states = NULL; |
| 10306 | for (k = 0; k < j; k++) |
| 10307 | xmlRelaxNGAddStates(ctxt, res, |
| 10308 | states->tabState[k]); |
| 10309 | } else { |
| 10310 | /* add all the new results to res and reff the container */ |
| 10311 | for (k = 0; k < ctxt->states->nbState; k++) |
| 10312 | xmlRelaxNGAddStates(ctxt, res, |
| 10313 | ctxt->states->tabState[k]); |
| 10314 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10315 | ctxt->states = NULL; |
| 10316 | } |
| 10317 | } |
| 10318 | } else { |
| 10319 | if (ctxt->state != NULL) { |
| 10320 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10321 | ctxt->state = NULL; |
| 10322 | } else if (ctxt->states != NULL) { |
| 10323 | for (k = 0; k < ctxt->states->nbState; k++) |
| 10324 | xmlRelaxNGFreeValidState(ctxt, |
| 10325 | ctxt->states->tabState[k]); |
| 10326 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10327 | ctxt->states = NULL; |
| 10328 | } |
| 10329 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10330 | } |
| 10331 | ctxt->flags = oldflags; |
| 10332 | if (res != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10333 | xmlRelaxNGFreeStates(ctxt, states); |
| 10334 | ctxt->states = res; |
| 10335 | ret = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10336 | } else if (j > 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10337 | states->nbState = j; |
| 10338 | ctxt->states = states; |
| 10339 | ret = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10340 | } else if (j == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10341 | ctxt->state = states->tabState[0]; |
| 10342 | xmlRelaxNGFreeStates(ctxt, states); |
| 10343 | ret = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10344 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10345 | ret = -1; |
| 10346 | xmlRelaxNGFreeStates(ctxt, states); |
| 10347 | if (ctxt->states != NULL) { |
| 10348 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10349 | ctxt->states = NULL; |
| 10350 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10351 | } |
| 10352 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10353 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10354 | ctxt->state = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10355 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10356 | return (ret); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10357 | } |
| 10358 | |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10359 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10360 | * xmlRelaxNGValidateDocument: |
| 10361 | * @ctxt: a Relax-NG validation context |
| 10362 | * @doc: the document |
| 10363 | * |
| 10364 | * Validate the given document |
| 10365 | * |
| 10366 | * Returns 0 if the validation succeeded or an error code. |
| 10367 | */ |
| 10368 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10369 | xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) |
| 10370 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10371 | int ret; |
| 10372 | xmlRelaxNGPtr schema; |
| 10373 | xmlRelaxNGGrammarPtr grammar; |
| 10374 | xmlRelaxNGValidStatePtr state; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10375 | xmlNodePtr node; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10376 | |
| 10377 | if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10378 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10379 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10380 | ctxt->errNo = XML_RELAXNG_OK; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10381 | schema = ctxt->schema; |
| 10382 | grammar = schema->topgrammar; |
| 10383 | if (grammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10384 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 10385 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10386 | } |
| 10387 | state = xmlRelaxNGNewValidState(ctxt, NULL); |
| 10388 | ctxt->state = state; |
| 10389 | ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10390 | if ((ctxt->state != NULL) && (state->seq != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10391 | state = ctxt->state; |
| 10392 | node = state->seq; |
| 10393 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 10394 | if (node != NULL) { |
| 10395 | if (ret != -1) { |
| 10396 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
| 10397 | ret = -1; |
| 10398 | } |
| 10399 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10400 | } else if (ctxt->states != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10401 | int i; |
| 10402 | int tmp = -1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10403 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10404 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 10405 | state = ctxt->states->tabState[i]; |
| 10406 | node = state->seq; |
| 10407 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 10408 | if (node == NULL) |
| 10409 | tmp = 0; |
| 10410 | xmlRelaxNGFreeValidState(ctxt, state); |
| 10411 | } |
| 10412 | if (tmp == -1) { |
| 10413 | if (ret != -1) { |
| 10414 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
| 10415 | ret = -1; |
| 10416 | } |
| 10417 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10418 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 10419 | if (ctxt->state != NULL) { |
| 10420 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10421 | ctxt->state = NULL; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 10422 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10423 | if (ret != 0) |
| 10424 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 10425 | #ifdef DEBUG |
| 10426 | else if (ctxt->errNr != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10427 | ctxt->error(ctxt->userData, |
| 10428 | "%d Extra error messages left on stack !\n", |
| 10429 | ctxt->errNr); |
| 10430 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 10431 | } |
| 10432 | #endif |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10433 | if (ctxt->idref == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10434 | xmlValidCtxt vctxt; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10435 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10436 | memset(&vctxt, 0, sizeof(xmlValidCtxt)); |
| 10437 | vctxt.valid = 1; |
| 10438 | vctxt.error = ctxt->error; |
| 10439 | vctxt.warning = ctxt->warning; |
| 10440 | vctxt.userData = ctxt->userData; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10441 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10442 | if (xmlValidateDocumentFinal(&vctxt, doc) != 1) |
| 10443 | ret = -1; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10444 | } |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10445 | if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10446 | ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10447 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10448 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10449 | } |
| 10450 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10451 | /************************************************************************ |
| 10452 | * * |
| 10453 | * Validation interfaces * |
| 10454 | * * |
| 10455 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10456 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10457 | /** |
| 10458 | * xmlRelaxNGNewValidCtxt: |
| 10459 | * @schema: a precompiled XML RelaxNGs |
| 10460 | * |
| 10461 | * Create an XML RelaxNGs validation context based on the given schema |
| 10462 | * |
| 10463 | * Returns the validation context or NULL in case of error |
| 10464 | */ |
| 10465 | xmlRelaxNGValidCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10466 | xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema) |
| 10467 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10468 | xmlRelaxNGValidCtxtPtr ret; |
| 10469 | |
| 10470 | ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt)); |
| 10471 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10472 | xmlRngVErrMemory(NULL, "building context\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10473 | return (NULL); |
| 10474 | } |
| 10475 | memset(ret, 0, sizeof(xmlRelaxNGValidCtxt)); |
| 10476 | ret->schema = schema; |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 10477 | ret->error = xmlGenericError; |
| 10478 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 10479 | ret->errNr = 0; |
| 10480 | ret->errMax = 0; |
| 10481 | ret->err = NULL; |
| 10482 | ret->errTab = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10483 | ret->idref = schema->idref; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10484 | ret->states = NULL; |
| 10485 | ret->freeState = NULL; |
| 10486 | ret->freeStates = NULL; |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10487 | ret->errNo = XML_RELAXNG_OK; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10488 | return (ret); |
| 10489 | } |
| 10490 | |
| 10491 | /** |
| 10492 | * xmlRelaxNGFreeValidCtxt: |
| 10493 | * @ctxt: the schema validation context |
| 10494 | * |
| 10495 | * Free the resources associated to the schema validation context |
| 10496 | */ |
| 10497 | void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10498 | xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt) |
| 10499 | { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10500 | int k; |
| 10501 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10502 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10503 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10504 | if (ctxt->states != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10505 | xmlRelaxNGFreeStates(NULL, ctxt->states); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10506 | if (ctxt->freeState != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10507 | for (k = 0; k < ctxt->freeState->nbState; k++) { |
| 10508 | xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]); |
| 10509 | } |
| 10510 | xmlRelaxNGFreeStates(NULL, ctxt->freeState); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10511 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10512 | if (ctxt->freeStates != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10513 | for (k = 0; k < ctxt->freeStatesNr; k++) { |
| 10514 | xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]); |
| 10515 | } |
| 10516 | xmlFree(ctxt->freeStates); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10517 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 10518 | if (ctxt->errTab != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10519 | xmlFree(ctxt->errTab); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 10520 | if (ctxt->elemTab != NULL) { |
| 10521 | xmlRegExecCtxtPtr exec; |
| 10522 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10523 | exec = xmlRelaxNGElemPop(ctxt); |
| 10524 | while (exec != NULL) { |
| 10525 | xmlRegFreeExecCtxt(exec); |
| 10526 | exec = xmlRelaxNGElemPop(ctxt); |
| 10527 | } |
| 10528 | xmlFree(ctxt->elemTab); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 10529 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10530 | xmlFree(ctxt); |
| 10531 | } |
| 10532 | |
| 10533 | /** |
| 10534 | * xmlRelaxNGSetValidErrors: |
| 10535 | * @ctxt: a Relax-NG validation context |
| 10536 | * @err: the error function |
| 10537 | * @warn: the warning function |
| 10538 | * @ctx: the functions context |
| 10539 | * |
| 10540 | * Set the error and warning callback informations |
| 10541 | */ |
| 10542 | void |
| 10543 | xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10544 | xmlRelaxNGValidityErrorFunc err, |
| 10545 | xmlRelaxNGValidityWarningFunc warn, void *ctx) |
| 10546 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10547 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10548 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10549 | ctxt->error = err; |
| 10550 | ctxt->warning = warn; |
| 10551 | ctxt->userData = ctx; |
| 10552 | } |
| 10553 | |
| 10554 | /** |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 10555 | * xmlRelaxNGGetValidErrors: |
| 10556 | * @ctxt: a Relax-NG validation context |
| 10557 | * @err: the error function result |
| 10558 | * @warn: the warning function result |
| 10559 | * @ctx: the functions context result |
| 10560 | * |
| 10561 | * Get the error and warning callback informations |
| 10562 | * |
| 10563 | * Returns -1 in case of error and 0 otherwise |
| 10564 | */ |
| 10565 | int |
| 10566 | xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10567 | xmlRelaxNGValidityErrorFunc * err, |
| 10568 | xmlRelaxNGValidityWarningFunc * warn, void **ctx) |
| 10569 | { |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 10570 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10571 | return (-1); |
| 10572 | if (err != NULL) |
| 10573 | *err = ctxt->error; |
| 10574 | if (warn != NULL) |
| 10575 | *warn = ctxt->warning; |
| 10576 | if (ctx != NULL) |
| 10577 | *ctx = ctxt->userData; |
| 10578 | return (0); |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 10579 | } |
| 10580 | |
| 10581 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10582 | * xmlRelaxNGValidateDoc: |
| 10583 | * @ctxt: a Relax-NG validation context |
| 10584 | * @doc: a parsed document tree |
| 10585 | * |
| 10586 | * Validate a document tree in memory. |
| 10587 | * |
| 10588 | * Returns 0 if the document is valid, a positive error code |
| 10589 | * number otherwise and -1 in case of internal or API error. |
| 10590 | */ |
| 10591 | int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10592 | xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) |
| 10593 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10594 | int ret; |
| 10595 | |
| 10596 | if ((ctxt == NULL) || (doc == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10597 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10598 | |
| 10599 | ctxt->doc = doc; |
| 10600 | |
| 10601 | ret = xmlRelaxNGValidateDocument(ctxt, doc); |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 10602 | /* |
| 10603 | * TODO: build error codes |
| 10604 | */ |
| 10605 | if (ret == -1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10606 | return (1); |
| 10607 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10608 | } |
| 10609 | |
| 10610 | #endif /* LIBXML_SCHEMAS_ENABLED */ |