Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1 | /* |
| 2 | * relaxng.c : implementation of the Relax-NG handling and validity checking |
| 3 | * |
| 4 | * See Copyright for the status of this software. |
| 5 | * |
| 6 | * Daniel Veillard <veillard@redhat.com> |
| 7 | */ |
| 8 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 9 | /** |
| 10 | * TODO: |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 11 | * - add support for DTD compatibility spec |
| 12 | * http://www.oasis-open.org/committees/relax-ng/compatibility-20011203.html |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 13 | * - report better mem allocations pbms at runtime and abort immediately. |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 16 | #define IN_LIBXML |
| 17 | #include "libxml.h" |
| 18 | |
| 19 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 20 | |
| 21 | #include <string.h> |
| 22 | #include <stdio.h> |
| 23 | #include <libxml/xmlmemory.h> |
| 24 | #include <libxml/parser.h> |
| 25 | #include <libxml/parserInternals.h> |
| 26 | #include <libxml/hash.h> |
| 27 | #include <libxml/uri.h> |
| 28 | |
| 29 | #include <libxml/relaxng.h> |
| 30 | |
| 31 | #include <libxml/xmlschemastypes.h> |
| 32 | #include <libxml/xmlautomata.h> |
| 33 | #include <libxml/xmlregexp.h> |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 34 | #include <libxml/xmlschemastypes.h> |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | * The Relax-NG namespace |
| 38 | */ |
| 39 | static const xmlChar *xmlRelaxNGNs = (const xmlChar *) |
| 40 | "http://relaxng.org/ns/structure/1.0"; |
| 41 | |
| 42 | #define IS_RELAXNG(node, type) \ |
| 43 | ((node != NULL) && (node->ns != NULL) && \ |
| 44 | (xmlStrEqual(node->name, (const xmlChar *) type)) && \ |
| 45 | (xmlStrEqual(node->ns->href, xmlRelaxNGNs))) |
| 46 | |
| 47 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 48 | /* #define DEBUG 1 */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 49 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 50 | /* #define DEBUG_GRAMMAR 1 */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 51 | |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 52 | /* #define DEBUG_CONTENT 1 */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 53 | |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 54 | /* #define DEBUG_TYPE 1 */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 55 | |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 56 | /* #define DEBUG_VALID 1 */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 57 | |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 58 | /* #define DEBUG_INTERLEAVE 1 */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 59 | |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 60 | /* #define DEBUG_LIST 1 */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 61 | |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 62 | /* #define DEBUG_INCLUDE */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 63 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 64 | /* #define DEBUG_ERROR 1 */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 65 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 66 | /* #define DEBUG_COMPILE 1 */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 67 | |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 68 | /* #define DEBUG_PROGRESSIVE 1 */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 69 | |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 70 | #define MAX_ERROR 5 |
| 71 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 72 | #define TODO \ |
| 73 | xmlGenericError(xmlGenericErrorContext, \ |
| 74 | "Unimplemented block at %s:%d\n", \ |
| 75 | __FILE__, __LINE__); |
| 76 | |
| 77 | typedef struct _xmlRelaxNGSchema xmlRelaxNGSchema; |
| 78 | typedef xmlRelaxNGSchema *xmlRelaxNGSchemaPtr; |
| 79 | |
| 80 | typedef struct _xmlRelaxNGDefine xmlRelaxNGDefine; |
| 81 | typedef xmlRelaxNGDefine *xmlRelaxNGDefinePtr; |
| 82 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 83 | typedef struct _xmlRelaxNGDocument xmlRelaxNGDocument; |
| 84 | typedef xmlRelaxNGDocument *xmlRelaxNGDocumentPtr; |
| 85 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 86 | typedef struct _xmlRelaxNGInclude xmlRelaxNGInclude; |
| 87 | typedef xmlRelaxNGInclude *xmlRelaxNGIncludePtr; |
| 88 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 89 | typedef enum { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 90 | XML_RELAXNG_COMBINE_UNDEFINED = 0, /* undefined */ |
| 91 | XML_RELAXNG_COMBINE_CHOICE, /* choice */ |
| 92 | XML_RELAXNG_COMBINE_INTERLEAVE /* interleave */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 93 | } xmlRelaxNGCombine; |
| 94 | |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 95 | typedef enum { |
| 96 | XML_RELAXNG_CONTENT_ERROR = -1, |
| 97 | XML_RELAXNG_CONTENT_EMPTY = 0, |
| 98 | XML_RELAXNG_CONTENT_SIMPLE, |
| 99 | XML_RELAXNG_CONTENT_COMPLEX |
| 100 | } xmlRelaxNGContentType; |
| 101 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 102 | typedef struct _xmlRelaxNGGrammar xmlRelaxNGGrammar; |
| 103 | typedef xmlRelaxNGGrammar *xmlRelaxNGGrammarPtr; |
| 104 | |
| 105 | struct _xmlRelaxNGGrammar { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 106 | xmlRelaxNGGrammarPtr parent; /* the parent grammar if any */ |
| 107 | xmlRelaxNGGrammarPtr children; /* the children grammar if any */ |
| 108 | xmlRelaxNGGrammarPtr next; /* the next grammar if any */ |
| 109 | xmlRelaxNGDefinePtr start; /* <start> content */ |
| 110 | xmlRelaxNGCombine combine; /* the default combine value */ |
| 111 | xmlRelaxNGDefinePtr startList; /* list of <start> definitions */ |
| 112 | xmlHashTablePtr defs; /* define* */ |
| 113 | xmlHashTablePtr refs; /* references */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 117 | typedef enum { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 118 | XML_RELAXNG_NOOP = -1, /* a no operation from simplification */ |
| 119 | XML_RELAXNG_EMPTY = 0, /* an empty pattern */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 120 | XML_RELAXNG_NOT_ALLOWED, /* not allowed top */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 121 | XML_RELAXNG_EXCEPT, /* except present in nameclass defs */ |
| 122 | XML_RELAXNG_TEXT, /* textual content */ |
| 123 | XML_RELAXNG_ELEMENT, /* an element */ |
| 124 | XML_RELAXNG_DATATYPE, /* extenal data type definition */ |
| 125 | XML_RELAXNG_PARAM, /* extenal data type parameter */ |
| 126 | XML_RELAXNG_VALUE, /* value from an extenal data type definition */ |
| 127 | XML_RELAXNG_LIST, /* a list of patterns */ |
| 128 | XML_RELAXNG_ATTRIBUTE, /* an attrbute following a pattern */ |
| 129 | XML_RELAXNG_DEF, /* a definition */ |
| 130 | XML_RELAXNG_REF, /* reference to a definition */ |
| 131 | XML_RELAXNG_EXTERNALREF, /* reference to an external def */ |
| 132 | XML_RELAXNG_PARENTREF, /* reference to a def in the parent grammar */ |
| 133 | XML_RELAXNG_OPTIONAL, /* optional patterns */ |
| 134 | XML_RELAXNG_ZEROORMORE, /* zero or more non empty patterns */ |
| 135 | XML_RELAXNG_ONEORMORE, /* one or more non empty patterns */ |
| 136 | XML_RELAXNG_CHOICE, /* a choice between non empty patterns */ |
| 137 | XML_RELAXNG_GROUP, /* a pair/group of non empty patterns */ |
| 138 | XML_RELAXNG_INTERLEAVE, /* interleaving choice of non-empty patterns */ |
| 139 | XML_RELAXNG_START /* Used to keep track of starts on grammars */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 140 | } xmlRelaxNGType; |
| 141 | |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 142 | #define IS_NULLABLE (1 << 0) |
| 143 | #define IS_NOT_NULLABLE (1 << 1) |
| 144 | #define IS_INDETERMINIST (1 << 2) |
| 145 | #define IS_MIXED (1 << 3) |
| 146 | #define IS_TRIABLE (1 << 4) |
| 147 | #define IS_PROCESSED (1 << 5) |
| 148 | #define IS_COMPILABLE (1 << 6) |
| 149 | #define IS_NOT_COMPILABLE (1 << 7) |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 150 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 151 | struct _xmlRelaxNGDefine { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 152 | xmlRelaxNGType type; /* the type of definition */ |
| 153 | xmlNodePtr node; /* the node in the source */ |
| 154 | xmlChar *name; /* the element local name if present */ |
| 155 | xmlChar *ns; /* the namespace local name if present */ |
| 156 | xmlChar *value; /* value when available */ |
| 157 | void *data; /* data lib or specific pointer */ |
| 158 | xmlRelaxNGDefinePtr content; /* the expected content */ |
| 159 | xmlRelaxNGDefinePtr parent; /* the parent definition, if any */ |
| 160 | xmlRelaxNGDefinePtr next; /* list within grouping sequences */ |
| 161 | xmlRelaxNGDefinePtr attrs; /* list of attributes for elements */ |
| 162 | xmlRelaxNGDefinePtr nameClass; /* the nameClass definition if any */ |
| 163 | xmlRelaxNGDefinePtr nextHash; /* next define in defs/refs hash tables */ |
| 164 | short depth; /* used for the cycle detection */ |
| 165 | short dflags; /* define related flags */ |
| 166 | xmlRegexpPtr contModel; /* a compiled content model if available */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | /** |
| 170 | * _xmlRelaxNG: |
| 171 | * |
| 172 | * A RelaxNGs definition |
| 173 | */ |
| 174 | struct _xmlRelaxNG { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 175 | void *_private; /* unused by the library for users or bindings */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 176 | xmlRelaxNGGrammarPtr topgrammar; |
| 177 | xmlDocPtr doc; |
| 178 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 179 | int idref; /* requires idref checking */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 180 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 181 | xmlHashTablePtr defs; /* define */ |
| 182 | xmlHashTablePtr refs; /* references */ |
| 183 | xmlRelaxNGDocumentPtr documents; /* all the documents loaded */ |
| 184 | xmlRelaxNGIncludePtr includes; /* all the includes loaded */ |
| 185 | int defNr; /* number of defines used */ |
| 186 | xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 187 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 188 | }; |
| 189 | |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 190 | #define XML_RELAXNG_IN_ATTRIBUTE (1 << 0) |
| 191 | #define XML_RELAXNG_IN_ONEORMORE (1 << 1) |
| 192 | #define XML_RELAXNG_IN_LIST (1 << 2) |
| 193 | #define XML_RELAXNG_IN_DATAEXCEPT (1 << 3) |
| 194 | #define XML_RELAXNG_IN_START (1 << 4) |
| 195 | #define XML_RELAXNG_IN_OOMGROUP (1 << 5) |
| 196 | #define XML_RELAXNG_IN_OOMINTERLEAVE (1 << 6) |
| 197 | #define XML_RELAXNG_IN_EXTERNALREF (1 << 7) |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 198 | #define XML_RELAXNG_IN_ANYEXCEPT (1 << 8) |
| 199 | #define XML_RELAXNG_IN_NSEXCEPT (1 << 9) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 200 | |
| 201 | struct _xmlRelaxNGParserCtxt { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 202 | void *userData; /* user specific data block */ |
| 203 | xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */ |
| 204 | xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */ |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 205 | xmlStructuredErrorFunc serror; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 206 | xmlRelaxNGValidErr err; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 207 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 208 | xmlRelaxNGPtr schema; /* The schema in use */ |
| 209 | xmlRelaxNGGrammarPtr grammar; /* the current grammar */ |
| 210 | xmlRelaxNGGrammarPtr parentgrammar; /* the parent grammar */ |
| 211 | int flags; /* parser flags */ |
| 212 | int nbErrors; /* number of errors at parse time */ |
| 213 | int nbWarnings; /* number of warnings at parse time */ |
| 214 | const xmlChar *define; /* the current define scope */ |
| 215 | xmlRelaxNGDefinePtr def; /* the current define */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 216 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 217 | int nbInterleaves; |
| 218 | xmlHashTablePtr interleaves; /* keep track of all the interleaves */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 219 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 220 | xmlRelaxNGDocumentPtr documents; /* all the documents loaded */ |
| 221 | xmlRelaxNGIncludePtr includes; /* all the includes loaded */ |
| 222 | xmlChar *URL; |
| 223 | xmlDocPtr document; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 224 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 225 | int defNr; /* number of defines used */ |
| 226 | int defMax; /* number of defines aloocated */ |
| 227 | xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */ |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 228 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 229 | const char *buffer; |
| 230 | int size; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 231 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 232 | /* the document stack */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 233 | xmlRelaxNGDocumentPtr doc; /* Current parsed external ref */ |
| 234 | int docNr; /* Depth of the parsing stack */ |
| 235 | int docMax; /* Max depth of the parsing stack */ |
| 236 | xmlRelaxNGDocumentPtr *docTab; /* array of docs */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 237 | |
| 238 | /* the include stack */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 239 | xmlRelaxNGIncludePtr inc; /* Current parsed include */ |
| 240 | int incNr; /* Depth of the include parsing stack */ |
| 241 | int incMax; /* Max depth of the parsing stack */ |
| 242 | xmlRelaxNGIncludePtr *incTab; /* array of incs */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 243 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 244 | int idref; /* requires idref checking */ |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 245 | |
| 246 | /* used to compile content models */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 247 | xmlAutomataPtr am; /* the automata */ |
| 248 | xmlAutomataStatePtr state; /* used to build the automata */ |
Daniel Veillard | 03c2f0a | 2004-01-25 19:54:59 +0000 | [diff] [blame] | 249 | |
| 250 | int crng; /* compact syntax and other flags */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 251 | }; |
| 252 | |
| 253 | #define FLAGS_IGNORABLE 1 |
| 254 | #define FLAGS_NEGATIVE 2 |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 255 | #define FLAGS_MIXED_CONTENT 4 |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 256 | |
| 257 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 258 | * xmlRelaxNGInterleaveGroup: |
| 259 | * |
| 260 | * A RelaxNGs partition set associated to lists of definitions |
| 261 | */ |
| 262 | typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup; |
| 263 | typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr; |
| 264 | struct _xmlRelaxNGInterleaveGroup { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 265 | xmlRelaxNGDefinePtr rule; /* the rule to satisfy */ |
| 266 | xmlRelaxNGDefinePtr *defs; /* the array of element definitions */ |
| 267 | xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 268 | }; |
| 269 | |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 270 | #define IS_DETERMINIST 1 |
| 271 | #define IS_NEEDCHECK 2 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 272 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 273 | /** |
| 274 | * xmlRelaxNGPartitions: |
| 275 | * |
| 276 | * A RelaxNGs partition associated to an interleave group |
| 277 | */ |
| 278 | typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition; |
| 279 | typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr; |
| 280 | struct _xmlRelaxNGPartition { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 281 | int nbgroups; /* number of groups in the partitions */ |
| 282 | xmlHashTablePtr triage; /* hash table used to direct nodes to the |
| 283 | * right group when possible */ |
| 284 | int flags; /* determinist ? */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 285 | xmlRelaxNGInterleaveGroupPtr *groups; |
| 286 | }; |
| 287 | |
| 288 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 289 | * xmlRelaxNGValidState: |
| 290 | * |
| 291 | * A RelaxNGs validation state |
| 292 | */ |
| 293 | #define MAX_ATTR 20 |
| 294 | typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState; |
| 295 | typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr; |
| 296 | struct _xmlRelaxNGValidState { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 297 | xmlNodePtr node; /* the current node */ |
| 298 | xmlNodePtr seq; /* the sequence of children left to validate */ |
| 299 | int nbAttrs; /* the number of attributes */ |
| 300 | int maxAttrs; /* the size of attrs */ |
| 301 | int nbAttrLeft; /* the number of attributes left to validate */ |
| 302 | xmlChar *value; /* the value when operating on string */ |
| 303 | xmlChar *endvalue; /* the end value when operating on string */ |
| 304 | xmlAttrPtr *attrs; /* the array of attributes */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 305 | }; |
| 306 | |
| 307 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 308 | * xmlRelaxNGStates: |
| 309 | * |
| 310 | * A RelaxNGs container for validation state |
| 311 | */ |
| 312 | typedef struct _xmlRelaxNGStates xmlRelaxNGStates; |
| 313 | typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr; |
| 314 | struct _xmlRelaxNGStates { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 315 | int nbState; /* the number of states */ |
| 316 | int maxState; /* the size of the array */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 317 | xmlRelaxNGValidStatePtr *tabState; |
| 318 | }; |
| 319 | |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 320 | #define ERROR_IS_DUP 1 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 321 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 322 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 323 | * xmlRelaxNGValidError: |
| 324 | * |
| 325 | * A RelaxNGs validation error |
| 326 | */ |
| 327 | typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError; |
| 328 | typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr; |
| 329 | struct _xmlRelaxNGValidError { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 330 | xmlRelaxNGValidErr err; /* the error number */ |
| 331 | int flags; /* flags */ |
| 332 | xmlNodePtr node; /* the current node */ |
| 333 | xmlNodePtr seq; /* the current child */ |
| 334 | const xmlChar *arg1; /* first arg */ |
| 335 | const xmlChar *arg2; /* second arg */ |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 336 | }; |
| 337 | |
| 338 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 339 | * xmlRelaxNGValidCtxt: |
| 340 | * |
| 341 | * A RelaxNGs validation context |
| 342 | */ |
| 343 | |
| 344 | struct _xmlRelaxNGValidCtxt { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 345 | void *userData; /* user specific data block */ |
| 346 | xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */ |
| 347 | xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */ |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 348 | xmlStructuredErrorFunc serror; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 349 | int nbErrors; /* number of errors in validation */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 350 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 351 | xmlRelaxNGPtr schema; /* The schema in use */ |
| 352 | xmlDocPtr doc; /* the document being validated */ |
| 353 | int flags; /* validation flags */ |
| 354 | int depth; /* validation depth */ |
| 355 | int idref; /* requires idref checking */ |
| 356 | int errNo; /* the first error found */ |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 357 | |
| 358 | /* |
| 359 | * Errors accumulated in branches may have to be stacked to be |
| 360 | * provided back when it's sure they affect validation. |
| 361 | */ |
| 362 | xmlRelaxNGValidErrorPtr err; /* Last error */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 363 | int errNr; /* Depth of the error stack */ |
| 364 | int errMax; /* Max depth of the error stack */ |
| 365 | xmlRelaxNGValidErrorPtr errTab; /* stack of errors */ |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 366 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 367 | xmlRelaxNGValidStatePtr state; /* the current validation state */ |
| 368 | xmlRelaxNGStatesPtr states; /* the accumulated state list */ |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 369 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 370 | xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */ |
| 371 | int freeStatesNr; |
| 372 | int freeStatesMax; |
| 373 | xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */ |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 374 | |
| 375 | /* |
| 376 | * This is used for "progressive" validation |
| 377 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 378 | xmlRegExecCtxtPtr elem; /* the current element regexp */ |
| 379 | int elemNr; /* the number of element validated */ |
| 380 | int elemMax; /* the max depth of elements */ |
| 381 | xmlRegExecCtxtPtr *elemTab; /* the stack of regexp runtime */ |
| 382 | int pstate; /* progressive state */ |
| 383 | xmlNodePtr pnode; /* the current node */ |
| 384 | xmlRelaxNGDefinePtr pdef; /* the non-streamable definition */ |
| 385 | int perr; /* signal error in content model |
| 386 | * outside the regexp */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 387 | }; |
| 388 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 389 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 390 | * xmlRelaxNGInclude: |
| 391 | * |
| 392 | * Structure associated to a RelaxNGs document element |
| 393 | */ |
| 394 | struct _xmlRelaxNGInclude { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 395 | xmlRelaxNGIncludePtr next; /* keep a chain of includes */ |
| 396 | xmlChar *href; /* the normalized href value */ |
| 397 | xmlDocPtr doc; /* the associated XML document */ |
| 398 | xmlRelaxNGDefinePtr content; /* the definitions */ |
| 399 | xmlRelaxNGPtr schema; /* the schema */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 400 | }; |
| 401 | |
| 402 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 403 | * xmlRelaxNGDocument: |
| 404 | * |
| 405 | * Structure associated to a RelaxNGs document element |
| 406 | */ |
| 407 | struct _xmlRelaxNGDocument { |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 408 | xmlRelaxNGDocumentPtr next; /* keep a chain of documents */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 409 | xmlChar *href; /* the normalized href value */ |
| 410 | xmlDocPtr doc; /* the associated XML document */ |
| 411 | xmlRelaxNGDefinePtr content; /* the definitions */ |
| 412 | xmlRelaxNGPtr schema; /* the schema */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 413 | }; |
| 414 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 415 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 416 | /************************************************************************ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 417 | * * |
| 418 | * Some factorized error routines * |
| 419 | * * |
| 420 | ************************************************************************/ |
| 421 | |
| 422 | /** |
| 423 | * xmlRngPErrMemory: |
| 424 | * @ctxt: an Relax-NG parser context |
| 425 | * @extra: extra informations |
| 426 | * |
| 427 | * Handle a redefinition of attribute error |
| 428 | */ |
| 429 | static void |
| 430 | xmlRngPErrMemory(xmlRelaxNGParserCtxtPtr ctxt, const char *extra) |
| 431 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 432 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 433 | xmlGenericErrorFunc channel = NULL; |
| 434 | void *data = NULL; |
| 435 | |
| 436 | if (ctxt != NULL) { |
| 437 | channel = ctxt->error; |
| 438 | data = ctxt->userData; |
| 439 | ctxt->nbErrors++; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 440 | schannel = ctxt->serror; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 441 | } |
| 442 | if (extra) |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 443 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 444 | NULL, NULL, XML_FROM_RELAXNGP, |
| 445 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra, |
| 446 | NULL, NULL, 0, 0, |
| 447 | "Memory allocation failed : %s\n", extra); |
| 448 | else |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 449 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 450 | NULL, NULL, XML_FROM_RELAXNGP, |
| 451 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL, |
| 452 | NULL, NULL, 0, 0, "Memory allocation failed\n"); |
| 453 | } |
| 454 | |
| 455 | /** |
| 456 | * xmlRngVErrMemory: |
| 457 | * @ctxt: a Relax-NG validation context |
| 458 | * @extra: extra informations |
| 459 | * |
| 460 | * Handle a redefinition of attribute error |
| 461 | */ |
| 462 | static void |
| 463 | xmlRngVErrMemory(xmlRelaxNGValidCtxtPtr ctxt, const char *extra) |
| 464 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 465 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 466 | xmlGenericErrorFunc channel = NULL; |
| 467 | void *data = NULL; |
| 468 | |
| 469 | if (ctxt != NULL) { |
| 470 | channel = ctxt->error; |
| 471 | data = ctxt->userData; |
| 472 | ctxt->nbErrors++; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 473 | schannel = ctxt->serror; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 474 | } |
| 475 | if (extra) |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 476 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 477 | NULL, NULL, XML_FROM_RELAXNGV, |
| 478 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra, |
| 479 | NULL, NULL, 0, 0, |
| 480 | "Memory allocation failed : %s\n", extra); |
| 481 | else |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 482 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 483 | NULL, NULL, XML_FROM_RELAXNGV, |
| 484 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL, |
| 485 | NULL, NULL, 0, 0, "Memory allocation failed\n"); |
| 486 | } |
| 487 | |
| 488 | /** |
| 489 | * xmlRngPErr: |
| 490 | * @ctxt: a Relax-NG parser context |
| 491 | * @node: the node raising the error |
| 492 | * @error: the error code |
| 493 | * @msg: message |
| 494 | * @str1: extra info |
| 495 | * @str2: extra info |
| 496 | * |
| 497 | * Handle a Relax NG Parsing error |
| 498 | */ |
| 499 | static void |
| 500 | xmlRngPErr(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, int error, |
| 501 | const char *msg, const xmlChar * str1, const xmlChar * str2) |
| 502 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 503 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 504 | xmlGenericErrorFunc channel = NULL; |
| 505 | void *data = NULL; |
| 506 | |
| 507 | if (ctxt != NULL) { |
| 508 | channel = ctxt->error; |
| 509 | data = ctxt->userData; |
| 510 | ctxt->nbErrors++; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 511 | schannel = ctxt->serror; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 512 | } |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 513 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 514 | NULL, node, XML_FROM_RELAXNGP, |
| 515 | error, XML_ERR_ERROR, NULL, 0, |
| 516 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
| 517 | msg, str1, str2); |
| 518 | } |
| 519 | |
| 520 | /** |
| 521 | * xmlRngVErr: |
| 522 | * @ctxt: a Relax-NG validation context |
| 523 | * @node: the node raising the error |
| 524 | * @error: the error code |
| 525 | * @msg: message |
| 526 | * @str1: extra info |
| 527 | * @str2: extra info |
| 528 | * |
| 529 | * Handle a Relax NG Validation error |
| 530 | */ |
| 531 | static void |
| 532 | xmlRngVErr(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node, int error, |
| 533 | const char *msg, const xmlChar * str1, const xmlChar * str2) |
| 534 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 535 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 536 | xmlGenericErrorFunc channel = NULL; |
| 537 | void *data = NULL; |
| 538 | |
| 539 | if (ctxt != NULL) { |
| 540 | channel = ctxt->error; |
| 541 | data = ctxt->userData; |
| 542 | ctxt->nbErrors++; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 543 | schannel = ctxt->serror; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 544 | } |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 545 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 546 | NULL, node, XML_FROM_RELAXNGV, |
| 547 | error, XML_ERR_ERROR, NULL, 0, |
| 548 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
| 549 | msg, str1, str2); |
| 550 | } |
| 551 | |
| 552 | /************************************************************************ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 553 | * * |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 554 | * Preliminary type checking interfaces * |
| 555 | * * |
| 556 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 557 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 558 | /** |
| 559 | * xmlRelaxNGTypeHave: |
| 560 | * @data: data needed for the library |
| 561 | * @type: the type name |
| 562 | * @value: the value to check |
| 563 | * |
| 564 | * Function provided by a type library to check if a type is exported |
| 565 | * |
| 566 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 567 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 568 | typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar * type); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 569 | |
| 570 | /** |
| 571 | * xmlRelaxNGTypeCheck: |
| 572 | * @data: data needed for the library |
| 573 | * @type: the type name |
| 574 | * @value: the value to check |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 575 | * @result: place to store the result if needed |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 576 | * |
| 577 | * Function provided by a type library to check if a value match a type |
| 578 | * |
| 579 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 580 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 581 | typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar * type, |
| 582 | const xmlChar * value, void **result, |
| 583 | xmlNodePtr node); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 584 | |
| 585 | /** |
| 586 | * xmlRelaxNGFacetCheck: |
| 587 | * @data: data needed for the library |
| 588 | * @type: the type name |
| 589 | * @facet: the facet name |
| 590 | * @val: the facet value |
| 591 | * @strval: the string value |
| 592 | * @value: the value to check |
| 593 | * |
| 594 | * Function provided by a type library to check a value facet |
| 595 | * |
| 596 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 597 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 598 | typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar * type, |
| 599 | const xmlChar * facet, |
| 600 | const xmlChar * val, |
| 601 | const xmlChar * strval, void *value); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 602 | |
| 603 | /** |
| 604 | * xmlRelaxNGTypeFree: |
| 605 | * @data: data needed for the library |
| 606 | * @result: the value to free |
| 607 | * |
| 608 | * Function provided by a type library to free a returned result |
| 609 | */ |
| 610 | typedef void (*xmlRelaxNGTypeFree) (void *data, void *result); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 611 | |
| 612 | /** |
| 613 | * xmlRelaxNGTypeCompare: |
| 614 | * @data: data needed for the library |
| 615 | * @type: the type name |
| 616 | * @value1: the first value |
| 617 | * @value2: the second value |
| 618 | * |
| 619 | * Function provided by a type library to compare two values accordingly |
| 620 | * to a type. |
| 621 | * |
| 622 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 623 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 624 | typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar * type, |
| 625 | const xmlChar * value1, |
| 626 | xmlNodePtr ctxt1, |
| 627 | void *comp1, |
| 628 | const xmlChar * value2, |
| 629 | xmlNodePtr ctxt2); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 630 | typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary; |
| 631 | typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr; |
| 632 | struct _xmlRelaxNGTypeLibrary { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 633 | const xmlChar *namespace; /* the datatypeLibrary value */ |
| 634 | void *data; /* data needed for the library */ |
| 635 | xmlRelaxNGTypeHave have; /* the export function */ |
| 636 | xmlRelaxNGTypeCheck check; /* the checking function */ |
| 637 | xmlRelaxNGTypeCompare comp; /* the compare function */ |
| 638 | xmlRelaxNGFacetCheck facet; /* the facet check function */ |
| 639 | xmlRelaxNGTypeFree freef; /* the freeing function */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 640 | }; |
| 641 | |
| 642 | /************************************************************************ |
| 643 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 644 | * Allocation functions * |
| 645 | * * |
| 646 | ************************************************************************/ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 647 | static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar); |
| 648 | static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 649 | static void xmlRelaxNGNormExtSpace(xmlChar * value); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 650 | static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 651 | static int xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt |
| 652 | ATTRIBUTE_UNUSED, |
| 653 | xmlRelaxNGValidStatePtr state1, |
| 654 | xmlRelaxNGValidStatePtr state2); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 655 | static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 656 | xmlRelaxNGValidStatePtr state); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 657 | |
| 658 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 659 | * xmlRelaxNGFreeDocument: |
| 660 | * @docu: a document structure |
| 661 | * |
| 662 | * Deallocate a RelaxNG document structure. |
| 663 | */ |
| 664 | static void |
| 665 | xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu) |
| 666 | { |
| 667 | if (docu == NULL) |
| 668 | return; |
| 669 | |
| 670 | if (docu->href != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 671 | xmlFree(docu->href); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 672 | if (docu->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 673 | xmlFreeDoc(docu->doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 674 | if (docu->schema != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 675 | xmlRelaxNGFreeInnerSchema(docu->schema); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 676 | xmlFree(docu); |
| 677 | } |
| 678 | |
| 679 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 680 | * xmlRelaxNGFreeDocumentList: |
| 681 | * @docu: a list of document structure |
| 682 | * |
| 683 | * Deallocate a RelaxNG document structures. |
| 684 | */ |
| 685 | static void |
| 686 | xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu) |
| 687 | { |
| 688 | xmlRelaxNGDocumentPtr next; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 689 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 690 | while (docu != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 691 | next = docu->next; |
| 692 | xmlRelaxNGFreeDocument(docu); |
| 693 | docu = next; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 694 | } |
| 695 | } |
| 696 | |
| 697 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 698 | * xmlRelaxNGFreeInclude: |
| 699 | * @incl: a include structure |
| 700 | * |
| 701 | * Deallocate a RelaxNG include structure. |
| 702 | */ |
| 703 | static void |
| 704 | xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl) |
| 705 | { |
| 706 | if (incl == NULL) |
| 707 | return; |
| 708 | |
| 709 | if (incl->href != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 710 | xmlFree(incl->href); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 711 | if (incl->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 712 | xmlFreeDoc(incl->doc); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 713 | if (incl->schema != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 714 | xmlRelaxNGFree(incl->schema); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 715 | xmlFree(incl); |
| 716 | } |
| 717 | |
| 718 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 719 | * xmlRelaxNGFreeIncludeList: |
| 720 | * @incl: a include structure list |
| 721 | * |
| 722 | * Deallocate a RelaxNG include structure. |
| 723 | */ |
| 724 | static void |
| 725 | xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl) |
| 726 | { |
| 727 | xmlRelaxNGIncludePtr next; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 728 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 729 | while (incl != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 730 | next = incl->next; |
| 731 | xmlRelaxNGFreeInclude(incl); |
| 732 | incl = next; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 733 | } |
| 734 | } |
| 735 | |
| 736 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 737 | * xmlRelaxNGNewRelaxNG: |
| 738 | * @ctxt: a Relax-NG validation context (optional) |
| 739 | * |
| 740 | * Allocate a new RelaxNG structure. |
| 741 | * |
| 742 | * Returns the newly allocated structure or NULL in case or error |
| 743 | */ |
| 744 | static xmlRelaxNGPtr |
| 745 | xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt) |
| 746 | { |
| 747 | xmlRelaxNGPtr ret; |
| 748 | |
| 749 | ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG)); |
| 750 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 751 | xmlRngPErrMemory(ctxt, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 752 | return (NULL); |
| 753 | } |
| 754 | memset(ret, 0, sizeof(xmlRelaxNG)); |
| 755 | |
| 756 | return (ret); |
| 757 | } |
| 758 | |
| 759 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 760 | * xmlRelaxNGFreeInnerSchema: |
| 761 | * @schema: a schema structure |
| 762 | * |
| 763 | * Deallocate a RelaxNG schema structure. |
| 764 | */ |
| 765 | static void |
| 766 | xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema) |
| 767 | { |
| 768 | if (schema == NULL) |
| 769 | return; |
| 770 | |
| 771 | if (schema->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 772 | xmlFreeDoc(schema->doc); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 773 | if (schema->defTab != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 774 | int i; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 775 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 776 | for (i = 0; i < schema->defNr; i++) |
| 777 | xmlRelaxNGFreeDefine(schema->defTab[i]); |
| 778 | xmlFree(schema->defTab); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | xmlFree(schema); |
| 782 | } |
| 783 | |
| 784 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 785 | * xmlRelaxNGFree: |
| 786 | * @schema: a schema structure |
| 787 | * |
| 788 | * Deallocate a RelaxNG structure. |
| 789 | */ |
| 790 | void |
| 791 | xmlRelaxNGFree(xmlRelaxNGPtr schema) |
| 792 | { |
| 793 | if (schema == NULL) |
| 794 | return; |
| 795 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 796 | if (schema->topgrammar != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 797 | xmlRelaxNGFreeGrammar(schema->topgrammar); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 798 | if (schema->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 799 | xmlFreeDoc(schema->doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 800 | if (schema->documents != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 801 | xmlRelaxNGFreeDocumentList(schema->documents); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 802 | if (schema->includes != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 803 | xmlRelaxNGFreeIncludeList(schema->includes); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 804 | if (schema->defTab != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 805 | int i; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 806 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 807 | for (i = 0; i < schema->defNr; i++) |
| 808 | xmlRelaxNGFreeDefine(schema->defTab[i]); |
| 809 | xmlFree(schema->defTab); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 810 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 811 | |
| 812 | xmlFree(schema); |
| 813 | } |
| 814 | |
| 815 | /** |
| 816 | * xmlRelaxNGNewGrammar: |
| 817 | * @ctxt: a Relax-NG validation context (optional) |
| 818 | * |
| 819 | * Allocate a new RelaxNG grammar. |
| 820 | * |
| 821 | * Returns the newly allocated structure or NULL in case or error |
| 822 | */ |
| 823 | static xmlRelaxNGGrammarPtr |
| 824 | xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt) |
| 825 | { |
| 826 | xmlRelaxNGGrammarPtr ret; |
| 827 | |
| 828 | ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar)); |
| 829 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 830 | xmlRngPErrMemory(ctxt, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 831 | return (NULL); |
| 832 | } |
| 833 | memset(ret, 0, sizeof(xmlRelaxNGGrammar)); |
| 834 | |
| 835 | return (ret); |
| 836 | } |
| 837 | |
| 838 | /** |
| 839 | * xmlRelaxNGFreeGrammar: |
| 840 | * @grammar: a grammar structure |
| 841 | * |
| 842 | * Deallocate a RelaxNG grammar structure. |
| 843 | */ |
| 844 | static void |
| 845 | xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar) |
| 846 | { |
| 847 | if (grammar == NULL) |
| 848 | return; |
| 849 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 850 | if (grammar->children != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 851 | xmlRelaxNGFreeGrammar(grammar->children); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 852 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 853 | if (grammar->next != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 854 | xmlRelaxNGFreeGrammar(grammar->next); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 855 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 856 | if (grammar->refs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 857 | xmlHashFree(grammar->refs, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 858 | } |
| 859 | if (grammar->defs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 860 | xmlHashFree(grammar->defs, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | xmlFree(grammar); |
| 864 | } |
| 865 | |
| 866 | /** |
| 867 | * xmlRelaxNGNewDefine: |
| 868 | * @ctxt: a Relax-NG validation context |
| 869 | * @node: the node in the input document. |
| 870 | * |
| 871 | * Allocate a new RelaxNG define. |
| 872 | * |
| 873 | * Returns the newly allocated structure or NULL in case or error |
| 874 | */ |
| 875 | static xmlRelaxNGDefinePtr |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 876 | xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 877 | { |
| 878 | xmlRelaxNGDefinePtr ret; |
| 879 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 880 | if (ctxt->defMax == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 881 | ctxt->defMax = 16; |
| 882 | ctxt->defNr = 0; |
| 883 | ctxt->defTab = (xmlRelaxNGDefinePtr *) |
| 884 | xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr)); |
| 885 | if (ctxt->defTab == NULL) { |
| 886 | xmlRngPErrMemory(ctxt, "allocating define\n"); |
| 887 | return (NULL); |
| 888 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 889 | } else if (ctxt->defMax <= ctxt->defNr) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 890 | xmlRelaxNGDefinePtr *tmp; |
| 891 | |
| 892 | ctxt->defMax *= 2; |
| 893 | tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab, |
| 894 | ctxt->defMax * |
| 895 | sizeof |
| 896 | (xmlRelaxNGDefinePtr)); |
| 897 | if (tmp == NULL) { |
| 898 | xmlRngPErrMemory(ctxt, "allocating define\n"); |
| 899 | return (NULL); |
| 900 | } |
| 901 | ctxt->defTab = tmp; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 902 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 903 | ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine)); |
| 904 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 905 | xmlRngPErrMemory(ctxt, "allocating define\n"); |
| 906 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 907 | } |
| 908 | memset(ret, 0, sizeof(xmlRelaxNGDefine)); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 909 | ctxt->defTab[ctxt->defNr++] = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 910 | ret->node = node; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 911 | ret->depth = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 912 | return (ret); |
| 913 | } |
| 914 | |
| 915 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 916 | * xmlRelaxNGFreePartition: |
| 917 | * @partitions: a partition set structure |
| 918 | * |
| 919 | * Deallocate RelaxNG partition set structures. |
| 920 | */ |
| 921 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 922 | xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions) |
| 923 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 924 | xmlRelaxNGInterleaveGroupPtr group; |
| 925 | int j; |
| 926 | |
| 927 | if (partitions != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 928 | if (partitions->groups != NULL) { |
| 929 | for (j = 0; j < partitions->nbgroups; j++) { |
| 930 | group = partitions->groups[j]; |
| 931 | if (group != NULL) { |
| 932 | if (group->defs != NULL) |
| 933 | xmlFree(group->defs); |
| 934 | if (group->attrs != NULL) |
| 935 | xmlFree(group->attrs); |
| 936 | xmlFree(group); |
| 937 | } |
| 938 | } |
| 939 | xmlFree(partitions->groups); |
| 940 | } |
| 941 | if (partitions->triage != NULL) { |
| 942 | xmlHashFree(partitions->triage, NULL); |
| 943 | } |
| 944 | xmlFree(partitions); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 945 | } |
| 946 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 947 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 948 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 949 | * xmlRelaxNGFreeDefine: |
| 950 | * @define: a define structure |
| 951 | * |
| 952 | * Deallocate a RelaxNG define structure. |
| 953 | */ |
| 954 | static void |
| 955 | xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define) |
| 956 | { |
| 957 | if (define == NULL) |
| 958 | return; |
| 959 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 960 | if ((define->type == XML_RELAXNG_VALUE) && (define->attrs != NULL)) { |
| 961 | xmlRelaxNGTypeLibraryPtr lib; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 962 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 963 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
| 964 | if ((lib != NULL) && (lib->freef != NULL)) |
| 965 | lib->freef(lib->data, (void *) define->attrs); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 966 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 967 | if ((define->data != NULL) && (define->type == XML_RELAXNG_INTERLEAVE)) |
| 968 | xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data); |
| 969 | if ((define->data != NULL) && (define->type == XML_RELAXNG_CHOICE)) |
| 970 | xmlHashFree((xmlHashTablePtr) define->data, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 971 | if (define->name != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 972 | xmlFree(define->name); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 973 | if (define->ns != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 974 | xmlFree(define->ns); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 975 | if (define->value != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 976 | xmlFree(define->value); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 977 | if (define->contModel != NULL) |
| 978 | xmlRegFreeRegexp(define->contModel); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 979 | xmlFree(define); |
| 980 | } |
| 981 | |
| 982 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 983 | * xmlRelaxNGNewStates: |
| 984 | * @ctxt: a Relax-NG validation context |
| 985 | * @size: the default size for the container |
| 986 | * |
| 987 | * Allocate a new RelaxNG validation state container |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 988 | * |
| 989 | * Returns the newly allocated structure or NULL in case or error |
| 990 | */ |
| 991 | static xmlRelaxNGStatesPtr |
| 992 | xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size) |
| 993 | { |
| 994 | xmlRelaxNGStatesPtr ret; |
| 995 | |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 996 | if ((ctxt != NULL) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 997 | (ctxt->freeState != NULL) && (ctxt->freeStatesNr > 0)) { |
| 998 | ctxt->freeStatesNr--; |
| 999 | ret = ctxt->freeStates[ctxt->freeStatesNr]; |
| 1000 | ret->nbState = 0; |
| 1001 | return (ret); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1002 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1003 | if (size < 16) |
| 1004 | size = 16; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1005 | |
| 1006 | ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) + |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1007 | (size - |
| 1008 | 1) * |
| 1009 | sizeof(xmlRelaxNGValidStatePtr)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1010 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1011 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1012 | return (NULL); |
| 1013 | } |
| 1014 | ret->nbState = 0; |
| 1015 | ret->maxState = size; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1016 | ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc((size) * |
| 1017 | sizeof |
| 1018 | (xmlRelaxNGValidStatePtr)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1019 | if (ret->tabState == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1020 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1021 | xmlFree(ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1022 | return (NULL); |
| 1023 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1024 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | /** |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1028 | * xmlRelaxNGAddStateUniq: |
| 1029 | * @ctxt: a Relax-NG validation context |
| 1030 | * @states: the states container |
| 1031 | * @state: the validation state |
| 1032 | * |
| 1033 | * Add a RelaxNG validation state to the container without checking |
| 1034 | * for unicity. |
| 1035 | * |
| 1036 | * Return 1 in case of success and 0 if this is a duplicate and -1 on error |
| 1037 | */ |
| 1038 | static int |
| 1039 | xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1040 | xmlRelaxNGStatesPtr states, |
| 1041 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1042 | { |
| 1043 | if (state == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1044 | return (-1); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1045 | } |
| 1046 | if (states->nbState >= states->maxState) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1047 | xmlRelaxNGValidStatePtr *tmp; |
| 1048 | int size; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1049 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1050 | size = states->maxState * 2; |
| 1051 | tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState, |
| 1052 | (size) * |
| 1053 | sizeof |
| 1054 | (xmlRelaxNGValidStatePtr)); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1055 | if (tmp == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1056 | xmlRngVErrMemory(ctxt, "adding states\n"); |
| 1057 | return (-1); |
| 1058 | } |
| 1059 | states->tabState = tmp; |
| 1060 | states->maxState = size; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1061 | } |
| 1062 | states->tabState[states->nbState++] = state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1063 | return (1); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1067 | * xmlRelaxNGAddState: |
| 1068 | * @ctxt: a Relax-NG validation context |
| 1069 | * @states: the states container |
| 1070 | * @state: the validation state |
| 1071 | * |
| 1072 | * Add a RelaxNG validation state to the container |
| 1073 | * |
| 1074 | * Return 1 in case of success and 0 if this is a duplicate and -1 on error |
| 1075 | */ |
| 1076 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1077 | xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt, |
| 1078 | xmlRelaxNGStatesPtr states, |
| 1079 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1080 | { |
| 1081 | int i; |
| 1082 | |
| 1083 | if (state == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1084 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1085 | } |
| 1086 | if (states->nbState >= states->maxState) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1087 | xmlRelaxNGValidStatePtr *tmp; |
| 1088 | int size; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1089 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1090 | size = states->maxState * 2; |
| 1091 | tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState, |
| 1092 | (size) * |
| 1093 | sizeof |
| 1094 | (xmlRelaxNGValidStatePtr)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1095 | if (tmp == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1096 | xmlRngVErrMemory(ctxt, "adding states\n"); |
| 1097 | return (-1); |
| 1098 | } |
| 1099 | states->tabState = tmp; |
| 1100 | states->maxState = size; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1101 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1102 | for (i = 0; i < states->nbState; i++) { |
| 1103 | if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) { |
| 1104 | xmlRelaxNGFreeValidState(ctxt, state); |
| 1105 | return (0); |
| 1106 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1107 | } |
| 1108 | states->tabState[states->nbState++] = state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1109 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | /** |
| 1113 | * xmlRelaxNGFreeStates: |
| 1114 | * @ctxt: a Relax-NG validation context |
| 1115 | * @states: teh container |
| 1116 | * |
| 1117 | * Free a RelaxNG validation state container |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1118 | */ |
| 1119 | static void |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1120 | xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1121 | xmlRelaxNGStatesPtr states) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1122 | { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1123 | if (states == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1124 | return; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1125 | if ((ctxt != NULL) && (ctxt->freeStates == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1126 | ctxt->freeStatesMax = 40; |
| 1127 | ctxt->freeStatesNr = 0; |
| 1128 | ctxt->freeStates = (xmlRelaxNGStatesPtr *) |
| 1129 | xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr)); |
| 1130 | if (ctxt->freeStates == NULL) { |
| 1131 | xmlRngVErrMemory(ctxt, "storing states\n"); |
| 1132 | } |
| 1133 | } else if ((ctxt != NULL) |
| 1134 | && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) { |
| 1135 | xmlRelaxNGStatesPtr *tmp; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1136 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1137 | tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates, |
| 1138 | 2 * ctxt->freeStatesMax * |
| 1139 | sizeof |
| 1140 | (xmlRelaxNGStatesPtr)); |
| 1141 | if (tmp == NULL) { |
| 1142 | xmlRngVErrMemory(ctxt, "storing states\n"); |
| 1143 | xmlFree(states->tabState); |
| 1144 | xmlFree(states); |
| 1145 | return; |
| 1146 | } |
| 1147 | ctxt->freeStates = tmp; |
| 1148 | ctxt->freeStatesMax *= 2; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1149 | } |
| 1150 | if ((ctxt == NULL) || (ctxt->freeState == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1151 | xmlFree(states->tabState); |
| 1152 | xmlFree(states); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1153 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1154 | ctxt->freeStates[ctxt->freeStatesNr++] = states; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1155 | } |
| 1156 | } |
| 1157 | |
| 1158 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1159 | * xmlRelaxNGNewValidState: |
| 1160 | * @ctxt: a Relax-NG validation context |
| 1161 | * @node: the current node or NULL for the document |
| 1162 | * |
| 1163 | * Allocate a new RelaxNG validation state |
| 1164 | * |
| 1165 | * Returns the newly allocated structure or NULL in case or error |
| 1166 | */ |
| 1167 | static xmlRelaxNGValidStatePtr |
| 1168 | xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node) |
| 1169 | { |
| 1170 | xmlRelaxNGValidStatePtr ret; |
| 1171 | xmlAttrPtr attr; |
| 1172 | xmlAttrPtr attrs[MAX_ATTR]; |
| 1173 | int nbAttrs = 0; |
| 1174 | xmlNodePtr root = NULL; |
| 1175 | |
| 1176 | if (node == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1177 | root = xmlDocGetRootElement(ctxt->doc); |
| 1178 | if (root == NULL) |
| 1179 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1180 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1181 | attr = node->properties; |
| 1182 | while (attr != NULL) { |
| 1183 | if (nbAttrs < MAX_ATTR) |
| 1184 | attrs[nbAttrs++] = attr; |
| 1185 | else |
| 1186 | nbAttrs++; |
| 1187 | attr = attr->next; |
| 1188 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1189 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1190 | if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) { |
| 1191 | ctxt->freeState->nbState--; |
| 1192 | ret = ctxt->freeState->tabState[ctxt->freeState->nbState]; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1193 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1194 | ret = |
| 1195 | (xmlRelaxNGValidStatePtr) |
| 1196 | xmlMalloc(sizeof(xmlRelaxNGValidState)); |
| 1197 | if (ret == NULL) { |
| 1198 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1199 | return (NULL); |
| 1200 | } |
| 1201 | memset(ret, 0, sizeof(xmlRelaxNGValidState)); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1202 | } |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 1203 | ret->value = NULL; |
| 1204 | ret->endvalue = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1205 | if (node == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1206 | ret->node = (xmlNodePtr) ctxt->doc; |
| 1207 | ret->seq = root; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1208 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1209 | ret->node = node; |
| 1210 | ret->seq = node->children; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1211 | } |
| 1212 | ret->nbAttrs = 0; |
| 1213 | if (nbAttrs > 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1214 | if (ret->attrs == NULL) { |
| 1215 | if (nbAttrs < 4) |
| 1216 | ret->maxAttrs = 4; |
| 1217 | else |
| 1218 | ret->maxAttrs = nbAttrs; |
| 1219 | ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs * |
| 1220 | sizeof(xmlAttrPtr)); |
| 1221 | if (ret->attrs == NULL) { |
| 1222 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1223 | return (ret); |
| 1224 | } |
| 1225 | } else if (ret->maxAttrs < nbAttrs) { |
| 1226 | xmlAttrPtr *tmp; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1227 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1228 | tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs * |
| 1229 | sizeof(xmlAttrPtr)); |
| 1230 | if (tmp == NULL) { |
| 1231 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1232 | return (ret); |
| 1233 | } |
| 1234 | ret->attrs = tmp; |
| 1235 | ret->maxAttrs = nbAttrs; |
| 1236 | } |
| 1237 | ret->nbAttrs = nbAttrs; |
| 1238 | if (nbAttrs < MAX_ATTR) { |
| 1239 | memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs); |
| 1240 | } else { |
| 1241 | attr = node->properties; |
| 1242 | nbAttrs = 0; |
| 1243 | while (attr != NULL) { |
| 1244 | ret->attrs[nbAttrs++] = attr; |
| 1245 | attr = attr->next; |
| 1246 | } |
| 1247 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1248 | } |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 1249 | ret->nbAttrLeft = ret->nbAttrs; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1250 | return (ret); |
| 1251 | } |
| 1252 | |
| 1253 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1254 | * xmlRelaxNGCopyValidState: |
| 1255 | * @ctxt: a Relax-NG validation context |
| 1256 | * @state: a validation state |
| 1257 | * |
| 1258 | * Copy the validation state |
| 1259 | * |
| 1260 | * Returns the newly allocated structure or NULL in case or error |
| 1261 | */ |
| 1262 | static xmlRelaxNGValidStatePtr |
| 1263 | xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1264 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1265 | { |
| 1266 | xmlRelaxNGValidStatePtr ret; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1267 | unsigned int maxAttrs; |
| 1268 | xmlAttrPtr *attrs; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1269 | |
| 1270 | if (state == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1271 | return (NULL); |
| 1272 | if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) { |
| 1273 | ctxt->freeState->nbState--; |
| 1274 | ret = ctxt->freeState->tabState[ctxt->freeState->nbState]; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1275 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1276 | ret = |
| 1277 | (xmlRelaxNGValidStatePtr) |
| 1278 | xmlMalloc(sizeof(xmlRelaxNGValidState)); |
| 1279 | if (ret == NULL) { |
| 1280 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1281 | return (NULL); |
| 1282 | } |
| 1283 | memset(ret, 0, sizeof(xmlRelaxNGValidState)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1284 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1285 | attrs = ret->attrs; |
| 1286 | maxAttrs = ret->maxAttrs; |
| 1287 | memcpy(ret, state, sizeof(xmlRelaxNGValidState)); |
| 1288 | ret->attrs = attrs; |
| 1289 | ret->maxAttrs = maxAttrs; |
| 1290 | if (state->nbAttrs > 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1291 | if (ret->attrs == NULL) { |
| 1292 | ret->maxAttrs = state->maxAttrs; |
| 1293 | ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs * |
| 1294 | sizeof(xmlAttrPtr)); |
| 1295 | if (ret->attrs == NULL) { |
| 1296 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1297 | ret->nbAttrs = 0; |
| 1298 | return (ret); |
| 1299 | } |
| 1300 | } else if (ret->maxAttrs < state->nbAttrs) { |
| 1301 | xmlAttrPtr *tmp; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1302 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1303 | tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs * |
| 1304 | sizeof(xmlAttrPtr)); |
| 1305 | if (tmp == NULL) { |
| 1306 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1307 | ret->nbAttrs = 0; |
| 1308 | return (ret); |
| 1309 | } |
| 1310 | ret->maxAttrs = state->maxAttrs; |
| 1311 | ret->attrs = tmp; |
| 1312 | } |
| 1313 | memcpy(ret->attrs, state->attrs, |
| 1314 | state->nbAttrs * sizeof(xmlAttrPtr)); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1315 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1316 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | /** |
| 1320 | * xmlRelaxNGEqualValidState: |
| 1321 | * @ctxt: a Relax-NG validation context |
| 1322 | * @state1: a validation state |
| 1323 | * @state2: a validation state |
| 1324 | * |
| 1325 | * Compare the validation states for equality |
| 1326 | * |
| 1327 | * Returns 1 if equald, 0 otherwise |
| 1328 | */ |
| 1329 | static int |
| 1330 | xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1331 | xmlRelaxNGValidStatePtr state1, |
| 1332 | xmlRelaxNGValidStatePtr state2) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1333 | { |
| 1334 | int i; |
| 1335 | |
| 1336 | if ((state1 == NULL) || (state2 == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1337 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1338 | if (state1 == state2) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1339 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1340 | if (state1->node != state2->node) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1341 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1342 | if (state1->seq != state2->seq) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1343 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1344 | if (state1->nbAttrLeft != state2->nbAttrLeft) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1345 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1346 | if (state1->nbAttrs != state2->nbAttrs) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1347 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1348 | if (state1->endvalue != state2->endvalue) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1349 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1350 | if ((state1->value != state2->value) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1351 | (!xmlStrEqual(state1->value, state2->value))) |
| 1352 | return (0); |
| 1353 | for (i = 0; i < state1->nbAttrs; i++) { |
| 1354 | if (state1->attrs[i] != state2->attrs[i]) |
| 1355 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1356 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1357 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
| 1360 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1361 | * xmlRelaxNGFreeValidState: |
| 1362 | * @state: a validation state structure |
| 1363 | * |
| 1364 | * Deallocate a RelaxNG validation state structure. |
| 1365 | */ |
| 1366 | static void |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1367 | xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1368 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1369 | { |
| 1370 | if (state == NULL) |
| 1371 | return; |
| 1372 | |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1373 | if ((ctxt != NULL) && (ctxt->freeState == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1374 | ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1375 | } |
| 1376 | if ((ctxt == NULL) || (ctxt->freeState == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1377 | if (state->attrs != NULL) |
| 1378 | xmlFree(state->attrs); |
| 1379 | xmlFree(state); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1380 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1381 | xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1382 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | /************************************************************************ |
| 1386 | * * |
Daniel Veillard | 03c2f0a | 2004-01-25 19:54:59 +0000 | [diff] [blame] | 1387 | * Semi internal functions * |
| 1388 | * * |
| 1389 | ************************************************************************/ |
| 1390 | |
| 1391 | /** |
| 1392 | * xmlRelaxParserSetFlag: |
| 1393 | * @ctxt: a RelaxNG parser context |
| 1394 | * @flags: a set of flags values |
| 1395 | * |
| 1396 | * Semi private function used to pass informations to a parser context |
| 1397 | * which are a combination of xmlRelaxNGParserFlag . |
| 1398 | * |
| 1399 | * Returns 0 if success and -1 in case of error |
| 1400 | */ |
| 1401 | int |
| 1402 | xmlRelaxParserSetFlag(xmlRelaxNGParserCtxtPtr ctxt, int flags) |
| 1403 | { |
| 1404 | if (ctxt == NULL) return(-1); |
| 1405 | if (flags & XML_RELAXNGP_FREE_DOC) { |
| 1406 | ctxt->crng |= XML_RELAXNGP_FREE_DOC; |
| 1407 | flags -= XML_RELAXNGP_FREE_DOC; |
| 1408 | } |
| 1409 | if (flags & XML_RELAXNGP_CRNG) { |
| 1410 | ctxt->crng |= XML_RELAXNGP_CRNG; |
| 1411 | flags -= XML_RELAXNGP_CRNG; |
| 1412 | } |
| 1413 | if (flags != 0) return(-1); |
| 1414 | return(0); |
| 1415 | } |
| 1416 | |
| 1417 | /************************************************************************ |
| 1418 | * * |
| 1419 | * Document functions * |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1420 | * * |
| 1421 | ************************************************************************/ |
| 1422 | static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1423 | xmlDocPtr doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1424 | |
| 1425 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1426 | * xmlRelaxNGIncludePush: |
| 1427 | * @ctxt: the parser context |
| 1428 | * @value: the element doc |
| 1429 | * |
| 1430 | * Pushes a new include on top of the include stack |
| 1431 | * |
| 1432 | * Returns 0 in case of error, the index in the stack otherwise |
| 1433 | */ |
| 1434 | static int |
| 1435 | xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1436 | xmlRelaxNGIncludePtr value) |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1437 | { |
| 1438 | if (ctxt->incTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1439 | ctxt->incMax = 4; |
| 1440 | ctxt->incNr = 0; |
| 1441 | ctxt->incTab = |
| 1442 | (xmlRelaxNGIncludePtr *) xmlMalloc(ctxt->incMax * |
| 1443 | sizeof(ctxt->incTab[0])); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1444 | if (ctxt->incTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1445 | xmlRngPErrMemory(ctxt, "allocating include\n"); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1446 | return (0); |
| 1447 | } |
| 1448 | } |
| 1449 | if (ctxt->incNr >= ctxt->incMax) { |
| 1450 | ctxt->incMax *= 2; |
| 1451 | ctxt->incTab = |
| 1452 | (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1453 | ctxt->incMax * |
| 1454 | sizeof(ctxt->incTab[0])); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1455 | if (ctxt->incTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1456 | xmlRngPErrMemory(ctxt, "allocating include\n"); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1457 | return (0); |
| 1458 | } |
| 1459 | } |
| 1460 | ctxt->incTab[ctxt->incNr] = value; |
| 1461 | ctxt->inc = value; |
| 1462 | return (ctxt->incNr++); |
| 1463 | } |
| 1464 | |
| 1465 | /** |
| 1466 | * xmlRelaxNGIncludePop: |
| 1467 | * @ctxt: the parser context |
| 1468 | * |
| 1469 | * Pops the top include from the include stack |
| 1470 | * |
| 1471 | * Returns the include just removed |
| 1472 | */ |
| 1473 | static xmlRelaxNGIncludePtr |
| 1474 | xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt) |
| 1475 | { |
| 1476 | xmlRelaxNGIncludePtr ret; |
| 1477 | |
| 1478 | if (ctxt->incNr <= 0) |
| 1479 | return (0); |
| 1480 | ctxt->incNr--; |
| 1481 | if (ctxt->incNr > 0) |
| 1482 | ctxt->inc = ctxt->incTab[ctxt->incNr - 1]; |
| 1483 | else |
| 1484 | ctxt->inc = NULL; |
| 1485 | ret = ctxt->incTab[ctxt->incNr]; |
| 1486 | ctxt->incTab[ctxt->incNr] = 0; |
| 1487 | return (ret); |
| 1488 | } |
| 1489 | |
| 1490 | /** |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1491 | * xmlRelaxNGRemoveRedefine: |
| 1492 | * @ctxt: the parser context |
| 1493 | * @URL: the normalized URL |
| 1494 | * @target: the included target |
| 1495 | * @name: the define name to eliminate |
| 1496 | * |
| 1497 | * Applies the elimination algorithm of 4.7 |
| 1498 | * |
| 1499 | * Returns 0 in case of error, 1 in case of success. |
| 1500 | */ |
| 1501 | static int |
| 1502 | xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1503 | const xmlChar * URL ATTRIBUTE_UNUSED, |
| 1504 | xmlNodePtr target, const xmlChar * name) |
| 1505 | { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1506 | int found = 0; |
| 1507 | xmlNodePtr tmp, tmp2; |
| 1508 | xmlChar *name2; |
| 1509 | |
| 1510 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 1511 | if (name == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1512 | xmlGenericError(xmlGenericErrorContext, |
| 1513 | "Elimination of <include> start from %s\n", URL); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 1514 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1515 | xmlGenericError(xmlGenericErrorContext, |
| 1516 | "Elimination of <include> define %s from %s\n", |
| 1517 | name, URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1518 | #endif |
| 1519 | tmp = target; |
| 1520 | while (tmp != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1521 | tmp2 = tmp->next; |
| 1522 | if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) { |
| 1523 | found = 1; |
| 1524 | xmlUnlinkNode(tmp); |
| 1525 | xmlFreeNode(tmp); |
| 1526 | } else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) { |
| 1527 | name2 = xmlGetProp(tmp, BAD_CAST "name"); |
| 1528 | xmlRelaxNGNormExtSpace(name2); |
| 1529 | if (name2 != NULL) { |
| 1530 | if (xmlStrEqual(name, name2)) { |
| 1531 | found = 1; |
| 1532 | xmlUnlinkNode(tmp); |
| 1533 | xmlFreeNode(tmp); |
| 1534 | } |
| 1535 | xmlFree(name2); |
| 1536 | } |
| 1537 | } else if (IS_RELAXNG(tmp, "include")) { |
| 1538 | xmlChar *href = NULL; |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 1539 | xmlRelaxNGDocumentPtr inc = tmp->psvi; |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1540 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1541 | if ((inc != NULL) && (inc->doc != NULL) && |
| 1542 | (inc->doc->children != NULL)) { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1543 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1544 | if (xmlStrEqual |
| 1545 | (inc->doc->children->name, BAD_CAST "grammar")) { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1546 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1547 | href = xmlGetProp(tmp, BAD_CAST "href"); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1548 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1549 | if (xmlRelaxNGRemoveRedefine(ctxt, href, |
| 1550 | inc->doc->children-> |
| 1551 | children, name) == 1) { |
| 1552 | found = 1; |
| 1553 | } |
| 1554 | if (href != NULL) |
| 1555 | xmlFree(href); |
| 1556 | } |
| 1557 | } |
| 1558 | } |
| 1559 | tmp = tmp2; |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1560 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1561 | return (found); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
| 1564 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1565 | * xmlRelaxNGLoadInclude: |
| 1566 | * @ctxt: the parser context |
| 1567 | * @URL: the normalized URL |
| 1568 | * @node: the include node. |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1569 | * @ns: the namespace passed from the context. |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1570 | * |
| 1571 | * First lookup if the document is already loaded into the parser context, |
| 1572 | * check against recursion. If not found the resource is loaded and |
| 1573 | * the content is preprocessed before being returned back to the caller. |
| 1574 | * |
| 1575 | * Returns the xmlRelaxNGIncludePtr or NULL in case of error |
| 1576 | */ |
| 1577 | static xmlRelaxNGIncludePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1578 | xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * URL, |
| 1579 | xmlNodePtr node, const xmlChar * ns) |
| 1580 | { |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1581 | xmlRelaxNGIncludePtr ret = NULL; |
| 1582 | xmlDocPtr doc; |
| 1583 | int i; |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1584 | xmlNodePtr root, cur; |
| 1585 | |
| 1586 | #ifdef DEBUG_INCLUDE |
| 1587 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1588 | "xmlRelaxNGLoadInclude(%s)\n", URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1589 | #endif |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1590 | |
| 1591 | /* |
| 1592 | * check against recursion in the stack |
| 1593 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1594 | for (i = 0; i < ctxt->incNr; i++) { |
| 1595 | if (xmlStrEqual(ctxt->incTab[i]->href, URL)) { |
| 1596 | xmlRngPErr(ctxt, NULL, XML_RNGP_INCLUDE_RECURSE, |
| 1597 | "Detected an Include recursion for %s\n", URL, |
| 1598 | NULL); |
| 1599 | return (NULL); |
| 1600 | } |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1601 | } |
| 1602 | |
| 1603 | /* |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1604 | * load the document |
| 1605 | */ |
Daniel Veillard | 87247e8 | 2004-01-13 20:42:02 +0000 | [diff] [blame] | 1606 | doc = xmlReadFile((const char *) URL,NULL,0); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1607 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1608 | xmlRngPErr(ctxt, node, XML_RNGP_PARSE_ERROR, |
| 1609 | "xmlRelaxNG: could not load %s\n", URL, NULL); |
| 1610 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1611 | } |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1612 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1613 | xmlGenericError(xmlGenericErrorContext, "Parsed %s Okay\n", URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1614 | #endif |
| 1615 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1616 | /* |
| 1617 | * Allocate the document structures and register it first. |
| 1618 | */ |
| 1619 | ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude)); |
| 1620 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1621 | xmlRngPErrMemory(ctxt, "allocating include\n"); |
| 1622 | xmlFreeDoc(doc); |
| 1623 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1624 | } |
| 1625 | memset(ret, 0, sizeof(xmlRelaxNGInclude)); |
| 1626 | ret->doc = doc; |
| 1627 | ret->href = xmlStrdup(URL); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1628 | ret->next = ctxt->includes; |
| 1629 | ctxt->includes = ret; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1630 | |
| 1631 | /* |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1632 | * transmit the ns if needed |
| 1633 | */ |
| 1634 | if (ns != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1635 | root = xmlDocGetRootElement(doc); |
| 1636 | if (root != NULL) { |
| 1637 | if (xmlHasProp(root, BAD_CAST "ns") == NULL) { |
| 1638 | xmlSetProp(root, BAD_CAST "ns", ns); |
| 1639 | } |
| 1640 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1641 | } |
| 1642 | |
| 1643 | /* |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1644 | * push it on the stack |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1645 | */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1646 | xmlRelaxNGIncludePush(ctxt, ret); |
| 1647 | |
| 1648 | /* |
| 1649 | * Some preprocessing of the document content, this include recursing |
| 1650 | * in the include stack. |
| 1651 | */ |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1652 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1653 | xmlGenericError(xmlGenericErrorContext, "cleanup of %s\n", URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1654 | #endif |
| 1655 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1656 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 1657 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1658 | ctxt->inc = NULL; |
| 1659 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
| 1662 | /* |
| 1663 | * Pop up the include from the stack |
| 1664 | */ |
| 1665 | xmlRelaxNGIncludePop(ctxt); |
| 1666 | |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1667 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1668 | xmlGenericError(xmlGenericErrorContext, "Checking of %s\n", URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1669 | #endif |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1670 | /* |
| 1671 | * Check that the top element is a grammar |
| 1672 | */ |
| 1673 | root = xmlDocGetRootElement(doc); |
| 1674 | if (root == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1675 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, |
| 1676 | "xmlRelaxNG: included document is empty %s\n", URL, |
| 1677 | NULL); |
| 1678 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1679 | } |
| 1680 | if (!IS_RELAXNG(root, "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1681 | xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING, |
| 1682 | "xmlRelaxNG: included document %s root is not a grammar\n", |
| 1683 | URL, NULL); |
| 1684 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1685 | } |
| 1686 | |
| 1687 | /* |
| 1688 | * Elimination of redefined rules in the include. |
| 1689 | */ |
| 1690 | cur = node->children; |
| 1691 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1692 | if (IS_RELAXNG(cur, "start")) { |
| 1693 | int found = 0; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1694 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1695 | found = |
| 1696 | xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL); |
| 1697 | if (!found) { |
| 1698 | xmlRngPErr(ctxt, node, XML_RNGP_START_MISSING, |
| 1699 | "xmlRelaxNG: include %s has a start but not the included grammar\n", |
| 1700 | URL, NULL); |
| 1701 | } |
| 1702 | } else if (IS_RELAXNG(cur, "define")) { |
| 1703 | xmlChar *name; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1704 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1705 | name = xmlGetProp(cur, BAD_CAST "name"); |
| 1706 | if (name == NULL) { |
| 1707 | xmlRngPErr(ctxt, node, XML_RNGP_NAME_MISSING, |
| 1708 | "xmlRelaxNG: include %s has define without name\n", |
| 1709 | URL, NULL); |
| 1710 | } else { |
| 1711 | int found; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1712 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1713 | xmlRelaxNGNormExtSpace(name); |
| 1714 | found = xmlRelaxNGRemoveRedefine(ctxt, URL, |
| 1715 | root->children, name); |
| 1716 | if (!found) { |
| 1717 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_MISSING, |
| 1718 | "xmlRelaxNG: include %s has a define %s but not the included grammar\n", |
| 1719 | URL, name); |
| 1720 | } |
| 1721 | xmlFree(name); |
| 1722 | } |
| 1723 | } |
| 1724 | cur = cur->next; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1725 | } |
| 1726 | |
| 1727 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1728 | return (ret); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
| 1731 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1732 | * xmlRelaxNGValidErrorPush: |
| 1733 | * @ctxt: the validation context |
| 1734 | * @err: the error code |
| 1735 | * @arg1: the first string argument |
| 1736 | * @arg2: the second string argument |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1737 | * @dup: arg need to be duplicated |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1738 | * |
| 1739 | * Pushes a new error on top of the error stack |
| 1740 | * |
| 1741 | * Returns 0 in case of error, the index in the stack otherwise |
| 1742 | */ |
| 1743 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1744 | xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt, |
| 1745 | xmlRelaxNGValidErr err, const xmlChar * arg1, |
| 1746 | const xmlChar * arg2, int dup) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1747 | { |
| 1748 | xmlRelaxNGValidErrorPtr cur; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1749 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 1750 | #ifdef DEBUG_ERROR |
| 1751 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1752 | "Pushing error %d at %d on stack\n", err, ctxt->errNr); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 1753 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1754 | if (ctxt->errTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1755 | ctxt->errMax = 8; |
| 1756 | ctxt->errNr = 0; |
| 1757 | ctxt->errTab = |
| 1758 | (xmlRelaxNGValidErrorPtr) xmlMalloc(ctxt->errMax * |
| 1759 | sizeof |
| 1760 | (xmlRelaxNGValidError)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1761 | if (ctxt->errTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1762 | xmlRngVErrMemory(ctxt, "pushing error\n"); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1763 | return (0); |
| 1764 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1765 | ctxt->err = NULL; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1766 | } |
| 1767 | if (ctxt->errNr >= ctxt->errMax) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1768 | ctxt->errMax *= 2; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1769 | ctxt->errTab = |
| 1770 | (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1771 | ctxt->errMax * |
| 1772 | sizeof |
| 1773 | (xmlRelaxNGValidError)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1774 | if (ctxt->errTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1775 | xmlRngVErrMemory(ctxt, "pushing error\n"); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1776 | return (0); |
| 1777 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1778 | ctxt->err = &ctxt->errTab[ctxt->errNr - 1]; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1779 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 1780 | if ((ctxt->err != NULL) && (ctxt->state != NULL) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1781 | (ctxt->err->node == ctxt->state->node) && (ctxt->err->err == err)) |
| 1782 | return (ctxt->errNr); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1783 | cur = &ctxt->errTab[ctxt->errNr]; |
| 1784 | cur->err = err; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1785 | if (dup) { |
| 1786 | cur->arg1 = xmlStrdup(arg1); |
| 1787 | cur->arg2 = xmlStrdup(arg2); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1788 | cur->flags = ERROR_IS_DUP; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1789 | } else { |
| 1790 | cur->arg1 = arg1; |
| 1791 | cur->arg2 = arg2; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1792 | cur->flags = 0; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1793 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1794 | if (ctxt->state != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1795 | cur->node = ctxt->state->node; |
| 1796 | cur->seq = ctxt->state->seq; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1797 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1798 | cur->node = NULL; |
| 1799 | cur->seq = NULL; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1800 | } |
| 1801 | ctxt->err = cur; |
| 1802 | return (ctxt->errNr++); |
| 1803 | } |
| 1804 | |
| 1805 | /** |
| 1806 | * xmlRelaxNGValidErrorPop: |
| 1807 | * @ctxt: the validation context |
| 1808 | * |
| 1809 | * Pops the top error from the error stack |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1810 | */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1811 | static void |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1812 | xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt) |
| 1813 | { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1814 | xmlRelaxNGValidErrorPtr cur; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1815 | |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1816 | if (ctxt->errNr <= 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1817 | ctxt->err = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1818 | return; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1819 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1820 | ctxt->errNr--; |
| 1821 | if (ctxt->errNr > 0) |
| 1822 | ctxt->err = &ctxt->errTab[ctxt->errNr - 1]; |
| 1823 | else |
| 1824 | ctxt->err = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1825 | cur = &ctxt->errTab[ctxt->errNr]; |
| 1826 | if (cur->flags & ERROR_IS_DUP) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1827 | if (cur->arg1 != NULL) |
| 1828 | xmlFree((xmlChar *) cur->arg1); |
| 1829 | cur->arg1 = NULL; |
| 1830 | if (cur->arg2 != NULL) |
| 1831 | xmlFree((xmlChar *) cur->arg2); |
| 1832 | cur->arg2 = NULL; |
| 1833 | cur->flags = 0; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1834 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1835 | } |
| 1836 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1837 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1838 | * xmlRelaxNGDocumentPush: |
| 1839 | * @ctxt: the parser context |
| 1840 | * @value: the element doc |
| 1841 | * |
| 1842 | * Pushes a new doc on top of the doc stack |
| 1843 | * |
| 1844 | * Returns 0 in case of error, the index in the stack otherwise |
| 1845 | */ |
| 1846 | static int |
| 1847 | xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1848 | xmlRelaxNGDocumentPtr value) |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1849 | { |
| 1850 | if (ctxt->docTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1851 | ctxt->docMax = 4; |
| 1852 | ctxt->docNr = 0; |
| 1853 | ctxt->docTab = |
| 1854 | (xmlRelaxNGDocumentPtr *) xmlMalloc(ctxt->docMax * |
| 1855 | sizeof(ctxt->docTab[0])); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1856 | if (ctxt->docTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1857 | xmlRngPErrMemory(ctxt, "adding document\n"); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1858 | return (0); |
| 1859 | } |
| 1860 | } |
| 1861 | if (ctxt->docNr >= ctxt->docMax) { |
| 1862 | ctxt->docMax *= 2; |
| 1863 | ctxt->docTab = |
| 1864 | (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1865 | ctxt->docMax * |
| 1866 | sizeof(ctxt->docTab[0])); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1867 | if (ctxt->docTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1868 | xmlRngPErrMemory(ctxt, "adding document\n"); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1869 | return (0); |
| 1870 | } |
| 1871 | } |
| 1872 | ctxt->docTab[ctxt->docNr] = value; |
| 1873 | ctxt->doc = value; |
| 1874 | return (ctxt->docNr++); |
| 1875 | } |
| 1876 | |
| 1877 | /** |
| 1878 | * xmlRelaxNGDocumentPop: |
| 1879 | * @ctxt: the parser context |
| 1880 | * |
| 1881 | * Pops the top doc from the doc stack |
| 1882 | * |
| 1883 | * Returns the doc just removed |
| 1884 | */ |
| 1885 | static xmlRelaxNGDocumentPtr |
| 1886 | xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt) |
| 1887 | { |
| 1888 | xmlRelaxNGDocumentPtr ret; |
| 1889 | |
| 1890 | if (ctxt->docNr <= 0) |
| 1891 | return (0); |
| 1892 | ctxt->docNr--; |
| 1893 | if (ctxt->docNr > 0) |
| 1894 | ctxt->doc = ctxt->docTab[ctxt->docNr - 1]; |
| 1895 | else |
| 1896 | ctxt->doc = NULL; |
| 1897 | ret = ctxt->docTab[ctxt->docNr]; |
| 1898 | ctxt->docTab[ctxt->docNr] = 0; |
| 1899 | return (ret); |
| 1900 | } |
| 1901 | |
| 1902 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1903 | * xmlRelaxNGLoadExternalRef: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1904 | * @ctxt: the parser context |
| 1905 | * @URL: the normalized URL |
| 1906 | * @ns: the inherited ns if any |
| 1907 | * |
| 1908 | * First lookup if the document is already loaded into the parser context, |
| 1909 | * check against recursion. If not found the resource is loaded and |
| 1910 | * the content is preprocessed before being returned back to the caller. |
| 1911 | * |
| 1912 | * Returns the xmlRelaxNGDocumentPtr or NULL in case of error |
| 1913 | */ |
| 1914 | static xmlRelaxNGDocumentPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1915 | xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt, |
| 1916 | const xmlChar * URL, const xmlChar * ns) |
| 1917 | { |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1918 | xmlRelaxNGDocumentPtr ret = NULL; |
| 1919 | xmlDocPtr doc; |
| 1920 | xmlNodePtr root; |
| 1921 | int i; |
| 1922 | |
| 1923 | /* |
| 1924 | * check against recursion in the stack |
| 1925 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1926 | for (i = 0; i < ctxt->docNr; i++) { |
| 1927 | if (xmlStrEqual(ctxt->docTab[i]->href, URL)) { |
| 1928 | xmlRngPErr(ctxt, NULL, XML_RNGP_EXTERNALREF_RECURSE, |
| 1929 | "Detected an externalRef recursion for %s\n", URL, |
| 1930 | NULL); |
| 1931 | return (NULL); |
| 1932 | } |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1933 | } |
| 1934 | |
| 1935 | /* |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1936 | * load the document |
| 1937 | */ |
Daniel Veillard | 87247e8 | 2004-01-13 20:42:02 +0000 | [diff] [blame] | 1938 | doc = xmlReadFile((const char *) URL,NULL,0); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1939 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1940 | xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR, |
| 1941 | "xmlRelaxNG: could not load %s\n", URL, NULL); |
| 1942 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1943 | } |
| 1944 | |
| 1945 | /* |
| 1946 | * Allocate the document structures and register it first. |
| 1947 | */ |
| 1948 | ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument)); |
| 1949 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1950 | xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_ERR_NO_MEMORY, |
| 1951 | "xmlRelaxNG: allocate memory for doc %s\n", URL, NULL); |
| 1952 | xmlFreeDoc(doc); |
| 1953 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1954 | } |
| 1955 | memset(ret, 0, sizeof(xmlRelaxNGDocument)); |
| 1956 | ret->doc = doc; |
| 1957 | ret->href = xmlStrdup(URL); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1958 | ret->next = ctxt->documents; |
| 1959 | ctxt->documents = ret; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1960 | |
| 1961 | /* |
| 1962 | * transmit the ns if needed |
| 1963 | */ |
| 1964 | if (ns != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1965 | root = xmlDocGetRootElement(doc); |
| 1966 | if (root != NULL) { |
| 1967 | if (xmlHasProp(root, BAD_CAST "ns") == NULL) { |
| 1968 | xmlSetProp(root, BAD_CAST "ns", ns); |
| 1969 | } |
| 1970 | } |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1971 | } |
| 1972 | |
| 1973 | /* |
| 1974 | * push it on the stack and register it in the hash table |
| 1975 | */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1976 | xmlRelaxNGDocumentPush(ctxt, ret); |
| 1977 | |
| 1978 | /* |
| 1979 | * Some preprocessing of the document content |
| 1980 | */ |
| 1981 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 1982 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1983 | ctxt->doc = NULL; |
| 1984 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1985 | } |
| 1986 | |
| 1987 | xmlRelaxNGDocumentPop(ctxt); |
| 1988 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1989 | return (ret); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1990 | } |
| 1991 | |
| 1992 | /************************************************************************ |
| 1993 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1994 | * Error functions * |
| 1995 | * * |
| 1996 | ************************************************************************/ |
| 1997 | |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1998 | #define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0); |
| 1999 | #define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0); |
| 2000 | #define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0); |
| 2001 | #define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1); |
| 2002 | #define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2003 | |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 2004 | static const char * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2005 | xmlRelaxNGDefName(xmlRelaxNGDefinePtr def) |
| 2006 | { |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 2007 | if (def == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2008 | return ("none"); |
| 2009 | switch (def->type) { |
| 2010 | case XML_RELAXNG_EMPTY: |
| 2011 | return ("empty"); |
| 2012 | case XML_RELAXNG_NOT_ALLOWED: |
| 2013 | return ("notAllowed"); |
| 2014 | case XML_RELAXNG_EXCEPT: |
| 2015 | return ("except"); |
| 2016 | case XML_RELAXNG_TEXT: |
| 2017 | return ("text"); |
| 2018 | case XML_RELAXNG_ELEMENT: |
| 2019 | return ("element"); |
| 2020 | case XML_RELAXNG_DATATYPE: |
| 2021 | return ("datatype"); |
| 2022 | case XML_RELAXNG_VALUE: |
| 2023 | return ("value"); |
| 2024 | case XML_RELAXNG_LIST: |
| 2025 | return ("list"); |
| 2026 | case XML_RELAXNG_ATTRIBUTE: |
| 2027 | return ("attribute"); |
| 2028 | case XML_RELAXNG_DEF: |
| 2029 | return ("def"); |
| 2030 | case XML_RELAXNG_REF: |
| 2031 | return ("ref"); |
| 2032 | case XML_RELAXNG_EXTERNALREF: |
| 2033 | return ("externalRef"); |
| 2034 | case XML_RELAXNG_PARENTREF: |
| 2035 | return ("parentRef"); |
| 2036 | case XML_RELAXNG_OPTIONAL: |
| 2037 | return ("optional"); |
| 2038 | case XML_RELAXNG_ZEROORMORE: |
| 2039 | return ("zeroOrMore"); |
| 2040 | case XML_RELAXNG_ONEORMORE: |
| 2041 | return ("oneOrMore"); |
| 2042 | case XML_RELAXNG_CHOICE: |
| 2043 | return ("choice"); |
| 2044 | case XML_RELAXNG_GROUP: |
| 2045 | return ("group"); |
| 2046 | case XML_RELAXNG_INTERLEAVE: |
| 2047 | return ("interleave"); |
| 2048 | case XML_RELAXNG_START: |
| 2049 | return ("start"); |
| 2050 | case XML_RELAXNG_NOOP: |
| 2051 | return ("noop"); |
| 2052 | case XML_RELAXNG_PARAM: |
| 2053 | return ("param"); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 2054 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2055 | return ("unknown"); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 2056 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 2057 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2058 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2059 | * xmlRelaxNGGetErrorString: |
| 2060 | * @err: the error code |
| 2061 | * @arg1: the first string argument |
| 2062 | * @arg2: the second string argument |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2063 | * |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2064 | * computes a formatted error string for the given error code and args |
| 2065 | * |
| 2066 | * Returns the error string, it must be deallocated by the caller |
| 2067 | */ |
| 2068 | static xmlChar * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2069 | xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar * arg1, |
| 2070 | const xmlChar * arg2) |
| 2071 | { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2072 | char msg[1000]; |
| 2073 | |
| 2074 | if (arg1 == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2075 | arg1 = BAD_CAST ""; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2076 | if (arg2 == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2077 | arg2 = BAD_CAST ""; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2078 | |
| 2079 | msg[0] = 0; |
| 2080 | switch (err) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2081 | case XML_RELAXNG_OK: |
| 2082 | return (NULL); |
| 2083 | case XML_RELAXNG_ERR_MEMORY: |
| 2084 | return (xmlCharStrdup("out of memory\n")); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2085 | case XML_RELAXNG_ERR_TYPE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2086 | snprintf(msg, 1000, "failed to validate type %s\n", arg1); |
| 2087 | break; |
| 2088 | case XML_RELAXNG_ERR_TYPEVAL: |
| 2089 | snprintf(msg, 1000, "Type %s doesn't allow value '%s'\n", arg1, |
| 2090 | arg2); |
| 2091 | break; |
| 2092 | case XML_RELAXNG_ERR_DUPID: |
| 2093 | snprintf(msg, 1000, "ID %s redefined\n", arg1); |
| 2094 | break; |
| 2095 | case XML_RELAXNG_ERR_TYPECMP: |
| 2096 | snprintf(msg, 1000, "failed to compare type %s\n", arg1); |
| 2097 | break; |
| 2098 | case XML_RELAXNG_ERR_NOSTATE: |
| 2099 | return (xmlCharStrdup("Internal error: no state\n")); |
| 2100 | case XML_RELAXNG_ERR_NODEFINE: |
| 2101 | return (xmlCharStrdup("Internal error: no define\n")); |
| 2102 | case XML_RELAXNG_ERR_INTERNAL: |
| 2103 | snprintf(msg, 1000, "Internal error: %s\n", arg1); |
| 2104 | break; |
| 2105 | case XML_RELAXNG_ERR_LISTEXTRA: |
| 2106 | snprintf(msg, 1000, "Extra data in list: %s\n", arg1); |
| 2107 | break; |
| 2108 | case XML_RELAXNG_ERR_INTERNODATA: |
| 2109 | return (xmlCharStrdup |
| 2110 | ("Internal: interleave block has no data\n")); |
| 2111 | case XML_RELAXNG_ERR_INTERSEQ: |
| 2112 | return (xmlCharStrdup("Invalid sequence in interleave\n")); |
| 2113 | case XML_RELAXNG_ERR_INTEREXTRA: |
| 2114 | snprintf(msg, 1000, "Extra element %s in interleave\n", arg1); |
| 2115 | break; |
| 2116 | case XML_RELAXNG_ERR_ELEMNAME: |
| 2117 | snprintf(msg, 1000, "Expecting element %s, got %s\n", arg1, |
| 2118 | arg2); |
| 2119 | break; |
| 2120 | case XML_RELAXNG_ERR_ELEMNONS: |
| 2121 | snprintf(msg, 1000, "Expecting a namespace for element %s\n", |
| 2122 | arg1); |
| 2123 | break; |
| 2124 | case XML_RELAXNG_ERR_ELEMWRONGNS: |
| 2125 | snprintf(msg, 1000, |
| 2126 | "Element %s has wrong namespace: expecting %s\n", arg1, |
| 2127 | arg2); |
| 2128 | break; |
| 2129 | case XML_RELAXNG_ERR_ELEMWRONG: |
| 2130 | snprintf(msg, 1000, "Did not expect element %s there\n", arg1); |
| 2131 | break; |
| 2132 | case XML_RELAXNG_ERR_TEXTWRONG: |
| 2133 | snprintf(msg, 1000, |
| 2134 | "Did not expect text in element %s content\n", arg1); |
| 2135 | break; |
| 2136 | case XML_RELAXNG_ERR_ELEMEXTRANS: |
| 2137 | snprintf(msg, 1000, "Expecting no namespace for element %s\n", |
| 2138 | arg1); |
| 2139 | break; |
| 2140 | case XML_RELAXNG_ERR_ELEMNOTEMPTY: |
| 2141 | snprintf(msg, 1000, "Expecting element %s to be empty\n", arg1); |
| 2142 | break; |
| 2143 | case XML_RELAXNG_ERR_NOELEM: |
| 2144 | snprintf(msg, 1000, "Expecting an element %s, got nothing\n", |
| 2145 | arg1); |
| 2146 | break; |
| 2147 | case XML_RELAXNG_ERR_NOTELEM: |
| 2148 | return (xmlCharStrdup("Expecting an element got text\n")); |
| 2149 | case XML_RELAXNG_ERR_ATTRVALID: |
| 2150 | snprintf(msg, 1000, "Element %s failed to validate attributes\n", |
| 2151 | arg1); |
| 2152 | break; |
| 2153 | case XML_RELAXNG_ERR_CONTENTVALID: |
| 2154 | snprintf(msg, 1000, "Element %s failed to validate content\n", |
| 2155 | arg1); |
| 2156 | break; |
| 2157 | case XML_RELAXNG_ERR_EXTRACONTENT: |
| 2158 | snprintf(msg, 1000, "Element %s has extra content: %s\n", |
| 2159 | arg1, arg2); |
| 2160 | break; |
| 2161 | case XML_RELAXNG_ERR_INVALIDATTR: |
| 2162 | snprintf(msg, 1000, "Invalid attribute %s for element %s\n", |
| 2163 | arg1, arg2); |
| 2164 | break; |
| 2165 | case XML_RELAXNG_ERR_LACKDATA: |
| 2166 | snprintf(msg, 1000, "Datatype element %s contains no data\n", |
| 2167 | arg1); |
| 2168 | break; |
| 2169 | case XML_RELAXNG_ERR_DATAELEM: |
| 2170 | snprintf(msg, 1000, "Datatype element %s has child elements\n", |
| 2171 | arg1); |
| 2172 | break; |
| 2173 | case XML_RELAXNG_ERR_VALELEM: |
| 2174 | snprintf(msg, 1000, "Value element %s has child elements\n", |
| 2175 | arg1); |
| 2176 | break; |
| 2177 | case XML_RELAXNG_ERR_LISTELEM: |
| 2178 | snprintf(msg, 1000, "List element %s has child elements\n", |
| 2179 | arg1); |
| 2180 | break; |
| 2181 | case XML_RELAXNG_ERR_DATATYPE: |
| 2182 | snprintf(msg, 1000, "Error validating datatype %s\n", arg1); |
| 2183 | break; |
| 2184 | case XML_RELAXNG_ERR_VALUE: |
| 2185 | snprintf(msg, 1000, "Error validating value %s\n", arg1); |
| 2186 | break; |
| 2187 | case XML_RELAXNG_ERR_LIST: |
| 2188 | return (xmlCharStrdup("Error validating list\n")); |
| 2189 | case XML_RELAXNG_ERR_NOGRAMMAR: |
| 2190 | return (xmlCharStrdup("No top grammar defined\n")); |
| 2191 | case XML_RELAXNG_ERR_EXTRADATA: |
| 2192 | return (xmlCharStrdup("Extra data in the document\n")); |
| 2193 | default: |
| 2194 | return (xmlCharStrdup("Unknown error !\n")); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2195 | } |
| 2196 | if (msg[0] == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2197 | snprintf(msg, 1000, "Unknown error code %d\n", err); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2198 | } |
Daniel Veillard | adbb0e6 | 2003-05-10 20:02:45 +0000 | [diff] [blame] | 2199 | msg[1000 - 1] = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2200 | return (xmlStrdup((xmlChar *) msg)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2201 | } |
| 2202 | |
| 2203 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2204 | * xmlRelaxNGShowValidError: |
| 2205 | * @ctxt: the validation context |
| 2206 | * @err: the error number |
| 2207 | * @node: the node |
| 2208 | * @child: the node child generating the problem. |
| 2209 | * @arg1: the first argument |
| 2210 | * @arg2: the second argument |
| 2211 | * |
| 2212 | * Show a validation error. |
| 2213 | */ |
| 2214 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2215 | xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt, |
| 2216 | xmlRelaxNGValidErr err, xmlNodePtr node, |
| 2217 | xmlNodePtr child, const xmlChar * arg1, |
| 2218 | const xmlChar * arg2) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2219 | { |
| 2220 | xmlChar *msg; |
| 2221 | |
| 2222 | if (ctxt->error == NULL) |
| 2223 | return; |
| 2224 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2225 | #ifdef DEBUG_ERROR |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2226 | xmlGenericError(xmlGenericErrorContext, "Show error %d\n", err); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2227 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2228 | msg = xmlRelaxNGGetErrorString(err, arg1, arg2); |
| 2229 | if (msg == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2230 | return; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2231 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2232 | if (ctxt->errNo == XML_RELAXNG_OK) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2233 | ctxt->errNo = err; |
| 2234 | xmlRngVErr(ctxt, (child == NULL ? node : child), err, |
| 2235 | (const char *) msg, arg1, arg2); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2236 | xmlFree(msg); |
| 2237 | } |
| 2238 | |
| 2239 | /** |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2240 | * xmlRelaxNGPopErrors: |
| 2241 | * @ctxt: the validation context |
| 2242 | * @level: the error level in the stack |
| 2243 | * |
| 2244 | * pop and discard all errors until the given level is reached |
| 2245 | */ |
| 2246 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2247 | xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level) |
| 2248 | { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2249 | int i; |
| 2250 | xmlRelaxNGValidErrorPtr err; |
| 2251 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2252 | #ifdef DEBUG_ERROR |
| 2253 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2254 | "Pop errors till level %d\n", level); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2255 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2256 | for (i = level; i < ctxt->errNr; i++) { |
| 2257 | err = &ctxt->errTab[i]; |
| 2258 | if (err->flags & ERROR_IS_DUP) { |
| 2259 | if (err->arg1 != NULL) |
| 2260 | xmlFree((xmlChar *) err->arg1); |
| 2261 | err->arg1 = NULL; |
| 2262 | if (err->arg2 != NULL) |
| 2263 | xmlFree((xmlChar *) err->arg2); |
| 2264 | err->arg2 = NULL; |
| 2265 | err->flags = 0; |
| 2266 | } |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2267 | } |
| 2268 | ctxt->errNr = level; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2269 | if (ctxt->errNr <= 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2270 | ctxt->err = NULL; |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2271 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2272 | |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2273 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2274 | * xmlRelaxNGDumpValidError: |
| 2275 | * @ctxt: the validation context |
| 2276 | * |
| 2277 | * Show all validation error over a given index. |
| 2278 | */ |
| 2279 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2280 | xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt) |
| 2281 | { |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 2282 | int i, j, k; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2283 | xmlRelaxNGValidErrorPtr err, dup; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2284 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2285 | #ifdef DEBUG_ERROR |
| 2286 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2287 | "Dumping error stack %d errors\n", ctxt->errNr); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2288 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2289 | for (i = 0, k = 0; i < ctxt->errNr; i++) { |
| 2290 | err = &ctxt->errTab[i]; |
| 2291 | if (k < MAX_ERROR) { |
| 2292 | for (j = 0; j < i; j++) { |
| 2293 | dup = &ctxt->errTab[j]; |
| 2294 | if ((err->err == dup->err) && (err->node == dup->node) && |
| 2295 | (xmlStrEqual(err->arg1, dup->arg1)) && |
| 2296 | (xmlStrEqual(err->arg2, dup->arg2))) { |
| 2297 | goto skip; |
| 2298 | } |
| 2299 | } |
| 2300 | xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq, |
| 2301 | err->arg1, err->arg2); |
| 2302 | k++; |
| 2303 | } |
| 2304 | skip: |
| 2305 | if (err->flags & ERROR_IS_DUP) { |
| 2306 | if (err->arg1 != NULL) |
| 2307 | xmlFree((xmlChar *) err->arg1); |
| 2308 | err->arg1 = NULL; |
| 2309 | if (err->arg2 != NULL) |
| 2310 | xmlFree((xmlChar *) err->arg2); |
| 2311 | err->arg2 = NULL; |
| 2312 | err->flags = 0; |
| 2313 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2314 | } |
| 2315 | ctxt->errNr = 0; |
| 2316 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2317 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2318 | /** |
| 2319 | * xmlRelaxNGAddValidError: |
| 2320 | * @ctxt: the validation context |
| 2321 | * @err: the error number |
| 2322 | * @arg1: the first argument |
| 2323 | * @arg2: the second argument |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2324 | * @dup: need to dup the args |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2325 | * |
| 2326 | * Register a validation error, either generating it if it's sure |
| 2327 | * or stacking it for later handling if unsure. |
| 2328 | */ |
| 2329 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2330 | xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt, |
| 2331 | xmlRelaxNGValidErr err, const xmlChar * arg1, |
| 2332 | const xmlChar * arg2, int dup) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2333 | { |
| 2334 | if ((ctxt == NULL) || (ctxt->error == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2335 | return; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2336 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2337 | #ifdef DEBUG_ERROR |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2338 | xmlGenericError(xmlGenericErrorContext, "Adding error %d\n", err); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2339 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2340 | /* |
| 2341 | * generate the error directly |
| 2342 | */ |
| 2343 | if (((ctxt->flags & 1) == 0) || (ctxt->flags & 2)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2344 | xmlNodePtr node, seq; |
| 2345 | |
| 2346 | /* |
| 2347 | * Flush first any stacked error which might be the |
| 2348 | * real cause of the problem. |
| 2349 | */ |
| 2350 | if (ctxt->errNr != 0) |
| 2351 | xmlRelaxNGDumpValidError(ctxt); |
| 2352 | if (ctxt->state != NULL) { |
| 2353 | node = ctxt->state->node; |
| 2354 | seq = ctxt->state->seq; |
| 2355 | } else { |
| 2356 | node = seq = NULL; |
| 2357 | } |
| 2358 | xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2359 | } |
| 2360 | /* |
| 2361 | * Stack the error for later processing if needed |
| 2362 | */ |
| 2363 | else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2364 | xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2365 | } |
| 2366 | } |
| 2367 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2368 | |
| 2369 | /************************************************************************ |
| 2370 | * * |
| 2371 | * Type library hooks * |
| 2372 | * * |
| 2373 | ************************************************************************/ |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2374 | static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2375 | const xmlChar * str); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2376 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2377 | /** |
| 2378 | * xmlRelaxNGSchemaTypeHave: |
| 2379 | * @data: data needed for the library |
| 2380 | * @type: the type name |
| 2381 | * |
| 2382 | * Check if the given type is provided by |
| 2383 | * the W3C XMLSchema Datatype library. |
| 2384 | * |
| 2385 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2386 | */ |
| 2387 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2388 | xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar * type) |
| 2389 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2390 | xmlSchemaTypePtr typ; |
| 2391 | |
| 2392 | if (type == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2393 | return (-1); |
| 2394 | typ = xmlSchemaGetPredefinedType(type, |
| 2395 | BAD_CAST |
| 2396 | "http://www.w3.org/2001/XMLSchema"); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2397 | if (typ == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2398 | return (0); |
| 2399 | return (1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2400 | } |
| 2401 | |
| 2402 | /** |
| 2403 | * xmlRelaxNGSchemaTypeCheck: |
| 2404 | * @data: data needed for the library |
| 2405 | * @type: the type name |
| 2406 | * @value: the value to check |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2407 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2408 | * |
| 2409 | * Check if the given type and value are validated by |
| 2410 | * the W3C XMLSchema Datatype library. |
| 2411 | * |
| 2412 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2413 | */ |
| 2414 | static int |
| 2415 | xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2416 | const xmlChar * type, |
| 2417 | const xmlChar * value, |
| 2418 | void **result, xmlNodePtr node) |
| 2419 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2420 | xmlSchemaTypePtr typ; |
| 2421 | int ret; |
| 2422 | |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2423 | if ((type == NULL) || (value == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2424 | return (-1); |
| 2425 | typ = xmlSchemaGetPredefinedType(type, |
| 2426 | BAD_CAST |
| 2427 | "http://www.w3.org/2001/XMLSchema"); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2428 | if (typ == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2429 | return (-1); |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2430 | ret = xmlSchemaValPredefTypeNode(typ, value, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2431 | (xmlSchemaValPtr *) result, node); |
| 2432 | if (ret == 2) /* special ID error code */ |
| 2433 | return (2); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2434 | if (ret == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2435 | return (1); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2436 | if (ret > 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2437 | return (0); |
| 2438 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2439 | } |
| 2440 | |
| 2441 | /** |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2442 | * xmlRelaxNGSchemaFacetCheck: |
| 2443 | * @data: data needed for the library |
| 2444 | * @type: the type name |
| 2445 | * @facet: the facet name |
| 2446 | * @val: the facet value |
| 2447 | * @strval: the string value |
| 2448 | * @value: the value to check |
| 2449 | * |
| 2450 | * Function provided by a type library to check a value facet |
| 2451 | * |
| 2452 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2453 | */ |
| 2454 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2455 | xmlRelaxNGSchemaFacetCheck(void *data ATTRIBUTE_UNUSED, |
| 2456 | const xmlChar * type, const xmlChar * facetname, |
| 2457 | const xmlChar * val, const xmlChar * strval, |
| 2458 | void *value) |
| 2459 | { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2460 | xmlSchemaFacetPtr facet; |
| 2461 | xmlSchemaTypePtr typ; |
| 2462 | int ret; |
| 2463 | |
| 2464 | if ((type == NULL) || (strval == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2465 | return (-1); |
| 2466 | typ = xmlSchemaGetPredefinedType(type, |
| 2467 | BAD_CAST |
| 2468 | "http://www.w3.org/2001/XMLSchema"); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2469 | if (typ == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2470 | return (-1); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2471 | |
| 2472 | facet = xmlSchemaNewFacet(); |
| 2473 | if (facet == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2474 | return (-1); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2475 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2476 | if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2477 | facet->type = XML_SCHEMA_FACET_MININCLUSIVE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2478 | } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2479 | facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2480 | } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2481 | facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2482 | } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2483 | facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2484 | } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2485 | facet->type = XML_SCHEMA_FACET_TOTALDIGITS; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2486 | } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2487 | facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2488 | } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2489 | facet->type = XML_SCHEMA_FACET_PATTERN; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2490 | } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2491 | facet->type = XML_SCHEMA_FACET_ENUMERATION; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2492 | } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2493 | facet->type = XML_SCHEMA_FACET_WHITESPACE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2494 | } else if (xmlStrEqual(facetname, BAD_CAST "length")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2495 | facet->type = XML_SCHEMA_FACET_LENGTH; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2496 | } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2497 | facet->type = XML_SCHEMA_FACET_MAXLENGTH; |
| 2498 | } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) { |
| 2499 | facet->type = XML_SCHEMA_FACET_MINLENGTH; |
| 2500 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2501 | xmlSchemaFreeFacet(facet); |
| 2502 | return (-1); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2503 | } |
| 2504 | facet->value = xmlStrdup(val); |
| 2505 | ret = xmlSchemaCheckFacet(facet, typ, NULL, type); |
| 2506 | if (ret != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2507 | xmlSchemaFreeFacet(facet); |
| 2508 | return (-1); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2509 | } |
| 2510 | ret = xmlSchemaValidateFacet(typ, facet, strval, value); |
| 2511 | xmlSchemaFreeFacet(facet); |
| 2512 | if (ret != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2513 | return (-1); |
| 2514 | return (0); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2515 | } |
| 2516 | |
| 2517 | /** |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2518 | * xmlRelaxNGSchemaFreeValue: |
| 2519 | * @data: data needed for the library |
| 2520 | * @value: the value to free |
| 2521 | * |
| 2522 | * Function provided by a type library to free a Schemas value |
| 2523 | * |
| 2524 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2525 | */ |
| 2526 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2527 | xmlRelaxNGSchemaFreeValue(void *data ATTRIBUTE_UNUSED, void *value) |
| 2528 | { |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2529 | xmlSchemaFreeValue(value); |
| 2530 | } |
| 2531 | |
| 2532 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2533 | * xmlRelaxNGSchemaTypeCompare: |
| 2534 | * @data: data needed for the library |
| 2535 | * @type: the type name |
| 2536 | * @value1: the first value |
| 2537 | * @value2: the second value |
| 2538 | * |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2539 | * Compare two values for equality accordingly a type from the W3C XMLSchema |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2540 | * Datatype library. |
| 2541 | * |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2542 | * 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] | 2543 | */ |
| 2544 | static int |
| 2545 | xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2546 | const xmlChar * type, |
| 2547 | const xmlChar * value1, |
| 2548 | xmlNodePtr ctxt1, |
| 2549 | void *comp1, |
| 2550 | const xmlChar * value2, xmlNodePtr ctxt2) |
| 2551 | { |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2552 | int ret; |
| 2553 | xmlSchemaTypePtr typ; |
| 2554 | xmlSchemaValPtr res1 = NULL, res2 = NULL; |
| 2555 | |
| 2556 | if ((type == NULL) || (value1 == NULL) || (value2 == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2557 | return (-1); |
| 2558 | typ = xmlSchemaGetPredefinedType(type, |
| 2559 | BAD_CAST |
| 2560 | "http://www.w3.org/2001/XMLSchema"); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2561 | if (typ == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2562 | return (-1); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2563 | if (comp1 == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2564 | ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1); |
| 2565 | if (ret != 0) |
| 2566 | return (-1); |
| 2567 | if (res1 == NULL) |
| 2568 | return (-1); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2569 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2570 | res1 = (xmlSchemaValPtr) comp1; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2571 | } |
| 2572 | ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2573 | if (ret != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2574 | xmlSchemaFreeValue(res1); |
| 2575 | return (-1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2576 | } |
| 2577 | if (res1 == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2578 | xmlSchemaFreeValue(res1); |
| 2579 | return (-1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2580 | } |
| 2581 | ret = xmlSchemaCompareValues(res1, res2); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2582 | if (res1 != (xmlSchemaValPtr) comp1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2583 | xmlSchemaFreeValue(res1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2584 | xmlSchemaFreeValue(res2); |
| 2585 | if (ret == -2) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2586 | return (-1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2587 | if (ret == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2588 | return (1); |
| 2589 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2590 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2591 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2592 | /** |
| 2593 | * xmlRelaxNGDefaultTypeHave: |
| 2594 | * @data: data needed for the library |
| 2595 | * @type: the type name |
| 2596 | * |
| 2597 | * Check if the given type is provided by |
| 2598 | * the default datatype library. |
| 2599 | * |
| 2600 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2601 | */ |
| 2602 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2603 | xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED, |
| 2604 | const xmlChar * type) |
| 2605 | { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2606 | if (type == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2607 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2608 | if (xmlStrEqual(type, BAD_CAST "string")) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2609 | return (1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2610 | if (xmlStrEqual(type, BAD_CAST "token")) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2611 | return (1); |
| 2612 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2613 | } |
| 2614 | |
| 2615 | /** |
| 2616 | * xmlRelaxNGDefaultTypeCheck: |
| 2617 | * @data: data needed for the library |
| 2618 | * @type: the type name |
| 2619 | * @value: the value to check |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2620 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2621 | * |
| 2622 | * Check if the given type and value are validated by |
| 2623 | * the default datatype library. |
| 2624 | * |
| 2625 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2626 | */ |
| 2627 | static int |
| 2628 | xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2629 | const xmlChar * type ATTRIBUTE_UNUSED, |
| 2630 | const xmlChar * value ATTRIBUTE_UNUSED, |
| 2631 | void **result ATTRIBUTE_UNUSED, |
| 2632 | xmlNodePtr node ATTRIBUTE_UNUSED) |
| 2633 | { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2634 | if (value == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2635 | return (-1); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2636 | if (xmlStrEqual(type, BAD_CAST "string")) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2637 | return (1); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2638 | if (xmlStrEqual(type, BAD_CAST "token")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2639 | return (1); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2640 | } |
| 2641 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2642 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2643 | } |
| 2644 | |
| 2645 | /** |
| 2646 | * xmlRelaxNGDefaultTypeCompare: |
| 2647 | * @data: data needed for the library |
| 2648 | * @type: the type name |
| 2649 | * @value1: the first value |
| 2650 | * @value2: the second value |
| 2651 | * |
| 2652 | * Compare two values accordingly a type from the default |
| 2653 | * datatype library. |
| 2654 | * |
| 2655 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2656 | */ |
| 2657 | static int |
| 2658 | xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2659 | const xmlChar * type, |
| 2660 | const xmlChar * value1, |
| 2661 | xmlNodePtr ctxt1 ATTRIBUTE_UNUSED, |
| 2662 | void *comp1 ATTRIBUTE_UNUSED, |
| 2663 | const xmlChar * value2, |
| 2664 | xmlNodePtr ctxt2 ATTRIBUTE_UNUSED) |
| 2665 | { |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2666 | int ret = -1; |
| 2667 | |
| 2668 | if (xmlStrEqual(type, BAD_CAST "string")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2669 | ret = xmlStrEqual(value1, value2); |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2670 | } else if (xmlStrEqual(type, BAD_CAST "token")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2671 | if (!xmlStrEqual(value1, value2)) { |
| 2672 | xmlChar *nval, *nvalue; |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2673 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2674 | /* |
| 2675 | * TODO: trivial optimizations are possible by |
| 2676 | * computing at compile-time |
| 2677 | */ |
| 2678 | nval = xmlRelaxNGNormalize(NULL, value1); |
| 2679 | nvalue = xmlRelaxNGNormalize(NULL, value2); |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2680 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2681 | if ((nval == NULL) || (nvalue == NULL)) |
| 2682 | ret = -1; |
| 2683 | else if (xmlStrEqual(nval, nvalue)) |
| 2684 | ret = 1; |
| 2685 | else |
| 2686 | ret = 0; |
| 2687 | if (nval != NULL) |
| 2688 | xmlFree(nval); |
| 2689 | if (nvalue != NULL) |
| 2690 | xmlFree(nvalue); |
| 2691 | } else |
| 2692 | ret = 1; |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2693 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2694 | return (ret); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2695 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2696 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2697 | static int xmlRelaxNGTypeInitialized = 0; |
| 2698 | static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL; |
| 2699 | |
| 2700 | /** |
| 2701 | * xmlRelaxNGFreeTypeLibrary: |
| 2702 | * @lib: the type library structure |
| 2703 | * @namespace: the URI bound to the library |
| 2704 | * |
| 2705 | * Free the structure associated to the type library |
| 2706 | */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2707 | static void |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2708 | xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2709 | const xmlChar * namespace ATTRIBUTE_UNUSED) |
| 2710 | { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2711 | if (lib == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2712 | return; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2713 | if (lib->namespace != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2714 | xmlFree((xmlChar *) lib->namespace); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2715 | xmlFree(lib); |
| 2716 | } |
| 2717 | |
| 2718 | /** |
| 2719 | * xmlRelaxNGRegisterTypeLibrary: |
| 2720 | * @namespace: the URI bound to the library |
| 2721 | * @data: data associated to the library |
| 2722 | * @have: the provide function |
| 2723 | * @check: the checking function |
| 2724 | * @comp: the comparison function |
| 2725 | * |
| 2726 | * Register a new type library |
| 2727 | * |
| 2728 | * Returns 0 in case of success and -1 in case of error. |
| 2729 | */ |
| 2730 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2731 | xmlRelaxNGRegisterTypeLibrary(const xmlChar * namespace, void *data, |
| 2732 | xmlRelaxNGTypeHave have, |
| 2733 | xmlRelaxNGTypeCheck check, |
| 2734 | xmlRelaxNGTypeCompare comp, |
| 2735 | xmlRelaxNGFacetCheck facet, |
| 2736 | xmlRelaxNGTypeFree freef) |
| 2737 | { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2738 | xmlRelaxNGTypeLibraryPtr lib; |
| 2739 | int ret; |
| 2740 | |
| 2741 | if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2742 | (check == NULL) || (comp == NULL)) |
| 2743 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2744 | if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2745 | xmlGenericError(xmlGenericErrorContext, |
| 2746 | "Relax-NG types library '%s' already registered\n", |
| 2747 | namespace); |
| 2748 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2749 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2750 | lib = |
| 2751 | (xmlRelaxNGTypeLibraryPtr) |
| 2752 | xmlMalloc(sizeof(xmlRelaxNGTypeLibrary)); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2753 | if (lib == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2754 | xmlRngVErrMemory(NULL, "adding types library\n"); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2755 | return (-1); |
| 2756 | } |
| 2757 | memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary)); |
| 2758 | lib->namespace = xmlStrdup(namespace); |
| 2759 | lib->data = data; |
| 2760 | lib->have = have; |
| 2761 | lib->comp = comp; |
| 2762 | lib->check = check; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2763 | lib->facet = facet; |
| 2764 | lib->freef = freef; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2765 | ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib); |
| 2766 | if (ret < 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2767 | xmlGenericError(xmlGenericErrorContext, |
| 2768 | "Relax-NG types library failed to register '%s'\n", |
| 2769 | namespace); |
| 2770 | xmlRelaxNGFreeTypeLibrary(lib, namespace); |
| 2771 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2772 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2773 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2774 | } |
| 2775 | |
| 2776 | /** |
| 2777 | * xmlRelaxNGInitTypes: |
| 2778 | * |
| 2779 | * Initilize the default type libraries. |
| 2780 | * |
| 2781 | * Returns 0 in case of success and -1 in case of error. |
| 2782 | */ |
| 2783 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2784 | xmlRelaxNGInitTypes(void) |
| 2785 | { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2786 | if (xmlRelaxNGTypeInitialized != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2787 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2788 | xmlRelaxNGRegisteredTypes = xmlHashCreate(10); |
| 2789 | if (xmlRelaxNGRegisteredTypes == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2790 | xmlGenericError(xmlGenericErrorContext, |
| 2791 | "Failed to allocate sh table for Relax-NG types\n"); |
| 2792 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2793 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2794 | xmlRelaxNGRegisterTypeLibrary(BAD_CAST |
| 2795 | "http://www.w3.org/2001/XMLSchema-datatypes", |
| 2796 | NULL, xmlRelaxNGSchemaTypeHave, |
| 2797 | xmlRelaxNGSchemaTypeCheck, |
| 2798 | xmlRelaxNGSchemaTypeCompare, |
| 2799 | xmlRelaxNGSchemaFacetCheck, |
| 2800 | xmlRelaxNGSchemaFreeValue); |
| 2801 | xmlRelaxNGRegisterTypeLibrary(xmlRelaxNGNs, NULL, |
| 2802 | xmlRelaxNGDefaultTypeHave, |
| 2803 | xmlRelaxNGDefaultTypeCheck, |
| 2804 | xmlRelaxNGDefaultTypeCompare, NULL, |
| 2805 | NULL); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2806 | xmlRelaxNGTypeInitialized = 1; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2807 | return (0); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2808 | } |
| 2809 | |
| 2810 | /** |
| 2811 | * xmlRelaxNGCleanupTypes: |
| 2812 | * |
| 2813 | * Cleanup the default Schemas type library associated to RelaxNG |
| 2814 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2815 | void |
| 2816 | xmlRelaxNGCleanupTypes(void) |
| 2817 | { |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 2818 | xmlSchemaCleanupTypes(); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2819 | if (xmlRelaxNGTypeInitialized == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2820 | return; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2821 | xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2822 | xmlRelaxNGFreeTypeLibrary); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2823 | xmlRelaxNGTypeInitialized = 0; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2824 | } |
| 2825 | |
| 2826 | /************************************************************************ |
| 2827 | * * |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2828 | * Compiling element content into regexp * |
| 2829 | * * |
| 2830 | * Sometime the element content can be compiled into a pure regexp, * |
| 2831 | * This allows a faster execution and streamability at that level * |
| 2832 | * * |
| 2833 | ************************************************************************/ |
| 2834 | |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2835 | static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, |
| 2836 | xmlRelaxNGDefinePtr def); |
| 2837 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2838 | /** |
| 2839 | * xmlRelaxNGIsCompileable: |
| 2840 | * @define: the definition to check |
| 2841 | * |
| 2842 | * Check if a definition is nullable. |
| 2843 | * |
| 2844 | * Returns 1 if yes, 0 if no and -1 in case of error |
| 2845 | */ |
| 2846 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2847 | xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def) |
| 2848 | { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2849 | int ret = -1; |
| 2850 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2851 | if (def == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2852 | return (-1); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2853 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2854 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 2855 | (def->dflags & IS_COMPILABLE)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2856 | return (1); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2857 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 2858 | (def->dflags & IS_NOT_COMPILABLE)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2859 | return (0); |
| 2860 | switch (def->type) { |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2861 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2862 | ret = xmlRelaxNGIsCompileable(def->content); |
| 2863 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2864 | case XML_RELAXNG_TEXT: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2865 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2866 | ret = 1; |
| 2867 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2868 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2869 | /* |
| 2870 | * Check if the element content is compileable |
| 2871 | */ |
| 2872 | if (((def->dflags & IS_NOT_COMPILABLE) == 0) && |
| 2873 | ((def->dflags & IS_COMPILABLE) == 0)) { |
| 2874 | xmlRelaxNGDefinePtr list; |
| 2875 | |
| 2876 | list = def->content; |
| 2877 | while (list != NULL) { |
| 2878 | ret = xmlRelaxNGIsCompileable(list); |
| 2879 | if (ret != 1) |
| 2880 | break; |
| 2881 | list = list->next; |
| 2882 | } |
| 2883 | if (ret == 0) |
| 2884 | def->dflags |= IS_NOT_COMPILABLE; |
| 2885 | if (ret == 1) |
| 2886 | def->dflags |= IS_COMPILABLE; |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2887 | #ifdef DEBUG_COMPILE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2888 | if (ret == 1) { |
| 2889 | xmlGenericError(xmlGenericErrorContext, |
| 2890 | "element content for %s is compilable\n", |
| 2891 | def->name); |
| 2892 | } else if (ret == 0) { |
| 2893 | xmlGenericError(xmlGenericErrorContext, |
| 2894 | "element content for %s is not compilable\n", |
| 2895 | def->name); |
| 2896 | } else { |
| 2897 | xmlGenericError(xmlGenericErrorContext, |
| 2898 | "Problem in RelaxNGIsCompileable for element %s\n", |
| 2899 | def->name); |
| 2900 | } |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2901 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2902 | } |
| 2903 | /* |
| 2904 | * All elements return a compileable status unless they |
| 2905 | * are generic like anyName |
| 2906 | */ |
| 2907 | if ((def->nameClass != NULL) || (def->name == NULL)) |
| 2908 | ret = 0; |
| 2909 | else |
| 2910 | ret = 1; |
| 2911 | return (ret); |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2912 | case XML_RELAXNG_REF: |
| 2913 | case XML_RELAXNG_EXTERNALREF: |
| 2914 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2915 | if (def->depth == -20) { |
| 2916 | return (1); |
| 2917 | } else { |
| 2918 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2919 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2920 | def->depth = -20; |
| 2921 | list = def->content; |
| 2922 | while (list != NULL) { |
| 2923 | ret = xmlRelaxNGIsCompileable(list); |
| 2924 | if (ret != 1) |
| 2925 | break; |
| 2926 | list = list->next; |
| 2927 | } |
| 2928 | } |
| 2929 | break; |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2930 | case XML_RELAXNG_START: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2931 | case XML_RELAXNG_OPTIONAL: |
| 2932 | case XML_RELAXNG_ZEROORMORE: |
| 2933 | case XML_RELAXNG_ONEORMORE: |
| 2934 | case XML_RELAXNG_CHOICE: |
| 2935 | case XML_RELAXNG_GROUP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2936 | case XML_RELAXNG_DEF:{ |
| 2937 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2938 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2939 | list = def->content; |
| 2940 | while (list != NULL) { |
| 2941 | ret = xmlRelaxNGIsCompileable(list); |
| 2942 | if (ret != 1) |
| 2943 | break; |
| 2944 | list = list->next; |
| 2945 | } |
| 2946 | break; |
| 2947 | } |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2948 | case XML_RELAXNG_EXCEPT: |
| 2949 | case XML_RELAXNG_ATTRIBUTE: |
| 2950 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2951 | case XML_RELAXNG_DATATYPE: |
| 2952 | case XML_RELAXNG_LIST: |
| 2953 | case XML_RELAXNG_PARAM: |
| 2954 | case XML_RELAXNG_VALUE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2955 | ret = 0; |
| 2956 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2957 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2958 | ret = -1; |
| 2959 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2960 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2961 | if (ret == 0) |
| 2962 | def->dflags |= IS_NOT_COMPILABLE; |
| 2963 | if (ret == 1) |
| 2964 | def->dflags |= IS_COMPILABLE; |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2965 | #ifdef DEBUG_COMPILE |
| 2966 | if (ret == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2967 | xmlGenericError(xmlGenericErrorContext, |
| 2968 | "RelaxNGIsCompileable %s : true\n", |
| 2969 | xmlRelaxNGDefName(def)); |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2970 | } else if (ret == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2971 | xmlGenericError(xmlGenericErrorContext, |
| 2972 | "RelaxNGIsCompileable %s : false\n", |
| 2973 | xmlRelaxNGDefName(def)); |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2974 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2975 | xmlGenericError(xmlGenericErrorContext, |
| 2976 | "Problem in RelaxNGIsCompileable %s\n", |
| 2977 | xmlRelaxNGDefName(def)); |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2978 | } |
| 2979 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2980 | return (ret); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2981 | } |
| 2982 | |
| 2983 | /** |
| 2984 | * xmlRelaxNGCompile: |
| 2985 | * ctxt: the RelaxNG parser context |
| 2986 | * @define: the definition tree to compile |
| 2987 | * |
| 2988 | * Compile the set of definitions, it works recursively, till the |
| 2989 | * element boundaries, where it tries to compile the content if possible |
| 2990 | * |
| 2991 | * Returns 0 if success and -1 in case of error |
| 2992 | */ |
| 2993 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2994 | xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) |
| 2995 | { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2996 | int ret = 0; |
| 2997 | xmlRelaxNGDefinePtr list; |
| 2998 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2999 | if ((ctxt == NULL) || (def == NULL)) |
| 3000 | return (-1); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3001 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3002 | switch (def->type) { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3003 | case XML_RELAXNG_START: |
| 3004 | if ((xmlRelaxNGIsCompileable(def) == 1) && (def->depth != -25)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3005 | xmlAutomataPtr oldam = ctxt->am; |
| 3006 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3007 | |
| 3008 | def->depth = -25; |
| 3009 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3010 | list = def->content; |
| 3011 | ctxt->am = xmlNewAutomata(); |
| 3012 | if (ctxt->am == NULL) |
| 3013 | return (-1); |
| 3014 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
| 3015 | while (list != NULL) { |
| 3016 | xmlRelaxNGCompile(ctxt, list); |
| 3017 | list = list->next; |
| 3018 | } |
| 3019 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
| 3020 | def->contModel = xmlAutomataCompile(ctxt->am); |
| 3021 | xmlRegexpIsDeterminist(def->contModel); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3022 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3023 | xmlFreeAutomata(ctxt->am); |
| 3024 | ctxt->state = oldstate; |
| 3025 | ctxt->am = oldam; |
| 3026 | } |
| 3027 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3028 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3029 | if ((ctxt->am != NULL) && (def->name != NULL)) { |
| 3030 | ctxt->state = xmlAutomataNewTransition2(ctxt->am, |
| 3031 | ctxt->state, NULL, |
| 3032 | def->name, def->ns, |
| 3033 | def); |
| 3034 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3035 | if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3036 | xmlAutomataPtr oldam = ctxt->am; |
| 3037 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3038 | |
| 3039 | def->depth = -25; |
| 3040 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3041 | list = def->content; |
| 3042 | ctxt->am = xmlNewAutomata(); |
| 3043 | if (ctxt->am == NULL) |
| 3044 | return (-1); |
| 3045 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
| 3046 | while (list != NULL) { |
| 3047 | xmlRelaxNGCompile(ctxt, list); |
| 3048 | list = list->next; |
| 3049 | } |
| 3050 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
| 3051 | def->contModel = xmlAutomataCompile(ctxt->am); |
| 3052 | if (!xmlRegexpIsDeterminist(def->contModel)) { |
| 3053 | /* |
| 3054 | * we can only use the automata if it is determinist |
| 3055 | */ |
| 3056 | xmlRegFreeRegexp(def->contModel); |
| 3057 | def->contModel = NULL; |
| 3058 | } |
| 3059 | xmlFreeAutomata(ctxt->am); |
| 3060 | ctxt->state = oldstate; |
| 3061 | ctxt->am = oldam; |
| 3062 | } else { |
| 3063 | xmlAutomataPtr oldam = ctxt->am; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3064 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3065 | /* |
| 3066 | * we can't build the content model for this element content |
| 3067 | * but it still might be possible to build it for some of its |
| 3068 | * children, recurse. |
| 3069 | */ |
| 3070 | ret = xmlRelaxNGTryCompile(ctxt, def); |
| 3071 | ctxt->am = oldam; |
| 3072 | } |
| 3073 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3074 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3075 | ret = xmlRelaxNGCompile(ctxt, def->content); |
| 3076 | break; |
| 3077 | case XML_RELAXNG_OPTIONAL:{ |
| 3078 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3079 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3080 | xmlRelaxNGCompile(ctxt, def->content); |
| 3081 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
| 3082 | break; |
| 3083 | } |
| 3084 | case XML_RELAXNG_ZEROORMORE:{ |
| 3085 | xmlAutomataStatePtr oldstate; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3086 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3087 | ctxt->state = |
| 3088 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 3089 | oldstate = ctxt->state; |
| 3090 | list = def->content; |
| 3091 | while (list != NULL) { |
| 3092 | xmlRelaxNGCompile(ctxt, list); |
| 3093 | list = list->next; |
| 3094 | } |
| 3095 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate); |
| 3096 | ctxt->state = |
| 3097 | xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 3098 | break; |
| 3099 | } |
| 3100 | case XML_RELAXNG_ONEORMORE:{ |
| 3101 | xmlAutomataStatePtr oldstate; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3102 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3103 | list = def->content; |
| 3104 | while (list != NULL) { |
| 3105 | xmlRelaxNGCompile(ctxt, list); |
| 3106 | list = list->next; |
| 3107 | } |
| 3108 | oldstate = ctxt->state; |
| 3109 | list = def->content; |
| 3110 | while (list != NULL) { |
| 3111 | xmlRelaxNGCompile(ctxt, list); |
| 3112 | list = list->next; |
| 3113 | } |
| 3114 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate); |
| 3115 | ctxt->state = |
| 3116 | xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 3117 | break; |
| 3118 | } |
| 3119 | case XML_RELAXNG_CHOICE:{ |
| 3120 | xmlAutomataStatePtr target = NULL; |
| 3121 | xmlAutomataStatePtr oldstate = ctxt->state; |
| 3122 | |
| 3123 | list = def->content; |
| 3124 | while (list != NULL) { |
| 3125 | ctxt->state = oldstate; |
| 3126 | ret = xmlRelaxNGCompile(ctxt, list); |
| 3127 | if (ret != 0) |
| 3128 | break; |
| 3129 | if (target == NULL) |
| 3130 | target = ctxt->state; |
| 3131 | else { |
| 3132 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, |
| 3133 | target); |
| 3134 | } |
| 3135 | list = list->next; |
| 3136 | } |
| 3137 | ctxt->state = target; |
| 3138 | |
| 3139 | break; |
| 3140 | } |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3141 | case XML_RELAXNG_REF: |
| 3142 | case XML_RELAXNG_EXTERNALREF: |
| 3143 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3144 | case XML_RELAXNG_GROUP: |
| 3145 | case XML_RELAXNG_DEF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3146 | list = def->content; |
| 3147 | while (list != NULL) { |
| 3148 | ret = xmlRelaxNGCompile(ctxt, list); |
| 3149 | if (ret != 0) |
| 3150 | break; |
| 3151 | list = list->next; |
| 3152 | } |
| 3153 | break; |
| 3154 | case XML_RELAXNG_TEXT:{ |
| 3155 | xmlAutomataStatePtr oldstate; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3156 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3157 | ctxt->state = |
| 3158 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 3159 | oldstate = ctxt->state; |
| 3160 | xmlRelaxNGCompile(ctxt, def->content); |
| 3161 | xmlAutomataNewTransition(ctxt->am, ctxt->state, |
| 3162 | ctxt->state, BAD_CAST "#text", |
| 3163 | NULL); |
| 3164 | ctxt->state = |
| 3165 | xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 3166 | break; |
| 3167 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3168 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3169 | ctxt->state = |
| 3170 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 3171 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3172 | case XML_RELAXNG_EXCEPT: |
| 3173 | case XML_RELAXNG_ATTRIBUTE: |
| 3174 | case XML_RELAXNG_INTERLEAVE: |
| 3175 | case XML_RELAXNG_NOT_ALLOWED: |
| 3176 | case XML_RELAXNG_DATATYPE: |
| 3177 | case XML_RELAXNG_LIST: |
| 3178 | case XML_RELAXNG_PARAM: |
| 3179 | case XML_RELAXNG_VALUE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3180 | /* This should not happen and generate an internal error */ |
| 3181 | fprintf(stderr, "RNG internal error trying to compile %s\n", |
| 3182 | xmlRelaxNGDefName(def)); |
| 3183 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3184 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3185 | return (ret); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3186 | } |
| 3187 | |
| 3188 | /** |
| 3189 | * xmlRelaxNGTryCompile: |
| 3190 | * ctxt: the RelaxNG parser context |
| 3191 | * @define: the definition tree to compile |
| 3192 | * |
| 3193 | * Try to compile the set of definitions, it works recursively, |
| 3194 | * possibly ignoring parts which cannot be compiled. |
| 3195 | * |
| 3196 | * Returns 0 if success and -1 in case of error |
| 3197 | */ |
| 3198 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3199 | xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) |
| 3200 | { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3201 | int ret = 0; |
| 3202 | xmlRelaxNGDefinePtr list; |
| 3203 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3204 | if ((ctxt == NULL) || (def == NULL)) |
| 3205 | return (-1); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3206 | |
| 3207 | if ((def->type == XML_RELAXNG_START) || |
| 3208 | (def->type == XML_RELAXNG_ELEMENT)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3209 | ret = xmlRelaxNGIsCompileable(def); |
| 3210 | if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) { |
| 3211 | ctxt->am = NULL; |
| 3212 | ret = xmlRelaxNGCompile(ctxt, def); |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3213 | #ifdef DEBUG_PROGRESSIVE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3214 | if (ret == 0) { |
| 3215 | if (def->type == XML_RELAXNG_START) |
| 3216 | xmlGenericError(xmlGenericErrorContext, |
| 3217 | "compiled the start\n"); |
| 3218 | else |
| 3219 | xmlGenericError(xmlGenericErrorContext, |
| 3220 | "compiled element %s\n", def->name); |
| 3221 | } else { |
| 3222 | if (def->type == XML_RELAXNG_START) |
| 3223 | xmlGenericError(xmlGenericErrorContext, |
| 3224 | "failed to compile the start\n"); |
| 3225 | else |
| 3226 | xmlGenericError(xmlGenericErrorContext, |
| 3227 | "failed to compile element %s\n", |
| 3228 | def->name); |
| 3229 | } |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3230 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3231 | return (ret); |
| 3232 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3233 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3234 | switch (def->type) { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3235 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3236 | ret = xmlRelaxNGTryCompile(ctxt, def->content); |
| 3237 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3238 | case XML_RELAXNG_TEXT: |
| 3239 | case XML_RELAXNG_DATATYPE: |
| 3240 | case XML_RELAXNG_LIST: |
| 3241 | case XML_RELAXNG_PARAM: |
| 3242 | case XML_RELAXNG_VALUE: |
| 3243 | case XML_RELAXNG_EMPTY: |
| 3244 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3245 | ret = 0; |
| 3246 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3247 | case XML_RELAXNG_OPTIONAL: |
| 3248 | case XML_RELAXNG_ZEROORMORE: |
| 3249 | case XML_RELAXNG_ONEORMORE: |
| 3250 | case XML_RELAXNG_CHOICE: |
| 3251 | case XML_RELAXNG_GROUP: |
| 3252 | case XML_RELAXNG_DEF: |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3253 | case XML_RELAXNG_START: |
| 3254 | case XML_RELAXNG_REF: |
| 3255 | case XML_RELAXNG_EXTERNALREF: |
| 3256 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3257 | list = def->content; |
| 3258 | while (list != NULL) { |
| 3259 | ret = xmlRelaxNGTryCompile(ctxt, list); |
| 3260 | if (ret != 0) |
| 3261 | break; |
| 3262 | list = list->next; |
| 3263 | } |
| 3264 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3265 | case XML_RELAXNG_EXCEPT: |
| 3266 | case XML_RELAXNG_ATTRIBUTE: |
| 3267 | case XML_RELAXNG_INTERLEAVE: |
| 3268 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3269 | ret = 0; |
| 3270 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3271 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3272 | return (ret); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 3273 | } |
| 3274 | |
| 3275 | /************************************************************************ |
| 3276 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3277 | * Parsing functions * |
| 3278 | * * |
| 3279 | ************************************************************************/ |
| 3280 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3281 | static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr |
| 3282 | ctxt, xmlNodePtr node); |
| 3283 | static xmlRelaxNGDefinePtr xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr |
| 3284 | ctxt, xmlNodePtr node); |
| 3285 | static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr |
| 3286 | ctxt, xmlNodePtr nodes, |
| 3287 | int group); |
| 3288 | static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr |
| 3289 | ctxt, xmlNodePtr node); |
| 3290 | static xmlRelaxNGPtr xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, |
| 3291 | xmlNodePtr node); |
| 3292 | static int xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt, |
| 3293 | xmlNodePtr nodes); |
| 3294 | static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr |
| 3295 | ctxt, xmlNodePtr node, |
| 3296 | xmlRelaxNGDefinePtr |
| 3297 | def); |
| 3298 | static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr |
| 3299 | ctxt, xmlNodePtr nodes); |
| 3300 | static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 3301 | xmlRelaxNGDefinePtr define, |
| 3302 | xmlNodePtr elem); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3303 | |
| 3304 | |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 3305 | #define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content)) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3306 | |
| 3307 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3308 | * xmlRelaxNGIsNullable: |
| 3309 | * @define: the definition to verify |
| 3310 | * |
| 3311 | * Check if a definition is nullable. |
| 3312 | * |
| 3313 | * Returns 1 if yes, 0 if no and -1 in case of error |
| 3314 | */ |
| 3315 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3316 | xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define) |
| 3317 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3318 | int ret; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3319 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3320 | if (define == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3321 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3322 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3323 | if (define->dflags & IS_NULLABLE) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3324 | return (1); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3325 | if (define->dflags & IS_NOT_NULLABLE) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3326 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3327 | switch (define->type) { |
| 3328 | case XML_RELAXNG_EMPTY: |
| 3329 | case XML_RELAXNG_TEXT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3330 | ret = 1; |
| 3331 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3332 | case XML_RELAXNG_NOOP: |
| 3333 | case XML_RELAXNG_DEF: |
| 3334 | case XML_RELAXNG_REF: |
| 3335 | case XML_RELAXNG_EXTERNALREF: |
| 3336 | case XML_RELAXNG_PARENTREF: |
| 3337 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3338 | ret = xmlRelaxNGIsNullable(define->content); |
| 3339 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3340 | case XML_RELAXNG_EXCEPT: |
| 3341 | case XML_RELAXNG_NOT_ALLOWED: |
| 3342 | case XML_RELAXNG_ELEMENT: |
| 3343 | case XML_RELAXNG_DATATYPE: |
| 3344 | case XML_RELAXNG_PARAM: |
| 3345 | case XML_RELAXNG_VALUE: |
| 3346 | case XML_RELAXNG_LIST: |
| 3347 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3348 | ret = 0; |
| 3349 | break; |
| 3350 | case XML_RELAXNG_CHOICE:{ |
| 3351 | xmlRelaxNGDefinePtr list = define->content; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3352 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3353 | while (list != NULL) { |
| 3354 | ret = xmlRelaxNGIsNullable(list); |
| 3355 | if (ret != 0) |
| 3356 | goto done; |
| 3357 | list = list->next; |
| 3358 | } |
| 3359 | ret = 0; |
| 3360 | break; |
| 3361 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3362 | case XML_RELAXNG_START: |
| 3363 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3364 | case XML_RELAXNG_GROUP:{ |
| 3365 | xmlRelaxNGDefinePtr list = define->content; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3366 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3367 | while (list != NULL) { |
| 3368 | ret = xmlRelaxNGIsNullable(list); |
| 3369 | if (ret != 1) |
| 3370 | goto done; |
| 3371 | list = list->next; |
| 3372 | } |
| 3373 | return (1); |
| 3374 | } |
| 3375 | default: |
| 3376 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3377 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3378 | done: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3379 | if (ret == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3380 | define->dflags |= IS_NOT_NULLABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3381 | if (ret == 1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3382 | define->dflags |= IS_NULLABLE; |
| 3383 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3384 | } |
| 3385 | |
| 3386 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3387 | * xmlRelaxNGIsBlank: |
| 3388 | * @str: a string |
| 3389 | * |
| 3390 | * Check if a string is ignorable c.f. 4.2. Whitespace |
| 3391 | * |
| 3392 | * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise |
| 3393 | */ |
| 3394 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3395 | xmlRelaxNGIsBlank(xmlChar * str) |
| 3396 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3397 | if (str == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3398 | return (1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3399 | while (*str != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3400 | if (!(IS_BLANK_CH(*str))) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3401 | return (0); |
| 3402 | str++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3403 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3404 | return (1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3405 | } |
| 3406 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3407 | /** |
| 3408 | * xmlRelaxNGGetDataTypeLibrary: |
| 3409 | * @ctxt: a Relax-NG parser context |
| 3410 | * @node: the current data or value element |
| 3411 | * |
| 3412 | * Applies algorithm from 4.3. datatypeLibrary attribute |
| 3413 | * |
| 3414 | * Returns the datatypeLibary value or NULL if not found |
| 3415 | */ |
| 3416 | static xmlChar * |
| 3417 | xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3418 | xmlNodePtr node) |
| 3419 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3420 | xmlChar *ret, *escape; |
| 3421 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3422 | if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3423 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
| 3424 | if (ret != NULL) { |
| 3425 | if (ret[0] == 0) { |
| 3426 | xmlFree(ret); |
| 3427 | return (NULL); |
| 3428 | } |
| 3429 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
| 3430 | if (escape == NULL) { |
| 3431 | return (ret); |
| 3432 | } |
| 3433 | xmlFree(ret); |
| 3434 | return (escape); |
| 3435 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3436 | } |
| 3437 | node = node->parent; |
| 3438 | while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3439 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
| 3440 | if (ret != NULL) { |
| 3441 | if (ret[0] == 0) { |
| 3442 | xmlFree(ret); |
| 3443 | return (NULL); |
| 3444 | } |
| 3445 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
| 3446 | if (escape == NULL) { |
| 3447 | return (ret); |
| 3448 | } |
| 3449 | xmlFree(ret); |
| 3450 | return (escape); |
| 3451 | } |
| 3452 | node = node->parent; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3453 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3454 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3455 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3456 | |
| 3457 | /** |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3458 | * xmlRelaxNGParseValue: |
| 3459 | * @ctxt: a Relax-NG parser context |
| 3460 | * @node: the data node. |
| 3461 | * |
| 3462 | * parse the content of a RelaxNG value node. |
| 3463 | * |
| 3464 | * Returns the definition pointer or NULL in case of error |
| 3465 | */ |
| 3466 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3467 | xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 3468 | { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3469 | xmlRelaxNGDefinePtr def = NULL; |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 3470 | xmlRelaxNGTypeLibraryPtr lib = NULL; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3471 | xmlChar *type; |
| 3472 | xmlChar *library; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3473 | int success = 0; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3474 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3475 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3476 | if (def == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3477 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3478 | def->type = XML_RELAXNG_VALUE; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3479 | |
| 3480 | type = xmlGetProp(node, BAD_CAST "type"); |
| 3481 | if (type != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3482 | xmlRelaxNGNormExtSpace(type); |
| 3483 | if (xmlValidateNCName(type, 0)) { |
| 3484 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE, |
| 3485 | "value type '%s' is not an NCName\n", type, NULL); |
| 3486 | } |
| 3487 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
| 3488 | if (library == NULL) |
| 3489 | library = |
| 3490 | xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3491 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3492 | def->name = type; |
| 3493 | def->ns = library; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3494 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3495 | lib = (xmlRelaxNGTypeLibraryPtr) |
| 3496 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
| 3497 | if (lib == NULL) { |
| 3498 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB, |
| 3499 | "Use of unregistered type library '%s'\n", library, |
| 3500 | NULL); |
| 3501 | def->data = NULL; |
| 3502 | } else { |
| 3503 | def->data = lib; |
| 3504 | if (lib->have == NULL) { |
| 3505 | xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB, |
| 3506 | "Internal error with type library '%s': no 'have'\n", |
| 3507 | library, NULL); |
| 3508 | } else { |
| 3509 | success = lib->have(lib->data, def->name); |
| 3510 | if (success != 1) { |
| 3511 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND, |
| 3512 | "Error type '%s' is not exported by type library '%s'\n", |
| 3513 | def->name, library); |
| 3514 | } |
| 3515 | } |
| 3516 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3517 | } |
| 3518 | if (node->children == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3519 | def->value = xmlStrdup(BAD_CAST ""); |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 3520 | } else if (((node->children->type != XML_TEXT_NODE) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3521 | (node->children->type != XML_CDATA_SECTION_NODE)) || |
| 3522 | (node->children->next != NULL)) { |
| 3523 | xmlRngPErr(ctxt, node, XML_RNGP_TEXT_EXPECTED, |
| 3524 | "Expecting a single text value for <value>content\n", |
| 3525 | NULL, NULL); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3526 | } else if (def != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3527 | def->value = xmlNodeGetContent(node); |
| 3528 | if (def->value == NULL) { |
| 3529 | xmlRngPErr(ctxt, node, XML_RNGP_VALUE_NO_CONTENT, |
| 3530 | "Element <value> has no content\n", NULL, NULL); |
| 3531 | } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) { |
| 3532 | void *val = NULL; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3533 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3534 | success = |
| 3535 | lib->check(lib->data, def->name, def->value, &val, node); |
| 3536 | if (success != 1) { |
| 3537 | xmlRngPErr(ctxt, node, XML_RNGP_INVALID_VALUE, |
| 3538 | "Value '%s' is not acceptable for type '%s'\n", |
| 3539 | def->value, def->name); |
| 3540 | } else { |
| 3541 | if (val != NULL) |
| 3542 | def->attrs = val; |
| 3543 | } |
| 3544 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3545 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3546 | return (def); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3547 | } |
| 3548 | |
| 3549 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3550 | * xmlRelaxNGParseData: |
| 3551 | * @ctxt: a Relax-NG parser context |
| 3552 | * @node: the data node. |
| 3553 | * |
| 3554 | * parse the content of a RelaxNG data node. |
| 3555 | * |
| 3556 | * Returns the definition pointer or NULL in case of error |
| 3557 | */ |
| 3558 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3559 | xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 3560 | { |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3561 | xmlRelaxNGDefinePtr def = NULL, except, last = NULL; |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 3562 | xmlRelaxNGDefinePtr param, lastparam = NULL; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3563 | xmlRelaxNGTypeLibraryPtr lib; |
| 3564 | xmlChar *type; |
| 3565 | xmlChar *library; |
| 3566 | xmlNodePtr content; |
| 3567 | int tmp; |
| 3568 | |
| 3569 | type = xmlGetProp(node, BAD_CAST "type"); |
| 3570 | if (type == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3571 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_MISSING, "data has no type\n", NULL, |
| 3572 | NULL); |
| 3573 | return (NULL); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3574 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3575 | xmlRelaxNGNormExtSpace(type); |
| 3576 | if (xmlValidateNCName(type, 0)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3577 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE, |
| 3578 | "data type '%s' is not an NCName\n", type, NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3579 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3580 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
| 3581 | if (library == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3582 | library = |
| 3583 | xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3584 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3585 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3586 | if (def == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3587 | xmlFree(type); |
| 3588 | return (NULL); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3589 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3590 | def->type = XML_RELAXNG_DATATYPE; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3591 | def->name = type; |
| 3592 | def->ns = library; |
| 3593 | |
| 3594 | lib = (xmlRelaxNGTypeLibraryPtr) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3595 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3596 | if (lib == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3597 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB, |
| 3598 | "Use of unregistered type library '%s'\n", library, |
| 3599 | NULL); |
| 3600 | def->data = NULL; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3601 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3602 | def->data = lib; |
| 3603 | if (lib->have == NULL) { |
| 3604 | xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB, |
| 3605 | "Internal error with type library '%s': no 'have'\n", |
| 3606 | library, NULL); |
| 3607 | } else { |
| 3608 | tmp = lib->have(lib->data, def->name); |
| 3609 | if (tmp != 1) { |
| 3610 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND, |
| 3611 | "Error type '%s' is not exported by type library '%s'\n", |
| 3612 | def->name, library); |
| 3613 | } else |
| 3614 | if ((xmlStrEqual |
| 3615 | (library, |
| 3616 | BAD_CAST |
| 3617 | "http://www.w3.org/2001/XMLSchema-datatypes")) |
| 3618 | && ((xmlStrEqual(def->name, BAD_CAST "IDREF")) |
| 3619 | || (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) { |
| 3620 | ctxt->idref = 1; |
| 3621 | } |
| 3622 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3623 | } |
| 3624 | content = node->children; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3625 | |
| 3626 | /* |
| 3627 | * Handle optional params |
| 3628 | */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3629 | while (content != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3630 | if (!xmlStrEqual(content->name, BAD_CAST "param")) |
| 3631 | break; |
| 3632 | if (xmlStrEqual(library, |
| 3633 | BAD_CAST "http://relaxng.org/ns/structure/1.0")) { |
| 3634 | xmlRngPErr(ctxt, node, XML_RNGP_PARAM_FORBIDDEN, |
| 3635 | "Type library '%s' does not allow type parameters\n", |
| 3636 | library, NULL); |
| 3637 | content = content->next; |
| 3638 | while ((content != NULL) && |
| 3639 | (xmlStrEqual(content->name, BAD_CAST "param"))) |
| 3640 | content = content->next; |
| 3641 | } else { |
| 3642 | param = xmlRelaxNGNewDefine(ctxt, node); |
| 3643 | if (param != NULL) { |
| 3644 | param->type = XML_RELAXNG_PARAM; |
| 3645 | param->name = xmlGetProp(content, BAD_CAST "name"); |
| 3646 | if (param->name == NULL) { |
| 3647 | xmlRngPErr(ctxt, node, XML_RNGP_PARAM_NAME_MISSING, |
| 3648 | "param has no name\n", NULL, NULL); |
| 3649 | } |
| 3650 | param->value = xmlNodeGetContent(content); |
| 3651 | if (lastparam == NULL) { |
| 3652 | def->attrs = lastparam = param; |
| 3653 | } else { |
| 3654 | lastparam->next = param; |
| 3655 | lastparam = param; |
| 3656 | } |
| 3657 | if (lib != NULL) { |
| 3658 | } |
| 3659 | } |
| 3660 | content = content->next; |
| 3661 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3662 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3663 | /* |
| 3664 | * Handle optional except |
| 3665 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3666 | if ((content != NULL) |
| 3667 | && (xmlStrEqual(content->name, BAD_CAST "except"))) { |
| 3668 | xmlNodePtr child; |
| 3669 | xmlRelaxNGDefinePtr tmp2, last2 = NULL; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3670 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3671 | except = xmlRelaxNGNewDefine(ctxt, node); |
| 3672 | if (except == NULL) { |
| 3673 | return (def); |
| 3674 | } |
| 3675 | except->type = XML_RELAXNG_EXCEPT; |
| 3676 | child = content->children; |
| 3677 | if (last == NULL) { |
| 3678 | def->content = except; |
| 3679 | } else { |
| 3680 | last->next = except; |
| 3681 | } |
| 3682 | if (child == NULL) { |
| 3683 | xmlRngPErr(ctxt, content, XML_RNGP_EXCEPT_NO_CONTENT, |
| 3684 | "except has no content\n", NULL, NULL); |
| 3685 | } |
| 3686 | while (child != NULL) { |
| 3687 | tmp2 = xmlRelaxNGParsePattern(ctxt, child); |
| 3688 | if (tmp2 != NULL) { |
| 3689 | if (last2 == NULL) { |
| 3690 | except->content = last2 = tmp2; |
| 3691 | } else { |
| 3692 | last2->next = tmp2; |
| 3693 | last2 = tmp2; |
| 3694 | } |
| 3695 | } |
| 3696 | child = child->next; |
| 3697 | } |
| 3698 | content = content->next; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3699 | } |
| 3700 | /* |
| 3701 | * Check there is no unhandled data |
| 3702 | */ |
| 3703 | if (content != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3704 | xmlRngPErr(ctxt, content, XML_RNGP_DATA_CONTENT, |
| 3705 | "Element data has unexpected content %s\n", |
| 3706 | content->name, NULL); |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3707 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3708 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3709 | return (def); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3710 | } |
| 3711 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3712 | static const xmlChar *invalidName = BAD_CAST "\1"; |
| 3713 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3714 | /** |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3715 | * xmlRelaxNGCompareNameClasses: |
| 3716 | * @defs1: the first element/attribute defs |
| 3717 | * @defs2: the second element/attribute defs |
| 3718 | * @name: the restriction on the name |
| 3719 | * @ns: the restriction on the namespace |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3720 | * |
| 3721 | * Compare the 2 lists of element definitions. The comparison is |
| 3722 | * that if both lists do not accept the same QNames, it returns 1 |
| 3723 | * If the 2 lists can accept the same QName the comparison returns 0 |
| 3724 | * |
| 3725 | * Returns 1 disttinct, 0 if equal |
| 3726 | */ |
| 3727 | static int |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3728 | xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3729 | xmlRelaxNGDefinePtr def2) |
| 3730 | { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3731 | int ret = 1; |
| 3732 | xmlNode node; |
| 3733 | xmlNs ns; |
| 3734 | xmlRelaxNGValidCtxt ctxt; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3735 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3736 | ctxt.flags = FLAGS_IGNORABLE; |
| 3737 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 3738 | memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt)); |
| 3739 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3740 | if ((def1->type == XML_RELAXNG_ELEMENT) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3741 | (def1->type == XML_RELAXNG_ATTRIBUTE)) { |
| 3742 | if (def2->type == XML_RELAXNG_TEXT) |
| 3743 | return (1); |
| 3744 | if (def1->name != NULL) { |
| 3745 | node.name = def1->name; |
| 3746 | } else { |
| 3747 | node.name = invalidName; |
| 3748 | } |
| 3749 | node.ns = &ns; |
| 3750 | if (def1->ns != NULL) { |
| 3751 | if (def1->ns[0] == 0) { |
| 3752 | node.ns = NULL; |
| 3753 | } else { |
| 3754 | ns.href = def1->ns; |
| 3755 | } |
| 3756 | } else { |
| 3757 | ns.href = invalidName; |
| 3758 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3759 | if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3760 | if (def1->nameClass != NULL) { |
| 3761 | ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2); |
| 3762 | } else { |
| 3763 | ret = 0; |
| 3764 | } |
| 3765 | } else { |
| 3766 | ret = 1; |
| 3767 | } |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3768 | } else if (def1->type == XML_RELAXNG_TEXT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3769 | if (def2->type == XML_RELAXNG_TEXT) |
| 3770 | return (0); |
| 3771 | return (1); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3772 | } else if (def1->type == XML_RELAXNG_EXCEPT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3773 | TODO ret = 0; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3774 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3775 | TODO ret = 0; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3776 | } |
| 3777 | if (ret == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3778 | return (ret); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3779 | if ((def2->type == XML_RELAXNG_ELEMENT) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3780 | (def2->type == XML_RELAXNG_ATTRIBUTE)) { |
| 3781 | if (def2->name != NULL) { |
| 3782 | node.name = def2->name; |
| 3783 | } else { |
| 3784 | node.name = invalidName; |
| 3785 | } |
| 3786 | node.ns = &ns; |
| 3787 | if (def2->ns != NULL) { |
| 3788 | if (def2->ns[0] == 0) { |
| 3789 | node.ns = NULL; |
| 3790 | } else { |
| 3791 | ns.href = def2->ns; |
| 3792 | } |
| 3793 | } else { |
| 3794 | ns.href = invalidName; |
| 3795 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3796 | if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3797 | if (def2->nameClass != NULL) { |
| 3798 | ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1); |
| 3799 | } else { |
| 3800 | ret = 0; |
| 3801 | } |
| 3802 | } else { |
| 3803 | ret = 1; |
| 3804 | } |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3805 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3806 | TODO ret = 0; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3807 | } |
| 3808 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3809 | return (ret); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3810 | } |
| 3811 | |
| 3812 | /** |
| 3813 | * xmlRelaxNGCompareElemDefLists: |
| 3814 | * @ctxt: a Relax-NG parser context |
| 3815 | * @defs1: the first list of element/attribute defs |
| 3816 | * @defs2: the second list of element/attribute defs |
| 3817 | * |
| 3818 | * Compare the 2 lists of element or attribute definitions. The comparison |
| 3819 | * is that if both lists do not accept the same QNames, it returns 1 |
| 3820 | * If the 2 lists can accept the same QName the comparison returns 0 |
| 3821 | * |
| 3822 | * Returns 1 disttinct, 0 if equal |
| 3823 | */ |
| 3824 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3825 | xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt |
| 3826 | ATTRIBUTE_UNUSED, xmlRelaxNGDefinePtr * def1, |
| 3827 | xmlRelaxNGDefinePtr * def2) |
| 3828 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3829 | xmlRelaxNGDefinePtr *basedef2 = def2; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3830 | |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3831 | if ((def1 == NULL) || (def2 == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3832 | return (1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3833 | if ((*def1 == NULL) || (*def2 == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3834 | return (1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3835 | while (*def1 != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3836 | while ((*def2) != NULL) { |
| 3837 | if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0) |
| 3838 | return (0); |
| 3839 | def2++; |
| 3840 | } |
| 3841 | def2 = basedef2; |
| 3842 | def1++; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3843 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3844 | return (1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3845 | } |
| 3846 | |
| 3847 | /** |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3848 | * xmlRelaxNGGenerateAttributes: |
| 3849 | * @ctxt: a Relax-NG parser context |
| 3850 | * @def: the definition definition |
| 3851 | * |
| 3852 | * Check if the definition can only generate attributes |
| 3853 | * |
| 3854 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 3855 | */ |
| 3856 | static int |
| 3857 | xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3858 | xmlRelaxNGDefinePtr def) |
| 3859 | { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3860 | xmlRelaxNGDefinePtr parent, cur, tmp; |
| 3861 | |
| 3862 | /* |
| 3863 | * Don't run that check in case of error. Infinite recursion |
| 3864 | * becomes possible. |
| 3865 | */ |
| 3866 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3867 | return (-1); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3868 | |
| 3869 | parent = NULL; |
| 3870 | cur = def; |
| 3871 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3872 | if ((cur->type == XML_RELAXNG_ELEMENT) || |
| 3873 | (cur->type == XML_RELAXNG_TEXT) || |
| 3874 | (cur->type == XML_RELAXNG_DATATYPE) || |
| 3875 | (cur->type == XML_RELAXNG_PARAM) || |
| 3876 | (cur->type == XML_RELAXNG_LIST) || |
| 3877 | (cur->type == XML_RELAXNG_VALUE) || |
| 3878 | (cur->type == XML_RELAXNG_EMPTY)) |
| 3879 | return (0); |
| 3880 | if ((cur->type == XML_RELAXNG_CHOICE) || |
| 3881 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
| 3882 | (cur->type == XML_RELAXNG_GROUP) || |
| 3883 | (cur->type == XML_RELAXNG_ONEORMORE) || |
| 3884 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
| 3885 | (cur->type == XML_RELAXNG_OPTIONAL) || |
| 3886 | (cur->type == XML_RELAXNG_PARENTREF) || |
| 3887 | (cur->type == XML_RELAXNG_EXTERNALREF) || |
| 3888 | (cur->type == XML_RELAXNG_REF) || |
| 3889 | (cur->type == XML_RELAXNG_DEF)) { |
| 3890 | if (cur->content != NULL) { |
| 3891 | parent = cur; |
| 3892 | cur = cur->content; |
| 3893 | tmp = cur; |
| 3894 | while (tmp != NULL) { |
| 3895 | tmp->parent = parent; |
| 3896 | tmp = tmp->next; |
| 3897 | } |
| 3898 | continue; |
| 3899 | } |
| 3900 | } |
| 3901 | if (cur == def) |
| 3902 | break; |
| 3903 | if (cur->next != NULL) { |
| 3904 | cur = cur->next; |
| 3905 | continue; |
| 3906 | } |
| 3907 | do { |
| 3908 | cur = cur->parent; |
| 3909 | if (cur == NULL) |
| 3910 | break; |
| 3911 | if (cur == def) |
| 3912 | return (1); |
| 3913 | if (cur->next != NULL) { |
| 3914 | cur = cur->next; |
| 3915 | break; |
| 3916 | } |
| 3917 | } while (cur != NULL); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3918 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3919 | return (1); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3920 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3921 | |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3922 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3923 | * xmlRelaxNGGetElements: |
| 3924 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3925 | * @def: the definition definition |
| 3926 | * @eora: gather elements (0) or attributes (1) |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3927 | * |
| 3928 | * Compute the list of top elements a definition can generate |
| 3929 | * |
| 3930 | * Returns a list of elements or NULL if none was found. |
| 3931 | */ |
| 3932 | static xmlRelaxNGDefinePtr * |
| 3933 | xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3934 | xmlRelaxNGDefinePtr def, int eora) |
| 3935 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3936 | xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3937 | int len = 0; |
| 3938 | int max = 0; |
| 3939 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3940 | /* |
| 3941 | * Don't run that check in case of error. Infinite recursion |
| 3942 | * becomes possible. |
| 3943 | */ |
| 3944 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3945 | return (NULL); |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3946 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3947 | parent = NULL; |
| 3948 | cur = def; |
| 3949 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3950 | if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) || |
| 3951 | (cur->type == XML_RELAXNG_TEXT))) || |
| 3952 | ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) { |
| 3953 | if (ret == NULL) { |
| 3954 | max = 10; |
| 3955 | ret = (xmlRelaxNGDefinePtr *) |
| 3956 | xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
| 3957 | if (ret == NULL) { |
| 3958 | xmlRngPErrMemory(ctxt, "getting element list\n"); |
| 3959 | return (NULL); |
| 3960 | } |
| 3961 | } else if (max <= len) { |
| 3962 | max *= 2; |
| 3963 | ret = |
| 3964 | xmlRealloc(ret, |
| 3965 | (max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
| 3966 | if (ret == NULL) { |
| 3967 | xmlRngPErrMemory(ctxt, "getting element list\n"); |
| 3968 | return (NULL); |
| 3969 | } |
| 3970 | } |
| 3971 | ret[len++] = cur; |
| 3972 | ret[len] = NULL; |
| 3973 | } else if ((cur->type == XML_RELAXNG_CHOICE) || |
| 3974 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
| 3975 | (cur->type == XML_RELAXNG_GROUP) || |
| 3976 | (cur->type == XML_RELAXNG_ONEORMORE) || |
| 3977 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
| 3978 | (cur->type == XML_RELAXNG_OPTIONAL) || |
| 3979 | (cur->type == XML_RELAXNG_PARENTREF) || |
| 3980 | (cur->type == XML_RELAXNG_REF) || |
William M. Brack | 236c8c0 | 2004-03-20 11:32:36 +0000 | [diff] [blame] | 3981 | (cur->type == XML_RELAXNG_DEF) || |
| 3982 | (cur->type == XML_RELAXNG_EXTERNALREF)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3983 | /* |
| 3984 | * Don't go within elements or attributes or string values. |
| 3985 | * Just gather the element top list |
| 3986 | */ |
| 3987 | if (cur->content != NULL) { |
| 3988 | parent = cur; |
| 3989 | cur = cur->content; |
| 3990 | tmp = cur; |
| 3991 | while (tmp != NULL) { |
| 3992 | tmp->parent = parent; |
| 3993 | tmp = tmp->next; |
| 3994 | } |
| 3995 | continue; |
| 3996 | } |
| 3997 | } |
| 3998 | if (cur == def) |
| 3999 | break; |
| 4000 | if (cur->next != NULL) { |
| 4001 | cur = cur->next; |
| 4002 | continue; |
| 4003 | } |
| 4004 | do { |
| 4005 | cur = cur->parent; |
| 4006 | if (cur == NULL) |
| 4007 | break; |
| 4008 | if (cur == def) |
| 4009 | return (ret); |
| 4010 | if (cur->next != NULL) { |
| 4011 | cur = cur->next; |
| 4012 | break; |
| 4013 | } |
| 4014 | } while (cur != NULL); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4015 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4016 | return (ret); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4017 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4018 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4019 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4020 | * xmlRelaxNGCheckChoiceDeterminism: |
| 4021 | * @ctxt: a Relax-NG parser context |
| 4022 | * @def: the choice definition |
| 4023 | * |
| 4024 | * Also used to find indeterministic pattern in choice |
| 4025 | */ |
| 4026 | static void |
| 4027 | xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4028 | xmlRelaxNGDefinePtr def) |
| 4029 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4030 | xmlRelaxNGDefinePtr **list; |
| 4031 | xmlRelaxNGDefinePtr cur; |
| 4032 | int nbchild = 0, i, j, ret; |
| 4033 | int is_nullable = 0; |
| 4034 | int is_indeterminist = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4035 | xmlHashTablePtr triage = NULL; |
| 4036 | int is_triable = 1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4037 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4038 | if ((def == NULL) || (def->type != XML_RELAXNG_CHOICE)) |
| 4039 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4040 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4041 | if (def->dflags & IS_PROCESSED) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4042 | return; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4043 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4044 | /* |
| 4045 | * Don't run that check in case of error. Infinite recursion |
| 4046 | * becomes possible. |
| 4047 | */ |
| 4048 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4049 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4050 | |
| 4051 | is_nullable = xmlRelaxNGIsNullable(def); |
| 4052 | |
| 4053 | cur = def->content; |
| 4054 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4055 | nbchild++; |
| 4056 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4057 | } |
| 4058 | |
| 4059 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4060 | sizeof(xmlRelaxNGDefinePtr |
| 4061 | *)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4062 | if (list == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4063 | xmlRngPErrMemory(ctxt, "building choice\n"); |
| 4064 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4065 | } |
| 4066 | i = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4067 | /* |
| 4068 | * a bit strong but safe |
| 4069 | */ |
| 4070 | if (is_nullable == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4071 | triage = xmlHashCreate(10); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4072 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4073 | is_triable = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4074 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4075 | cur = def->content; |
| 4076 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4077 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 0); |
| 4078 | if ((list[i] == NULL) || (list[i][0] == NULL)) { |
| 4079 | is_triable = 0; |
| 4080 | } else if (is_triable == 1) { |
| 4081 | xmlRelaxNGDefinePtr *tmp; |
| 4082 | int res; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4083 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4084 | tmp = list[i]; |
| 4085 | while ((*tmp != NULL) && (is_triable == 1)) { |
| 4086 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
| 4087 | res = xmlHashAddEntry2(triage, |
| 4088 | BAD_CAST "#text", NULL, |
| 4089 | (void *) cur); |
| 4090 | if (res != 0) |
| 4091 | is_triable = -1; |
| 4092 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
| 4093 | ((*tmp)->name != NULL)) { |
| 4094 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4095 | res = xmlHashAddEntry2(triage, |
| 4096 | (*tmp)->name, NULL, |
| 4097 | (void *) cur); |
| 4098 | else |
| 4099 | res = xmlHashAddEntry2(triage, |
| 4100 | (*tmp)->name, (*tmp)->ns, |
| 4101 | (void *) cur); |
| 4102 | if (res != 0) |
| 4103 | is_triable = -1; |
| 4104 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
| 4105 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4106 | res = xmlHashAddEntry2(triage, |
| 4107 | BAD_CAST "#any", NULL, |
| 4108 | (void *) cur); |
| 4109 | else |
| 4110 | res = xmlHashAddEntry2(triage, |
| 4111 | BAD_CAST "#any", (*tmp)->ns, |
| 4112 | (void *) cur); |
| 4113 | if (res != 0) |
| 4114 | is_triable = -1; |
| 4115 | } else { |
| 4116 | is_triable = -1; |
| 4117 | } |
| 4118 | tmp++; |
| 4119 | } |
| 4120 | } |
| 4121 | i++; |
| 4122 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4123 | } |
| 4124 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4125 | for (i = 0; i < nbchild; i++) { |
| 4126 | if (list[i] == NULL) |
| 4127 | continue; |
| 4128 | for (j = 0; j < i; j++) { |
| 4129 | if (list[j] == NULL) |
| 4130 | continue; |
| 4131 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
| 4132 | if (ret == 0) { |
| 4133 | is_indeterminist = 1; |
| 4134 | } |
| 4135 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4136 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4137 | for (i = 0; i < nbchild; i++) { |
| 4138 | if (list[i] != NULL) |
| 4139 | xmlFree(list[i]); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4140 | } |
| 4141 | |
| 4142 | xmlFree(list); |
| 4143 | if (is_indeterminist) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4144 | def->dflags |= IS_INDETERMINIST; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4145 | } |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4146 | if (is_triable == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4147 | def->dflags |= IS_TRIABLE; |
| 4148 | def->data = triage; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4149 | } else if (triage != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4150 | xmlHashFree(triage, NULL); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4151 | } |
| 4152 | def->dflags |= IS_PROCESSED; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4153 | } |
| 4154 | |
| 4155 | /** |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4156 | * xmlRelaxNGCheckGroupAttrs: |
| 4157 | * @ctxt: a Relax-NG parser context |
| 4158 | * @def: the group definition |
| 4159 | * |
| 4160 | * Detects violations of rule 7.3 |
| 4161 | */ |
| 4162 | static void |
| 4163 | xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4164 | xmlRelaxNGDefinePtr def) |
| 4165 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4166 | xmlRelaxNGDefinePtr **list; |
| 4167 | xmlRelaxNGDefinePtr cur; |
| 4168 | int nbchild = 0, i, j, ret; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4169 | |
| 4170 | if ((def == NULL) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4171 | ((def->type != XML_RELAXNG_GROUP) && |
| 4172 | (def->type != XML_RELAXNG_ELEMENT))) |
| 4173 | return; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4174 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4175 | if (def->dflags & IS_PROCESSED) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4176 | return; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4177 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4178 | /* |
| 4179 | * Don't run that check in case of error. Infinite recursion |
| 4180 | * becomes possible. |
| 4181 | */ |
| 4182 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4183 | return; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4184 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4185 | cur = def->attrs; |
| 4186 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4187 | nbchild++; |
| 4188 | cur = cur->next; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4189 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4190 | cur = def->content; |
| 4191 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4192 | nbchild++; |
| 4193 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4194 | } |
| 4195 | |
| 4196 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4197 | sizeof(xmlRelaxNGDefinePtr |
| 4198 | *)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4199 | if (list == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4200 | xmlRngPErrMemory(ctxt, "building group\n"); |
| 4201 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4202 | } |
| 4203 | i = 0; |
| 4204 | cur = def->attrs; |
| 4205 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4206 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4207 | i++; |
| 4208 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4209 | } |
| 4210 | cur = def->content; |
| 4211 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4212 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4213 | i++; |
| 4214 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4215 | } |
| 4216 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4217 | for (i = 0; i < nbchild; i++) { |
| 4218 | if (list[i] == NULL) |
| 4219 | continue; |
| 4220 | for (j = 0; j < i; j++) { |
| 4221 | if (list[j] == NULL) |
| 4222 | continue; |
| 4223 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
| 4224 | if (ret == 0) { |
| 4225 | xmlRngPErr(ctxt, def->node, XML_RNGP_GROUP_ATTR_CONFLICT, |
| 4226 | "Attributes conflicts in group\n", NULL, NULL); |
| 4227 | } |
| 4228 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4229 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4230 | for (i = 0; i < nbchild; i++) { |
| 4231 | if (list[i] != NULL) |
| 4232 | xmlFree(list[i]); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4233 | } |
| 4234 | |
| 4235 | xmlFree(list); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4236 | def->dflags |= IS_PROCESSED; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4237 | } |
| 4238 | |
| 4239 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4240 | * xmlRelaxNGComputeInterleaves: |
| 4241 | * @def: the interleave definition |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4242 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4243 | * @name: the definition name |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4244 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4245 | * A lot of work for preprocessing interleave definitions |
| 4246 | * is potentially needed to get a decent execution speed at runtime |
| 4247 | * - trying to get a total order on the element nodes generated |
| 4248 | * by the interleaves, order the list of interleave definitions |
| 4249 | * following that order. |
| 4250 | * - if <text/> is used to handle mixed content, it is better to |
| 4251 | * flag this in the define and simplify the runtime checking |
| 4252 | * algorithm |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4253 | */ |
| 4254 | static void |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4255 | xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4256 | xmlRelaxNGParserCtxtPtr ctxt, |
| 4257 | xmlChar * name ATTRIBUTE_UNUSED) |
| 4258 | { |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4259 | xmlRelaxNGDefinePtr cur, *tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4260 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4261 | xmlRelaxNGPartitionPtr partitions = NULL; |
| 4262 | xmlRelaxNGInterleaveGroupPtr *groups = NULL; |
| 4263 | xmlRelaxNGInterleaveGroupPtr group; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4264 | int i, j, ret, res; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4265 | int nbgroups = 0; |
| 4266 | int nbchild = 0; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 4267 | int is_mixed = 0; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4268 | int is_determinist = 1; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4269 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4270 | /* |
| 4271 | * Don't run that check in case of error. Infinite recursion |
| 4272 | * becomes possible. |
| 4273 | */ |
| 4274 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4275 | return; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4276 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4277 | #ifdef DEBUG_INTERLEAVE |
| 4278 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4279 | "xmlRelaxNGComputeInterleaves(%s)\n", name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4280 | #endif |
| 4281 | cur = def->content; |
| 4282 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4283 | nbchild++; |
| 4284 | cur = cur->next; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4285 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4286 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4287 | #ifdef DEBUG_INTERLEAVE |
| 4288 | xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild); |
| 4289 | #endif |
| 4290 | groups = (xmlRelaxNGInterleaveGroupPtr *) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4291 | xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4292 | if (groups == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4293 | goto error; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4294 | cur = def->content; |
| 4295 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4296 | groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr) |
| 4297 | xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup)); |
| 4298 | if (groups[nbgroups] == NULL) |
| 4299 | goto error; |
| 4300 | if (cur->type == XML_RELAXNG_TEXT) |
| 4301 | is_mixed++; |
| 4302 | groups[nbgroups]->rule = cur; |
| 4303 | groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0); |
| 4304 | groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4305 | nbgroups++; |
| 4306 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4307 | } |
| 4308 | #ifdef DEBUG_INTERLEAVE |
| 4309 | xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups); |
| 4310 | #endif |
| 4311 | |
| 4312 | /* |
| 4313 | * Let's check that all rules makes a partitions according to 7.4 |
| 4314 | */ |
| 4315 | partitions = (xmlRelaxNGPartitionPtr) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4316 | xmlMalloc(sizeof(xmlRelaxNGPartition)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4317 | if (partitions == NULL) |
| 4318 | goto error; |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 4319 | memset(partitions, 0, sizeof(xmlRelaxNGPartition)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4320 | partitions->nbgroups = nbgroups; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4321 | partitions->triage = xmlHashCreate(nbgroups); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4322 | for (i = 0; i < nbgroups; i++) { |
| 4323 | group = groups[i]; |
| 4324 | for (j = i + 1; j < nbgroups; j++) { |
| 4325 | if (groups[j] == NULL) |
| 4326 | continue; |
| 4327 | |
| 4328 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs, |
| 4329 | groups[j]->defs); |
| 4330 | if (ret == 0) { |
| 4331 | xmlRngPErr(ctxt, def->node, XML_RNGP_ELEM_TEXT_CONFLICT, |
| 4332 | "Element or text conflicts in interleave\n", |
| 4333 | NULL, NULL); |
| 4334 | } |
| 4335 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs, |
| 4336 | groups[j]->attrs); |
| 4337 | if (ret == 0) { |
| 4338 | xmlRngPErr(ctxt, def->node, XML_RNGP_ATTR_CONFLICT, |
| 4339 | "Attributes conflicts in interleave\n", NULL, |
| 4340 | NULL); |
| 4341 | } |
| 4342 | } |
| 4343 | tmp = group->defs; |
| 4344 | if ((tmp != NULL) && (*tmp != NULL)) { |
| 4345 | while (*tmp != NULL) { |
| 4346 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
| 4347 | res = xmlHashAddEntry2(partitions->triage, |
| 4348 | BAD_CAST "#text", NULL, |
| 4349 | (void *) (long) (i + 1)); |
| 4350 | if (res != 0) |
| 4351 | is_determinist = -1; |
| 4352 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
| 4353 | ((*tmp)->name != NULL)) { |
| 4354 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4355 | res = xmlHashAddEntry2(partitions->triage, |
| 4356 | (*tmp)->name, NULL, |
| 4357 | (void *) (long) (i + 1)); |
| 4358 | else |
| 4359 | res = xmlHashAddEntry2(partitions->triage, |
| 4360 | (*tmp)->name, (*tmp)->ns, |
| 4361 | (void *) (long) (i + 1)); |
| 4362 | if (res != 0) |
| 4363 | is_determinist = -1; |
| 4364 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
| 4365 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4366 | res = xmlHashAddEntry2(partitions->triage, |
| 4367 | BAD_CAST "#any", NULL, |
| 4368 | (void *) (long) (i + 1)); |
| 4369 | else |
| 4370 | res = xmlHashAddEntry2(partitions->triage, |
| 4371 | BAD_CAST "#any", (*tmp)->ns, |
| 4372 | (void *) (long) (i + 1)); |
| 4373 | if ((*tmp)->nameClass != NULL) |
| 4374 | is_determinist = 2; |
| 4375 | if (res != 0) |
| 4376 | is_determinist = -1; |
| 4377 | } else { |
| 4378 | is_determinist = -1; |
| 4379 | } |
| 4380 | tmp++; |
| 4381 | } |
| 4382 | } else { |
| 4383 | is_determinist = 0; |
| 4384 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4385 | } |
| 4386 | partitions->groups = groups; |
| 4387 | |
| 4388 | /* |
| 4389 | * and save the partition list back in the def |
| 4390 | */ |
| 4391 | def->data = partitions; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 4392 | if (is_mixed != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4393 | def->dflags |= IS_MIXED; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4394 | if (is_determinist == 1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4395 | partitions->flags = IS_DETERMINIST; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4396 | if (is_determinist == 2) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4397 | partitions->flags = IS_DETERMINIST | IS_NEEDCHECK; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4398 | return; |
| 4399 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4400 | error: |
| 4401 | xmlRngPErrMemory(ctxt, "in interleave computation\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4402 | if (groups != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4403 | for (i = 0; i < nbgroups; i++) |
| 4404 | if (groups[i] != NULL) { |
| 4405 | if (groups[i]->defs != NULL) |
| 4406 | xmlFree(groups[i]->defs); |
| 4407 | xmlFree(groups[i]); |
| 4408 | } |
| 4409 | xmlFree(groups); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4410 | } |
| 4411 | xmlRelaxNGFreePartition(partitions); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4412 | } |
| 4413 | |
| 4414 | /** |
| 4415 | * xmlRelaxNGParseInterleave: |
| 4416 | * @ctxt: a Relax-NG parser context |
| 4417 | * @node: the data node. |
| 4418 | * |
| 4419 | * parse the content of a RelaxNG interleave node. |
| 4420 | * |
| 4421 | * Returns the definition pointer or NULL in case of error |
| 4422 | */ |
| 4423 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4424 | xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4425 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4426 | xmlRelaxNGDefinePtr def = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4427 | xmlRelaxNGDefinePtr last = NULL, cur; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4428 | xmlNodePtr child; |
| 4429 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4430 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4431 | if (def == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4432 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4433 | } |
| 4434 | def->type = XML_RELAXNG_INTERLEAVE; |
| 4435 | |
| 4436 | if (ctxt->interleaves == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4437 | ctxt->interleaves = xmlHashCreate(10); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4438 | if (ctxt->interleaves == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4439 | xmlRngPErrMemory(ctxt, "create interleaves\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4440 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4441 | char name[32]; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4442 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4443 | snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++); |
| 4444 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) { |
| 4445 | xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_ADD, |
| 4446 | "Failed to add %s to hash table\n", |
| 4447 | (const xmlChar *) name, NULL); |
| 4448 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4449 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4450 | child = node->children; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4451 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4452 | xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_NO_CONTENT, |
| 4453 | "Element interleave is empty\n", NULL, NULL); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 4454 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4455 | while (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4456 | if (IS_RELAXNG(child, "element")) { |
| 4457 | cur = xmlRelaxNGParseElement(ctxt, child); |
| 4458 | } else { |
| 4459 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 4460 | } |
| 4461 | if (cur != NULL) { |
| 4462 | cur->parent = def; |
| 4463 | if (last == NULL) { |
| 4464 | def->content = last = cur; |
| 4465 | } else { |
| 4466 | last->next = cur; |
| 4467 | last = cur; |
| 4468 | } |
| 4469 | } |
| 4470 | child = child->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4471 | } |
| 4472 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4473 | return (def); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4474 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4475 | |
| 4476 | /** |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4477 | * xmlRelaxNGParseInclude: |
| 4478 | * @ctxt: a Relax-NG parser context |
| 4479 | * @node: the include node |
| 4480 | * |
| 4481 | * Integrate the content of an include node in the current grammar |
| 4482 | * |
| 4483 | * Returns 0 in case of success or -1 in case of error |
| 4484 | */ |
| 4485 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4486 | xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4487 | { |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4488 | xmlRelaxNGIncludePtr incl; |
| 4489 | xmlNodePtr root; |
| 4490 | int ret = 0, tmp; |
| 4491 | |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 4492 | incl = node->psvi; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4493 | if (incl == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4494 | xmlRngPErr(ctxt, node, XML_RNGP_INCLUDE_EMPTY, |
| 4495 | "Include node has no data\n", NULL, NULL); |
| 4496 | return (-1); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4497 | } |
| 4498 | root = xmlDocGetRootElement(incl->doc); |
| 4499 | if (root == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4500 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, "Include document is empty\n", |
| 4501 | NULL, NULL); |
| 4502 | return (-1); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4503 | } |
| 4504 | if (!xmlStrEqual(root->name, BAD_CAST "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4505 | xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING, |
| 4506 | "Include document root is not a grammar\n", NULL, NULL); |
| 4507 | return (-1); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4508 | } |
| 4509 | |
| 4510 | /* |
| 4511 | * Merge the definition from both the include and the internal list |
| 4512 | */ |
| 4513 | if (root->children != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4514 | tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children); |
| 4515 | if (tmp != 0) |
| 4516 | ret = -1; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4517 | } |
| 4518 | if (node->children != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4519 | tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children); |
| 4520 | if (tmp != 0) |
| 4521 | ret = -1; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4522 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4523 | return (ret); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4524 | } |
| 4525 | |
| 4526 | /** |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4527 | * xmlRelaxNGParseDefine: |
| 4528 | * @ctxt: a Relax-NG parser context |
| 4529 | * @node: the define node |
| 4530 | * |
| 4531 | * parse the content of a RelaxNG define element node. |
| 4532 | * |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4533 | * Returns 0 in case of success or -1 in case of error |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4534 | */ |
| 4535 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4536 | xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4537 | { |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4538 | xmlChar *name; |
| 4539 | int ret = 0, tmp; |
| 4540 | xmlRelaxNGDefinePtr def; |
| 4541 | const xmlChar *olddefine; |
| 4542 | |
| 4543 | name = xmlGetProp(node, BAD_CAST "name"); |
| 4544 | if (name == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4545 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_NAME_MISSING, |
| 4546 | "define has no name\n", NULL, NULL); |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4547 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4548 | xmlRelaxNGNormExtSpace(name); |
| 4549 | if (xmlValidateNCName(name, 0)) { |
| 4550 | xmlRngPErr(ctxt, node, XML_RNGP_INVALID_DEFINE_NAME, |
| 4551 | "define name '%s' is not an NCName\n", name, NULL); |
| 4552 | } |
| 4553 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4554 | if (def == NULL) { |
| 4555 | xmlFree(name); |
| 4556 | return (-1); |
| 4557 | } |
| 4558 | def->type = XML_RELAXNG_DEF; |
| 4559 | def->name = name; |
| 4560 | if (node->children == NULL) { |
| 4561 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_EMPTY, |
| 4562 | "define has no children\n", NULL, NULL); |
| 4563 | } else { |
| 4564 | olddefine = ctxt->define; |
| 4565 | ctxt->define = name; |
| 4566 | def->content = |
| 4567 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4568 | ctxt->define = olddefine; |
| 4569 | } |
| 4570 | if (ctxt->grammar->defs == NULL) |
| 4571 | ctxt->grammar->defs = xmlHashCreate(10); |
| 4572 | if (ctxt->grammar->defs == NULL) { |
| 4573 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED, |
| 4574 | "Could not create definition hash\n", NULL, NULL); |
| 4575 | ret = -1; |
| 4576 | } else { |
| 4577 | tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def); |
| 4578 | if (tmp < 0) { |
| 4579 | xmlRelaxNGDefinePtr prev; |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 4580 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4581 | prev = xmlHashLookup(ctxt->grammar->defs, name); |
| 4582 | if (prev == NULL) { |
| 4583 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED, |
| 4584 | "Internal error on define aggregation of %s\n", |
| 4585 | name, NULL); |
| 4586 | ret = -1; |
| 4587 | } else { |
| 4588 | while (prev->nextHash != NULL) |
| 4589 | prev = prev->nextHash; |
| 4590 | prev->nextHash = def; |
| 4591 | } |
| 4592 | } |
| 4593 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4594 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4595 | return (ret); |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4596 | } |
| 4597 | |
| 4598 | /** |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4599 | * xmlRelaxNGProcessExternalRef: |
| 4600 | * @ctxt: the parser context |
| 4601 | * @node: the externlRef node |
| 4602 | * |
| 4603 | * Process and compile an externlRef node |
| 4604 | * |
| 4605 | * Returns the xmlRelaxNGDefinePtr or NULL in case of error |
| 4606 | */ |
| 4607 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4608 | xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4609 | { |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4610 | xmlRelaxNGDocumentPtr docu; |
| 4611 | xmlNodePtr root, tmp; |
| 4612 | xmlChar *ns; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4613 | int newNs = 0, oldflags; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4614 | xmlRelaxNGDefinePtr def; |
| 4615 | |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 4616 | docu = node->psvi; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4617 | if (docu != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4618 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4619 | if (def == NULL) |
| 4620 | return (NULL); |
| 4621 | def->type = XML_RELAXNG_EXTERNALREF; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4622 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4623 | if (docu->content == NULL) { |
| 4624 | /* |
| 4625 | * Then do the parsing for good |
| 4626 | */ |
| 4627 | root = xmlDocGetRootElement(docu->doc); |
| 4628 | if (root == NULL) { |
| 4629 | xmlRngPErr(ctxt, node, XML_RNGP_EXTERNALREF_EMTPY, |
| 4630 | "xmlRelaxNGParse: %s is empty\n", ctxt->URL, |
| 4631 | NULL); |
| 4632 | return (NULL); |
| 4633 | } |
| 4634 | /* |
| 4635 | * ns transmission rules |
| 4636 | */ |
| 4637 | ns = xmlGetProp(root, BAD_CAST "ns"); |
| 4638 | if (ns == NULL) { |
| 4639 | tmp = node; |
| 4640 | while ((tmp != NULL) && (tmp->type == XML_ELEMENT_NODE)) { |
| 4641 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 4642 | if (ns != NULL) { |
| 4643 | break; |
| 4644 | } |
| 4645 | tmp = tmp->parent; |
| 4646 | } |
| 4647 | if (ns != NULL) { |
| 4648 | xmlSetProp(root, BAD_CAST "ns", ns); |
| 4649 | newNs = 1; |
| 4650 | xmlFree(ns); |
| 4651 | } |
| 4652 | } else { |
| 4653 | xmlFree(ns); |
| 4654 | } |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4655 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4656 | /* |
| 4657 | * Parsing to get a precompiled schemas. |
| 4658 | */ |
| 4659 | oldflags = ctxt->flags; |
| 4660 | ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF; |
| 4661 | docu->schema = xmlRelaxNGParseDocument(ctxt, root); |
| 4662 | ctxt->flags = oldflags; |
| 4663 | if ((docu->schema != NULL) && |
| 4664 | (docu->schema->topgrammar != NULL)) { |
| 4665 | docu->content = docu->schema->topgrammar->start; |
| 4666 | } |
| 4667 | |
| 4668 | /* |
| 4669 | * the externalRef may be reused in a different ns context |
| 4670 | */ |
| 4671 | if (newNs == 1) { |
| 4672 | xmlUnsetProp(root, BAD_CAST "ns"); |
| 4673 | } |
| 4674 | } |
| 4675 | def->content = docu->content; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4676 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4677 | def = NULL; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4678 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4679 | return (def); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4680 | } |
| 4681 | |
| 4682 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4683 | * xmlRelaxNGParsePattern: |
| 4684 | * @ctxt: a Relax-NG parser context |
| 4685 | * @node: the pattern node. |
| 4686 | * |
| 4687 | * parse the content of a RelaxNG pattern node. |
| 4688 | * |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4689 | * Returns the definition pointer or NULL in case of error or if no |
| 4690 | * pattern is generated. |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4691 | */ |
| 4692 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4693 | xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4694 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4695 | xmlRelaxNGDefinePtr def = NULL; |
| 4696 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4697 | if (node == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4698 | return (NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4699 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4700 | if (IS_RELAXNG(node, "element")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4701 | def = xmlRelaxNGParseElement(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4702 | } else if (IS_RELAXNG(node, "attribute")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4703 | def = xmlRelaxNGParseAttribute(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4704 | } else if (IS_RELAXNG(node, "empty")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4705 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4706 | if (def == NULL) |
| 4707 | return (NULL); |
| 4708 | def->type = XML_RELAXNG_EMPTY; |
| 4709 | if (node->children != NULL) { |
| 4710 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_NOT_EMPTY, |
| 4711 | "empty: had a child node\n", NULL, NULL); |
| 4712 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4713 | } else if (IS_RELAXNG(node, "text")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4714 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4715 | if (def == NULL) |
| 4716 | return (NULL); |
| 4717 | def->type = XML_RELAXNG_TEXT; |
| 4718 | if (node->children != NULL) { |
| 4719 | xmlRngPErr(ctxt, node, XML_RNGP_TEXT_HAS_CHILD, |
| 4720 | "text: had a child node\n", NULL, NULL); |
| 4721 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4722 | } else if (IS_RELAXNG(node, "zeroOrMore")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4723 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4724 | if (def == NULL) |
| 4725 | return (NULL); |
| 4726 | def->type = XML_RELAXNG_ZEROORMORE; |
| 4727 | if (node->children == NULL) { |
| 4728 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4729 | "Element %s is empty\n", node->name, NULL); |
| 4730 | } else { |
| 4731 | def->content = |
| 4732 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4733 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4734 | } else if (IS_RELAXNG(node, "oneOrMore")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4735 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4736 | if (def == NULL) |
| 4737 | return (NULL); |
| 4738 | def->type = XML_RELAXNG_ONEORMORE; |
| 4739 | if (node->children == NULL) { |
| 4740 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4741 | "Element %s is empty\n", node->name, NULL); |
| 4742 | } else { |
| 4743 | def->content = |
| 4744 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4745 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4746 | } else if (IS_RELAXNG(node, "optional")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4747 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4748 | if (def == NULL) |
| 4749 | return (NULL); |
| 4750 | def->type = XML_RELAXNG_OPTIONAL; |
| 4751 | if (node->children == NULL) { |
| 4752 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4753 | "Element %s is empty\n", node->name, NULL); |
| 4754 | } else { |
| 4755 | def->content = |
| 4756 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4757 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4758 | } else if (IS_RELAXNG(node, "choice")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4759 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4760 | if (def == NULL) |
| 4761 | return (NULL); |
| 4762 | def->type = XML_RELAXNG_CHOICE; |
| 4763 | if (node->children == NULL) { |
| 4764 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4765 | "Element %s is empty\n", node->name, NULL); |
| 4766 | } else { |
| 4767 | def->content = |
| 4768 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4769 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4770 | } else if (IS_RELAXNG(node, "group")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4771 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4772 | if (def == NULL) |
| 4773 | return (NULL); |
| 4774 | def->type = XML_RELAXNG_GROUP; |
| 4775 | if (node->children == NULL) { |
| 4776 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4777 | "Element %s is empty\n", node->name, NULL); |
| 4778 | } else { |
| 4779 | def->content = |
| 4780 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4781 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4782 | } else if (IS_RELAXNG(node, "ref")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4783 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4784 | if (def == NULL) |
| 4785 | return (NULL); |
| 4786 | def->type = XML_RELAXNG_REF; |
| 4787 | def->name = xmlGetProp(node, BAD_CAST "name"); |
| 4788 | if (def->name == NULL) { |
| 4789 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NO_NAME, "ref has no name\n", |
| 4790 | NULL, NULL); |
| 4791 | } else { |
| 4792 | xmlRelaxNGNormExtSpace(def->name); |
| 4793 | if (xmlValidateNCName(def->name, 0)) { |
| 4794 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NAME_INVALID, |
| 4795 | "ref name '%s' is not an NCName\n", def->name, |
| 4796 | NULL); |
| 4797 | } |
| 4798 | } |
| 4799 | if (node->children != NULL) { |
| 4800 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NOT_EMPTY, "ref is not empty\n", |
| 4801 | NULL, NULL); |
| 4802 | } |
| 4803 | if (ctxt->grammar->refs == NULL) |
| 4804 | ctxt->grammar->refs = xmlHashCreate(10); |
| 4805 | if (ctxt->grammar->refs == NULL) { |
| 4806 | xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED, |
| 4807 | "Could not create references hash\n", NULL, NULL); |
| 4808 | def = NULL; |
| 4809 | } else { |
| 4810 | int tmp; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4811 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4812 | tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def); |
| 4813 | if (tmp < 0) { |
| 4814 | xmlRelaxNGDefinePtr prev; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4815 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4816 | prev = (xmlRelaxNGDefinePtr) |
| 4817 | xmlHashLookup(ctxt->grammar->refs, def->name); |
| 4818 | if (prev == NULL) { |
| 4819 | if (def->name != NULL) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 4820 | xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED, |
| 4821 | "Error refs definitions '%s'\n", |
| 4822 | def->name, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4823 | } else { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 4824 | xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED, |
| 4825 | "Error refs definitions\n", |
| 4826 | NULL, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4827 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4828 | def = NULL; |
| 4829 | } else { |
| 4830 | def->nextHash = prev->nextHash; |
| 4831 | prev->nextHash = def; |
| 4832 | } |
| 4833 | } |
| 4834 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 4835 | } else if (IS_RELAXNG(node, "data")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4836 | def = xmlRelaxNGParseData(ctxt, node); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 4837 | } else if (IS_RELAXNG(node, "value")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4838 | def = xmlRelaxNGParseValue(ctxt, node); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 4839 | } else if (IS_RELAXNG(node, "list")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4840 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4841 | if (def == NULL) |
| 4842 | return (NULL); |
| 4843 | def->type = XML_RELAXNG_LIST; |
| 4844 | if (node->children == NULL) { |
| 4845 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4846 | "Element %s is empty\n", node->name, NULL); |
| 4847 | } else { |
| 4848 | def->content = |
| 4849 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4850 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4851 | } else if (IS_RELAXNG(node, "interleave")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4852 | def = xmlRelaxNGParseInterleave(ctxt, node); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4853 | } else if (IS_RELAXNG(node, "externalRef")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4854 | def = xmlRelaxNGProcessExternalRef(ctxt, node); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4855 | } else if (IS_RELAXNG(node, "notAllowed")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4856 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4857 | if (def == NULL) |
| 4858 | return (NULL); |
| 4859 | def->type = XML_RELAXNG_NOT_ALLOWED; |
| 4860 | if (node->children != NULL) { |
| 4861 | xmlRngPErr(ctxt, node, XML_RNGP_NOTALLOWED_NOT_EMPTY, |
| 4862 | "xmlRelaxNGParse: notAllowed element is not empty\n", |
| 4863 | NULL, NULL); |
| 4864 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4865 | } else if (IS_RELAXNG(node, "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4866 | xmlRelaxNGGrammarPtr grammar, old; |
| 4867 | xmlRelaxNGGrammarPtr oldparent; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4868 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4869 | #ifdef DEBUG_GRAMMAR |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4870 | xmlGenericError(xmlGenericErrorContext, |
| 4871 | "Found <grammar> pattern\n"); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4872 | #endif |
| 4873 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4874 | oldparent = ctxt->parentgrammar; |
| 4875 | old = ctxt->grammar; |
| 4876 | ctxt->parentgrammar = old; |
| 4877 | grammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
| 4878 | if (old != NULL) { |
| 4879 | ctxt->grammar = old; |
| 4880 | ctxt->parentgrammar = oldparent; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4881 | #if 0 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4882 | if (grammar != NULL) { |
| 4883 | grammar->next = old->next; |
| 4884 | old->next = grammar; |
| 4885 | } |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4886 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4887 | } |
| 4888 | if (grammar != NULL) |
| 4889 | def = grammar->start; |
| 4890 | else |
| 4891 | def = NULL; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4892 | } else if (IS_RELAXNG(node, "parentRef")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4893 | if (ctxt->parentgrammar == NULL) { |
| 4894 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_PARENT, |
| 4895 | "Use of parentRef without a parent grammar\n", NULL, |
| 4896 | NULL); |
| 4897 | return (NULL); |
| 4898 | } |
| 4899 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4900 | if (def == NULL) |
| 4901 | return (NULL); |
| 4902 | def->type = XML_RELAXNG_PARENTREF; |
| 4903 | def->name = xmlGetProp(node, BAD_CAST "name"); |
| 4904 | if (def->name == NULL) { |
| 4905 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_NAME, |
| 4906 | "parentRef has no name\n", NULL, NULL); |
| 4907 | } else { |
| 4908 | xmlRelaxNGNormExtSpace(def->name); |
| 4909 | if (xmlValidateNCName(def->name, 0)) { |
| 4910 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NAME_INVALID, |
| 4911 | "parentRef name '%s' is not an NCName\n", |
| 4912 | def->name, NULL); |
| 4913 | } |
| 4914 | } |
| 4915 | if (node->children != NULL) { |
| 4916 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NOT_EMPTY, |
| 4917 | "parentRef is not empty\n", NULL, NULL); |
| 4918 | } |
| 4919 | if (ctxt->parentgrammar->refs == NULL) |
| 4920 | ctxt->parentgrammar->refs = xmlHashCreate(10); |
| 4921 | if (ctxt->parentgrammar->refs == NULL) { |
| 4922 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED, |
| 4923 | "Could not create references hash\n", NULL, NULL); |
| 4924 | def = NULL; |
| 4925 | } else if (def->name != NULL) { |
| 4926 | int tmp; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4927 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4928 | tmp = |
| 4929 | xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def); |
| 4930 | if (tmp < 0) { |
| 4931 | xmlRelaxNGDefinePtr prev; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4932 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4933 | prev = (xmlRelaxNGDefinePtr) |
| 4934 | xmlHashLookup(ctxt->parentgrammar->refs, def->name); |
| 4935 | if (prev == NULL) { |
| 4936 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED, |
| 4937 | "Internal error parentRef definitions '%s'\n", |
| 4938 | def->name, NULL); |
| 4939 | def = NULL; |
| 4940 | } else { |
| 4941 | def->nextHash = prev->nextHash; |
| 4942 | prev->nextHash = def; |
| 4943 | } |
| 4944 | } |
| 4945 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4946 | } else if (IS_RELAXNG(node, "mixed")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4947 | if (node->children == NULL) { |
| 4948 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, "Mixed is empty\n", |
| 4949 | NULL, NULL); |
| 4950 | def = NULL; |
| 4951 | } else { |
| 4952 | def = xmlRelaxNGParseInterleave(ctxt, node); |
| 4953 | if (def != NULL) { |
| 4954 | xmlRelaxNGDefinePtr tmp; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4955 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4956 | if ((def->content != NULL) && (def->content->next != NULL)) { |
| 4957 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
| 4958 | if (tmp != NULL) { |
| 4959 | tmp->type = XML_RELAXNG_GROUP; |
| 4960 | tmp->content = def->content; |
| 4961 | def->content = tmp; |
| 4962 | } |
| 4963 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4964 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4965 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
| 4966 | if (tmp == NULL) |
| 4967 | return (def); |
| 4968 | tmp->type = XML_RELAXNG_TEXT; |
| 4969 | tmp->next = def->content; |
| 4970 | def->content = tmp; |
| 4971 | } |
| 4972 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4973 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4974 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_CONSTRUCT, |
| 4975 | "Unexpected node %s is not a pattern\n", node->name, |
| 4976 | NULL); |
| 4977 | def = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4978 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4979 | return (def); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4980 | } |
| 4981 | |
| 4982 | /** |
| 4983 | * xmlRelaxNGParseAttribute: |
| 4984 | * @ctxt: a Relax-NG parser context |
| 4985 | * @node: the element node |
| 4986 | * |
| 4987 | * parse the content of a RelaxNG attribute node. |
| 4988 | * |
| 4989 | * Returns the definition pointer or NULL in case of error. |
| 4990 | */ |
| 4991 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4992 | xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4993 | { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4994 | xmlRelaxNGDefinePtr ret, cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4995 | xmlNodePtr child; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4996 | int old_flags; |
| 4997 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4998 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4999 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5000 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5001 | ret->type = XML_RELAXNG_ATTRIBUTE; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5002 | ret->parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5003 | child = node->children; |
| 5004 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5005 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_EMPTY, |
| 5006 | "xmlRelaxNGParseattribute: attribute has no children\n", |
| 5007 | NULL, NULL); |
| 5008 | return (ret); |
| 5009 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5010 | old_flags = ctxt->flags; |
| 5011 | ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5012 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5013 | if (cur != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5014 | child = child->next; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5015 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5016 | if (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5017 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 5018 | if (cur != NULL) { |
| 5019 | switch (cur->type) { |
| 5020 | case XML_RELAXNG_EMPTY: |
| 5021 | case XML_RELAXNG_NOT_ALLOWED: |
| 5022 | case XML_RELAXNG_TEXT: |
| 5023 | case XML_RELAXNG_ELEMENT: |
| 5024 | case XML_RELAXNG_DATATYPE: |
| 5025 | case XML_RELAXNG_VALUE: |
| 5026 | case XML_RELAXNG_LIST: |
| 5027 | case XML_RELAXNG_REF: |
| 5028 | case XML_RELAXNG_PARENTREF: |
| 5029 | case XML_RELAXNG_EXTERNALREF: |
| 5030 | case XML_RELAXNG_DEF: |
| 5031 | case XML_RELAXNG_ONEORMORE: |
| 5032 | case XML_RELAXNG_ZEROORMORE: |
| 5033 | case XML_RELAXNG_OPTIONAL: |
| 5034 | case XML_RELAXNG_CHOICE: |
| 5035 | case XML_RELAXNG_GROUP: |
| 5036 | case XML_RELAXNG_INTERLEAVE: |
| 5037 | case XML_RELAXNG_ATTRIBUTE: |
| 5038 | ret->content = cur; |
| 5039 | cur->parent = ret; |
| 5040 | break; |
| 5041 | case XML_RELAXNG_START: |
| 5042 | case XML_RELAXNG_PARAM: |
| 5043 | case XML_RELAXNG_EXCEPT: |
| 5044 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CONTENT, |
| 5045 | "attribute has invalid content\n", NULL, |
| 5046 | NULL); |
| 5047 | break; |
| 5048 | case XML_RELAXNG_NOOP: |
| 5049 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_NOOP, |
| 5050 | "RNG Internal error, noop found in attribute\n", |
| 5051 | NULL, NULL); |
| 5052 | break; |
| 5053 | } |
| 5054 | } |
| 5055 | child = child->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5056 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5057 | if (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5058 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CHILDREN, |
| 5059 | "attribute has multiple children\n", NULL, NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5060 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5061 | ctxt->flags = old_flags; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5062 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5063 | } |
| 5064 | |
| 5065 | /** |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5066 | * xmlRelaxNGParseExceptNameClass: |
| 5067 | * @ctxt: a Relax-NG parser context |
| 5068 | * @node: the except node |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5069 | * @attr: 1 if within an attribute, 0 if within an element |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5070 | * |
| 5071 | * parse the content of a RelaxNG nameClass node. |
| 5072 | * |
| 5073 | * Returns the definition pointer or NULL in case of error. |
| 5074 | */ |
| 5075 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5076 | xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5077 | xmlNodePtr node, int attr) |
| 5078 | { |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5079 | xmlRelaxNGDefinePtr ret, cur, last = NULL; |
| 5080 | xmlNodePtr child; |
| 5081 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5082 | if (!IS_RELAXNG(node, "except")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5083 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MISSING, |
| 5084 | "Expecting an except node\n", NULL, NULL); |
| 5085 | return (NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5086 | } |
| 5087 | if (node->next != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5088 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MULTIPLE, |
| 5089 | "exceptNameClass allows only a single except node\n", |
| 5090 | NULL, NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5091 | } |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5092 | if (node->children == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5093 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_EMPTY, "except has no content\n", |
| 5094 | NULL, NULL); |
| 5095 | return (NULL); |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5096 | } |
| 5097 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5098 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5099 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5100 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5101 | ret->type = XML_RELAXNG_EXCEPT; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5102 | child = node->children; |
| 5103 | while (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5104 | cur = xmlRelaxNGNewDefine(ctxt, child); |
| 5105 | if (cur == NULL) |
| 5106 | break; |
| 5107 | if (attr) |
| 5108 | cur->type = XML_RELAXNG_ATTRIBUTE; |
| 5109 | else |
| 5110 | cur->type = XML_RELAXNG_ELEMENT; |
| 5111 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 5112 | if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5113 | if (last == NULL) { |
| 5114 | ret->content = cur; |
| 5115 | } else { |
| 5116 | last->next = cur; |
| 5117 | } |
| 5118 | last = cur; |
| 5119 | } |
| 5120 | child = child->next; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5121 | } |
| 5122 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5123 | return (ret); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5124 | } |
| 5125 | |
| 5126 | /** |
| 5127 | * xmlRelaxNGParseNameClass: |
| 5128 | * @ctxt: a Relax-NG parser context |
| 5129 | * @node: the nameClass node |
| 5130 | * @def: the current definition |
| 5131 | * |
| 5132 | * parse the content of a RelaxNG nameClass node. |
| 5133 | * |
| 5134 | * Returns the definition pointer or NULL in case of error. |
| 5135 | */ |
| 5136 | static xmlRelaxNGDefinePtr |
| 5137 | xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5138 | xmlRelaxNGDefinePtr def) |
| 5139 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5140 | xmlRelaxNGDefinePtr ret, tmp; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5141 | xmlChar *val; |
| 5142 | |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5143 | ret = def; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5144 | if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) || |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5145 | (IS_RELAXNG(node, "nsName"))) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5146 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 5147 | (def->type != XML_RELAXNG_ATTRIBUTE)) { |
| 5148 | ret = xmlRelaxNGNewDefine(ctxt, node); |
| 5149 | if (ret == NULL) |
| 5150 | return (NULL); |
| 5151 | ret->parent = def; |
| 5152 | if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) |
| 5153 | ret->type = XML_RELAXNG_ATTRIBUTE; |
| 5154 | else |
| 5155 | ret->type = XML_RELAXNG_ELEMENT; |
| 5156 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5157 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5158 | if (IS_RELAXNG(node, "name")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5159 | val = xmlNodeGetContent(node); |
| 5160 | xmlRelaxNGNormExtSpace(val); |
| 5161 | if (xmlValidateNCName(val, 0)) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5162 | if (node->parent != NULL) |
| 5163 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME, |
| 5164 | "Element %s name '%s' is not an NCName\n", |
| 5165 | node->parent->name, val); |
| 5166 | else |
| 5167 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME, |
| 5168 | "name '%s' is not an NCName\n", |
| 5169 | val, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5170 | } |
| 5171 | ret->name = val; |
| 5172 | val = xmlGetProp(node, BAD_CAST "ns"); |
| 5173 | ret->ns = val; |
| 5174 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5175 | (val != NULL) && |
| 5176 | (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5177 | xmlRngPErr(ctxt, node, XML_RNGP_XML_NS, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5178 | "Attribute with namespace '%s' is not allowed\n", |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5179 | val, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5180 | } |
| 5181 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5182 | (val != NULL) && |
| 5183 | (val[0] == 0) && (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5184 | xmlRngPErr(ctxt, node, XML_RNGP_XMLNS_NAME, |
| 5185 | "Attribute with QName 'xmlns' is not allowed\n", |
| 5186 | val, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5187 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5188 | } else if (IS_RELAXNG(node, "anyName")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5189 | ret->name = NULL; |
| 5190 | ret->ns = NULL; |
| 5191 | if (node->children != NULL) { |
| 5192 | ret->nameClass = |
| 5193 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
| 5194 | (def->type == |
| 5195 | XML_RELAXNG_ATTRIBUTE)); |
| 5196 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5197 | } else if (IS_RELAXNG(node, "nsName")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5198 | ret->name = NULL; |
| 5199 | ret->ns = xmlGetProp(node, BAD_CAST "ns"); |
| 5200 | if (ret->ns == NULL) { |
| 5201 | xmlRngPErr(ctxt, node, XML_RNGP_NSNAME_NO_NS, |
| 5202 | "nsName has no ns attribute\n", NULL, NULL); |
| 5203 | } |
| 5204 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5205 | (ret->ns != NULL) && |
| 5206 | (xmlStrEqual |
| 5207 | (ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
| 5208 | xmlRngPErr(ctxt, node, XML_RNGP_XML_NS, |
| 5209 | "Attribute with namespace '%s' is not allowed\n", |
| 5210 | ret->ns, NULL); |
| 5211 | } |
| 5212 | if (node->children != NULL) { |
| 5213 | ret->nameClass = |
| 5214 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
| 5215 | (def->type == |
| 5216 | XML_RELAXNG_ATTRIBUTE)); |
| 5217 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5218 | } else if (IS_RELAXNG(node, "choice")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5219 | xmlNodePtr child; |
| 5220 | xmlRelaxNGDefinePtr last = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5221 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5222 | ret = xmlRelaxNGNewDefine(ctxt, node); |
| 5223 | if (ret == NULL) |
| 5224 | return (NULL); |
| 5225 | ret->parent = def; |
| 5226 | ret->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5227 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5228 | if (node->children == NULL) { |
| 5229 | xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_EMPTY, |
| 5230 | "Element choice is empty\n", NULL, NULL); |
| 5231 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5232 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5233 | child = node->children; |
| 5234 | while (child != NULL) { |
| 5235 | tmp = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5236 | if (tmp != NULL) { |
| 5237 | if (last == NULL) { |
| 5238 | last = ret->nameClass = tmp; |
| 5239 | } else { |
| 5240 | last->next = tmp; |
| 5241 | last = tmp; |
| 5242 | } |
| 5243 | } |
| 5244 | child = child->next; |
| 5245 | } |
| 5246 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5247 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5248 | xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_CONTENT, |
| 5249 | "expecting name, anyName, nsName or choice : got %s\n", |
| 5250 | node->name, NULL); |
| 5251 | return (NULL); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5252 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5253 | if (ret != def) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5254 | if (def->nameClass == NULL) { |
| 5255 | def->nameClass = ret; |
| 5256 | } else { |
| 5257 | tmp = def->nameClass; |
| 5258 | while (tmp->next != NULL) { |
| 5259 | tmp = tmp->next; |
| 5260 | } |
| 5261 | tmp->next = ret; |
| 5262 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5263 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5264 | return (ret); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5265 | } |
| 5266 | |
| 5267 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5268 | * xmlRelaxNGParseElement: |
| 5269 | * @ctxt: a Relax-NG parser context |
| 5270 | * @node: the element node |
| 5271 | * |
| 5272 | * parse the content of a RelaxNG element node. |
| 5273 | * |
| 5274 | * Returns the definition pointer or NULL in case of error. |
| 5275 | */ |
| 5276 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5277 | xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 5278 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5279 | xmlRelaxNGDefinePtr ret, cur, last; |
| 5280 | xmlNodePtr child; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5281 | const xmlChar *olddefine; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5282 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5283 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5284 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5285 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5286 | ret->type = XML_RELAXNG_ELEMENT; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5287 | ret->parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5288 | child = node->children; |
| 5289 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5290 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_EMPTY, |
| 5291 | "xmlRelaxNGParseElement: element has no children\n", |
| 5292 | NULL, NULL); |
| 5293 | return (ret); |
| 5294 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5295 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5296 | if (cur != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5297 | child = child->next; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5298 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5299 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5300 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NO_CONTENT, |
| 5301 | "xmlRelaxNGParseElement: element has no content\n", |
| 5302 | NULL, NULL); |
| 5303 | return (ret); |
| 5304 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5305 | olddefine = ctxt->define; |
| 5306 | ctxt->define = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5307 | last = NULL; |
| 5308 | while (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5309 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 5310 | if (cur != NULL) { |
| 5311 | cur->parent = ret; |
| 5312 | switch (cur->type) { |
| 5313 | case XML_RELAXNG_EMPTY: |
| 5314 | case XML_RELAXNG_NOT_ALLOWED: |
| 5315 | case XML_RELAXNG_TEXT: |
| 5316 | case XML_RELAXNG_ELEMENT: |
| 5317 | case XML_RELAXNG_DATATYPE: |
| 5318 | case XML_RELAXNG_VALUE: |
| 5319 | case XML_RELAXNG_LIST: |
| 5320 | case XML_RELAXNG_REF: |
| 5321 | case XML_RELAXNG_PARENTREF: |
| 5322 | case XML_RELAXNG_EXTERNALREF: |
| 5323 | case XML_RELAXNG_DEF: |
| 5324 | case XML_RELAXNG_ZEROORMORE: |
| 5325 | case XML_RELAXNG_ONEORMORE: |
| 5326 | case XML_RELAXNG_OPTIONAL: |
| 5327 | case XML_RELAXNG_CHOICE: |
| 5328 | case XML_RELAXNG_GROUP: |
| 5329 | case XML_RELAXNG_INTERLEAVE: |
| 5330 | if (last == NULL) { |
| 5331 | ret->content = last = cur; |
| 5332 | } else { |
| 5333 | if ((last->type == XML_RELAXNG_ELEMENT) && |
| 5334 | (ret->content == last)) { |
| 5335 | ret->content = xmlRelaxNGNewDefine(ctxt, node); |
| 5336 | if (ret->content != NULL) { |
| 5337 | ret->content->type = XML_RELAXNG_GROUP; |
| 5338 | ret->content->content = last; |
| 5339 | } else { |
| 5340 | ret->content = last; |
| 5341 | } |
| 5342 | } |
| 5343 | last->next = cur; |
| 5344 | last = cur; |
| 5345 | } |
| 5346 | break; |
| 5347 | case XML_RELAXNG_ATTRIBUTE: |
| 5348 | cur->next = ret->attrs; |
| 5349 | ret->attrs = cur; |
| 5350 | break; |
| 5351 | case XML_RELAXNG_START: |
| 5352 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5353 | "RNG Internal error, start found in element\n", |
| 5354 | NULL, NULL); |
| 5355 | break; |
| 5356 | case XML_RELAXNG_PARAM: |
| 5357 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5358 | "RNG Internal error, param found in element\n", |
| 5359 | NULL, NULL); |
| 5360 | break; |
| 5361 | case XML_RELAXNG_EXCEPT: |
| 5362 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5363 | "RNG Internal error, except found in element\n", |
| 5364 | NULL, NULL); |
| 5365 | break; |
| 5366 | case XML_RELAXNG_NOOP: |
| 5367 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5368 | "RNG Internal error, noop found in element\n", |
| 5369 | NULL, NULL); |
| 5370 | break; |
| 5371 | } |
| 5372 | } |
| 5373 | child = child->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5374 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5375 | ctxt->define = olddefine; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5376 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5377 | } |
| 5378 | |
| 5379 | /** |
| 5380 | * xmlRelaxNGParsePatterns: |
| 5381 | * @ctxt: a Relax-NG parser context |
| 5382 | * @nodes: list of nodes |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5383 | * @group: use an implicit <group> for elements |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5384 | * |
| 5385 | * parse the content of a RelaxNG start node. |
| 5386 | * |
| 5387 | * Returns the definition pointer or NULL in case of error. |
| 5388 | */ |
| 5389 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5390 | xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5391 | int group) |
| 5392 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5393 | xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5394 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5395 | parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5396 | while (nodes != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5397 | if (IS_RELAXNG(nodes, "element")) { |
| 5398 | cur = xmlRelaxNGParseElement(ctxt, nodes); |
| 5399 | if (def == NULL) { |
| 5400 | def = last = cur; |
| 5401 | } else { |
| 5402 | if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) && |
| 5403 | (def == last)) { |
| 5404 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
| 5405 | def->type = XML_RELAXNG_GROUP; |
| 5406 | def->content = last; |
| 5407 | } |
| 5408 | last->next = cur; |
| 5409 | last = cur; |
| 5410 | } |
| 5411 | cur->parent = parent; |
| 5412 | } else { |
| 5413 | cur = xmlRelaxNGParsePattern(ctxt, nodes); |
| 5414 | if (cur != NULL) { |
| 5415 | if (def == NULL) { |
| 5416 | def = last = cur; |
| 5417 | } else { |
| 5418 | last->next = cur; |
| 5419 | last = cur; |
| 5420 | } |
| 5421 | } |
| 5422 | } |
| 5423 | nodes = nodes->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5424 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5425 | return (def); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5426 | } |
| 5427 | |
| 5428 | /** |
| 5429 | * xmlRelaxNGParseStart: |
| 5430 | * @ctxt: a Relax-NG parser context |
| 5431 | * @nodes: start children nodes |
| 5432 | * |
| 5433 | * parse the content of a RelaxNG start node. |
| 5434 | * |
| 5435 | * Returns 0 in case of success, -1 in case of error |
| 5436 | */ |
| 5437 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5438 | xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) |
| 5439 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5440 | int ret = 0; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5441 | xmlRelaxNGDefinePtr def = NULL, last; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5442 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5443 | if (nodes == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5444 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, "start has no children\n", |
| 5445 | NULL, NULL); |
| 5446 | return (-1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5447 | } |
| 5448 | if (IS_RELAXNG(nodes, "empty")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5449 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
| 5450 | if (def == NULL) |
| 5451 | return (-1); |
| 5452 | def->type = XML_RELAXNG_EMPTY; |
| 5453 | if (nodes->children != NULL) { |
| 5454 | xmlRngPErr(ctxt, nodes, XML_RNGP_EMPTY_CONTENT, |
| 5455 | "element empty is not empty\n", NULL, NULL); |
| 5456 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5457 | } else if (IS_RELAXNG(nodes, "notAllowed")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5458 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
| 5459 | if (def == NULL) |
| 5460 | return (-1); |
| 5461 | def->type = XML_RELAXNG_NOT_ALLOWED; |
| 5462 | if (nodes->children != NULL) { |
| 5463 | xmlRngPErr(ctxt, nodes, XML_RNGP_NOTALLOWED_NOT_EMPTY, |
| 5464 | "element notAllowed is not empty\n", NULL, NULL); |
| 5465 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5466 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5467 | def = xmlRelaxNGParsePatterns(ctxt, nodes, 1); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5468 | } |
| 5469 | if (ctxt->grammar->start != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5470 | last = ctxt->grammar->start; |
| 5471 | while (last->next != NULL) |
| 5472 | last = last->next; |
| 5473 | last->next = def; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5474 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5475 | ctxt->grammar->start = def; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5476 | } |
| 5477 | nodes = nodes->next; |
| 5478 | if (nodes != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5479 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_CONTENT, |
| 5480 | "start more than one children\n", NULL, NULL); |
| 5481 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5482 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5483 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5484 | } |
| 5485 | |
| 5486 | /** |
| 5487 | * xmlRelaxNGParseGrammarContent: |
| 5488 | * @ctxt: a Relax-NG parser context |
| 5489 | * @nodes: grammar children nodes |
| 5490 | * |
| 5491 | * parse the content of a RelaxNG grammar node. |
| 5492 | * |
| 5493 | * Returns 0 in case of success, -1 in case of error |
| 5494 | */ |
| 5495 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5496 | xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt, |
| 5497 | xmlNodePtr nodes) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5498 | { |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5499 | int ret = 0, tmp; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5500 | |
| 5501 | if (nodes == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5502 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_EMPTY, |
| 5503 | "grammar has no children\n", NULL, NULL); |
| 5504 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5505 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5506 | while (nodes != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5507 | if (IS_RELAXNG(nodes, "start")) { |
| 5508 | if (nodes->children == NULL) { |
| 5509 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, |
| 5510 | "start has no children\n", NULL, NULL); |
| 5511 | } else { |
| 5512 | tmp = xmlRelaxNGParseStart(ctxt, nodes->children); |
| 5513 | if (tmp != 0) |
| 5514 | ret = -1; |
| 5515 | } |
| 5516 | } else if (IS_RELAXNG(nodes, "define")) { |
| 5517 | tmp = xmlRelaxNGParseDefine(ctxt, nodes); |
| 5518 | if (tmp != 0) |
| 5519 | ret = -1; |
| 5520 | } else if (IS_RELAXNG(nodes, "include")) { |
| 5521 | tmp = xmlRelaxNGParseInclude(ctxt, nodes); |
| 5522 | if (tmp != 0) |
| 5523 | ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5524 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5525 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT, |
| 5526 | "grammar has unexpected child %s\n", nodes->name, |
| 5527 | NULL); |
| 5528 | ret = -1; |
| 5529 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5530 | nodes = nodes->next; |
| 5531 | } |
| 5532 | return (ret); |
| 5533 | } |
| 5534 | |
| 5535 | /** |
| 5536 | * xmlRelaxNGCheckReference: |
| 5537 | * @ref: the ref |
| 5538 | * @ctxt: a Relax-NG parser context |
| 5539 | * @name: the name associated to the defines |
| 5540 | * |
| 5541 | * Applies the 4.17. combine attribute rule for all the define |
| 5542 | * element of a given grammar using the same name. |
| 5543 | */ |
| 5544 | static void |
| 5545 | xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5546 | xmlRelaxNGParserCtxtPtr ctxt, |
| 5547 | const xmlChar * name) |
| 5548 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5549 | xmlRelaxNGGrammarPtr grammar; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5550 | xmlRelaxNGDefinePtr def, cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5551 | |
| 5552 | grammar = ctxt->grammar; |
| 5553 | if (grammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5554 | xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR, |
| 5555 | "Internal error: no grammar in CheckReference %s\n", |
| 5556 | name, NULL); |
| 5557 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5558 | } |
| 5559 | if (ref->content != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5560 | xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR, |
| 5561 | "Internal error: reference has content in CheckReference %s\n", |
| 5562 | name, NULL); |
| 5563 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5564 | } |
| 5565 | if (grammar->defs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5566 | def = xmlHashLookup(grammar->defs, name); |
| 5567 | if (def != NULL) { |
| 5568 | cur = ref; |
| 5569 | while (cur != NULL) { |
| 5570 | cur->content = def; |
| 5571 | cur = cur->nextHash; |
| 5572 | } |
| 5573 | } else { |
| 5574 | xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF, |
| 5575 | "Reference %s has no matching definition\n", name, |
| 5576 | NULL); |
| 5577 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5578 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5579 | xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF, |
| 5580 | "Reference %s has no matching definition\n", name, |
| 5581 | NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5582 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5583 | } |
| 5584 | |
| 5585 | /** |
| 5586 | * xmlRelaxNGCheckCombine: |
| 5587 | * @define: the define(s) list |
| 5588 | * @ctxt: a Relax-NG parser context |
| 5589 | * @name: the name associated to the defines |
| 5590 | * |
| 5591 | * Applies the 4.17. combine attribute rule for all the define |
| 5592 | * element of a given grammar using the same name. |
| 5593 | */ |
| 5594 | static void |
| 5595 | xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5596 | xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * name) |
| 5597 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5598 | xmlChar *combine; |
| 5599 | int choiceOrInterleave = -1; |
| 5600 | int missing = 0; |
| 5601 | xmlRelaxNGDefinePtr cur, last, tmp, tmp2; |
| 5602 | |
| 5603 | if (define->nextHash == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5604 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5605 | cur = define; |
| 5606 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5607 | combine = xmlGetProp(cur->node, BAD_CAST "combine"); |
| 5608 | if (combine != NULL) { |
| 5609 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
| 5610 | if (choiceOrInterleave == -1) |
| 5611 | choiceOrInterleave = 1; |
| 5612 | else if (choiceOrInterleave == 0) { |
| 5613 | xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE, |
| 5614 | "Defines for %s use both 'choice' and 'interleave'\n", |
| 5615 | name, NULL); |
| 5616 | } |
| 5617 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
| 5618 | if (choiceOrInterleave == -1) |
| 5619 | choiceOrInterleave = 0; |
| 5620 | else if (choiceOrInterleave == 1) { |
| 5621 | xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE, |
| 5622 | "Defines for %s use both 'choice' and 'interleave'\n", |
| 5623 | name, NULL); |
| 5624 | } |
| 5625 | } else { |
| 5626 | xmlRngPErr(ctxt, define->node, XML_RNGP_UNKNOWN_COMBINE, |
| 5627 | "Defines for %s use unknown combine value '%s''\n", |
| 5628 | name, combine); |
| 5629 | } |
| 5630 | xmlFree(combine); |
| 5631 | } else { |
| 5632 | if (missing == 0) |
| 5633 | missing = 1; |
| 5634 | else { |
| 5635 | xmlRngPErr(ctxt, define->node, XML_RNGP_NEED_COMBINE, |
| 5636 | "Some defines for %s needs the combine attribute\n", |
| 5637 | name, NULL); |
| 5638 | } |
| 5639 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5640 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5641 | cur = cur->nextHash; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5642 | } |
| 5643 | #ifdef DEBUG |
| 5644 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5645 | "xmlRelaxNGCheckCombine(): merging %s defines: %d\n", |
| 5646 | name, choiceOrInterleave); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5647 | #endif |
| 5648 | if (choiceOrInterleave == -1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5649 | choiceOrInterleave = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5650 | cur = xmlRelaxNGNewDefine(ctxt, define->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5651 | if (cur == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5652 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5653 | if (choiceOrInterleave == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5654 | cur->type = XML_RELAXNG_INTERLEAVE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5655 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5656 | cur->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5657 | tmp = define; |
| 5658 | last = NULL; |
| 5659 | while (tmp != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5660 | if (tmp->content != NULL) { |
| 5661 | if (tmp->content->next != NULL) { |
| 5662 | /* |
| 5663 | * we need first to create a wrapper. |
| 5664 | */ |
| 5665 | tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node); |
| 5666 | if (tmp2 == NULL) |
| 5667 | break; |
| 5668 | tmp2->type = XML_RELAXNG_GROUP; |
| 5669 | tmp2->content = tmp->content; |
| 5670 | } else { |
| 5671 | tmp2 = tmp->content; |
| 5672 | } |
| 5673 | if (last == NULL) { |
| 5674 | cur->content = tmp2; |
| 5675 | } else { |
| 5676 | last->next = tmp2; |
| 5677 | } |
| 5678 | last = tmp2; |
| 5679 | } |
| 5680 | tmp->content = cur; |
| 5681 | tmp = tmp->nextHash; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5682 | } |
| 5683 | define->content = cur; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5684 | if (choiceOrInterleave == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5685 | if (ctxt->interleaves == NULL) |
| 5686 | ctxt->interleaves = xmlHashCreate(10); |
| 5687 | if (ctxt->interleaves == NULL) { |
| 5688 | xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5689 | "Failed to create interleaves hash table\n", NULL, |
| 5690 | NULL); |
| 5691 | } else { |
| 5692 | char tmpname[32]; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5693 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5694 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
| 5695 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < |
| 5696 | 0) { |
| 5697 | xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5698 | "Failed to add %s to hash table\n", |
| 5699 | (const xmlChar *) tmpname, NULL); |
| 5700 | } |
| 5701 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5702 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5703 | } |
| 5704 | |
| 5705 | /** |
| 5706 | * xmlRelaxNGCombineStart: |
| 5707 | * @ctxt: a Relax-NG parser context |
| 5708 | * @grammar: the grammar |
| 5709 | * |
| 5710 | * Applies the 4.17. combine rule for all the start |
| 5711 | * element of a given grammar. |
| 5712 | */ |
| 5713 | static void |
| 5714 | xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5715 | xmlRelaxNGGrammarPtr grammar) |
| 5716 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5717 | xmlRelaxNGDefinePtr starts; |
| 5718 | xmlChar *combine; |
| 5719 | int choiceOrInterleave = -1; |
| 5720 | int missing = 0; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5721 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5722 | |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5723 | starts = grammar->start; |
| 5724 | if ((starts == NULL) || (starts->next == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5725 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5726 | cur = starts; |
| 5727 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5728 | if ((cur->node == NULL) || (cur->node->parent == NULL) || |
| 5729 | (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) { |
| 5730 | combine = NULL; |
| 5731 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_MISSING, |
| 5732 | "Internal error: start element not found\n", NULL, |
| 5733 | NULL); |
| 5734 | } else { |
| 5735 | combine = xmlGetProp(cur->node->parent, BAD_CAST "combine"); |
| 5736 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5737 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5738 | if (combine != NULL) { |
| 5739 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
| 5740 | if (choiceOrInterleave == -1) |
| 5741 | choiceOrInterleave = 1; |
| 5742 | else if (choiceOrInterleave == 0) { |
| 5743 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE, |
| 5744 | "<start> use both 'choice' and 'interleave'\n", |
| 5745 | NULL, NULL); |
| 5746 | } |
| 5747 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
| 5748 | if (choiceOrInterleave == -1) |
| 5749 | choiceOrInterleave = 0; |
| 5750 | else if (choiceOrInterleave == 1) { |
| 5751 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE, |
| 5752 | "<start> use both 'choice' and 'interleave'\n", |
| 5753 | NULL, NULL); |
| 5754 | } |
| 5755 | } else { |
| 5756 | xmlRngPErr(ctxt, cur->node, XML_RNGP_UNKNOWN_COMBINE, |
| 5757 | "<start> uses unknown combine value '%s''\n", |
| 5758 | combine, NULL); |
| 5759 | } |
| 5760 | xmlFree(combine); |
| 5761 | } else { |
| 5762 | if (missing == 0) |
| 5763 | missing = 1; |
| 5764 | else { |
| 5765 | xmlRngPErr(ctxt, cur->node, XML_RNGP_NEED_COMBINE, |
| 5766 | "Some <start> element miss the combine attribute\n", |
| 5767 | NULL, NULL); |
| 5768 | } |
| 5769 | } |
| 5770 | |
| 5771 | cur = cur->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5772 | } |
| 5773 | #ifdef DEBUG |
| 5774 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5775 | "xmlRelaxNGCombineStart(): merging <start>: %d\n", |
| 5776 | choiceOrInterleave); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5777 | #endif |
| 5778 | if (choiceOrInterleave == -1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5779 | choiceOrInterleave = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5780 | cur = xmlRelaxNGNewDefine(ctxt, starts->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5781 | if (cur == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5782 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5783 | if (choiceOrInterleave == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5784 | cur->type = XML_RELAXNG_INTERLEAVE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5785 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5786 | cur->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5787 | cur->content = grammar->start; |
| 5788 | grammar->start = cur; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5789 | if (choiceOrInterleave == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5790 | if (ctxt->interleaves == NULL) |
| 5791 | ctxt->interleaves = xmlHashCreate(10); |
| 5792 | if (ctxt->interleaves == NULL) { |
| 5793 | xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5794 | "Failed to create interleaves hash table\n", NULL, |
| 5795 | NULL); |
| 5796 | } else { |
| 5797 | char tmpname[32]; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5798 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5799 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
| 5800 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < |
| 5801 | 0) { |
| 5802 | xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5803 | "Failed to add %s to hash table\n", |
| 5804 | (const xmlChar *) tmpname, NULL); |
| 5805 | } |
| 5806 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5807 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5808 | } |
| 5809 | |
| 5810 | /** |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5811 | * xmlRelaxNGCheckCycles: |
| 5812 | * @ctxt: a Relax-NG parser context |
| 5813 | * @nodes: grammar children nodes |
| 5814 | * @depth: the counter |
| 5815 | * |
| 5816 | * Check for cycles. |
| 5817 | * |
| 5818 | * Returns 0 if check passed, and -1 in case of error |
| 5819 | */ |
| 5820 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5821 | xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt, |
| 5822 | xmlRelaxNGDefinePtr cur, int depth) |
| 5823 | { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5824 | int ret = 0; |
| 5825 | |
| 5826 | while ((ret == 0) && (cur != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5827 | if ((cur->type == XML_RELAXNG_REF) || |
| 5828 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5829 | if (cur->depth == -1) { |
| 5830 | cur->depth = depth; |
| 5831 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
| 5832 | cur->depth = -2; |
| 5833 | } else if (depth == cur->depth) { |
| 5834 | xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_CYCLE, |
| 5835 | "Detected a cycle in %s references\n", |
| 5836 | cur->name, NULL); |
| 5837 | return (-1); |
| 5838 | } |
| 5839 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
| 5840 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1); |
| 5841 | } else { |
| 5842 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
| 5843 | } |
| 5844 | cur = cur->next; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5845 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5846 | return (ret); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5847 | } |
| 5848 | |
| 5849 | /** |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5850 | * xmlRelaxNGTryUnlink: |
| 5851 | * @ctxt: a Relax-NG parser context |
| 5852 | * @cur: the definition to unlink |
| 5853 | * @parent: the parent definition |
| 5854 | * @prev: the previous sibling definition |
| 5855 | * |
| 5856 | * Try to unlink a definition. If not possble make it a NOOP |
| 5857 | * |
| 5858 | * Returns the new prev definition |
| 5859 | */ |
| 5860 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5861 | xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 5862 | xmlRelaxNGDefinePtr cur, |
| 5863 | xmlRelaxNGDefinePtr parent, xmlRelaxNGDefinePtr prev) |
| 5864 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5865 | if (prev != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5866 | prev->next = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5867 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5868 | if (parent != NULL) { |
| 5869 | if (parent->content == cur) |
| 5870 | parent->content = cur->next; |
| 5871 | else if (parent->attrs == cur) |
| 5872 | parent->attrs = cur->next; |
| 5873 | else if (parent->nameClass == cur) |
| 5874 | parent->nameClass = cur->next; |
| 5875 | } else { |
| 5876 | cur->type = XML_RELAXNG_NOOP; |
| 5877 | prev = cur; |
| 5878 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5879 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5880 | return (prev); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5881 | } |
| 5882 | |
| 5883 | /** |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5884 | * xmlRelaxNGSimplify: |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5885 | * @ctxt: a Relax-NG parser context |
| 5886 | * @nodes: grammar children nodes |
| 5887 | * |
| 5888 | * Check for simplification of empty and notAllowed |
| 5889 | */ |
| 5890 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5891 | xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt, |
| 5892 | xmlRelaxNGDefinePtr cur, xmlRelaxNGDefinePtr parent) |
| 5893 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5894 | xmlRelaxNGDefinePtr prev = NULL; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5895 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5896 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5897 | if ((cur->type == XML_RELAXNG_REF) || |
| 5898 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5899 | if (cur->depth != -3) { |
| 5900 | cur->depth = -3; |
| 5901 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
| 5902 | } |
| 5903 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
| 5904 | cur->parent = parent; |
| 5905 | if ((parent != NULL) && |
| 5906 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
| 5907 | (parent->type == XML_RELAXNG_LIST) || |
| 5908 | (parent->type == XML_RELAXNG_GROUP) || |
| 5909 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 5910 | (parent->type == XML_RELAXNG_ONEORMORE) || |
| 5911 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 5912 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
| 5913 | break; |
| 5914 | } |
| 5915 | if ((parent != NULL) && (parent->type == XML_RELAXNG_CHOICE)) { |
| 5916 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5917 | } else |
| 5918 | prev = cur; |
| 5919 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 5920 | cur->parent = parent; |
| 5921 | if ((parent != NULL) && |
| 5922 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
| 5923 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 5924 | parent->type = XML_RELAXNG_EMPTY; |
| 5925 | break; |
| 5926 | } |
| 5927 | if ((parent != NULL) && |
| 5928 | ((parent->type == XML_RELAXNG_GROUP) || |
| 5929 | (parent->type == XML_RELAXNG_INTERLEAVE))) { |
| 5930 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5931 | } else |
| 5932 | prev = cur; |
| 5933 | } else { |
| 5934 | cur->parent = parent; |
| 5935 | if (cur->content != NULL) |
| 5936 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
| 5937 | if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL)) |
| 5938 | xmlRelaxNGSimplify(ctxt, cur->attrs, cur); |
| 5939 | if (cur->nameClass != NULL) |
| 5940 | xmlRelaxNGSimplify(ctxt, cur->nameClass, cur); |
| 5941 | /* |
| 5942 | * On Elements, try to move attribute only generating rules on |
| 5943 | * the attrs rules. |
| 5944 | */ |
| 5945 | if (cur->type == XML_RELAXNG_ELEMENT) { |
| 5946 | int attronly; |
| 5947 | xmlRelaxNGDefinePtr tmp, pre; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 5948 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5949 | while (cur->content != NULL) { |
| 5950 | attronly = |
| 5951 | xmlRelaxNGGenerateAttributes(ctxt, cur->content); |
| 5952 | if (attronly == 1) { |
| 5953 | /* |
| 5954 | * migrate cur->content to attrs |
| 5955 | */ |
| 5956 | tmp = cur->content; |
| 5957 | cur->content = tmp->next; |
| 5958 | tmp->next = cur->attrs; |
| 5959 | cur->attrs = tmp; |
| 5960 | } else { |
| 5961 | /* |
| 5962 | * cur->content can generate elements or text |
| 5963 | */ |
| 5964 | break; |
| 5965 | } |
| 5966 | } |
| 5967 | pre = cur->content; |
| 5968 | while ((pre != NULL) && (pre->next != NULL)) { |
| 5969 | tmp = pre->next; |
| 5970 | attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp); |
| 5971 | if (attronly == 1) { |
| 5972 | /* |
| 5973 | * migrate tmp to attrs |
| 5974 | */ |
| 5975 | pre->next = tmp->next; |
| 5976 | tmp->next = cur->attrs; |
| 5977 | cur->attrs = tmp; |
| 5978 | } else { |
| 5979 | pre = tmp; |
| 5980 | } |
| 5981 | } |
| 5982 | } |
| 5983 | /* |
| 5984 | * This may result in a simplification |
| 5985 | */ |
| 5986 | if ((cur->type == XML_RELAXNG_GROUP) || |
| 5987 | (cur->type == XML_RELAXNG_INTERLEAVE)) { |
| 5988 | if (cur->content == NULL) |
| 5989 | cur->type = XML_RELAXNG_EMPTY; |
| 5990 | else if (cur->content->next == NULL) { |
| 5991 | if ((parent == NULL) && (prev == NULL)) { |
| 5992 | cur->type = XML_RELAXNG_NOOP; |
| 5993 | } else if (prev == NULL) { |
| 5994 | parent->content = cur->content; |
| 5995 | cur->content->next = cur->next; |
| 5996 | cur = cur->content; |
| 5997 | } else { |
| 5998 | cur->content->next = cur->next; |
| 5999 | prev->next = cur->content; |
| 6000 | cur = cur->content; |
| 6001 | } |
| 6002 | } |
| 6003 | } |
| 6004 | /* |
| 6005 | * the current node may have been transformed back |
| 6006 | */ |
| 6007 | if ((cur->type == XML_RELAXNG_EXCEPT) && |
| 6008 | (cur->content != NULL) && |
| 6009 | (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) { |
| 6010 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 6011 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
| 6012 | if ((parent != NULL) && |
| 6013 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
| 6014 | (parent->type == XML_RELAXNG_LIST) || |
| 6015 | (parent->type == XML_RELAXNG_GROUP) || |
| 6016 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 6017 | (parent->type == XML_RELAXNG_ONEORMORE) || |
| 6018 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 6019 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
| 6020 | break; |
| 6021 | } |
| 6022 | if ((parent != NULL) && |
| 6023 | (parent->type == XML_RELAXNG_CHOICE)) { |
| 6024 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 6025 | } else |
| 6026 | prev = cur; |
| 6027 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 6028 | if ((parent != NULL) && |
| 6029 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
| 6030 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 6031 | parent->type = XML_RELAXNG_EMPTY; |
| 6032 | break; |
| 6033 | } |
| 6034 | if ((parent != NULL) && |
| 6035 | ((parent->type == XML_RELAXNG_GROUP) || |
| 6036 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 6037 | (parent->type == XML_RELAXNG_CHOICE))) { |
| 6038 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 6039 | } else |
| 6040 | prev = cur; |
| 6041 | } else { |
| 6042 | prev = cur; |
| 6043 | } |
| 6044 | } |
| 6045 | cur = cur->next; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6046 | } |
| 6047 | } |
| 6048 | |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6049 | /** |
| 6050 | * xmlRelaxNGGroupContentType: |
| 6051 | * @ct1: the first content type |
| 6052 | * @ct2: the second content type |
| 6053 | * |
| 6054 | * Try to group 2 content types |
| 6055 | * |
| 6056 | * Returns the content type |
| 6057 | */ |
| 6058 | static xmlRelaxNGContentType |
| 6059 | xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6060 | xmlRelaxNGContentType ct2) |
| 6061 | { |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6062 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6063 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
| 6064 | return (XML_RELAXNG_CONTENT_ERROR); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6065 | if (ct1 == XML_RELAXNG_CONTENT_EMPTY) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6066 | return (ct2); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6067 | if (ct2 == XML_RELAXNG_CONTENT_EMPTY) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6068 | return (ct1); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6069 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6070 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
| 6071 | return (XML_RELAXNG_CONTENT_COMPLEX); |
| 6072 | return (XML_RELAXNG_CONTENT_ERROR); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6073 | } |
| 6074 | |
| 6075 | /** |
| 6076 | * xmlRelaxNGMaxContentType: |
| 6077 | * @ct1: the first content type |
| 6078 | * @ct2: the second content type |
| 6079 | * |
| 6080 | * Compute the max content-type |
| 6081 | * |
| 6082 | * Returns the content type |
| 6083 | */ |
| 6084 | static xmlRelaxNGContentType |
| 6085 | xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6086 | xmlRelaxNGContentType ct2) |
| 6087 | { |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6088 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6089 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
| 6090 | return (XML_RELAXNG_CONTENT_ERROR); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6091 | if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6092 | (ct2 == XML_RELAXNG_CONTENT_SIMPLE)) |
| 6093 | return (XML_RELAXNG_CONTENT_SIMPLE); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6094 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6095 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
| 6096 | return (XML_RELAXNG_CONTENT_COMPLEX); |
| 6097 | return (XML_RELAXNG_CONTENT_EMPTY); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6098 | } |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6099 | |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6100 | /** |
| 6101 | * xmlRelaxNGCheckRules: |
| 6102 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6103 | * @cur: the current definition |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6104 | * @flags: some accumulated flags |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6105 | * @ptype: the parent type |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6106 | * |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6107 | * Check for rules in section 7.1 and 7.2 |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6108 | * |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6109 | * Returns the content type of @cur |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6110 | */ |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6111 | static xmlRelaxNGContentType |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6112 | xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt, |
| 6113 | xmlRelaxNGDefinePtr cur, int flags, |
| 6114 | xmlRelaxNGType ptype) |
| 6115 | { |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6116 | int nflags = flags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6117 | xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6118 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6119 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6120 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6121 | if ((cur->type == XML_RELAXNG_REF) || |
| 6122 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 6123 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6124 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_REF, |
| 6125 | "Found forbidden pattern list//ref\n", NULL, |
| 6126 | NULL); |
| 6127 | } |
| 6128 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6129 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_REF, |
| 6130 | "Found forbidden pattern data/except//ref\n", |
| 6131 | NULL, NULL); |
| 6132 | } |
| 6133 | if (cur->depth > -4) { |
| 6134 | cur->depth = -4; |
| 6135 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, |
| 6136 | flags, cur->type); |
| 6137 | cur->depth = ret - 15; |
| 6138 | } else if (cur->depth == -4) { |
| 6139 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6140 | } else { |
| 6141 | ret = (xmlRelaxNGContentType) (cur->depth + 15); |
| 6142 | } |
| 6143 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
| 6144 | /* |
| 6145 | * The 7.3 Attribute derivation rule for groups is plugged there |
| 6146 | */ |
| 6147 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
| 6148 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6149 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ELEM, |
| 6150 | "Found forbidden pattern data/except//element(ref)\n", |
| 6151 | NULL, NULL); |
| 6152 | } |
| 6153 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6154 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ELEM, |
| 6155 | "Found forbidden pattern list//element(ref)\n", |
| 6156 | NULL, NULL); |
| 6157 | } |
| 6158 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6159 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM, |
| 6160 | "Found forbidden pattern attribute//element(ref)\n", |
| 6161 | NULL, NULL); |
| 6162 | } |
| 6163 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6164 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM, |
| 6165 | "Found forbidden pattern attribute//element(ref)\n", |
| 6166 | NULL, NULL); |
| 6167 | } |
| 6168 | /* |
| 6169 | * reset since in the simple form elements are only child |
| 6170 | * of grammar/define |
| 6171 | */ |
| 6172 | nflags = 0; |
| 6173 | ret = |
| 6174 | xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type); |
| 6175 | if (ret != XML_RELAXNG_CONTENT_EMPTY) { |
| 6176 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_EMPTY, |
| 6177 | "Element %s attributes have a content type error\n", |
| 6178 | cur->name, NULL); |
| 6179 | } |
| 6180 | ret = |
| 6181 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6182 | cur->type); |
| 6183 | if (ret == XML_RELAXNG_CONTENT_ERROR) { |
| 6184 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_ERROR, |
| 6185 | "Element %s has a content type error\n", |
| 6186 | cur->name, NULL); |
| 6187 | } else { |
| 6188 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6189 | } |
| 6190 | } else if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
| 6191 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6192 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ATTR, |
| 6193 | "Found forbidden pattern attribute//attribute\n", |
| 6194 | NULL, NULL); |
| 6195 | } |
| 6196 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6197 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ATTR, |
| 6198 | "Found forbidden pattern list//attribute\n", |
| 6199 | NULL, NULL); |
| 6200 | } |
| 6201 | if (flags & XML_RELAXNG_IN_OOMGROUP) { |
| 6202 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_GROUP_ATTR, |
| 6203 | "Found forbidden pattern oneOrMore//group//attribute\n", |
| 6204 | NULL, NULL); |
| 6205 | } |
| 6206 | if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) { |
| 6207 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_INTERLEAVE_ATTR, |
| 6208 | "Found forbidden pattern oneOrMore//interleave//attribute\n", |
| 6209 | NULL, NULL); |
| 6210 | } |
| 6211 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6212 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ATTR, |
| 6213 | "Found forbidden pattern data/except//attribute\n", |
| 6214 | NULL, NULL); |
| 6215 | } |
| 6216 | if (flags & XML_RELAXNG_IN_START) { |
| 6217 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ATTR, |
| 6218 | "Found forbidden pattern start//attribute\n", |
| 6219 | NULL, NULL); |
| 6220 | } |
| 6221 | if ((!(flags & XML_RELAXNG_IN_ONEORMORE)) |
| 6222 | && (cur->name == NULL)) { |
| 6223 | if (cur->ns == NULL) { |
| 6224 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ANYNAME_ATTR_ANCESTOR, |
| 6225 | "Found anyName attribute without oneOrMore ancestor\n", |
| 6226 | NULL, NULL); |
| 6227 | } else { |
| 6228 | xmlRngPErr(ctxt, cur->node, XML_RNGP_NSNAME_ATTR_ANCESTOR, |
| 6229 | "Found nsName attribute without oneOrMore ancestor\n", |
| 6230 | NULL, NULL); |
| 6231 | } |
| 6232 | } |
| 6233 | nflags = flags | XML_RELAXNG_IN_ATTRIBUTE; |
| 6234 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6235 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6236 | } else if ((cur->type == XML_RELAXNG_ONEORMORE) || |
| 6237 | (cur->type == XML_RELAXNG_ZEROORMORE)) { |
| 6238 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6239 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ONEMORE, |
| 6240 | "Found forbidden pattern data/except//oneOrMore\n", |
| 6241 | NULL, NULL); |
| 6242 | } |
| 6243 | if (flags & XML_RELAXNG_IN_START) { |
| 6244 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ONEMORE, |
| 6245 | "Found forbidden pattern start//oneOrMore\n", |
| 6246 | NULL, NULL); |
| 6247 | } |
| 6248 | nflags = flags | XML_RELAXNG_IN_ONEORMORE; |
| 6249 | ret = |
| 6250 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6251 | cur->type); |
| 6252 | ret = xmlRelaxNGGroupContentType(ret, ret); |
| 6253 | } else if (cur->type == XML_RELAXNG_LIST) { |
| 6254 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6255 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_LIST, |
| 6256 | "Found forbidden pattern list//list\n", NULL, |
| 6257 | NULL); |
| 6258 | } |
| 6259 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6260 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_LIST, |
| 6261 | "Found forbidden pattern data/except//list\n", |
| 6262 | NULL, NULL); |
| 6263 | } |
| 6264 | if (flags & XML_RELAXNG_IN_START) { |
| 6265 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_LIST, |
| 6266 | "Found forbidden pattern start//list\n", NULL, |
| 6267 | NULL); |
| 6268 | } |
| 6269 | nflags = flags | XML_RELAXNG_IN_LIST; |
| 6270 | ret = |
| 6271 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6272 | cur->type); |
| 6273 | } else if (cur->type == XML_RELAXNG_GROUP) { |
| 6274 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6275 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_GROUP, |
| 6276 | "Found forbidden pattern data/except//group\n", |
| 6277 | NULL, NULL); |
| 6278 | } |
| 6279 | if (flags & XML_RELAXNG_IN_START) { |
| 6280 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_GROUP, |
| 6281 | "Found forbidden pattern start//group\n", NULL, |
| 6282 | NULL); |
| 6283 | } |
| 6284 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
| 6285 | nflags = flags | XML_RELAXNG_IN_OOMGROUP; |
| 6286 | else |
| 6287 | nflags = flags; |
| 6288 | ret = |
| 6289 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6290 | cur->type); |
| 6291 | /* |
| 6292 | * The 7.3 Attribute derivation rule for groups is plugged there |
| 6293 | */ |
| 6294 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
| 6295 | } else if (cur->type == XML_RELAXNG_INTERLEAVE) { |
| 6296 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6297 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_INTERLEAVE, |
| 6298 | "Found forbidden pattern list//interleave\n", |
| 6299 | NULL, NULL); |
| 6300 | } |
| 6301 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6302 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE, |
| 6303 | "Found forbidden pattern data/except//interleave\n", |
| 6304 | NULL, NULL); |
| 6305 | } |
| 6306 | if (flags & XML_RELAXNG_IN_START) { |
| 6307 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE, |
| 6308 | "Found forbidden pattern start//interleave\n", |
| 6309 | NULL, NULL); |
| 6310 | } |
| 6311 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
| 6312 | nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE; |
| 6313 | else |
| 6314 | nflags = flags; |
| 6315 | ret = |
| 6316 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6317 | cur->type); |
| 6318 | } else if (cur->type == XML_RELAXNG_EXCEPT) { |
| 6319 | if ((cur->parent != NULL) && |
| 6320 | (cur->parent->type == XML_RELAXNG_DATATYPE)) |
| 6321 | nflags = flags | XML_RELAXNG_IN_DATAEXCEPT; |
| 6322 | else |
| 6323 | nflags = flags; |
| 6324 | ret = |
| 6325 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6326 | cur->type); |
| 6327 | } else if (cur->type == XML_RELAXNG_DATATYPE) { |
| 6328 | if (flags & XML_RELAXNG_IN_START) { |
| 6329 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_DATA, |
| 6330 | "Found forbidden pattern start//data\n", NULL, |
| 6331 | NULL); |
| 6332 | } |
| 6333 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6334 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
| 6335 | } else if (cur->type == XML_RELAXNG_VALUE) { |
| 6336 | if (flags & XML_RELAXNG_IN_START) { |
| 6337 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_VALUE, |
| 6338 | "Found forbidden pattern start//value\n", NULL, |
| 6339 | NULL); |
| 6340 | } |
| 6341 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6342 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
| 6343 | } else if (cur->type == XML_RELAXNG_TEXT) { |
| 6344 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6345 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_TEXT, |
| 6346 | "Found forbidden pattern list//text\n", NULL, |
| 6347 | NULL); |
| 6348 | } |
| 6349 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6350 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_TEXT, |
| 6351 | "Found forbidden pattern data/except//text\n", |
| 6352 | NULL, NULL); |
| 6353 | } |
| 6354 | if (flags & XML_RELAXNG_IN_START) { |
| 6355 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_TEXT, |
| 6356 | "Found forbidden pattern start//text\n", NULL, |
| 6357 | NULL); |
| 6358 | } |
| 6359 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6360 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 6361 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6362 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_EMPTY, |
| 6363 | "Found forbidden pattern data/except//empty\n", |
| 6364 | NULL, NULL); |
| 6365 | } |
| 6366 | if (flags & XML_RELAXNG_IN_START) { |
| 6367 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_EMPTY, |
| 6368 | "Found forbidden pattern start//empty\n", NULL, |
| 6369 | NULL); |
| 6370 | } |
| 6371 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6372 | } else if (cur->type == XML_RELAXNG_CHOICE) { |
| 6373 | xmlRelaxNGCheckChoiceDeterminism(ctxt, cur); |
| 6374 | ret = |
| 6375 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6376 | } else { |
| 6377 | ret = |
| 6378 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6379 | } |
| 6380 | cur = cur->next; |
| 6381 | if (ptype == XML_RELAXNG_GROUP) { |
| 6382 | val = xmlRelaxNGGroupContentType(val, ret); |
| 6383 | } else if (ptype == XML_RELAXNG_INTERLEAVE) { |
| 6384 | tmp = xmlRelaxNGGroupContentType(val, ret); |
| 6385 | if (tmp != XML_RELAXNG_CONTENT_ERROR) |
| 6386 | tmp = xmlRelaxNGMaxContentType(val, ret); |
| 6387 | } else if (ptype == XML_RELAXNG_CHOICE) { |
| 6388 | val = xmlRelaxNGMaxContentType(val, ret); |
| 6389 | } else if (ptype == XML_RELAXNG_LIST) { |
| 6390 | val = XML_RELAXNG_CONTENT_SIMPLE; |
| 6391 | } else if (ptype == XML_RELAXNG_EXCEPT) { |
| 6392 | if (ret == XML_RELAXNG_CONTENT_ERROR) |
| 6393 | val = XML_RELAXNG_CONTENT_ERROR; |
| 6394 | else |
| 6395 | val = XML_RELAXNG_CONTENT_SIMPLE; |
| 6396 | } else { |
| 6397 | val = xmlRelaxNGGroupContentType(val, ret); |
| 6398 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6399 | |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6400 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6401 | return (val); |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6402 | } |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6403 | |
| 6404 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6405 | * xmlRelaxNGParseGrammar: |
| 6406 | * @ctxt: a Relax-NG parser context |
| 6407 | * @nodes: grammar children nodes |
| 6408 | * |
| 6409 | * parse a Relax-NG <grammar> node |
| 6410 | * |
| 6411 | * Returns the internal xmlRelaxNGGrammarPtr built or |
| 6412 | * NULL in case of error |
| 6413 | */ |
| 6414 | static xmlRelaxNGGrammarPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6415 | xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) |
| 6416 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6417 | xmlRelaxNGGrammarPtr ret, tmp, old; |
| 6418 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6419 | #ifdef DEBUG_GRAMMAR |
| 6420 | xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n"); |
| 6421 | #endif |
| 6422 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6423 | ret = xmlRelaxNGNewGrammar(ctxt); |
| 6424 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6425 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6426 | |
| 6427 | /* |
| 6428 | * Link the new grammar in the tree |
| 6429 | */ |
| 6430 | ret->parent = ctxt->grammar; |
| 6431 | if (ctxt->grammar != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6432 | tmp = ctxt->grammar->children; |
| 6433 | if (tmp == NULL) { |
| 6434 | ctxt->grammar->children = ret; |
| 6435 | } else { |
| 6436 | while (tmp->next != NULL) |
| 6437 | tmp = tmp->next; |
| 6438 | tmp->next = ret; |
| 6439 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6440 | } |
| 6441 | |
| 6442 | old = ctxt->grammar; |
| 6443 | ctxt->grammar = ret; |
| 6444 | xmlRelaxNGParseGrammarContent(ctxt, nodes); |
| 6445 | ctxt->grammar = ret; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6446 | if (ctxt->grammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6447 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT, |
| 6448 | "Failed to parse <grammar> content\n", NULL, NULL); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6449 | } else if (ctxt->grammar->start == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6450 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_NO_START, |
| 6451 | "Element <grammar> has no <start>\n", NULL, NULL); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6452 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6453 | |
| 6454 | /* |
| 6455 | * Apply 4.17 mergingd rules to defines and starts |
| 6456 | */ |
| 6457 | xmlRelaxNGCombineStart(ctxt, ret); |
| 6458 | if (ret->defs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6459 | xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine, |
| 6460 | ctxt); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6461 | } |
| 6462 | |
| 6463 | /* |
| 6464 | * link together defines and refs in this grammar |
| 6465 | */ |
| 6466 | if (ret->refs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6467 | xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference, |
| 6468 | ctxt); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6469 | } |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 6470 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6471 | ctxt->grammar = old; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6472 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6473 | } |
| 6474 | |
| 6475 | /** |
| 6476 | * xmlRelaxNGParseDocument: |
| 6477 | * @ctxt: a Relax-NG parser context |
| 6478 | * @node: the root node of the RelaxNG schema |
| 6479 | * |
| 6480 | * parse a Relax-NG definition resource and build an internal |
| 6481 | * xmlRelaxNG struture which can be used to validate instances. |
| 6482 | * |
| 6483 | * Returns the internal XML RelaxNG structure built or |
| 6484 | * NULL in case of error |
| 6485 | */ |
| 6486 | static xmlRelaxNGPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6487 | xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 6488 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6489 | xmlRelaxNGPtr schema = NULL; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6490 | const xmlChar *olddefine; |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 6491 | xmlRelaxNGGrammarPtr old; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6492 | |
| 6493 | if ((ctxt == NULL) || (node == NULL)) |
| 6494 | return (NULL); |
| 6495 | |
| 6496 | schema = xmlRelaxNGNewRelaxNG(ctxt); |
| 6497 | if (schema == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6498 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6499 | |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6500 | olddefine = ctxt->define; |
| 6501 | ctxt->define = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6502 | if (IS_RELAXNG(node, "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6503 | schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6504 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6505 | xmlRelaxNGGrammarPtr tmp, ret; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6506 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6507 | schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt); |
| 6508 | if (schema->topgrammar == NULL) { |
| 6509 | return (schema); |
| 6510 | } |
| 6511 | /* |
| 6512 | * Link the new grammar in the tree |
| 6513 | */ |
| 6514 | ret->parent = ctxt->grammar; |
| 6515 | if (ctxt->grammar != NULL) { |
| 6516 | tmp = ctxt->grammar->children; |
| 6517 | if (tmp == NULL) { |
| 6518 | ctxt->grammar->children = ret; |
| 6519 | } else { |
| 6520 | while (tmp->next != NULL) |
| 6521 | tmp = tmp->next; |
| 6522 | tmp->next = ret; |
| 6523 | } |
| 6524 | } |
| 6525 | old = ctxt->grammar; |
| 6526 | ctxt->grammar = ret; |
| 6527 | xmlRelaxNGParseStart(ctxt, node); |
| 6528 | if (old != NULL) |
| 6529 | ctxt->grammar = old; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6530 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6531 | ctxt->define = olddefine; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 6532 | if (schema->topgrammar->start != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6533 | xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0); |
| 6534 | if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) { |
| 6535 | xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL); |
| 6536 | while ((schema->topgrammar->start != NULL) && |
| 6537 | (schema->topgrammar->start->type == XML_RELAXNG_NOOP) && |
| 6538 | (schema->topgrammar->start->next != NULL)) |
| 6539 | schema->topgrammar->start = |
| 6540 | schema->topgrammar->start->content; |
| 6541 | xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start, |
| 6542 | XML_RELAXNG_IN_START, XML_RELAXNG_NOOP); |
| 6543 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 6544 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6545 | #ifdef DEBUG |
| 6546 | if (schema == NULL) |
| 6547 | xmlGenericError(xmlGenericErrorContext, |
| 6548 | "xmlRelaxNGParseDocument() failed\n"); |
| 6549 | #endif |
| 6550 | |
| 6551 | return (schema); |
| 6552 | } |
| 6553 | |
| 6554 | /************************************************************************ |
| 6555 | * * |
| 6556 | * Reading RelaxNGs * |
| 6557 | * * |
| 6558 | ************************************************************************/ |
| 6559 | |
| 6560 | /** |
| 6561 | * xmlRelaxNGNewParserCtxt: |
| 6562 | * @URL: the location of the schema |
| 6563 | * |
| 6564 | * Create an XML RelaxNGs parse context for that file/resource expected |
| 6565 | * to contain an XML RelaxNGs file. |
| 6566 | * |
| 6567 | * Returns the parser context or NULL in case of error |
| 6568 | */ |
| 6569 | xmlRelaxNGParserCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6570 | xmlRelaxNGNewParserCtxt(const char *URL) |
| 6571 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6572 | xmlRelaxNGParserCtxtPtr ret; |
| 6573 | |
| 6574 | if (URL == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6575 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6576 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6577 | ret = |
| 6578 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6579 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6580 | xmlRngPErrMemory(NULL, "building parser\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6581 | return (NULL); |
| 6582 | } |
| 6583 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6584 | ret->URL = xmlStrdup((const xmlChar *) URL); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 6585 | ret->error = xmlGenericError; |
| 6586 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6587 | return (ret); |
| 6588 | } |
| 6589 | |
| 6590 | /** |
| 6591 | * xmlRelaxNGNewMemParserCtxt: |
| 6592 | * @buffer: a pointer to a char array containing the schemas |
| 6593 | * @size: the size of the array |
| 6594 | * |
| 6595 | * Create an XML RelaxNGs parse context for that memory buffer expected |
| 6596 | * to contain an XML RelaxNGs file. |
| 6597 | * |
| 6598 | * Returns the parser context or NULL in case of error |
| 6599 | */ |
| 6600 | xmlRelaxNGParserCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6601 | xmlRelaxNGNewMemParserCtxt(const char *buffer, int size) |
| 6602 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6603 | xmlRelaxNGParserCtxtPtr ret; |
| 6604 | |
| 6605 | if ((buffer == NULL) || (size <= 0)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6606 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6607 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6608 | ret = |
| 6609 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6610 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6611 | xmlRngPErrMemory(NULL, "building parser\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6612 | return (NULL); |
| 6613 | } |
| 6614 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6615 | ret->buffer = buffer; |
| 6616 | ret->size = size; |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 6617 | ret->error = xmlGenericError; |
| 6618 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6619 | return (ret); |
| 6620 | } |
| 6621 | |
| 6622 | /** |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6623 | * xmlRelaxNGNewDocParserCtxt: |
| 6624 | * @doc: a preparsed document tree |
| 6625 | * |
| 6626 | * Create an XML RelaxNGs parser context for that document. |
| 6627 | * Note: since the process of compiling a RelaxNG schemas modifies the |
| 6628 | * document, the @doc parameter is duplicated internally. |
| 6629 | * |
| 6630 | * Returns the parser context or NULL in case of error |
| 6631 | */ |
| 6632 | xmlRelaxNGParserCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6633 | xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc) |
| 6634 | { |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6635 | xmlRelaxNGParserCtxtPtr ret; |
| 6636 | xmlDocPtr copy; |
| 6637 | |
| 6638 | if (doc == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6639 | return (NULL); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6640 | copy = xmlCopyDoc(doc, 1); |
| 6641 | if (copy == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6642 | return (NULL); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6643 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6644 | ret = |
| 6645 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6646 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6647 | xmlRngPErrMemory(NULL, "building parser\n"); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6648 | return (NULL); |
| 6649 | } |
| 6650 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6651 | ret->document = copy; |
| 6652 | ret->userData = xmlGenericErrorContext; |
| 6653 | return (ret); |
| 6654 | } |
| 6655 | |
| 6656 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6657 | * xmlRelaxNGFreeParserCtxt: |
| 6658 | * @ctxt: the schema parser context |
| 6659 | * |
| 6660 | * Free the resources associated to the schema parser context |
| 6661 | */ |
| 6662 | void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6663 | xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt) |
| 6664 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6665 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6666 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6667 | if (ctxt->URL != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6668 | xmlFree(ctxt->URL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6669 | if (ctxt->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6670 | xmlRelaxNGFreeDocument(ctxt->doc); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 6671 | if (ctxt->interleaves != NULL) |
| 6672 | xmlHashFree(ctxt->interleaves, NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6673 | if (ctxt->documents != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6674 | xmlRelaxNGFreeDocumentList(ctxt->documents); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 6675 | if (ctxt->includes != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6676 | xmlRelaxNGFreeIncludeList(ctxt->includes); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6677 | if (ctxt->docTab != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6678 | xmlFree(ctxt->docTab); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 6679 | if (ctxt->incTab != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6680 | xmlFree(ctxt->incTab); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6681 | if (ctxt->defTab != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6682 | int i; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6683 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6684 | for (i = 0; i < ctxt->defNr; i++) |
| 6685 | xmlRelaxNGFreeDefine(ctxt->defTab[i]); |
| 6686 | xmlFree(ctxt->defTab); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6687 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6688 | xmlFree(ctxt); |
| 6689 | } |
| 6690 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6691 | /** |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6692 | * xmlRelaxNGNormExtSpace: |
| 6693 | * @value: a value |
| 6694 | * |
| 6695 | * Removes the leading and ending spaces of the value |
| 6696 | * The string is modified "in situ" |
| 6697 | */ |
| 6698 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6699 | xmlRelaxNGNormExtSpace(xmlChar * value) |
| 6700 | { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6701 | xmlChar *start = value; |
| 6702 | xmlChar *cur = value; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6703 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6704 | if (value == NULL) |
| 6705 | return; |
| 6706 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6707 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6708 | cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6709 | if (cur == start) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6710 | do { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6711 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6712 | cur++; |
| 6713 | if (*cur == 0) |
| 6714 | return; |
| 6715 | start = cur; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6716 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6717 | cur++; |
| 6718 | if (*cur == 0) { |
| 6719 | *start = 0; |
| 6720 | return; |
| 6721 | } |
| 6722 | } while (1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6723 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6724 | do { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6725 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6726 | *start++ = *cur++; |
| 6727 | if (*cur == 0) { |
| 6728 | *start = 0; |
| 6729 | return; |
| 6730 | } |
| 6731 | /* don't try to normalize the inner spaces */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6732 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6733 | cur++; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6734 | if (*cur == 0) { |
| 6735 | *start = 0; |
| 6736 | return; |
| 6737 | } |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 6738 | *start++ = *cur++; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6739 | } while (1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6740 | } |
| 6741 | } |
| 6742 | |
| 6743 | /** |
| 6744 | * xmlRelaxNGCheckAttributes: |
| 6745 | * @ctxt: a Relax-NG parser context |
| 6746 | * @node: a Relax-NG node |
| 6747 | * |
| 6748 | * Check all the attributes on the given node |
| 6749 | */ |
| 6750 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6751 | xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 6752 | { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6753 | xmlAttrPtr cur, next; |
| 6754 | |
| 6755 | cur = node->properties; |
| 6756 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6757 | next = cur->next; |
| 6758 | if ((cur->ns == NULL) || |
| 6759 | (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
| 6760 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
| 6761 | if ((!xmlStrEqual(node->name, BAD_CAST "element")) && |
| 6762 | (!xmlStrEqual(node->name, BAD_CAST "attribute")) && |
| 6763 | (!xmlStrEqual(node->name, BAD_CAST "ref")) && |
| 6764 | (!xmlStrEqual(node->name, BAD_CAST "parentRef")) && |
| 6765 | (!xmlStrEqual(node->name, BAD_CAST "param")) && |
| 6766 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
| 6767 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6768 | "Attribute %s is not allowed on %s\n", |
| 6769 | cur->name, node->name); |
| 6770 | } |
| 6771 | } else if (xmlStrEqual(cur->name, BAD_CAST "type")) { |
| 6772 | if ((!xmlStrEqual(node->name, BAD_CAST "value")) && |
| 6773 | (!xmlStrEqual(node->name, BAD_CAST "data"))) { |
| 6774 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6775 | "Attribute %s is not allowed on %s\n", |
| 6776 | cur->name, node->name); |
| 6777 | } |
| 6778 | } else if (xmlStrEqual(cur->name, BAD_CAST "href")) { |
| 6779 | if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) && |
| 6780 | (!xmlStrEqual(node->name, BAD_CAST "include"))) { |
| 6781 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6782 | "Attribute %s is not allowed on %s\n", |
| 6783 | cur->name, node->name); |
| 6784 | } |
| 6785 | } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) { |
| 6786 | if ((!xmlStrEqual(node->name, BAD_CAST "start")) && |
| 6787 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
| 6788 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6789 | "Attribute %s is not allowed on %s\n", |
| 6790 | cur->name, node->name); |
| 6791 | } |
| 6792 | } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) { |
| 6793 | xmlChar *val; |
| 6794 | xmlURIPtr uri; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6795 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6796 | val = xmlNodeListGetString(node->doc, cur->children, 1); |
| 6797 | if (val != NULL) { |
| 6798 | if (val[0] != 0) { |
| 6799 | uri = xmlParseURI((const char *) val); |
| 6800 | if (uri == NULL) { |
| 6801 | xmlRngPErr(ctxt, node, XML_RNGP_INVALID_URI, |
| 6802 | "Attribute %s contains invalid URI %s\n", |
| 6803 | cur->name, val); |
| 6804 | } else { |
| 6805 | if (uri->scheme == NULL) { |
| 6806 | xmlRngPErr(ctxt, node, XML_RNGP_URI_NOT_ABSOLUTE, |
| 6807 | "Attribute %s URI %s is not absolute\n", |
| 6808 | cur->name, val); |
| 6809 | } |
| 6810 | if (uri->fragment != NULL) { |
| 6811 | xmlRngPErr(ctxt, node, XML_RNGP_URI_FRAGMENT, |
| 6812 | "Attribute %s URI %s has a fragment ID\n", |
| 6813 | cur->name, val); |
| 6814 | } |
| 6815 | xmlFreeURI(uri); |
| 6816 | } |
| 6817 | } |
| 6818 | xmlFree(val); |
| 6819 | } |
| 6820 | } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) { |
| 6821 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_ATTRIBUTE, |
| 6822 | "Unknown attribute %s on %s\n", cur->name, |
| 6823 | node->name); |
| 6824 | } |
| 6825 | } |
| 6826 | cur = next; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6827 | } |
| 6828 | } |
| 6829 | |
| 6830 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6831 | * xmlRelaxNGCleanupTree: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6832 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6833 | * @root: an xmlNodePtr subtree |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6834 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6835 | * Cleanup the subtree from unwanted nodes for parsing, resolve |
| 6836 | * Include and externalRef lookups. |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6837 | */ |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6838 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6839 | xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root) |
| 6840 | { |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6841 | xmlNodePtr cur, delete; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6842 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6843 | delete = NULL; |
| 6844 | cur = root; |
| 6845 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6846 | if (delete != NULL) { |
| 6847 | xmlUnlinkNode(delete); |
| 6848 | xmlFreeNode(delete); |
| 6849 | delete = NULL; |
| 6850 | } |
| 6851 | if (cur->type == XML_ELEMENT_NODE) { |
| 6852 | /* |
| 6853 | * Simplification 4.1. Annotations |
| 6854 | */ |
| 6855 | if ((cur->ns == NULL) || |
| 6856 | (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
| 6857 | if ((cur->parent != NULL) && |
| 6858 | (cur->parent->type == XML_ELEMENT_NODE) && |
| 6859 | ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) || |
| 6860 | (xmlStrEqual(cur->parent->name, BAD_CAST "value")) || |
| 6861 | (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) { |
| 6862 | xmlRngPErr(ctxt, cur, XML_RNGP_FOREIGN_ELEMENT, |
| 6863 | "element %s doesn't allow foreign elements\n", |
| 6864 | cur->parent->name, NULL); |
| 6865 | } |
| 6866 | delete = cur; |
| 6867 | goto skip_children; |
| 6868 | } else { |
| 6869 | xmlRelaxNGCleanupAttributes(ctxt, cur); |
| 6870 | if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) { |
| 6871 | xmlChar *href, *ns, *base, *URL; |
| 6872 | xmlRelaxNGDocumentPtr docu; |
| 6873 | xmlNodePtr tmp; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6874 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6875 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6876 | if (ns == NULL) { |
| 6877 | tmp = cur->parent; |
| 6878 | while ((tmp != NULL) && |
| 6879 | (tmp->type == XML_ELEMENT_NODE)) { |
| 6880 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 6881 | if (ns != NULL) |
| 6882 | break; |
| 6883 | tmp = tmp->parent; |
| 6884 | } |
| 6885 | } |
| 6886 | href = xmlGetProp(cur, BAD_CAST "href"); |
| 6887 | if (href == NULL) { |
| 6888 | xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF, |
| 6889 | "xmlRelaxNGParse: externalRef has no href attribute\n", |
| 6890 | NULL, NULL); |
| 6891 | delete = cur; |
| 6892 | goto skip_children; |
| 6893 | } |
| 6894 | base = xmlNodeGetBase(cur->doc, cur); |
| 6895 | URL = xmlBuildURI(href, base); |
| 6896 | if (URL == NULL) { |
| 6897 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
| 6898 | "Failed to compute URL for externalRef %s\n", |
| 6899 | href, NULL); |
| 6900 | if (href != NULL) |
| 6901 | xmlFree(href); |
| 6902 | if (base != NULL) |
| 6903 | xmlFree(base); |
| 6904 | delete = cur; |
| 6905 | goto skip_children; |
| 6906 | } |
| 6907 | if (href != NULL) |
| 6908 | xmlFree(href); |
| 6909 | if (base != NULL) |
| 6910 | xmlFree(base); |
| 6911 | docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns); |
| 6912 | if (docu == NULL) { |
| 6913 | xmlRngPErr(ctxt, cur, XML_RNGP_EXTERNAL_REF_FAILURE, |
| 6914 | "Failed to load externalRef %s\n", URL, |
| 6915 | NULL); |
| 6916 | xmlFree(URL); |
| 6917 | delete = cur; |
| 6918 | goto skip_children; |
| 6919 | } |
| 6920 | if (ns != NULL) |
| 6921 | xmlFree(ns); |
| 6922 | xmlFree(URL); |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 6923 | cur->psvi = docu; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6924 | } else if (xmlStrEqual(cur->name, BAD_CAST "include")) { |
| 6925 | xmlChar *href, *ns, *base, *URL; |
| 6926 | xmlRelaxNGIncludePtr incl; |
| 6927 | xmlNodePtr tmp; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6928 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6929 | href = xmlGetProp(cur, BAD_CAST "href"); |
| 6930 | if (href == NULL) { |
| 6931 | xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF, |
| 6932 | "xmlRelaxNGParse: include has no href attribute\n", |
| 6933 | NULL, NULL); |
| 6934 | delete = cur; |
| 6935 | goto skip_children; |
| 6936 | } |
| 6937 | base = xmlNodeGetBase(cur->doc, cur); |
| 6938 | URL = xmlBuildURI(href, base); |
| 6939 | if (URL == NULL) { |
| 6940 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
| 6941 | "Failed to compute URL for include %s\n", |
| 6942 | href, NULL); |
| 6943 | if (href != NULL) |
| 6944 | xmlFree(href); |
| 6945 | if (base != NULL) |
| 6946 | xmlFree(base); |
| 6947 | delete = cur; |
| 6948 | goto skip_children; |
| 6949 | } |
| 6950 | if (href != NULL) |
| 6951 | xmlFree(href); |
| 6952 | if (base != NULL) |
| 6953 | xmlFree(base); |
| 6954 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6955 | if (ns == NULL) { |
| 6956 | tmp = cur->parent; |
| 6957 | while ((tmp != NULL) && |
| 6958 | (tmp->type == XML_ELEMENT_NODE)) { |
| 6959 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 6960 | if (ns != NULL) |
| 6961 | break; |
| 6962 | tmp = tmp->parent; |
| 6963 | } |
| 6964 | } |
| 6965 | incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns); |
| 6966 | if (ns != NULL) |
| 6967 | xmlFree(ns); |
| 6968 | if (incl == NULL) { |
| 6969 | xmlRngPErr(ctxt, cur, XML_RNGP_INCLUDE_FAILURE, |
| 6970 | "Failed to load include %s\n", URL, |
| 6971 | NULL); |
| 6972 | xmlFree(URL); |
| 6973 | delete = cur; |
| 6974 | goto skip_children; |
| 6975 | } |
| 6976 | xmlFree(URL); |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 6977 | cur->psvi = incl; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6978 | } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) || |
| 6979 | (xmlStrEqual(cur->name, BAD_CAST "attribute"))) |
| 6980 | { |
| 6981 | xmlChar *name, *ns; |
| 6982 | xmlNodePtr text = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6983 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6984 | /* |
| 6985 | * Simplification 4.8. name attribute of element |
| 6986 | * and attribute elements |
| 6987 | */ |
| 6988 | name = xmlGetProp(cur, BAD_CAST "name"); |
| 6989 | if (name != NULL) { |
| 6990 | if (cur->children == NULL) { |
| 6991 | text = |
| 6992 | xmlNewChild(cur, cur->ns, BAD_CAST "name", |
| 6993 | name); |
| 6994 | } else { |
| 6995 | xmlNodePtr node; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6996 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6997 | node = xmlNewNode(cur->ns, BAD_CAST "name"); |
| 6998 | if (node != NULL) { |
| 6999 | xmlAddPrevSibling(cur->children, node); |
| 7000 | text = xmlNewText(name); |
| 7001 | xmlAddChild(node, text); |
| 7002 | text = node; |
| 7003 | } |
| 7004 | } |
| 7005 | if (text == NULL) { |
| 7006 | xmlRngPErr(ctxt, cur, XML_RNGP_CREATE_FAILURE, |
| 7007 | "Failed to create a name %s element\n", |
| 7008 | name, NULL); |
| 7009 | } |
| 7010 | xmlUnsetProp(cur, BAD_CAST "name"); |
| 7011 | xmlFree(name); |
| 7012 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 7013 | if (ns != NULL) { |
| 7014 | if (text != NULL) { |
| 7015 | xmlSetProp(text, BAD_CAST "ns", ns); |
| 7016 | /* xmlUnsetProp(cur, BAD_CAST "ns"); */ |
| 7017 | } |
| 7018 | xmlFree(ns); |
| 7019 | } else if (xmlStrEqual(cur->name, |
| 7020 | BAD_CAST "attribute")) { |
| 7021 | xmlSetProp(text, BAD_CAST "ns", BAD_CAST ""); |
| 7022 | } |
| 7023 | } |
| 7024 | } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) || |
| 7025 | (xmlStrEqual(cur->name, BAD_CAST "nsName")) || |
| 7026 | (xmlStrEqual(cur->name, BAD_CAST "value"))) { |
| 7027 | /* |
| 7028 | * Simplification 4.8. name attribute of element |
| 7029 | * and attribute elements |
| 7030 | */ |
| 7031 | if (xmlHasProp(cur, BAD_CAST "ns") == NULL) { |
| 7032 | xmlNodePtr node; |
| 7033 | xmlChar *ns = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7034 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7035 | node = cur->parent; |
| 7036 | while ((node != NULL) && |
| 7037 | (node->type == XML_ELEMENT_NODE)) { |
| 7038 | ns = xmlGetProp(node, BAD_CAST "ns"); |
| 7039 | if (ns != NULL) { |
| 7040 | break; |
| 7041 | } |
| 7042 | node = node->parent; |
| 7043 | } |
| 7044 | if (ns == NULL) { |
| 7045 | xmlSetProp(cur, BAD_CAST "ns", BAD_CAST ""); |
| 7046 | } else { |
| 7047 | xmlSetProp(cur, BAD_CAST "ns", ns); |
| 7048 | xmlFree(ns); |
| 7049 | } |
| 7050 | } |
| 7051 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
| 7052 | xmlChar *name, *local, *prefix; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7053 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7054 | /* |
| 7055 | * Simplification: 4.10. QNames |
| 7056 | */ |
| 7057 | name = xmlNodeGetContent(cur); |
| 7058 | if (name != NULL) { |
| 7059 | local = xmlSplitQName2(name, &prefix); |
| 7060 | if (local != NULL) { |
| 7061 | xmlNsPtr ns; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7062 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7063 | ns = xmlSearchNs(cur->doc, cur, prefix); |
| 7064 | if (ns == NULL) { |
| 7065 | xmlRngPErr(ctxt, cur, |
| 7066 | XML_RNGP_PREFIX_UNDEFINED, |
| 7067 | "xmlRelaxNGParse: no namespace for prefix %s\n", |
| 7068 | prefix, NULL); |
| 7069 | } else { |
| 7070 | xmlSetProp(cur, BAD_CAST "ns", |
| 7071 | ns->href); |
| 7072 | xmlNodeSetContent(cur, local); |
| 7073 | } |
| 7074 | xmlFree(local); |
| 7075 | xmlFree(prefix); |
| 7076 | } |
| 7077 | xmlFree(name); |
| 7078 | } |
| 7079 | } |
| 7080 | /* |
| 7081 | * 4.16 |
| 7082 | */ |
| 7083 | if (xmlStrEqual(cur->name, BAD_CAST "nsName")) { |
| 7084 | if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
| 7085 | xmlRngPErr(ctxt, cur, |
| 7086 | XML_RNGP_PAT_NSNAME_EXCEPT_NSNAME, |
| 7087 | "Found nsName/except//nsName forbidden construct\n", |
| 7088 | NULL, NULL); |
| 7089 | } |
| 7090 | } |
| 7091 | } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) && |
| 7092 | (cur != root)) { |
| 7093 | int oldflags = ctxt->flags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7094 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7095 | /* |
| 7096 | * 4.16 |
| 7097 | */ |
| 7098 | if ((cur->parent != NULL) && |
| 7099 | (xmlStrEqual |
| 7100 | (cur->parent->name, BAD_CAST "anyName"))) { |
| 7101 | ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT; |
| 7102 | xmlRelaxNGCleanupTree(ctxt, cur); |
| 7103 | ctxt->flags = oldflags; |
| 7104 | goto skip_children; |
| 7105 | } else if ((cur->parent != NULL) && |
| 7106 | (xmlStrEqual |
| 7107 | (cur->parent->name, BAD_CAST "nsName"))) { |
| 7108 | ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT; |
| 7109 | xmlRelaxNGCleanupTree(ctxt, cur); |
| 7110 | ctxt->flags = oldflags; |
| 7111 | goto skip_children; |
| 7112 | } |
| 7113 | } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) { |
| 7114 | /* |
| 7115 | * 4.16 |
| 7116 | */ |
| 7117 | if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) { |
| 7118 | xmlRngPErr(ctxt, cur, |
| 7119 | XML_RNGP_PAT_ANYNAME_EXCEPT_ANYNAME, |
| 7120 | "Found anyName/except//anyName forbidden construct\n", |
| 7121 | NULL, NULL); |
| 7122 | } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
| 7123 | xmlRngPErr(ctxt, cur, |
| 7124 | XML_RNGP_PAT_NSNAME_EXCEPT_ANYNAME, |
| 7125 | "Found nsName/except//anyName forbidden construct\n", |
| 7126 | NULL, NULL); |
| 7127 | } |
| 7128 | } |
| 7129 | /* |
| 7130 | * Thisd is not an else since "include" is transformed |
| 7131 | * into a div |
| 7132 | */ |
| 7133 | if (xmlStrEqual(cur->name, BAD_CAST "div")) { |
| 7134 | xmlChar *ns; |
| 7135 | xmlNodePtr child, ins, tmp; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7136 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7137 | /* |
| 7138 | * implements rule 4.11 |
| 7139 | */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7140 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7141 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 7142 | |
| 7143 | child = cur->children; |
| 7144 | ins = cur; |
| 7145 | while (child != NULL) { |
| 7146 | if (ns != NULL) { |
| 7147 | if (!xmlHasProp(child, BAD_CAST "ns")) { |
| 7148 | xmlSetProp(child, BAD_CAST "ns", ns); |
| 7149 | } |
| 7150 | } |
| 7151 | tmp = child->next; |
| 7152 | xmlUnlinkNode(child); |
| 7153 | ins = xmlAddNextSibling(ins, child); |
| 7154 | child = tmp; |
| 7155 | } |
| 7156 | if (ns != NULL) |
| 7157 | xmlFree(ns); |
| 7158 | delete = cur; |
| 7159 | goto skip_children; |
| 7160 | } |
| 7161 | } |
| 7162 | } |
| 7163 | /* |
| 7164 | * Simplification 4.2 whitespaces |
| 7165 | */ |
| 7166 | else if ((cur->type == XML_TEXT_NODE) || |
| 7167 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 7168 | if (IS_BLANK_NODE(cur)) { |
| 7169 | if (cur->parent->type == XML_ELEMENT_NODE) { |
| 7170 | if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value")) |
| 7171 | && |
| 7172 | (!xmlStrEqual |
| 7173 | (cur->parent->name, BAD_CAST "param"))) |
| 7174 | delete = cur; |
| 7175 | } else { |
| 7176 | delete = cur; |
| 7177 | goto skip_children; |
| 7178 | } |
| 7179 | } |
| 7180 | } else { |
| 7181 | delete = cur; |
| 7182 | goto skip_children; |
| 7183 | } |
| 7184 | |
| 7185 | /* |
| 7186 | * Skip to next node |
| 7187 | */ |
| 7188 | if (cur->children != NULL) { |
| 7189 | if ((cur->children->type != XML_ENTITY_DECL) && |
| 7190 | (cur->children->type != XML_ENTITY_REF_NODE) && |
| 7191 | (cur->children->type != XML_ENTITY_NODE)) { |
| 7192 | cur = cur->children; |
| 7193 | continue; |
| 7194 | } |
| 7195 | } |
| 7196 | skip_children: |
| 7197 | if (cur->next != NULL) { |
| 7198 | cur = cur->next; |
| 7199 | continue; |
| 7200 | } |
| 7201 | |
| 7202 | do { |
| 7203 | cur = cur->parent; |
| 7204 | if (cur == NULL) |
| 7205 | break; |
| 7206 | if (cur == root) { |
| 7207 | cur = NULL; |
| 7208 | break; |
| 7209 | } |
| 7210 | if (cur->next != NULL) { |
| 7211 | cur = cur->next; |
| 7212 | break; |
| 7213 | } |
| 7214 | } while (cur != NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7215 | } |
| 7216 | if (delete != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7217 | xmlUnlinkNode(delete); |
| 7218 | xmlFreeNode(delete); |
| 7219 | delete = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7220 | } |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7221 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7222 | |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7223 | /** |
| 7224 | * xmlRelaxNGCleanupDoc: |
| 7225 | * @ctxt: a Relax-NG parser context |
| 7226 | * @doc: an xmldocPtr document pointer |
| 7227 | * |
| 7228 | * Cleanup the document from unwanted nodes for parsing, resolve |
| 7229 | * Include and externalRef lookups. |
| 7230 | * |
| 7231 | * Returns the cleaned up document or NULL in case of error |
| 7232 | */ |
| 7233 | static xmlDocPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7234 | xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc) |
| 7235 | { |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7236 | xmlNodePtr root; |
| 7237 | |
| 7238 | /* |
| 7239 | * Extract the root |
| 7240 | */ |
| 7241 | root = xmlDocGetRootElement(doc); |
| 7242 | if (root == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7243 | xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n", |
| 7244 | ctxt->URL, NULL); |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7245 | return (NULL); |
| 7246 | } |
| 7247 | xmlRelaxNGCleanupTree(ctxt, root); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7248 | return (doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7249 | } |
| 7250 | |
| 7251 | /** |
| 7252 | * xmlRelaxNGParse: |
| 7253 | * @ctxt: a Relax-NG parser context |
| 7254 | * |
| 7255 | * parse a schema definition resource and build an internal |
| 7256 | * XML Shema struture which can be used to validate instances. |
| 7257 | * *WARNING* this interface is highly subject to change |
| 7258 | * |
| 7259 | * Returns the internal XML RelaxNG structure built from the resource or |
| 7260 | * NULL in case of error |
| 7261 | */ |
| 7262 | xmlRelaxNGPtr |
| 7263 | xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt) |
| 7264 | { |
| 7265 | xmlRelaxNGPtr ret = NULL; |
| 7266 | xmlDocPtr doc; |
| 7267 | xmlNodePtr root; |
| 7268 | |
| 7269 | xmlRelaxNGInitTypes(); |
| 7270 | |
| 7271 | if (ctxt == NULL) |
| 7272 | return (NULL); |
| 7273 | |
| 7274 | /* |
| 7275 | * First step is to parse the input document into an DOM/Infoset |
| 7276 | */ |
| 7277 | if (ctxt->URL != NULL) { |
Daniel Veillard | 87247e8 | 2004-01-13 20:42:02 +0000 | [diff] [blame] | 7278 | doc = xmlReadFile((const char *) ctxt->URL,NULL,0); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7279 | if (doc == NULL) { |
| 7280 | xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR, |
| 7281 | "xmlRelaxNGParse: could not load %s\n", ctxt->URL, |
| 7282 | NULL); |
| 7283 | return (NULL); |
| 7284 | } |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7285 | } else if (ctxt->buffer != NULL) { |
Daniel Veillard | 87247e8 | 2004-01-13 20:42:02 +0000 | [diff] [blame] | 7286 | doc = xmlReadMemory(ctxt->buffer, ctxt->size,NULL,NULL,0); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7287 | if (doc == NULL) { |
| 7288 | xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR, |
| 7289 | "xmlRelaxNGParse: could not parse schemas\n", NULL, |
| 7290 | NULL); |
| 7291 | return (NULL); |
| 7292 | } |
| 7293 | doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
| 7294 | ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 7295 | } else if (ctxt->document != NULL) { |
| 7296 | doc = ctxt->document; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7297 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7298 | xmlRngPErr(ctxt, NULL, XML_RNGP_EMPTY, |
| 7299 | "xmlRelaxNGParse: nothing to parse\n", NULL, NULL); |
| 7300 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7301 | } |
| 7302 | ctxt->document = doc; |
| 7303 | |
| 7304 | /* |
| 7305 | * Some preprocessing of the document content |
| 7306 | */ |
| 7307 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 7308 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7309 | xmlFreeDoc(ctxt->document); |
| 7310 | ctxt->document = NULL; |
| 7311 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7312 | } |
| 7313 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7314 | /* |
| 7315 | * Then do the parsing for good |
| 7316 | */ |
| 7317 | root = xmlDocGetRootElement(doc); |
| 7318 | if (root == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7319 | xmlRngPErr(ctxt, (xmlNodePtr) doc, |
| 7320 | XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n", |
| 7321 | ctxt->URL, NULL); |
| 7322 | xmlFreeDoc(doc); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7323 | return (NULL); |
| 7324 | } |
| 7325 | ret = xmlRelaxNGParseDocument(ctxt, root); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7326 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7327 | xmlFreeDoc(doc); |
| 7328 | return (NULL); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7329 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7330 | |
| 7331 | /* |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7332 | * Check the ref/defines links |
| 7333 | */ |
| 7334 | /* |
| 7335 | * try to preprocess interleaves |
| 7336 | */ |
| 7337 | if (ctxt->interleaves != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7338 | xmlHashScan(ctxt->interleaves, |
| 7339 | (xmlHashScanner) xmlRelaxNGComputeInterleaves, ctxt); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7340 | } |
| 7341 | |
| 7342 | /* |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7343 | * if there was a parsing error return NULL |
| 7344 | */ |
| 7345 | if (ctxt->nbErrors > 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7346 | xmlRelaxNGFree(ret); |
| 7347 | ctxt->document = NULL; |
| 7348 | xmlFreeDoc(doc); |
| 7349 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7350 | } |
| 7351 | |
| 7352 | /* |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 7353 | * try to compile (parts of) the schemas |
| 7354 | */ |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7355 | if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) { |
| 7356 | if (ret->topgrammar->start->type != XML_RELAXNG_START) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7357 | xmlRelaxNGDefinePtr def; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7358 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7359 | def = xmlRelaxNGNewDefine(ctxt, NULL); |
| 7360 | if (def != NULL) { |
| 7361 | def->type = XML_RELAXNG_START; |
| 7362 | def->content = ret->topgrammar->start; |
| 7363 | ret->topgrammar->start = def; |
| 7364 | } |
| 7365 | } |
| 7366 | xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7367 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 7368 | |
| 7369 | /* |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7370 | * Transfer the pointer for cleanup at the schema level. |
| 7371 | */ |
| 7372 | ret->doc = doc; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7373 | ctxt->document = NULL; |
| 7374 | ret->documents = ctxt->documents; |
| 7375 | ctxt->documents = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7376 | |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 7377 | ret->includes = ctxt->includes; |
| 7378 | ctxt->includes = NULL; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7379 | ret->defNr = ctxt->defNr; |
| 7380 | ret->defTab = ctxt->defTab; |
| 7381 | ctxt->defTab = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7382 | if (ctxt->idref == 1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7383 | ret->idref = 1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7384 | |
| 7385 | return (ret); |
| 7386 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7387 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7388 | /** |
| 7389 | * xmlRelaxNGSetParserErrors: |
| 7390 | * @ctxt: a Relax-NG validation context |
| 7391 | * @err: the error callback |
| 7392 | * @warn: the warning callback |
| 7393 | * @ctx: contextual data for the callbacks |
| 7394 | * |
| 7395 | * Set the callback functions used to handle errors for a validation context |
| 7396 | */ |
| 7397 | void |
| 7398 | xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7399 | xmlRelaxNGValidityErrorFunc err, |
| 7400 | xmlRelaxNGValidityWarningFunc warn, void *ctx) |
| 7401 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7402 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7403 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7404 | ctxt->error = err; |
| 7405 | ctxt->warning = warn; |
| 7406 | ctxt->userData = ctx; |
| 7407 | } |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 7408 | |
| 7409 | /** |
| 7410 | * xmlRelaxNGGetParserErrors: |
| 7411 | * @ctxt: a Relax-NG validation context |
| 7412 | * @err: the error callback result |
| 7413 | * @warn: the warning callback result |
| 7414 | * @ctx: contextual data for the callbacks result |
| 7415 | * |
| 7416 | * Get the callback information used to handle errors for a validation context |
| 7417 | * |
| 7418 | * Returns -1 in case of failure, 0 otherwise. |
| 7419 | */ |
| 7420 | int |
| 7421 | xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7422 | xmlRelaxNGValidityErrorFunc * err, |
| 7423 | xmlRelaxNGValidityWarningFunc * warn, void **ctx) |
| 7424 | { |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 7425 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7426 | return (-1); |
| 7427 | if (err != NULL) |
| 7428 | *err = ctxt->error; |
| 7429 | if (warn != NULL) |
| 7430 | *warn = ctxt->warning; |
| 7431 | if (ctx != NULL) |
| 7432 | *ctx = ctxt->userData; |
| 7433 | return (0); |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 7434 | } |
| 7435 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 7436 | #ifdef LIBXML_OUTPUT_ENABLED |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7437 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7438 | /************************************************************************ |
| 7439 | * * |
| 7440 | * Dump back a compiled form * |
| 7441 | * * |
| 7442 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7443 | static void xmlRelaxNGDumpDefine(FILE * output, |
| 7444 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7445 | |
| 7446 | /** |
| 7447 | * xmlRelaxNGDumpDefines: |
| 7448 | * @output: the file output |
| 7449 | * @defines: a list of define structures |
| 7450 | * |
| 7451 | * Dump a RelaxNG structure back |
| 7452 | */ |
| 7453 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7454 | xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines) |
| 7455 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7456 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7457 | xmlRelaxNGDumpDefine(output, defines); |
| 7458 | defines = defines->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7459 | } |
| 7460 | } |
| 7461 | |
| 7462 | /** |
| 7463 | * xmlRelaxNGDumpDefine: |
| 7464 | * @output: the file output |
| 7465 | * @define: a define structure |
| 7466 | * |
| 7467 | * Dump a RelaxNG structure back |
| 7468 | */ |
| 7469 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7470 | xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define) |
| 7471 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7472 | if (define == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7473 | return; |
| 7474 | switch (define->type) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7475 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7476 | fprintf(output, "<empty/>\n"); |
| 7477 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7478 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7479 | fprintf(output, "<notAllowed/>\n"); |
| 7480 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7481 | case XML_RELAXNG_TEXT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7482 | fprintf(output, "<text/>\n"); |
| 7483 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7484 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7485 | fprintf(output, "<element>\n"); |
| 7486 | if (define->name != NULL) { |
| 7487 | fprintf(output, "<name"); |
| 7488 | if (define->ns != NULL) |
| 7489 | fprintf(output, " ns=\"%s\"", define->ns); |
| 7490 | fprintf(output, ">%s</name>\n", define->name); |
| 7491 | } |
| 7492 | xmlRelaxNGDumpDefines(output, define->attrs); |
| 7493 | xmlRelaxNGDumpDefines(output, define->content); |
| 7494 | fprintf(output, "</element>\n"); |
| 7495 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7496 | case XML_RELAXNG_LIST: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7497 | fprintf(output, "<list>\n"); |
| 7498 | xmlRelaxNGDumpDefines(output, define->content); |
| 7499 | fprintf(output, "</list>\n"); |
| 7500 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7501 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7502 | fprintf(output, "<oneOrMore>\n"); |
| 7503 | xmlRelaxNGDumpDefines(output, define->content); |
| 7504 | fprintf(output, "</oneOrMore>\n"); |
| 7505 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7506 | case XML_RELAXNG_ZEROORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7507 | fprintf(output, "<zeroOrMore>\n"); |
| 7508 | xmlRelaxNGDumpDefines(output, define->content); |
| 7509 | fprintf(output, "</zeroOrMore>\n"); |
| 7510 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7511 | case XML_RELAXNG_CHOICE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7512 | fprintf(output, "<choice>\n"); |
| 7513 | xmlRelaxNGDumpDefines(output, define->content); |
| 7514 | fprintf(output, "</choice>\n"); |
| 7515 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7516 | case XML_RELAXNG_GROUP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7517 | fprintf(output, "<group>\n"); |
| 7518 | xmlRelaxNGDumpDefines(output, define->content); |
| 7519 | fprintf(output, "</group>\n"); |
| 7520 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7521 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7522 | fprintf(output, "<interleave>\n"); |
| 7523 | xmlRelaxNGDumpDefines(output, define->content); |
| 7524 | fprintf(output, "</interleave>\n"); |
| 7525 | break; |
| 7526 | case XML_RELAXNG_OPTIONAL: |
| 7527 | fprintf(output, "<optional>\n"); |
| 7528 | xmlRelaxNGDumpDefines(output, define->content); |
| 7529 | fprintf(output, "</optional>\n"); |
| 7530 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7531 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7532 | fprintf(output, "<attribute>\n"); |
| 7533 | xmlRelaxNGDumpDefines(output, define->content); |
| 7534 | fprintf(output, "</attribute>\n"); |
| 7535 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7536 | case XML_RELAXNG_DEF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7537 | fprintf(output, "<define"); |
| 7538 | if (define->name != NULL) |
| 7539 | fprintf(output, " name=\"%s\"", define->name); |
| 7540 | fprintf(output, ">\n"); |
| 7541 | xmlRelaxNGDumpDefines(output, define->content); |
| 7542 | fprintf(output, "</define>\n"); |
| 7543 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7544 | case XML_RELAXNG_REF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7545 | fprintf(output, "<ref"); |
| 7546 | if (define->name != NULL) |
| 7547 | fprintf(output, " name=\"%s\"", define->name); |
| 7548 | fprintf(output, ">\n"); |
| 7549 | xmlRelaxNGDumpDefines(output, define->content); |
| 7550 | fprintf(output, "</ref>\n"); |
| 7551 | break; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7552 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7553 | fprintf(output, "<parentRef"); |
| 7554 | if (define->name != NULL) |
| 7555 | fprintf(output, " name=\"%s\"", define->name); |
| 7556 | fprintf(output, ">\n"); |
| 7557 | xmlRelaxNGDumpDefines(output, define->content); |
| 7558 | fprintf(output, "</parentRef>\n"); |
| 7559 | break; |
| 7560 | case XML_RELAXNG_EXTERNALREF: |
| 7561 | fprintf(output, "<externalRef>"); |
| 7562 | xmlRelaxNGDumpDefines(output, define->content); |
| 7563 | fprintf(output, "</externalRef>\n"); |
| 7564 | break; |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 7565 | case XML_RELAXNG_DATATYPE: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7566 | case XML_RELAXNG_VALUE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7567 | TODO break; |
| 7568 | case XML_RELAXNG_START: |
| 7569 | case XML_RELAXNG_EXCEPT: |
| 7570 | case XML_RELAXNG_PARAM: |
| 7571 | TODO break; |
| 7572 | case XML_RELAXNG_NOOP: |
| 7573 | xmlRelaxNGDumpDefines(output, define->content); |
| 7574 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7575 | } |
| 7576 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7577 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7578 | /** |
| 7579 | * xmlRelaxNGDumpGrammar: |
| 7580 | * @output: the file output |
| 7581 | * @grammar: a grammar structure |
| 7582 | * @top: is this a top grammar |
| 7583 | * |
| 7584 | * Dump a RelaxNG structure back |
| 7585 | */ |
| 7586 | static void |
| 7587 | xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top) |
| 7588 | { |
| 7589 | if (grammar == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7590 | return; |
| 7591 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7592 | fprintf(output, "<grammar"); |
| 7593 | if (top) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7594 | fprintf(output, " xmlns=\"http://relaxng.org/ns/structure/1.0\""); |
| 7595 | switch (grammar->combine) { |
| 7596 | case XML_RELAXNG_COMBINE_UNDEFINED: |
| 7597 | break; |
| 7598 | case XML_RELAXNG_COMBINE_CHOICE: |
| 7599 | fprintf(output, " combine=\"choice\""); |
| 7600 | break; |
| 7601 | case XML_RELAXNG_COMBINE_INTERLEAVE: |
| 7602 | fprintf(output, " combine=\"interleave\""); |
| 7603 | break; |
| 7604 | default: |
| 7605 | fprintf(output, " <!-- invalid combine value -->"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7606 | } |
| 7607 | fprintf(output, ">\n"); |
| 7608 | if (grammar->start == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7609 | fprintf(output, " <!-- grammar had no start -->"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7610 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7611 | fprintf(output, "<start>\n"); |
| 7612 | xmlRelaxNGDumpDefine(output, grammar->start); |
| 7613 | fprintf(output, "</start>\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7614 | } |
| 7615 | /* TODO ? Dump the defines ? */ |
| 7616 | fprintf(output, "</grammar>\n"); |
| 7617 | } |
| 7618 | |
| 7619 | /** |
| 7620 | * xmlRelaxNGDump: |
| 7621 | * @output: the file output |
| 7622 | * @schema: a schema structure |
| 7623 | * |
| 7624 | * Dump a RelaxNG structure back |
| 7625 | */ |
| 7626 | void |
| 7627 | xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema) |
| 7628 | { |
| 7629 | if (schema == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7630 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
| 7631 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7632 | } |
| 7633 | fprintf(output, "RelaxNG: "); |
| 7634 | if (schema->doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7635 | fprintf(output, "no document\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7636 | } else if (schema->doc->URL != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7637 | fprintf(output, "%s\n", schema->doc->URL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7638 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7639 | fprintf(output, "\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7640 | } |
| 7641 | if (schema->topgrammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7642 | fprintf(output, "RelaxNG has no top grammar\n"); |
| 7643 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7644 | } |
| 7645 | xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1); |
| 7646 | } |
| 7647 | |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7648 | /** |
| 7649 | * xmlRelaxNGDumpTree: |
| 7650 | * @output: the file output |
| 7651 | * @schema: a schema structure |
| 7652 | * |
| 7653 | * Dump the transformed RelaxNG tree. |
| 7654 | */ |
| 7655 | void |
| 7656 | xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema) |
| 7657 | { |
| 7658 | if (schema == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7659 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
| 7660 | return; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7661 | } |
| 7662 | if (schema->doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7663 | fprintf(output, "no document\n"); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7664 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7665 | xmlDocDump(output, schema->doc); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7666 | } |
| 7667 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 7668 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7669 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7670 | /************************************************************************ |
| 7671 | * * |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7672 | * Validation of compiled content * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7673 | * * |
| 7674 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7675 | static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
| 7676 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7677 | |
| 7678 | /** |
| 7679 | * xmlRelaxNGValidateCompiledCallback: |
| 7680 | * @exec: the regular expression instance |
| 7681 | * @token: the token which matched |
| 7682 | * @transdata: callback data, the define for the subelement if available |
| 7683 | @ @inputdata: callback data, the Relax NG validation context |
| 7684 | * |
| 7685 | * Handle the callback and if needed validate the element children. |
| 7686 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7687 | static void |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7688 | xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7689 | const xmlChar * token, |
| 7690 | void *transdata, void *inputdata) |
| 7691 | { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7692 | xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata; |
| 7693 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata; |
| 7694 | int ret; |
| 7695 | |
| 7696 | #ifdef DEBUG_COMPILE |
| 7697 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7698 | "Compiled callback for: '%s'\n", token); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7699 | #endif |
| 7700 | if (ctxt == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7701 | fprintf(stderr, "callback on %s missing context\n", token); |
| 7702 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7703 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7704 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7705 | } |
| 7706 | if (define == NULL) { |
| 7707 | if (token[0] == '#') |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7708 | return; |
| 7709 | fprintf(stderr, "callback on %s missing define\n", token); |
| 7710 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7711 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7712 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7713 | } |
| 7714 | if ((ctxt == NULL) || (define == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7715 | fprintf(stderr, "callback on %s missing info\n", token); |
| 7716 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7717 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7718 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7719 | } else if (define->type != XML_RELAXNG_ELEMENT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7720 | fprintf(stderr, "callback on %s define is not element\n", token); |
| 7721 | if (ctxt->errNo == XML_RELAXNG_OK) |
| 7722 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7723 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7724 | } |
| 7725 | ret = xmlRelaxNGValidateDefinition(ctxt, define); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7726 | if (ret != 0) |
| 7727 | ctxt->perr = ret; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7728 | } |
| 7729 | |
| 7730 | /** |
| 7731 | * xmlRelaxNGValidateCompiledContent: |
| 7732 | * @ctxt: the RelaxNG validation context |
| 7733 | * @regexp: the regular expression as compiled |
| 7734 | * @content: list of children to test against the regexp |
| 7735 | * |
| 7736 | * Validate the content model of an element or start using the regexp |
| 7737 | * |
| 7738 | * Returns 0 in case of success, -1 in case of error. |
| 7739 | */ |
| 7740 | static int |
| 7741 | xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7742 | xmlRegexpPtr regexp, xmlNodePtr content) |
| 7743 | { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7744 | xmlRegExecCtxtPtr exec; |
| 7745 | xmlNodePtr cur; |
Daniel Veillard | 6216360 | 2003-04-17 09:36:38 +0000 | [diff] [blame] | 7746 | int ret = 0; |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7747 | int oldperr = ctxt->perr; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7748 | |
| 7749 | if ((ctxt == NULL) || (regexp == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7750 | return (-1); |
| 7751 | exec = xmlRegNewExecCtxt(regexp, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7752 | xmlRelaxNGValidateCompiledCallback, ctxt); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7753 | ctxt->perr = 0; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7754 | cur = content; |
| 7755 | while (cur != NULL) { |
| 7756 | ctxt->state->seq = cur; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7757 | switch (cur->type) { |
| 7758 | case XML_TEXT_NODE: |
| 7759 | case XML_CDATA_SECTION_NODE: |
| 7760 | if (xmlIsBlankNode(cur)) |
| 7761 | break; |
| 7762 | ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt); |
| 7763 | if (ret < 0) { |
| 7764 | VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, |
| 7765 | cur->parent->name); |
| 7766 | } |
| 7767 | break; |
| 7768 | case XML_ELEMENT_NODE: |
| 7769 | if (cur->ns != NULL) { |
| 7770 | ret = xmlRegExecPushString2(exec, cur->name, |
| 7771 | cur->ns->href, ctxt); |
| 7772 | } else { |
| 7773 | ret = xmlRegExecPushString(exec, cur->name, ctxt); |
| 7774 | } |
| 7775 | if (ret < 0) { |
| 7776 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name); |
| 7777 | } |
| 7778 | break; |
| 7779 | default: |
| 7780 | break; |
| 7781 | } |
| 7782 | if (ret < 0) |
| 7783 | break; |
| 7784 | /* |
| 7785 | * Switch to next element |
| 7786 | */ |
| 7787 | cur = cur->next; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7788 | } |
| 7789 | ret = xmlRegExecPushString(exec, NULL, NULL); |
| 7790 | if (ret == 1) { |
| 7791 | ret = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7792 | ctxt->state->seq = NULL; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7793 | } else if (ret == 0) { |
| 7794 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7795 | * TODO: get some of the names needed to exit the current state of exec |
| 7796 | */ |
| 7797 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST ""); |
| 7798 | ret = -1; |
| 7799 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 7800 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7801 | } else { |
| 7802 | ret = -1; |
| 7803 | } |
| 7804 | xmlRegFreeExecCtxt(exec); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7805 | /* |
| 7806 | * There might be content model errors outside of the pure |
| 7807 | * regexp validation, e.g. for attribute values. |
| 7808 | */ |
| 7809 | if ((ret == 0) && (ctxt->perr != 0)) { |
| 7810 | ret = ctxt->perr; |
| 7811 | } |
| 7812 | ctxt->perr = oldperr; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7813 | return (ret); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7814 | } |
| 7815 | |
| 7816 | /************************************************************************ |
| 7817 | * * |
| 7818 | * Progressive validation of when possible * |
| 7819 | * * |
| 7820 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7821 | static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
| 7822 | xmlRelaxNGDefinePtr defines); |
| 7823 | static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 7824 | int dolog); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 7825 | static void xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7826 | |
| 7827 | /** |
| 7828 | * xmlRelaxNGElemPush: |
| 7829 | * @ctxt: the validation context |
| 7830 | * @exec: the regexp runtime for the new content model |
| 7831 | * |
| 7832 | * Push a new regexp for the current node content model on the stack |
| 7833 | * |
| 7834 | * Returns 0 in case of success and -1 in case of error. |
| 7835 | */ |
| 7836 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7837 | xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec) |
| 7838 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7839 | if (ctxt->elemTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7840 | ctxt->elemMax = 10; |
| 7841 | ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax * |
| 7842 | sizeof |
| 7843 | (xmlRegExecCtxtPtr)); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7844 | if (ctxt->elemTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7845 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 7846 | return (-1); |
| 7847 | } |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7848 | } |
| 7849 | if (ctxt->elemNr >= ctxt->elemMax) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7850 | ctxt->elemMax *= 2; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7851 | ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7852 | ctxt->elemMax * |
| 7853 | sizeof |
| 7854 | (xmlRegExecCtxtPtr)); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7855 | if (ctxt->elemTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7856 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 7857 | return (-1); |
| 7858 | } |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7859 | } |
| 7860 | ctxt->elemTab[ctxt->elemNr++] = exec; |
| 7861 | ctxt->elem = exec; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7862 | return (0); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7863 | } |
| 7864 | |
| 7865 | /** |
| 7866 | * xmlRelaxNGElemPop: |
| 7867 | * @ctxt: the validation context |
| 7868 | * |
| 7869 | * Pop the regexp of the current node content model from the stack |
| 7870 | * |
| 7871 | * Returns the exec or NULL if empty |
| 7872 | */ |
| 7873 | static xmlRegExecCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7874 | xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt) |
| 7875 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7876 | xmlRegExecCtxtPtr ret; |
| 7877 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7878 | if (ctxt->elemNr <= 0) |
| 7879 | return (NULL); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7880 | ctxt->elemNr--; |
| 7881 | ret = ctxt->elemTab[ctxt->elemNr]; |
| 7882 | ctxt->elemTab[ctxt->elemNr] = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7883 | if (ctxt->elemNr > 0) |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7884 | ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1]; |
| 7885 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7886 | ctxt->elem = NULL; |
| 7887 | return (ret); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7888 | } |
| 7889 | |
| 7890 | /** |
| 7891 | * xmlRelaxNGValidateProgressiveCallback: |
| 7892 | * @exec: the regular expression instance |
| 7893 | * @token: the token which matched |
| 7894 | * @transdata: callback data, the define for the subelement if available |
| 7895 | @ @inputdata: callback data, the Relax NG validation context |
| 7896 | * |
| 7897 | * Handle the callback and if needed validate the element children. |
| 7898 | * some of the in/out informations are passed via the context in @inputdata. |
| 7899 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7900 | static void |
| 7901 | xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec |
| 7902 | ATTRIBUTE_UNUSED, |
| 7903 | const xmlChar * token, |
| 7904 | void *transdata, void *inputdata) |
| 7905 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7906 | xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata; |
| 7907 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7908 | xmlRelaxNGValidStatePtr state, oldstate; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7909 | xmlNodePtr node = ctxt->pnode; |
Daniel Veillard | c3ca5ba | 2003-05-09 22:26:28 +0000 | [diff] [blame] | 7910 | int ret = 0, oldflags; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7911 | |
| 7912 | #ifdef DEBUG_PROGRESSIVE |
| 7913 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7914 | "Progressive callback for: '%s'\n", token); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7915 | #endif |
| 7916 | if (ctxt == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7917 | fprintf(stderr, "callback on %s missing context\n", token); |
| 7918 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7919 | } |
| 7920 | ctxt->pstate = 1; |
| 7921 | if (define == NULL) { |
| 7922 | if (token[0] == '#') |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7923 | return; |
| 7924 | fprintf(stderr, "callback on %s missing define\n", token); |
| 7925 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7926 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7927 | ctxt->pstate = -1; |
| 7928 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7929 | } |
| 7930 | if ((ctxt == NULL) || (define == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7931 | fprintf(stderr, "callback on %s missing info\n", token); |
| 7932 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7933 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7934 | ctxt->pstate = -1; |
| 7935 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7936 | } else if (define->type != XML_RELAXNG_ELEMENT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7937 | fprintf(stderr, "callback on %s define is not element\n", token); |
| 7938 | if (ctxt->errNo == XML_RELAXNG_OK) |
| 7939 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7940 | ctxt->pstate = -1; |
| 7941 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7942 | } |
| 7943 | if (node->type != XML_ELEMENT_NODE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7944 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
| 7945 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 7946 | xmlRelaxNGDumpValidError(ctxt); |
| 7947 | ctxt->pstate = -1; |
| 7948 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7949 | } |
| 7950 | if (define->contModel == NULL) { |
| 7951 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7952 | * this node cannot be validated in a streamable fashion |
| 7953 | */ |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7954 | #ifdef DEBUG_PROGRESSIVE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7955 | xmlGenericError(xmlGenericErrorContext, |
| 7956 | "Element '%s' validation is not streamable\n", |
| 7957 | token); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7958 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7959 | ctxt->pstate = 0; |
| 7960 | ctxt->pdef = define; |
| 7961 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7962 | } |
| 7963 | exec = xmlRegNewExecCtxt(define->contModel, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7964 | xmlRelaxNGValidateProgressiveCallback, ctxt); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7965 | if (exec == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7966 | ctxt->pstate = -1; |
| 7967 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7968 | } |
| 7969 | xmlRelaxNGElemPush(ctxt, exec); |
| 7970 | |
| 7971 | /* |
| 7972 | * Validate the attributes part of the content. |
| 7973 | */ |
| 7974 | state = xmlRelaxNGNewValidState(ctxt, node); |
| 7975 | if (state == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7976 | ctxt->pstate = -1; |
| 7977 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7978 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7979 | oldstate = ctxt->state; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7980 | ctxt->state = state; |
| 7981 | if (define->attrs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7982 | ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
| 7983 | if (ret != 0) { |
| 7984 | ctxt->pstate = -1; |
| 7985 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
| 7986 | } |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7987 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7988 | if (ctxt->state != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7989 | ctxt->state->seq = NULL; |
| 7990 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
| 7991 | if (ret != 0) { |
| 7992 | ctxt->pstate = -1; |
| 7993 | } |
| 7994 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7995 | } else if (ctxt->states != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7996 | int tmp = -1, i; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7997 | |
| 7998 | oldflags = ctxt->flags; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7999 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8000 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 8001 | state = ctxt->states->tabState[i]; |
| 8002 | ctxt->state = state; |
| 8003 | ctxt->state->seq = NULL; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8004 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8005 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
| 8006 | tmp = 0; |
| 8007 | break; |
| 8008 | } |
| 8009 | } |
| 8010 | if (tmp != 0) { |
| 8011 | /* |
| 8012 | * validation error, log the message for the "best" one |
| 8013 | */ |
| 8014 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8015 | xmlRelaxNGLogBestError(ctxt); |
| 8016 | } |
| 8017 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 8018 | xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[i]); |
| 8019 | } |
| 8020 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 8021 | ctxt->states = NULL; |
| 8022 | if ((ret == 0) && (tmp == -1)) |
| 8023 | ctxt->pstate = -1; |
| 8024 | ctxt->flags = oldflags; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8025 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8026 | if (ctxt->pstate == -1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8027 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 8028 | xmlRelaxNGDumpValidError(ctxt); |
| 8029 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8030 | } |
| 8031 | ctxt->state = oldstate; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8032 | } |
| 8033 | |
| 8034 | /** |
| 8035 | * xmlRelaxNGValidatePushElement: |
| 8036 | * @ctxt: the validation context |
| 8037 | * @doc: a document instance |
| 8038 | * @elem: an element instance |
| 8039 | * |
| 8040 | * Push a new element start on the RelaxNG validation stack. |
| 8041 | * |
| 8042 | * returns 1 if no validation problem was found or 0 if validating the |
| 8043 | * element requires a full node, and -1 in case of error. |
| 8044 | */ |
| 8045 | int |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8046 | xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8047 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8048 | xmlNodePtr elem) |
| 8049 | { |
| 8050 | int ret = 1; |
| 8051 | |
| 8052 | if ((ctxt == NULL) || (elem == NULL)) |
| 8053 | return (-1); |
| 8054 | |
| 8055 | #ifdef DEBUG_PROGRESSIVE |
| 8056 | xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name); |
| 8057 | #endif |
| 8058 | if (ctxt->elem == 0) { |
| 8059 | xmlRelaxNGPtr schema; |
| 8060 | xmlRelaxNGGrammarPtr grammar; |
| 8061 | xmlRegExecCtxtPtr exec; |
| 8062 | xmlRelaxNGDefinePtr define; |
| 8063 | |
| 8064 | schema = ctxt->schema; |
| 8065 | if (schema == NULL) { |
| 8066 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 8067 | return (-1); |
| 8068 | } |
| 8069 | grammar = schema->topgrammar; |
| 8070 | if ((grammar == NULL) || (grammar->start == NULL)) { |
| 8071 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 8072 | return (-1); |
| 8073 | } |
| 8074 | define = grammar->start; |
| 8075 | if (define->contModel == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8076 | ctxt->pdef = define; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8077 | return (0); |
| 8078 | } |
| 8079 | exec = xmlRegNewExecCtxt(define->contModel, |
| 8080 | xmlRelaxNGValidateProgressiveCallback, |
| 8081 | ctxt); |
| 8082 | if (exec == NULL) { |
| 8083 | return (-1); |
| 8084 | } |
| 8085 | xmlRelaxNGElemPush(ctxt, exec); |
| 8086 | } |
| 8087 | ctxt->pnode = elem; |
| 8088 | ctxt->pstate = 0; |
| 8089 | if (elem->ns != NULL) { |
| 8090 | ret = |
| 8091 | xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href, |
| 8092 | ctxt); |
| 8093 | } else { |
| 8094 | ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt); |
| 8095 | } |
| 8096 | if (ret < 0) { |
| 8097 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name); |
| 8098 | } else { |
| 8099 | if (ctxt->pstate == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8100 | ret = 0; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8101 | else if (ctxt->pstate < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8102 | ret = -1; |
| 8103 | else |
| 8104 | ret = 1; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8105 | } |
| 8106 | #ifdef DEBUG_PROGRESSIVE |
| 8107 | if (ret < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8108 | xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n", |
| 8109 | elem->name); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8110 | #endif |
| 8111 | return (ret); |
| 8112 | } |
| 8113 | |
| 8114 | /** |
| 8115 | * xmlRelaxNGValidatePushCData: |
| 8116 | * @ctxt: the RelaxNG validation context |
| 8117 | * @data: some character data read |
| 8118 | * @len: the lenght of the data |
| 8119 | * |
| 8120 | * check the CData parsed for validation in the current stack |
| 8121 | * |
| 8122 | * returns 1 if no validation problem was found or -1 otherwise |
| 8123 | */ |
| 8124 | int |
| 8125 | xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8126 | const xmlChar * data, int len ATTRIBUTE_UNUSED) |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8127 | { |
| 8128 | int ret = 1; |
| 8129 | |
| 8130 | if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL)) |
| 8131 | return (-1); |
| 8132 | |
| 8133 | #ifdef DEBUG_PROGRESSIVE |
| 8134 | xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len); |
| 8135 | #endif |
| 8136 | |
| 8137 | while (*data != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8138 | if (!IS_BLANK_CH(*data)) |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8139 | break; |
| 8140 | data++; |
| 8141 | } |
| 8142 | if (*data == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8143 | return (1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8144 | |
| 8145 | ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt); |
| 8146 | if (ret < 0) { |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8147 | VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO "); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8148 | #ifdef DEBUG_PROGRESSIVE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8149 | xmlGenericError(xmlGenericErrorContext, "CDATA failed\n"); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8150 | #endif |
| 8151 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8152 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8153 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8154 | return (1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8155 | } |
| 8156 | |
| 8157 | /** |
| 8158 | * xmlRelaxNGValidatePopElement: |
| 8159 | * @ctxt: the RelaxNG validation context |
| 8160 | * @doc: a document instance |
| 8161 | * @elem: an element instance |
| 8162 | * |
| 8163 | * Pop the element end from the RelaxNG validation stack. |
| 8164 | * |
| 8165 | * returns 1 if no validation problem was found or 0 otherwise |
| 8166 | */ |
| 8167 | int |
| 8168 | xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8169 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8170 | xmlNodePtr elem) |
| 8171 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8172 | int ret; |
| 8173 | xmlRegExecCtxtPtr exec; |
| 8174 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8175 | if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL)) |
| 8176 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8177 | #ifdef DEBUG_PROGRESSIVE |
| 8178 | xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name); |
| 8179 | #endif |
| 8180 | /* |
| 8181 | * verify that we reached a terminal state of the content model. |
| 8182 | */ |
| 8183 | exec = xmlRelaxNGElemPop(ctxt); |
| 8184 | ret = xmlRegExecPushString(exec, NULL, NULL); |
| 8185 | if (ret == 0) { |
| 8186 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8187 | * TODO: get some of the names needed to exit the current state of exec |
| 8188 | */ |
| 8189 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST ""); |
| 8190 | ret = -1; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8191 | } else if (ret < 0) { |
| 8192 | ret = -1; |
| 8193 | } else { |
| 8194 | ret = 1; |
| 8195 | } |
| 8196 | xmlRegFreeExecCtxt(exec); |
| 8197 | #ifdef DEBUG_PROGRESSIVE |
| 8198 | if (ret < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8199 | xmlGenericError(xmlGenericErrorContext, "PopElem %s failed\n", |
| 8200 | elem->name); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8201 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8202 | return (ret); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8203 | } |
| 8204 | |
| 8205 | /** |
| 8206 | * xmlRelaxNGValidateFullElement: |
| 8207 | * @ctxt: the validation context |
| 8208 | * @doc: a document instance |
| 8209 | * @elem: an element instance |
| 8210 | * |
| 8211 | * Validate a full subtree when xmlRelaxNGValidatePushElement() returned |
| 8212 | * 0 and the content of the node has been expanded. |
| 8213 | * |
| 8214 | * returns 1 if no validation problem was found or -1 in case of error. |
| 8215 | */ |
| 8216 | int |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8217 | xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8218 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8219 | xmlNodePtr elem) |
| 8220 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8221 | int ret; |
| 8222 | xmlRelaxNGValidStatePtr state; |
| 8223 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8224 | if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL)) |
| 8225 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8226 | #ifdef DEBUG_PROGRESSIVE |
| 8227 | xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name); |
| 8228 | #endif |
| 8229 | state = xmlRelaxNGNewValidState(ctxt, elem->parent); |
| 8230 | if (state == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8231 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8232 | } |
| 8233 | state->seq = elem; |
| 8234 | ctxt->state = state; |
| 8235 | ctxt->errNo = XML_RELAXNG_OK; |
| 8236 | ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8237 | if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK)) |
| 8238 | ret = -1; |
| 8239 | else |
| 8240 | ret = 1; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8241 | xmlRelaxNGFreeValidState(ctxt, state); |
| 8242 | ctxt->state = NULL; |
| 8243 | #ifdef DEBUG_PROGRESSIVE |
| 8244 | if (ret < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8245 | xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n", |
| 8246 | elem->name); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8247 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8248 | return (ret); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8249 | } |
| 8250 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 8251 | /************************************************************************ |
| 8252 | * * |
| 8253 | * Generic interpreted validation implementation * |
| 8254 | * * |
| 8255 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8256 | static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
| 8257 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8258 | |
| 8259 | /** |
| 8260 | * xmlRelaxNGSkipIgnored: |
| 8261 | * @ctxt: a schema validation context |
| 8262 | * @node: the top node. |
| 8263 | * |
| 8264 | * Skip ignorable nodes in that context |
| 8265 | * |
| 8266 | * Returns the new sibling or NULL in case of error. |
| 8267 | */ |
| 8268 | static xmlNodePtr |
| 8269 | xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8270 | xmlNodePtr node) |
| 8271 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8272 | /* |
| 8273 | * TODO complete and handle entities |
| 8274 | */ |
| 8275 | while ((node != NULL) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8276 | ((node->type == XML_COMMENT_NODE) || |
| 8277 | (node->type == XML_PI_NODE) || |
William M. Brack | 7217c86 | 2004-03-15 02:43:56 +0000 | [diff] [blame] | 8278 | (node->type == XML_XINCLUDE_START) || |
| 8279 | (node->type == XML_XINCLUDE_END) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8280 | (((node->type == XML_TEXT_NODE) || |
| 8281 | (node->type == XML_CDATA_SECTION_NODE)) && |
| 8282 | ((ctxt->flags & FLAGS_MIXED_CONTENT) || |
| 8283 | (IS_BLANK_NODE(node)))))) { |
| 8284 | node = node->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8285 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8286 | return (node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8287 | } |
| 8288 | |
| 8289 | /** |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8290 | * xmlRelaxNGNormalize: |
| 8291 | * @ctxt: a schema validation context |
| 8292 | * @str: the string to normalize |
| 8293 | * |
| 8294 | * Implements the normalizeWhiteSpace( s ) function from |
| 8295 | * section 6.2.9 of the spec |
| 8296 | * |
| 8297 | * Returns the new string or NULL in case of error. |
| 8298 | */ |
| 8299 | static xmlChar * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8300 | xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar * str) |
| 8301 | { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8302 | xmlChar *ret, *p; |
| 8303 | const xmlChar *tmp; |
| 8304 | int len; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8305 | |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8306 | if (str == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8307 | return (NULL); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8308 | tmp = str; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8309 | while (*tmp != 0) |
| 8310 | tmp++; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8311 | len = tmp - str; |
| 8312 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 8313 | ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar)); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8314 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8315 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 8316 | return (NULL); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8317 | } |
| 8318 | p = ret; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8319 | while (IS_BLANK_CH(*str)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8320 | str++; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8321 | while (*str != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8322 | if (IS_BLANK_CH(*str)) { |
| 8323 | while (IS_BLANK_CH(*str)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8324 | str++; |
| 8325 | if (*str == 0) |
| 8326 | break; |
| 8327 | *p++ = ' '; |
| 8328 | } else |
| 8329 | *p++ = *str++; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8330 | } |
| 8331 | *p = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8332 | return (ret); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8333 | } |
| 8334 | |
| 8335 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8336 | * xmlRelaxNGValidateDatatype: |
| 8337 | * @ctxt: a Relax-NG validation context |
| 8338 | * @value: the string value |
| 8339 | * @type: the datatype definition |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8340 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8341 | * |
| 8342 | * Validate the given value against the dataype |
| 8343 | * |
| 8344 | * Returns 0 if the validation succeeded or an error code. |
| 8345 | */ |
| 8346 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8347 | xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt, |
| 8348 | const xmlChar * value, |
| 8349 | xmlRelaxNGDefinePtr define, xmlNodePtr node) |
| 8350 | { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8351 | int ret, tmp; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8352 | xmlRelaxNGTypeLibraryPtr lib; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8353 | void *result = NULL; |
| 8354 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8355 | |
| 8356 | if ((define == NULL) || (define->data == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8357 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8358 | } |
| 8359 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8360 | if (lib->check != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8361 | if ((define->attrs != NULL) && |
| 8362 | (define->attrs->type == XML_RELAXNG_PARAM)) { |
| 8363 | ret = |
| 8364 | lib->check(lib->data, define->name, value, &result, node); |
| 8365 | } else { |
| 8366 | ret = lib->check(lib->data, define->name, value, NULL, node); |
| 8367 | } |
| 8368 | } else |
| 8369 | ret = -1; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8370 | if (ret < 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8371 | VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name); |
| 8372 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
| 8373 | lib->freef(lib->data, result); |
| 8374 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8375 | } else if (ret == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8376 | ret = 0; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8377 | } else if (ret == 2) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8378 | VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8379 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8380 | VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value); |
| 8381 | ret = -1; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8382 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8383 | cur = define->attrs; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8384 | while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8385 | if (lib->facet != NULL) { |
| 8386 | tmp = lib->facet(lib->data, define->name, cur->name, |
| 8387 | cur->value, value, result); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8388 | if (tmp != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8389 | ret = -1; |
| 8390 | } |
| 8391 | cur = cur->next; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8392 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8393 | if ((ret == 0) && (define->content != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8394 | const xmlChar *oldvalue, *oldendvalue; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8395 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8396 | oldvalue = ctxt->state->value; |
| 8397 | oldendvalue = ctxt->state->endvalue; |
| 8398 | ctxt->state->value = (xmlChar *) value; |
| 8399 | ctxt->state->endvalue = NULL; |
| 8400 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 8401 | ctxt->state->value = (xmlChar *) oldvalue; |
| 8402 | ctxt->state->endvalue = (xmlChar *) oldendvalue; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8403 | } |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8404 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8405 | lib->freef(lib->data, result); |
| 8406 | return (ret); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8407 | } |
| 8408 | |
| 8409 | /** |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8410 | * xmlRelaxNGNextValue: |
| 8411 | * @ctxt: a Relax-NG validation context |
| 8412 | * |
| 8413 | * Skip to the next value when validating within a list |
| 8414 | * |
| 8415 | * Returns 0 if the operation succeeded or an error code. |
| 8416 | */ |
| 8417 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8418 | xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt) |
| 8419 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8420 | xmlChar *cur; |
| 8421 | |
| 8422 | cur = ctxt->state->value; |
| 8423 | if ((cur == NULL) || (ctxt->state->endvalue == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8424 | ctxt->state->value = NULL; |
| 8425 | ctxt->state->endvalue = NULL; |
| 8426 | return (0); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8427 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8428 | while (*cur != 0) |
| 8429 | cur++; |
| 8430 | while ((cur != ctxt->state->endvalue) && (*cur == 0)) |
| 8431 | cur++; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8432 | if (cur == ctxt->state->endvalue) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8433 | ctxt->state->value = NULL; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8434 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8435 | ctxt->state->value = cur; |
| 8436 | return (0); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8437 | } |
| 8438 | |
| 8439 | /** |
| 8440 | * xmlRelaxNGValidateValueList: |
| 8441 | * @ctxt: a Relax-NG validation context |
| 8442 | * @defines: the list of definitions to verify |
| 8443 | * |
| 8444 | * Validate the given set of definitions for the current value |
| 8445 | * |
| 8446 | * Returns 0 if the validation succeeded or an error code. |
| 8447 | */ |
| 8448 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8449 | xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt, |
| 8450 | xmlRelaxNGDefinePtr defines) |
| 8451 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8452 | int ret = 0; |
| 8453 | |
| 8454 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8455 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
| 8456 | if (ret != 0) |
| 8457 | break; |
| 8458 | defines = defines->next; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8459 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8460 | return (ret); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8461 | } |
| 8462 | |
| 8463 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8464 | * xmlRelaxNGValidateValue: |
| 8465 | * @ctxt: a Relax-NG validation context |
| 8466 | * @define: the definition to verify |
| 8467 | * |
| 8468 | * Validate the given definition for the current value |
| 8469 | * |
| 8470 | * Returns 0 if the validation succeeded or an error code. |
| 8471 | */ |
| 8472 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8473 | xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
| 8474 | xmlRelaxNGDefinePtr define) |
| 8475 | { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8476 | int ret = 0, oldflags; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8477 | xmlChar *value; |
| 8478 | |
| 8479 | value = ctxt->state->value; |
| 8480 | switch (define->type) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8481 | case XML_RELAXNG_EMPTY:{ |
| 8482 | if ((value != NULL) && (value[0] != 0)) { |
| 8483 | int idx = 0; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 8484 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8485 | while (IS_BLANK_CH(value[idx])) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8486 | idx++; |
| 8487 | if (value[idx] != 0) |
| 8488 | ret = -1; |
| 8489 | } |
| 8490 | break; |
| 8491 | } |
| 8492 | case XML_RELAXNG_TEXT: |
| 8493 | break; |
| 8494 | case XML_RELAXNG_VALUE:{ |
| 8495 | if (!xmlStrEqual(value, define->value)) { |
| 8496 | if (define->name != NULL) { |
| 8497 | xmlRelaxNGTypeLibraryPtr lib; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8498 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8499 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
| 8500 | if ((lib != NULL) && (lib->comp != NULL)) { |
| 8501 | ret = lib->comp(lib->data, define->name, |
| 8502 | define->value, define->node, |
| 8503 | (void *) define->attrs, |
| 8504 | value, ctxt->state->node); |
| 8505 | } else |
| 8506 | ret = -1; |
| 8507 | if (ret < 0) { |
| 8508 | VALID_ERR2(XML_RELAXNG_ERR_TYPECMP, |
| 8509 | define->name); |
| 8510 | return (-1); |
| 8511 | } else if (ret == 1) { |
| 8512 | ret = 0; |
| 8513 | } else { |
| 8514 | ret = -1; |
| 8515 | } |
| 8516 | } else { |
| 8517 | xmlChar *nval, *nvalue; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8518 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8519 | /* |
| 8520 | * TODO: trivial optimizations are possible by |
| 8521 | * computing at compile-time |
| 8522 | */ |
| 8523 | nval = xmlRelaxNGNormalize(ctxt, define->value); |
| 8524 | nvalue = xmlRelaxNGNormalize(ctxt, value); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8525 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8526 | if ((nval == NULL) || (nvalue == NULL) || |
| 8527 | (!xmlStrEqual(nval, nvalue))) |
| 8528 | ret = -1; |
| 8529 | if (nval != NULL) |
| 8530 | xmlFree(nval); |
| 8531 | if (nvalue != NULL) |
| 8532 | xmlFree(nvalue); |
| 8533 | } |
| 8534 | } |
| 8535 | if (ret == 0) |
| 8536 | xmlRelaxNGNextValue(ctxt); |
| 8537 | break; |
| 8538 | } |
| 8539 | case XML_RELAXNG_DATATYPE:{ |
| 8540 | ret = xmlRelaxNGValidateDatatype(ctxt, value, define, |
| 8541 | ctxt->state->seq); |
| 8542 | if (ret == 0) |
| 8543 | xmlRelaxNGNextValue(ctxt); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8544 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8545 | break; |
| 8546 | } |
| 8547 | case XML_RELAXNG_CHOICE:{ |
| 8548 | xmlRelaxNGDefinePtr list = define->content; |
| 8549 | xmlChar *oldvalue; |
| 8550 | |
| 8551 | oldflags = ctxt->flags; |
| 8552 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8553 | |
| 8554 | oldvalue = ctxt->state->value; |
| 8555 | while (list != NULL) { |
| 8556 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8557 | if (ret == 0) { |
| 8558 | break; |
| 8559 | } |
| 8560 | ctxt->state->value = oldvalue; |
| 8561 | list = list->next; |
| 8562 | } |
| 8563 | ctxt->flags = oldflags; |
| 8564 | if (ret != 0) { |
| 8565 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8566 | xmlRelaxNGDumpValidError(ctxt); |
| 8567 | } else { |
| 8568 | if (ctxt->errNr > 0) |
| 8569 | xmlRelaxNGPopErrors(ctxt, 0); |
| 8570 | } |
| 8571 | if (ret == 0) |
| 8572 | xmlRelaxNGNextValue(ctxt); |
| 8573 | break; |
| 8574 | } |
| 8575 | case XML_RELAXNG_LIST:{ |
| 8576 | xmlRelaxNGDefinePtr list = define->content; |
| 8577 | xmlChar *oldvalue, *oldend, *val, *cur; |
| 8578 | |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8579 | #ifdef DEBUG_LIST |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8580 | int nb_values = 0; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8581 | #endif |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8582 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8583 | oldvalue = ctxt->state->value; |
| 8584 | oldend = ctxt->state->endvalue; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8585 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8586 | val = xmlStrdup(oldvalue); |
| 8587 | if (val == NULL) { |
| 8588 | val = xmlStrdup(BAD_CAST ""); |
| 8589 | } |
| 8590 | if (val == NULL) { |
| 8591 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 8592 | return (-1); |
| 8593 | } |
| 8594 | cur = val; |
| 8595 | while (*cur != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8596 | if (IS_BLANK_CH(*cur)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8597 | *cur = 0; |
| 8598 | cur++; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8599 | #ifdef DEBUG_LIST |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8600 | nb_values++; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8601 | #endif |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8602 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8603 | *cur++ = 0; |
| 8604 | } else |
| 8605 | cur++; |
| 8606 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8607 | #ifdef DEBUG_LIST |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8608 | xmlGenericError(xmlGenericErrorContext, |
| 8609 | "list value: '%s' found %d items\n", |
| 8610 | oldvalue, nb_values); |
| 8611 | nb_values = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8612 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8613 | ctxt->state->endvalue = cur; |
| 8614 | cur = val; |
| 8615 | while ((*cur == 0) && (cur != ctxt->state->endvalue)) |
| 8616 | cur++; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8617 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8618 | ctxt->state->value = cur; |
| 8619 | |
| 8620 | while (list != NULL) { |
| 8621 | if (ctxt->state->value == ctxt->state->endvalue) |
| 8622 | ctxt->state->value = NULL; |
| 8623 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8624 | if (ret != 0) { |
| 8625 | #ifdef DEBUG_LIST |
| 8626 | xmlGenericError(xmlGenericErrorContext, |
| 8627 | "Failed to validate value: '%s' with %d rule\n", |
| 8628 | ctxt->state->value, nb_values); |
| 8629 | #endif |
| 8630 | break; |
| 8631 | } |
| 8632 | #ifdef DEBUG_LIST |
| 8633 | nb_values++; |
| 8634 | #endif |
| 8635 | list = list->next; |
| 8636 | } |
| 8637 | |
| 8638 | if ((ret == 0) && (ctxt->state->value != NULL) && |
| 8639 | (ctxt->state->value != ctxt->state->endvalue)) { |
| 8640 | VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA, |
| 8641 | ctxt->state->value); |
| 8642 | ret = -1; |
| 8643 | } |
| 8644 | xmlFree(val); |
| 8645 | ctxt->state->value = oldvalue; |
| 8646 | ctxt->state->endvalue = oldend; |
| 8647 | break; |
| 8648 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8649 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8650 | ret = xmlRelaxNGValidateValueList(ctxt, define->content); |
| 8651 | if (ret != 0) { |
| 8652 | break; |
| 8653 | } |
| 8654 | /* no break on purpose */ |
| 8655 | case XML_RELAXNG_ZEROORMORE:{ |
| 8656 | xmlChar *cur, *temp; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8657 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8658 | oldflags = ctxt->flags; |
| 8659 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8660 | cur = ctxt->state->value; |
| 8661 | temp = NULL; |
| 8662 | while ((cur != NULL) && (cur != ctxt->state->endvalue) && |
| 8663 | (temp != cur)) { |
| 8664 | temp = cur; |
| 8665 | ret = |
| 8666 | xmlRelaxNGValidateValueList(ctxt, define->content); |
| 8667 | if (ret != 0) { |
| 8668 | ctxt->state->value = temp; |
| 8669 | ret = 0; |
| 8670 | break; |
| 8671 | } |
| 8672 | cur = ctxt->state->value; |
| 8673 | } |
| 8674 | ctxt->flags = oldflags; |
| 8675 | if (ret != 0) { |
| 8676 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8677 | xmlRelaxNGDumpValidError(ctxt); |
| 8678 | } else { |
| 8679 | if (ctxt->errNr > 0) |
| 8680 | xmlRelaxNGPopErrors(ctxt, 0); |
| 8681 | } |
| 8682 | break; |
| 8683 | } |
| 8684 | case XML_RELAXNG_EXCEPT:{ |
| 8685 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8686 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8687 | list = define->content; |
| 8688 | while (list != NULL) { |
| 8689 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8690 | if (ret == 0) { |
| 8691 | ret = -1; |
| 8692 | break; |
| 8693 | } else |
| 8694 | ret = 0; |
| 8695 | list = list->next; |
| 8696 | } |
| 8697 | break; |
| 8698 | } |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 8699 | case XML_RELAXNG_DEF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8700 | case XML_RELAXNG_GROUP:{ |
| 8701 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8702 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8703 | list = define->content; |
| 8704 | while (list != NULL) { |
| 8705 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8706 | if (ret != 0) { |
| 8707 | ret = -1; |
| 8708 | break; |
| 8709 | } else |
| 8710 | ret = 0; |
| 8711 | list = list->next; |
| 8712 | } |
| 8713 | break; |
| 8714 | } |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 8715 | case XML_RELAXNG_REF: |
| 8716 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8717 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 8718 | break; |
| 8719 | default: |
| 8720 | TODO ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8721 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8722 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8723 | } |
| 8724 | |
| 8725 | /** |
| 8726 | * xmlRelaxNGValidateValueContent: |
| 8727 | * @ctxt: a Relax-NG validation context |
| 8728 | * @defines: the list of definitions to verify |
| 8729 | * |
| 8730 | * Validate the given definitions for the current value |
| 8731 | * |
| 8732 | * Returns 0 if the validation succeeded or an error code. |
| 8733 | */ |
| 8734 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8735 | xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt, |
| 8736 | xmlRelaxNGDefinePtr defines) |
| 8737 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8738 | int ret = 0; |
| 8739 | |
| 8740 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8741 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
| 8742 | if (ret != 0) |
| 8743 | break; |
| 8744 | defines = defines->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8745 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8746 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8747 | } |
| 8748 | |
| 8749 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8750 | * xmlRelaxNGAttributeMatch: |
| 8751 | * @ctxt: a Relax-NG validation context |
| 8752 | * @define: the definition to check |
| 8753 | * @prop: the attribute |
| 8754 | * |
| 8755 | * Check if the attribute matches the definition nameClass |
| 8756 | * |
| 8757 | * Returns 1 if the attribute matches, 0 if no, or -1 in case of error |
| 8758 | */ |
| 8759 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8760 | xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 8761 | xmlRelaxNGDefinePtr define, xmlAttrPtr prop) |
| 8762 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8763 | int ret; |
| 8764 | |
| 8765 | if (define->name != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8766 | if (!xmlStrEqual(define->name, prop->name)) |
| 8767 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8768 | } |
| 8769 | if (define->ns != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8770 | if (define->ns[0] == 0) { |
| 8771 | if (prop->ns != NULL) |
| 8772 | return (0); |
| 8773 | } else { |
| 8774 | if ((prop->ns == NULL) || |
| 8775 | (!xmlStrEqual(define->ns, prop->ns->href))) |
| 8776 | return (0); |
| 8777 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8778 | } |
| 8779 | if (define->nameClass == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8780 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8781 | define = define->nameClass; |
| 8782 | if (define->type == XML_RELAXNG_EXCEPT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8783 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8784 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8785 | list = define->content; |
| 8786 | while (list != NULL) { |
| 8787 | ret = xmlRelaxNGAttributeMatch(ctxt, list, prop); |
| 8788 | if (ret == 1) |
| 8789 | return (0); |
| 8790 | if (ret < 0) |
| 8791 | return (ret); |
| 8792 | list = list->next; |
| 8793 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8794 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8795 | TODO} |
| 8796 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8797 | } |
| 8798 | |
| 8799 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8800 | * xmlRelaxNGValidateAttribute: |
| 8801 | * @ctxt: a Relax-NG validation context |
| 8802 | * @define: the definition to verify |
| 8803 | * |
| 8804 | * Validate the given attribute definition for that node |
| 8805 | * |
| 8806 | * Returns 0 if the validation succeeded or an error code. |
| 8807 | */ |
| 8808 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8809 | xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt, |
| 8810 | xmlRelaxNGDefinePtr define) |
| 8811 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8812 | int ret = 0, i; |
| 8813 | xmlChar *value, *oldvalue; |
| 8814 | xmlAttrPtr prop = NULL, tmp; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8815 | xmlNodePtr oldseq; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8816 | |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 8817 | if (ctxt->state->nbAttrLeft <= 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8818 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8819 | if (define->name != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8820 | for (i = 0; i < ctxt->state->nbAttrs; i++) { |
| 8821 | tmp = ctxt->state->attrs[i]; |
| 8822 | if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) { |
| 8823 | if ((((define->ns == NULL) || (define->ns[0] == 0)) && |
| 8824 | (tmp->ns == NULL)) || |
| 8825 | ((tmp->ns != NULL) && |
| 8826 | (xmlStrEqual(define->ns, tmp->ns->href)))) { |
| 8827 | prop = tmp; |
| 8828 | break; |
| 8829 | } |
| 8830 | } |
| 8831 | } |
| 8832 | if (prop != NULL) { |
| 8833 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 8834 | oldvalue = ctxt->state->value; |
| 8835 | oldseq = ctxt->state->seq; |
| 8836 | ctxt->state->seq = (xmlNodePtr) prop; |
| 8837 | ctxt->state->value = value; |
| 8838 | ctxt->state->endvalue = NULL; |
| 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 | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8856 | #ifdef DEBUG |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8857 | xmlGenericError(xmlGenericErrorContext, |
| 8858 | "xmlRelaxNGValidateAttribute(%s): %d\n", |
| 8859 | define->name, ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8860 | #endif |
| 8861 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8862 | for (i = 0; i < ctxt->state->nbAttrs; i++) { |
| 8863 | tmp = ctxt->state->attrs[i]; |
| 8864 | if ((tmp != NULL) && |
| 8865 | (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) { |
| 8866 | prop = tmp; |
| 8867 | break; |
| 8868 | } |
| 8869 | } |
| 8870 | if (prop != NULL) { |
| 8871 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 8872 | oldvalue = ctxt->state->value; |
| 8873 | oldseq = ctxt->state->seq; |
| 8874 | ctxt->state->seq = (xmlNodePtr) prop; |
| 8875 | ctxt->state->value = value; |
| 8876 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
| 8877 | if (ctxt->state->value != NULL) |
| 8878 | value = ctxt->state->value; |
| 8879 | if (value != NULL) |
| 8880 | xmlFree(value); |
| 8881 | ctxt->state->value = oldvalue; |
| 8882 | ctxt->state->seq = oldseq; |
| 8883 | if (ret == 0) { |
| 8884 | /* |
| 8885 | * flag the attribute as processed |
| 8886 | */ |
| 8887 | ctxt->state->attrs[i] = NULL; |
| 8888 | ctxt->state->nbAttrLeft--; |
| 8889 | } |
| 8890 | } else { |
| 8891 | ret = -1; |
| 8892 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8893 | #ifdef DEBUG |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8894 | if (define->ns != NULL) { |
| 8895 | xmlGenericError(xmlGenericErrorContext, |
| 8896 | "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n", |
| 8897 | define->ns, ret); |
| 8898 | } else { |
| 8899 | xmlGenericError(xmlGenericErrorContext, |
| 8900 | "xmlRelaxNGValidateAttribute(anyName): %d\n", |
| 8901 | ret); |
| 8902 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8903 | #endif |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8904 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8905 | |
| 8906 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8907 | } |
| 8908 | |
| 8909 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8910 | * xmlRelaxNGValidateAttributeList: |
| 8911 | * @ctxt: a Relax-NG validation context |
| 8912 | * @define: the list of definition to verify |
| 8913 | * |
| 8914 | * Validate the given node against the list of attribute definitions |
| 8915 | * |
| 8916 | * Returns 0 if the validation succeeded or an error code. |
| 8917 | */ |
| 8918 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8919 | xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
| 8920 | xmlRelaxNGDefinePtr defines) |
| 8921 | { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8922 | int ret = 0, res; |
| 8923 | int needmore = 0; |
| 8924 | xmlRelaxNGDefinePtr cur; |
| 8925 | |
| 8926 | cur = defines; |
| 8927 | while (cur != NULL) { |
| 8928 | if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8929 | if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0) |
| 8930 | ret = -1; |
| 8931 | } else |
| 8932 | needmore = 1; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8933 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8934 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8935 | if (!needmore) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8936 | return (ret); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8937 | cur = defines; |
| 8938 | while (cur != NULL) { |
| 8939 | if (cur->type != XML_RELAXNG_ATTRIBUTE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8940 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
| 8941 | res = xmlRelaxNGValidateDefinition(ctxt, cur); |
| 8942 | if (res < 0) |
| 8943 | ret = -1; |
| 8944 | } else { |
| 8945 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 8946 | return (-1); |
| 8947 | } |
| 8948 | if (res == -1) /* continues on -2 */ |
| 8949 | break; |
| 8950 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8951 | cur = cur->next; |
| 8952 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8953 | |
| 8954 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8955 | } |
| 8956 | |
| 8957 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8958 | * xmlRelaxNGNodeMatchesList: |
| 8959 | * @node: the node |
| 8960 | * @list: a NULL terminated array of definitions |
| 8961 | * |
| 8962 | * Check if a node can be matched by one of the definitions |
| 8963 | * |
| 8964 | * Returns 1 if matches 0 otherwise |
| 8965 | */ |
| 8966 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8967 | xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr * list) |
| 8968 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8969 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8970 | int i = 0, tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8971 | |
| 8972 | if ((node == NULL) || (list == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8973 | return (0); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8974 | |
| 8975 | cur = list[i++]; |
| 8976 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8977 | if ((node->type == XML_ELEMENT_NODE) && |
| 8978 | (cur->type == XML_RELAXNG_ELEMENT)) { |
| 8979 | tmp = xmlRelaxNGElementMatch(NULL, cur, node); |
| 8980 | if (tmp == 1) |
| 8981 | return (1); |
| 8982 | } else if (((node->type == XML_TEXT_NODE) || |
| 8983 | (node->type == XML_CDATA_SECTION_NODE)) && |
| 8984 | (cur->type == XML_RELAXNG_TEXT)) { |
| 8985 | return (1); |
| 8986 | } |
| 8987 | cur = list[i++]; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8988 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8989 | return (0); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8990 | } |
| 8991 | |
| 8992 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8993 | * xmlRelaxNGValidateInterleave: |
| 8994 | * @ctxt: a Relax-NG validation context |
| 8995 | * @define: the definition to verify |
| 8996 | * |
| 8997 | * Validate an interleave definition for a node. |
| 8998 | * |
| 8999 | * Returns 0 if the validation succeeded or an error code. |
| 9000 | */ |
| 9001 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9002 | xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt, |
| 9003 | xmlRelaxNGDefinePtr define) |
| 9004 | { |
William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 9005 | int ret = 0, i, nbgroups; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9006 | int errNr = ctxt->errNr; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9007 | int oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9008 | |
| 9009 | xmlRelaxNGValidStatePtr oldstate; |
| 9010 | xmlRelaxNGPartitionPtr partitions; |
| 9011 | xmlRelaxNGInterleaveGroupPtr group = NULL; |
| 9012 | xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem; |
| 9013 | xmlNodePtr *list = NULL, *lasts = NULL; |
| 9014 | |
| 9015 | if (define->data != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9016 | partitions = (xmlRelaxNGPartitionPtr) define->data; |
| 9017 | nbgroups = partitions->nbgroups; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9018 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9019 | VALID_ERR(XML_RELAXNG_ERR_INTERNODATA); |
| 9020 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9021 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9022 | /* |
| 9023 | * Optimizations for MIXED |
| 9024 | */ |
| 9025 | oldflags = ctxt->flags; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 9026 | if (define->dflags & IS_MIXED) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9027 | ctxt->flags |= FLAGS_MIXED_CONTENT; |
| 9028 | if (nbgroups == 2) { |
| 9029 | /* |
| 9030 | * this is a pure <mixed> case |
| 9031 | */ |
| 9032 | if (ctxt->state != NULL) |
| 9033 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
| 9034 | ctxt->state->seq); |
| 9035 | if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT) |
| 9036 | ret = xmlRelaxNGValidateDefinition(ctxt, |
| 9037 | partitions->groups[1]-> |
| 9038 | rule); |
| 9039 | else |
| 9040 | ret = xmlRelaxNGValidateDefinition(ctxt, |
| 9041 | partitions->groups[0]-> |
| 9042 | rule); |
| 9043 | if (ret == 0) { |
| 9044 | if (ctxt->state != NULL) |
| 9045 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
| 9046 | ctxt->state-> |
| 9047 | seq); |
| 9048 | } |
| 9049 | ctxt->flags = oldflags; |
| 9050 | return (ret); |
| 9051 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9052 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9053 | |
| 9054 | /* |
| 9055 | * Build arrays to store the first and last node of the chain |
| 9056 | * pertaining to each group |
| 9057 | */ |
| 9058 | list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
| 9059 | if (list == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9060 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 9061 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9062 | } |
| 9063 | memset(list, 0, nbgroups * sizeof(xmlNodePtr)); |
| 9064 | lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
| 9065 | if (lasts == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9066 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 9067 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9068 | } |
| 9069 | memset(lasts, 0, nbgroups * sizeof(xmlNodePtr)); |
| 9070 | |
| 9071 | /* |
| 9072 | * Walk the sequence of children finding the right group and |
| 9073 | * sorting them in sequences. |
| 9074 | */ |
| 9075 | cur = ctxt->state->seq; |
| 9076 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 9077 | start = cur; |
| 9078 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9079 | ctxt->state->seq = cur; |
| 9080 | if ((partitions->triage != NULL) && |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9081 | (partitions->flags & IS_DETERMINIST)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9082 | void *tmp = NULL; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9083 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9084 | if ((cur->type == XML_TEXT_NODE) || |
| 9085 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 9086 | tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text", |
| 9087 | NULL); |
| 9088 | } else if (cur->type == XML_ELEMENT_NODE) { |
| 9089 | if (cur->ns != NULL) { |
| 9090 | tmp = xmlHashLookup2(partitions->triage, cur->name, |
| 9091 | cur->ns->href); |
| 9092 | if (tmp == NULL) |
| 9093 | tmp = xmlHashLookup2(partitions->triage, |
| 9094 | BAD_CAST "#any", |
| 9095 | cur->ns->href); |
| 9096 | } else |
| 9097 | tmp = |
| 9098 | xmlHashLookup2(partitions->triage, cur->name, |
| 9099 | NULL); |
| 9100 | if (tmp == NULL) |
| 9101 | tmp = |
| 9102 | xmlHashLookup2(partitions->triage, BAD_CAST "#any", |
| 9103 | NULL); |
| 9104 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9105 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9106 | if (tmp == NULL) { |
| 9107 | i = nbgroups; |
| 9108 | } else { |
| 9109 | i = ((long) tmp) - 1; |
| 9110 | if (partitions->flags & IS_NEEDCHECK) { |
| 9111 | group = partitions->groups[i]; |
| 9112 | if (!xmlRelaxNGNodeMatchesList(cur, group->defs)) |
| 9113 | i = nbgroups; |
| 9114 | } |
| 9115 | } |
| 9116 | } else { |
| 9117 | for (i = 0; i < nbgroups; i++) { |
| 9118 | group = partitions->groups[i]; |
| 9119 | if (group == NULL) |
| 9120 | continue; |
| 9121 | if (xmlRelaxNGNodeMatchesList(cur, group->defs)) |
| 9122 | break; |
| 9123 | } |
| 9124 | } |
| 9125 | /* |
| 9126 | * We break as soon as an element not matched is found |
| 9127 | */ |
| 9128 | if (i >= nbgroups) { |
| 9129 | break; |
| 9130 | } |
| 9131 | if (lasts[i] != NULL) { |
| 9132 | lasts[i]->next = cur; |
| 9133 | lasts[i] = cur; |
| 9134 | } else { |
| 9135 | list[i] = cur; |
| 9136 | lasts[i] = cur; |
| 9137 | } |
| 9138 | if (cur->next != NULL) |
| 9139 | lastchg = cur->next; |
| 9140 | else |
| 9141 | lastchg = cur; |
| 9142 | cur = xmlRelaxNGSkipIgnored(ctxt, cur->next); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9143 | } |
| 9144 | if (ret != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9145 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
| 9146 | ret = -1; |
| 9147 | goto done; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9148 | } |
| 9149 | lastelem = cur; |
| 9150 | oldstate = ctxt->state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9151 | for (i = 0; i < nbgroups; i++) { |
| 9152 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate); |
| 9153 | group = partitions->groups[i]; |
| 9154 | if (lasts[i] != NULL) { |
| 9155 | last = lasts[i]->next; |
| 9156 | lasts[i]->next = NULL; |
| 9157 | } |
| 9158 | ctxt->state->seq = list[i]; |
| 9159 | ret = xmlRelaxNGValidateDefinition(ctxt, group->rule); |
| 9160 | if (ret != 0) |
| 9161 | break; |
| 9162 | if (ctxt->state != NULL) { |
| 9163 | cur = ctxt->state->seq; |
| 9164 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 9165 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 9166 | oldstate = ctxt->state; |
| 9167 | ctxt->state = NULL; |
| 9168 | if (cur != NULL) { |
| 9169 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
| 9170 | ret = -1; |
| 9171 | ctxt->state = oldstate; |
| 9172 | goto done; |
| 9173 | } |
| 9174 | } else if (ctxt->states != NULL) { |
| 9175 | int j; |
| 9176 | int found = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9177 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9178 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 9179 | cur = ctxt->states->tabState[j]->seq; |
| 9180 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 9181 | if (cur == NULL) { |
| 9182 | found = 1; |
| 9183 | break; |
| 9184 | } |
| 9185 | } |
| 9186 | if (ctxt->states->nbState > 0) { |
| 9187 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 9188 | oldstate = |
| 9189 | ctxt->states->tabState[ctxt->states->nbState - 1]; |
| 9190 | } |
| 9191 | for (j = 0; j < ctxt->states->nbState - 1; j++) { |
| 9192 | xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[j]); |
| 9193 | } |
| 9194 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9195 | ctxt->states = NULL; |
| 9196 | if (found == 0) { |
| 9197 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
| 9198 | ret = -1; |
| 9199 | ctxt->state = oldstate; |
| 9200 | goto done; |
| 9201 | } |
| 9202 | } else { |
| 9203 | ret = -1; |
| 9204 | break; |
| 9205 | } |
| 9206 | if (lasts[i] != NULL) { |
| 9207 | lasts[i]->next = last; |
| 9208 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9209 | } |
| 9210 | if (ctxt->state != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9211 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9212 | ctxt->state = oldstate; |
| 9213 | ctxt->state->seq = lastelem; |
| 9214 | if (ret != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9215 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
| 9216 | ret = -1; |
| 9217 | goto done; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9218 | } |
| 9219 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9220 | done: |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9221 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9222 | /* |
| 9223 | * builds the next links chain from the prev one |
| 9224 | */ |
| 9225 | cur = lastchg; |
| 9226 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9227 | if ((cur == start) || (cur->prev == NULL)) |
| 9228 | break; |
| 9229 | cur->prev->next = cur; |
| 9230 | cur = cur->prev; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9231 | } |
| 9232 | if (ret == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9233 | if (ctxt->errNr > errNr) |
| 9234 | xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9235 | } |
| 9236 | |
| 9237 | xmlFree(list); |
| 9238 | xmlFree(lasts); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9239 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9240 | } |
| 9241 | |
| 9242 | /** |
| 9243 | * xmlRelaxNGValidateDefinitionList: |
| 9244 | * @ctxt: a Relax-NG validation context |
| 9245 | * @define: the list of definition to verify |
| 9246 | * |
| 9247 | * Validate the given node content against the (list) of definitions |
| 9248 | * |
| 9249 | * Returns 0 if the validation succeeded or an error code. |
| 9250 | */ |
| 9251 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9252 | xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt, |
| 9253 | xmlRelaxNGDefinePtr defines) |
| 9254 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9255 | int ret = 0, res; |
| 9256 | |
| 9257 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 9258 | if (defines == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9259 | VALID_ERR2(XML_RELAXNG_ERR_INTERNAL, |
| 9260 | BAD_CAST "NULL definition list"); |
| 9261 | return (-1); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 9262 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9263 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9264 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
| 9265 | res = xmlRelaxNGValidateDefinition(ctxt, defines); |
| 9266 | if (res < 0) |
| 9267 | ret = -1; |
| 9268 | } else { |
| 9269 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 9270 | return (-1); |
| 9271 | } |
| 9272 | if (res == -1) /* continues on -2 */ |
| 9273 | break; |
| 9274 | defines = defines->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9275 | } |
| 9276 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9277 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9278 | } |
| 9279 | |
| 9280 | /** |
| 9281 | * xmlRelaxNGElementMatch: |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9282 | * @ctxt: a Relax-NG validation context |
| 9283 | * @define: the definition to check |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9284 | * @elem: the element |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9285 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9286 | * Check if the element matches the definition nameClass |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9287 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9288 | * 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] | 9289 | */ |
| 9290 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9291 | xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 9292 | xmlRelaxNGDefinePtr define, xmlNodePtr elem) |
| 9293 | { |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 9294 | int ret = 0, oldflags = 0; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9295 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9296 | if (define->name != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9297 | if (!xmlStrEqual(elem->name, define->name)) { |
| 9298 | VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name); |
| 9299 | return (0); |
| 9300 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9301 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9302 | if ((define->ns != NULL) && (define->ns[0] != 0)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9303 | if (elem->ns == NULL) { |
| 9304 | VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, elem->name); |
| 9305 | return (0); |
| 9306 | } else if (!xmlStrEqual(elem->ns->href, define->ns)) { |
| 9307 | VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS, |
| 9308 | elem->name, define->ns); |
| 9309 | return (0); |
| 9310 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9311 | } else if ((elem->ns != NULL) && (define->ns != NULL) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9312 | (define->name == NULL)) { |
| 9313 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, elem->name); |
| 9314 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9315 | } else if ((elem->ns != NULL) && (define->name != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9316 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, define->name); |
| 9317 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9318 | } |
| 9319 | |
| 9320 | if (define->nameClass == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9321 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9322 | |
| 9323 | define = define->nameClass; |
| 9324 | if (define->type == XML_RELAXNG_EXCEPT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9325 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9326 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9327 | if (ctxt != NULL) { |
| 9328 | oldflags = ctxt->flags; |
| 9329 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9330 | } |
| 9331 | |
| 9332 | list = define->content; |
| 9333 | while (list != NULL) { |
| 9334 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
| 9335 | if (ret == 1) { |
| 9336 | if (ctxt != NULL) |
| 9337 | ctxt->flags = oldflags; |
| 9338 | return (0); |
| 9339 | } |
| 9340 | if (ret < 0) { |
| 9341 | if (ctxt != NULL) |
| 9342 | ctxt->flags = oldflags; |
| 9343 | return (ret); |
| 9344 | } |
| 9345 | list = list->next; |
| 9346 | } |
| 9347 | ret = 1; |
| 9348 | if (ctxt != NULL) { |
| 9349 | ctxt->flags = oldflags; |
| 9350 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9351 | } else if (define->type == XML_RELAXNG_CHOICE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9352 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9353 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9354 | if (ctxt != NULL) { |
| 9355 | oldflags = ctxt->flags; |
| 9356 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9357 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9358 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9359 | list = define->nameClass; |
| 9360 | while (list != NULL) { |
| 9361 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
| 9362 | if (ret == 1) { |
| 9363 | if (ctxt != NULL) |
| 9364 | ctxt->flags = oldflags; |
| 9365 | return (1); |
| 9366 | } |
| 9367 | if (ret < 0) { |
| 9368 | if (ctxt != NULL) |
| 9369 | ctxt->flags = oldflags; |
| 9370 | return (ret); |
| 9371 | } |
| 9372 | list = list->next; |
| 9373 | } |
| 9374 | if (ctxt != NULL) { |
| 9375 | if (ret != 0) { |
| 9376 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9377 | xmlRelaxNGDumpValidError(ctxt); |
| 9378 | } else { |
| 9379 | if (ctxt->errNr > 0) |
| 9380 | xmlRelaxNGPopErrors(ctxt, 0); |
| 9381 | } |
| 9382 | } |
| 9383 | ret = 0; |
| 9384 | if (ctxt != NULL) { |
| 9385 | ctxt->flags = oldflags; |
| 9386 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9387 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9388 | TODO ret = -1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9389 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9390 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9391 | } |
| 9392 | |
| 9393 | /** |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9394 | * xmlRelaxNGBestState: |
| 9395 | * @ctxt: a Relax-NG validation context |
| 9396 | * |
| 9397 | * Find the "best" state in the ctxt->states list of states to report |
| 9398 | * errors about. I.e. a state with no element left in the child list |
| 9399 | * or the one with the less attributes left. |
| 9400 | * This is called only if a falidation error was detected |
| 9401 | * |
| 9402 | * Returns the index of the "best" state or -1 in case of error |
| 9403 | */ |
| 9404 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9405 | xmlRelaxNGBestState(xmlRelaxNGValidCtxtPtr ctxt) |
| 9406 | { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9407 | xmlRelaxNGValidStatePtr state; |
| 9408 | int i, tmp; |
| 9409 | int best = -1; |
| 9410 | int value = 1000000; |
| 9411 | |
| 9412 | if ((ctxt == NULL) || (ctxt->states == NULL) || |
| 9413 | (ctxt->states->nbState <= 0)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9414 | return (-1); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9415 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9416 | for (i = 0; i < ctxt->states->nbState; i++) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9417 | state = ctxt->states->tabState[i]; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9418 | if (state == NULL) |
| 9419 | continue; |
| 9420 | if (state->seq != NULL) { |
| 9421 | if ((best == -1) || (value > 100000)) { |
| 9422 | value = 100000; |
| 9423 | best = i; |
| 9424 | } |
| 9425 | } else { |
| 9426 | tmp = state->nbAttrLeft; |
| 9427 | if ((best == -1) || (value > tmp)) { |
| 9428 | value = tmp; |
| 9429 | best = i; |
| 9430 | } |
| 9431 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9432 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9433 | return (best); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9434 | } |
| 9435 | |
| 9436 | /** |
| 9437 | * xmlRelaxNGLogBestError: |
| 9438 | * @ctxt: a Relax-NG validation context |
| 9439 | * |
| 9440 | * Find the "best" state in the ctxt->states list of states to report |
| 9441 | * errors about and log it. |
| 9442 | */ |
| 9443 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9444 | xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt) |
| 9445 | { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9446 | int best; |
| 9447 | |
| 9448 | if ((ctxt == NULL) || (ctxt->states == NULL) || |
| 9449 | (ctxt->states->nbState <= 0)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9450 | return; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9451 | |
| 9452 | best = xmlRelaxNGBestState(ctxt); |
| 9453 | if ((best >= 0) && (best < ctxt->states->nbState)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9454 | ctxt->state = ctxt->states->tabState[best]; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9455 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9456 | xmlRelaxNGValidateElementEnd(ctxt, 1); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9457 | } |
| 9458 | } |
| 9459 | |
| 9460 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9461 | * xmlRelaxNGValidateElementEnd: |
| 9462 | * @ctxt: a Relax-NG validation context |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 9463 | * @dolog: indicate that error logging should be done |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9464 | * |
| 9465 | * Validate the end of the element, implements check that |
| 9466 | * there is nothing left not consumed in the element content |
| 9467 | * or in the attribute list. |
| 9468 | * |
| 9469 | * Returns 0 if the validation succeeded or an error code. |
| 9470 | */ |
| 9471 | static int |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 9472 | xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int dolog) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9473 | { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9474 | int i; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9475 | xmlRelaxNGValidStatePtr state; |
| 9476 | |
| 9477 | state = ctxt->state; |
| 9478 | if (state->seq != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9479 | state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq); |
| 9480 | if (state->seq != NULL) { |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 9481 | if (dolog) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9482 | VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT, |
| 9483 | state->node->name, state->seq->name); |
| 9484 | } |
| 9485 | return (-1); |
| 9486 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9487 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9488 | for (i = 0; i < state->nbAttrs; i++) { |
| 9489 | if (state->attrs[i] != NULL) { |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 9490 | if (dolog) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9491 | VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR, |
| 9492 | state->attrs[i]->name, state->node->name); |
| 9493 | } |
| 9494 | return (-1 - i); |
| 9495 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9496 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9497 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9498 | } |
| 9499 | |
| 9500 | /** |
| 9501 | * xmlRelaxNGValidateState: |
| 9502 | * @ctxt: a Relax-NG validation context |
| 9503 | * @define: the definition to verify |
| 9504 | * |
| 9505 | * Validate the current state against the definition |
| 9506 | * |
| 9507 | * Returns 0 if the validation succeeded or an error code. |
| 9508 | */ |
| 9509 | static int |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9510 | xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt, |
| 9511 | xmlRelaxNGDefinePtr define) |
| 9512 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9513 | xmlNodePtr node; |
| 9514 | int ret = 0, i, tmp, oldflags, errNr; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9515 | xmlRelaxNGValidStatePtr oldstate = NULL, state; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9516 | |
| 9517 | if (define == NULL) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9518 | VALID_ERR(XML_RELAXNG_ERR_NODEFINE); |
| 9519 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9520 | } |
| 9521 | |
| 9522 | if (ctxt->state != NULL) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9523 | node = ctxt->state->seq; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9524 | } else { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9525 | node = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9526 | } |
| 9527 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9528 | for (i = 0; i < ctxt->depth; i++) |
| 9529 | xmlGenericError(xmlGenericErrorContext, " "); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9530 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9531 | "Start validating %s ", xmlRelaxNGDefName(define)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9532 | if (define->name != NULL) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9533 | xmlGenericError(xmlGenericErrorContext, "%s ", define->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9534 | if ((node != NULL) && (node->name != NULL)) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9535 | xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9536 | else |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9537 | xmlGenericError(xmlGenericErrorContext, "\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9538 | #endif |
| 9539 | ctxt->depth++; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9540 | switch (define->type) { |
| 9541 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9542 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 9543 | ret = 0; |
| 9544 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9545 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9546 | ret = -1; |
| 9547 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9548 | case XML_RELAXNG_TEXT: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9549 | while ((node != NULL) && |
| 9550 | ((node->type == XML_TEXT_NODE) || |
| 9551 | (node->type == XML_COMMENT_NODE) || |
| 9552 | (node->type == XML_PI_NODE) || |
| 9553 | (node->type == XML_CDATA_SECTION_NODE))) |
| 9554 | node = node->next; |
| 9555 | ctxt->state->seq = node; |
| 9556 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9557 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9558 | errNr = ctxt->errNr; |
| 9559 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 9560 | if (node == NULL) { |
| 9561 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name); |
| 9562 | ret = -1; |
| 9563 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9564 | xmlRelaxNGDumpValidError(ctxt); |
| 9565 | break; |
| 9566 | } |
| 9567 | if (node->type != XML_ELEMENT_NODE) { |
| 9568 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
| 9569 | ret = -1; |
| 9570 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9571 | xmlRelaxNGDumpValidError(ctxt); |
| 9572 | break; |
| 9573 | } |
| 9574 | /* |
| 9575 | * This node was already validated successfully against |
| 9576 | * this definition. |
| 9577 | */ |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 9578 | if (node->psvi == define) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9579 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
| 9580 | if (ctxt->errNr > errNr) |
| 9581 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9582 | if (ctxt->errNr != 0) { |
| 9583 | while ((ctxt->err != NULL) && |
| 9584 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) |
| 9585 | && (xmlStrEqual(ctxt->err->arg2, node->name))) |
| 9586 | || |
| 9587 | ((ctxt->err->err == |
| 9588 | XML_RELAXNG_ERR_ELEMEXTRANS) |
| 9589 | && (xmlStrEqual(ctxt->err->arg1, node->name))) |
| 9590 | || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) |
| 9591 | || (ctxt->err->err == |
| 9592 | XML_RELAXNG_ERR_NOTELEM))) |
| 9593 | xmlRelaxNGValidErrorPop(ctxt); |
| 9594 | } |
| 9595 | break; |
| 9596 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9597 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9598 | ret = xmlRelaxNGElementMatch(ctxt, define, node); |
| 9599 | if (ret <= 0) { |
| 9600 | ret = -1; |
| 9601 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9602 | xmlRelaxNGDumpValidError(ctxt); |
| 9603 | break; |
| 9604 | } |
| 9605 | ret = 0; |
| 9606 | if (ctxt->errNr != 0) { |
| 9607 | if (ctxt->errNr > errNr) |
| 9608 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9609 | while ((ctxt->err != NULL) && |
| 9610 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) && |
| 9611 | (xmlStrEqual(ctxt->err->arg2, node->name))) || |
| 9612 | ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) && |
| 9613 | (xmlStrEqual(ctxt->err->arg1, node->name))) || |
| 9614 | (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) || |
| 9615 | (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM))) |
| 9616 | xmlRelaxNGValidErrorPop(ctxt); |
| 9617 | } |
| 9618 | errNr = ctxt->errNr; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9619 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9620 | oldflags = ctxt->flags; |
| 9621 | if (ctxt->flags & FLAGS_MIXED_CONTENT) { |
| 9622 | ctxt->flags -= FLAGS_MIXED_CONTENT; |
| 9623 | } |
| 9624 | state = xmlRelaxNGNewValidState(ctxt, node); |
| 9625 | if (state == NULL) { |
| 9626 | ret = -1; |
| 9627 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9628 | xmlRelaxNGDumpValidError(ctxt); |
| 9629 | break; |
| 9630 | } |
Daniel Veillard | 7fe1f3a | 2003-03-31 22:13:33 +0000 | [diff] [blame] | 9631 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9632 | oldstate = ctxt->state; |
| 9633 | ctxt->state = state; |
| 9634 | if (define->attrs != NULL) { |
| 9635 | tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
| 9636 | if (tmp != 0) { |
| 9637 | ret = -1; |
| 9638 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
| 9639 | } |
| 9640 | } |
| 9641 | if (define->contModel != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9642 | xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state; |
| 9643 | xmlRelaxNGStatesPtr tmpstates = ctxt->states; |
| 9644 | xmlNodePtr nseq; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9645 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9646 | nstate = xmlRelaxNGNewValidState(ctxt, node); |
| 9647 | ctxt->state = nstate; |
| 9648 | ctxt->states = NULL; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9649 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9650 | tmp = xmlRelaxNGValidateCompiledContent(ctxt, |
| 9651 | define->contModel, |
| 9652 | ctxt->state->seq); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9653 | nseq = ctxt->state->seq; |
| 9654 | ctxt->state = tmpstate; |
| 9655 | ctxt->states = tmpstates; |
| 9656 | xmlRelaxNGFreeValidState(ctxt, nstate); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9657 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9658 | #ifdef DEBUG_COMPILE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9659 | xmlGenericError(xmlGenericErrorContext, |
| 9660 | "Validating content of '%s' : %d\n", |
| 9661 | define->name, tmp); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9662 | #endif |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9663 | if (tmp != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9664 | ret = -1; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9665 | |
| 9666 | if (ctxt->states != NULL) { |
| 9667 | tmp = -1; |
| 9668 | |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9669 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9670 | state = ctxt->states->tabState[i]; |
| 9671 | ctxt->state = state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9672 | ctxt->state->seq = nseq; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9673 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9674 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9675 | tmp = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9676 | break; |
| 9677 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9678 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9679 | if (tmp != 0) { |
| 9680 | /* |
| 9681 | * validation error, log the message for the "best" one |
| 9682 | */ |
| 9683 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9684 | xmlRelaxNGLogBestError(ctxt); |
| 9685 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9686 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9687 | xmlRelaxNGFreeValidState(ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9688 | ctxt->states-> |
| 9689 | tabState[i]); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9690 | } |
| 9691 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9692 | ctxt->flags = oldflags; |
| 9693 | ctxt->states = NULL; |
| 9694 | if ((ret == 0) && (tmp == -1)) |
| 9695 | ret = -1; |
| 9696 | } else { |
| 9697 | state = ctxt->state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9698 | ctxt->state->seq = nseq; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9699 | if (ret == 0) |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9700 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9701 | xmlRelaxNGFreeValidState(ctxt, state); |
| 9702 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9703 | } else { |
| 9704 | if (define->content != NULL) { |
| 9705 | tmp = xmlRelaxNGValidateDefinitionList(ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9706 | define-> |
| 9707 | content); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9708 | if (tmp != 0) { |
| 9709 | ret = -1; |
| 9710 | if (ctxt->state == NULL) { |
| 9711 | ctxt->state = oldstate; |
| 9712 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, |
| 9713 | node->name); |
| 9714 | ctxt->state = NULL; |
| 9715 | } else { |
| 9716 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, |
| 9717 | node->name); |
| 9718 | } |
| 9719 | |
| 9720 | } |
| 9721 | } |
| 9722 | if (ctxt->states != NULL) { |
| 9723 | tmp = -1; |
| 9724 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9725 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9726 | state = ctxt->states->tabState[i]; |
| 9727 | ctxt->state = state; |
| 9728 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9729 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9730 | tmp = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9731 | break; |
| 9732 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9733 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9734 | if (tmp != 0) { |
| 9735 | /* |
| 9736 | * validation error, log the message for the "best" one |
| 9737 | */ |
| 9738 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9739 | xmlRelaxNGLogBestError(ctxt); |
| 9740 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9741 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9742 | xmlRelaxNGFreeValidState(ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9743 | ctxt->states-> |
| 9744 | tabState[i]); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9745 | } |
| 9746 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9747 | ctxt->flags = oldflags; |
| 9748 | ctxt->states = NULL; |
| 9749 | if ((ret == 0) && (tmp == -1)) |
| 9750 | ret = -1; |
| 9751 | } else { |
| 9752 | state = ctxt->state; |
| 9753 | if (ret == 0) |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9754 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9755 | xmlRelaxNGFreeValidState(ctxt, state); |
| 9756 | } |
| 9757 | } |
| 9758 | if (ret == 0) { |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 9759 | node->psvi = define; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9760 | } |
| 9761 | ctxt->flags = oldflags; |
| 9762 | ctxt->state = oldstate; |
| 9763 | if (oldstate != NULL) |
| 9764 | oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
| 9765 | if (ret != 0) { |
| 9766 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 9767 | xmlRelaxNGDumpValidError(ctxt); |
| 9768 | ret = 0; |
| 9769 | } else { |
| 9770 | ret = -2; |
| 9771 | } |
| 9772 | } else { |
| 9773 | if (ctxt->errNr > errNr) |
| 9774 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9775 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9776 | |
| 9777 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9778 | xmlGenericError(xmlGenericErrorContext, |
| 9779 | "xmlRelaxNGValidateDefinition(): validated %s : %d", |
| 9780 | node->name, ret); |
| 9781 | if (oldstate == NULL) |
| 9782 | xmlGenericError(xmlGenericErrorContext, ": no state\n"); |
| 9783 | else if (oldstate->seq == NULL) |
| 9784 | xmlGenericError(xmlGenericErrorContext, ": done\n"); |
| 9785 | else if (oldstate->seq->type == XML_ELEMENT_NODE) |
| 9786 | xmlGenericError(xmlGenericErrorContext, ": next elem %s\n", |
| 9787 | oldstate->seq->name); |
| 9788 | else |
| 9789 | xmlGenericError(xmlGenericErrorContext, ": next %s %d\n", |
| 9790 | oldstate->seq->name, oldstate->seq->type); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9791 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9792 | break; |
| 9793 | case XML_RELAXNG_OPTIONAL:{ |
| 9794 | errNr = ctxt->errNr; |
| 9795 | oldflags = ctxt->flags; |
| 9796 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9797 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
| 9798 | ret = |
| 9799 | xmlRelaxNGValidateDefinitionList(ctxt, |
| 9800 | define->content); |
| 9801 | if (ret != 0) { |
| 9802 | if (ctxt->state != NULL) |
| 9803 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 9804 | ctxt->state = oldstate; |
| 9805 | ctxt->flags = oldflags; |
| 9806 | ret = 0; |
| 9807 | if (ctxt->errNr > errNr) |
| 9808 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9809 | break; |
| 9810 | } |
| 9811 | if (ctxt->states != NULL) { |
| 9812 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
| 9813 | } else { |
| 9814 | ctxt->states = xmlRelaxNGNewStates(ctxt, 1); |
| 9815 | if (ctxt->states == NULL) { |
| 9816 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 9817 | ctxt->flags = oldflags; |
| 9818 | ret = -1; |
| 9819 | if (ctxt->errNr > errNr) |
| 9820 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9821 | break; |
| 9822 | } |
| 9823 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
| 9824 | xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state); |
| 9825 | ctxt->state = NULL; |
| 9826 | } |
| 9827 | ctxt->flags = oldflags; |
| 9828 | ret = 0; |
| 9829 | if (ctxt->errNr > errNr) |
| 9830 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9831 | break; |
| 9832 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9833 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9834 | errNr = ctxt->errNr; |
| 9835 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 9836 | if (ret != 0) { |
| 9837 | break; |
| 9838 | } |
| 9839 | if (ctxt->errNr > errNr) |
| 9840 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9841 | /* no break on purpose */ |
| 9842 | case XML_RELAXNG_ZEROORMORE:{ |
| 9843 | int progress; |
| 9844 | xmlRelaxNGStatesPtr states = NULL, res = NULL; |
| 9845 | int base, j; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9846 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9847 | errNr = ctxt->errNr; |
| 9848 | res = xmlRelaxNGNewStates(ctxt, 1); |
| 9849 | if (res == NULL) { |
| 9850 | ret = -1; |
| 9851 | break; |
| 9852 | } |
| 9853 | /* |
| 9854 | * All the input states are also exit states |
| 9855 | */ |
| 9856 | if (ctxt->state != NULL) { |
| 9857 | xmlRelaxNGAddStates(ctxt, res, |
| 9858 | xmlRelaxNGCopyValidState(ctxt, |
| 9859 | ctxt-> |
| 9860 | state)); |
| 9861 | } else { |
| 9862 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 9863 | xmlRelaxNGAddStates(ctxt, res, |
| 9864 | xmlRelaxNGCopyValidState(ctxt, |
| 9865 | ctxt-> |
| 9866 | states-> |
| 9867 | tabState |
| 9868 | [j])); |
| 9869 | } |
| 9870 | } |
| 9871 | oldflags = ctxt->flags; |
| 9872 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9873 | do { |
| 9874 | progress = 0; |
| 9875 | base = res->nbState; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9876 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9877 | if (ctxt->states != NULL) { |
| 9878 | states = ctxt->states; |
| 9879 | for (i = 0; i < states->nbState; i++) { |
| 9880 | ctxt->state = states->tabState[i]; |
| 9881 | ctxt->states = NULL; |
| 9882 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
| 9883 | define-> |
| 9884 | content); |
| 9885 | if (ret == 0) { |
| 9886 | if (ctxt->state != NULL) { |
| 9887 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 9888 | ctxt->state); |
| 9889 | ctxt->state = NULL; |
| 9890 | if (tmp == 1) |
| 9891 | progress = 1; |
| 9892 | } else if (ctxt->states != NULL) { |
| 9893 | for (j = 0; j < ctxt->states->nbState; |
| 9894 | j++) { |
| 9895 | tmp = |
| 9896 | xmlRelaxNGAddStates(ctxt, res, |
| 9897 | ctxt-> |
| 9898 | states-> |
| 9899 | tabState |
| 9900 | [j]); |
| 9901 | if (tmp == 1) |
| 9902 | progress = 1; |
| 9903 | } |
| 9904 | xmlRelaxNGFreeStates(ctxt, |
| 9905 | ctxt->states); |
| 9906 | ctxt->states = NULL; |
| 9907 | } |
| 9908 | } else { |
| 9909 | if (ctxt->state != NULL) { |
| 9910 | xmlRelaxNGFreeValidState(ctxt, |
| 9911 | ctxt->state); |
| 9912 | ctxt->state = NULL; |
| 9913 | } |
| 9914 | } |
| 9915 | } |
| 9916 | } else { |
| 9917 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
| 9918 | define-> |
| 9919 | content); |
| 9920 | if (ret != 0) { |
| 9921 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 9922 | ctxt->state = NULL; |
| 9923 | } else { |
| 9924 | base = res->nbState; |
| 9925 | if (ctxt->state != NULL) { |
| 9926 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 9927 | ctxt->state); |
| 9928 | ctxt->state = NULL; |
| 9929 | if (tmp == 1) |
| 9930 | progress = 1; |
| 9931 | } else if (ctxt->states != NULL) { |
| 9932 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 9933 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 9934 | ctxt-> |
| 9935 | states-> |
| 9936 | tabState[j]); |
| 9937 | if (tmp == 1) |
| 9938 | progress = 1; |
| 9939 | } |
| 9940 | if (states == NULL) { |
| 9941 | states = ctxt->states; |
| 9942 | } else { |
| 9943 | xmlRelaxNGFreeStates(ctxt, |
| 9944 | ctxt->states); |
| 9945 | } |
| 9946 | ctxt->states = NULL; |
| 9947 | } |
| 9948 | } |
| 9949 | } |
| 9950 | if (progress) { |
| 9951 | /* |
| 9952 | * Collect all the new nodes added at that step |
| 9953 | * and make them the new node set |
| 9954 | */ |
| 9955 | if (res->nbState - base == 1) { |
| 9956 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, |
| 9957 | res-> |
| 9958 | tabState |
| 9959 | [base]); |
| 9960 | } else { |
| 9961 | if (states == NULL) { |
| 9962 | xmlRelaxNGNewStates(ctxt, |
| 9963 | res->nbState - base); |
| 9964 | } |
| 9965 | states->nbState = 0; |
| 9966 | for (i = base; i < res->nbState; i++) |
| 9967 | xmlRelaxNGAddStates(ctxt, states, |
| 9968 | xmlRelaxNGCopyValidState |
| 9969 | (ctxt, |
| 9970 | res->tabState[i])); |
| 9971 | ctxt->states = states; |
| 9972 | } |
| 9973 | } |
| 9974 | } while (progress == 1); |
| 9975 | if (states != NULL) { |
| 9976 | xmlRelaxNGFreeStates(ctxt, states); |
| 9977 | } |
| 9978 | ctxt->states = res; |
| 9979 | ctxt->flags = oldflags; |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 9980 | #if 0 |
| 9981 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9982 | * errors may have to be propagated back... |
| 9983 | */ |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9984 | if (ctxt->errNr > errNr) |
| 9985 | xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 9986 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9987 | ret = 0; |
| 9988 | break; |
| 9989 | } |
| 9990 | case XML_RELAXNG_CHOICE:{ |
| 9991 | xmlRelaxNGDefinePtr list = NULL; |
| 9992 | xmlRelaxNGStatesPtr states = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9993 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9994 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9995 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9996 | errNr = ctxt->errNr; |
| 9997 | if ((define->dflags & IS_TRIABLE) |
| 9998 | && (define->data != NULL)) { |
| 9999 | xmlHashTablePtr triage = |
| 10000 | (xmlHashTablePtr) define->data; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 10001 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10002 | /* |
| 10003 | * Something we can optimize cleanly there is only one |
| 10004 | * possble branch out ! |
| 10005 | */ |
| 10006 | if (node == NULL) { |
| 10007 | ret = -1; |
| 10008 | break; |
| 10009 | } |
| 10010 | if ((node->type == XML_TEXT_NODE) || |
| 10011 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 10012 | list = |
| 10013 | xmlHashLookup2(triage, BAD_CAST "#text", NULL); |
| 10014 | } else if (node->type == XML_ELEMENT_NODE) { |
| 10015 | if (node->ns != NULL) { |
| 10016 | list = xmlHashLookup2(triage, node->name, |
| 10017 | node->ns->href); |
| 10018 | if (list == NULL) |
| 10019 | list = |
| 10020 | xmlHashLookup2(triage, BAD_CAST "#any", |
| 10021 | node->ns->href); |
| 10022 | } else |
| 10023 | list = |
| 10024 | xmlHashLookup2(triage, node->name, NULL); |
| 10025 | if (list == NULL) |
| 10026 | list = |
| 10027 | xmlHashLookup2(triage, BAD_CAST "#any", |
| 10028 | NULL); |
| 10029 | } |
| 10030 | if (list == NULL) { |
| 10031 | ret = -1; |
| 10032 | break; |
| 10033 | } |
| 10034 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
| 10035 | if (ret == 0) { |
| 10036 | } |
| 10037 | break; |
| 10038 | } |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 10039 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10040 | list = define->content; |
| 10041 | oldflags = ctxt->flags; |
| 10042 | ctxt->flags |= FLAGS_IGNORABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10043 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10044 | while (list != NULL) { |
| 10045 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
| 10046 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
| 10047 | if (ret == 0) { |
| 10048 | if (states == NULL) { |
| 10049 | states = xmlRelaxNGNewStates(ctxt, 1); |
| 10050 | } |
| 10051 | if (ctxt->state != NULL) { |
| 10052 | xmlRelaxNGAddStates(ctxt, states, ctxt->state); |
| 10053 | } else if (ctxt->states != NULL) { |
| 10054 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 10055 | xmlRelaxNGAddStates(ctxt, states, |
| 10056 | ctxt->states-> |
| 10057 | tabState[i]); |
| 10058 | } |
| 10059 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10060 | ctxt->states = NULL; |
| 10061 | } |
| 10062 | } else { |
| 10063 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10064 | } |
| 10065 | ctxt->state = oldstate; |
| 10066 | list = list->next; |
| 10067 | } |
| 10068 | if (states != NULL) { |
| 10069 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 10070 | ctxt->states = states; |
| 10071 | ctxt->state = NULL; |
| 10072 | ret = 0; |
| 10073 | } else { |
| 10074 | ctxt->states = NULL; |
| 10075 | } |
| 10076 | ctxt->flags = oldflags; |
| 10077 | if (ret != 0) { |
| 10078 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 10079 | xmlRelaxNGDumpValidError(ctxt); |
| 10080 | } |
| 10081 | } else { |
| 10082 | if (ctxt->errNr > errNr) |
| 10083 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 10084 | } |
| 10085 | break; |
| 10086 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10087 | case XML_RELAXNG_DEF: |
| 10088 | case XML_RELAXNG_GROUP: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10089 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 10090 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10091 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10092 | ret = xmlRelaxNGValidateInterleave(ctxt, define); |
| 10093 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10094 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10095 | ret = xmlRelaxNGValidateAttribute(ctxt, define); |
| 10096 | break; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 10097 | case XML_RELAXNG_START: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10098 | case XML_RELAXNG_NOOP: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10099 | case XML_RELAXNG_REF: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10100 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 10101 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10102 | ret = xmlRelaxNGValidateDefinition(ctxt, define->content); |
| 10103 | break; |
| 10104 | case XML_RELAXNG_DATATYPE:{ |
| 10105 | xmlNodePtr child; |
| 10106 | xmlChar *content = NULL; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10107 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10108 | child = node; |
| 10109 | while (child != NULL) { |
| 10110 | if (child->type == XML_ELEMENT_NODE) { |
| 10111 | VALID_ERR2(XML_RELAXNG_ERR_DATAELEM, |
| 10112 | node->parent->name); |
| 10113 | ret = -1; |
| 10114 | break; |
| 10115 | } else if ((child->type == XML_TEXT_NODE) || |
| 10116 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 10117 | content = xmlStrcat(content, child->content); |
| 10118 | } |
| 10119 | /* TODO: handle entities ... */ |
| 10120 | child = child->next; |
| 10121 | } |
| 10122 | if (ret == -1) { |
| 10123 | if (content != NULL) |
| 10124 | xmlFree(content); |
| 10125 | break; |
| 10126 | } |
| 10127 | if (content == NULL) { |
| 10128 | content = xmlStrdup(BAD_CAST ""); |
| 10129 | if (content == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10130 | xmlRngVErrMemory(ctxt, "validating\n"); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10131 | ret = -1; |
| 10132 | break; |
| 10133 | } |
| 10134 | } |
| 10135 | ret = xmlRelaxNGValidateDatatype(ctxt, content, define, |
| 10136 | ctxt->state->seq); |
| 10137 | if (ret == -1) { |
| 10138 | VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name); |
| 10139 | } else if (ret == 0) { |
| 10140 | ctxt->state->seq = NULL; |
| 10141 | } |
| 10142 | if (content != NULL) |
| 10143 | xmlFree(content); |
| 10144 | break; |
| 10145 | } |
| 10146 | case XML_RELAXNG_VALUE:{ |
| 10147 | xmlChar *content = NULL; |
| 10148 | xmlChar *oldvalue; |
| 10149 | xmlNodePtr child; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10150 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10151 | child = node; |
| 10152 | while (child != NULL) { |
| 10153 | if (child->type == XML_ELEMENT_NODE) { |
| 10154 | VALID_ERR2(XML_RELAXNG_ERR_VALELEM, |
| 10155 | node->parent->name); |
| 10156 | ret = -1; |
| 10157 | break; |
| 10158 | } else if ((child->type == XML_TEXT_NODE) || |
| 10159 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 10160 | content = xmlStrcat(content, child->content); |
| 10161 | } |
| 10162 | /* TODO: handle entities ... */ |
| 10163 | child = child->next; |
| 10164 | } |
| 10165 | if (ret == -1) { |
| 10166 | if (content != NULL) |
| 10167 | xmlFree(content); |
| 10168 | break; |
| 10169 | } |
| 10170 | if (content == NULL) { |
| 10171 | content = xmlStrdup(BAD_CAST ""); |
| 10172 | if (content == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10173 | xmlRngVErrMemory(ctxt, "validating\n"); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10174 | ret = -1; |
| 10175 | break; |
| 10176 | } |
| 10177 | } |
| 10178 | oldvalue = ctxt->state->value; |
| 10179 | ctxt->state->value = content; |
| 10180 | ret = xmlRelaxNGValidateValue(ctxt, define); |
| 10181 | ctxt->state->value = oldvalue; |
| 10182 | if (ret == -1) { |
| 10183 | VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name); |
| 10184 | } else if (ret == 0) { |
| 10185 | ctxt->state->seq = NULL; |
| 10186 | } |
| 10187 | if (content != NULL) |
| 10188 | xmlFree(content); |
| 10189 | break; |
| 10190 | } |
| 10191 | case XML_RELAXNG_LIST:{ |
| 10192 | xmlChar *content; |
| 10193 | xmlNodePtr child; |
| 10194 | xmlChar *oldvalue, *oldendvalue; |
| 10195 | int len; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10196 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10197 | /* |
| 10198 | * Make sure it's only text nodes |
| 10199 | */ |
| 10200 | |
| 10201 | content = NULL; |
| 10202 | child = node; |
| 10203 | while (child != NULL) { |
| 10204 | if (child->type == XML_ELEMENT_NODE) { |
| 10205 | VALID_ERR2(XML_RELAXNG_ERR_LISTELEM, |
| 10206 | node->parent->name); |
| 10207 | ret = -1; |
| 10208 | break; |
| 10209 | } else if ((child->type == XML_TEXT_NODE) || |
| 10210 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 10211 | content = xmlStrcat(content, child->content); |
| 10212 | } |
| 10213 | /* TODO: handle entities ... */ |
| 10214 | child = child->next; |
| 10215 | } |
| 10216 | if (ret == -1) { |
| 10217 | if (content != NULL) |
| 10218 | xmlFree(content); |
| 10219 | break; |
| 10220 | } |
| 10221 | if (content == NULL) { |
| 10222 | content = xmlStrdup(BAD_CAST ""); |
| 10223 | if (content == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10224 | xmlRngVErrMemory(ctxt, "validating\n"); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10225 | ret = -1; |
| 10226 | break; |
| 10227 | } |
| 10228 | } |
| 10229 | len = xmlStrlen(content); |
| 10230 | oldvalue = ctxt->state->value; |
| 10231 | oldendvalue = ctxt->state->endvalue; |
| 10232 | ctxt->state->value = content; |
| 10233 | ctxt->state->endvalue = content + len; |
| 10234 | ret = xmlRelaxNGValidateValue(ctxt, define); |
| 10235 | ctxt->state->value = oldvalue; |
| 10236 | ctxt->state->endvalue = oldendvalue; |
| 10237 | if (ret == -1) { |
| 10238 | VALID_ERR(XML_RELAXNG_ERR_LIST); |
| 10239 | } else if ((ret == 0) && (node != NULL)) { |
| 10240 | ctxt->state->seq = node->next; |
| 10241 | } |
| 10242 | if (content != NULL) |
| 10243 | xmlFree(content); |
| 10244 | break; |
| 10245 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10246 | case XML_RELAXNG_EXCEPT: |
| 10247 | case XML_RELAXNG_PARAM: |
| 10248 | TODO ret = -1; |
| 10249 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10250 | } |
| 10251 | ctxt->depth--; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10252 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10253 | for (i = 0; i < ctxt->depth; i++) |
| 10254 | xmlGenericError(xmlGenericErrorContext, " "); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10255 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10256 | "Validating %s ", xmlRelaxNGDefName(define)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10257 | if (define->name != NULL) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10258 | xmlGenericError(xmlGenericErrorContext, "%s ", define->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10259 | if (ret == 0) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10260 | xmlGenericError(xmlGenericErrorContext, "suceeded\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10261 | else |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10262 | xmlGenericError(xmlGenericErrorContext, "failed\n"); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10263 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10264 | return (ret); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10265 | } |
| 10266 | |
| 10267 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10268 | * xmlRelaxNGValidateDefinition: |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10269 | * @ctxt: a Relax-NG validation context |
| 10270 | * @define: the definition to verify |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10271 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10272 | * Validate the current node lists against the definition |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10273 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10274 | * Returns 0 if the validation succeeded or an error code. |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10275 | */ |
| 10276 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10277 | xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
| 10278 | xmlRelaxNGDefinePtr define) |
| 10279 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10280 | xmlRelaxNGStatesPtr states, res; |
| 10281 | int i, j, k, ret, oldflags; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10282 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10283 | /* |
| 10284 | * We should NOT have both ctxt->state and ctxt->states |
| 10285 | */ |
| 10286 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10287 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10288 | ctxt->state = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10289 | } |
| 10290 | |
| 10291 | if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10292 | if (ctxt->states != NULL) { |
| 10293 | ctxt->state = ctxt->states->tabState[0]; |
| 10294 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10295 | ctxt->states = NULL; |
| 10296 | } |
| 10297 | ret = xmlRelaxNGValidateState(ctxt, define); |
| 10298 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10299 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10300 | ctxt->state = NULL; |
| 10301 | } |
| 10302 | if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) { |
| 10303 | ctxt->state = ctxt->states->tabState[0]; |
| 10304 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10305 | ctxt->states = NULL; |
| 10306 | } |
| 10307 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10308 | } |
| 10309 | |
| 10310 | states = ctxt->states; |
| 10311 | ctxt->states = NULL; |
| 10312 | res = NULL; |
| 10313 | j = 0; |
| 10314 | oldflags = ctxt->flags; |
| 10315 | ctxt->flags |= FLAGS_IGNORABLE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10316 | for (i = 0; i < states->nbState; i++) { |
| 10317 | ctxt->state = states->tabState[i]; |
| 10318 | ctxt->states = NULL; |
| 10319 | ret = xmlRelaxNGValidateState(ctxt, define); |
| 10320 | /* |
| 10321 | * We should NOT have both ctxt->state and ctxt->states |
| 10322 | */ |
| 10323 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10324 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10325 | ctxt->state = NULL; |
| 10326 | } |
| 10327 | if (ret == 0) { |
| 10328 | if (ctxt->states == NULL) { |
| 10329 | if (res != NULL) { |
| 10330 | /* add the state to the container */ |
| 10331 | xmlRelaxNGAddStates(ctxt, res, ctxt->state); |
| 10332 | ctxt->state = NULL; |
| 10333 | } else { |
| 10334 | /* add the state directly in states */ |
| 10335 | states->tabState[j++] = ctxt->state; |
| 10336 | ctxt->state = NULL; |
| 10337 | } |
| 10338 | } else { |
| 10339 | if (res == NULL) { |
| 10340 | /* make it the new container and copy other results */ |
| 10341 | res = ctxt->states; |
| 10342 | ctxt->states = NULL; |
| 10343 | for (k = 0; k < j; k++) |
| 10344 | xmlRelaxNGAddStates(ctxt, res, |
| 10345 | states->tabState[k]); |
| 10346 | } else { |
| 10347 | /* add all the new results to res and reff the container */ |
| 10348 | for (k = 0; k < ctxt->states->nbState; k++) |
| 10349 | xmlRelaxNGAddStates(ctxt, res, |
| 10350 | ctxt->states->tabState[k]); |
| 10351 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10352 | ctxt->states = NULL; |
| 10353 | } |
| 10354 | } |
| 10355 | } else { |
| 10356 | if (ctxt->state != NULL) { |
| 10357 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10358 | ctxt->state = NULL; |
| 10359 | } else if (ctxt->states != NULL) { |
| 10360 | for (k = 0; k < ctxt->states->nbState; k++) |
| 10361 | xmlRelaxNGFreeValidState(ctxt, |
| 10362 | ctxt->states->tabState[k]); |
| 10363 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10364 | ctxt->states = NULL; |
| 10365 | } |
| 10366 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10367 | } |
| 10368 | ctxt->flags = oldflags; |
| 10369 | if (res != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10370 | xmlRelaxNGFreeStates(ctxt, states); |
| 10371 | ctxt->states = res; |
| 10372 | ret = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10373 | } else if (j > 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10374 | states->nbState = j; |
| 10375 | ctxt->states = states; |
| 10376 | ret = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10377 | } else if (j == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10378 | ctxt->state = states->tabState[0]; |
| 10379 | xmlRelaxNGFreeStates(ctxt, states); |
| 10380 | ret = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10381 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10382 | ret = -1; |
| 10383 | xmlRelaxNGFreeStates(ctxt, states); |
| 10384 | if (ctxt->states != NULL) { |
| 10385 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10386 | ctxt->states = NULL; |
| 10387 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10388 | } |
| 10389 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10390 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10391 | ctxt->state = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10392 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10393 | return (ret); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10394 | } |
| 10395 | |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10396 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10397 | * xmlRelaxNGValidateDocument: |
| 10398 | * @ctxt: a Relax-NG validation context |
| 10399 | * @doc: the document |
| 10400 | * |
| 10401 | * Validate the given document |
| 10402 | * |
| 10403 | * Returns 0 if the validation succeeded or an error code. |
| 10404 | */ |
| 10405 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10406 | xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) |
| 10407 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10408 | int ret; |
| 10409 | xmlRelaxNGPtr schema; |
| 10410 | xmlRelaxNGGrammarPtr grammar; |
| 10411 | xmlRelaxNGValidStatePtr state; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10412 | xmlNodePtr node; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10413 | |
| 10414 | if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10415 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10416 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10417 | ctxt->errNo = XML_RELAXNG_OK; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10418 | schema = ctxt->schema; |
| 10419 | grammar = schema->topgrammar; |
| 10420 | if (grammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10421 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 10422 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10423 | } |
| 10424 | state = xmlRelaxNGNewValidState(ctxt, NULL); |
| 10425 | ctxt->state = state; |
| 10426 | ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10427 | if ((ctxt->state != NULL) && (state->seq != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10428 | state = ctxt->state; |
| 10429 | node = state->seq; |
| 10430 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 10431 | if (node != NULL) { |
| 10432 | if (ret != -1) { |
| 10433 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
| 10434 | ret = -1; |
| 10435 | } |
| 10436 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10437 | } else if (ctxt->states != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10438 | int i; |
| 10439 | int tmp = -1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10440 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10441 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 10442 | state = ctxt->states->tabState[i]; |
| 10443 | node = state->seq; |
| 10444 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 10445 | if (node == NULL) |
| 10446 | tmp = 0; |
| 10447 | xmlRelaxNGFreeValidState(ctxt, state); |
| 10448 | } |
| 10449 | if (tmp == -1) { |
| 10450 | if (ret != -1) { |
| 10451 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
| 10452 | ret = -1; |
| 10453 | } |
| 10454 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10455 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 10456 | if (ctxt->state != NULL) { |
| 10457 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10458 | ctxt->state = NULL; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 10459 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10460 | if (ret != 0) |
| 10461 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 10462 | #ifdef DEBUG |
| 10463 | else if (ctxt->errNr != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10464 | ctxt->error(ctxt->userData, |
| 10465 | "%d Extra error messages left on stack !\n", |
| 10466 | ctxt->errNr); |
| 10467 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 10468 | } |
| 10469 | #endif |
Daniel Veillard | f54cd53 | 2004-02-25 11:52:31 +0000 | [diff] [blame] | 10470 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10471 | if (ctxt->idref == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10472 | xmlValidCtxt vctxt; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10473 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10474 | memset(&vctxt, 0, sizeof(xmlValidCtxt)); |
| 10475 | vctxt.valid = 1; |
| 10476 | vctxt.error = ctxt->error; |
| 10477 | vctxt.warning = ctxt->warning; |
| 10478 | vctxt.userData = ctxt->userData; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10479 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10480 | if (xmlValidateDocumentFinal(&vctxt, doc) != 1) |
| 10481 | ret = -1; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10482 | } |
Daniel Veillard | f54cd53 | 2004-02-25 11:52:31 +0000 | [diff] [blame] | 10483 | #endif /* LIBXML_VALID_ENABLED */ |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10484 | if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10485 | ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10486 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10487 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10488 | } |
| 10489 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10490 | /************************************************************************ |
| 10491 | * * |
| 10492 | * Validation interfaces * |
| 10493 | * * |
| 10494 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10495 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10496 | /** |
| 10497 | * xmlRelaxNGNewValidCtxt: |
| 10498 | * @schema: a precompiled XML RelaxNGs |
| 10499 | * |
| 10500 | * Create an XML RelaxNGs validation context based on the given schema |
| 10501 | * |
| 10502 | * Returns the validation context or NULL in case of error |
| 10503 | */ |
| 10504 | xmlRelaxNGValidCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10505 | xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema) |
| 10506 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10507 | xmlRelaxNGValidCtxtPtr ret; |
| 10508 | |
| 10509 | ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt)); |
| 10510 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10511 | xmlRngVErrMemory(NULL, "building context\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10512 | return (NULL); |
| 10513 | } |
| 10514 | memset(ret, 0, sizeof(xmlRelaxNGValidCtxt)); |
| 10515 | ret->schema = schema; |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 10516 | ret->error = xmlGenericError; |
| 10517 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 10518 | ret->errNr = 0; |
| 10519 | ret->errMax = 0; |
| 10520 | ret->err = NULL; |
| 10521 | ret->errTab = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10522 | ret->idref = schema->idref; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10523 | ret->states = NULL; |
| 10524 | ret->freeState = NULL; |
| 10525 | ret->freeStates = NULL; |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10526 | ret->errNo = XML_RELAXNG_OK; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10527 | return (ret); |
| 10528 | } |
| 10529 | |
| 10530 | /** |
| 10531 | * xmlRelaxNGFreeValidCtxt: |
| 10532 | * @ctxt: the schema validation context |
| 10533 | * |
| 10534 | * Free the resources associated to the schema validation context |
| 10535 | */ |
| 10536 | void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10537 | xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt) |
| 10538 | { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10539 | int k; |
| 10540 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10541 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10542 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10543 | if (ctxt->states != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10544 | xmlRelaxNGFreeStates(NULL, ctxt->states); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10545 | if (ctxt->freeState != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10546 | for (k = 0; k < ctxt->freeState->nbState; k++) { |
| 10547 | xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]); |
| 10548 | } |
| 10549 | xmlRelaxNGFreeStates(NULL, ctxt->freeState); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10550 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10551 | if (ctxt->freeStates != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10552 | for (k = 0; k < ctxt->freeStatesNr; k++) { |
| 10553 | xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]); |
| 10554 | } |
| 10555 | xmlFree(ctxt->freeStates); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10556 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 10557 | if (ctxt->errTab != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10558 | xmlFree(ctxt->errTab); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 10559 | if (ctxt->elemTab != NULL) { |
| 10560 | xmlRegExecCtxtPtr exec; |
| 10561 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10562 | exec = xmlRelaxNGElemPop(ctxt); |
| 10563 | while (exec != NULL) { |
| 10564 | xmlRegFreeExecCtxt(exec); |
| 10565 | exec = xmlRelaxNGElemPop(ctxt); |
| 10566 | } |
| 10567 | xmlFree(ctxt->elemTab); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 10568 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10569 | xmlFree(ctxt); |
| 10570 | } |
| 10571 | |
| 10572 | /** |
| 10573 | * xmlRelaxNGSetValidErrors: |
| 10574 | * @ctxt: a Relax-NG validation context |
| 10575 | * @err: the error function |
| 10576 | * @warn: the warning function |
| 10577 | * @ctx: the functions context |
| 10578 | * |
| 10579 | * Set the error and warning callback informations |
| 10580 | */ |
| 10581 | void |
| 10582 | xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10583 | xmlRelaxNGValidityErrorFunc err, |
| 10584 | xmlRelaxNGValidityWarningFunc warn, void *ctx) |
| 10585 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10586 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10587 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10588 | ctxt->error = err; |
| 10589 | ctxt->warning = warn; |
| 10590 | ctxt->userData = ctx; |
| 10591 | } |
| 10592 | |
| 10593 | /** |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 10594 | * xmlRelaxNGGetValidErrors: |
| 10595 | * @ctxt: a Relax-NG validation context |
| 10596 | * @err: the error function result |
| 10597 | * @warn: the warning function result |
| 10598 | * @ctx: the functions context result |
| 10599 | * |
| 10600 | * Get the error and warning callback informations |
| 10601 | * |
| 10602 | * Returns -1 in case of error and 0 otherwise |
| 10603 | */ |
| 10604 | int |
| 10605 | xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10606 | xmlRelaxNGValidityErrorFunc * err, |
| 10607 | xmlRelaxNGValidityWarningFunc * warn, void **ctx) |
| 10608 | { |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 10609 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10610 | return (-1); |
| 10611 | if (err != NULL) |
| 10612 | *err = ctxt->error; |
| 10613 | if (warn != NULL) |
| 10614 | *warn = ctxt->warning; |
| 10615 | if (ctx != NULL) |
| 10616 | *ctx = ctxt->userData; |
| 10617 | return (0); |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 10618 | } |
| 10619 | |
| 10620 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10621 | * xmlRelaxNGValidateDoc: |
| 10622 | * @ctxt: a Relax-NG validation context |
| 10623 | * @doc: a parsed document tree |
| 10624 | * |
| 10625 | * Validate a document tree in memory. |
| 10626 | * |
| 10627 | * Returns 0 if the document is valid, a positive error code |
| 10628 | * number otherwise and -1 in case of internal or API error. |
| 10629 | */ |
| 10630 | int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10631 | xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) |
| 10632 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10633 | int ret; |
| 10634 | |
| 10635 | if ((ctxt == NULL) || (doc == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10636 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10637 | |
| 10638 | ctxt->doc = doc; |
| 10639 | |
| 10640 | ret = xmlRelaxNGValidateDocument(ctxt, doc); |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 10641 | /* |
| 10642 | * TODO: build error codes |
| 10643 | */ |
| 10644 | if (ret == -1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10645 | return (1); |
| 10646 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10647 | } |
| 10648 | |
| 10649 | #endif /* LIBXML_SCHEMAS_ENABLED */ |