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 | 4259532 | 2004-11-08 10:52:06 +0000 | [diff] [blame] | 251 | int freedoc; /* need to free the document */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 252 | }; |
| 253 | |
| 254 | #define FLAGS_IGNORABLE 1 |
| 255 | #define FLAGS_NEGATIVE 2 |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 256 | #define FLAGS_MIXED_CONTENT 4 |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 257 | |
| 258 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 259 | * xmlRelaxNGInterleaveGroup: |
| 260 | * |
| 261 | * A RelaxNGs partition set associated to lists of definitions |
| 262 | */ |
| 263 | typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup; |
| 264 | typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr; |
| 265 | struct _xmlRelaxNGInterleaveGroup { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 266 | xmlRelaxNGDefinePtr rule; /* the rule to satisfy */ |
| 267 | xmlRelaxNGDefinePtr *defs; /* the array of element definitions */ |
| 268 | xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 269 | }; |
| 270 | |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 271 | #define IS_DETERMINIST 1 |
| 272 | #define IS_NEEDCHECK 2 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 273 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 274 | /** |
| 275 | * xmlRelaxNGPartitions: |
| 276 | * |
| 277 | * A RelaxNGs partition associated to an interleave group |
| 278 | */ |
| 279 | typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition; |
| 280 | typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr; |
| 281 | struct _xmlRelaxNGPartition { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 282 | int nbgroups; /* number of groups in the partitions */ |
| 283 | xmlHashTablePtr triage; /* hash table used to direct nodes to the |
| 284 | * right group when possible */ |
| 285 | int flags; /* determinist ? */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 286 | xmlRelaxNGInterleaveGroupPtr *groups; |
| 287 | }; |
| 288 | |
| 289 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 290 | * xmlRelaxNGValidState: |
| 291 | * |
| 292 | * A RelaxNGs validation state |
| 293 | */ |
| 294 | #define MAX_ATTR 20 |
| 295 | typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState; |
| 296 | typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr; |
| 297 | struct _xmlRelaxNGValidState { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 298 | xmlNodePtr node; /* the current node */ |
| 299 | xmlNodePtr seq; /* the sequence of children left to validate */ |
| 300 | int nbAttrs; /* the number of attributes */ |
| 301 | int maxAttrs; /* the size of attrs */ |
| 302 | int nbAttrLeft; /* the number of attributes left to validate */ |
| 303 | xmlChar *value; /* the value when operating on string */ |
| 304 | xmlChar *endvalue; /* the end value when operating on string */ |
| 305 | xmlAttrPtr *attrs; /* the array of attributes */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 306 | }; |
| 307 | |
| 308 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 309 | * xmlRelaxNGStates: |
| 310 | * |
| 311 | * A RelaxNGs container for validation state |
| 312 | */ |
| 313 | typedef struct _xmlRelaxNGStates xmlRelaxNGStates; |
| 314 | typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr; |
| 315 | struct _xmlRelaxNGStates { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 316 | int nbState; /* the number of states */ |
| 317 | int maxState; /* the size of the array */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 318 | xmlRelaxNGValidStatePtr *tabState; |
| 319 | }; |
| 320 | |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 321 | #define ERROR_IS_DUP 1 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 322 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 323 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 324 | * xmlRelaxNGValidError: |
| 325 | * |
| 326 | * A RelaxNGs validation error |
| 327 | */ |
| 328 | typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError; |
| 329 | typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr; |
| 330 | struct _xmlRelaxNGValidError { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 331 | xmlRelaxNGValidErr err; /* the error number */ |
| 332 | int flags; /* flags */ |
| 333 | xmlNodePtr node; /* the current node */ |
| 334 | xmlNodePtr seq; /* the current child */ |
| 335 | const xmlChar *arg1; /* first arg */ |
| 336 | const xmlChar *arg2; /* second arg */ |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 337 | }; |
| 338 | |
| 339 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 340 | * xmlRelaxNGValidCtxt: |
| 341 | * |
| 342 | * A RelaxNGs validation context |
| 343 | */ |
| 344 | |
| 345 | struct _xmlRelaxNGValidCtxt { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 346 | void *userData; /* user specific data block */ |
| 347 | xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */ |
| 348 | xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */ |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 349 | xmlStructuredErrorFunc serror; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 350 | int nbErrors; /* number of errors in validation */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 351 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 352 | xmlRelaxNGPtr schema; /* The schema in use */ |
| 353 | xmlDocPtr doc; /* the document being validated */ |
| 354 | int flags; /* validation flags */ |
| 355 | int depth; /* validation depth */ |
| 356 | int idref; /* requires idref checking */ |
| 357 | int errNo; /* the first error found */ |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 358 | |
| 359 | /* |
| 360 | * Errors accumulated in branches may have to be stacked to be |
| 361 | * provided back when it's sure they affect validation. |
| 362 | */ |
| 363 | xmlRelaxNGValidErrorPtr err; /* Last error */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 364 | int errNr; /* Depth of the error stack */ |
| 365 | int errMax; /* Max depth of the error stack */ |
| 366 | xmlRelaxNGValidErrorPtr errTab; /* stack of errors */ |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 367 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 368 | xmlRelaxNGValidStatePtr state; /* the current validation state */ |
| 369 | xmlRelaxNGStatesPtr states; /* the accumulated state list */ |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 370 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 371 | xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */ |
| 372 | int freeStatesNr; |
| 373 | int freeStatesMax; |
| 374 | xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */ |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 375 | |
| 376 | /* |
| 377 | * This is used for "progressive" validation |
| 378 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 379 | xmlRegExecCtxtPtr elem; /* the current element regexp */ |
| 380 | int elemNr; /* the number of element validated */ |
| 381 | int elemMax; /* the max depth of elements */ |
| 382 | xmlRegExecCtxtPtr *elemTab; /* the stack of regexp runtime */ |
| 383 | int pstate; /* progressive state */ |
| 384 | xmlNodePtr pnode; /* the current node */ |
| 385 | xmlRelaxNGDefinePtr pdef; /* the non-streamable definition */ |
| 386 | int perr; /* signal error in content model |
| 387 | * outside the regexp */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 388 | }; |
| 389 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 390 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 391 | * xmlRelaxNGInclude: |
| 392 | * |
| 393 | * Structure associated to a RelaxNGs document element |
| 394 | */ |
| 395 | struct _xmlRelaxNGInclude { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 396 | xmlRelaxNGIncludePtr next; /* keep a chain of includes */ |
| 397 | xmlChar *href; /* the normalized href value */ |
| 398 | xmlDocPtr doc; /* the associated XML document */ |
| 399 | xmlRelaxNGDefinePtr content; /* the definitions */ |
| 400 | xmlRelaxNGPtr schema; /* the schema */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 401 | }; |
| 402 | |
| 403 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 404 | * xmlRelaxNGDocument: |
| 405 | * |
| 406 | * Structure associated to a RelaxNGs document element |
| 407 | */ |
| 408 | struct _xmlRelaxNGDocument { |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 409 | xmlRelaxNGDocumentPtr next; /* keep a chain of documents */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 410 | xmlChar *href; /* the normalized href value */ |
| 411 | xmlDocPtr doc; /* the associated XML document */ |
| 412 | xmlRelaxNGDefinePtr content; /* the definitions */ |
| 413 | xmlRelaxNGPtr schema; /* the schema */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 414 | }; |
| 415 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 416 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 417 | /************************************************************************ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 418 | * * |
| 419 | * Some factorized error routines * |
| 420 | * * |
| 421 | ************************************************************************/ |
| 422 | |
| 423 | /** |
| 424 | * xmlRngPErrMemory: |
| 425 | * @ctxt: an Relax-NG parser context |
| 426 | * @extra: extra informations |
| 427 | * |
| 428 | * Handle a redefinition of attribute error |
| 429 | */ |
| 430 | static void |
| 431 | xmlRngPErrMemory(xmlRelaxNGParserCtxtPtr ctxt, const char *extra) |
| 432 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 433 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 434 | xmlGenericErrorFunc channel = NULL; |
| 435 | void *data = NULL; |
| 436 | |
| 437 | if (ctxt != NULL) { |
| 438 | channel = ctxt->error; |
| 439 | data = ctxt->userData; |
| 440 | ctxt->nbErrors++; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 441 | schannel = ctxt->serror; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 442 | } |
| 443 | if (extra) |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 444 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 445 | NULL, NULL, XML_FROM_RELAXNGP, |
| 446 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra, |
| 447 | NULL, NULL, 0, 0, |
| 448 | "Memory allocation failed : %s\n", extra); |
| 449 | else |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 450 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 451 | NULL, NULL, XML_FROM_RELAXNGP, |
| 452 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL, |
| 453 | NULL, NULL, 0, 0, "Memory allocation failed\n"); |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * xmlRngVErrMemory: |
| 458 | * @ctxt: a Relax-NG validation context |
| 459 | * @extra: extra informations |
| 460 | * |
| 461 | * Handle a redefinition of attribute error |
| 462 | */ |
| 463 | static void |
| 464 | xmlRngVErrMemory(xmlRelaxNGValidCtxtPtr ctxt, const char *extra) |
| 465 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 466 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 467 | xmlGenericErrorFunc channel = NULL; |
| 468 | void *data = NULL; |
| 469 | |
| 470 | if (ctxt != NULL) { |
| 471 | channel = ctxt->error; |
| 472 | data = ctxt->userData; |
| 473 | ctxt->nbErrors++; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 474 | schannel = ctxt->serror; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 475 | } |
| 476 | if (extra) |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 477 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 478 | NULL, NULL, XML_FROM_RELAXNGV, |
| 479 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra, |
| 480 | NULL, NULL, 0, 0, |
| 481 | "Memory allocation failed : %s\n", extra); |
| 482 | else |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 483 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 484 | NULL, NULL, XML_FROM_RELAXNGV, |
| 485 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL, |
| 486 | NULL, NULL, 0, 0, "Memory allocation failed\n"); |
| 487 | } |
| 488 | |
| 489 | /** |
| 490 | * xmlRngPErr: |
| 491 | * @ctxt: a Relax-NG parser context |
| 492 | * @node: the node raising the error |
| 493 | * @error: the error code |
| 494 | * @msg: message |
| 495 | * @str1: extra info |
| 496 | * @str2: extra info |
| 497 | * |
| 498 | * Handle a Relax NG Parsing error |
| 499 | */ |
| 500 | static void |
| 501 | xmlRngPErr(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, int error, |
| 502 | const char *msg, const xmlChar * str1, const xmlChar * str2) |
| 503 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 504 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 505 | xmlGenericErrorFunc channel = NULL; |
| 506 | void *data = NULL; |
| 507 | |
| 508 | if (ctxt != NULL) { |
| 509 | channel = ctxt->error; |
| 510 | data = ctxt->userData; |
| 511 | ctxt->nbErrors++; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 512 | schannel = ctxt->serror; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 513 | } |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 514 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 515 | NULL, node, XML_FROM_RELAXNGP, |
| 516 | error, XML_ERR_ERROR, NULL, 0, |
| 517 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
| 518 | msg, str1, str2); |
| 519 | } |
| 520 | |
| 521 | /** |
| 522 | * xmlRngVErr: |
| 523 | * @ctxt: a Relax-NG validation context |
| 524 | * @node: the node raising the error |
| 525 | * @error: the error code |
| 526 | * @msg: message |
| 527 | * @str1: extra info |
| 528 | * @str2: extra info |
| 529 | * |
| 530 | * Handle a Relax NG Validation error |
| 531 | */ |
| 532 | static void |
| 533 | xmlRngVErr(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node, int error, |
| 534 | const char *msg, const xmlChar * str1, const xmlChar * str2) |
| 535 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 536 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 537 | xmlGenericErrorFunc channel = NULL; |
| 538 | void *data = NULL; |
| 539 | |
| 540 | if (ctxt != NULL) { |
| 541 | channel = ctxt->error; |
| 542 | data = ctxt->userData; |
| 543 | ctxt->nbErrors++; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 544 | schannel = ctxt->serror; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 545 | } |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 546 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 547 | NULL, node, XML_FROM_RELAXNGV, |
| 548 | error, XML_ERR_ERROR, NULL, 0, |
| 549 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
| 550 | msg, str1, str2); |
| 551 | } |
| 552 | |
| 553 | /************************************************************************ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 554 | * * |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 555 | * Preliminary type checking interfaces * |
| 556 | * * |
| 557 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 558 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 559 | /** |
| 560 | * xmlRelaxNGTypeHave: |
| 561 | * @data: data needed for the library |
| 562 | * @type: the type name |
| 563 | * @value: the value to check |
| 564 | * |
| 565 | * Function provided by a type library to check if a type is exported |
| 566 | * |
| 567 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 568 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 569 | typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar * type); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 570 | |
| 571 | /** |
| 572 | * xmlRelaxNGTypeCheck: |
| 573 | * @data: data needed for the library |
| 574 | * @type: the type name |
| 575 | * @value: the value to check |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 576 | * @result: place to store the result if needed |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 577 | * |
| 578 | * Function provided by a type library to check if a value match a type |
| 579 | * |
| 580 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 581 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 582 | typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar * type, |
| 583 | const xmlChar * value, void **result, |
| 584 | xmlNodePtr node); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 585 | |
| 586 | /** |
| 587 | * xmlRelaxNGFacetCheck: |
| 588 | * @data: data needed for the library |
| 589 | * @type: the type name |
| 590 | * @facet: the facet name |
| 591 | * @val: the facet value |
| 592 | * @strval: the string value |
| 593 | * @value: the value to check |
| 594 | * |
| 595 | * Function provided by a type library to check a value facet |
| 596 | * |
| 597 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 598 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 599 | typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar * type, |
| 600 | const xmlChar * facet, |
| 601 | const xmlChar * val, |
| 602 | const xmlChar * strval, void *value); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 603 | |
| 604 | /** |
| 605 | * xmlRelaxNGTypeFree: |
| 606 | * @data: data needed for the library |
| 607 | * @result: the value to free |
| 608 | * |
| 609 | * Function provided by a type library to free a returned result |
| 610 | */ |
| 611 | typedef void (*xmlRelaxNGTypeFree) (void *data, void *result); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 612 | |
| 613 | /** |
| 614 | * xmlRelaxNGTypeCompare: |
| 615 | * @data: data needed for the library |
| 616 | * @type: the type name |
| 617 | * @value1: the first value |
| 618 | * @value2: the second value |
| 619 | * |
| 620 | * Function provided by a type library to compare two values accordingly |
| 621 | * to a type. |
| 622 | * |
| 623 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 624 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 625 | typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar * type, |
| 626 | const xmlChar * value1, |
| 627 | xmlNodePtr ctxt1, |
| 628 | void *comp1, |
| 629 | const xmlChar * value2, |
| 630 | xmlNodePtr ctxt2); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 631 | typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary; |
| 632 | typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr; |
| 633 | struct _xmlRelaxNGTypeLibrary { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 634 | const xmlChar *namespace; /* the datatypeLibrary value */ |
| 635 | void *data; /* data needed for the library */ |
| 636 | xmlRelaxNGTypeHave have; /* the export function */ |
| 637 | xmlRelaxNGTypeCheck check; /* the checking function */ |
| 638 | xmlRelaxNGTypeCompare comp; /* the compare function */ |
| 639 | xmlRelaxNGFacetCheck facet; /* the facet check function */ |
| 640 | xmlRelaxNGTypeFree freef; /* the freeing function */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 641 | }; |
| 642 | |
| 643 | /************************************************************************ |
| 644 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 645 | * Allocation functions * |
| 646 | * * |
| 647 | ************************************************************************/ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 648 | static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar); |
| 649 | static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 650 | static void xmlRelaxNGNormExtSpace(xmlChar * value); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 651 | static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 652 | static int xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt |
| 653 | ATTRIBUTE_UNUSED, |
| 654 | xmlRelaxNGValidStatePtr state1, |
| 655 | xmlRelaxNGValidStatePtr state2); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 656 | static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 657 | xmlRelaxNGValidStatePtr state); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 658 | |
| 659 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 660 | * xmlRelaxNGFreeDocument: |
| 661 | * @docu: a document structure |
| 662 | * |
| 663 | * Deallocate a RelaxNG document structure. |
| 664 | */ |
| 665 | static void |
| 666 | xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu) |
| 667 | { |
| 668 | if (docu == NULL) |
| 669 | return; |
| 670 | |
| 671 | if (docu->href != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 672 | xmlFree(docu->href); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 673 | if (docu->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 674 | xmlFreeDoc(docu->doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 675 | if (docu->schema != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 676 | xmlRelaxNGFreeInnerSchema(docu->schema); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 677 | xmlFree(docu); |
| 678 | } |
| 679 | |
| 680 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 681 | * xmlRelaxNGFreeDocumentList: |
| 682 | * @docu: a list of document structure |
| 683 | * |
| 684 | * Deallocate a RelaxNG document structures. |
| 685 | */ |
| 686 | static void |
| 687 | xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu) |
| 688 | { |
| 689 | xmlRelaxNGDocumentPtr next; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 690 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 691 | while (docu != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 692 | next = docu->next; |
| 693 | xmlRelaxNGFreeDocument(docu); |
| 694 | docu = next; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 695 | } |
| 696 | } |
| 697 | |
| 698 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 699 | * xmlRelaxNGFreeInclude: |
| 700 | * @incl: a include structure |
| 701 | * |
| 702 | * Deallocate a RelaxNG include structure. |
| 703 | */ |
| 704 | static void |
| 705 | xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl) |
| 706 | { |
| 707 | if (incl == NULL) |
| 708 | return; |
| 709 | |
| 710 | if (incl->href != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 711 | xmlFree(incl->href); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 712 | if (incl->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 713 | xmlFreeDoc(incl->doc); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 714 | if (incl->schema != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 715 | xmlRelaxNGFree(incl->schema); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 716 | xmlFree(incl); |
| 717 | } |
| 718 | |
| 719 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 720 | * xmlRelaxNGFreeIncludeList: |
| 721 | * @incl: a include structure list |
| 722 | * |
| 723 | * Deallocate a RelaxNG include structure. |
| 724 | */ |
| 725 | static void |
| 726 | xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl) |
| 727 | { |
| 728 | xmlRelaxNGIncludePtr next; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 729 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 730 | while (incl != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 731 | next = incl->next; |
| 732 | xmlRelaxNGFreeInclude(incl); |
| 733 | incl = next; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 734 | } |
| 735 | } |
| 736 | |
| 737 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 738 | * xmlRelaxNGNewRelaxNG: |
| 739 | * @ctxt: a Relax-NG validation context (optional) |
| 740 | * |
| 741 | * Allocate a new RelaxNG structure. |
| 742 | * |
| 743 | * Returns the newly allocated structure or NULL in case or error |
| 744 | */ |
| 745 | static xmlRelaxNGPtr |
| 746 | xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt) |
| 747 | { |
| 748 | xmlRelaxNGPtr ret; |
| 749 | |
| 750 | ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG)); |
| 751 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 752 | xmlRngPErrMemory(ctxt, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 753 | return (NULL); |
| 754 | } |
| 755 | memset(ret, 0, sizeof(xmlRelaxNG)); |
| 756 | |
| 757 | return (ret); |
| 758 | } |
| 759 | |
| 760 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 761 | * xmlRelaxNGFreeInnerSchema: |
| 762 | * @schema: a schema structure |
| 763 | * |
| 764 | * Deallocate a RelaxNG schema structure. |
| 765 | */ |
| 766 | static void |
| 767 | xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema) |
| 768 | { |
| 769 | if (schema == NULL) |
| 770 | return; |
| 771 | |
| 772 | if (schema->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 773 | xmlFreeDoc(schema->doc); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 774 | if (schema->defTab != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 775 | int i; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 776 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 777 | for (i = 0; i < schema->defNr; i++) |
| 778 | xmlRelaxNGFreeDefine(schema->defTab[i]); |
| 779 | xmlFree(schema->defTab); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | xmlFree(schema); |
| 783 | } |
| 784 | |
| 785 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 786 | * xmlRelaxNGFree: |
| 787 | * @schema: a schema structure |
| 788 | * |
| 789 | * Deallocate a RelaxNG structure. |
| 790 | */ |
| 791 | void |
| 792 | xmlRelaxNGFree(xmlRelaxNGPtr schema) |
| 793 | { |
| 794 | if (schema == NULL) |
| 795 | return; |
| 796 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 797 | if (schema->topgrammar != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 798 | xmlRelaxNGFreeGrammar(schema->topgrammar); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 799 | if (schema->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 800 | xmlFreeDoc(schema->doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 801 | if (schema->documents != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 802 | xmlRelaxNGFreeDocumentList(schema->documents); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 803 | if (schema->includes != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 804 | xmlRelaxNGFreeIncludeList(schema->includes); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 805 | if (schema->defTab != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 806 | int i; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 807 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 808 | for (i = 0; i < schema->defNr; i++) |
| 809 | xmlRelaxNGFreeDefine(schema->defTab[i]); |
| 810 | xmlFree(schema->defTab); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 811 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 812 | |
| 813 | xmlFree(schema); |
| 814 | } |
| 815 | |
| 816 | /** |
| 817 | * xmlRelaxNGNewGrammar: |
| 818 | * @ctxt: a Relax-NG validation context (optional) |
| 819 | * |
| 820 | * Allocate a new RelaxNG grammar. |
| 821 | * |
| 822 | * Returns the newly allocated structure or NULL in case or error |
| 823 | */ |
| 824 | static xmlRelaxNGGrammarPtr |
| 825 | xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt) |
| 826 | { |
| 827 | xmlRelaxNGGrammarPtr ret; |
| 828 | |
| 829 | ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar)); |
| 830 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 831 | xmlRngPErrMemory(ctxt, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 832 | return (NULL); |
| 833 | } |
| 834 | memset(ret, 0, sizeof(xmlRelaxNGGrammar)); |
| 835 | |
| 836 | return (ret); |
| 837 | } |
| 838 | |
| 839 | /** |
| 840 | * xmlRelaxNGFreeGrammar: |
| 841 | * @grammar: a grammar structure |
| 842 | * |
| 843 | * Deallocate a RelaxNG grammar structure. |
| 844 | */ |
| 845 | static void |
| 846 | xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar) |
| 847 | { |
| 848 | if (grammar == NULL) |
| 849 | return; |
| 850 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 851 | if (grammar->children != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 852 | xmlRelaxNGFreeGrammar(grammar->children); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 853 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 854 | if (grammar->next != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 855 | xmlRelaxNGFreeGrammar(grammar->next); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 856 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 857 | if (grammar->refs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 858 | xmlHashFree(grammar->refs, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 859 | } |
| 860 | if (grammar->defs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 861 | xmlHashFree(grammar->defs, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | xmlFree(grammar); |
| 865 | } |
| 866 | |
| 867 | /** |
| 868 | * xmlRelaxNGNewDefine: |
| 869 | * @ctxt: a Relax-NG validation context |
| 870 | * @node: the node in the input document. |
| 871 | * |
| 872 | * Allocate a new RelaxNG define. |
| 873 | * |
| 874 | * Returns the newly allocated structure or NULL in case or error |
| 875 | */ |
| 876 | static xmlRelaxNGDefinePtr |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 877 | xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 878 | { |
| 879 | xmlRelaxNGDefinePtr ret; |
| 880 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 881 | if (ctxt->defMax == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 882 | ctxt->defMax = 16; |
| 883 | ctxt->defNr = 0; |
| 884 | ctxt->defTab = (xmlRelaxNGDefinePtr *) |
| 885 | xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr)); |
| 886 | if (ctxt->defTab == NULL) { |
| 887 | xmlRngPErrMemory(ctxt, "allocating define\n"); |
| 888 | return (NULL); |
| 889 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 890 | } else if (ctxt->defMax <= ctxt->defNr) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 891 | xmlRelaxNGDefinePtr *tmp; |
| 892 | |
| 893 | ctxt->defMax *= 2; |
| 894 | tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab, |
| 895 | ctxt->defMax * |
| 896 | sizeof |
| 897 | (xmlRelaxNGDefinePtr)); |
| 898 | if (tmp == NULL) { |
| 899 | xmlRngPErrMemory(ctxt, "allocating define\n"); |
| 900 | return (NULL); |
| 901 | } |
| 902 | ctxt->defTab = tmp; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 903 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 904 | ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine)); |
| 905 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 906 | xmlRngPErrMemory(ctxt, "allocating define\n"); |
| 907 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 908 | } |
| 909 | memset(ret, 0, sizeof(xmlRelaxNGDefine)); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 910 | ctxt->defTab[ctxt->defNr++] = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 911 | ret->node = node; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 912 | ret->depth = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 913 | return (ret); |
| 914 | } |
| 915 | |
| 916 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 917 | * xmlRelaxNGFreePartition: |
| 918 | * @partitions: a partition set structure |
| 919 | * |
| 920 | * Deallocate RelaxNG partition set structures. |
| 921 | */ |
| 922 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 923 | xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions) |
| 924 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 925 | xmlRelaxNGInterleaveGroupPtr group; |
| 926 | int j; |
| 927 | |
| 928 | if (partitions != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 929 | if (partitions->groups != NULL) { |
| 930 | for (j = 0; j < partitions->nbgroups; j++) { |
| 931 | group = partitions->groups[j]; |
| 932 | if (group != NULL) { |
| 933 | if (group->defs != NULL) |
| 934 | xmlFree(group->defs); |
| 935 | if (group->attrs != NULL) |
| 936 | xmlFree(group->attrs); |
| 937 | xmlFree(group); |
| 938 | } |
| 939 | } |
| 940 | xmlFree(partitions->groups); |
| 941 | } |
| 942 | if (partitions->triage != NULL) { |
| 943 | xmlHashFree(partitions->triage, NULL); |
| 944 | } |
| 945 | xmlFree(partitions); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 946 | } |
| 947 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 948 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 949 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 950 | * xmlRelaxNGFreeDefine: |
| 951 | * @define: a define structure |
| 952 | * |
| 953 | * Deallocate a RelaxNG define structure. |
| 954 | */ |
| 955 | static void |
| 956 | xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define) |
| 957 | { |
| 958 | if (define == NULL) |
| 959 | return; |
| 960 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 961 | if ((define->type == XML_RELAXNG_VALUE) && (define->attrs != NULL)) { |
| 962 | xmlRelaxNGTypeLibraryPtr lib; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 963 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 964 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
| 965 | if ((lib != NULL) && (lib->freef != NULL)) |
| 966 | lib->freef(lib->data, (void *) define->attrs); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 967 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 968 | if ((define->data != NULL) && (define->type == XML_RELAXNG_INTERLEAVE)) |
| 969 | xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data); |
| 970 | if ((define->data != NULL) && (define->type == XML_RELAXNG_CHOICE)) |
| 971 | xmlHashFree((xmlHashTablePtr) define->data, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 972 | if (define->name != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 973 | xmlFree(define->name); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 974 | if (define->ns != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 975 | xmlFree(define->ns); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 976 | if (define->value != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 977 | xmlFree(define->value); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 978 | if (define->contModel != NULL) |
| 979 | xmlRegFreeRegexp(define->contModel); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 980 | xmlFree(define); |
| 981 | } |
| 982 | |
| 983 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 984 | * xmlRelaxNGNewStates: |
| 985 | * @ctxt: a Relax-NG validation context |
| 986 | * @size: the default size for the container |
| 987 | * |
| 988 | * Allocate a new RelaxNG validation state container |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 989 | * |
| 990 | * Returns the newly allocated structure or NULL in case or error |
| 991 | */ |
| 992 | static xmlRelaxNGStatesPtr |
| 993 | xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size) |
| 994 | { |
| 995 | xmlRelaxNGStatesPtr ret; |
| 996 | |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 997 | if ((ctxt != NULL) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 998 | (ctxt->freeState != NULL) && (ctxt->freeStatesNr > 0)) { |
| 999 | ctxt->freeStatesNr--; |
| 1000 | ret = ctxt->freeStates[ctxt->freeStatesNr]; |
| 1001 | ret->nbState = 0; |
| 1002 | return (ret); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1003 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1004 | if (size < 16) |
| 1005 | size = 16; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1006 | |
| 1007 | ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) + |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1008 | (size - |
| 1009 | 1) * |
| 1010 | sizeof(xmlRelaxNGValidStatePtr)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1011 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1012 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1013 | return (NULL); |
| 1014 | } |
| 1015 | ret->nbState = 0; |
| 1016 | ret->maxState = size; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1017 | ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc((size) * |
| 1018 | sizeof |
| 1019 | (xmlRelaxNGValidStatePtr)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1020 | if (ret->tabState == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1021 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1022 | xmlFree(ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1023 | return (NULL); |
| 1024 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1025 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | /** |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1029 | * xmlRelaxNGAddStateUniq: |
| 1030 | * @ctxt: a Relax-NG validation context |
| 1031 | * @states: the states container |
| 1032 | * @state: the validation state |
| 1033 | * |
| 1034 | * Add a RelaxNG validation state to the container without checking |
| 1035 | * for unicity. |
| 1036 | * |
| 1037 | * Return 1 in case of success and 0 if this is a duplicate and -1 on error |
| 1038 | */ |
| 1039 | static int |
| 1040 | xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1041 | xmlRelaxNGStatesPtr states, |
| 1042 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1043 | { |
| 1044 | if (state == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1045 | return (-1); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1046 | } |
| 1047 | if (states->nbState >= states->maxState) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1048 | xmlRelaxNGValidStatePtr *tmp; |
| 1049 | int size; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1050 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1051 | size = states->maxState * 2; |
| 1052 | tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState, |
| 1053 | (size) * |
| 1054 | sizeof |
| 1055 | (xmlRelaxNGValidStatePtr)); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1056 | if (tmp == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1057 | xmlRngVErrMemory(ctxt, "adding states\n"); |
| 1058 | return (-1); |
| 1059 | } |
| 1060 | states->tabState = tmp; |
| 1061 | states->maxState = size; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1062 | } |
| 1063 | states->tabState[states->nbState++] = state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1064 | return (1); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1068 | * xmlRelaxNGAddState: |
| 1069 | * @ctxt: a Relax-NG validation context |
| 1070 | * @states: the states container |
| 1071 | * @state: the validation state |
| 1072 | * |
| 1073 | * Add a RelaxNG validation state to the container |
| 1074 | * |
| 1075 | * Return 1 in case of success and 0 if this is a duplicate and -1 on error |
| 1076 | */ |
| 1077 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1078 | xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt, |
| 1079 | xmlRelaxNGStatesPtr states, |
| 1080 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1081 | { |
| 1082 | int i; |
| 1083 | |
| 1084 | if (state == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1085 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1086 | } |
| 1087 | if (states->nbState >= states->maxState) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1088 | xmlRelaxNGValidStatePtr *tmp; |
| 1089 | int size; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1090 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1091 | size = states->maxState * 2; |
| 1092 | tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState, |
| 1093 | (size) * |
| 1094 | sizeof |
| 1095 | (xmlRelaxNGValidStatePtr)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1096 | if (tmp == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1097 | xmlRngVErrMemory(ctxt, "adding states\n"); |
| 1098 | return (-1); |
| 1099 | } |
| 1100 | states->tabState = tmp; |
| 1101 | states->maxState = size; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1102 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1103 | for (i = 0; i < states->nbState; i++) { |
| 1104 | if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) { |
| 1105 | xmlRelaxNGFreeValidState(ctxt, state); |
| 1106 | return (0); |
| 1107 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1108 | } |
| 1109 | states->tabState[states->nbState++] = state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1110 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | /** |
| 1114 | * xmlRelaxNGFreeStates: |
| 1115 | * @ctxt: a Relax-NG validation context |
| 1116 | * @states: teh container |
| 1117 | * |
| 1118 | * Free a RelaxNG validation state container |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1119 | */ |
| 1120 | static void |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1121 | xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1122 | xmlRelaxNGStatesPtr states) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1123 | { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1124 | if (states == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1125 | return; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1126 | if ((ctxt != NULL) && (ctxt->freeStates == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1127 | ctxt->freeStatesMax = 40; |
| 1128 | ctxt->freeStatesNr = 0; |
| 1129 | ctxt->freeStates = (xmlRelaxNGStatesPtr *) |
| 1130 | xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr)); |
| 1131 | if (ctxt->freeStates == NULL) { |
| 1132 | xmlRngVErrMemory(ctxt, "storing states\n"); |
| 1133 | } |
| 1134 | } else if ((ctxt != NULL) |
| 1135 | && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) { |
| 1136 | xmlRelaxNGStatesPtr *tmp; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1137 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1138 | tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates, |
| 1139 | 2 * ctxt->freeStatesMax * |
| 1140 | sizeof |
| 1141 | (xmlRelaxNGStatesPtr)); |
| 1142 | if (tmp == NULL) { |
| 1143 | xmlRngVErrMemory(ctxt, "storing states\n"); |
| 1144 | xmlFree(states->tabState); |
| 1145 | xmlFree(states); |
| 1146 | return; |
| 1147 | } |
| 1148 | ctxt->freeStates = tmp; |
| 1149 | ctxt->freeStatesMax *= 2; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1150 | } |
| 1151 | if ((ctxt == NULL) || (ctxt->freeState == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1152 | xmlFree(states->tabState); |
| 1153 | xmlFree(states); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1154 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1155 | ctxt->freeStates[ctxt->freeStatesNr++] = states; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1160 | * xmlRelaxNGNewValidState: |
| 1161 | * @ctxt: a Relax-NG validation context |
| 1162 | * @node: the current node or NULL for the document |
| 1163 | * |
| 1164 | * Allocate a new RelaxNG validation state |
| 1165 | * |
| 1166 | * Returns the newly allocated structure or NULL in case or error |
| 1167 | */ |
| 1168 | static xmlRelaxNGValidStatePtr |
| 1169 | xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node) |
| 1170 | { |
| 1171 | xmlRelaxNGValidStatePtr ret; |
| 1172 | xmlAttrPtr attr; |
| 1173 | xmlAttrPtr attrs[MAX_ATTR]; |
| 1174 | int nbAttrs = 0; |
| 1175 | xmlNodePtr root = NULL; |
| 1176 | |
| 1177 | if (node == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1178 | root = xmlDocGetRootElement(ctxt->doc); |
| 1179 | if (root == NULL) |
| 1180 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1181 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1182 | attr = node->properties; |
| 1183 | while (attr != NULL) { |
| 1184 | if (nbAttrs < MAX_ATTR) |
| 1185 | attrs[nbAttrs++] = attr; |
| 1186 | else |
| 1187 | nbAttrs++; |
| 1188 | attr = attr->next; |
| 1189 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1190 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1191 | if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) { |
| 1192 | ctxt->freeState->nbState--; |
| 1193 | ret = ctxt->freeState->tabState[ctxt->freeState->nbState]; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1194 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1195 | ret = |
| 1196 | (xmlRelaxNGValidStatePtr) |
| 1197 | xmlMalloc(sizeof(xmlRelaxNGValidState)); |
| 1198 | if (ret == NULL) { |
| 1199 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1200 | return (NULL); |
| 1201 | } |
| 1202 | memset(ret, 0, sizeof(xmlRelaxNGValidState)); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1203 | } |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 1204 | ret->value = NULL; |
| 1205 | ret->endvalue = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1206 | if (node == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1207 | ret->node = (xmlNodePtr) ctxt->doc; |
| 1208 | ret->seq = root; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1209 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1210 | ret->node = node; |
| 1211 | ret->seq = node->children; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1212 | } |
| 1213 | ret->nbAttrs = 0; |
| 1214 | if (nbAttrs > 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1215 | if (ret->attrs == NULL) { |
| 1216 | if (nbAttrs < 4) |
| 1217 | ret->maxAttrs = 4; |
| 1218 | else |
| 1219 | ret->maxAttrs = nbAttrs; |
| 1220 | ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs * |
| 1221 | sizeof(xmlAttrPtr)); |
| 1222 | if (ret->attrs == NULL) { |
| 1223 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1224 | return (ret); |
| 1225 | } |
| 1226 | } else if (ret->maxAttrs < nbAttrs) { |
| 1227 | xmlAttrPtr *tmp; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1228 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1229 | tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs * |
| 1230 | sizeof(xmlAttrPtr)); |
| 1231 | if (tmp == NULL) { |
| 1232 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1233 | return (ret); |
| 1234 | } |
| 1235 | ret->attrs = tmp; |
| 1236 | ret->maxAttrs = nbAttrs; |
| 1237 | } |
| 1238 | ret->nbAttrs = nbAttrs; |
| 1239 | if (nbAttrs < MAX_ATTR) { |
| 1240 | memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs); |
| 1241 | } else { |
| 1242 | attr = node->properties; |
| 1243 | nbAttrs = 0; |
| 1244 | while (attr != NULL) { |
| 1245 | ret->attrs[nbAttrs++] = attr; |
| 1246 | attr = attr->next; |
| 1247 | } |
| 1248 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1249 | } |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 1250 | ret->nbAttrLeft = ret->nbAttrs; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1251 | return (ret); |
| 1252 | } |
| 1253 | |
| 1254 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1255 | * xmlRelaxNGCopyValidState: |
| 1256 | * @ctxt: a Relax-NG validation context |
| 1257 | * @state: a validation state |
| 1258 | * |
| 1259 | * Copy the validation state |
| 1260 | * |
| 1261 | * Returns the newly allocated structure or NULL in case or error |
| 1262 | */ |
| 1263 | static xmlRelaxNGValidStatePtr |
| 1264 | xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1265 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1266 | { |
| 1267 | xmlRelaxNGValidStatePtr ret; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1268 | unsigned int maxAttrs; |
| 1269 | xmlAttrPtr *attrs; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1270 | |
| 1271 | if (state == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1272 | return (NULL); |
| 1273 | if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) { |
| 1274 | ctxt->freeState->nbState--; |
| 1275 | ret = ctxt->freeState->tabState[ctxt->freeState->nbState]; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1276 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1277 | ret = |
| 1278 | (xmlRelaxNGValidStatePtr) |
| 1279 | xmlMalloc(sizeof(xmlRelaxNGValidState)); |
| 1280 | if (ret == NULL) { |
| 1281 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1282 | return (NULL); |
| 1283 | } |
| 1284 | memset(ret, 0, sizeof(xmlRelaxNGValidState)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1285 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1286 | attrs = ret->attrs; |
| 1287 | maxAttrs = ret->maxAttrs; |
| 1288 | memcpy(ret, state, sizeof(xmlRelaxNGValidState)); |
| 1289 | ret->attrs = attrs; |
| 1290 | ret->maxAttrs = maxAttrs; |
| 1291 | if (state->nbAttrs > 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1292 | if (ret->attrs == NULL) { |
| 1293 | ret->maxAttrs = state->maxAttrs; |
| 1294 | ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs * |
| 1295 | sizeof(xmlAttrPtr)); |
| 1296 | if (ret->attrs == NULL) { |
| 1297 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1298 | ret->nbAttrs = 0; |
| 1299 | return (ret); |
| 1300 | } |
| 1301 | } else if (ret->maxAttrs < state->nbAttrs) { |
| 1302 | xmlAttrPtr *tmp; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1303 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1304 | tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs * |
| 1305 | sizeof(xmlAttrPtr)); |
| 1306 | if (tmp == NULL) { |
| 1307 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1308 | ret->nbAttrs = 0; |
| 1309 | return (ret); |
| 1310 | } |
| 1311 | ret->maxAttrs = state->maxAttrs; |
| 1312 | ret->attrs = tmp; |
| 1313 | } |
| 1314 | memcpy(ret->attrs, state->attrs, |
| 1315 | state->nbAttrs * sizeof(xmlAttrPtr)); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1316 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1317 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | /** |
| 1321 | * xmlRelaxNGEqualValidState: |
| 1322 | * @ctxt: a Relax-NG validation context |
| 1323 | * @state1: a validation state |
| 1324 | * @state2: a validation state |
| 1325 | * |
| 1326 | * Compare the validation states for equality |
| 1327 | * |
| 1328 | * Returns 1 if equald, 0 otherwise |
| 1329 | */ |
| 1330 | static int |
| 1331 | xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1332 | xmlRelaxNGValidStatePtr state1, |
| 1333 | xmlRelaxNGValidStatePtr state2) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1334 | { |
| 1335 | int i; |
| 1336 | |
| 1337 | if ((state1 == NULL) || (state2 == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1338 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1339 | if (state1 == state2) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1340 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1341 | if (state1->node != state2->node) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1342 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1343 | if (state1->seq != state2->seq) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1344 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1345 | if (state1->nbAttrLeft != state2->nbAttrLeft) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1346 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1347 | if (state1->nbAttrs != state2->nbAttrs) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1348 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1349 | if (state1->endvalue != state2->endvalue) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1350 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1351 | if ((state1->value != state2->value) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1352 | (!xmlStrEqual(state1->value, state2->value))) |
| 1353 | return (0); |
| 1354 | for (i = 0; i < state1->nbAttrs; i++) { |
| 1355 | if (state1->attrs[i] != state2->attrs[i]) |
| 1356 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1357 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1358 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
| 1361 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1362 | * xmlRelaxNGFreeValidState: |
| 1363 | * @state: a validation state structure |
| 1364 | * |
| 1365 | * Deallocate a RelaxNG validation state structure. |
| 1366 | */ |
| 1367 | static void |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1368 | xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1369 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1370 | { |
| 1371 | if (state == NULL) |
| 1372 | return; |
| 1373 | |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1374 | if ((ctxt != NULL) && (ctxt->freeState == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1375 | ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1376 | } |
| 1377 | if ((ctxt == NULL) || (ctxt->freeState == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1378 | if (state->attrs != NULL) |
| 1379 | xmlFree(state->attrs); |
| 1380 | xmlFree(state); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1381 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1382 | xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1383 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
| 1386 | /************************************************************************ |
| 1387 | * * |
Daniel Veillard | 03c2f0a | 2004-01-25 19:54:59 +0000 | [diff] [blame] | 1388 | * Semi internal functions * |
| 1389 | * * |
| 1390 | ************************************************************************/ |
| 1391 | |
| 1392 | /** |
| 1393 | * xmlRelaxParserSetFlag: |
| 1394 | * @ctxt: a RelaxNG parser context |
| 1395 | * @flags: a set of flags values |
| 1396 | * |
| 1397 | * Semi private function used to pass informations to a parser context |
| 1398 | * which are a combination of xmlRelaxNGParserFlag . |
| 1399 | * |
| 1400 | * Returns 0 if success and -1 in case of error |
| 1401 | */ |
| 1402 | int |
| 1403 | xmlRelaxParserSetFlag(xmlRelaxNGParserCtxtPtr ctxt, int flags) |
| 1404 | { |
| 1405 | if (ctxt == NULL) return(-1); |
| 1406 | if (flags & XML_RELAXNGP_FREE_DOC) { |
| 1407 | ctxt->crng |= XML_RELAXNGP_FREE_DOC; |
| 1408 | flags -= XML_RELAXNGP_FREE_DOC; |
| 1409 | } |
| 1410 | if (flags & XML_RELAXNGP_CRNG) { |
| 1411 | ctxt->crng |= XML_RELAXNGP_CRNG; |
| 1412 | flags -= XML_RELAXNGP_CRNG; |
| 1413 | } |
| 1414 | if (flags != 0) return(-1); |
| 1415 | return(0); |
| 1416 | } |
| 1417 | |
| 1418 | /************************************************************************ |
| 1419 | * * |
| 1420 | * Document functions * |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1421 | * * |
| 1422 | ************************************************************************/ |
| 1423 | static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1424 | xmlDocPtr doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1425 | |
| 1426 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1427 | * xmlRelaxNGIncludePush: |
| 1428 | * @ctxt: the parser context |
| 1429 | * @value: the element doc |
| 1430 | * |
| 1431 | * Pushes a new include on top of the include stack |
| 1432 | * |
| 1433 | * Returns 0 in case of error, the index in the stack otherwise |
| 1434 | */ |
| 1435 | static int |
| 1436 | xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1437 | xmlRelaxNGIncludePtr value) |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1438 | { |
| 1439 | if (ctxt->incTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1440 | ctxt->incMax = 4; |
| 1441 | ctxt->incNr = 0; |
| 1442 | ctxt->incTab = |
| 1443 | (xmlRelaxNGIncludePtr *) xmlMalloc(ctxt->incMax * |
| 1444 | sizeof(ctxt->incTab[0])); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1445 | if (ctxt->incTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1446 | xmlRngPErrMemory(ctxt, "allocating include\n"); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1447 | return (0); |
| 1448 | } |
| 1449 | } |
| 1450 | if (ctxt->incNr >= ctxt->incMax) { |
| 1451 | ctxt->incMax *= 2; |
| 1452 | ctxt->incTab = |
| 1453 | (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1454 | ctxt->incMax * |
| 1455 | sizeof(ctxt->incTab[0])); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1456 | if (ctxt->incTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1457 | xmlRngPErrMemory(ctxt, "allocating include\n"); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1458 | return (0); |
| 1459 | } |
| 1460 | } |
| 1461 | ctxt->incTab[ctxt->incNr] = value; |
| 1462 | ctxt->inc = value; |
| 1463 | return (ctxt->incNr++); |
| 1464 | } |
| 1465 | |
| 1466 | /** |
| 1467 | * xmlRelaxNGIncludePop: |
| 1468 | * @ctxt: the parser context |
| 1469 | * |
| 1470 | * Pops the top include from the include stack |
| 1471 | * |
| 1472 | * Returns the include just removed |
| 1473 | */ |
| 1474 | static xmlRelaxNGIncludePtr |
| 1475 | xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt) |
| 1476 | { |
| 1477 | xmlRelaxNGIncludePtr ret; |
| 1478 | |
| 1479 | if (ctxt->incNr <= 0) |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1480 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1481 | ctxt->incNr--; |
| 1482 | if (ctxt->incNr > 0) |
| 1483 | ctxt->inc = ctxt->incTab[ctxt->incNr - 1]; |
| 1484 | else |
| 1485 | ctxt->inc = NULL; |
| 1486 | ret = ctxt->incTab[ctxt->incNr]; |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1487 | ctxt->incTab[ctxt->incNr] = NULL; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1488 | return (ret); |
| 1489 | } |
| 1490 | |
| 1491 | /** |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1492 | * xmlRelaxNGRemoveRedefine: |
| 1493 | * @ctxt: the parser context |
| 1494 | * @URL: the normalized URL |
| 1495 | * @target: the included target |
| 1496 | * @name: the define name to eliminate |
| 1497 | * |
| 1498 | * Applies the elimination algorithm of 4.7 |
| 1499 | * |
| 1500 | * Returns 0 in case of error, 1 in case of success. |
| 1501 | */ |
| 1502 | static int |
| 1503 | xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1504 | const xmlChar * URL ATTRIBUTE_UNUSED, |
| 1505 | xmlNodePtr target, const xmlChar * name) |
| 1506 | { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1507 | int found = 0; |
| 1508 | xmlNodePtr tmp, tmp2; |
| 1509 | xmlChar *name2; |
| 1510 | |
| 1511 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 1512 | if (name == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1513 | xmlGenericError(xmlGenericErrorContext, |
| 1514 | "Elimination of <include> start from %s\n", URL); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 1515 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1516 | xmlGenericError(xmlGenericErrorContext, |
| 1517 | "Elimination of <include> define %s from %s\n", |
| 1518 | name, URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1519 | #endif |
| 1520 | tmp = target; |
| 1521 | while (tmp != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1522 | tmp2 = tmp->next; |
| 1523 | if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) { |
| 1524 | found = 1; |
| 1525 | xmlUnlinkNode(tmp); |
| 1526 | xmlFreeNode(tmp); |
| 1527 | } else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) { |
| 1528 | name2 = xmlGetProp(tmp, BAD_CAST "name"); |
| 1529 | xmlRelaxNGNormExtSpace(name2); |
| 1530 | if (name2 != NULL) { |
| 1531 | if (xmlStrEqual(name, name2)) { |
| 1532 | found = 1; |
| 1533 | xmlUnlinkNode(tmp); |
| 1534 | xmlFreeNode(tmp); |
| 1535 | } |
| 1536 | xmlFree(name2); |
| 1537 | } |
| 1538 | } else if (IS_RELAXNG(tmp, "include")) { |
| 1539 | xmlChar *href = NULL; |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 1540 | xmlRelaxNGDocumentPtr inc = tmp->psvi; |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1541 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1542 | if ((inc != NULL) && (inc->doc != NULL) && |
| 1543 | (inc->doc->children != NULL)) { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1544 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1545 | if (xmlStrEqual |
| 1546 | (inc->doc->children->name, BAD_CAST "grammar")) { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1547 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1548 | href = xmlGetProp(tmp, BAD_CAST "href"); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1549 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1550 | if (xmlRelaxNGRemoveRedefine(ctxt, href, |
| 1551 | inc->doc->children-> |
| 1552 | children, name) == 1) { |
| 1553 | found = 1; |
| 1554 | } |
| 1555 | if (href != NULL) |
| 1556 | xmlFree(href); |
| 1557 | } |
| 1558 | } |
| 1559 | } |
| 1560 | tmp = tmp2; |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1561 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1562 | return (found); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1563 | } |
| 1564 | |
| 1565 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1566 | * xmlRelaxNGLoadInclude: |
| 1567 | * @ctxt: the parser context |
| 1568 | * @URL: the normalized URL |
| 1569 | * @node: the include node. |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1570 | * @ns: the namespace passed from the context. |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1571 | * |
| 1572 | * First lookup if the document is already loaded into the parser context, |
| 1573 | * check against recursion. If not found the resource is loaded and |
| 1574 | * the content is preprocessed before being returned back to the caller. |
| 1575 | * |
| 1576 | * Returns the xmlRelaxNGIncludePtr or NULL in case of error |
| 1577 | */ |
| 1578 | static xmlRelaxNGIncludePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1579 | xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * URL, |
| 1580 | xmlNodePtr node, const xmlChar * ns) |
| 1581 | { |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1582 | xmlRelaxNGIncludePtr ret = NULL; |
| 1583 | xmlDocPtr doc; |
| 1584 | int i; |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1585 | xmlNodePtr root, cur; |
| 1586 | |
| 1587 | #ifdef DEBUG_INCLUDE |
| 1588 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1589 | "xmlRelaxNGLoadInclude(%s)\n", URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1590 | #endif |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1591 | |
| 1592 | /* |
| 1593 | * check against recursion in the stack |
| 1594 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1595 | for (i = 0; i < ctxt->incNr; i++) { |
| 1596 | if (xmlStrEqual(ctxt->incTab[i]->href, URL)) { |
| 1597 | xmlRngPErr(ctxt, NULL, XML_RNGP_INCLUDE_RECURSE, |
| 1598 | "Detected an Include recursion for %s\n", URL, |
| 1599 | NULL); |
| 1600 | return (NULL); |
| 1601 | } |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
| 1604 | /* |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1605 | * load the document |
| 1606 | */ |
Daniel Veillard | 87247e8 | 2004-01-13 20:42:02 +0000 | [diff] [blame] | 1607 | doc = xmlReadFile((const char *) URL,NULL,0); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1608 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1609 | xmlRngPErr(ctxt, node, XML_RNGP_PARSE_ERROR, |
| 1610 | "xmlRelaxNG: could not load %s\n", URL, NULL); |
| 1611 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1612 | } |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1613 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1614 | xmlGenericError(xmlGenericErrorContext, "Parsed %s Okay\n", URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1615 | #endif |
| 1616 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1617 | /* |
| 1618 | * Allocate the document structures and register it first. |
| 1619 | */ |
| 1620 | ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude)); |
| 1621 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1622 | xmlRngPErrMemory(ctxt, "allocating include\n"); |
| 1623 | xmlFreeDoc(doc); |
| 1624 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1625 | } |
| 1626 | memset(ret, 0, sizeof(xmlRelaxNGInclude)); |
| 1627 | ret->doc = doc; |
| 1628 | ret->href = xmlStrdup(URL); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1629 | ret->next = ctxt->includes; |
| 1630 | ctxt->includes = ret; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1631 | |
| 1632 | /* |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1633 | * transmit the ns if needed |
| 1634 | */ |
| 1635 | if (ns != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1636 | root = xmlDocGetRootElement(doc); |
| 1637 | if (root != NULL) { |
| 1638 | if (xmlHasProp(root, BAD_CAST "ns") == NULL) { |
| 1639 | xmlSetProp(root, BAD_CAST "ns", ns); |
| 1640 | } |
| 1641 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1642 | } |
| 1643 | |
| 1644 | /* |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1645 | * push it on the stack |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1646 | */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1647 | xmlRelaxNGIncludePush(ctxt, ret); |
| 1648 | |
| 1649 | /* |
| 1650 | * Some preprocessing of the document content, this include recursing |
| 1651 | * in the include stack. |
| 1652 | */ |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1653 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1654 | xmlGenericError(xmlGenericErrorContext, "cleanup of %s\n", URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1655 | #endif |
| 1656 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1657 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 1658 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1659 | ctxt->inc = NULL; |
| 1660 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1661 | } |
| 1662 | |
| 1663 | /* |
| 1664 | * Pop up the include from the stack |
| 1665 | */ |
| 1666 | xmlRelaxNGIncludePop(ctxt); |
| 1667 | |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1668 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1669 | xmlGenericError(xmlGenericErrorContext, "Checking of %s\n", URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1670 | #endif |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1671 | /* |
| 1672 | * Check that the top element is a grammar |
| 1673 | */ |
| 1674 | root = xmlDocGetRootElement(doc); |
| 1675 | if (root == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1676 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, |
| 1677 | "xmlRelaxNG: included document is empty %s\n", URL, |
| 1678 | NULL); |
| 1679 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1680 | } |
| 1681 | if (!IS_RELAXNG(root, "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1682 | xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING, |
| 1683 | "xmlRelaxNG: included document %s root is not a grammar\n", |
| 1684 | URL, NULL); |
| 1685 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1686 | } |
| 1687 | |
| 1688 | /* |
| 1689 | * Elimination of redefined rules in the include. |
| 1690 | */ |
| 1691 | cur = node->children; |
| 1692 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1693 | if (IS_RELAXNG(cur, "start")) { |
| 1694 | int found = 0; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1695 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1696 | found = |
| 1697 | xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL); |
| 1698 | if (!found) { |
| 1699 | xmlRngPErr(ctxt, node, XML_RNGP_START_MISSING, |
| 1700 | "xmlRelaxNG: include %s has a start but not the included grammar\n", |
| 1701 | URL, NULL); |
| 1702 | } |
| 1703 | } else if (IS_RELAXNG(cur, "define")) { |
| 1704 | xmlChar *name; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1705 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1706 | name = xmlGetProp(cur, BAD_CAST "name"); |
| 1707 | if (name == NULL) { |
| 1708 | xmlRngPErr(ctxt, node, XML_RNGP_NAME_MISSING, |
| 1709 | "xmlRelaxNG: include %s has define without name\n", |
| 1710 | URL, NULL); |
| 1711 | } else { |
| 1712 | int found; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1713 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1714 | xmlRelaxNGNormExtSpace(name); |
| 1715 | found = xmlRelaxNGRemoveRedefine(ctxt, URL, |
| 1716 | root->children, name); |
| 1717 | if (!found) { |
| 1718 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_MISSING, |
| 1719 | "xmlRelaxNG: include %s has a define %s but not the included grammar\n", |
| 1720 | URL, name); |
| 1721 | } |
| 1722 | xmlFree(name); |
| 1723 | } |
| 1724 | } |
| 1725 | cur = cur->next; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1726 | } |
| 1727 | |
| 1728 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1729 | return (ret); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1730 | } |
| 1731 | |
| 1732 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1733 | * xmlRelaxNGValidErrorPush: |
| 1734 | * @ctxt: the validation context |
| 1735 | * @err: the error code |
| 1736 | * @arg1: the first string argument |
| 1737 | * @arg2: the second string argument |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1738 | * @dup: arg need to be duplicated |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1739 | * |
| 1740 | * Pushes a new error on top of the error stack |
| 1741 | * |
| 1742 | * Returns 0 in case of error, the index in the stack otherwise |
| 1743 | */ |
| 1744 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1745 | xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt, |
| 1746 | xmlRelaxNGValidErr err, const xmlChar * arg1, |
| 1747 | const xmlChar * arg2, int dup) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1748 | { |
| 1749 | xmlRelaxNGValidErrorPtr cur; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1750 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 1751 | #ifdef DEBUG_ERROR |
| 1752 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1753 | "Pushing error %d at %d on stack\n", err, ctxt->errNr); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 1754 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1755 | if (ctxt->errTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1756 | ctxt->errMax = 8; |
| 1757 | ctxt->errNr = 0; |
| 1758 | ctxt->errTab = |
| 1759 | (xmlRelaxNGValidErrorPtr) xmlMalloc(ctxt->errMax * |
| 1760 | sizeof |
| 1761 | (xmlRelaxNGValidError)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1762 | if (ctxt->errTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1763 | xmlRngVErrMemory(ctxt, "pushing error\n"); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1764 | return (0); |
| 1765 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1766 | ctxt->err = NULL; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1767 | } |
| 1768 | if (ctxt->errNr >= ctxt->errMax) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1769 | ctxt->errMax *= 2; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1770 | ctxt->errTab = |
| 1771 | (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1772 | ctxt->errMax * |
| 1773 | sizeof |
| 1774 | (xmlRelaxNGValidError)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1775 | if (ctxt->errTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1776 | xmlRngVErrMemory(ctxt, "pushing error\n"); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1777 | return (0); |
| 1778 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1779 | ctxt->err = &ctxt->errTab[ctxt->errNr - 1]; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1780 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 1781 | if ((ctxt->err != NULL) && (ctxt->state != NULL) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1782 | (ctxt->err->node == ctxt->state->node) && (ctxt->err->err == err)) |
| 1783 | return (ctxt->errNr); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1784 | cur = &ctxt->errTab[ctxt->errNr]; |
| 1785 | cur->err = err; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1786 | if (dup) { |
| 1787 | cur->arg1 = xmlStrdup(arg1); |
| 1788 | cur->arg2 = xmlStrdup(arg2); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1789 | cur->flags = ERROR_IS_DUP; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1790 | } else { |
| 1791 | cur->arg1 = arg1; |
| 1792 | cur->arg2 = arg2; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1793 | cur->flags = 0; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1794 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1795 | if (ctxt->state != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1796 | cur->node = ctxt->state->node; |
| 1797 | cur->seq = ctxt->state->seq; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1798 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1799 | cur->node = NULL; |
| 1800 | cur->seq = NULL; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1801 | } |
| 1802 | ctxt->err = cur; |
| 1803 | return (ctxt->errNr++); |
| 1804 | } |
| 1805 | |
| 1806 | /** |
| 1807 | * xmlRelaxNGValidErrorPop: |
| 1808 | * @ctxt: the validation context |
| 1809 | * |
| 1810 | * Pops the top error from the error stack |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1811 | */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1812 | static void |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1813 | xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt) |
| 1814 | { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1815 | xmlRelaxNGValidErrorPtr cur; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1816 | |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1817 | if (ctxt->errNr <= 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1818 | ctxt->err = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1819 | return; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1820 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1821 | ctxt->errNr--; |
| 1822 | if (ctxt->errNr > 0) |
| 1823 | ctxt->err = &ctxt->errTab[ctxt->errNr - 1]; |
| 1824 | else |
| 1825 | ctxt->err = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1826 | cur = &ctxt->errTab[ctxt->errNr]; |
| 1827 | if (cur->flags & ERROR_IS_DUP) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1828 | if (cur->arg1 != NULL) |
| 1829 | xmlFree((xmlChar *) cur->arg1); |
| 1830 | cur->arg1 = NULL; |
| 1831 | if (cur->arg2 != NULL) |
| 1832 | xmlFree((xmlChar *) cur->arg2); |
| 1833 | cur->arg2 = NULL; |
| 1834 | cur->flags = 0; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1835 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1838 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1839 | * xmlRelaxNGDocumentPush: |
| 1840 | * @ctxt: the parser context |
| 1841 | * @value: the element doc |
| 1842 | * |
| 1843 | * Pushes a new doc on top of the doc stack |
| 1844 | * |
| 1845 | * Returns 0 in case of error, the index in the stack otherwise |
| 1846 | */ |
| 1847 | static int |
| 1848 | xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1849 | xmlRelaxNGDocumentPtr value) |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1850 | { |
| 1851 | if (ctxt->docTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1852 | ctxt->docMax = 4; |
| 1853 | ctxt->docNr = 0; |
| 1854 | ctxt->docTab = |
| 1855 | (xmlRelaxNGDocumentPtr *) xmlMalloc(ctxt->docMax * |
| 1856 | sizeof(ctxt->docTab[0])); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1857 | if (ctxt->docTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1858 | xmlRngPErrMemory(ctxt, "adding document\n"); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1859 | return (0); |
| 1860 | } |
| 1861 | } |
| 1862 | if (ctxt->docNr >= ctxt->docMax) { |
| 1863 | ctxt->docMax *= 2; |
| 1864 | ctxt->docTab = |
| 1865 | (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1866 | ctxt->docMax * |
| 1867 | sizeof(ctxt->docTab[0])); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1868 | if (ctxt->docTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1869 | xmlRngPErrMemory(ctxt, "adding document\n"); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1870 | return (0); |
| 1871 | } |
| 1872 | } |
| 1873 | ctxt->docTab[ctxt->docNr] = value; |
| 1874 | ctxt->doc = value; |
| 1875 | return (ctxt->docNr++); |
| 1876 | } |
| 1877 | |
| 1878 | /** |
| 1879 | * xmlRelaxNGDocumentPop: |
| 1880 | * @ctxt: the parser context |
| 1881 | * |
| 1882 | * Pops the top doc from the doc stack |
| 1883 | * |
| 1884 | * Returns the doc just removed |
| 1885 | */ |
| 1886 | static xmlRelaxNGDocumentPtr |
| 1887 | xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt) |
| 1888 | { |
| 1889 | xmlRelaxNGDocumentPtr ret; |
| 1890 | |
| 1891 | if (ctxt->docNr <= 0) |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1892 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1893 | ctxt->docNr--; |
| 1894 | if (ctxt->docNr > 0) |
| 1895 | ctxt->doc = ctxt->docTab[ctxt->docNr - 1]; |
| 1896 | else |
| 1897 | ctxt->doc = NULL; |
| 1898 | ret = ctxt->docTab[ctxt->docNr]; |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1899 | ctxt->docTab[ctxt->docNr] = NULL; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1900 | return (ret); |
| 1901 | } |
| 1902 | |
| 1903 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1904 | * xmlRelaxNGLoadExternalRef: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1905 | * @ctxt: the parser context |
| 1906 | * @URL: the normalized URL |
| 1907 | * @ns: the inherited ns if any |
| 1908 | * |
| 1909 | * First lookup if the document is already loaded into the parser context, |
| 1910 | * check against recursion. If not found the resource is loaded and |
| 1911 | * the content is preprocessed before being returned back to the caller. |
| 1912 | * |
| 1913 | * Returns the xmlRelaxNGDocumentPtr or NULL in case of error |
| 1914 | */ |
| 1915 | static xmlRelaxNGDocumentPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1916 | xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt, |
| 1917 | const xmlChar * URL, const xmlChar * ns) |
| 1918 | { |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1919 | xmlRelaxNGDocumentPtr ret = NULL; |
| 1920 | xmlDocPtr doc; |
| 1921 | xmlNodePtr root; |
| 1922 | int i; |
| 1923 | |
| 1924 | /* |
| 1925 | * check against recursion in the stack |
| 1926 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1927 | for (i = 0; i < ctxt->docNr; i++) { |
| 1928 | if (xmlStrEqual(ctxt->docTab[i]->href, URL)) { |
| 1929 | xmlRngPErr(ctxt, NULL, XML_RNGP_EXTERNALREF_RECURSE, |
| 1930 | "Detected an externalRef recursion for %s\n", URL, |
| 1931 | NULL); |
| 1932 | return (NULL); |
| 1933 | } |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1934 | } |
| 1935 | |
| 1936 | /* |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1937 | * load the document |
| 1938 | */ |
Daniel Veillard | 87247e8 | 2004-01-13 20:42:02 +0000 | [diff] [blame] | 1939 | doc = xmlReadFile((const char *) URL,NULL,0); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1940 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1941 | xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR, |
| 1942 | "xmlRelaxNG: could not load %s\n", URL, NULL); |
| 1943 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1944 | } |
| 1945 | |
| 1946 | /* |
| 1947 | * Allocate the document structures and register it first. |
| 1948 | */ |
| 1949 | ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument)); |
| 1950 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1951 | xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_ERR_NO_MEMORY, |
| 1952 | "xmlRelaxNG: allocate memory for doc %s\n", URL, NULL); |
| 1953 | xmlFreeDoc(doc); |
| 1954 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1955 | } |
| 1956 | memset(ret, 0, sizeof(xmlRelaxNGDocument)); |
| 1957 | ret->doc = doc; |
| 1958 | ret->href = xmlStrdup(URL); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1959 | ret->next = ctxt->documents; |
| 1960 | ctxt->documents = ret; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1961 | |
| 1962 | /* |
| 1963 | * transmit the ns if needed |
| 1964 | */ |
| 1965 | if (ns != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1966 | root = xmlDocGetRootElement(doc); |
| 1967 | if (root != NULL) { |
| 1968 | if (xmlHasProp(root, BAD_CAST "ns") == NULL) { |
| 1969 | xmlSetProp(root, BAD_CAST "ns", ns); |
| 1970 | } |
| 1971 | } |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1972 | } |
| 1973 | |
| 1974 | /* |
| 1975 | * push it on the stack and register it in the hash table |
| 1976 | */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1977 | xmlRelaxNGDocumentPush(ctxt, ret); |
| 1978 | |
| 1979 | /* |
| 1980 | * Some preprocessing of the document content |
| 1981 | */ |
| 1982 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 1983 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1984 | ctxt->doc = NULL; |
| 1985 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1986 | } |
| 1987 | |
| 1988 | xmlRelaxNGDocumentPop(ctxt); |
| 1989 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1990 | return (ret); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1991 | } |
| 1992 | |
| 1993 | /************************************************************************ |
| 1994 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1995 | * Error functions * |
| 1996 | * * |
| 1997 | ************************************************************************/ |
| 1998 | |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1999 | #define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0); |
| 2000 | #define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0); |
| 2001 | #define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0); |
| 2002 | #define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1); |
| 2003 | #define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2004 | |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 2005 | static const char * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2006 | xmlRelaxNGDefName(xmlRelaxNGDefinePtr def) |
| 2007 | { |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 2008 | if (def == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2009 | return ("none"); |
| 2010 | switch (def->type) { |
| 2011 | case XML_RELAXNG_EMPTY: |
| 2012 | return ("empty"); |
| 2013 | case XML_RELAXNG_NOT_ALLOWED: |
| 2014 | return ("notAllowed"); |
| 2015 | case XML_RELAXNG_EXCEPT: |
| 2016 | return ("except"); |
| 2017 | case XML_RELAXNG_TEXT: |
| 2018 | return ("text"); |
| 2019 | case XML_RELAXNG_ELEMENT: |
| 2020 | return ("element"); |
| 2021 | case XML_RELAXNG_DATATYPE: |
| 2022 | return ("datatype"); |
| 2023 | case XML_RELAXNG_VALUE: |
| 2024 | return ("value"); |
| 2025 | case XML_RELAXNG_LIST: |
| 2026 | return ("list"); |
| 2027 | case XML_RELAXNG_ATTRIBUTE: |
| 2028 | return ("attribute"); |
| 2029 | case XML_RELAXNG_DEF: |
| 2030 | return ("def"); |
| 2031 | case XML_RELAXNG_REF: |
| 2032 | return ("ref"); |
| 2033 | case XML_RELAXNG_EXTERNALREF: |
| 2034 | return ("externalRef"); |
| 2035 | case XML_RELAXNG_PARENTREF: |
| 2036 | return ("parentRef"); |
| 2037 | case XML_RELAXNG_OPTIONAL: |
| 2038 | return ("optional"); |
| 2039 | case XML_RELAXNG_ZEROORMORE: |
| 2040 | return ("zeroOrMore"); |
| 2041 | case XML_RELAXNG_ONEORMORE: |
| 2042 | return ("oneOrMore"); |
| 2043 | case XML_RELAXNG_CHOICE: |
| 2044 | return ("choice"); |
| 2045 | case XML_RELAXNG_GROUP: |
| 2046 | return ("group"); |
| 2047 | case XML_RELAXNG_INTERLEAVE: |
| 2048 | return ("interleave"); |
| 2049 | case XML_RELAXNG_START: |
| 2050 | return ("start"); |
| 2051 | case XML_RELAXNG_NOOP: |
| 2052 | return ("noop"); |
| 2053 | case XML_RELAXNG_PARAM: |
| 2054 | return ("param"); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 2055 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2056 | return ("unknown"); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 2057 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 2058 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2059 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2060 | * xmlRelaxNGGetErrorString: |
| 2061 | * @err: the error code |
| 2062 | * @arg1: the first string argument |
| 2063 | * @arg2: the second string argument |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2064 | * |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2065 | * computes a formatted error string for the given error code and args |
| 2066 | * |
| 2067 | * Returns the error string, it must be deallocated by the caller |
| 2068 | */ |
| 2069 | static xmlChar * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2070 | xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar * arg1, |
| 2071 | const xmlChar * arg2) |
| 2072 | { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2073 | char msg[1000]; |
| 2074 | |
| 2075 | if (arg1 == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2076 | arg1 = BAD_CAST ""; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2077 | if (arg2 == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2078 | arg2 = BAD_CAST ""; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2079 | |
| 2080 | msg[0] = 0; |
| 2081 | switch (err) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2082 | case XML_RELAXNG_OK: |
| 2083 | return (NULL); |
| 2084 | case XML_RELAXNG_ERR_MEMORY: |
| 2085 | return (xmlCharStrdup("out of memory\n")); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2086 | case XML_RELAXNG_ERR_TYPE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2087 | snprintf(msg, 1000, "failed to validate type %s\n", arg1); |
| 2088 | break; |
| 2089 | case XML_RELAXNG_ERR_TYPEVAL: |
| 2090 | snprintf(msg, 1000, "Type %s doesn't allow value '%s'\n", arg1, |
| 2091 | arg2); |
| 2092 | break; |
| 2093 | case XML_RELAXNG_ERR_DUPID: |
| 2094 | snprintf(msg, 1000, "ID %s redefined\n", arg1); |
| 2095 | break; |
| 2096 | case XML_RELAXNG_ERR_TYPECMP: |
| 2097 | snprintf(msg, 1000, "failed to compare type %s\n", arg1); |
| 2098 | break; |
| 2099 | case XML_RELAXNG_ERR_NOSTATE: |
| 2100 | return (xmlCharStrdup("Internal error: no state\n")); |
| 2101 | case XML_RELAXNG_ERR_NODEFINE: |
| 2102 | return (xmlCharStrdup("Internal error: no define\n")); |
| 2103 | case XML_RELAXNG_ERR_INTERNAL: |
| 2104 | snprintf(msg, 1000, "Internal error: %s\n", arg1); |
| 2105 | break; |
| 2106 | case XML_RELAXNG_ERR_LISTEXTRA: |
| 2107 | snprintf(msg, 1000, "Extra data in list: %s\n", arg1); |
| 2108 | break; |
| 2109 | case XML_RELAXNG_ERR_INTERNODATA: |
| 2110 | return (xmlCharStrdup |
| 2111 | ("Internal: interleave block has no data\n")); |
| 2112 | case XML_RELAXNG_ERR_INTERSEQ: |
| 2113 | return (xmlCharStrdup("Invalid sequence in interleave\n")); |
| 2114 | case XML_RELAXNG_ERR_INTEREXTRA: |
| 2115 | snprintf(msg, 1000, "Extra element %s in interleave\n", arg1); |
| 2116 | break; |
| 2117 | case XML_RELAXNG_ERR_ELEMNAME: |
| 2118 | snprintf(msg, 1000, "Expecting element %s, got %s\n", arg1, |
| 2119 | arg2); |
| 2120 | break; |
| 2121 | case XML_RELAXNG_ERR_ELEMNONS: |
| 2122 | snprintf(msg, 1000, "Expecting a namespace for element %s\n", |
| 2123 | arg1); |
| 2124 | break; |
| 2125 | case XML_RELAXNG_ERR_ELEMWRONGNS: |
| 2126 | snprintf(msg, 1000, |
| 2127 | "Element %s has wrong namespace: expecting %s\n", arg1, |
| 2128 | arg2); |
| 2129 | break; |
| 2130 | case XML_RELAXNG_ERR_ELEMWRONG: |
| 2131 | snprintf(msg, 1000, "Did not expect element %s there\n", arg1); |
| 2132 | break; |
| 2133 | case XML_RELAXNG_ERR_TEXTWRONG: |
| 2134 | snprintf(msg, 1000, |
| 2135 | "Did not expect text in element %s content\n", arg1); |
| 2136 | break; |
| 2137 | case XML_RELAXNG_ERR_ELEMEXTRANS: |
| 2138 | snprintf(msg, 1000, "Expecting no namespace for element %s\n", |
| 2139 | arg1); |
| 2140 | break; |
| 2141 | case XML_RELAXNG_ERR_ELEMNOTEMPTY: |
| 2142 | snprintf(msg, 1000, "Expecting element %s to be empty\n", arg1); |
| 2143 | break; |
| 2144 | case XML_RELAXNG_ERR_NOELEM: |
| 2145 | snprintf(msg, 1000, "Expecting an element %s, got nothing\n", |
| 2146 | arg1); |
| 2147 | break; |
| 2148 | case XML_RELAXNG_ERR_NOTELEM: |
| 2149 | return (xmlCharStrdup("Expecting an element got text\n")); |
| 2150 | case XML_RELAXNG_ERR_ATTRVALID: |
| 2151 | snprintf(msg, 1000, "Element %s failed to validate attributes\n", |
| 2152 | arg1); |
| 2153 | break; |
| 2154 | case XML_RELAXNG_ERR_CONTENTVALID: |
| 2155 | snprintf(msg, 1000, "Element %s failed to validate content\n", |
| 2156 | arg1); |
| 2157 | break; |
| 2158 | case XML_RELAXNG_ERR_EXTRACONTENT: |
| 2159 | snprintf(msg, 1000, "Element %s has extra content: %s\n", |
| 2160 | arg1, arg2); |
| 2161 | break; |
| 2162 | case XML_RELAXNG_ERR_INVALIDATTR: |
| 2163 | snprintf(msg, 1000, "Invalid attribute %s for element %s\n", |
| 2164 | arg1, arg2); |
| 2165 | break; |
| 2166 | case XML_RELAXNG_ERR_LACKDATA: |
| 2167 | snprintf(msg, 1000, "Datatype element %s contains no data\n", |
| 2168 | arg1); |
| 2169 | break; |
| 2170 | case XML_RELAXNG_ERR_DATAELEM: |
| 2171 | snprintf(msg, 1000, "Datatype element %s has child elements\n", |
| 2172 | arg1); |
| 2173 | break; |
| 2174 | case XML_RELAXNG_ERR_VALELEM: |
| 2175 | snprintf(msg, 1000, "Value element %s has child elements\n", |
| 2176 | arg1); |
| 2177 | break; |
| 2178 | case XML_RELAXNG_ERR_LISTELEM: |
| 2179 | snprintf(msg, 1000, "List element %s has child elements\n", |
| 2180 | arg1); |
| 2181 | break; |
| 2182 | case XML_RELAXNG_ERR_DATATYPE: |
| 2183 | snprintf(msg, 1000, "Error validating datatype %s\n", arg1); |
| 2184 | break; |
| 2185 | case XML_RELAXNG_ERR_VALUE: |
| 2186 | snprintf(msg, 1000, "Error validating value %s\n", arg1); |
| 2187 | break; |
| 2188 | case XML_RELAXNG_ERR_LIST: |
| 2189 | return (xmlCharStrdup("Error validating list\n")); |
| 2190 | case XML_RELAXNG_ERR_NOGRAMMAR: |
| 2191 | return (xmlCharStrdup("No top grammar defined\n")); |
| 2192 | case XML_RELAXNG_ERR_EXTRADATA: |
| 2193 | return (xmlCharStrdup("Extra data in the document\n")); |
| 2194 | default: |
| 2195 | return (xmlCharStrdup("Unknown error !\n")); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2196 | } |
| 2197 | if (msg[0] == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2198 | snprintf(msg, 1000, "Unknown error code %d\n", err); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2199 | } |
Daniel Veillard | adbb0e6 | 2003-05-10 20:02:45 +0000 | [diff] [blame] | 2200 | msg[1000 - 1] = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2201 | return (xmlStrdup((xmlChar *) msg)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2202 | } |
| 2203 | |
| 2204 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2205 | * xmlRelaxNGShowValidError: |
| 2206 | * @ctxt: the validation context |
| 2207 | * @err: the error number |
| 2208 | * @node: the node |
| 2209 | * @child: the node child generating the problem. |
| 2210 | * @arg1: the first argument |
| 2211 | * @arg2: the second argument |
| 2212 | * |
| 2213 | * Show a validation error. |
| 2214 | */ |
| 2215 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2216 | xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt, |
| 2217 | xmlRelaxNGValidErr err, xmlNodePtr node, |
| 2218 | xmlNodePtr child, const xmlChar * arg1, |
| 2219 | const xmlChar * arg2) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2220 | { |
| 2221 | xmlChar *msg; |
| 2222 | |
| 2223 | if (ctxt->error == NULL) |
| 2224 | return; |
| 2225 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2226 | #ifdef DEBUG_ERROR |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2227 | xmlGenericError(xmlGenericErrorContext, "Show error %d\n", err); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2228 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2229 | msg = xmlRelaxNGGetErrorString(err, arg1, arg2); |
| 2230 | if (msg == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2231 | return; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2232 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2233 | if (ctxt->errNo == XML_RELAXNG_OK) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2234 | ctxt->errNo = err; |
| 2235 | xmlRngVErr(ctxt, (child == NULL ? node : child), err, |
| 2236 | (const char *) msg, arg1, arg2); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2237 | xmlFree(msg); |
| 2238 | } |
| 2239 | |
| 2240 | /** |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2241 | * xmlRelaxNGPopErrors: |
| 2242 | * @ctxt: the validation context |
| 2243 | * @level: the error level in the stack |
| 2244 | * |
| 2245 | * pop and discard all errors until the given level is reached |
| 2246 | */ |
| 2247 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2248 | xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level) |
| 2249 | { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2250 | int i; |
| 2251 | xmlRelaxNGValidErrorPtr err; |
| 2252 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2253 | #ifdef DEBUG_ERROR |
| 2254 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2255 | "Pop errors till level %d\n", level); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2256 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2257 | for (i = level; i < ctxt->errNr; i++) { |
| 2258 | err = &ctxt->errTab[i]; |
| 2259 | if (err->flags & ERROR_IS_DUP) { |
| 2260 | if (err->arg1 != NULL) |
| 2261 | xmlFree((xmlChar *) err->arg1); |
| 2262 | err->arg1 = NULL; |
| 2263 | if (err->arg2 != NULL) |
| 2264 | xmlFree((xmlChar *) err->arg2); |
| 2265 | err->arg2 = NULL; |
| 2266 | err->flags = 0; |
| 2267 | } |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2268 | } |
| 2269 | ctxt->errNr = level; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2270 | if (ctxt->errNr <= 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2271 | ctxt->err = NULL; |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2272 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2273 | |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2274 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2275 | * xmlRelaxNGDumpValidError: |
| 2276 | * @ctxt: the validation context |
| 2277 | * |
| 2278 | * Show all validation error over a given index. |
| 2279 | */ |
| 2280 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2281 | xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt) |
| 2282 | { |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 2283 | int i, j, k; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2284 | xmlRelaxNGValidErrorPtr err, dup; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2285 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2286 | #ifdef DEBUG_ERROR |
| 2287 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2288 | "Dumping error stack %d errors\n", ctxt->errNr); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2289 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2290 | for (i = 0, k = 0; i < ctxt->errNr; i++) { |
| 2291 | err = &ctxt->errTab[i]; |
| 2292 | if (k < MAX_ERROR) { |
| 2293 | for (j = 0; j < i; j++) { |
| 2294 | dup = &ctxt->errTab[j]; |
| 2295 | if ((err->err == dup->err) && (err->node == dup->node) && |
| 2296 | (xmlStrEqual(err->arg1, dup->arg1)) && |
| 2297 | (xmlStrEqual(err->arg2, dup->arg2))) { |
| 2298 | goto skip; |
| 2299 | } |
| 2300 | } |
| 2301 | xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq, |
| 2302 | err->arg1, err->arg2); |
| 2303 | k++; |
| 2304 | } |
| 2305 | skip: |
| 2306 | if (err->flags & ERROR_IS_DUP) { |
| 2307 | if (err->arg1 != NULL) |
| 2308 | xmlFree((xmlChar *) err->arg1); |
| 2309 | err->arg1 = NULL; |
| 2310 | if (err->arg2 != NULL) |
| 2311 | xmlFree((xmlChar *) err->arg2); |
| 2312 | err->arg2 = NULL; |
| 2313 | err->flags = 0; |
| 2314 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2315 | } |
| 2316 | ctxt->errNr = 0; |
| 2317 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2318 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2319 | /** |
| 2320 | * xmlRelaxNGAddValidError: |
| 2321 | * @ctxt: the validation context |
| 2322 | * @err: the error number |
| 2323 | * @arg1: the first argument |
| 2324 | * @arg2: the second argument |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2325 | * @dup: need to dup the args |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2326 | * |
| 2327 | * Register a validation error, either generating it if it's sure |
| 2328 | * or stacking it for later handling if unsure. |
| 2329 | */ |
| 2330 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2331 | xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt, |
| 2332 | xmlRelaxNGValidErr err, const xmlChar * arg1, |
| 2333 | const xmlChar * arg2, int dup) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2334 | { |
| 2335 | if ((ctxt == NULL) || (ctxt->error == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2336 | return; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2337 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2338 | #ifdef DEBUG_ERROR |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2339 | xmlGenericError(xmlGenericErrorContext, "Adding error %d\n", err); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2340 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2341 | /* |
| 2342 | * generate the error directly |
| 2343 | */ |
William M. Brack | 6092962 | 2004-03-27 17:54:18 +0000 | [diff] [blame] | 2344 | if (((ctxt->flags & FLAGS_IGNORABLE) == 0) || |
| 2345 | (ctxt->flags & FLAGS_NEGATIVE)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2346 | xmlNodePtr node, seq; |
| 2347 | |
| 2348 | /* |
| 2349 | * Flush first any stacked error which might be the |
| 2350 | * real cause of the problem. |
| 2351 | */ |
| 2352 | if (ctxt->errNr != 0) |
| 2353 | xmlRelaxNGDumpValidError(ctxt); |
| 2354 | if (ctxt->state != NULL) { |
| 2355 | node = ctxt->state->node; |
| 2356 | seq = ctxt->state->seq; |
| 2357 | } else { |
| 2358 | node = seq = NULL; |
| 2359 | } |
| 2360 | xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2361 | } |
| 2362 | /* |
| 2363 | * Stack the error for later processing if needed |
| 2364 | */ |
| 2365 | else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2366 | xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2367 | } |
| 2368 | } |
| 2369 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2370 | |
| 2371 | /************************************************************************ |
| 2372 | * * |
| 2373 | * Type library hooks * |
| 2374 | * * |
| 2375 | ************************************************************************/ |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2376 | static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2377 | const xmlChar * str); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2378 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2379 | /** |
| 2380 | * xmlRelaxNGSchemaTypeHave: |
| 2381 | * @data: data needed for the library |
| 2382 | * @type: the type name |
| 2383 | * |
| 2384 | * Check if the given type is provided by |
| 2385 | * the W3C XMLSchema Datatype library. |
| 2386 | * |
| 2387 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2388 | */ |
| 2389 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2390 | xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar * type) |
| 2391 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2392 | xmlSchemaTypePtr typ; |
| 2393 | |
| 2394 | if (type == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2395 | return (-1); |
| 2396 | typ = xmlSchemaGetPredefinedType(type, |
| 2397 | BAD_CAST |
| 2398 | "http://www.w3.org/2001/XMLSchema"); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2399 | if (typ == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2400 | return (0); |
| 2401 | return (1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2402 | } |
| 2403 | |
| 2404 | /** |
| 2405 | * xmlRelaxNGSchemaTypeCheck: |
| 2406 | * @data: data needed for the library |
| 2407 | * @type: the type name |
| 2408 | * @value: the value to check |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2409 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2410 | * |
| 2411 | * Check if the given type and value are validated by |
| 2412 | * the W3C XMLSchema Datatype library. |
| 2413 | * |
| 2414 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2415 | */ |
| 2416 | static int |
| 2417 | xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2418 | const xmlChar * type, |
| 2419 | const xmlChar * value, |
| 2420 | void **result, xmlNodePtr node) |
| 2421 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2422 | xmlSchemaTypePtr typ; |
| 2423 | int ret; |
| 2424 | |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2425 | if ((type == NULL) || (value == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2426 | return (-1); |
| 2427 | typ = xmlSchemaGetPredefinedType(type, |
| 2428 | BAD_CAST |
| 2429 | "http://www.w3.org/2001/XMLSchema"); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2430 | if (typ == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2431 | return (-1); |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2432 | ret = xmlSchemaValPredefTypeNode(typ, value, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2433 | (xmlSchemaValPtr *) result, node); |
| 2434 | if (ret == 2) /* special ID error code */ |
| 2435 | return (2); |
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 (1); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2438 | if (ret > 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2439 | return (0); |
| 2440 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2441 | } |
| 2442 | |
| 2443 | /** |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2444 | * xmlRelaxNGSchemaFacetCheck: |
| 2445 | * @data: data needed for the library |
| 2446 | * @type: the type name |
| 2447 | * @facet: the facet name |
| 2448 | * @val: the facet value |
| 2449 | * @strval: the string value |
| 2450 | * @value: the value to check |
| 2451 | * |
| 2452 | * Function provided by a type library to check a value facet |
| 2453 | * |
| 2454 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2455 | */ |
| 2456 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2457 | xmlRelaxNGSchemaFacetCheck(void *data ATTRIBUTE_UNUSED, |
| 2458 | const xmlChar * type, const xmlChar * facetname, |
| 2459 | const xmlChar * val, const xmlChar * strval, |
| 2460 | void *value) |
| 2461 | { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2462 | xmlSchemaFacetPtr facet; |
| 2463 | xmlSchemaTypePtr typ; |
| 2464 | int ret; |
| 2465 | |
| 2466 | if ((type == NULL) || (strval == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2467 | return (-1); |
| 2468 | typ = xmlSchemaGetPredefinedType(type, |
| 2469 | BAD_CAST |
| 2470 | "http://www.w3.org/2001/XMLSchema"); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2471 | if (typ == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2472 | return (-1); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2473 | |
| 2474 | facet = xmlSchemaNewFacet(); |
| 2475 | if (facet == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2476 | return (-1); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2477 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2478 | if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2479 | facet->type = XML_SCHEMA_FACET_MININCLUSIVE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2480 | } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2481 | facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2482 | } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2483 | facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2484 | } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2485 | facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2486 | } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2487 | facet->type = XML_SCHEMA_FACET_TOTALDIGITS; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2488 | } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2489 | facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2490 | } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2491 | facet->type = XML_SCHEMA_FACET_PATTERN; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2492 | } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2493 | facet->type = XML_SCHEMA_FACET_ENUMERATION; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2494 | } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2495 | facet->type = XML_SCHEMA_FACET_WHITESPACE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2496 | } else if (xmlStrEqual(facetname, BAD_CAST "length")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2497 | facet->type = XML_SCHEMA_FACET_LENGTH; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2498 | } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2499 | facet->type = XML_SCHEMA_FACET_MAXLENGTH; |
| 2500 | } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) { |
| 2501 | facet->type = XML_SCHEMA_FACET_MINLENGTH; |
| 2502 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2503 | xmlSchemaFreeFacet(facet); |
| 2504 | return (-1); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2505 | } |
Daniel Veillard | 6dc9196 | 2004-03-22 19:10:02 +0000 | [diff] [blame] | 2506 | facet->value = val; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2507 | ret = xmlSchemaCheckFacet(facet, typ, NULL, type); |
| 2508 | if (ret != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2509 | xmlSchemaFreeFacet(facet); |
| 2510 | return (-1); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2511 | } |
| 2512 | ret = xmlSchemaValidateFacet(typ, facet, strval, value); |
| 2513 | xmlSchemaFreeFacet(facet); |
| 2514 | if (ret != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2515 | return (-1); |
| 2516 | return (0); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2517 | } |
| 2518 | |
| 2519 | /** |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2520 | * xmlRelaxNGSchemaFreeValue: |
| 2521 | * @data: data needed for the library |
| 2522 | * @value: the value to free |
| 2523 | * |
| 2524 | * Function provided by a type library to free a Schemas value |
| 2525 | * |
| 2526 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2527 | */ |
| 2528 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2529 | xmlRelaxNGSchemaFreeValue(void *data ATTRIBUTE_UNUSED, void *value) |
| 2530 | { |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2531 | xmlSchemaFreeValue(value); |
| 2532 | } |
| 2533 | |
| 2534 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2535 | * xmlRelaxNGSchemaTypeCompare: |
| 2536 | * @data: data needed for the library |
| 2537 | * @type: the type name |
| 2538 | * @value1: the first value |
| 2539 | * @value2: the second value |
| 2540 | * |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2541 | * Compare two values for equality accordingly a type from the W3C XMLSchema |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2542 | * Datatype library. |
| 2543 | * |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2544 | * 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] | 2545 | */ |
| 2546 | static int |
| 2547 | xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2548 | const xmlChar * type, |
| 2549 | const xmlChar * value1, |
| 2550 | xmlNodePtr ctxt1, |
| 2551 | void *comp1, |
| 2552 | const xmlChar * value2, xmlNodePtr ctxt2) |
| 2553 | { |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2554 | int ret; |
| 2555 | xmlSchemaTypePtr typ; |
| 2556 | xmlSchemaValPtr res1 = NULL, res2 = NULL; |
| 2557 | |
| 2558 | if ((type == NULL) || (value1 == NULL) || (value2 == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2559 | return (-1); |
| 2560 | typ = xmlSchemaGetPredefinedType(type, |
| 2561 | BAD_CAST |
| 2562 | "http://www.w3.org/2001/XMLSchema"); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2563 | if (typ == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2564 | return (-1); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2565 | if (comp1 == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2566 | ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1); |
| 2567 | if (ret != 0) |
| 2568 | return (-1); |
| 2569 | if (res1 == NULL) |
| 2570 | return (-1); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2571 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2572 | res1 = (xmlSchemaValPtr) comp1; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2573 | } |
| 2574 | ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2575 | if (ret != 0) { |
Daniel Veillard | f464403 | 2005-06-13 11:41:31 +0000 | [diff] [blame] | 2576 | if ((comp1 == NULL) && (res1 != NULL)) |
| 2577 | xmlSchemaFreeValue(res1); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2578 | return (-1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2579 | } |
| 2580 | if (res1 == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2581 | return (-1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2582 | } |
| 2583 | ret = xmlSchemaCompareValues(res1, res2); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2584 | if (res1 != (xmlSchemaValPtr) comp1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2585 | xmlSchemaFreeValue(res1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2586 | xmlSchemaFreeValue(res2); |
| 2587 | if (ret == -2) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2588 | return (-1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2589 | if (ret == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2590 | return (1); |
| 2591 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2592 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2593 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2594 | /** |
| 2595 | * xmlRelaxNGDefaultTypeHave: |
| 2596 | * @data: data needed for the library |
| 2597 | * @type: the type name |
| 2598 | * |
| 2599 | * Check if the given type is provided by |
| 2600 | * the default datatype library. |
| 2601 | * |
| 2602 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2603 | */ |
| 2604 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2605 | xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED, |
| 2606 | const xmlChar * type) |
| 2607 | { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2608 | if (type == NULL) |
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 "string")) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2611 | return (1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2612 | if (xmlStrEqual(type, BAD_CAST "token")) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2613 | return (1); |
| 2614 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2615 | } |
| 2616 | |
| 2617 | /** |
| 2618 | * xmlRelaxNGDefaultTypeCheck: |
| 2619 | * @data: data needed for the library |
| 2620 | * @type: the type name |
| 2621 | * @value: the value to check |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2622 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2623 | * |
| 2624 | * Check if the given type and value are validated by |
| 2625 | * the default datatype library. |
| 2626 | * |
| 2627 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2628 | */ |
| 2629 | static int |
| 2630 | xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2631 | const xmlChar * type ATTRIBUTE_UNUSED, |
| 2632 | const xmlChar * value ATTRIBUTE_UNUSED, |
| 2633 | void **result ATTRIBUTE_UNUSED, |
| 2634 | xmlNodePtr node ATTRIBUTE_UNUSED) |
| 2635 | { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2636 | if (value == NULL) |
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 "string")) |
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 | if (xmlStrEqual(type, BAD_CAST "token")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2641 | return (1); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2642 | } |
| 2643 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2644 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2645 | } |
| 2646 | |
| 2647 | /** |
| 2648 | * xmlRelaxNGDefaultTypeCompare: |
| 2649 | * @data: data needed for the library |
| 2650 | * @type: the type name |
| 2651 | * @value1: the first value |
| 2652 | * @value2: the second value |
| 2653 | * |
| 2654 | * Compare two values accordingly a type from the default |
| 2655 | * datatype library. |
| 2656 | * |
| 2657 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2658 | */ |
| 2659 | static int |
| 2660 | xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2661 | const xmlChar * type, |
| 2662 | const xmlChar * value1, |
| 2663 | xmlNodePtr ctxt1 ATTRIBUTE_UNUSED, |
| 2664 | void *comp1 ATTRIBUTE_UNUSED, |
| 2665 | const xmlChar * value2, |
| 2666 | xmlNodePtr ctxt2 ATTRIBUTE_UNUSED) |
| 2667 | { |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2668 | int ret = -1; |
| 2669 | |
| 2670 | if (xmlStrEqual(type, BAD_CAST "string")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2671 | ret = xmlStrEqual(value1, value2); |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2672 | } else if (xmlStrEqual(type, BAD_CAST "token")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2673 | if (!xmlStrEqual(value1, value2)) { |
| 2674 | xmlChar *nval, *nvalue; |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2675 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2676 | /* |
| 2677 | * TODO: trivial optimizations are possible by |
| 2678 | * computing at compile-time |
| 2679 | */ |
| 2680 | nval = xmlRelaxNGNormalize(NULL, value1); |
| 2681 | nvalue = xmlRelaxNGNormalize(NULL, value2); |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2682 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2683 | if ((nval == NULL) || (nvalue == NULL)) |
| 2684 | ret = -1; |
| 2685 | else if (xmlStrEqual(nval, nvalue)) |
| 2686 | ret = 1; |
| 2687 | else |
| 2688 | ret = 0; |
| 2689 | if (nval != NULL) |
| 2690 | xmlFree(nval); |
| 2691 | if (nvalue != NULL) |
| 2692 | xmlFree(nvalue); |
| 2693 | } else |
| 2694 | ret = 1; |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2695 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2696 | return (ret); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2697 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2698 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2699 | static int xmlRelaxNGTypeInitialized = 0; |
| 2700 | static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL; |
| 2701 | |
| 2702 | /** |
| 2703 | * xmlRelaxNGFreeTypeLibrary: |
| 2704 | * @lib: the type library structure |
| 2705 | * @namespace: the URI bound to the library |
| 2706 | * |
| 2707 | * Free the structure associated to the type library |
| 2708 | */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2709 | static void |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2710 | xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2711 | const xmlChar * namespace ATTRIBUTE_UNUSED) |
| 2712 | { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2713 | if (lib == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2714 | return; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2715 | if (lib->namespace != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2716 | xmlFree((xmlChar *) lib->namespace); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2717 | xmlFree(lib); |
| 2718 | } |
| 2719 | |
| 2720 | /** |
| 2721 | * xmlRelaxNGRegisterTypeLibrary: |
| 2722 | * @namespace: the URI bound to the library |
| 2723 | * @data: data associated to the library |
| 2724 | * @have: the provide function |
| 2725 | * @check: the checking function |
| 2726 | * @comp: the comparison function |
| 2727 | * |
| 2728 | * Register a new type library |
| 2729 | * |
| 2730 | * Returns 0 in case of success and -1 in case of error. |
| 2731 | */ |
| 2732 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2733 | xmlRelaxNGRegisterTypeLibrary(const xmlChar * namespace, void *data, |
| 2734 | xmlRelaxNGTypeHave have, |
| 2735 | xmlRelaxNGTypeCheck check, |
| 2736 | xmlRelaxNGTypeCompare comp, |
| 2737 | xmlRelaxNGFacetCheck facet, |
| 2738 | xmlRelaxNGTypeFree freef) |
| 2739 | { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2740 | xmlRelaxNGTypeLibraryPtr lib; |
| 2741 | int ret; |
| 2742 | |
| 2743 | if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2744 | (check == NULL) || (comp == NULL)) |
| 2745 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2746 | if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2747 | xmlGenericError(xmlGenericErrorContext, |
| 2748 | "Relax-NG types library '%s' already registered\n", |
| 2749 | namespace); |
| 2750 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2751 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2752 | lib = |
| 2753 | (xmlRelaxNGTypeLibraryPtr) |
| 2754 | xmlMalloc(sizeof(xmlRelaxNGTypeLibrary)); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2755 | if (lib == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2756 | xmlRngVErrMemory(NULL, "adding types library\n"); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2757 | return (-1); |
| 2758 | } |
| 2759 | memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary)); |
| 2760 | lib->namespace = xmlStrdup(namespace); |
| 2761 | lib->data = data; |
| 2762 | lib->have = have; |
| 2763 | lib->comp = comp; |
| 2764 | lib->check = check; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2765 | lib->facet = facet; |
| 2766 | lib->freef = freef; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2767 | ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib); |
| 2768 | if (ret < 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2769 | xmlGenericError(xmlGenericErrorContext, |
| 2770 | "Relax-NG types library failed to register '%s'\n", |
| 2771 | namespace); |
| 2772 | xmlRelaxNGFreeTypeLibrary(lib, namespace); |
| 2773 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2774 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2775 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2776 | } |
| 2777 | |
| 2778 | /** |
| 2779 | * xmlRelaxNGInitTypes: |
| 2780 | * |
| 2781 | * Initilize the default type libraries. |
| 2782 | * |
| 2783 | * Returns 0 in case of success and -1 in case of error. |
| 2784 | */ |
Daniel Veillard | dd6d300 | 2004-11-03 14:20:29 +0000 | [diff] [blame] | 2785 | int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2786 | xmlRelaxNGInitTypes(void) |
| 2787 | { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2788 | if (xmlRelaxNGTypeInitialized != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2789 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2790 | xmlRelaxNGRegisteredTypes = xmlHashCreate(10); |
| 2791 | if (xmlRelaxNGRegisteredTypes == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2792 | xmlGenericError(xmlGenericErrorContext, |
| 2793 | "Failed to allocate sh table for Relax-NG types\n"); |
| 2794 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2795 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2796 | xmlRelaxNGRegisterTypeLibrary(BAD_CAST |
| 2797 | "http://www.w3.org/2001/XMLSchema-datatypes", |
| 2798 | NULL, xmlRelaxNGSchemaTypeHave, |
| 2799 | xmlRelaxNGSchemaTypeCheck, |
| 2800 | xmlRelaxNGSchemaTypeCompare, |
| 2801 | xmlRelaxNGSchemaFacetCheck, |
| 2802 | xmlRelaxNGSchemaFreeValue); |
| 2803 | xmlRelaxNGRegisterTypeLibrary(xmlRelaxNGNs, NULL, |
| 2804 | xmlRelaxNGDefaultTypeHave, |
| 2805 | xmlRelaxNGDefaultTypeCheck, |
| 2806 | xmlRelaxNGDefaultTypeCompare, NULL, |
| 2807 | NULL); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2808 | xmlRelaxNGTypeInitialized = 1; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2809 | return (0); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2810 | } |
| 2811 | |
| 2812 | /** |
| 2813 | * xmlRelaxNGCleanupTypes: |
| 2814 | * |
| 2815 | * Cleanup the default Schemas type library associated to RelaxNG |
| 2816 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2817 | void |
| 2818 | xmlRelaxNGCleanupTypes(void) |
| 2819 | { |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 2820 | xmlSchemaCleanupTypes(); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2821 | if (xmlRelaxNGTypeInitialized == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2822 | return; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2823 | xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2824 | xmlRelaxNGFreeTypeLibrary); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2825 | xmlRelaxNGTypeInitialized = 0; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2826 | } |
| 2827 | |
| 2828 | /************************************************************************ |
| 2829 | * * |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2830 | * Compiling element content into regexp * |
| 2831 | * * |
| 2832 | * Sometime the element content can be compiled into a pure regexp, * |
| 2833 | * This allows a faster execution and streamability at that level * |
| 2834 | * * |
| 2835 | ************************************************************************/ |
| 2836 | |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2837 | static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, |
| 2838 | xmlRelaxNGDefinePtr def); |
| 2839 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2840 | /** |
| 2841 | * xmlRelaxNGIsCompileable: |
| 2842 | * @define: the definition to check |
| 2843 | * |
| 2844 | * Check if a definition is nullable. |
| 2845 | * |
| 2846 | * Returns 1 if yes, 0 if no and -1 in case of error |
| 2847 | */ |
| 2848 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2849 | xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def) |
| 2850 | { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2851 | int ret = -1; |
| 2852 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2853 | if (def == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2854 | return (-1); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2855 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2856 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 2857 | (def->dflags & IS_COMPILABLE)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2858 | return (1); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2859 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 2860 | (def->dflags & IS_NOT_COMPILABLE)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2861 | return (0); |
| 2862 | switch (def->type) { |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2863 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2864 | ret = xmlRelaxNGIsCompileable(def->content); |
| 2865 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2866 | case XML_RELAXNG_TEXT: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2867 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2868 | ret = 1; |
| 2869 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2870 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2871 | /* |
| 2872 | * Check if the element content is compileable |
| 2873 | */ |
| 2874 | if (((def->dflags & IS_NOT_COMPILABLE) == 0) && |
| 2875 | ((def->dflags & IS_COMPILABLE) == 0)) { |
| 2876 | xmlRelaxNGDefinePtr list; |
| 2877 | |
| 2878 | list = def->content; |
| 2879 | while (list != NULL) { |
| 2880 | ret = xmlRelaxNGIsCompileable(list); |
| 2881 | if (ret != 1) |
| 2882 | break; |
| 2883 | list = list->next; |
| 2884 | } |
William M. Brack | 6092962 | 2004-03-27 17:54:18 +0000 | [diff] [blame] | 2885 | /* |
| 2886 | * Because the routine is recursive, we must guard against |
| 2887 | * discovering both COMPILABLE and NOT_COMPILABLE |
| 2888 | */ |
| 2889 | if (ret == 0) { |
| 2890 | def->dflags &= ~IS_COMPILABLE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2891 | def->dflags |= IS_NOT_COMPILABLE; |
William M. Brack | 6092962 | 2004-03-27 17:54:18 +0000 | [diff] [blame] | 2892 | } |
| 2893 | if ((ret == 1) && !(def->dflags &= IS_NOT_COMPILABLE)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2894 | def->dflags |= IS_COMPILABLE; |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2895 | #ifdef DEBUG_COMPILE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2896 | if (ret == 1) { |
| 2897 | xmlGenericError(xmlGenericErrorContext, |
| 2898 | "element content for %s is compilable\n", |
| 2899 | def->name); |
| 2900 | } else if (ret == 0) { |
| 2901 | xmlGenericError(xmlGenericErrorContext, |
| 2902 | "element content for %s is not compilable\n", |
| 2903 | def->name); |
| 2904 | } else { |
| 2905 | xmlGenericError(xmlGenericErrorContext, |
| 2906 | "Problem in RelaxNGIsCompileable for element %s\n", |
| 2907 | def->name); |
| 2908 | } |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2909 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2910 | } |
| 2911 | /* |
| 2912 | * All elements return a compileable status unless they |
| 2913 | * are generic like anyName |
| 2914 | */ |
| 2915 | if ((def->nameClass != NULL) || (def->name == NULL)) |
| 2916 | ret = 0; |
| 2917 | else |
| 2918 | ret = 1; |
| 2919 | return (ret); |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2920 | case XML_RELAXNG_REF: |
| 2921 | case XML_RELAXNG_EXTERNALREF: |
| 2922 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2923 | if (def->depth == -20) { |
| 2924 | return (1); |
| 2925 | } else { |
| 2926 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2927 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2928 | def->depth = -20; |
| 2929 | list = def->content; |
| 2930 | while (list != NULL) { |
| 2931 | ret = xmlRelaxNGIsCompileable(list); |
| 2932 | if (ret != 1) |
| 2933 | break; |
| 2934 | list = list->next; |
| 2935 | } |
| 2936 | } |
| 2937 | break; |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2938 | case XML_RELAXNG_START: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2939 | case XML_RELAXNG_OPTIONAL: |
| 2940 | case XML_RELAXNG_ZEROORMORE: |
| 2941 | case XML_RELAXNG_ONEORMORE: |
| 2942 | case XML_RELAXNG_CHOICE: |
| 2943 | case XML_RELAXNG_GROUP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2944 | case XML_RELAXNG_DEF:{ |
| 2945 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2946 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2947 | list = def->content; |
| 2948 | while (list != NULL) { |
| 2949 | ret = xmlRelaxNGIsCompileable(list); |
| 2950 | if (ret != 1) |
| 2951 | break; |
| 2952 | list = list->next; |
| 2953 | } |
| 2954 | break; |
| 2955 | } |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2956 | case XML_RELAXNG_EXCEPT: |
| 2957 | case XML_RELAXNG_ATTRIBUTE: |
| 2958 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2959 | case XML_RELAXNG_DATATYPE: |
| 2960 | case XML_RELAXNG_LIST: |
| 2961 | case XML_RELAXNG_PARAM: |
| 2962 | case XML_RELAXNG_VALUE: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2963 | case XML_RELAXNG_NOT_ALLOWED: |
William M. Brack | 7e29c0a | 2004-04-02 09:07:22 +0000 | [diff] [blame] | 2964 | ret = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2965 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2966 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2967 | if (ret == 0) |
| 2968 | def->dflags |= IS_NOT_COMPILABLE; |
| 2969 | if (ret == 1) |
| 2970 | def->dflags |= IS_COMPILABLE; |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2971 | #ifdef DEBUG_COMPILE |
| 2972 | if (ret == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2973 | xmlGenericError(xmlGenericErrorContext, |
| 2974 | "RelaxNGIsCompileable %s : true\n", |
| 2975 | xmlRelaxNGDefName(def)); |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2976 | } else if (ret == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2977 | xmlGenericError(xmlGenericErrorContext, |
| 2978 | "RelaxNGIsCompileable %s : false\n", |
| 2979 | xmlRelaxNGDefName(def)); |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2980 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2981 | xmlGenericError(xmlGenericErrorContext, |
| 2982 | "Problem in RelaxNGIsCompileable %s\n", |
| 2983 | xmlRelaxNGDefName(def)); |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2984 | } |
| 2985 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2986 | return (ret); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2987 | } |
| 2988 | |
| 2989 | /** |
| 2990 | * xmlRelaxNGCompile: |
| 2991 | * ctxt: the RelaxNG parser context |
| 2992 | * @define: the definition tree to compile |
| 2993 | * |
| 2994 | * Compile the set of definitions, it works recursively, till the |
| 2995 | * element boundaries, where it tries to compile the content if possible |
| 2996 | * |
| 2997 | * Returns 0 if success and -1 in case of error |
| 2998 | */ |
| 2999 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3000 | xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) |
| 3001 | { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3002 | int ret = 0; |
| 3003 | xmlRelaxNGDefinePtr list; |
| 3004 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3005 | if ((ctxt == NULL) || (def == NULL)) |
| 3006 | return (-1); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3007 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3008 | switch (def->type) { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3009 | case XML_RELAXNG_START: |
| 3010 | if ((xmlRelaxNGIsCompileable(def) == 1) && (def->depth != -25)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3011 | xmlAutomataPtr oldam = ctxt->am; |
| 3012 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3013 | |
| 3014 | def->depth = -25; |
| 3015 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3016 | list = def->content; |
| 3017 | ctxt->am = xmlNewAutomata(); |
| 3018 | if (ctxt->am == NULL) |
| 3019 | return (-1); |
| 3020 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
| 3021 | while (list != NULL) { |
| 3022 | xmlRelaxNGCompile(ctxt, list); |
| 3023 | list = list->next; |
| 3024 | } |
| 3025 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
| 3026 | def->contModel = xmlAutomataCompile(ctxt->am); |
| 3027 | xmlRegexpIsDeterminist(def->contModel); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3028 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3029 | xmlFreeAutomata(ctxt->am); |
| 3030 | ctxt->state = oldstate; |
| 3031 | ctxt->am = oldam; |
| 3032 | } |
| 3033 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3034 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3035 | if ((ctxt->am != NULL) && (def->name != NULL)) { |
| 3036 | ctxt->state = xmlAutomataNewTransition2(ctxt->am, |
| 3037 | ctxt->state, NULL, |
| 3038 | def->name, def->ns, |
| 3039 | def); |
| 3040 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3041 | if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3042 | xmlAutomataPtr oldam = ctxt->am; |
| 3043 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3044 | |
| 3045 | def->depth = -25; |
| 3046 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3047 | list = def->content; |
| 3048 | ctxt->am = xmlNewAutomata(); |
| 3049 | if (ctxt->am == NULL) |
| 3050 | return (-1); |
| 3051 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
| 3052 | while (list != NULL) { |
| 3053 | xmlRelaxNGCompile(ctxt, list); |
| 3054 | list = list->next; |
| 3055 | } |
| 3056 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
| 3057 | def->contModel = xmlAutomataCompile(ctxt->am); |
| 3058 | if (!xmlRegexpIsDeterminist(def->contModel)) { |
| 3059 | /* |
| 3060 | * we can only use the automata if it is determinist |
| 3061 | */ |
| 3062 | xmlRegFreeRegexp(def->contModel); |
| 3063 | def->contModel = NULL; |
| 3064 | } |
| 3065 | xmlFreeAutomata(ctxt->am); |
| 3066 | ctxt->state = oldstate; |
| 3067 | ctxt->am = oldam; |
| 3068 | } else { |
| 3069 | xmlAutomataPtr oldam = ctxt->am; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3070 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3071 | /* |
| 3072 | * we can't build the content model for this element content |
| 3073 | * but it still might be possible to build it for some of its |
| 3074 | * children, recurse. |
| 3075 | */ |
| 3076 | ret = xmlRelaxNGTryCompile(ctxt, def); |
| 3077 | ctxt->am = oldam; |
| 3078 | } |
| 3079 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3080 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3081 | ret = xmlRelaxNGCompile(ctxt, def->content); |
| 3082 | break; |
| 3083 | case XML_RELAXNG_OPTIONAL:{ |
| 3084 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3085 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3086 | xmlRelaxNGCompile(ctxt, def->content); |
| 3087 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
| 3088 | break; |
| 3089 | } |
| 3090 | case XML_RELAXNG_ZEROORMORE:{ |
| 3091 | xmlAutomataStatePtr oldstate; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3092 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3093 | ctxt->state = |
| 3094 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 3095 | oldstate = ctxt->state; |
| 3096 | list = def->content; |
| 3097 | while (list != NULL) { |
| 3098 | xmlRelaxNGCompile(ctxt, list); |
| 3099 | list = list->next; |
| 3100 | } |
| 3101 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate); |
| 3102 | ctxt->state = |
| 3103 | xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 3104 | break; |
| 3105 | } |
| 3106 | case XML_RELAXNG_ONEORMORE:{ |
| 3107 | xmlAutomataStatePtr oldstate; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3108 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3109 | list = def->content; |
| 3110 | while (list != NULL) { |
| 3111 | xmlRelaxNGCompile(ctxt, list); |
| 3112 | list = list->next; |
| 3113 | } |
| 3114 | oldstate = ctxt->state; |
| 3115 | list = def->content; |
| 3116 | while (list != NULL) { |
| 3117 | xmlRelaxNGCompile(ctxt, list); |
| 3118 | list = list->next; |
| 3119 | } |
| 3120 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate); |
| 3121 | ctxt->state = |
| 3122 | xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 3123 | break; |
| 3124 | } |
| 3125 | case XML_RELAXNG_CHOICE:{ |
| 3126 | xmlAutomataStatePtr target = NULL; |
| 3127 | xmlAutomataStatePtr oldstate = ctxt->state; |
| 3128 | |
| 3129 | list = def->content; |
| 3130 | while (list != NULL) { |
| 3131 | ctxt->state = oldstate; |
| 3132 | ret = xmlRelaxNGCompile(ctxt, list); |
| 3133 | if (ret != 0) |
| 3134 | break; |
| 3135 | if (target == NULL) |
| 3136 | target = ctxt->state; |
| 3137 | else { |
| 3138 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, |
| 3139 | target); |
| 3140 | } |
| 3141 | list = list->next; |
| 3142 | } |
| 3143 | ctxt->state = target; |
| 3144 | |
| 3145 | break; |
| 3146 | } |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3147 | case XML_RELAXNG_REF: |
| 3148 | case XML_RELAXNG_EXTERNALREF: |
| 3149 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3150 | case XML_RELAXNG_GROUP: |
| 3151 | case XML_RELAXNG_DEF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3152 | list = def->content; |
| 3153 | while (list != NULL) { |
| 3154 | ret = xmlRelaxNGCompile(ctxt, list); |
| 3155 | if (ret != 0) |
| 3156 | break; |
| 3157 | list = list->next; |
| 3158 | } |
| 3159 | break; |
| 3160 | case XML_RELAXNG_TEXT:{ |
| 3161 | xmlAutomataStatePtr oldstate; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3162 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3163 | ctxt->state = |
| 3164 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 3165 | oldstate = ctxt->state; |
| 3166 | xmlRelaxNGCompile(ctxt, def->content); |
| 3167 | xmlAutomataNewTransition(ctxt->am, ctxt->state, |
| 3168 | ctxt->state, BAD_CAST "#text", |
| 3169 | NULL); |
| 3170 | ctxt->state = |
| 3171 | xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 3172 | break; |
| 3173 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3174 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3175 | ctxt->state = |
| 3176 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 3177 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3178 | case XML_RELAXNG_EXCEPT: |
| 3179 | case XML_RELAXNG_ATTRIBUTE: |
| 3180 | case XML_RELAXNG_INTERLEAVE: |
| 3181 | case XML_RELAXNG_NOT_ALLOWED: |
| 3182 | case XML_RELAXNG_DATATYPE: |
| 3183 | case XML_RELAXNG_LIST: |
| 3184 | case XML_RELAXNG_PARAM: |
| 3185 | case XML_RELAXNG_VALUE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3186 | /* This should not happen and generate an internal error */ |
| 3187 | fprintf(stderr, "RNG internal error trying to compile %s\n", |
| 3188 | xmlRelaxNGDefName(def)); |
| 3189 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3190 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3191 | return (ret); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3192 | } |
| 3193 | |
| 3194 | /** |
| 3195 | * xmlRelaxNGTryCompile: |
| 3196 | * ctxt: the RelaxNG parser context |
| 3197 | * @define: the definition tree to compile |
| 3198 | * |
| 3199 | * Try to compile the set of definitions, it works recursively, |
| 3200 | * possibly ignoring parts which cannot be compiled. |
| 3201 | * |
| 3202 | * Returns 0 if success and -1 in case of error |
| 3203 | */ |
| 3204 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3205 | xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) |
| 3206 | { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3207 | int ret = 0; |
| 3208 | xmlRelaxNGDefinePtr list; |
| 3209 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3210 | if ((ctxt == NULL) || (def == NULL)) |
| 3211 | return (-1); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3212 | |
| 3213 | if ((def->type == XML_RELAXNG_START) || |
| 3214 | (def->type == XML_RELAXNG_ELEMENT)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3215 | ret = xmlRelaxNGIsCompileable(def); |
| 3216 | if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) { |
| 3217 | ctxt->am = NULL; |
| 3218 | ret = xmlRelaxNGCompile(ctxt, def); |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3219 | #ifdef DEBUG_PROGRESSIVE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3220 | if (ret == 0) { |
| 3221 | if (def->type == XML_RELAXNG_START) |
| 3222 | xmlGenericError(xmlGenericErrorContext, |
| 3223 | "compiled the start\n"); |
| 3224 | else |
| 3225 | xmlGenericError(xmlGenericErrorContext, |
| 3226 | "compiled element %s\n", def->name); |
| 3227 | } else { |
| 3228 | if (def->type == XML_RELAXNG_START) |
| 3229 | xmlGenericError(xmlGenericErrorContext, |
| 3230 | "failed to compile the start\n"); |
| 3231 | else |
| 3232 | xmlGenericError(xmlGenericErrorContext, |
| 3233 | "failed to compile element %s\n", |
| 3234 | def->name); |
| 3235 | } |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3236 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3237 | return (ret); |
| 3238 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3239 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3240 | switch (def->type) { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3241 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3242 | ret = xmlRelaxNGTryCompile(ctxt, def->content); |
| 3243 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3244 | case XML_RELAXNG_TEXT: |
| 3245 | case XML_RELAXNG_DATATYPE: |
| 3246 | case XML_RELAXNG_LIST: |
| 3247 | case XML_RELAXNG_PARAM: |
| 3248 | case XML_RELAXNG_VALUE: |
| 3249 | case XML_RELAXNG_EMPTY: |
| 3250 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3251 | ret = 0; |
| 3252 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3253 | case XML_RELAXNG_OPTIONAL: |
| 3254 | case XML_RELAXNG_ZEROORMORE: |
| 3255 | case XML_RELAXNG_ONEORMORE: |
| 3256 | case XML_RELAXNG_CHOICE: |
| 3257 | case XML_RELAXNG_GROUP: |
| 3258 | case XML_RELAXNG_DEF: |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3259 | case XML_RELAXNG_START: |
| 3260 | case XML_RELAXNG_REF: |
| 3261 | case XML_RELAXNG_EXTERNALREF: |
| 3262 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3263 | list = def->content; |
| 3264 | while (list != NULL) { |
| 3265 | ret = xmlRelaxNGTryCompile(ctxt, list); |
| 3266 | if (ret != 0) |
| 3267 | break; |
| 3268 | list = list->next; |
| 3269 | } |
| 3270 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3271 | case XML_RELAXNG_EXCEPT: |
| 3272 | case XML_RELAXNG_ATTRIBUTE: |
| 3273 | case XML_RELAXNG_INTERLEAVE: |
| 3274 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3275 | ret = 0; |
| 3276 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3277 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3278 | return (ret); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 3279 | } |
| 3280 | |
| 3281 | /************************************************************************ |
| 3282 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3283 | * Parsing functions * |
| 3284 | * * |
| 3285 | ************************************************************************/ |
| 3286 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3287 | static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr |
| 3288 | ctxt, xmlNodePtr node); |
| 3289 | static xmlRelaxNGDefinePtr xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr |
| 3290 | ctxt, xmlNodePtr node); |
| 3291 | static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr |
| 3292 | ctxt, xmlNodePtr nodes, |
| 3293 | int group); |
| 3294 | static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr |
| 3295 | ctxt, xmlNodePtr node); |
| 3296 | static xmlRelaxNGPtr xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, |
| 3297 | xmlNodePtr node); |
| 3298 | static int xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt, |
| 3299 | xmlNodePtr nodes); |
| 3300 | static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr |
| 3301 | ctxt, xmlNodePtr node, |
| 3302 | xmlRelaxNGDefinePtr |
| 3303 | def); |
| 3304 | static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr |
| 3305 | ctxt, xmlNodePtr nodes); |
| 3306 | static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 3307 | xmlRelaxNGDefinePtr define, |
| 3308 | xmlNodePtr elem); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3309 | |
| 3310 | |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 3311 | #define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content)) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3312 | |
| 3313 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3314 | * xmlRelaxNGIsNullable: |
| 3315 | * @define: the definition to verify |
| 3316 | * |
| 3317 | * Check if a definition is nullable. |
| 3318 | * |
| 3319 | * Returns 1 if yes, 0 if no and -1 in case of error |
| 3320 | */ |
| 3321 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3322 | xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define) |
| 3323 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3324 | int ret; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3325 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3326 | if (define == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3327 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3328 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3329 | if (define->dflags & IS_NULLABLE) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3330 | return (1); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3331 | if (define->dflags & IS_NOT_NULLABLE) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3332 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3333 | switch (define->type) { |
| 3334 | case XML_RELAXNG_EMPTY: |
| 3335 | case XML_RELAXNG_TEXT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3336 | ret = 1; |
| 3337 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3338 | case XML_RELAXNG_NOOP: |
| 3339 | case XML_RELAXNG_DEF: |
| 3340 | case XML_RELAXNG_REF: |
| 3341 | case XML_RELAXNG_EXTERNALREF: |
| 3342 | case XML_RELAXNG_PARENTREF: |
| 3343 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3344 | ret = xmlRelaxNGIsNullable(define->content); |
| 3345 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3346 | case XML_RELAXNG_EXCEPT: |
| 3347 | case XML_RELAXNG_NOT_ALLOWED: |
| 3348 | case XML_RELAXNG_ELEMENT: |
| 3349 | case XML_RELAXNG_DATATYPE: |
| 3350 | case XML_RELAXNG_PARAM: |
| 3351 | case XML_RELAXNG_VALUE: |
| 3352 | case XML_RELAXNG_LIST: |
| 3353 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3354 | ret = 0; |
| 3355 | break; |
| 3356 | case XML_RELAXNG_CHOICE:{ |
| 3357 | xmlRelaxNGDefinePtr list = define->content; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3358 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3359 | while (list != NULL) { |
| 3360 | ret = xmlRelaxNGIsNullable(list); |
| 3361 | if (ret != 0) |
| 3362 | goto done; |
| 3363 | list = list->next; |
| 3364 | } |
| 3365 | ret = 0; |
| 3366 | break; |
| 3367 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3368 | case XML_RELAXNG_START: |
| 3369 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3370 | case XML_RELAXNG_GROUP:{ |
| 3371 | xmlRelaxNGDefinePtr list = define->content; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3372 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3373 | while (list != NULL) { |
| 3374 | ret = xmlRelaxNGIsNullable(list); |
| 3375 | if (ret != 1) |
| 3376 | goto done; |
| 3377 | list = list->next; |
| 3378 | } |
| 3379 | return (1); |
| 3380 | } |
| 3381 | default: |
| 3382 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3383 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3384 | done: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3385 | if (ret == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3386 | define->dflags |= IS_NOT_NULLABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3387 | if (ret == 1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3388 | define->dflags |= IS_NULLABLE; |
| 3389 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3390 | } |
| 3391 | |
| 3392 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3393 | * xmlRelaxNGIsBlank: |
| 3394 | * @str: a string |
| 3395 | * |
| 3396 | * Check if a string is ignorable c.f. 4.2. Whitespace |
| 3397 | * |
| 3398 | * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise |
| 3399 | */ |
| 3400 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3401 | xmlRelaxNGIsBlank(xmlChar * str) |
| 3402 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3403 | if (str == NULL) |
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 | while (*str != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3406 | if (!(IS_BLANK_CH(*str))) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3407 | return (0); |
| 3408 | str++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3409 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3410 | return (1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3411 | } |
| 3412 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3413 | /** |
| 3414 | * xmlRelaxNGGetDataTypeLibrary: |
| 3415 | * @ctxt: a Relax-NG parser context |
| 3416 | * @node: the current data or value element |
| 3417 | * |
| 3418 | * Applies algorithm from 4.3. datatypeLibrary attribute |
| 3419 | * |
| 3420 | * Returns the datatypeLibary value or NULL if not found |
| 3421 | */ |
| 3422 | static xmlChar * |
| 3423 | xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3424 | xmlNodePtr node) |
| 3425 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3426 | xmlChar *ret, *escape; |
| 3427 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3428 | if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3429 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
| 3430 | if (ret != NULL) { |
| 3431 | if (ret[0] == 0) { |
| 3432 | xmlFree(ret); |
| 3433 | return (NULL); |
| 3434 | } |
| 3435 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
| 3436 | if (escape == NULL) { |
| 3437 | return (ret); |
| 3438 | } |
| 3439 | xmlFree(ret); |
| 3440 | return (escape); |
| 3441 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3442 | } |
| 3443 | node = node->parent; |
| 3444 | while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3445 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
| 3446 | if (ret != NULL) { |
| 3447 | if (ret[0] == 0) { |
| 3448 | xmlFree(ret); |
| 3449 | return (NULL); |
| 3450 | } |
| 3451 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
| 3452 | if (escape == NULL) { |
| 3453 | return (ret); |
| 3454 | } |
| 3455 | xmlFree(ret); |
| 3456 | return (escape); |
| 3457 | } |
| 3458 | node = node->parent; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3459 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3460 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3461 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3462 | |
| 3463 | /** |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3464 | * xmlRelaxNGParseValue: |
| 3465 | * @ctxt: a Relax-NG parser context |
| 3466 | * @node: the data node. |
| 3467 | * |
| 3468 | * parse the content of a RelaxNG value node. |
| 3469 | * |
| 3470 | * Returns the definition pointer or NULL in case of error |
| 3471 | */ |
| 3472 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3473 | xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 3474 | { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3475 | xmlRelaxNGDefinePtr def = NULL; |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 3476 | xmlRelaxNGTypeLibraryPtr lib = NULL; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3477 | xmlChar *type; |
| 3478 | xmlChar *library; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3479 | int success = 0; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3480 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3481 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3482 | if (def == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3483 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3484 | def->type = XML_RELAXNG_VALUE; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3485 | |
| 3486 | type = xmlGetProp(node, BAD_CAST "type"); |
| 3487 | if (type != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3488 | xmlRelaxNGNormExtSpace(type); |
| 3489 | if (xmlValidateNCName(type, 0)) { |
| 3490 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE, |
| 3491 | "value type '%s' is not an NCName\n", type, NULL); |
| 3492 | } |
| 3493 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
| 3494 | if (library == NULL) |
| 3495 | library = |
| 3496 | xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3497 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3498 | def->name = type; |
| 3499 | def->ns = library; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3500 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3501 | lib = (xmlRelaxNGTypeLibraryPtr) |
| 3502 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
| 3503 | if (lib == NULL) { |
| 3504 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB, |
| 3505 | "Use of unregistered type library '%s'\n", library, |
| 3506 | NULL); |
| 3507 | def->data = NULL; |
| 3508 | } else { |
| 3509 | def->data = lib; |
| 3510 | if (lib->have == NULL) { |
| 3511 | xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB, |
| 3512 | "Internal error with type library '%s': no 'have'\n", |
| 3513 | library, NULL); |
| 3514 | } else { |
| 3515 | success = lib->have(lib->data, def->name); |
| 3516 | if (success != 1) { |
| 3517 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND, |
| 3518 | "Error type '%s' is not exported by type library '%s'\n", |
| 3519 | def->name, library); |
| 3520 | } |
| 3521 | } |
| 3522 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3523 | } |
| 3524 | if (node->children == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3525 | def->value = xmlStrdup(BAD_CAST ""); |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 3526 | } else if (((node->children->type != XML_TEXT_NODE) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3527 | (node->children->type != XML_CDATA_SECTION_NODE)) || |
| 3528 | (node->children->next != NULL)) { |
| 3529 | xmlRngPErr(ctxt, node, XML_RNGP_TEXT_EXPECTED, |
| 3530 | "Expecting a single text value for <value>content\n", |
| 3531 | NULL, NULL); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3532 | } else if (def != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3533 | def->value = xmlNodeGetContent(node); |
| 3534 | if (def->value == NULL) { |
| 3535 | xmlRngPErr(ctxt, node, XML_RNGP_VALUE_NO_CONTENT, |
| 3536 | "Element <value> has no content\n", NULL, NULL); |
| 3537 | } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) { |
| 3538 | void *val = NULL; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3539 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3540 | success = |
| 3541 | lib->check(lib->data, def->name, def->value, &val, node); |
| 3542 | if (success != 1) { |
| 3543 | xmlRngPErr(ctxt, node, XML_RNGP_INVALID_VALUE, |
| 3544 | "Value '%s' is not acceptable for type '%s'\n", |
| 3545 | def->value, def->name); |
| 3546 | } else { |
| 3547 | if (val != NULL) |
| 3548 | def->attrs = val; |
| 3549 | } |
| 3550 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3551 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3552 | return (def); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3553 | } |
| 3554 | |
| 3555 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3556 | * xmlRelaxNGParseData: |
| 3557 | * @ctxt: a Relax-NG parser context |
| 3558 | * @node: the data node. |
| 3559 | * |
| 3560 | * parse the content of a RelaxNG data node. |
| 3561 | * |
| 3562 | * Returns the definition pointer or NULL in case of error |
| 3563 | */ |
| 3564 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3565 | xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 3566 | { |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3567 | xmlRelaxNGDefinePtr def = NULL, except, last = NULL; |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 3568 | xmlRelaxNGDefinePtr param, lastparam = NULL; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3569 | xmlRelaxNGTypeLibraryPtr lib; |
| 3570 | xmlChar *type; |
| 3571 | xmlChar *library; |
| 3572 | xmlNodePtr content; |
| 3573 | int tmp; |
| 3574 | |
| 3575 | type = xmlGetProp(node, BAD_CAST "type"); |
| 3576 | if (type == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3577 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_MISSING, "data has no type\n", NULL, |
| 3578 | NULL); |
| 3579 | return (NULL); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3580 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3581 | xmlRelaxNGNormExtSpace(type); |
| 3582 | if (xmlValidateNCName(type, 0)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3583 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE, |
| 3584 | "data type '%s' is not an NCName\n", type, NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3585 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3586 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
| 3587 | if (library == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3588 | library = |
| 3589 | xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3590 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3591 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3592 | if (def == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3593 | xmlFree(type); |
| 3594 | return (NULL); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3595 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3596 | def->type = XML_RELAXNG_DATATYPE; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3597 | def->name = type; |
| 3598 | def->ns = library; |
| 3599 | |
| 3600 | lib = (xmlRelaxNGTypeLibraryPtr) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3601 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3602 | if (lib == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3603 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB, |
| 3604 | "Use of unregistered type library '%s'\n", library, |
| 3605 | NULL); |
| 3606 | def->data = NULL; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3607 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3608 | def->data = lib; |
| 3609 | if (lib->have == NULL) { |
| 3610 | xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB, |
| 3611 | "Internal error with type library '%s': no 'have'\n", |
| 3612 | library, NULL); |
| 3613 | } else { |
| 3614 | tmp = lib->have(lib->data, def->name); |
| 3615 | if (tmp != 1) { |
| 3616 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND, |
| 3617 | "Error type '%s' is not exported by type library '%s'\n", |
| 3618 | def->name, library); |
| 3619 | } else |
| 3620 | if ((xmlStrEqual |
| 3621 | (library, |
| 3622 | BAD_CAST |
| 3623 | "http://www.w3.org/2001/XMLSchema-datatypes")) |
| 3624 | && ((xmlStrEqual(def->name, BAD_CAST "IDREF")) |
| 3625 | || (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) { |
| 3626 | ctxt->idref = 1; |
| 3627 | } |
| 3628 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3629 | } |
| 3630 | content = node->children; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3631 | |
| 3632 | /* |
| 3633 | * Handle optional params |
| 3634 | */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3635 | while (content != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3636 | if (!xmlStrEqual(content->name, BAD_CAST "param")) |
| 3637 | break; |
| 3638 | if (xmlStrEqual(library, |
| 3639 | BAD_CAST "http://relaxng.org/ns/structure/1.0")) { |
| 3640 | xmlRngPErr(ctxt, node, XML_RNGP_PARAM_FORBIDDEN, |
| 3641 | "Type library '%s' does not allow type parameters\n", |
| 3642 | library, NULL); |
| 3643 | content = content->next; |
| 3644 | while ((content != NULL) && |
| 3645 | (xmlStrEqual(content->name, BAD_CAST "param"))) |
| 3646 | content = content->next; |
| 3647 | } else { |
| 3648 | param = xmlRelaxNGNewDefine(ctxt, node); |
| 3649 | if (param != NULL) { |
| 3650 | param->type = XML_RELAXNG_PARAM; |
| 3651 | param->name = xmlGetProp(content, BAD_CAST "name"); |
| 3652 | if (param->name == NULL) { |
| 3653 | xmlRngPErr(ctxt, node, XML_RNGP_PARAM_NAME_MISSING, |
| 3654 | "param has no name\n", NULL, NULL); |
| 3655 | } |
| 3656 | param->value = xmlNodeGetContent(content); |
| 3657 | if (lastparam == NULL) { |
| 3658 | def->attrs = lastparam = param; |
| 3659 | } else { |
| 3660 | lastparam->next = param; |
| 3661 | lastparam = param; |
| 3662 | } |
| 3663 | if (lib != NULL) { |
| 3664 | } |
| 3665 | } |
| 3666 | content = content->next; |
| 3667 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3668 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3669 | /* |
| 3670 | * Handle optional except |
| 3671 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3672 | if ((content != NULL) |
| 3673 | && (xmlStrEqual(content->name, BAD_CAST "except"))) { |
| 3674 | xmlNodePtr child; |
| 3675 | xmlRelaxNGDefinePtr tmp2, last2 = NULL; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3676 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3677 | except = xmlRelaxNGNewDefine(ctxt, node); |
| 3678 | if (except == NULL) { |
| 3679 | return (def); |
| 3680 | } |
| 3681 | except->type = XML_RELAXNG_EXCEPT; |
| 3682 | child = content->children; |
| 3683 | if (last == NULL) { |
| 3684 | def->content = except; |
| 3685 | } else { |
| 3686 | last->next = except; |
| 3687 | } |
| 3688 | if (child == NULL) { |
| 3689 | xmlRngPErr(ctxt, content, XML_RNGP_EXCEPT_NO_CONTENT, |
| 3690 | "except has no content\n", NULL, NULL); |
| 3691 | } |
| 3692 | while (child != NULL) { |
| 3693 | tmp2 = xmlRelaxNGParsePattern(ctxt, child); |
| 3694 | if (tmp2 != NULL) { |
| 3695 | if (last2 == NULL) { |
| 3696 | except->content = last2 = tmp2; |
| 3697 | } else { |
| 3698 | last2->next = tmp2; |
| 3699 | last2 = tmp2; |
| 3700 | } |
| 3701 | } |
| 3702 | child = child->next; |
| 3703 | } |
| 3704 | content = content->next; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3705 | } |
| 3706 | /* |
| 3707 | * Check there is no unhandled data |
| 3708 | */ |
| 3709 | if (content != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3710 | xmlRngPErr(ctxt, content, XML_RNGP_DATA_CONTENT, |
| 3711 | "Element data has unexpected content %s\n", |
| 3712 | content->name, NULL); |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3713 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3714 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3715 | return (def); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3716 | } |
| 3717 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3718 | static const xmlChar *invalidName = BAD_CAST "\1"; |
| 3719 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3720 | /** |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3721 | * xmlRelaxNGCompareNameClasses: |
| 3722 | * @defs1: the first element/attribute defs |
| 3723 | * @defs2: the second element/attribute defs |
| 3724 | * @name: the restriction on the name |
| 3725 | * @ns: the restriction on the namespace |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3726 | * |
| 3727 | * Compare the 2 lists of element definitions. The comparison is |
| 3728 | * that if both lists do not accept the same QNames, it returns 1 |
| 3729 | * If the 2 lists can accept the same QName the comparison returns 0 |
| 3730 | * |
| 3731 | * Returns 1 disttinct, 0 if equal |
| 3732 | */ |
| 3733 | static int |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3734 | xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3735 | xmlRelaxNGDefinePtr def2) |
| 3736 | { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3737 | int ret = 1; |
| 3738 | xmlNode node; |
| 3739 | xmlNs ns; |
| 3740 | xmlRelaxNGValidCtxt ctxt; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3741 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3742 | ctxt.flags = FLAGS_IGNORABLE; |
| 3743 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 3744 | memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt)); |
| 3745 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3746 | if ((def1->type == XML_RELAXNG_ELEMENT) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3747 | (def1->type == XML_RELAXNG_ATTRIBUTE)) { |
| 3748 | if (def2->type == XML_RELAXNG_TEXT) |
| 3749 | return (1); |
| 3750 | if (def1->name != NULL) { |
| 3751 | node.name = def1->name; |
| 3752 | } else { |
| 3753 | node.name = invalidName; |
| 3754 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3755 | if (def1->ns != NULL) { |
| 3756 | if (def1->ns[0] == 0) { |
| 3757 | node.ns = NULL; |
| 3758 | } else { |
William M. Brack | a74a6ff | 2004-04-02 14:03:22 +0000 | [diff] [blame] | 3759 | node.ns = &ns; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3760 | ns.href = def1->ns; |
| 3761 | } |
| 3762 | } else { |
William M. Brack | a74a6ff | 2004-04-02 14:03:22 +0000 | [diff] [blame] | 3763 | node.ns = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3764 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3765 | if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3766 | if (def1->nameClass != NULL) { |
| 3767 | ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2); |
| 3768 | } else { |
| 3769 | ret = 0; |
| 3770 | } |
| 3771 | } else { |
| 3772 | ret = 1; |
| 3773 | } |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3774 | } else if (def1->type == XML_RELAXNG_TEXT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3775 | if (def2->type == XML_RELAXNG_TEXT) |
| 3776 | return (0); |
| 3777 | return (1); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3778 | } else if (def1->type == XML_RELAXNG_EXCEPT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3779 | TODO ret = 0; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3780 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3781 | TODO ret = 0; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3782 | } |
| 3783 | if (ret == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3784 | return (ret); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3785 | if ((def2->type == XML_RELAXNG_ELEMENT) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3786 | (def2->type == XML_RELAXNG_ATTRIBUTE)) { |
| 3787 | if (def2->name != NULL) { |
| 3788 | node.name = def2->name; |
| 3789 | } else { |
| 3790 | node.name = invalidName; |
| 3791 | } |
| 3792 | node.ns = &ns; |
| 3793 | if (def2->ns != NULL) { |
| 3794 | if (def2->ns[0] == 0) { |
| 3795 | node.ns = NULL; |
| 3796 | } else { |
| 3797 | ns.href = def2->ns; |
| 3798 | } |
| 3799 | } else { |
| 3800 | ns.href = invalidName; |
| 3801 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3802 | if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3803 | if (def2->nameClass != NULL) { |
| 3804 | ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1); |
| 3805 | } else { |
| 3806 | ret = 0; |
| 3807 | } |
| 3808 | } else { |
| 3809 | ret = 1; |
| 3810 | } |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3811 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3812 | TODO ret = 0; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3813 | } |
| 3814 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3815 | return (ret); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3816 | } |
| 3817 | |
| 3818 | /** |
| 3819 | * xmlRelaxNGCompareElemDefLists: |
| 3820 | * @ctxt: a Relax-NG parser context |
| 3821 | * @defs1: the first list of element/attribute defs |
| 3822 | * @defs2: the second list of element/attribute defs |
| 3823 | * |
| 3824 | * Compare the 2 lists of element or attribute definitions. The comparison |
| 3825 | * is that if both lists do not accept the same QNames, it returns 1 |
| 3826 | * If the 2 lists can accept the same QName the comparison returns 0 |
| 3827 | * |
| 3828 | * Returns 1 disttinct, 0 if equal |
| 3829 | */ |
| 3830 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3831 | xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt |
| 3832 | ATTRIBUTE_UNUSED, xmlRelaxNGDefinePtr * def1, |
| 3833 | xmlRelaxNGDefinePtr * def2) |
| 3834 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3835 | xmlRelaxNGDefinePtr *basedef2 = def2; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3836 | |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3837 | if ((def1 == NULL) || (def2 == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3838 | return (1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3839 | if ((*def1 == NULL) || (*def2 == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3840 | return (1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3841 | while (*def1 != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3842 | while ((*def2) != NULL) { |
| 3843 | if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0) |
| 3844 | return (0); |
| 3845 | def2++; |
| 3846 | } |
| 3847 | def2 = basedef2; |
| 3848 | def1++; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3849 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3850 | return (1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3851 | } |
| 3852 | |
| 3853 | /** |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3854 | * xmlRelaxNGGenerateAttributes: |
| 3855 | * @ctxt: a Relax-NG parser context |
| 3856 | * @def: the definition definition |
| 3857 | * |
| 3858 | * Check if the definition can only generate attributes |
| 3859 | * |
| 3860 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 3861 | */ |
| 3862 | static int |
| 3863 | xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3864 | xmlRelaxNGDefinePtr def) |
| 3865 | { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3866 | xmlRelaxNGDefinePtr parent, cur, tmp; |
| 3867 | |
| 3868 | /* |
| 3869 | * Don't run that check in case of error. Infinite recursion |
| 3870 | * becomes possible. |
| 3871 | */ |
| 3872 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3873 | return (-1); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3874 | |
| 3875 | parent = NULL; |
| 3876 | cur = def; |
| 3877 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3878 | if ((cur->type == XML_RELAXNG_ELEMENT) || |
| 3879 | (cur->type == XML_RELAXNG_TEXT) || |
| 3880 | (cur->type == XML_RELAXNG_DATATYPE) || |
| 3881 | (cur->type == XML_RELAXNG_PARAM) || |
| 3882 | (cur->type == XML_RELAXNG_LIST) || |
| 3883 | (cur->type == XML_RELAXNG_VALUE) || |
| 3884 | (cur->type == XML_RELAXNG_EMPTY)) |
| 3885 | return (0); |
| 3886 | if ((cur->type == XML_RELAXNG_CHOICE) || |
| 3887 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
| 3888 | (cur->type == XML_RELAXNG_GROUP) || |
| 3889 | (cur->type == XML_RELAXNG_ONEORMORE) || |
| 3890 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
| 3891 | (cur->type == XML_RELAXNG_OPTIONAL) || |
| 3892 | (cur->type == XML_RELAXNG_PARENTREF) || |
| 3893 | (cur->type == XML_RELAXNG_EXTERNALREF) || |
| 3894 | (cur->type == XML_RELAXNG_REF) || |
| 3895 | (cur->type == XML_RELAXNG_DEF)) { |
| 3896 | if (cur->content != NULL) { |
| 3897 | parent = cur; |
| 3898 | cur = cur->content; |
| 3899 | tmp = cur; |
| 3900 | while (tmp != NULL) { |
| 3901 | tmp->parent = parent; |
| 3902 | tmp = tmp->next; |
| 3903 | } |
| 3904 | continue; |
| 3905 | } |
| 3906 | } |
| 3907 | if (cur == def) |
| 3908 | break; |
| 3909 | if (cur->next != NULL) { |
| 3910 | cur = cur->next; |
| 3911 | continue; |
| 3912 | } |
| 3913 | do { |
| 3914 | cur = cur->parent; |
| 3915 | if (cur == NULL) |
| 3916 | break; |
| 3917 | if (cur == def) |
| 3918 | return (1); |
| 3919 | if (cur->next != NULL) { |
| 3920 | cur = cur->next; |
| 3921 | break; |
| 3922 | } |
| 3923 | } while (cur != NULL); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3924 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3925 | return (1); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3926 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3927 | |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3928 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3929 | * xmlRelaxNGGetElements: |
| 3930 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3931 | * @def: the definition definition |
| 3932 | * @eora: gather elements (0) or attributes (1) |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3933 | * |
| 3934 | * Compute the list of top elements a definition can generate |
| 3935 | * |
| 3936 | * Returns a list of elements or NULL if none was found. |
| 3937 | */ |
| 3938 | static xmlRelaxNGDefinePtr * |
| 3939 | xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3940 | xmlRelaxNGDefinePtr def, int eora) |
| 3941 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3942 | xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3943 | int len = 0; |
| 3944 | int max = 0; |
| 3945 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3946 | /* |
| 3947 | * Don't run that check in case of error. Infinite recursion |
| 3948 | * becomes possible. |
| 3949 | */ |
| 3950 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3951 | return (NULL); |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3952 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3953 | parent = NULL; |
| 3954 | cur = def; |
| 3955 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3956 | if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) || |
| 3957 | (cur->type == XML_RELAXNG_TEXT))) || |
| 3958 | ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) { |
| 3959 | if (ret == NULL) { |
| 3960 | max = 10; |
| 3961 | ret = (xmlRelaxNGDefinePtr *) |
| 3962 | xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
| 3963 | if (ret == NULL) { |
| 3964 | xmlRngPErrMemory(ctxt, "getting element list\n"); |
| 3965 | return (NULL); |
| 3966 | } |
| 3967 | } else if (max <= len) { |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 3968 | xmlRelaxNGDefinePtr *temp; |
| 3969 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3970 | max *= 2; |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 3971 | temp = xmlRealloc(ret, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3972 | (max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 3973 | if (temp == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3974 | xmlRngPErrMemory(ctxt, "getting element list\n"); |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 3975 | xmlFree(ret); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3976 | return (NULL); |
| 3977 | } |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 3978 | ret = temp; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3979 | } |
| 3980 | ret[len++] = cur; |
| 3981 | ret[len] = NULL; |
| 3982 | } else if ((cur->type == XML_RELAXNG_CHOICE) || |
| 3983 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
| 3984 | (cur->type == XML_RELAXNG_GROUP) || |
| 3985 | (cur->type == XML_RELAXNG_ONEORMORE) || |
| 3986 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
| 3987 | (cur->type == XML_RELAXNG_OPTIONAL) || |
| 3988 | (cur->type == XML_RELAXNG_PARENTREF) || |
| 3989 | (cur->type == XML_RELAXNG_REF) || |
William M. Brack | 236c8c0 | 2004-03-20 11:32:36 +0000 | [diff] [blame] | 3990 | (cur->type == XML_RELAXNG_DEF) || |
| 3991 | (cur->type == XML_RELAXNG_EXTERNALREF)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3992 | /* |
| 3993 | * Don't go within elements or attributes or string values. |
| 3994 | * Just gather the element top list |
| 3995 | */ |
| 3996 | if (cur->content != NULL) { |
| 3997 | parent = cur; |
| 3998 | cur = cur->content; |
| 3999 | tmp = cur; |
| 4000 | while (tmp != NULL) { |
| 4001 | tmp->parent = parent; |
| 4002 | tmp = tmp->next; |
| 4003 | } |
| 4004 | continue; |
| 4005 | } |
| 4006 | } |
| 4007 | if (cur == def) |
| 4008 | break; |
| 4009 | if (cur->next != NULL) { |
| 4010 | cur = cur->next; |
| 4011 | continue; |
| 4012 | } |
| 4013 | do { |
| 4014 | cur = cur->parent; |
| 4015 | if (cur == NULL) |
| 4016 | break; |
| 4017 | if (cur == def) |
| 4018 | return (ret); |
| 4019 | if (cur->next != NULL) { |
| 4020 | cur = cur->next; |
| 4021 | break; |
| 4022 | } |
| 4023 | } while (cur != NULL); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4024 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4025 | return (ret); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4026 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4027 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4028 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4029 | * xmlRelaxNGCheckChoiceDeterminism: |
| 4030 | * @ctxt: a Relax-NG parser context |
| 4031 | * @def: the choice definition |
| 4032 | * |
| 4033 | * Also used to find indeterministic pattern in choice |
| 4034 | */ |
| 4035 | static void |
| 4036 | xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4037 | xmlRelaxNGDefinePtr def) |
| 4038 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4039 | xmlRelaxNGDefinePtr **list; |
| 4040 | xmlRelaxNGDefinePtr cur; |
| 4041 | int nbchild = 0, i, j, ret; |
| 4042 | int is_nullable = 0; |
| 4043 | int is_indeterminist = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4044 | xmlHashTablePtr triage = NULL; |
| 4045 | int is_triable = 1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4046 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4047 | if ((def == NULL) || (def->type != XML_RELAXNG_CHOICE)) |
| 4048 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4049 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4050 | if (def->dflags & IS_PROCESSED) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4051 | return; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4052 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4053 | /* |
| 4054 | * Don't run that check in case of error. Infinite recursion |
| 4055 | * becomes possible. |
| 4056 | */ |
| 4057 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4058 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4059 | |
| 4060 | is_nullable = xmlRelaxNGIsNullable(def); |
| 4061 | |
| 4062 | cur = def->content; |
| 4063 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4064 | nbchild++; |
| 4065 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4066 | } |
| 4067 | |
| 4068 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4069 | sizeof(xmlRelaxNGDefinePtr |
| 4070 | *)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4071 | if (list == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4072 | xmlRngPErrMemory(ctxt, "building choice\n"); |
| 4073 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4074 | } |
| 4075 | i = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4076 | /* |
| 4077 | * a bit strong but safe |
| 4078 | */ |
| 4079 | if (is_nullable == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4080 | triage = xmlHashCreate(10); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4081 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4082 | is_triable = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4083 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4084 | cur = def->content; |
| 4085 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4086 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 0); |
| 4087 | if ((list[i] == NULL) || (list[i][0] == NULL)) { |
| 4088 | is_triable = 0; |
| 4089 | } else if (is_triable == 1) { |
| 4090 | xmlRelaxNGDefinePtr *tmp; |
| 4091 | int res; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4092 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4093 | tmp = list[i]; |
| 4094 | while ((*tmp != NULL) && (is_triable == 1)) { |
| 4095 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
| 4096 | res = xmlHashAddEntry2(triage, |
| 4097 | BAD_CAST "#text", NULL, |
| 4098 | (void *) cur); |
| 4099 | if (res != 0) |
| 4100 | is_triable = -1; |
| 4101 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
| 4102 | ((*tmp)->name != NULL)) { |
| 4103 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4104 | res = xmlHashAddEntry2(triage, |
| 4105 | (*tmp)->name, NULL, |
| 4106 | (void *) cur); |
| 4107 | else |
| 4108 | res = xmlHashAddEntry2(triage, |
| 4109 | (*tmp)->name, (*tmp)->ns, |
| 4110 | (void *) cur); |
| 4111 | if (res != 0) |
| 4112 | is_triable = -1; |
| 4113 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
| 4114 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4115 | res = xmlHashAddEntry2(triage, |
| 4116 | BAD_CAST "#any", NULL, |
| 4117 | (void *) cur); |
| 4118 | else |
| 4119 | res = xmlHashAddEntry2(triage, |
| 4120 | BAD_CAST "#any", (*tmp)->ns, |
| 4121 | (void *) cur); |
| 4122 | if (res != 0) |
| 4123 | is_triable = -1; |
| 4124 | } else { |
| 4125 | is_triable = -1; |
| 4126 | } |
| 4127 | tmp++; |
| 4128 | } |
| 4129 | } |
| 4130 | i++; |
| 4131 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4132 | } |
| 4133 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4134 | for (i = 0; i < nbchild; i++) { |
| 4135 | if (list[i] == NULL) |
| 4136 | continue; |
| 4137 | for (j = 0; j < i; j++) { |
| 4138 | if (list[j] == NULL) |
| 4139 | continue; |
| 4140 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
| 4141 | if (ret == 0) { |
| 4142 | is_indeterminist = 1; |
| 4143 | } |
| 4144 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4145 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4146 | for (i = 0; i < nbchild; i++) { |
| 4147 | if (list[i] != NULL) |
| 4148 | xmlFree(list[i]); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4149 | } |
| 4150 | |
| 4151 | xmlFree(list); |
| 4152 | if (is_indeterminist) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4153 | def->dflags |= IS_INDETERMINIST; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4154 | } |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4155 | if (is_triable == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4156 | def->dflags |= IS_TRIABLE; |
| 4157 | def->data = triage; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4158 | } else if (triage != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4159 | xmlHashFree(triage, NULL); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4160 | } |
| 4161 | def->dflags |= IS_PROCESSED; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4162 | } |
| 4163 | |
| 4164 | /** |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4165 | * xmlRelaxNGCheckGroupAttrs: |
| 4166 | * @ctxt: a Relax-NG parser context |
| 4167 | * @def: the group definition |
| 4168 | * |
| 4169 | * Detects violations of rule 7.3 |
| 4170 | */ |
| 4171 | static void |
| 4172 | xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4173 | xmlRelaxNGDefinePtr def) |
| 4174 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4175 | xmlRelaxNGDefinePtr **list; |
| 4176 | xmlRelaxNGDefinePtr cur; |
| 4177 | int nbchild = 0, i, j, ret; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4178 | |
| 4179 | if ((def == NULL) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4180 | ((def->type != XML_RELAXNG_GROUP) && |
| 4181 | (def->type != XML_RELAXNG_ELEMENT))) |
| 4182 | return; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4183 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4184 | if (def->dflags & IS_PROCESSED) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4185 | return; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4186 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4187 | /* |
| 4188 | * Don't run that check in case of error. Infinite recursion |
| 4189 | * becomes possible. |
| 4190 | */ |
| 4191 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4192 | return; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4193 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4194 | cur = def->attrs; |
| 4195 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4196 | nbchild++; |
| 4197 | cur = cur->next; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4198 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4199 | cur = def->content; |
| 4200 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4201 | nbchild++; |
| 4202 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4203 | } |
| 4204 | |
| 4205 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4206 | sizeof(xmlRelaxNGDefinePtr |
| 4207 | *)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4208 | if (list == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4209 | xmlRngPErrMemory(ctxt, "building group\n"); |
| 4210 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4211 | } |
| 4212 | i = 0; |
| 4213 | cur = def->attrs; |
| 4214 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4215 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4216 | i++; |
| 4217 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4218 | } |
| 4219 | cur = def->content; |
| 4220 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4221 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4222 | i++; |
| 4223 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4224 | } |
| 4225 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4226 | for (i = 0; i < nbchild; i++) { |
| 4227 | if (list[i] == NULL) |
| 4228 | continue; |
| 4229 | for (j = 0; j < i; j++) { |
| 4230 | if (list[j] == NULL) |
| 4231 | continue; |
| 4232 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
| 4233 | if (ret == 0) { |
| 4234 | xmlRngPErr(ctxt, def->node, XML_RNGP_GROUP_ATTR_CONFLICT, |
| 4235 | "Attributes conflicts in group\n", NULL, NULL); |
| 4236 | } |
| 4237 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4238 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4239 | for (i = 0; i < nbchild; i++) { |
| 4240 | if (list[i] != NULL) |
| 4241 | xmlFree(list[i]); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4242 | } |
| 4243 | |
| 4244 | xmlFree(list); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4245 | def->dflags |= IS_PROCESSED; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4246 | } |
| 4247 | |
| 4248 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4249 | * xmlRelaxNGComputeInterleaves: |
| 4250 | * @def: the interleave definition |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4251 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4252 | * @name: the definition name |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4253 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4254 | * A lot of work for preprocessing interleave definitions |
| 4255 | * is potentially needed to get a decent execution speed at runtime |
| 4256 | * - trying to get a total order on the element nodes generated |
| 4257 | * by the interleaves, order the list of interleave definitions |
| 4258 | * following that order. |
| 4259 | * - if <text/> is used to handle mixed content, it is better to |
| 4260 | * flag this in the define and simplify the runtime checking |
| 4261 | * algorithm |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4262 | */ |
| 4263 | static void |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4264 | xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4265 | xmlRelaxNGParserCtxtPtr ctxt, |
| 4266 | xmlChar * name ATTRIBUTE_UNUSED) |
| 4267 | { |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4268 | xmlRelaxNGDefinePtr cur, *tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4269 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4270 | xmlRelaxNGPartitionPtr partitions = NULL; |
| 4271 | xmlRelaxNGInterleaveGroupPtr *groups = NULL; |
| 4272 | xmlRelaxNGInterleaveGroupPtr group; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4273 | int i, j, ret, res; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4274 | int nbgroups = 0; |
| 4275 | int nbchild = 0; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 4276 | int is_mixed = 0; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4277 | int is_determinist = 1; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4278 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4279 | /* |
| 4280 | * Don't run that check in case of error. Infinite recursion |
| 4281 | * becomes possible. |
| 4282 | */ |
| 4283 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4284 | return; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4285 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4286 | #ifdef DEBUG_INTERLEAVE |
| 4287 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4288 | "xmlRelaxNGComputeInterleaves(%s)\n", name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4289 | #endif |
| 4290 | cur = def->content; |
| 4291 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4292 | nbchild++; |
| 4293 | cur = cur->next; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4294 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4295 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4296 | #ifdef DEBUG_INTERLEAVE |
| 4297 | xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild); |
| 4298 | #endif |
| 4299 | groups = (xmlRelaxNGInterleaveGroupPtr *) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4300 | xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4301 | if (groups == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4302 | goto error; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4303 | cur = def->content; |
| 4304 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4305 | groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr) |
| 4306 | xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup)); |
| 4307 | if (groups[nbgroups] == NULL) |
| 4308 | goto error; |
| 4309 | if (cur->type == XML_RELAXNG_TEXT) |
| 4310 | is_mixed++; |
| 4311 | groups[nbgroups]->rule = cur; |
| 4312 | groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0); |
| 4313 | groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4314 | nbgroups++; |
| 4315 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4316 | } |
| 4317 | #ifdef DEBUG_INTERLEAVE |
| 4318 | xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups); |
| 4319 | #endif |
| 4320 | |
| 4321 | /* |
| 4322 | * Let's check that all rules makes a partitions according to 7.4 |
| 4323 | */ |
| 4324 | partitions = (xmlRelaxNGPartitionPtr) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4325 | xmlMalloc(sizeof(xmlRelaxNGPartition)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4326 | if (partitions == NULL) |
| 4327 | goto error; |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 4328 | memset(partitions, 0, sizeof(xmlRelaxNGPartition)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4329 | partitions->nbgroups = nbgroups; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4330 | partitions->triage = xmlHashCreate(nbgroups); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4331 | for (i = 0; i < nbgroups; i++) { |
| 4332 | group = groups[i]; |
| 4333 | for (j = i + 1; j < nbgroups; j++) { |
| 4334 | if (groups[j] == NULL) |
| 4335 | continue; |
| 4336 | |
| 4337 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs, |
| 4338 | groups[j]->defs); |
| 4339 | if (ret == 0) { |
| 4340 | xmlRngPErr(ctxt, def->node, XML_RNGP_ELEM_TEXT_CONFLICT, |
| 4341 | "Element or text conflicts in interleave\n", |
| 4342 | NULL, NULL); |
| 4343 | } |
| 4344 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs, |
| 4345 | groups[j]->attrs); |
| 4346 | if (ret == 0) { |
| 4347 | xmlRngPErr(ctxt, def->node, XML_RNGP_ATTR_CONFLICT, |
| 4348 | "Attributes conflicts in interleave\n", NULL, |
| 4349 | NULL); |
| 4350 | } |
| 4351 | } |
| 4352 | tmp = group->defs; |
| 4353 | if ((tmp != NULL) && (*tmp != NULL)) { |
| 4354 | while (*tmp != NULL) { |
| 4355 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
| 4356 | res = xmlHashAddEntry2(partitions->triage, |
| 4357 | BAD_CAST "#text", NULL, |
| 4358 | (void *) (long) (i + 1)); |
| 4359 | if (res != 0) |
| 4360 | is_determinist = -1; |
| 4361 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
| 4362 | ((*tmp)->name != NULL)) { |
| 4363 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4364 | res = xmlHashAddEntry2(partitions->triage, |
| 4365 | (*tmp)->name, NULL, |
| 4366 | (void *) (long) (i + 1)); |
| 4367 | else |
| 4368 | res = xmlHashAddEntry2(partitions->triage, |
| 4369 | (*tmp)->name, (*tmp)->ns, |
| 4370 | (void *) (long) (i + 1)); |
| 4371 | if (res != 0) |
| 4372 | is_determinist = -1; |
| 4373 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
| 4374 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4375 | res = xmlHashAddEntry2(partitions->triage, |
| 4376 | BAD_CAST "#any", NULL, |
| 4377 | (void *) (long) (i + 1)); |
| 4378 | else |
| 4379 | res = xmlHashAddEntry2(partitions->triage, |
| 4380 | BAD_CAST "#any", (*tmp)->ns, |
| 4381 | (void *) (long) (i + 1)); |
| 4382 | if ((*tmp)->nameClass != NULL) |
| 4383 | is_determinist = 2; |
| 4384 | if (res != 0) |
| 4385 | is_determinist = -1; |
| 4386 | } else { |
| 4387 | is_determinist = -1; |
| 4388 | } |
| 4389 | tmp++; |
| 4390 | } |
| 4391 | } else { |
| 4392 | is_determinist = 0; |
| 4393 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4394 | } |
| 4395 | partitions->groups = groups; |
| 4396 | |
| 4397 | /* |
| 4398 | * and save the partition list back in the def |
| 4399 | */ |
| 4400 | def->data = partitions; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 4401 | if (is_mixed != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4402 | def->dflags |= IS_MIXED; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4403 | if (is_determinist == 1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4404 | partitions->flags = IS_DETERMINIST; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4405 | if (is_determinist == 2) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4406 | partitions->flags = IS_DETERMINIST | IS_NEEDCHECK; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4407 | return; |
| 4408 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4409 | error: |
| 4410 | xmlRngPErrMemory(ctxt, "in interleave computation\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4411 | if (groups != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4412 | for (i = 0; i < nbgroups; i++) |
| 4413 | if (groups[i] != NULL) { |
| 4414 | if (groups[i]->defs != NULL) |
| 4415 | xmlFree(groups[i]->defs); |
| 4416 | xmlFree(groups[i]); |
| 4417 | } |
| 4418 | xmlFree(groups); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4419 | } |
| 4420 | xmlRelaxNGFreePartition(partitions); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4421 | } |
| 4422 | |
| 4423 | /** |
| 4424 | * xmlRelaxNGParseInterleave: |
| 4425 | * @ctxt: a Relax-NG parser context |
| 4426 | * @node: the data node. |
| 4427 | * |
| 4428 | * parse the content of a RelaxNG interleave node. |
| 4429 | * |
| 4430 | * Returns the definition pointer or NULL in case of error |
| 4431 | */ |
| 4432 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4433 | xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4434 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4435 | xmlRelaxNGDefinePtr def = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4436 | xmlRelaxNGDefinePtr last = NULL, cur; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4437 | xmlNodePtr child; |
| 4438 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4439 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4440 | if (def == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4441 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4442 | } |
| 4443 | def->type = XML_RELAXNG_INTERLEAVE; |
| 4444 | |
| 4445 | if (ctxt->interleaves == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4446 | ctxt->interleaves = xmlHashCreate(10); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4447 | if (ctxt->interleaves == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4448 | xmlRngPErrMemory(ctxt, "create interleaves\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4449 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4450 | char name[32]; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4451 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4452 | snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++); |
| 4453 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) { |
| 4454 | xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_ADD, |
| 4455 | "Failed to add %s to hash table\n", |
| 4456 | (const xmlChar *) name, NULL); |
| 4457 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4458 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4459 | child = node->children; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4460 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4461 | xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_NO_CONTENT, |
| 4462 | "Element interleave is empty\n", NULL, NULL); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 4463 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4464 | while (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4465 | if (IS_RELAXNG(child, "element")) { |
| 4466 | cur = xmlRelaxNGParseElement(ctxt, child); |
| 4467 | } else { |
| 4468 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 4469 | } |
| 4470 | if (cur != NULL) { |
| 4471 | cur->parent = def; |
| 4472 | if (last == NULL) { |
| 4473 | def->content = last = cur; |
| 4474 | } else { |
| 4475 | last->next = cur; |
| 4476 | last = cur; |
| 4477 | } |
| 4478 | } |
| 4479 | child = child->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4480 | } |
| 4481 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4482 | return (def); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4483 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4484 | |
| 4485 | /** |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4486 | * xmlRelaxNGParseInclude: |
| 4487 | * @ctxt: a Relax-NG parser context |
| 4488 | * @node: the include node |
| 4489 | * |
| 4490 | * Integrate the content of an include node in the current grammar |
| 4491 | * |
| 4492 | * Returns 0 in case of success or -1 in case of error |
| 4493 | */ |
| 4494 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4495 | xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4496 | { |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4497 | xmlRelaxNGIncludePtr incl; |
| 4498 | xmlNodePtr root; |
| 4499 | int ret = 0, tmp; |
| 4500 | |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 4501 | incl = node->psvi; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4502 | if (incl == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4503 | xmlRngPErr(ctxt, node, XML_RNGP_INCLUDE_EMPTY, |
| 4504 | "Include node has no data\n", NULL, NULL); |
| 4505 | return (-1); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4506 | } |
| 4507 | root = xmlDocGetRootElement(incl->doc); |
| 4508 | if (root == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4509 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, "Include document is empty\n", |
| 4510 | NULL, NULL); |
| 4511 | return (-1); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4512 | } |
| 4513 | if (!xmlStrEqual(root->name, BAD_CAST "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4514 | xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING, |
| 4515 | "Include document root is not a grammar\n", NULL, NULL); |
| 4516 | return (-1); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4517 | } |
| 4518 | |
| 4519 | /* |
| 4520 | * Merge the definition from both the include and the internal list |
| 4521 | */ |
| 4522 | if (root->children != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4523 | tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children); |
| 4524 | if (tmp != 0) |
| 4525 | ret = -1; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4526 | } |
| 4527 | if (node->children != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4528 | tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children); |
| 4529 | if (tmp != 0) |
| 4530 | ret = -1; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4531 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4532 | return (ret); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4533 | } |
| 4534 | |
| 4535 | /** |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4536 | * xmlRelaxNGParseDefine: |
| 4537 | * @ctxt: a Relax-NG parser context |
| 4538 | * @node: the define node |
| 4539 | * |
| 4540 | * parse the content of a RelaxNG define element node. |
| 4541 | * |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4542 | * Returns 0 in case of success or -1 in case of error |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4543 | */ |
| 4544 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4545 | xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4546 | { |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4547 | xmlChar *name; |
| 4548 | int ret = 0, tmp; |
| 4549 | xmlRelaxNGDefinePtr def; |
| 4550 | const xmlChar *olddefine; |
| 4551 | |
| 4552 | name = xmlGetProp(node, BAD_CAST "name"); |
| 4553 | if (name == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4554 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_NAME_MISSING, |
| 4555 | "define has no name\n", NULL, NULL); |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4556 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4557 | xmlRelaxNGNormExtSpace(name); |
| 4558 | if (xmlValidateNCName(name, 0)) { |
| 4559 | xmlRngPErr(ctxt, node, XML_RNGP_INVALID_DEFINE_NAME, |
| 4560 | "define name '%s' is not an NCName\n", name, NULL); |
| 4561 | } |
| 4562 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4563 | if (def == NULL) { |
| 4564 | xmlFree(name); |
| 4565 | return (-1); |
| 4566 | } |
| 4567 | def->type = XML_RELAXNG_DEF; |
| 4568 | def->name = name; |
| 4569 | if (node->children == NULL) { |
| 4570 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_EMPTY, |
| 4571 | "define has no children\n", NULL, NULL); |
| 4572 | } else { |
| 4573 | olddefine = ctxt->define; |
| 4574 | ctxt->define = name; |
| 4575 | def->content = |
| 4576 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4577 | ctxt->define = olddefine; |
| 4578 | } |
| 4579 | if (ctxt->grammar->defs == NULL) |
| 4580 | ctxt->grammar->defs = xmlHashCreate(10); |
| 4581 | if (ctxt->grammar->defs == NULL) { |
| 4582 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED, |
| 4583 | "Could not create definition hash\n", NULL, NULL); |
| 4584 | ret = -1; |
| 4585 | } else { |
| 4586 | tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def); |
| 4587 | if (tmp < 0) { |
| 4588 | xmlRelaxNGDefinePtr prev; |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 4589 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4590 | prev = xmlHashLookup(ctxt->grammar->defs, name); |
| 4591 | if (prev == NULL) { |
| 4592 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED, |
| 4593 | "Internal error on define aggregation of %s\n", |
| 4594 | name, NULL); |
| 4595 | ret = -1; |
| 4596 | } else { |
| 4597 | while (prev->nextHash != NULL) |
| 4598 | prev = prev->nextHash; |
| 4599 | prev->nextHash = def; |
| 4600 | } |
| 4601 | } |
| 4602 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4603 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4604 | return (ret); |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4605 | } |
| 4606 | |
| 4607 | /** |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4608 | * xmlRelaxNGProcessExternalRef: |
| 4609 | * @ctxt: the parser context |
| 4610 | * @node: the externlRef node |
| 4611 | * |
| 4612 | * Process and compile an externlRef node |
| 4613 | * |
| 4614 | * Returns the xmlRelaxNGDefinePtr or NULL in case of error |
| 4615 | */ |
| 4616 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4617 | xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4618 | { |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4619 | xmlRelaxNGDocumentPtr docu; |
| 4620 | xmlNodePtr root, tmp; |
| 4621 | xmlChar *ns; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4622 | int newNs = 0, oldflags; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4623 | xmlRelaxNGDefinePtr def; |
| 4624 | |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 4625 | docu = node->psvi; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4626 | if (docu != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4627 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4628 | if (def == NULL) |
| 4629 | return (NULL); |
| 4630 | def->type = XML_RELAXNG_EXTERNALREF; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4631 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4632 | if (docu->content == NULL) { |
| 4633 | /* |
| 4634 | * Then do the parsing for good |
| 4635 | */ |
| 4636 | root = xmlDocGetRootElement(docu->doc); |
| 4637 | if (root == NULL) { |
| 4638 | xmlRngPErr(ctxt, node, XML_RNGP_EXTERNALREF_EMTPY, |
| 4639 | "xmlRelaxNGParse: %s is empty\n", ctxt->URL, |
| 4640 | NULL); |
| 4641 | return (NULL); |
| 4642 | } |
| 4643 | /* |
| 4644 | * ns transmission rules |
| 4645 | */ |
| 4646 | ns = xmlGetProp(root, BAD_CAST "ns"); |
| 4647 | if (ns == NULL) { |
| 4648 | tmp = node; |
| 4649 | while ((tmp != NULL) && (tmp->type == XML_ELEMENT_NODE)) { |
| 4650 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 4651 | if (ns != NULL) { |
| 4652 | break; |
| 4653 | } |
| 4654 | tmp = tmp->parent; |
| 4655 | } |
| 4656 | if (ns != NULL) { |
| 4657 | xmlSetProp(root, BAD_CAST "ns", ns); |
| 4658 | newNs = 1; |
| 4659 | xmlFree(ns); |
| 4660 | } |
| 4661 | } else { |
| 4662 | xmlFree(ns); |
| 4663 | } |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4664 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4665 | /* |
| 4666 | * Parsing to get a precompiled schemas. |
| 4667 | */ |
| 4668 | oldflags = ctxt->flags; |
| 4669 | ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF; |
| 4670 | docu->schema = xmlRelaxNGParseDocument(ctxt, root); |
| 4671 | ctxt->flags = oldflags; |
| 4672 | if ((docu->schema != NULL) && |
| 4673 | (docu->schema->topgrammar != NULL)) { |
| 4674 | docu->content = docu->schema->topgrammar->start; |
| 4675 | } |
| 4676 | |
| 4677 | /* |
| 4678 | * the externalRef may be reused in a different ns context |
| 4679 | */ |
| 4680 | if (newNs == 1) { |
| 4681 | xmlUnsetProp(root, BAD_CAST "ns"); |
| 4682 | } |
| 4683 | } |
| 4684 | def->content = docu->content; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4685 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4686 | def = NULL; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4687 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4688 | return (def); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4689 | } |
| 4690 | |
| 4691 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4692 | * xmlRelaxNGParsePattern: |
| 4693 | * @ctxt: a Relax-NG parser context |
| 4694 | * @node: the pattern node. |
| 4695 | * |
| 4696 | * parse the content of a RelaxNG pattern node. |
| 4697 | * |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4698 | * Returns the definition pointer or NULL in case of error or if no |
| 4699 | * pattern is generated. |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4700 | */ |
| 4701 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4702 | xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4703 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4704 | xmlRelaxNGDefinePtr def = NULL; |
| 4705 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4706 | if (node == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4707 | return (NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4708 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4709 | if (IS_RELAXNG(node, "element")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4710 | def = xmlRelaxNGParseElement(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4711 | } else if (IS_RELAXNG(node, "attribute")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4712 | def = xmlRelaxNGParseAttribute(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4713 | } else if (IS_RELAXNG(node, "empty")) { |
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_EMPTY; |
| 4718 | if (node->children != NULL) { |
| 4719 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_NOT_EMPTY, |
| 4720 | "empty: 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, "text")) { |
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_TEXT; |
| 4727 | if (node->children != NULL) { |
| 4728 | xmlRngPErr(ctxt, node, XML_RNGP_TEXT_HAS_CHILD, |
| 4729 | "text: had a child node\n", NULL, NULL); |
| 4730 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4731 | } else if (IS_RELAXNG(node, "zeroOrMore")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4732 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4733 | if (def == NULL) |
| 4734 | return (NULL); |
| 4735 | def->type = XML_RELAXNG_ZEROORMORE; |
| 4736 | if (node->children == NULL) { |
| 4737 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4738 | "Element %s is empty\n", node->name, NULL); |
| 4739 | } else { |
| 4740 | def->content = |
| 4741 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4742 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4743 | } else if (IS_RELAXNG(node, "oneOrMore")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4744 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4745 | if (def == NULL) |
| 4746 | return (NULL); |
| 4747 | def->type = XML_RELAXNG_ONEORMORE; |
| 4748 | if (node->children == NULL) { |
| 4749 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4750 | "Element %s is empty\n", node->name, NULL); |
| 4751 | } else { |
| 4752 | def->content = |
| 4753 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4754 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4755 | } else if (IS_RELAXNG(node, "optional")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4756 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4757 | if (def == NULL) |
| 4758 | return (NULL); |
| 4759 | def->type = XML_RELAXNG_OPTIONAL; |
| 4760 | if (node->children == NULL) { |
| 4761 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4762 | "Element %s is empty\n", node->name, NULL); |
| 4763 | } else { |
| 4764 | def->content = |
| 4765 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4766 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4767 | } else if (IS_RELAXNG(node, "choice")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4768 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4769 | if (def == NULL) |
| 4770 | return (NULL); |
| 4771 | def->type = XML_RELAXNG_CHOICE; |
| 4772 | if (node->children == NULL) { |
| 4773 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4774 | "Element %s is empty\n", node->name, NULL); |
| 4775 | } else { |
| 4776 | def->content = |
| 4777 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4778 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4779 | } else if (IS_RELAXNG(node, "group")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4780 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4781 | if (def == NULL) |
| 4782 | return (NULL); |
| 4783 | def->type = XML_RELAXNG_GROUP; |
| 4784 | if (node->children == NULL) { |
| 4785 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4786 | "Element %s is empty\n", node->name, NULL); |
| 4787 | } else { |
| 4788 | def->content = |
| 4789 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4790 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4791 | } else if (IS_RELAXNG(node, "ref")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4792 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4793 | if (def == NULL) |
| 4794 | return (NULL); |
| 4795 | def->type = XML_RELAXNG_REF; |
| 4796 | def->name = xmlGetProp(node, BAD_CAST "name"); |
| 4797 | if (def->name == NULL) { |
| 4798 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NO_NAME, "ref has no name\n", |
| 4799 | NULL, NULL); |
| 4800 | } else { |
| 4801 | xmlRelaxNGNormExtSpace(def->name); |
| 4802 | if (xmlValidateNCName(def->name, 0)) { |
| 4803 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NAME_INVALID, |
| 4804 | "ref name '%s' is not an NCName\n", def->name, |
| 4805 | NULL); |
| 4806 | } |
| 4807 | } |
| 4808 | if (node->children != NULL) { |
| 4809 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NOT_EMPTY, "ref is not empty\n", |
| 4810 | NULL, NULL); |
| 4811 | } |
| 4812 | if (ctxt->grammar->refs == NULL) |
| 4813 | ctxt->grammar->refs = xmlHashCreate(10); |
| 4814 | if (ctxt->grammar->refs == NULL) { |
| 4815 | xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED, |
| 4816 | "Could not create references hash\n", NULL, NULL); |
| 4817 | def = NULL; |
| 4818 | } else { |
| 4819 | int tmp; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4820 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4821 | tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def); |
| 4822 | if (tmp < 0) { |
| 4823 | xmlRelaxNGDefinePtr prev; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4824 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4825 | prev = (xmlRelaxNGDefinePtr) |
| 4826 | xmlHashLookup(ctxt->grammar->refs, def->name); |
| 4827 | if (prev == NULL) { |
| 4828 | if (def->name != NULL) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 4829 | xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED, |
| 4830 | "Error refs definitions '%s'\n", |
| 4831 | def->name, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4832 | } else { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 4833 | xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED, |
| 4834 | "Error refs definitions\n", |
| 4835 | NULL, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4836 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4837 | def = NULL; |
| 4838 | } else { |
| 4839 | def->nextHash = prev->nextHash; |
| 4840 | prev->nextHash = def; |
| 4841 | } |
| 4842 | } |
| 4843 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 4844 | } else if (IS_RELAXNG(node, "data")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4845 | def = xmlRelaxNGParseData(ctxt, node); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 4846 | } else if (IS_RELAXNG(node, "value")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4847 | def = xmlRelaxNGParseValue(ctxt, node); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 4848 | } else if (IS_RELAXNG(node, "list")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4849 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4850 | if (def == NULL) |
| 4851 | return (NULL); |
| 4852 | def->type = XML_RELAXNG_LIST; |
| 4853 | if (node->children == NULL) { |
| 4854 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4855 | "Element %s is empty\n", node->name, NULL); |
| 4856 | } else { |
| 4857 | def->content = |
| 4858 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4859 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4860 | } else if (IS_RELAXNG(node, "interleave")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4861 | def = xmlRelaxNGParseInterleave(ctxt, node); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4862 | } else if (IS_RELAXNG(node, "externalRef")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4863 | def = xmlRelaxNGProcessExternalRef(ctxt, node); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4864 | } else if (IS_RELAXNG(node, "notAllowed")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4865 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4866 | if (def == NULL) |
| 4867 | return (NULL); |
| 4868 | def->type = XML_RELAXNG_NOT_ALLOWED; |
| 4869 | if (node->children != NULL) { |
| 4870 | xmlRngPErr(ctxt, node, XML_RNGP_NOTALLOWED_NOT_EMPTY, |
| 4871 | "xmlRelaxNGParse: notAllowed element is not empty\n", |
| 4872 | NULL, NULL); |
| 4873 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4874 | } else if (IS_RELAXNG(node, "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4875 | xmlRelaxNGGrammarPtr grammar, old; |
| 4876 | xmlRelaxNGGrammarPtr oldparent; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4877 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4878 | #ifdef DEBUG_GRAMMAR |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4879 | xmlGenericError(xmlGenericErrorContext, |
| 4880 | "Found <grammar> pattern\n"); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4881 | #endif |
| 4882 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4883 | oldparent = ctxt->parentgrammar; |
| 4884 | old = ctxt->grammar; |
| 4885 | ctxt->parentgrammar = old; |
| 4886 | grammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
| 4887 | if (old != NULL) { |
| 4888 | ctxt->grammar = old; |
| 4889 | ctxt->parentgrammar = oldparent; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4890 | #if 0 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4891 | if (grammar != NULL) { |
| 4892 | grammar->next = old->next; |
| 4893 | old->next = grammar; |
| 4894 | } |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4895 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4896 | } |
| 4897 | if (grammar != NULL) |
| 4898 | def = grammar->start; |
| 4899 | else |
| 4900 | def = NULL; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4901 | } else if (IS_RELAXNG(node, "parentRef")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4902 | if (ctxt->parentgrammar == NULL) { |
| 4903 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_PARENT, |
| 4904 | "Use of parentRef without a parent grammar\n", NULL, |
| 4905 | NULL); |
| 4906 | return (NULL); |
| 4907 | } |
| 4908 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4909 | if (def == NULL) |
| 4910 | return (NULL); |
| 4911 | def->type = XML_RELAXNG_PARENTREF; |
| 4912 | def->name = xmlGetProp(node, BAD_CAST "name"); |
| 4913 | if (def->name == NULL) { |
| 4914 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_NAME, |
| 4915 | "parentRef has no name\n", NULL, NULL); |
| 4916 | } else { |
| 4917 | xmlRelaxNGNormExtSpace(def->name); |
| 4918 | if (xmlValidateNCName(def->name, 0)) { |
| 4919 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NAME_INVALID, |
| 4920 | "parentRef name '%s' is not an NCName\n", |
| 4921 | def->name, NULL); |
| 4922 | } |
| 4923 | } |
| 4924 | if (node->children != NULL) { |
| 4925 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NOT_EMPTY, |
| 4926 | "parentRef is not empty\n", NULL, NULL); |
| 4927 | } |
| 4928 | if (ctxt->parentgrammar->refs == NULL) |
| 4929 | ctxt->parentgrammar->refs = xmlHashCreate(10); |
| 4930 | if (ctxt->parentgrammar->refs == NULL) { |
| 4931 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED, |
| 4932 | "Could not create references hash\n", NULL, NULL); |
| 4933 | def = NULL; |
| 4934 | } else if (def->name != NULL) { |
| 4935 | int tmp; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4936 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4937 | tmp = |
| 4938 | xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def); |
| 4939 | if (tmp < 0) { |
| 4940 | xmlRelaxNGDefinePtr prev; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4941 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4942 | prev = (xmlRelaxNGDefinePtr) |
| 4943 | xmlHashLookup(ctxt->parentgrammar->refs, def->name); |
| 4944 | if (prev == NULL) { |
| 4945 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED, |
| 4946 | "Internal error parentRef definitions '%s'\n", |
| 4947 | def->name, NULL); |
| 4948 | def = NULL; |
| 4949 | } else { |
| 4950 | def->nextHash = prev->nextHash; |
| 4951 | prev->nextHash = def; |
| 4952 | } |
| 4953 | } |
| 4954 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4955 | } else if (IS_RELAXNG(node, "mixed")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4956 | if (node->children == NULL) { |
| 4957 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, "Mixed is empty\n", |
| 4958 | NULL, NULL); |
| 4959 | def = NULL; |
| 4960 | } else { |
| 4961 | def = xmlRelaxNGParseInterleave(ctxt, node); |
| 4962 | if (def != NULL) { |
| 4963 | xmlRelaxNGDefinePtr tmp; |
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 | if ((def->content != NULL) && (def->content->next != NULL)) { |
| 4966 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
| 4967 | if (tmp != NULL) { |
| 4968 | tmp->type = XML_RELAXNG_GROUP; |
| 4969 | tmp->content = def->content; |
| 4970 | def->content = tmp; |
| 4971 | } |
| 4972 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4973 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4974 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
| 4975 | if (tmp == NULL) |
| 4976 | return (def); |
| 4977 | tmp->type = XML_RELAXNG_TEXT; |
| 4978 | tmp->next = def->content; |
| 4979 | def->content = tmp; |
| 4980 | } |
| 4981 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4982 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4983 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_CONSTRUCT, |
| 4984 | "Unexpected node %s is not a pattern\n", node->name, |
| 4985 | NULL); |
| 4986 | def = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4987 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4988 | return (def); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4989 | } |
| 4990 | |
| 4991 | /** |
| 4992 | * xmlRelaxNGParseAttribute: |
| 4993 | * @ctxt: a Relax-NG parser context |
| 4994 | * @node: the element node |
| 4995 | * |
| 4996 | * parse the content of a RelaxNG attribute node. |
| 4997 | * |
| 4998 | * Returns the definition pointer or NULL in case of error. |
| 4999 | */ |
| 5000 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5001 | xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 5002 | { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5003 | xmlRelaxNGDefinePtr ret, cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5004 | xmlNodePtr child; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5005 | int old_flags; |
| 5006 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5007 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5008 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5009 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5010 | ret->type = XML_RELAXNG_ATTRIBUTE; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5011 | ret->parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5012 | child = node->children; |
| 5013 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5014 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_EMPTY, |
| 5015 | "xmlRelaxNGParseattribute: attribute has no children\n", |
| 5016 | NULL, NULL); |
| 5017 | return (ret); |
| 5018 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5019 | old_flags = ctxt->flags; |
| 5020 | ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5021 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5022 | if (cur != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5023 | child = child->next; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5024 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5025 | if (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5026 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 5027 | if (cur != NULL) { |
| 5028 | switch (cur->type) { |
| 5029 | case XML_RELAXNG_EMPTY: |
| 5030 | case XML_RELAXNG_NOT_ALLOWED: |
| 5031 | case XML_RELAXNG_TEXT: |
| 5032 | case XML_RELAXNG_ELEMENT: |
| 5033 | case XML_RELAXNG_DATATYPE: |
| 5034 | case XML_RELAXNG_VALUE: |
| 5035 | case XML_RELAXNG_LIST: |
| 5036 | case XML_RELAXNG_REF: |
| 5037 | case XML_RELAXNG_PARENTREF: |
| 5038 | case XML_RELAXNG_EXTERNALREF: |
| 5039 | case XML_RELAXNG_DEF: |
| 5040 | case XML_RELAXNG_ONEORMORE: |
| 5041 | case XML_RELAXNG_ZEROORMORE: |
| 5042 | case XML_RELAXNG_OPTIONAL: |
| 5043 | case XML_RELAXNG_CHOICE: |
| 5044 | case XML_RELAXNG_GROUP: |
| 5045 | case XML_RELAXNG_INTERLEAVE: |
| 5046 | case XML_RELAXNG_ATTRIBUTE: |
| 5047 | ret->content = cur; |
| 5048 | cur->parent = ret; |
| 5049 | break; |
| 5050 | case XML_RELAXNG_START: |
| 5051 | case XML_RELAXNG_PARAM: |
| 5052 | case XML_RELAXNG_EXCEPT: |
| 5053 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CONTENT, |
| 5054 | "attribute has invalid content\n", NULL, |
| 5055 | NULL); |
| 5056 | break; |
| 5057 | case XML_RELAXNG_NOOP: |
| 5058 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_NOOP, |
| 5059 | "RNG Internal error, noop found in attribute\n", |
| 5060 | NULL, NULL); |
| 5061 | break; |
| 5062 | } |
| 5063 | } |
| 5064 | child = child->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5065 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5066 | if (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5067 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CHILDREN, |
| 5068 | "attribute has multiple children\n", NULL, NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5069 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5070 | ctxt->flags = old_flags; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5071 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5072 | } |
| 5073 | |
| 5074 | /** |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5075 | * xmlRelaxNGParseExceptNameClass: |
| 5076 | * @ctxt: a Relax-NG parser context |
| 5077 | * @node: the except node |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5078 | * @attr: 1 if within an attribute, 0 if within an element |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5079 | * |
| 5080 | * parse the content of a RelaxNG nameClass node. |
| 5081 | * |
| 5082 | * Returns the definition pointer or NULL in case of error. |
| 5083 | */ |
| 5084 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5085 | xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5086 | xmlNodePtr node, int attr) |
| 5087 | { |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5088 | xmlRelaxNGDefinePtr ret, cur, last = NULL; |
| 5089 | xmlNodePtr child; |
| 5090 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5091 | if (!IS_RELAXNG(node, "except")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5092 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MISSING, |
| 5093 | "Expecting an except node\n", NULL, NULL); |
| 5094 | return (NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5095 | } |
| 5096 | if (node->next != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5097 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MULTIPLE, |
| 5098 | "exceptNameClass allows only a single except node\n", |
| 5099 | NULL, NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5100 | } |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5101 | if (node->children == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5102 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_EMPTY, "except has no content\n", |
| 5103 | NULL, NULL); |
| 5104 | return (NULL); |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5105 | } |
| 5106 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5107 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5108 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5109 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5110 | ret->type = XML_RELAXNG_EXCEPT; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5111 | child = node->children; |
| 5112 | while (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5113 | cur = xmlRelaxNGNewDefine(ctxt, child); |
| 5114 | if (cur == NULL) |
| 5115 | break; |
| 5116 | if (attr) |
| 5117 | cur->type = XML_RELAXNG_ATTRIBUTE; |
| 5118 | else |
| 5119 | cur->type = XML_RELAXNG_ELEMENT; |
| 5120 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 5121 | if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5122 | if (last == NULL) { |
| 5123 | ret->content = cur; |
| 5124 | } else { |
| 5125 | last->next = cur; |
| 5126 | } |
| 5127 | last = cur; |
| 5128 | } |
| 5129 | child = child->next; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5130 | } |
| 5131 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5132 | return (ret); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5133 | } |
| 5134 | |
| 5135 | /** |
| 5136 | * xmlRelaxNGParseNameClass: |
| 5137 | * @ctxt: a Relax-NG parser context |
| 5138 | * @node: the nameClass node |
| 5139 | * @def: the current definition |
| 5140 | * |
| 5141 | * parse the content of a RelaxNG nameClass node. |
| 5142 | * |
| 5143 | * Returns the definition pointer or NULL in case of error. |
| 5144 | */ |
| 5145 | static xmlRelaxNGDefinePtr |
| 5146 | xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5147 | xmlRelaxNGDefinePtr def) |
| 5148 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5149 | xmlRelaxNGDefinePtr ret, tmp; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5150 | xmlChar *val; |
| 5151 | |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5152 | ret = def; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5153 | if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) || |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5154 | (IS_RELAXNG(node, "nsName"))) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5155 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 5156 | (def->type != XML_RELAXNG_ATTRIBUTE)) { |
| 5157 | ret = xmlRelaxNGNewDefine(ctxt, node); |
| 5158 | if (ret == NULL) |
| 5159 | return (NULL); |
| 5160 | ret->parent = def; |
| 5161 | if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) |
| 5162 | ret->type = XML_RELAXNG_ATTRIBUTE; |
| 5163 | else |
| 5164 | ret->type = XML_RELAXNG_ELEMENT; |
| 5165 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5166 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5167 | if (IS_RELAXNG(node, "name")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5168 | val = xmlNodeGetContent(node); |
| 5169 | xmlRelaxNGNormExtSpace(val); |
| 5170 | if (xmlValidateNCName(val, 0)) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5171 | if (node->parent != NULL) |
| 5172 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME, |
| 5173 | "Element %s name '%s' is not an NCName\n", |
| 5174 | node->parent->name, val); |
| 5175 | else |
| 5176 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME, |
| 5177 | "name '%s' is not an NCName\n", |
| 5178 | val, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5179 | } |
| 5180 | ret->name = val; |
| 5181 | val = xmlGetProp(node, BAD_CAST "ns"); |
| 5182 | ret->ns = val; |
| 5183 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5184 | (val != NULL) && |
| 5185 | (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5186 | xmlRngPErr(ctxt, node, XML_RNGP_XML_NS, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5187 | "Attribute with namespace '%s' is not allowed\n", |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5188 | val, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5189 | } |
| 5190 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5191 | (val != NULL) && |
| 5192 | (val[0] == 0) && (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5193 | xmlRngPErr(ctxt, node, XML_RNGP_XMLNS_NAME, |
| 5194 | "Attribute with QName 'xmlns' is not allowed\n", |
| 5195 | val, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5196 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5197 | } else if (IS_RELAXNG(node, "anyName")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5198 | ret->name = NULL; |
| 5199 | ret->ns = NULL; |
| 5200 | if (node->children != NULL) { |
| 5201 | ret->nameClass = |
| 5202 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
| 5203 | (def->type == |
| 5204 | XML_RELAXNG_ATTRIBUTE)); |
| 5205 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5206 | } else if (IS_RELAXNG(node, "nsName")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5207 | ret->name = NULL; |
| 5208 | ret->ns = xmlGetProp(node, BAD_CAST "ns"); |
| 5209 | if (ret->ns == NULL) { |
| 5210 | xmlRngPErr(ctxt, node, XML_RNGP_NSNAME_NO_NS, |
| 5211 | "nsName has no ns attribute\n", NULL, NULL); |
| 5212 | } |
| 5213 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5214 | (ret->ns != NULL) && |
| 5215 | (xmlStrEqual |
| 5216 | (ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
| 5217 | xmlRngPErr(ctxt, node, XML_RNGP_XML_NS, |
| 5218 | "Attribute with namespace '%s' is not allowed\n", |
| 5219 | ret->ns, NULL); |
| 5220 | } |
| 5221 | if (node->children != NULL) { |
| 5222 | ret->nameClass = |
| 5223 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
| 5224 | (def->type == |
| 5225 | XML_RELAXNG_ATTRIBUTE)); |
| 5226 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5227 | } else if (IS_RELAXNG(node, "choice")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5228 | xmlNodePtr child; |
| 5229 | xmlRelaxNGDefinePtr last = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5230 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5231 | ret = xmlRelaxNGNewDefine(ctxt, node); |
| 5232 | if (ret == NULL) |
| 5233 | return (NULL); |
| 5234 | ret->parent = def; |
| 5235 | ret->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5236 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5237 | if (node->children == NULL) { |
| 5238 | xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_EMPTY, |
| 5239 | "Element choice is empty\n", NULL, NULL); |
| 5240 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5241 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5242 | child = node->children; |
| 5243 | while (child != NULL) { |
| 5244 | tmp = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5245 | if (tmp != NULL) { |
| 5246 | if (last == NULL) { |
| 5247 | last = ret->nameClass = tmp; |
| 5248 | } else { |
| 5249 | last->next = tmp; |
| 5250 | last = tmp; |
| 5251 | } |
| 5252 | } |
| 5253 | child = child->next; |
| 5254 | } |
| 5255 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5256 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5257 | xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_CONTENT, |
| 5258 | "expecting name, anyName, nsName or choice : got %s\n", |
| 5259 | node->name, NULL); |
| 5260 | return (NULL); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5261 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5262 | if (ret != def) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5263 | if (def->nameClass == NULL) { |
| 5264 | def->nameClass = ret; |
| 5265 | } else { |
| 5266 | tmp = def->nameClass; |
| 5267 | while (tmp->next != NULL) { |
| 5268 | tmp = tmp->next; |
| 5269 | } |
| 5270 | tmp->next = ret; |
| 5271 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5272 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5273 | return (ret); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5274 | } |
| 5275 | |
| 5276 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5277 | * xmlRelaxNGParseElement: |
| 5278 | * @ctxt: a Relax-NG parser context |
| 5279 | * @node: the element node |
| 5280 | * |
| 5281 | * parse the content of a RelaxNG element node. |
| 5282 | * |
| 5283 | * Returns the definition pointer or NULL in case of error. |
| 5284 | */ |
| 5285 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5286 | xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 5287 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5288 | xmlRelaxNGDefinePtr ret, cur, last; |
| 5289 | xmlNodePtr child; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5290 | const xmlChar *olddefine; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5291 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5292 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5293 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5294 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5295 | ret->type = XML_RELAXNG_ELEMENT; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5296 | ret->parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5297 | child = node->children; |
| 5298 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5299 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_EMPTY, |
| 5300 | "xmlRelaxNGParseElement: element has no children\n", |
| 5301 | NULL, NULL); |
| 5302 | return (ret); |
| 5303 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5304 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5305 | if (cur != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5306 | child = child->next; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5307 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5308 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5309 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NO_CONTENT, |
| 5310 | "xmlRelaxNGParseElement: element has no content\n", |
| 5311 | NULL, NULL); |
| 5312 | return (ret); |
| 5313 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5314 | olddefine = ctxt->define; |
| 5315 | ctxt->define = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5316 | last = NULL; |
| 5317 | while (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5318 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 5319 | if (cur != NULL) { |
| 5320 | cur->parent = ret; |
| 5321 | switch (cur->type) { |
| 5322 | case XML_RELAXNG_EMPTY: |
| 5323 | case XML_RELAXNG_NOT_ALLOWED: |
| 5324 | case XML_RELAXNG_TEXT: |
| 5325 | case XML_RELAXNG_ELEMENT: |
| 5326 | case XML_RELAXNG_DATATYPE: |
| 5327 | case XML_RELAXNG_VALUE: |
| 5328 | case XML_RELAXNG_LIST: |
| 5329 | case XML_RELAXNG_REF: |
| 5330 | case XML_RELAXNG_PARENTREF: |
| 5331 | case XML_RELAXNG_EXTERNALREF: |
| 5332 | case XML_RELAXNG_DEF: |
| 5333 | case XML_RELAXNG_ZEROORMORE: |
| 5334 | case XML_RELAXNG_ONEORMORE: |
| 5335 | case XML_RELAXNG_OPTIONAL: |
| 5336 | case XML_RELAXNG_CHOICE: |
| 5337 | case XML_RELAXNG_GROUP: |
| 5338 | case XML_RELAXNG_INTERLEAVE: |
| 5339 | if (last == NULL) { |
| 5340 | ret->content = last = cur; |
| 5341 | } else { |
| 5342 | if ((last->type == XML_RELAXNG_ELEMENT) && |
| 5343 | (ret->content == last)) { |
| 5344 | ret->content = xmlRelaxNGNewDefine(ctxt, node); |
| 5345 | if (ret->content != NULL) { |
| 5346 | ret->content->type = XML_RELAXNG_GROUP; |
| 5347 | ret->content->content = last; |
| 5348 | } else { |
| 5349 | ret->content = last; |
| 5350 | } |
| 5351 | } |
| 5352 | last->next = cur; |
| 5353 | last = cur; |
| 5354 | } |
| 5355 | break; |
| 5356 | case XML_RELAXNG_ATTRIBUTE: |
| 5357 | cur->next = ret->attrs; |
| 5358 | ret->attrs = cur; |
| 5359 | break; |
| 5360 | case XML_RELAXNG_START: |
| 5361 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5362 | "RNG Internal error, start found in element\n", |
| 5363 | NULL, NULL); |
| 5364 | break; |
| 5365 | case XML_RELAXNG_PARAM: |
| 5366 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5367 | "RNG Internal error, param found in element\n", |
| 5368 | NULL, NULL); |
| 5369 | break; |
| 5370 | case XML_RELAXNG_EXCEPT: |
| 5371 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5372 | "RNG Internal error, except found in element\n", |
| 5373 | NULL, NULL); |
| 5374 | break; |
| 5375 | case XML_RELAXNG_NOOP: |
| 5376 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5377 | "RNG Internal error, noop found in element\n", |
| 5378 | NULL, NULL); |
| 5379 | break; |
| 5380 | } |
| 5381 | } |
| 5382 | child = child->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5383 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5384 | ctxt->define = olddefine; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5385 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5386 | } |
| 5387 | |
| 5388 | /** |
| 5389 | * xmlRelaxNGParsePatterns: |
| 5390 | * @ctxt: a Relax-NG parser context |
| 5391 | * @nodes: list of nodes |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5392 | * @group: use an implicit <group> for elements |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5393 | * |
| 5394 | * parse the content of a RelaxNG start node. |
| 5395 | * |
| 5396 | * Returns the definition pointer or NULL in case of error. |
| 5397 | */ |
| 5398 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5399 | xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5400 | int group) |
| 5401 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5402 | xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5403 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5404 | parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5405 | while (nodes != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5406 | if (IS_RELAXNG(nodes, "element")) { |
| 5407 | cur = xmlRelaxNGParseElement(ctxt, nodes); |
| 5408 | if (def == NULL) { |
| 5409 | def = last = cur; |
| 5410 | } else { |
| 5411 | if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) && |
| 5412 | (def == last)) { |
| 5413 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
| 5414 | def->type = XML_RELAXNG_GROUP; |
| 5415 | def->content = last; |
| 5416 | } |
| 5417 | last->next = cur; |
| 5418 | last = cur; |
| 5419 | } |
| 5420 | cur->parent = parent; |
| 5421 | } else { |
| 5422 | cur = xmlRelaxNGParsePattern(ctxt, nodes); |
| 5423 | if (cur != NULL) { |
| 5424 | if (def == NULL) { |
| 5425 | def = last = cur; |
| 5426 | } else { |
| 5427 | last->next = cur; |
| 5428 | last = cur; |
| 5429 | } |
| 5430 | } |
| 5431 | } |
| 5432 | nodes = nodes->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5433 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5434 | return (def); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5435 | } |
| 5436 | |
| 5437 | /** |
| 5438 | * xmlRelaxNGParseStart: |
| 5439 | * @ctxt: a Relax-NG parser context |
| 5440 | * @nodes: start children nodes |
| 5441 | * |
| 5442 | * parse the content of a RelaxNG start node. |
| 5443 | * |
| 5444 | * Returns 0 in case of success, -1 in case of error |
| 5445 | */ |
| 5446 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5447 | xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) |
| 5448 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5449 | int ret = 0; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5450 | xmlRelaxNGDefinePtr def = NULL, last; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5451 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5452 | if (nodes == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5453 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, "start has no children\n", |
| 5454 | NULL, NULL); |
| 5455 | return (-1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5456 | } |
| 5457 | if (IS_RELAXNG(nodes, "empty")) { |
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_EMPTY; |
| 5462 | if (nodes->children != NULL) { |
| 5463 | xmlRngPErr(ctxt, nodes, XML_RNGP_EMPTY_CONTENT, |
| 5464 | "element empty is not empty\n", NULL, NULL); |
| 5465 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5466 | } else if (IS_RELAXNG(nodes, "notAllowed")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5467 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
| 5468 | if (def == NULL) |
| 5469 | return (-1); |
| 5470 | def->type = XML_RELAXNG_NOT_ALLOWED; |
| 5471 | if (nodes->children != NULL) { |
| 5472 | xmlRngPErr(ctxt, nodes, XML_RNGP_NOTALLOWED_NOT_EMPTY, |
| 5473 | "element notAllowed is not empty\n", NULL, NULL); |
| 5474 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5475 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5476 | def = xmlRelaxNGParsePatterns(ctxt, nodes, 1); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5477 | } |
| 5478 | if (ctxt->grammar->start != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5479 | last = ctxt->grammar->start; |
| 5480 | while (last->next != NULL) |
| 5481 | last = last->next; |
| 5482 | last->next = def; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5483 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5484 | ctxt->grammar->start = def; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5485 | } |
| 5486 | nodes = nodes->next; |
| 5487 | if (nodes != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5488 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_CONTENT, |
| 5489 | "start more than one children\n", NULL, NULL); |
| 5490 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5491 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5492 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5493 | } |
| 5494 | |
| 5495 | /** |
| 5496 | * xmlRelaxNGParseGrammarContent: |
| 5497 | * @ctxt: a Relax-NG parser context |
| 5498 | * @nodes: grammar children nodes |
| 5499 | * |
| 5500 | * parse the content of a RelaxNG grammar node. |
| 5501 | * |
| 5502 | * Returns 0 in case of success, -1 in case of error |
| 5503 | */ |
| 5504 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5505 | xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt, |
| 5506 | xmlNodePtr nodes) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5507 | { |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5508 | int ret = 0, tmp; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5509 | |
| 5510 | if (nodes == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5511 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_EMPTY, |
| 5512 | "grammar has no children\n", NULL, NULL); |
| 5513 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5514 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5515 | while (nodes != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5516 | if (IS_RELAXNG(nodes, "start")) { |
| 5517 | if (nodes->children == NULL) { |
| 5518 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, |
| 5519 | "start has no children\n", NULL, NULL); |
| 5520 | } else { |
| 5521 | tmp = xmlRelaxNGParseStart(ctxt, nodes->children); |
| 5522 | if (tmp != 0) |
| 5523 | ret = -1; |
| 5524 | } |
| 5525 | } else if (IS_RELAXNG(nodes, "define")) { |
| 5526 | tmp = xmlRelaxNGParseDefine(ctxt, nodes); |
| 5527 | if (tmp != 0) |
| 5528 | ret = -1; |
| 5529 | } else if (IS_RELAXNG(nodes, "include")) { |
| 5530 | tmp = xmlRelaxNGParseInclude(ctxt, nodes); |
| 5531 | if (tmp != 0) |
| 5532 | ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5533 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5534 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT, |
| 5535 | "grammar has unexpected child %s\n", nodes->name, |
| 5536 | NULL); |
| 5537 | ret = -1; |
| 5538 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5539 | nodes = nodes->next; |
| 5540 | } |
| 5541 | return (ret); |
| 5542 | } |
| 5543 | |
| 5544 | /** |
| 5545 | * xmlRelaxNGCheckReference: |
| 5546 | * @ref: the ref |
| 5547 | * @ctxt: a Relax-NG parser context |
| 5548 | * @name: the name associated to the defines |
| 5549 | * |
| 5550 | * Applies the 4.17. combine attribute rule for all the define |
| 5551 | * element of a given grammar using the same name. |
| 5552 | */ |
| 5553 | static void |
| 5554 | xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5555 | xmlRelaxNGParserCtxtPtr ctxt, |
| 5556 | const xmlChar * name) |
| 5557 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5558 | xmlRelaxNGGrammarPtr grammar; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5559 | xmlRelaxNGDefinePtr def, cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5560 | |
| 5561 | grammar = ctxt->grammar; |
| 5562 | if (grammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5563 | xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR, |
| 5564 | "Internal error: no grammar in CheckReference %s\n", |
| 5565 | name, NULL); |
| 5566 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5567 | } |
| 5568 | if (ref->content != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5569 | xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR, |
| 5570 | "Internal error: reference has content in CheckReference %s\n", |
| 5571 | name, NULL); |
| 5572 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5573 | } |
| 5574 | if (grammar->defs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5575 | def = xmlHashLookup(grammar->defs, name); |
| 5576 | if (def != NULL) { |
| 5577 | cur = ref; |
| 5578 | while (cur != NULL) { |
| 5579 | cur->content = def; |
| 5580 | cur = cur->nextHash; |
| 5581 | } |
| 5582 | } else { |
| 5583 | xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF, |
| 5584 | "Reference %s has no matching definition\n", name, |
| 5585 | NULL); |
| 5586 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5587 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5588 | xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF, |
| 5589 | "Reference %s has no matching definition\n", name, |
| 5590 | NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5591 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5592 | } |
| 5593 | |
| 5594 | /** |
| 5595 | * xmlRelaxNGCheckCombine: |
| 5596 | * @define: the define(s) list |
| 5597 | * @ctxt: a Relax-NG parser context |
| 5598 | * @name: the name associated to the defines |
| 5599 | * |
| 5600 | * Applies the 4.17. combine attribute rule for all the define |
| 5601 | * element of a given grammar using the same name. |
| 5602 | */ |
| 5603 | static void |
| 5604 | xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5605 | xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * name) |
| 5606 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5607 | xmlChar *combine; |
| 5608 | int choiceOrInterleave = -1; |
| 5609 | int missing = 0; |
| 5610 | xmlRelaxNGDefinePtr cur, last, tmp, tmp2; |
| 5611 | |
| 5612 | if (define->nextHash == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5613 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5614 | cur = define; |
| 5615 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5616 | combine = xmlGetProp(cur->node, BAD_CAST "combine"); |
| 5617 | if (combine != NULL) { |
| 5618 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
| 5619 | if (choiceOrInterleave == -1) |
| 5620 | choiceOrInterleave = 1; |
| 5621 | else if (choiceOrInterleave == 0) { |
| 5622 | xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE, |
| 5623 | "Defines for %s use both 'choice' and 'interleave'\n", |
| 5624 | name, NULL); |
| 5625 | } |
| 5626 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
| 5627 | if (choiceOrInterleave == -1) |
| 5628 | choiceOrInterleave = 0; |
| 5629 | else if (choiceOrInterleave == 1) { |
| 5630 | xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE, |
| 5631 | "Defines for %s use both 'choice' and 'interleave'\n", |
| 5632 | name, NULL); |
| 5633 | } |
| 5634 | } else { |
| 5635 | xmlRngPErr(ctxt, define->node, XML_RNGP_UNKNOWN_COMBINE, |
| 5636 | "Defines for %s use unknown combine value '%s''\n", |
| 5637 | name, combine); |
| 5638 | } |
| 5639 | xmlFree(combine); |
| 5640 | } else { |
| 5641 | if (missing == 0) |
| 5642 | missing = 1; |
| 5643 | else { |
| 5644 | xmlRngPErr(ctxt, define->node, XML_RNGP_NEED_COMBINE, |
| 5645 | "Some defines for %s needs the combine attribute\n", |
| 5646 | name, NULL); |
| 5647 | } |
| 5648 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5649 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5650 | cur = cur->nextHash; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5651 | } |
| 5652 | #ifdef DEBUG |
| 5653 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5654 | "xmlRelaxNGCheckCombine(): merging %s defines: %d\n", |
| 5655 | name, choiceOrInterleave); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5656 | #endif |
| 5657 | if (choiceOrInterleave == -1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5658 | choiceOrInterleave = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5659 | cur = xmlRelaxNGNewDefine(ctxt, define->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5660 | if (cur == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5661 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5662 | if (choiceOrInterleave == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5663 | cur->type = XML_RELAXNG_INTERLEAVE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5664 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5665 | cur->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5666 | tmp = define; |
| 5667 | last = NULL; |
| 5668 | while (tmp != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5669 | if (tmp->content != NULL) { |
| 5670 | if (tmp->content->next != NULL) { |
| 5671 | /* |
| 5672 | * we need first to create a wrapper. |
| 5673 | */ |
| 5674 | tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node); |
| 5675 | if (tmp2 == NULL) |
| 5676 | break; |
| 5677 | tmp2->type = XML_RELAXNG_GROUP; |
| 5678 | tmp2->content = tmp->content; |
| 5679 | } else { |
| 5680 | tmp2 = tmp->content; |
| 5681 | } |
| 5682 | if (last == NULL) { |
| 5683 | cur->content = tmp2; |
| 5684 | } else { |
| 5685 | last->next = tmp2; |
| 5686 | } |
| 5687 | last = tmp2; |
| 5688 | } |
| 5689 | tmp->content = cur; |
| 5690 | tmp = tmp->nextHash; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5691 | } |
| 5692 | define->content = cur; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5693 | if (choiceOrInterleave == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5694 | if (ctxt->interleaves == NULL) |
| 5695 | ctxt->interleaves = xmlHashCreate(10); |
| 5696 | if (ctxt->interleaves == NULL) { |
| 5697 | xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5698 | "Failed to create interleaves hash table\n", NULL, |
| 5699 | NULL); |
| 5700 | } else { |
| 5701 | char tmpname[32]; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5702 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5703 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
| 5704 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < |
| 5705 | 0) { |
| 5706 | xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5707 | "Failed to add %s to hash table\n", |
| 5708 | (const xmlChar *) tmpname, NULL); |
| 5709 | } |
| 5710 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5711 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5712 | } |
| 5713 | |
| 5714 | /** |
| 5715 | * xmlRelaxNGCombineStart: |
| 5716 | * @ctxt: a Relax-NG parser context |
| 5717 | * @grammar: the grammar |
| 5718 | * |
| 5719 | * Applies the 4.17. combine rule for all the start |
| 5720 | * element of a given grammar. |
| 5721 | */ |
| 5722 | static void |
| 5723 | xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5724 | xmlRelaxNGGrammarPtr grammar) |
| 5725 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5726 | xmlRelaxNGDefinePtr starts; |
| 5727 | xmlChar *combine; |
| 5728 | int choiceOrInterleave = -1; |
| 5729 | int missing = 0; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5730 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5731 | |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5732 | starts = grammar->start; |
| 5733 | if ((starts == NULL) || (starts->next == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5734 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5735 | cur = starts; |
| 5736 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5737 | if ((cur->node == NULL) || (cur->node->parent == NULL) || |
| 5738 | (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) { |
| 5739 | combine = NULL; |
| 5740 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_MISSING, |
| 5741 | "Internal error: start element not found\n", NULL, |
| 5742 | NULL); |
| 5743 | } else { |
| 5744 | combine = xmlGetProp(cur->node->parent, BAD_CAST "combine"); |
| 5745 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5746 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5747 | if (combine != NULL) { |
| 5748 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
| 5749 | if (choiceOrInterleave == -1) |
| 5750 | choiceOrInterleave = 1; |
| 5751 | else if (choiceOrInterleave == 0) { |
| 5752 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE, |
| 5753 | "<start> use both 'choice' and 'interleave'\n", |
| 5754 | NULL, NULL); |
| 5755 | } |
| 5756 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
| 5757 | if (choiceOrInterleave == -1) |
| 5758 | choiceOrInterleave = 0; |
| 5759 | else if (choiceOrInterleave == 1) { |
| 5760 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE, |
| 5761 | "<start> use both 'choice' and 'interleave'\n", |
| 5762 | NULL, NULL); |
| 5763 | } |
| 5764 | } else { |
| 5765 | xmlRngPErr(ctxt, cur->node, XML_RNGP_UNKNOWN_COMBINE, |
| 5766 | "<start> uses unknown combine value '%s''\n", |
| 5767 | combine, NULL); |
| 5768 | } |
| 5769 | xmlFree(combine); |
| 5770 | } else { |
| 5771 | if (missing == 0) |
| 5772 | missing = 1; |
| 5773 | else { |
| 5774 | xmlRngPErr(ctxt, cur->node, XML_RNGP_NEED_COMBINE, |
| 5775 | "Some <start> element miss the combine attribute\n", |
| 5776 | NULL, NULL); |
| 5777 | } |
| 5778 | } |
| 5779 | |
| 5780 | cur = cur->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5781 | } |
| 5782 | #ifdef DEBUG |
| 5783 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5784 | "xmlRelaxNGCombineStart(): merging <start>: %d\n", |
| 5785 | choiceOrInterleave); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5786 | #endif |
| 5787 | if (choiceOrInterleave == -1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5788 | choiceOrInterleave = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5789 | cur = xmlRelaxNGNewDefine(ctxt, starts->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5790 | if (cur == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5791 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5792 | if (choiceOrInterleave == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5793 | cur->type = XML_RELAXNG_INTERLEAVE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5794 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5795 | cur->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5796 | cur->content = grammar->start; |
| 5797 | grammar->start = cur; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5798 | if (choiceOrInterleave == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5799 | if (ctxt->interleaves == NULL) |
| 5800 | ctxt->interleaves = xmlHashCreate(10); |
| 5801 | if (ctxt->interleaves == NULL) { |
| 5802 | xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5803 | "Failed to create interleaves hash table\n", NULL, |
| 5804 | NULL); |
| 5805 | } else { |
| 5806 | char tmpname[32]; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5807 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5808 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
| 5809 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < |
| 5810 | 0) { |
| 5811 | xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5812 | "Failed to add %s to hash table\n", |
| 5813 | (const xmlChar *) tmpname, NULL); |
| 5814 | } |
| 5815 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5816 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5817 | } |
| 5818 | |
| 5819 | /** |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5820 | * xmlRelaxNGCheckCycles: |
| 5821 | * @ctxt: a Relax-NG parser context |
| 5822 | * @nodes: grammar children nodes |
| 5823 | * @depth: the counter |
| 5824 | * |
| 5825 | * Check for cycles. |
| 5826 | * |
| 5827 | * Returns 0 if check passed, and -1 in case of error |
| 5828 | */ |
| 5829 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5830 | xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt, |
| 5831 | xmlRelaxNGDefinePtr cur, int depth) |
| 5832 | { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5833 | int ret = 0; |
| 5834 | |
| 5835 | while ((ret == 0) && (cur != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5836 | if ((cur->type == XML_RELAXNG_REF) || |
| 5837 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5838 | if (cur->depth == -1) { |
| 5839 | cur->depth = depth; |
| 5840 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
| 5841 | cur->depth = -2; |
| 5842 | } else if (depth == cur->depth) { |
| 5843 | xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_CYCLE, |
| 5844 | "Detected a cycle in %s references\n", |
| 5845 | cur->name, NULL); |
| 5846 | return (-1); |
| 5847 | } |
| 5848 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
| 5849 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1); |
| 5850 | } else { |
| 5851 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
| 5852 | } |
| 5853 | cur = cur->next; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5854 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5855 | return (ret); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5856 | } |
| 5857 | |
| 5858 | /** |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5859 | * xmlRelaxNGTryUnlink: |
| 5860 | * @ctxt: a Relax-NG parser context |
| 5861 | * @cur: the definition to unlink |
| 5862 | * @parent: the parent definition |
| 5863 | * @prev: the previous sibling definition |
| 5864 | * |
| 5865 | * Try to unlink a definition. If not possble make it a NOOP |
| 5866 | * |
| 5867 | * Returns the new prev definition |
| 5868 | */ |
| 5869 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5870 | xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 5871 | xmlRelaxNGDefinePtr cur, |
| 5872 | xmlRelaxNGDefinePtr parent, xmlRelaxNGDefinePtr prev) |
| 5873 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5874 | if (prev != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5875 | prev->next = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5876 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5877 | if (parent != NULL) { |
| 5878 | if (parent->content == cur) |
| 5879 | parent->content = cur->next; |
| 5880 | else if (parent->attrs == cur) |
| 5881 | parent->attrs = cur->next; |
| 5882 | else if (parent->nameClass == cur) |
| 5883 | parent->nameClass = cur->next; |
| 5884 | } else { |
| 5885 | cur->type = XML_RELAXNG_NOOP; |
| 5886 | prev = cur; |
| 5887 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5888 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5889 | return (prev); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5890 | } |
| 5891 | |
| 5892 | /** |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5893 | * xmlRelaxNGSimplify: |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5894 | * @ctxt: a Relax-NG parser context |
| 5895 | * @nodes: grammar children nodes |
| 5896 | * |
| 5897 | * Check for simplification of empty and notAllowed |
| 5898 | */ |
| 5899 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5900 | xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt, |
| 5901 | xmlRelaxNGDefinePtr cur, xmlRelaxNGDefinePtr parent) |
| 5902 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5903 | xmlRelaxNGDefinePtr prev = NULL; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5904 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5905 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5906 | if ((cur->type == XML_RELAXNG_REF) || |
| 5907 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5908 | if (cur->depth != -3) { |
| 5909 | cur->depth = -3; |
| 5910 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
| 5911 | } |
| 5912 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
| 5913 | cur->parent = parent; |
| 5914 | if ((parent != NULL) && |
| 5915 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
| 5916 | (parent->type == XML_RELAXNG_LIST) || |
| 5917 | (parent->type == XML_RELAXNG_GROUP) || |
| 5918 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 5919 | (parent->type == XML_RELAXNG_ONEORMORE) || |
| 5920 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 5921 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
| 5922 | break; |
| 5923 | } |
| 5924 | if ((parent != NULL) && (parent->type == XML_RELAXNG_CHOICE)) { |
| 5925 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5926 | } else |
| 5927 | prev = cur; |
| 5928 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 5929 | cur->parent = parent; |
| 5930 | if ((parent != NULL) && |
| 5931 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
| 5932 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 5933 | parent->type = XML_RELAXNG_EMPTY; |
| 5934 | break; |
| 5935 | } |
| 5936 | if ((parent != NULL) && |
| 5937 | ((parent->type == XML_RELAXNG_GROUP) || |
| 5938 | (parent->type == XML_RELAXNG_INTERLEAVE))) { |
| 5939 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5940 | } else |
| 5941 | prev = cur; |
| 5942 | } else { |
| 5943 | cur->parent = parent; |
| 5944 | if (cur->content != NULL) |
| 5945 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
| 5946 | if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL)) |
| 5947 | xmlRelaxNGSimplify(ctxt, cur->attrs, cur); |
| 5948 | if (cur->nameClass != NULL) |
| 5949 | xmlRelaxNGSimplify(ctxt, cur->nameClass, cur); |
| 5950 | /* |
| 5951 | * On Elements, try to move attribute only generating rules on |
| 5952 | * the attrs rules. |
| 5953 | */ |
| 5954 | if (cur->type == XML_RELAXNG_ELEMENT) { |
| 5955 | int attronly; |
| 5956 | xmlRelaxNGDefinePtr tmp, pre; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 5957 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5958 | while (cur->content != NULL) { |
| 5959 | attronly = |
| 5960 | xmlRelaxNGGenerateAttributes(ctxt, cur->content); |
| 5961 | if (attronly == 1) { |
| 5962 | /* |
| 5963 | * migrate cur->content to attrs |
| 5964 | */ |
| 5965 | tmp = cur->content; |
| 5966 | cur->content = tmp->next; |
| 5967 | tmp->next = cur->attrs; |
| 5968 | cur->attrs = tmp; |
| 5969 | } else { |
| 5970 | /* |
| 5971 | * cur->content can generate elements or text |
| 5972 | */ |
| 5973 | break; |
| 5974 | } |
| 5975 | } |
| 5976 | pre = cur->content; |
| 5977 | while ((pre != NULL) && (pre->next != NULL)) { |
| 5978 | tmp = pre->next; |
| 5979 | attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp); |
| 5980 | if (attronly == 1) { |
| 5981 | /* |
| 5982 | * migrate tmp to attrs |
| 5983 | */ |
| 5984 | pre->next = tmp->next; |
| 5985 | tmp->next = cur->attrs; |
| 5986 | cur->attrs = tmp; |
| 5987 | } else { |
| 5988 | pre = tmp; |
| 5989 | } |
| 5990 | } |
| 5991 | } |
| 5992 | /* |
| 5993 | * This may result in a simplification |
| 5994 | */ |
| 5995 | if ((cur->type == XML_RELAXNG_GROUP) || |
| 5996 | (cur->type == XML_RELAXNG_INTERLEAVE)) { |
| 5997 | if (cur->content == NULL) |
| 5998 | cur->type = XML_RELAXNG_EMPTY; |
| 5999 | else if (cur->content->next == NULL) { |
| 6000 | if ((parent == NULL) && (prev == NULL)) { |
| 6001 | cur->type = XML_RELAXNG_NOOP; |
| 6002 | } else if (prev == NULL) { |
| 6003 | parent->content = cur->content; |
| 6004 | cur->content->next = cur->next; |
| 6005 | cur = cur->content; |
| 6006 | } else { |
| 6007 | cur->content->next = cur->next; |
| 6008 | prev->next = cur->content; |
| 6009 | cur = cur->content; |
| 6010 | } |
| 6011 | } |
| 6012 | } |
| 6013 | /* |
| 6014 | * the current node may have been transformed back |
| 6015 | */ |
| 6016 | if ((cur->type == XML_RELAXNG_EXCEPT) && |
| 6017 | (cur->content != NULL) && |
| 6018 | (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) { |
| 6019 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 6020 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
| 6021 | if ((parent != NULL) && |
| 6022 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
| 6023 | (parent->type == XML_RELAXNG_LIST) || |
| 6024 | (parent->type == XML_RELAXNG_GROUP) || |
| 6025 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 6026 | (parent->type == XML_RELAXNG_ONEORMORE) || |
| 6027 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 6028 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
| 6029 | break; |
| 6030 | } |
| 6031 | if ((parent != NULL) && |
| 6032 | (parent->type == XML_RELAXNG_CHOICE)) { |
| 6033 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 6034 | } else |
| 6035 | prev = cur; |
| 6036 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 6037 | if ((parent != NULL) && |
| 6038 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
| 6039 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 6040 | parent->type = XML_RELAXNG_EMPTY; |
| 6041 | break; |
| 6042 | } |
| 6043 | if ((parent != NULL) && |
| 6044 | ((parent->type == XML_RELAXNG_GROUP) || |
| 6045 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 6046 | (parent->type == XML_RELAXNG_CHOICE))) { |
| 6047 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 6048 | } else |
| 6049 | prev = cur; |
| 6050 | } else { |
| 6051 | prev = cur; |
| 6052 | } |
| 6053 | } |
| 6054 | cur = cur->next; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6055 | } |
| 6056 | } |
| 6057 | |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6058 | /** |
| 6059 | * xmlRelaxNGGroupContentType: |
| 6060 | * @ct1: the first content type |
| 6061 | * @ct2: the second content type |
| 6062 | * |
| 6063 | * Try to group 2 content types |
| 6064 | * |
| 6065 | * Returns the content type |
| 6066 | */ |
| 6067 | static xmlRelaxNGContentType |
| 6068 | xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6069 | xmlRelaxNGContentType ct2) |
| 6070 | { |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6071 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6072 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
| 6073 | return (XML_RELAXNG_CONTENT_ERROR); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6074 | if (ct1 == XML_RELAXNG_CONTENT_EMPTY) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6075 | return (ct2); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6076 | if (ct2 == XML_RELAXNG_CONTENT_EMPTY) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6077 | return (ct1); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6078 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6079 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
| 6080 | return (XML_RELAXNG_CONTENT_COMPLEX); |
| 6081 | return (XML_RELAXNG_CONTENT_ERROR); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6082 | } |
| 6083 | |
| 6084 | /** |
| 6085 | * xmlRelaxNGMaxContentType: |
| 6086 | * @ct1: the first content type |
| 6087 | * @ct2: the second content type |
| 6088 | * |
| 6089 | * Compute the max content-type |
| 6090 | * |
| 6091 | * Returns the content type |
| 6092 | */ |
| 6093 | static xmlRelaxNGContentType |
| 6094 | xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6095 | xmlRelaxNGContentType ct2) |
| 6096 | { |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6097 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6098 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
| 6099 | return (XML_RELAXNG_CONTENT_ERROR); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6100 | if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6101 | (ct2 == XML_RELAXNG_CONTENT_SIMPLE)) |
| 6102 | return (XML_RELAXNG_CONTENT_SIMPLE); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6103 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6104 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
| 6105 | return (XML_RELAXNG_CONTENT_COMPLEX); |
| 6106 | return (XML_RELAXNG_CONTENT_EMPTY); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6107 | } |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6108 | |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6109 | /** |
| 6110 | * xmlRelaxNGCheckRules: |
| 6111 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6112 | * @cur: the current definition |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6113 | * @flags: some accumulated flags |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6114 | * @ptype: the parent type |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6115 | * |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6116 | * Check for rules in section 7.1 and 7.2 |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6117 | * |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6118 | * Returns the content type of @cur |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6119 | */ |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6120 | static xmlRelaxNGContentType |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6121 | xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt, |
| 6122 | xmlRelaxNGDefinePtr cur, int flags, |
| 6123 | xmlRelaxNGType ptype) |
| 6124 | { |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6125 | int nflags = flags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6126 | xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6127 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6128 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6129 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6130 | if ((cur->type == XML_RELAXNG_REF) || |
| 6131 | (cur->type == XML_RELAXNG_PARENTREF)) { |
Daniel Veillard | 63d68a3 | 2005-03-31 13:50:00 +0000 | [diff] [blame] | 6132 | /* |
| 6133 | * This should actually be caught by list//element(ref) at the |
| 6134 | * element boundaries, c.f. Bug #159968 local refs are dropped |
| 6135 | * in step 4.19. |
| 6136 | */ |
| 6137 | #if 0 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6138 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6139 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_REF, |
| 6140 | "Found forbidden pattern list//ref\n", NULL, |
| 6141 | NULL); |
| 6142 | } |
Daniel Veillard | 63d68a3 | 2005-03-31 13:50:00 +0000 | [diff] [blame] | 6143 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6144 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6145 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_REF, |
| 6146 | "Found forbidden pattern data/except//ref\n", |
| 6147 | NULL, NULL); |
| 6148 | } |
| 6149 | if (cur->depth > -4) { |
| 6150 | cur->depth = -4; |
| 6151 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, |
| 6152 | flags, cur->type); |
| 6153 | cur->depth = ret - 15; |
| 6154 | } else if (cur->depth == -4) { |
| 6155 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6156 | } else { |
| 6157 | ret = (xmlRelaxNGContentType) (cur->depth + 15); |
| 6158 | } |
| 6159 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
| 6160 | /* |
| 6161 | * The 7.3 Attribute derivation rule for groups is plugged there |
| 6162 | */ |
| 6163 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
| 6164 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6165 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ELEM, |
| 6166 | "Found forbidden pattern data/except//element(ref)\n", |
| 6167 | NULL, NULL); |
| 6168 | } |
| 6169 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6170 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ELEM, |
| 6171 | "Found forbidden pattern list//element(ref)\n", |
| 6172 | NULL, NULL); |
| 6173 | } |
| 6174 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6175 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM, |
| 6176 | "Found forbidden pattern attribute//element(ref)\n", |
| 6177 | NULL, NULL); |
| 6178 | } |
| 6179 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6180 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM, |
| 6181 | "Found forbidden pattern attribute//element(ref)\n", |
| 6182 | NULL, NULL); |
| 6183 | } |
| 6184 | /* |
| 6185 | * reset since in the simple form elements are only child |
| 6186 | * of grammar/define |
| 6187 | */ |
| 6188 | nflags = 0; |
| 6189 | ret = |
| 6190 | xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type); |
| 6191 | if (ret != XML_RELAXNG_CONTENT_EMPTY) { |
| 6192 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_EMPTY, |
| 6193 | "Element %s attributes have a content type error\n", |
| 6194 | cur->name, NULL); |
| 6195 | } |
| 6196 | ret = |
| 6197 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6198 | cur->type); |
| 6199 | if (ret == XML_RELAXNG_CONTENT_ERROR) { |
| 6200 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_ERROR, |
| 6201 | "Element %s has a content type error\n", |
| 6202 | cur->name, NULL); |
| 6203 | } else { |
| 6204 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6205 | } |
| 6206 | } else if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
| 6207 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6208 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ATTR, |
| 6209 | "Found forbidden pattern attribute//attribute\n", |
| 6210 | NULL, NULL); |
| 6211 | } |
| 6212 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6213 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ATTR, |
| 6214 | "Found forbidden pattern list//attribute\n", |
| 6215 | NULL, NULL); |
| 6216 | } |
| 6217 | if (flags & XML_RELAXNG_IN_OOMGROUP) { |
| 6218 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_GROUP_ATTR, |
| 6219 | "Found forbidden pattern oneOrMore//group//attribute\n", |
| 6220 | NULL, NULL); |
| 6221 | } |
| 6222 | if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) { |
| 6223 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_INTERLEAVE_ATTR, |
| 6224 | "Found forbidden pattern oneOrMore//interleave//attribute\n", |
| 6225 | NULL, NULL); |
| 6226 | } |
| 6227 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6228 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ATTR, |
| 6229 | "Found forbidden pattern data/except//attribute\n", |
| 6230 | NULL, NULL); |
| 6231 | } |
| 6232 | if (flags & XML_RELAXNG_IN_START) { |
| 6233 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ATTR, |
| 6234 | "Found forbidden pattern start//attribute\n", |
| 6235 | NULL, NULL); |
| 6236 | } |
| 6237 | if ((!(flags & XML_RELAXNG_IN_ONEORMORE)) |
| 6238 | && (cur->name == NULL)) { |
| 6239 | if (cur->ns == NULL) { |
| 6240 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ANYNAME_ATTR_ANCESTOR, |
| 6241 | "Found anyName attribute without oneOrMore ancestor\n", |
| 6242 | NULL, NULL); |
| 6243 | } else { |
| 6244 | xmlRngPErr(ctxt, cur->node, XML_RNGP_NSNAME_ATTR_ANCESTOR, |
| 6245 | "Found nsName attribute without oneOrMore ancestor\n", |
| 6246 | NULL, NULL); |
| 6247 | } |
| 6248 | } |
| 6249 | nflags = flags | XML_RELAXNG_IN_ATTRIBUTE; |
| 6250 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6251 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6252 | } else if ((cur->type == XML_RELAXNG_ONEORMORE) || |
| 6253 | (cur->type == XML_RELAXNG_ZEROORMORE)) { |
| 6254 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6255 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ONEMORE, |
| 6256 | "Found forbidden pattern data/except//oneOrMore\n", |
| 6257 | NULL, NULL); |
| 6258 | } |
| 6259 | if (flags & XML_RELAXNG_IN_START) { |
| 6260 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ONEMORE, |
| 6261 | "Found forbidden pattern start//oneOrMore\n", |
| 6262 | NULL, NULL); |
| 6263 | } |
| 6264 | nflags = flags | XML_RELAXNG_IN_ONEORMORE; |
| 6265 | ret = |
| 6266 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6267 | cur->type); |
| 6268 | ret = xmlRelaxNGGroupContentType(ret, ret); |
| 6269 | } else if (cur->type == XML_RELAXNG_LIST) { |
| 6270 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6271 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_LIST, |
| 6272 | "Found forbidden pattern list//list\n", NULL, |
| 6273 | NULL); |
| 6274 | } |
| 6275 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6276 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_LIST, |
| 6277 | "Found forbidden pattern data/except//list\n", |
| 6278 | NULL, NULL); |
| 6279 | } |
| 6280 | if (flags & XML_RELAXNG_IN_START) { |
| 6281 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_LIST, |
| 6282 | "Found forbidden pattern start//list\n", NULL, |
| 6283 | NULL); |
| 6284 | } |
| 6285 | nflags = flags | XML_RELAXNG_IN_LIST; |
| 6286 | ret = |
| 6287 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6288 | cur->type); |
| 6289 | } else if (cur->type == XML_RELAXNG_GROUP) { |
| 6290 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6291 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_GROUP, |
| 6292 | "Found forbidden pattern data/except//group\n", |
| 6293 | NULL, NULL); |
| 6294 | } |
| 6295 | if (flags & XML_RELAXNG_IN_START) { |
| 6296 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_GROUP, |
| 6297 | "Found forbidden pattern start//group\n", NULL, |
| 6298 | NULL); |
| 6299 | } |
| 6300 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
| 6301 | nflags = flags | XML_RELAXNG_IN_OOMGROUP; |
| 6302 | else |
| 6303 | nflags = flags; |
| 6304 | ret = |
| 6305 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6306 | cur->type); |
| 6307 | /* |
| 6308 | * The 7.3 Attribute derivation rule for groups is plugged there |
| 6309 | */ |
| 6310 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
| 6311 | } else if (cur->type == XML_RELAXNG_INTERLEAVE) { |
| 6312 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6313 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_INTERLEAVE, |
| 6314 | "Found forbidden pattern list//interleave\n", |
| 6315 | NULL, NULL); |
| 6316 | } |
| 6317 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6318 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE, |
| 6319 | "Found forbidden pattern data/except//interleave\n", |
| 6320 | NULL, NULL); |
| 6321 | } |
| 6322 | if (flags & XML_RELAXNG_IN_START) { |
| 6323 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE, |
| 6324 | "Found forbidden pattern start//interleave\n", |
| 6325 | NULL, NULL); |
| 6326 | } |
| 6327 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
| 6328 | nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE; |
| 6329 | else |
| 6330 | nflags = flags; |
| 6331 | ret = |
| 6332 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6333 | cur->type); |
| 6334 | } else if (cur->type == XML_RELAXNG_EXCEPT) { |
| 6335 | if ((cur->parent != NULL) && |
| 6336 | (cur->parent->type == XML_RELAXNG_DATATYPE)) |
| 6337 | nflags = flags | XML_RELAXNG_IN_DATAEXCEPT; |
| 6338 | else |
| 6339 | nflags = flags; |
| 6340 | ret = |
| 6341 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6342 | cur->type); |
| 6343 | } else if (cur->type == XML_RELAXNG_DATATYPE) { |
| 6344 | if (flags & XML_RELAXNG_IN_START) { |
| 6345 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_DATA, |
| 6346 | "Found forbidden pattern start//data\n", NULL, |
| 6347 | NULL); |
| 6348 | } |
| 6349 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6350 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
| 6351 | } else if (cur->type == XML_RELAXNG_VALUE) { |
| 6352 | if (flags & XML_RELAXNG_IN_START) { |
| 6353 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_VALUE, |
| 6354 | "Found forbidden pattern start//value\n", NULL, |
| 6355 | NULL); |
| 6356 | } |
| 6357 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6358 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
| 6359 | } else if (cur->type == XML_RELAXNG_TEXT) { |
| 6360 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6361 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_TEXT, |
| 6362 | "Found forbidden pattern list//text\n", NULL, |
| 6363 | NULL); |
| 6364 | } |
| 6365 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6366 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_TEXT, |
| 6367 | "Found forbidden pattern data/except//text\n", |
| 6368 | NULL, NULL); |
| 6369 | } |
| 6370 | if (flags & XML_RELAXNG_IN_START) { |
| 6371 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_TEXT, |
| 6372 | "Found forbidden pattern start//text\n", NULL, |
| 6373 | NULL); |
| 6374 | } |
| 6375 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6376 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 6377 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6378 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_EMPTY, |
| 6379 | "Found forbidden pattern data/except//empty\n", |
| 6380 | NULL, NULL); |
| 6381 | } |
| 6382 | if (flags & XML_RELAXNG_IN_START) { |
| 6383 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_EMPTY, |
| 6384 | "Found forbidden pattern start//empty\n", NULL, |
| 6385 | NULL); |
| 6386 | } |
| 6387 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6388 | } else if (cur->type == XML_RELAXNG_CHOICE) { |
| 6389 | xmlRelaxNGCheckChoiceDeterminism(ctxt, cur); |
| 6390 | ret = |
| 6391 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6392 | } else { |
| 6393 | ret = |
| 6394 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6395 | } |
| 6396 | cur = cur->next; |
| 6397 | if (ptype == XML_RELAXNG_GROUP) { |
| 6398 | val = xmlRelaxNGGroupContentType(val, ret); |
| 6399 | } else if (ptype == XML_RELAXNG_INTERLEAVE) { |
| 6400 | tmp = xmlRelaxNGGroupContentType(val, ret); |
| 6401 | if (tmp != XML_RELAXNG_CONTENT_ERROR) |
| 6402 | tmp = xmlRelaxNGMaxContentType(val, ret); |
| 6403 | } else if (ptype == XML_RELAXNG_CHOICE) { |
| 6404 | val = xmlRelaxNGMaxContentType(val, ret); |
| 6405 | } else if (ptype == XML_RELAXNG_LIST) { |
| 6406 | val = XML_RELAXNG_CONTENT_SIMPLE; |
| 6407 | } else if (ptype == XML_RELAXNG_EXCEPT) { |
| 6408 | if (ret == XML_RELAXNG_CONTENT_ERROR) |
| 6409 | val = XML_RELAXNG_CONTENT_ERROR; |
| 6410 | else |
| 6411 | val = XML_RELAXNG_CONTENT_SIMPLE; |
| 6412 | } else { |
| 6413 | val = xmlRelaxNGGroupContentType(val, ret); |
| 6414 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6415 | |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6416 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6417 | return (val); |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6418 | } |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6419 | |
| 6420 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6421 | * xmlRelaxNGParseGrammar: |
| 6422 | * @ctxt: a Relax-NG parser context |
| 6423 | * @nodes: grammar children nodes |
| 6424 | * |
| 6425 | * parse a Relax-NG <grammar> node |
| 6426 | * |
| 6427 | * Returns the internal xmlRelaxNGGrammarPtr built or |
| 6428 | * NULL in case of error |
| 6429 | */ |
| 6430 | static xmlRelaxNGGrammarPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6431 | xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) |
| 6432 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6433 | xmlRelaxNGGrammarPtr ret, tmp, old; |
| 6434 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6435 | #ifdef DEBUG_GRAMMAR |
| 6436 | xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n"); |
| 6437 | #endif |
| 6438 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6439 | ret = xmlRelaxNGNewGrammar(ctxt); |
| 6440 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6441 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6442 | |
| 6443 | /* |
| 6444 | * Link the new grammar in the tree |
| 6445 | */ |
| 6446 | ret->parent = ctxt->grammar; |
| 6447 | if (ctxt->grammar != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6448 | tmp = ctxt->grammar->children; |
| 6449 | if (tmp == NULL) { |
| 6450 | ctxt->grammar->children = ret; |
| 6451 | } else { |
| 6452 | while (tmp->next != NULL) |
| 6453 | tmp = tmp->next; |
| 6454 | tmp->next = ret; |
| 6455 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6456 | } |
| 6457 | |
| 6458 | old = ctxt->grammar; |
| 6459 | ctxt->grammar = ret; |
| 6460 | xmlRelaxNGParseGrammarContent(ctxt, nodes); |
| 6461 | ctxt->grammar = ret; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6462 | if (ctxt->grammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6463 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT, |
| 6464 | "Failed to parse <grammar> content\n", NULL, NULL); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6465 | } else if (ctxt->grammar->start == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6466 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_NO_START, |
| 6467 | "Element <grammar> has no <start>\n", NULL, NULL); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6468 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6469 | |
| 6470 | /* |
| 6471 | * Apply 4.17 mergingd rules to defines and starts |
| 6472 | */ |
| 6473 | xmlRelaxNGCombineStart(ctxt, ret); |
| 6474 | if (ret->defs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6475 | xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine, |
| 6476 | ctxt); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6477 | } |
| 6478 | |
| 6479 | /* |
| 6480 | * link together defines and refs in this grammar |
| 6481 | */ |
| 6482 | if (ret->refs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6483 | xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference, |
| 6484 | ctxt); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6485 | } |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 6486 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6487 | ctxt->grammar = old; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6488 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6489 | } |
| 6490 | |
| 6491 | /** |
| 6492 | * xmlRelaxNGParseDocument: |
| 6493 | * @ctxt: a Relax-NG parser context |
| 6494 | * @node: the root node of the RelaxNG schema |
| 6495 | * |
| 6496 | * parse a Relax-NG definition resource and build an internal |
| 6497 | * xmlRelaxNG struture which can be used to validate instances. |
| 6498 | * |
| 6499 | * Returns the internal XML RelaxNG structure built or |
| 6500 | * NULL in case of error |
| 6501 | */ |
| 6502 | static xmlRelaxNGPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6503 | xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 6504 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6505 | xmlRelaxNGPtr schema = NULL; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6506 | const xmlChar *olddefine; |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 6507 | xmlRelaxNGGrammarPtr old; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6508 | |
| 6509 | if ((ctxt == NULL) || (node == NULL)) |
| 6510 | return (NULL); |
| 6511 | |
| 6512 | schema = xmlRelaxNGNewRelaxNG(ctxt); |
| 6513 | if (schema == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6514 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6515 | |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6516 | olddefine = ctxt->define; |
| 6517 | ctxt->define = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6518 | if (IS_RELAXNG(node, "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6519 | schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6520 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6521 | xmlRelaxNGGrammarPtr tmp, ret; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6522 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6523 | schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt); |
| 6524 | if (schema->topgrammar == NULL) { |
| 6525 | return (schema); |
| 6526 | } |
| 6527 | /* |
| 6528 | * Link the new grammar in the tree |
| 6529 | */ |
| 6530 | ret->parent = ctxt->grammar; |
| 6531 | if (ctxt->grammar != NULL) { |
| 6532 | tmp = ctxt->grammar->children; |
| 6533 | if (tmp == NULL) { |
| 6534 | ctxt->grammar->children = ret; |
| 6535 | } else { |
| 6536 | while (tmp->next != NULL) |
| 6537 | tmp = tmp->next; |
| 6538 | tmp->next = ret; |
| 6539 | } |
| 6540 | } |
| 6541 | old = ctxt->grammar; |
| 6542 | ctxt->grammar = ret; |
| 6543 | xmlRelaxNGParseStart(ctxt, node); |
| 6544 | if (old != NULL) |
| 6545 | ctxt->grammar = old; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6546 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6547 | ctxt->define = olddefine; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 6548 | if (schema->topgrammar->start != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6549 | xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0); |
| 6550 | if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) { |
| 6551 | xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL); |
| 6552 | while ((schema->topgrammar->start != NULL) && |
| 6553 | (schema->topgrammar->start->type == XML_RELAXNG_NOOP) && |
| 6554 | (schema->topgrammar->start->next != NULL)) |
| 6555 | schema->topgrammar->start = |
| 6556 | schema->topgrammar->start->content; |
| 6557 | xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start, |
| 6558 | XML_RELAXNG_IN_START, XML_RELAXNG_NOOP); |
| 6559 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 6560 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6561 | #ifdef DEBUG |
| 6562 | if (schema == NULL) |
| 6563 | xmlGenericError(xmlGenericErrorContext, |
| 6564 | "xmlRelaxNGParseDocument() failed\n"); |
| 6565 | #endif |
| 6566 | |
| 6567 | return (schema); |
| 6568 | } |
| 6569 | |
| 6570 | /************************************************************************ |
| 6571 | * * |
| 6572 | * Reading RelaxNGs * |
| 6573 | * * |
| 6574 | ************************************************************************/ |
| 6575 | |
| 6576 | /** |
| 6577 | * xmlRelaxNGNewParserCtxt: |
| 6578 | * @URL: the location of the schema |
| 6579 | * |
| 6580 | * Create an XML RelaxNGs parse context for that file/resource expected |
| 6581 | * to contain an XML RelaxNGs file. |
| 6582 | * |
| 6583 | * Returns the parser context or NULL in case of error |
| 6584 | */ |
| 6585 | xmlRelaxNGParserCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6586 | xmlRelaxNGNewParserCtxt(const char *URL) |
| 6587 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6588 | xmlRelaxNGParserCtxtPtr ret; |
| 6589 | |
| 6590 | if (URL == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6591 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6592 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6593 | ret = |
| 6594 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6595 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6596 | xmlRngPErrMemory(NULL, "building parser\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6597 | return (NULL); |
| 6598 | } |
| 6599 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6600 | ret->URL = xmlStrdup((const xmlChar *) URL); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 6601 | ret->error = xmlGenericError; |
| 6602 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6603 | return (ret); |
| 6604 | } |
| 6605 | |
| 6606 | /** |
| 6607 | * xmlRelaxNGNewMemParserCtxt: |
| 6608 | * @buffer: a pointer to a char array containing the schemas |
| 6609 | * @size: the size of the array |
| 6610 | * |
| 6611 | * Create an XML RelaxNGs parse context for that memory buffer expected |
| 6612 | * to contain an XML RelaxNGs file. |
| 6613 | * |
| 6614 | * Returns the parser context or NULL in case of error |
| 6615 | */ |
| 6616 | xmlRelaxNGParserCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6617 | xmlRelaxNGNewMemParserCtxt(const char *buffer, int size) |
| 6618 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6619 | xmlRelaxNGParserCtxtPtr ret; |
| 6620 | |
| 6621 | if ((buffer == NULL) || (size <= 0)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6622 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6623 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6624 | ret = |
| 6625 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6626 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6627 | xmlRngPErrMemory(NULL, "building parser\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6628 | return (NULL); |
| 6629 | } |
| 6630 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6631 | ret->buffer = buffer; |
| 6632 | ret->size = size; |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 6633 | ret->error = xmlGenericError; |
| 6634 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6635 | return (ret); |
| 6636 | } |
| 6637 | |
| 6638 | /** |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6639 | * xmlRelaxNGNewDocParserCtxt: |
| 6640 | * @doc: a preparsed document tree |
| 6641 | * |
| 6642 | * Create an XML RelaxNGs parser context for that document. |
| 6643 | * Note: since the process of compiling a RelaxNG schemas modifies the |
| 6644 | * document, the @doc parameter is duplicated internally. |
| 6645 | * |
| 6646 | * Returns the parser context or NULL in case of error |
| 6647 | */ |
| 6648 | xmlRelaxNGParserCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6649 | xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc) |
| 6650 | { |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6651 | xmlRelaxNGParserCtxtPtr ret; |
| 6652 | xmlDocPtr copy; |
| 6653 | |
| 6654 | if (doc == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6655 | return (NULL); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6656 | copy = xmlCopyDoc(doc, 1); |
| 6657 | if (copy == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6658 | return (NULL); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6659 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6660 | ret = |
| 6661 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6662 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6663 | xmlRngPErrMemory(NULL, "building parser\n"); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6664 | return (NULL); |
| 6665 | } |
| 6666 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6667 | ret->document = copy; |
Daniel Veillard | 4259532 | 2004-11-08 10:52:06 +0000 | [diff] [blame] | 6668 | ret->freedoc = 1; |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6669 | ret->userData = xmlGenericErrorContext; |
| 6670 | return (ret); |
| 6671 | } |
| 6672 | |
| 6673 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6674 | * xmlRelaxNGFreeParserCtxt: |
| 6675 | * @ctxt: the schema parser context |
| 6676 | * |
| 6677 | * Free the resources associated to the schema parser context |
| 6678 | */ |
| 6679 | void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6680 | xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt) |
| 6681 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6682 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6683 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6684 | if (ctxt->URL != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6685 | xmlFree(ctxt->URL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6686 | if (ctxt->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6687 | xmlRelaxNGFreeDocument(ctxt->doc); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 6688 | if (ctxt->interleaves != NULL) |
| 6689 | xmlHashFree(ctxt->interleaves, NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6690 | if (ctxt->documents != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6691 | xmlRelaxNGFreeDocumentList(ctxt->documents); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 6692 | if (ctxt->includes != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6693 | xmlRelaxNGFreeIncludeList(ctxt->includes); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6694 | if (ctxt->docTab != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6695 | xmlFree(ctxt->docTab); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 6696 | if (ctxt->incTab != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6697 | xmlFree(ctxt->incTab); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6698 | if (ctxt->defTab != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6699 | int i; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6700 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6701 | for (i = 0; i < ctxt->defNr; i++) |
| 6702 | xmlRelaxNGFreeDefine(ctxt->defTab[i]); |
| 6703 | xmlFree(ctxt->defTab); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6704 | } |
Daniel Veillard | 4259532 | 2004-11-08 10:52:06 +0000 | [diff] [blame] | 6705 | if ((ctxt->document != NULL) && (ctxt->freedoc)) |
| 6706 | xmlFreeDoc(ctxt->document); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6707 | xmlFree(ctxt); |
| 6708 | } |
| 6709 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6710 | /** |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6711 | * xmlRelaxNGNormExtSpace: |
| 6712 | * @value: a value |
| 6713 | * |
| 6714 | * Removes the leading and ending spaces of the value |
| 6715 | * The string is modified "in situ" |
| 6716 | */ |
| 6717 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6718 | xmlRelaxNGNormExtSpace(xmlChar * value) |
| 6719 | { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6720 | xmlChar *start = value; |
| 6721 | xmlChar *cur = value; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6722 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6723 | if (value == NULL) |
| 6724 | return; |
| 6725 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6726 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6727 | cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6728 | if (cur == start) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6729 | do { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6730 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6731 | cur++; |
| 6732 | if (*cur == 0) |
| 6733 | return; |
| 6734 | start = cur; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6735 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6736 | cur++; |
| 6737 | if (*cur == 0) { |
| 6738 | *start = 0; |
| 6739 | return; |
| 6740 | } |
| 6741 | } while (1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6742 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6743 | do { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6744 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6745 | *start++ = *cur++; |
| 6746 | if (*cur == 0) { |
| 6747 | *start = 0; |
| 6748 | return; |
| 6749 | } |
| 6750 | /* don't try to normalize the inner spaces */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6751 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6752 | cur++; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6753 | if (*cur == 0) { |
| 6754 | *start = 0; |
| 6755 | return; |
| 6756 | } |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 6757 | *start++ = *cur++; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6758 | } while (1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6759 | } |
| 6760 | } |
| 6761 | |
| 6762 | /** |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 6763 | * xmlRelaxNGCleanupAttributes: |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6764 | * @ctxt: a Relax-NG parser context |
| 6765 | * @node: a Relax-NG node |
| 6766 | * |
| 6767 | * Check all the attributes on the given node |
| 6768 | */ |
| 6769 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6770 | xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 6771 | { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6772 | xmlAttrPtr cur, next; |
| 6773 | |
| 6774 | cur = node->properties; |
| 6775 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6776 | next = cur->next; |
| 6777 | if ((cur->ns == NULL) || |
| 6778 | (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
| 6779 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
| 6780 | if ((!xmlStrEqual(node->name, BAD_CAST "element")) && |
| 6781 | (!xmlStrEqual(node->name, BAD_CAST "attribute")) && |
| 6782 | (!xmlStrEqual(node->name, BAD_CAST "ref")) && |
| 6783 | (!xmlStrEqual(node->name, BAD_CAST "parentRef")) && |
| 6784 | (!xmlStrEqual(node->name, BAD_CAST "param")) && |
| 6785 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
| 6786 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6787 | "Attribute %s is not allowed on %s\n", |
| 6788 | cur->name, node->name); |
| 6789 | } |
| 6790 | } else if (xmlStrEqual(cur->name, BAD_CAST "type")) { |
| 6791 | if ((!xmlStrEqual(node->name, BAD_CAST "value")) && |
| 6792 | (!xmlStrEqual(node->name, BAD_CAST "data"))) { |
| 6793 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6794 | "Attribute %s is not allowed on %s\n", |
| 6795 | cur->name, node->name); |
| 6796 | } |
| 6797 | } else if (xmlStrEqual(cur->name, BAD_CAST "href")) { |
| 6798 | if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) && |
| 6799 | (!xmlStrEqual(node->name, BAD_CAST "include"))) { |
| 6800 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6801 | "Attribute %s is not allowed on %s\n", |
| 6802 | cur->name, node->name); |
| 6803 | } |
| 6804 | } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) { |
| 6805 | if ((!xmlStrEqual(node->name, BAD_CAST "start")) && |
| 6806 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
| 6807 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6808 | "Attribute %s is not allowed on %s\n", |
| 6809 | cur->name, node->name); |
| 6810 | } |
| 6811 | } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) { |
| 6812 | xmlChar *val; |
| 6813 | xmlURIPtr uri; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6814 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6815 | val = xmlNodeListGetString(node->doc, cur->children, 1); |
| 6816 | if (val != NULL) { |
| 6817 | if (val[0] != 0) { |
| 6818 | uri = xmlParseURI((const char *) val); |
| 6819 | if (uri == NULL) { |
| 6820 | xmlRngPErr(ctxt, node, XML_RNGP_INVALID_URI, |
| 6821 | "Attribute %s contains invalid URI %s\n", |
| 6822 | cur->name, val); |
| 6823 | } else { |
| 6824 | if (uri->scheme == NULL) { |
| 6825 | xmlRngPErr(ctxt, node, XML_RNGP_URI_NOT_ABSOLUTE, |
| 6826 | "Attribute %s URI %s is not absolute\n", |
| 6827 | cur->name, val); |
| 6828 | } |
| 6829 | if (uri->fragment != NULL) { |
| 6830 | xmlRngPErr(ctxt, node, XML_RNGP_URI_FRAGMENT, |
| 6831 | "Attribute %s URI %s has a fragment ID\n", |
| 6832 | cur->name, val); |
| 6833 | } |
| 6834 | xmlFreeURI(uri); |
| 6835 | } |
| 6836 | } |
| 6837 | xmlFree(val); |
| 6838 | } |
| 6839 | } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) { |
| 6840 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_ATTRIBUTE, |
| 6841 | "Unknown attribute %s on %s\n", cur->name, |
| 6842 | node->name); |
| 6843 | } |
| 6844 | } |
| 6845 | cur = next; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6846 | } |
| 6847 | } |
| 6848 | |
| 6849 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6850 | * xmlRelaxNGCleanupTree: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6851 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6852 | * @root: an xmlNodePtr subtree |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6853 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6854 | * Cleanup the subtree from unwanted nodes for parsing, resolve |
| 6855 | * Include and externalRef lookups. |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6856 | */ |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6857 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6858 | xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root) |
| 6859 | { |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6860 | xmlNodePtr cur, delete; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6861 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6862 | delete = NULL; |
| 6863 | cur = root; |
| 6864 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6865 | if (delete != NULL) { |
| 6866 | xmlUnlinkNode(delete); |
| 6867 | xmlFreeNode(delete); |
| 6868 | delete = NULL; |
| 6869 | } |
| 6870 | if (cur->type == XML_ELEMENT_NODE) { |
| 6871 | /* |
| 6872 | * Simplification 4.1. Annotations |
| 6873 | */ |
| 6874 | if ((cur->ns == NULL) || |
| 6875 | (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
| 6876 | if ((cur->parent != NULL) && |
| 6877 | (cur->parent->type == XML_ELEMENT_NODE) && |
| 6878 | ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) || |
| 6879 | (xmlStrEqual(cur->parent->name, BAD_CAST "value")) || |
| 6880 | (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) { |
| 6881 | xmlRngPErr(ctxt, cur, XML_RNGP_FOREIGN_ELEMENT, |
| 6882 | "element %s doesn't allow foreign elements\n", |
| 6883 | cur->parent->name, NULL); |
| 6884 | } |
| 6885 | delete = cur; |
| 6886 | goto skip_children; |
| 6887 | } else { |
| 6888 | xmlRelaxNGCleanupAttributes(ctxt, cur); |
| 6889 | if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) { |
| 6890 | xmlChar *href, *ns, *base, *URL; |
| 6891 | xmlRelaxNGDocumentPtr docu; |
| 6892 | xmlNodePtr tmp; |
Daniel Veillard | 6dc9196 | 2004-03-22 19:10:02 +0000 | [diff] [blame] | 6893 | xmlURIPtr uri; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6894 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6895 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6896 | if (ns == NULL) { |
| 6897 | tmp = cur->parent; |
| 6898 | while ((tmp != NULL) && |
| 6899 | (tmp->type == XML_ELEMENT_NODE)) { |
| 6900 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 6901 | if (ns != NULL) |
| 6902 | break; |
| 6903 | tmp = tmp->parent; |
| 6904 | } |
| 6905 | } |
| 6906 | href = xmlGetProp(cur, BAD_CAST "href"); |
| 6907 | if (href == NULL) { |
| 6908 | xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF, |
| 6909 | "xmlRelaxNGParse: externalRef has no href attribute\n", |
| 6910 | NULL, NULL); |
Daniel Veillard | f2e066a | 2005-06-30 13:04:44 +0000 | [diff] [blame] | 6911 | if (ns != NULL) |
| 6912 | xmlFree(ns); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6913 | delete = cur; |
| 6914 | goto skip_children; |
| 6915 | } |
Daniel Veillard | 6dc9196 | 2004-03-22 19:10:02 +0000 | [diff] [blame] | 6916 | uri = xmlParseURI((const char *) href); |
| 6917 | if (uri == NULL) { |
| 6918 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
| 6919 | "Incorrect URI for externalRef %s\n", |
| 6920 | href, NULL); |
Daniel Veillard | f2e066a | 2005-06-30 13:04:44 +0000 | [diff] [blame] | 6921 | if (ns != NULL) |
| 6922 | xmlFree(ns); |
Daniel Veillard | 6dc9196 | 2004-03-22 19:10:02 +0000 | [diff] [blame] | 6923 | if (href != NULL) |
| 6924 | xmlFree(href); |
| 6925 | delete = cur; |
| 6926 | goto skip_children; |
| 6927 | } |
| 6928 | if (uri->fragment != NULL) { |
| 6929 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
| 6930 | "Fragment forbidden in URI for externalRef %s\n", |
| 6931 | href, NULL); |
Daniel Veillard | f2e066a | 2005-06-30 13:04:44 +0000 | [diff] [blame] | 6932 | if (ns != NULL) |
| 6933 | xmlFree(ns); |
Daniel Veillard | 6dc9196 | 2004-03-22 19:10:02 +0000 | [diff] [blame] | 6934 | xmlFreeURI(uri); |
| 6935 | if (href != NULL) |
| 6936 | xmlFree(href); |
| 6937 | delete = cur; |
| 6938 | goto skip_children; |
| 6939 | } |
| 6940 | xmlFreeURI(uri); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6941 | base = xmlNodeGetBase(cur->doc, cur); |
| 6942 | URL = xmlBuildURI(href, base); |
| 6943 | if (URL == NULL) { |
| 6944 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
| 6945 | "Failed to compute URL for externalRef %s\n", |
| 6946 | href, NULL); |
Daniel Veillard | f2e066a | 2005-06-30 13:04:44 +0000 | [diff] [blame] | 6947 | if (ns != NULL) |
| 6948 | xmlFree(ns); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6949 | if (href != NULL) |
| 6950 | xmlFree(href); |
| 6951 | if (base != NULL) |
| 6952 | xmlFree(base); |
| 6953 | delete = cur; |
| 6954 | goto skip_children; |
| 6955 | } |
| 6956 | if (href != NULL) |
| 6957 | xmlFree(href); |
| 6958 | if (base != NULL) |
| 6959 | xmlFree(base); |
| 6960 | docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns); |
| 6961 | if (docu == NULL) { |
| 6962 | xmlRngPErr(ctxt, cur, XML_RNGP_EXTERNAL_REF_FAILURE, |
| 6963 | "Failed to load externalRef %s\n", URL, |
| 6964 | NULL); |
Daniel Veillard | f2e066a | 2005-06-30 13:04:44 +0000 | [diff] [blame] | 6965 | if (ns != NULL) |
| 6966 | xmlFree(ns); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6967 | xmlFree(URL); |
| 6968 | delete = cur; |
| 6969 | goto skip_children; |
| 6970 | } |
| 6971 | if (ns != NULL) |
| 6972 | xmlFree(ns); |
| 6973 | xmlFree(URL); |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 6974 | cur->psvi = docu; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6975 | } else if (xmlStrEqual(cur->name, BAD_CAST "include")) { |
| 6976 | xmlChar *href, *ns, *base, *URL; |
| 6977 | xmlRelaxNGIncludePtr incl; |
| 6978 | xmlNodePtr tmp; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6979 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6980 | href = xmlGetProp(cur, BAD_CAST "href"); |
| 6981 | if (href == NULL) { |
| 6982 | xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF, |
| 6983 | "xmlRelaxNGParse: include has no href attribute\n", |
| 6984 | NULL, NULL); |
| 6985 | delete = cur; |
| 6986 | goto skip_children; |
| 6987 | } |
| 6988 | base = xmlNodeGetBase(cur->doc, cur); |
| 6989 | URL = xmlBuildURI(href, base); |
| 6990 | if (URL == NULL) { |
| 6991 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
| 6992 | "Failed to compute URL for include %s\n", |
| 6993 | href, NULL); |
| 6994 | if (href != NULL) |
| 6995 | xmlFree(href); |
| 6996 | if (base != NULL) |
| 6997 | xmlFree(base); |
| 6998 | delete = cur; |
| 6999 | goto skip_children; |
| 7000 | } |
| 7001 | if (href != NULL) |
| 7002 | xmlFree(href); |
| 7003 | if (base != NULL) |
| 7004 | xmlFree(base); |
| 7005 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 7006 | if (ns == NULL) { |
| 7007 | tmp = cur->parent; |
| 7008 | while ((tmp != NULL) && |
| 7009 | (tmp->type == XML_ELEMENT_NODE)) { |
| 7010 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 7011 | if (ns != NULL) |
| 7012 | break; |
| 7013 | tmp = tmp->parent; |
| 7014 | } |
| 7015 | } |
| 7016 | incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns); |
| 7017 | if (ns != NULL) |
| 7018 | xmlFree(ns); |
| 7019 | if (incl == NULL) { |
| 7020 | xmlRngPErr(ctxt, cur, XML_RNGP_INCLUDE_FAILURE, |
| 7021 | "Failed to load include %s\n", URL, |
| 7022 | NULL); |
| 7023 | xmlFree(URL); |
| 7024 | delete = cur; |
| 7025 | goto skip_children; |
| 7026 | } |
| 7027 | xmlFree(URL); |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 7028 | cur->psvi = incl; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7029 | } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) || |
| 7030 | (xmlStrEqual(cur->name, BAD_CAST "attribute"))) |
| 7031 | { |
| 7032 | xmlChar *name, *ns; |
| 7033 | xmlNodePtr text = 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 | /* |
| 7036 | * Simplification 4.8. name attribute of element |
| 7037 | * and attribute elements |
| 7038 | */ |
| 7039 | name = xmlGetProp(cur, BAD_CAST "name"); |
| 7040 | if (name != NULL) { |
| 7041 | if (cur->children == NULL) { |
| 7042 | text = |
| 7043 | xmlNewChild(cur, cur->ns, BAD_CAST "name", |
| 7044 | name); |
| 7045 | } else { |
| 7046 | xmlNodePtr node; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7047 | |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 7048 | node = xmlNewDocNode(cur->doc, cur->ns, |
| 7049 | BAD_CAST "name", NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7050 | if (node != NULL) { |
| 7051 | xmlAddPrevSibling(cur->children, node); |
| 7052 | text = xmlNewText(name); |
| 7053 | xmlAddChild(node, text); |
| 7054 | text = node; |
| 7055 | } |
| 7056 | } |
| 7057 | if (text == NULL) { |
| 7058 | xmlRngPErr(ctxt, cur, XML_RNGP_CREATE_FAILURE, |
| 7059 | "Failed to create a name %s element\n", |
| 7060 | name, NULL); |
| 7061 | } |
| 7062 | xmlUnsetProp(cur, BAD_CAST "name"); |
| 7063 | xmlFree(name); |
| 7064 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 7065 | if (ns != NULL) { |
| 7066 | if (text != NULL) { |
| 7067 | xmlSetProp(text, BAD_CAST "ns", ns); |
| 7068 | /* xmlUnsetProp(cur, BAD_CAST "ns"); */ |
| 7069 | } |
| 7070 | xmlFree(ns); |
| 7071 | } else if (xmlStrEqual(cur->name, |
| 7072 | BAD_CAST "attribute")) { |
| 7073 | xmlSetProp(text, BAD_CAST "ns", BAD_CAST ""); |
| 7074 | } |
| 7075 | } |
| 7076 | } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) || |
| 7077 | (xmlStrEqual(cur->name, BAD_CAST "nsName")) || |
| 7078 | (xmlStrEqual(cur->name, BAD_CAST "value"))) { |
| 7079 | /* |
| 7080 | * Simplification 4.8. name attribute of element |
| 7081 | * and attribute elements |
| 7082 | */ |
| 7083 | if (xmlHasProp(cur, BAD_CAST "ns") == NULL) { |
| 7084 | xmlNodePtr node; |
| 7085 | xmlChar *ns = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7086 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7087 | node = cur->parent; |
| 7088 | while ((node != NULL) && |
| 7089 | (node->type == XML_ELEMENT_NODE)) { |
| 7090 | ns = xmlGetProp(node, BAD_CAST "ns"); |
| 7091 | if (ns != NULL) { |
| 7092 | break; |
| 7093 | } |
| 7094 | node = node->parent; |
| 7095 | } |
| 7096 | if (ns == NULL) { |
| 7097 | xmlSetProp(cur, BAD_CAST "ns", BAD_CAST ""); |
| 7098 | } else { |
| 7099 | xmlSetProp(cur, BAD_CAST "ns", ns); |
| 7100 | xmlFree(ns); |
| 7101 | } |
| 7102 | } |
| 7103 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
| 7104 | xmlChar *name, *local, *prefix; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7105 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7106 | /* |
| 7107 | * Simplification: 4.10. QNames |
| 7108 | */ |
| 7109 | name = xmlNodeGetContent(cur); |
| 7110 | if (name != NULL) { |
| 7111 | local = xmlSplitQName2(name, &prefix); |
| 7112 | if (local != NULL) { |
| 7113 | xmlNsPtr ns; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7114 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7115 | ns = xmlSearchNs(cur->doc, cur, prefix); |
| 7116 | if (ns == NULL) { |
| 7117 | xmlRngPErr(ctxt, cur, |
| 7118 | XML_RNGP_PREFIX_UNDEFINED, |
| 7119 | "xmlRelaxNGParse: no namespace for prefix %s\n", |
| 7120 | prefix, NULL); |
| 7121 | } else { |
| 7122 | xmlSetProp(cur, BAD_CAST "ns", |
| 7123 | ns->href); |
| 7124 | xmlNodeSetContent(cur, local); |
| 7125 | } |
| 7126 | xmlFree(local); |
| 7127 | xmlFree(prefix); |
| 7128 | } |
| 7129 | xmlFree(name); |
| 7130 | } |
| 7131 | } |
| 7132 | /* |
| 7133 | * 4.16 |
| 7134 | */ |
| 7135 | if (xmlStrEqual(cur->name, BAD_CAST "nsName")) { |
| 7136 | if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
| 7137 | xmlRngPErr(ctxt, cur, |
| 7138 | XML_RNGP_PAT_NSNAME_EXCEPT_NSNAME, |
| 7139 | "Found nsName/except//nsName forbidden construct\n", |
| 7140 | NULL, NULL); |
| 7141 | } |
| 7142 | } |
| 7143 | } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) && |
| 7144 | (cur != root)) { |
| 7145 | int oldflags = ctxt->flags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7146 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7147 | /* |
| 7148 | * 4.16 |
| 7149 | */ |
| 7150 | if ((cur->parent != NULL) && |
| 7151 | (xmlStrEqual |
| 7152 | (cur->parent->name, BAD_CAST "anyName"))) { |
| 7153 | ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT; |
| 7154 | xmlRelaxNGCleanupTree(ctxt, cur); |
| 7155 | ctxt->flags = oldflags; |
| 7156 | goto skip_children; |
| 7157 | } else if ((cur->parent != NULL) && |
| 7158 | (xmlStrEqual |
| 7159 | (cur->parent->name, BAD_CAST "nsName"))) { |
| 7160 | ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT; |
| 7161 | xmlRelaxNGCleanupTree(ctxt, cur); |
| 7162 | ctxt->flags = oldflags; |
| 7163 | goto skip_children; |
| 7164 | } |
| 7165 | } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) { |
| 7166 | /* |
| 7167 | * 4.16 |
| 7168 | */ |
| 7169 | if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) { |
| 7170 | xmlRngPErr(ctxt, cur, |
| 7171 | XML_RNGP_PAT_ANYNAME_EXCEPT_ANYNAME, |
| 7172 | "Found anyName/except//anyName forbidden construct\n", |
| 7173 | NULL, NULL); |
| 7174 | } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
| 7175 | xmlRngPErr(ctxt, cur, |
| 7176 | XML_RNGP_PAT_NSNAME_EXCEPT_ANYNAME, |
| 7177 | "Found nsName/except//anyName forbidden construct\n", |
| 7178 | NULL, NULL); |
| 7179 | } |
| 7180 | } |
| 7181 | /* |
| 7182 | * Thisd is not an else since "include" is transformed |
| 7183 | * into a div |
| 7184 | */ |
| 7185 | if (xmlStrEqual(cur->name, BAD_CAST "div")) { |
| 7186 | xmlChar *ns; |
| 7187 | xmlNodePtr child, ins, tmp; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7188 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7189 | /* |
| 7190 | * implements rule 4.11 |
| 7191 | */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7192 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7193 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 7194 | |
| 7195 | child = cur->children; |
| 7196 | ins = cur; |
| 7197 | while (child != NULL) { |
| 7198 | if (ns != NULL) { |
| 7199 | if (!xmlHasProp(child, BAD_CAST "ns")) { |
| 7200 | xmlSetProp(child, BAD_CAST "ns", ns); |
| 7201 | } |
| 7202 | } |
| 7203 | tmp = child->next; |
| 7204 | xmlUnlinkNode(child); |
| 7205 | ins = xmlAddNextSibling(ins, child); |
| 7206 | child = tmp; |
| 7207 | } |
| 7208 | if (ns != NULL) |
| 7209 | xmlFree(ns); |
William M. Brack | 8eabb05 | 2004-06-07 14:15:54 +0000 | [diff] [blame] | 7210 | /* |
| 7211 | * Since we are about to delete cur, if it's nsDef is non-NULL we |
| 7212 | * need to preserve it (it contains the ns definitions for the |
| 7213 | * children we just moved). We'll just stick it on to the end |
| 7214 | * of cur->parent's list, since it's never going to be re-serialized |
| 7215 | * (bug 143738). |
| 7216 | */ |
| 7217 | if (cur->nsDef != NULL) { |
| 7218 | xmlNsPtr parDef = (xmlNsPtr)&cur->parent->nsDef; |
| 7219 | while (parDef->next != NULL) |
| 7220 | parDef = parDef->next; |
| 7221 | parDef->next = cur->nsDef; |
| 7222 | cur->nsDef = NULL; |
| 7223 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7224 | delete = cur; |
| 7225 | goto skip_children; |
| 7226 | } |
| 7227 | } |
| 7228 | } |
| 7229 | /* |
| 7230 | * Simplification 4.2 whitespaces |
| 7231 | */ |
| 7232 | else if ((cur->type == XML_TEXT_NODE) || |
| 7233 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 7234 | if (IS_BLANK_NODE(cur)) { |
| 7235 | if (cur->parent->type == XML_ELEMENT_NODE) { |
| 7236 | if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value")) |
| 7237 | && |
| 7238 | (!xmlStrEqual |
| 7239 | (cur->parent->name, BAD_CAST "param"))) |
| 7240 | delete = cur; |
| 7241 | } else { |
| 7242 | delete = cur; |
| 7243 | goto skip_children; |
| 7244 | } |
| 7245 | } |
| 7246 | } else { |
| 7247 | delete = cur; |
| 7248 | goto skip_children; |
| 7249 | } |
| 7250 | |
| 7251 | /* |
| 7252 | * Skip to next node |
| 7253 | */ |
| 7254 | if (cur->children != NULL) { |
| 7255 | if ((cur->children->type != XML_ENTITY_DECL) && |
| 7256 | (cur->children->type != XML_ENTITY_REF_NODE) && |
| 7257 | (cur->children->type != XML_ENTITY_NODE)) { |
| 7258 | cur = cur->children; |
| 7259 | continue; |
| 7260 | } |
| 7261 | } |
| 7262 | skip_children: |
| 7263 | if (cur->next != NULL) { |
| 7264 | cur = cur->next; |
| 7265 | continue; |
| 7266 | } |
| 7267 | |
| 7268 | do { |
| 7269 | cur = cur->parent; |
| 7270 | if (cur == NULL) |
| 7271 | break; |
| 7272 | if (cur == root) { |
| 7273 | cur = NULL; |
| 7274 | break; |
| 7275 | } |
| 7276 | if (cur->next != NULL) { |
| 7277 | cur = cur->next; |
| 7278 | break; |
| 7279 | } |
| 7280 | } while (cur != NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7281 | } |
| 7282 | if (delete != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7283 | xmlUnlinkNode(delete); |
| 7284 | xmlFreeNode(delete); |
| 7285 | delete = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7286 | } |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7287 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7288 | |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7289 | /** |
| 7290 | * xmlRelaxNGCleanupDoc: |
| 7291 | * @ctxt: a Relax-NG parser context |
| 7292 | * @doc: an xmldocPtr document pointer |
| 7293 | * |
| 7294 | * Cleanup the document from unwanted nodes for parsing, resolve |
| 7295 | * Include and externalRef lookups. |
| 7296 | * |
| 7297 | * Returns the cleaned up document or NULL in case of error |
| 7298 | */ |
| 7299 | static xmlDocPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7300 | xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc) |
| 7301 | { |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7302 | xmlNodePtr root; |
| 7303 | |
| 7304 | /* |
| 7305 | * Extract the root |
| 7306 | */ |
| 7307 | root = xmlDocGetRootElement(doc); |
| 7308 | if (root == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7309 | xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n", |
| 7310 | ctxt->URL, NULL); |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7311 | return (NULL); |
| 7312 | } |
| 7313 | xmlRelaxNGCleanupTree(ctxt, root); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7314 | return (doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7315 | } |
| 7316 | |
| 7317 | /** |
| 7318 | * xmlRelaxNGParse: |
| 7319 | * @ctxt: a Relax-NG parser context |
| 7320 | * |
| 7321 | * parse a schema definition resource and build an internal |
| 7322 | * XML Shema struture which can be used to validate instances. |
| 7323 | * *WARNING* this interface is highly subject to change |
| 7324 | * |
| 7325 | * Returns the internal XML RelaxNG structure built from the resource or |
| 7326 | * NULL in case of error |
| 7327 | */ |
| 7328 | xmlRelaxNGPtr |
| 7329 | xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt) |
| 7330 | { |
| 7331 | xmlRelaxNGPtr ret = NULL; |
| 7332 | xmlDocPtr doc; |
| 7333 | xmlNodePtr root; |
| 7334 | |
| 7335 | xmlRelaxNGInitTypes(); |
| 7336 | |
| 7337 | if (ctxt == NULL) |
| 7338 | return (NULL); |
| 7339 | |
| 7340 | /* |
| 7341 | * First step is to parse the input document into an DOM/Infoset |
| 7342 | */ |
| 7343 | if (ctxt->URL != NULL) { |
Daniel Veillard | 87247e8 | 2004-01-13 20:42:02 +0000 | [diff] [blame] | 7344 | doc = xmlReadFile((const char *) ctxt->URL,NULL,0); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7345 | if (doc == NULL) { |
| 7346 | xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR, |
| 7347 | "xmlRelaxNGParse: could not load %s\n", ctxt->URL, |
| 7348 | NULL); |
| 7349 | return (NULL); |
| 7350 | } |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7351 | } else if (ctxt->buffer != NULL) { |
Daniel Veillard | 87247e8 | 2004-01-13 20:42:02 +0000 | [diff] [blame] | 7352 | doc = xmlReadMemory(ctxt->buffer, ctxt->size,NULL,NULL,0); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7353 | if (doc == NULL) { |
| 7354 | xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR, |
| 7355 | "xmlRelaxNGParse: could not parse schemas\n", NULL, |
| 7356 | NULL); |
| 7357 | return (NULL); |
| 7358 | } |
| 7359 | doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
| 7360 | ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 7361 | } else if (ctxt->document != NULL) { |
| 7362 | doc = ctxt->document; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7363 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7364 | xmlRngPErr(ctxt, NULL, XML_RNGP_EMPTY, |
| 7365 | "xmlRelaxNGParse: nothing to parse\n", NULL, NULL); |
| 7366 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7367 | } |
| 7368 | ctxt->document = doc; |
| 7369 | |
| 7370 | /* |
| 7371 | * Some preprocessing of the document content |
| 7372 | */ |
| 7373 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 7374 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7375 | xmlFreeDoc(ctxt->document); |
| 7376 | ctxt->document = NULL; |
| 7377 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7378 | } |
| 7379 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7380 | /* |
| 7381 | * Then do the parsing for good |
| 7382 | */ |
| 7383 | root = xmlDocGetRootElement(doc); |
| 7384 | if (root == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7385 | xmlRngPErr(ctxt, (xmlNodePtr) doc, |
| 7386 | XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n", |
| 7387 | ctxt->URL, NULL); |
| 7388 | xmlFreeDoc(doc); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7389 | return (NULL); |
| 7390 | } |
| 7391 | ret = xmlRelaxNGParseDocument(ctxt, root); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7392 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7393 | xmlFreeDoc(doc); |
| 7394 | return (NULL); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7395 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7396 | |
| 7397 | /* |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7398 | * Check the ref/defines links |
| 7399 | */ |
| 7400 | /* |
| 7401 | * try to preprocess interleaves |
| 7402 | */ |
| 7403 | if (ctxt->interleaves != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7404 | xmlHashScan(ctxt->interleaves, |
| 7405 | (xmlHashScanner) xmlRelaxNGComputeInterleaves, ctxt); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7406 | } |
| 7407 | |
| 7408 | /* |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7409 | * if there was a parsing error return NULL |
| 7410 | */ |
| 7411 | if (ctxt->nbErrors > 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7412 | xmlRelaxNGFree(ret); |
| 7413 | ctxt->document = NULL; |
| 7414 | xmlFreeDoc(doc); |
| 7415 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7416 | } |
| 7417 | |
| 7418 | /* |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 7419 | * try to compile (parts of) the schemas |
| 7420 | */ |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7421 | if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) { |
| 7422 | if (ret->topgrammar->start->type != XML_RELAXNG_START) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7423 | xmlRelaxNGDefinePtr def; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7424 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7425 | def = xmlRelaxNGNewDefine(ctxt, NULL); |
| 7426 | if (def != NULL) { |
| 7427 | def->type = XML_RELAXNG_START; |
| 7428 | def->content = ret->topgrammar->start; |
| 7429 | ret->topgrammar->start = def; |
| 7430 | } |
| 7431 | } |
| 7432 | xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7433 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 7434 | |
| 7435 | /* |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7436 | * Transfer the pointer for cleanup at the schema level. |
| 7437 | */ |
| 7438 | ret->doc = doc; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7439 | ctxt->document = NULL; |
| 7440 | ret->documents = ctxt->documents; |
| 7441 | ctxt->documents = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7442 | |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 7443 | ret->includes = ctxt->includes; |
| 7444 | ctxt->includes = NULL; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7445 | ret->defNr = ctxt->defNr; |
| 7446 | ret->defTab = ctxt->defTab; |
| 7447 | ctxt->defTab = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7448 | if (ctxt->idref == 1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7449 | ret->idref = 1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7450 | |
| 7451 | return (ret); |
| 7452 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7453 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7454 | /** |
| 7455 | * xmlRelaxNGSetParserErrors: |
| 7456 | * @ctxt: a Relax-NG validation context |
| 7457 | * @err: the error callback |
| 7458 | * @warn: the warning callback |
| 7459 | * @ctx: contextual data for the callbacks |
| 7460 | * |
| 7461 | * Set the callback functions used to handle errors for a validation context |
| 7462 | */ |
| 7463 | void |
| 7464 | xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7465 | xmlRelaxNGValidityErrorFunc err, |
| 7466 | xmlRelaxNGValidityWarningFunc warn, void *ctx) |
| 7467 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7468 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7469 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7470 | ctxt->error = err; |
| 7471 | ctxt->warning = warn; |
| 7472 | ctxt->userData = ctx; |
| 7473 | } |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 7474 | |
| 7475 | /** |
| 7476 | * xmlRelaxNGGetParserErrors: |
| 7477 | * @ctxt: a Relax-NG validation context |
| 7478 | * @err: the error callback result |
| 7479 | * @warn: the warning callback result |
| 7480 | * @ctx: contextual data for the callbacks result |
| 7481 | * |
| 7482 | * Get the callback information used to handle errors for a validation context |
| 7483 | * |
| 7484 | * Returns -1 in case of failure, 0 otherwise. |
| 7485 | */ |
| 7486 | int |
| 7487 | xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7488 | xmlRelaxNGValidityErrorFunc * err, |
| 7489 | xmlRelaxNGValidityWarningFunc * warn, void **ctx) |
| 7490 | { |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 7491 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7492 | return (-1); |
| 7493 | if (err != NULL) |
| 7494 | *err = ctxt->error; |
| 7495 | if (warn != NULL) |
| 7496 | *warn = ctxt->warning; |
| 7497 | if (ctx != NULL) |
| 7498 | *ctx = ctxt->userData; |
| 7499 | return (0); |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 7500 | } |
| 7501 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 7502 | #ifdef LIBXML_OUTPUT_ENABLED |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7503 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7504 | /************************************************************************ |
| 7505 | * * |
| 7506 | * Dump back a compiled form * |
| 7507 | * * |
| 7508 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7509 | static void xmlRelaxNGDumpDefine(FILE * output, |
| 7510 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7511 | |
| 7512 | /** |
| 7513 | * xmlRelaxNGDumpDefines: |
| 7514 | * @output: the file output |
| 7515 | * @defines: a list of define structures |
| 7516 | * |
| 7517 | * Dump a RelaxNG structure back |
| 7518 | */ |
| 7519 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7520 | xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines) |
| 7521 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7522 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7523 | xmlRelaxNGDumpDefine(output, defines); |
| 7524 | defines = defines->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7525 | } |
| 7526 | } |
| 7527 | |
| 7528 | /** |
| 7529 | * xmlRelaxNGDumpDefine: |
| 7530 | * @output: the file output |
| 7531 | * @define: a define structure |
| 7532 | * |
| 7533 | * Dump a RelaxNG structure back |
| 7534 | */ |
| 7535 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7536 | xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define) |
| 7537 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7538 | if (define == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7539 | return; |
| 7540 | switch (define->type) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7541 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7542 | fprintf(output, "<empty/>\n"); |
| 7543 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7544 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7545 | fprintf(output, "<notAllowed/>\n"); |
| 7546 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7547 | case XML_RELAXNG_TEXT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7548 | fprintf(output, "<text/>\n"); |
| 7549 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7550 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7551 | fprintf(output, "<element>\n"); |
| 7552 | if (define->name != NULL) { |
| 7553 | fprintf(output, "<name"); |
| 7554 | if (define->ns != NULL) |
| 7555 | fprintf(output, " ns=\"%s\"", define->ns); |
| 7556 | fprintf(output, ">%s</name>\n", define->name); |
| 7557 | } |
| 7558 | xmlRelaxNGDumpDefines(output, define->attrs); |
| 7559 | xmlRelaxNGDumpDefines(output, define->content); |
| 7560 | fprintf(output, "</element>\n"); |
| 7561 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7562 | case XML_RELAXNG_LIST: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7563 | fprintf(output, "<list>\n"); |
| 7564 | xmlRelaxNGDumpDefines(output, define->content); |
| 7565 | fprintf(output, "</list>\n"); |
| 7566 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7567 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7568 | fprintf(output, "<oneOrMore>\n"); |
| 7569 | xmlRelaxNGDumpDefines(output, define->content); |
| 7570 | fprintf(output, "</oneOrMore>\n"); |
| 7571 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7572 | case XML_RELAXNG_ZEROORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7573 | fprintf(output, "<zeroOrMore>\n"); |
| 7574 | xmlRelaxNGDumpDefines(output, define->content); |
| 7575 | fprintf(output, "</zeroOrMore>\n"); |
| 7576 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7577 | case XML_RELAXNG_CHOICE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7578 | fprintf(output, "<choice>\n"); |
| 7579 | xmlRelaxNGDumpDefines(output, define->content); |
| 7580 | fprintf(output, "</choice>\n"); |
| 7581 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7582 | case XML_RELAXNG_GROUP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7583 | fprintf(output, "<group>\n"); |
| 7584 | xmlRelaxNGDumpDefines(output, define->content); |
| 7585 | fprintf(output, "</group>\n"); |
| 7586 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7587 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7588 | fprintf(output, "<interleave>\n"); |
| 7589 | xmlRelaxNGDumpDefines(output, define->content); |
| 7590 | fprintf(output, "</interleave>\n"); |
| 7591 | break; |
| 7592 | case XML_RELAXNG_OPTIONAL: |
| 7593 | fprintf(output, "<optional>\n"); |
| 7594 | xmlRelaxNGDumpDefines(output, define->content); |
| 7595 | fprintf(output, "</optional>\n"); |
| 7596 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7597 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7598 | fprintf(output, "<attribute>\n"); |
| 7599 | xmlRelaxNGDumpDefines(output, define->content); |
| 7600 | fprintf(output, "</attribute>\n"); |
| 7601 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7602 | case XML_RELAXNG_DEF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7603 | fprintf(output, "<define"); |
| 7604 | if (define->name != NULL) |
| 7605 | fprintf(output, " name=\"%s\"", define->name); |
| 7606 | fprintf(output, ">\n"); |
| 7607 | xmlRelaxNGDumpDefines(output, define->content); |
| 7608 | fprintf(output, "</define>\n"); |
| 7609 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7610 | case XML_RELAXNG_REF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7611 | fprintf(output, "<ref"); |
| 7612 | if (define->name != NULL) |
| 7613 | fprintf(output, " name=\"%s\"", define->name); |
| 7614 | fprintf(output, ">\n"); |
| 7615 | xmlRelaxNGDumpDefines(output, define->content); |
| 7616 | fprintf(output, "</ref>\n"); |
| 7617 | break; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7618 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7619 | fprintf(output, "<parentRef"); |
| 7620 | if (define->name != NULL) |
| 7621 | fprintf(output, " name=\"%s\"", define->name); |
| 7622 | fprintf(output, ">\n"); |
| 7623 | xmlRelaxNGDumpDefines(output, define->content); |
| 7624 | fprintf(output, "</parentRef>\n"); |
| 7625 | break; |
| 7626 | case XML_RELAXNG_EXTERNALREF: |
| 7627 | fprintf(output, "<externalRef>"); |
| 7628 | xmlRelaxNGDumpDefines(output, define->content); |
| 7629 | fprintf(output, "</externalRef>\n"); |
| 7630 | break; |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 7631 | case XML_RELAXNG_DATATYPE: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7632 | case XML_RELAXNG_VALUE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7633 | TODO break; |
| 7634 | case XML_RELAXNG_START: |
| 7635 | case XML_RELAXNG_EXCEPT: |
| 7636 | case XML_RELAXNG_PARAM: |
| 7637 | TODO break; |
| 7638 | case XML_RELAXNG_NOOP: |
| 7639 | xmlRelaxNGDumpDefines(output, define->content); |
| 7640 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7641 | } |
| 7642 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7643 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7644 | /** |
| 7645 | * xmlRelaxNGDumpGrammar: |
| 7646 | * @output: the file output |
| 7647 | * @grammar: a grammar structure |
| 7648 | * @top: is this a top grammar |
| 7649 | * |
| 7650 | * Dump a RelaxNG structure back |
| 7651 | */ |
| 7652 | static void |
| 7653 | xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top) |
| 7654 | { |
| 7655 | if (grammar == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7656 | return; |
| 7657 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7658 | fprintf(output, "<grammar"); |
| 7659 | if (top) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7660 | fprintf(output, " xmlns=\"http://relaxng.org/ns/structure/1.0\""); |
| 7661 | switch (grammar->combine) { |
| 7662 | case XML_RELAXNG_COMBINE_UNDEFINED: |
| 7663 | break; |
| 7664 | case XML_RELAXNG_COMBINE_CHOICE: |
| 7665 | fprintf(output, " combine=\"choice\""); |
| 7666 | break; |
| 7667 | case XML_RELAXNG_COMBINE_INTERLEAVE: |
| 7668 | fprintf(output, " combine=\"interleave\""); |
| 7669 | break; |
| 7670 | default: |
| 7671 | fprintf(output, " <!-- invalid combine value -->"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7672 | } |
| 7673 | fprintf(output, ">\n"); |
| 7674 | if (grammar->start == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7675 | fprintf(output, " <!-- grammar had no start -->"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7676 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7677 | fprintf(output, "<start>\n"); |
| 7678 | xmlRelaxNGDumpDefine(output, grammar->start); |
| 7679 | fprintf(output, "</start>\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7680 | } |
| 7681 | /* TODO ? Dump the defines ? */ |
| 7682 | fprintf(output, "</grammar>\n"); |
| 7683 | } |
| 7684 | |
| 7685 | /** |
| 7686 | * xmlRelaxNGDump: |
| 7687 | * @output: the file output |
| 7688 | * @schema: a schema structure |
| 7689 | * |
| 7690 | * Dump a RelaxNG structure back |
| 7691 | */ |
| 7692 | void |
| 7693 | xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema) |
| 7694 | { |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 7695 | if (output == NULL) |
| 7696 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7697 | if (schema == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7698 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
| 7699 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7700 | } |
| 7701 | fprintf(output, "RelaxNG: "); |
| 7702 | if (schema->doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7703 | fprintf(output, "no document\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7704 | } else if (schema->doc->URL != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7705 | fprintf(output, "%s\n", schema->doc->URL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7706 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7707 | fprintf(output, "\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7708 | } |
| 7709 | if (schema->topgrammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7710 | fprintf(output, "RelaxNG has no top grammar\n"); |
| 7711 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7712 | } |
| 7713 | xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1); |
| 7714 | } |
| 7715 | |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7716 | /** |
| 7717 | * xmlRelaxNGDumpTree: |
| 7718 | * @output: the file output |
| 7719 | * @schema: a schema structure |
| 7720 | * |
| 7721 | * Dump the transformed RelaxNG tree. |
| 7722 | */ |
| 7723 | void |
| 7724 | xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema) |
| 7725 | { |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 7726 | if (output == NULL) |
| 7727 | return; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7728 | if (schema == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7729 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
| 7730 | return; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7731 | } |
| 7732 | if (schema->doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7733 | fprintf(output, "no document\n"); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7734 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7735 | xmlDocDump(output, schema->doc); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7736 | } |
| 7737 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 7738 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7739 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7740 | /************************************************************************ |
| 7741 | * * |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7742 | * Validation of compiled content * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7743 | * * |
| 7744 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7745 | static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
| 7746 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7747 | |
| 7748 | /** |
| 7749 | * xmlRelaxNGValidateCompiledCallback: |
| 7750 | * @exec: the regular expression instance |
| 7751 | * @token: the token which matched |
| 7752 | * @transdata: callback data, the define for the subelement if available |
| 7753 | @ @inputdata: callback data, the Relax NG validation context |
| 7754 | * |
| 7755 | * Handle the callback and if needed validate the element children. |
| 7756 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7757 | static void |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7758 | xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7759 | const xmlChar * token, |
| 7760 | void *transdata, void *inputdata) |
| 7761 | { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7762 | xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata; |
| 7763 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata; |
| 7764 | int ret; |
| 7765 | |
| 7766 | #ifdef DEBUG_COMPILE |
| 7767 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7768 | "Compiled callback for: '%s'\n", token); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7769 | #endif |
| 7770 | if (ctxt == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7771 | fprintf(stderr, "callback on %s missing context\n", token); |
| 7772 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7773 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7774 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7775 | } |
| 7776 | if (define == NULL) { |
| 7777 | if (token[0] == '#') |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7778 | return; |
| 7779 | fprintf(stderr, "callback on %s missing define\n", token); |
| 7780 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7781 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7782 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7783 | } |
| 7784 | if ((ctxt == NULL) || (define == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7785 | fprintf(stderr, "callback on %s missing info\n", token); |
| 7786 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7787 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7788 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7789 | } else if (define->type != XML_RELAXNG_ELEMENT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7790 | fprintf(stderr, "callback on %s define is not element\n", token); |
| 7791 | if (ctxt->errNo == XML_RELAXNG_OK) |
| 7792 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7793 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7794 | } |
| 7795 | ret = xmlRelaxNGValidateDefinition(ctxt, define); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7796 | if (ret != 0) |
| 7797 | ctxt->perr = ret; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7798 | } |
| 7799 | |
| 7800 | /** |
| 7801 | * xmlRelaxNGValidateCompiledContent: |
| 7802 | * @ctxt: the RelaxNG validation context |
| 7803 | * @regexp: the regular expression as compiled |
| 7804 | * @content: list of children to test against the regexp |
| 7805 | * |
| 7806 | * Validate the content model of an element or start using the regexp |
| 7807 | * |
| 7808 | * Returns 0 in case of success, -1 in case of error. |
| 7809 | */ |
| 7810 | static int |
| 7811 | xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7812 | xmlRegexpPtr regexp, xmlNodePtr content) |
| 7813 | { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7814 | xmlRegExecCtxtPtr exec; |
| 7815 | xmlNodePtr cur; |
Daniel Veillard | 6216360 | 2003-04-17 09:36:38 +0000 | [diff] [blame] | 7816 | int ret = 0; |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7817 | int oldperr = ctxt->perr; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7818 | |
| 7819 | if ((ctxt == NULL) || (regexp == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7820 | return (-1); |
| 7821 | exec = xmlRegNewExecCtxt(regexp, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7822 | xmlRelaxNGValidateCompiledCallback, ctxt); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7823 | ctxt->perr = 0; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7824 | cur = content; |
| 7825 | while (cur != NULL) { |
| 7826 | ctxt->state->seq = cur; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7827 | switch (cur->type) { |
| 7828 | case XML_TEXT_NODE: |
| 7829 | case XML_CDATA_SECTION_NODE: |
| 7830 | if (xmlIsBlankNode(cur)) |
| 7831 | break; |
| 7832 | ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt); |
| 7833 | if (ret < 0) { |
| 7834 | VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, |
| 7835 | cur->parent->name); |
| 7836 | } |
| 7837 | break; |
| 7838 | case XML_ELEMENT_NODE: |
| 7839 | if (cur->ns != NULL) { |
| 7840 | ret = xmlRegExecPushString2(exec, cur->name, |
| 7841 | cur->ns->href, ctxt); |
| 7842 | } else { |
| 7843 | ret = xmlRegExecPushString(exec, cur->name, ctxt); |
| 7844 | } |
| 7845 | if (ret < 0) { |
| 7846 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name); |
| 7847 | } |
| 7848 | break; |
| 7849 | default: |
| 7850 | break; |
| 7851 | } |
| 7852 | if (ret < 0) |
| 7853 | break; |
| 7854 | /* |
| 7855 | * Switch to next element |
| 7856 | */ |
| 7857 | cur = cur->next; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7858 | } |
| 7859 | ret = xmlRegExecPushString(exec, NULL, NULL); |
| 7860 | if (ret == 1) { |
| 7861 | ret = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7862 | ctxt->state->seq = NULL; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7863 | } else if (ret == 0) { |
| 7864 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7865 | * TODO: get some of the names needed to exit the current state of exec |
| 7866 | */ |
| 7867 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST ""); |
| 7868 | ret = -1; |
| 7869 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 7870 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7871 | } else { |
| 7872 | ret = -1; |
| 7873 | } |
| 7874 | xmlRegFreeExecCtxt(exec); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7875 | /* |
| 7876 | * There might be content model errors outside of the pure |
| 7877 | * regexp validation, e.g. for attribute values. |
| 7878 | */ |
| 7879 | if ((ret == 0) && (ctxt->perr != 0)) { |
| 7880 | ret = ctxt->perr; |
| 7881 | } |
| 7882 | ctxt->perr = oldperr; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7883 | return (ret); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7884 | } |
| 7885 | |
| 7886 | /************************************************************************ |
| 7887 | * * |
| 7888 | * Progressive validation of when possible * |
| 7889 | * * |
| 7890 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7891 | static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
| 7892 | xmlRelaxNGDefinePtr defines); |
| 7893 | static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 7894 | int dolog); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 7895 | static void xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7896 | |
| 7897 | /** |
| 7898 | * xmlRelaxNGElemPush: |
| 7899 | * @ctxt: the validation context |
| 7900 | * @exec: the regexp runtime for the new content model |
| 7901 | * |
| 7902 | * Push a new regexp for the current node content model on the stack |
| 7903 | * |
| 7904 | * Returns 0 in case of success and -1 in case of error. |
| 7905 | */ |
| 7906 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7907 | xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec) |
| 7908 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7909 | if (ctxt->elemTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7910 | ctxt->elemMax = 10; |
| 7911 | ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax * |
| 7912 | sizeof |
| 7913 | (xmlRegExecCtxtPtr)); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7914 | if (ctxt->elemTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7915 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 7916 | return (-1); |
| 7917 | } |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7918 | } |
| 7919 | if (ctxt->elemNr >= ctxt->elemMax) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7920 | ctxt->elemMax *= 2; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7921 | ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7922 | ctxt->elemMax * |
| 7923 | sizeof |
| 7924 | (xmlRegExecCtxtPtr)); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7925 | if (ctxt->elemTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7926 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 7927 | return (-1); |
| 7928 | } |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7929 | } |
| 7930 | ctxt->elemTab[ctxt->elemNr++] = exec; |
| 7931 | ctxt->elem = exec; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7932 | return (0); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7933 | } |
| 7934 | |
| 7935 | /** |
| 7936 | * xmlRelaxNGElemPop: |
| 7937 | * @ctxt: the validation context |
| 7938 | * |
| 7939 | * Pop the regexp of the current node content model from the stack |
| 7940 | * |
| 7941 | * Returns the exec or NULL if empty |
| 7942 | */ |
| 7943 | static xmlRegExecCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7944 | xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt) |
| 7945 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7946 | xmlRegExecCtxtPtr ret; |
| 7947 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7948 | if (ctxt->elemNr <= 0) |
| 7949 | return (NULL); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7950 | ctxt->elemNr--; |
| 7951 | ret = ctxt->elemTab[ctxt->elemNr]; |
| 7952 | ctxt->elemTab[ctxt->elemNr] = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7953 | if (ctxt->elemNr > 0) |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7954 | ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1]; |
| 7955 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7956 | ctxt->elem = NULL; |
| 7957 | return (ret); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7958 | } |
| 7959 | |
| 7960 | /** |
| 7961 | * xmlRelaxNGValidateProgressiveCallback: |
| 7962 | * @exec: the regular expression instance |
| 7963 | * @token: the token which matched |
| 7964 | * @transdata: callback data, the define for the subelement if available |
| 7965 | @ @inputdata: callback data, the Relax NG validation context |
| 7966 | * |
| 7967 | * Handle the callback and if needed validate the element children. |
| 7968 | * some of the in/out informations are passed via the context in @inputdata. |
| 7969 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7970 | static void |
| 7971 | xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec |
| 7972 | ATTRIBUTE_UNUSED, |
| 7973 | const xmlChar * token, |
| 7974 | void *transdata, void *inputdata) |
| 7975 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7976 | xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata; |
| 7977 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7978 | xmlRelaxNGValidStatePtr state, oldstate; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7979 | xmlNodePtr node = ctxt->pnode; |
Daniel Veillard | c3ca5ba | 2003-05-09 22:26:28 +0000 | [diff] [blame] | 7980 | int ret = 0, oldflags; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7981 | |
| 7982 | #ifdef DEBUG_PROGRESSIVE |
| 7983 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7984 | "Progressive callback for: '%s'\n", token); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7985 | #endif |
| 7986 | if (ctxt == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7987 | fprintf(stderr, "callback on %s missing context\n", token); |
| 7988 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7989 | } |
| 7990 | ctxt->pstate = 1; |
| 7991 | if (define == NULL) { |
| 7992 | if (token[0] == '#') |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7993 | return; |
| 7994 | fprintf(stderr, "callback on %s missing define\n", token); |
| 7995 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7996 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7997 | ctxt->pstate = -1; |
| 7998 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7999 | } |
| 8000 | if ((ctxt == NULL) || (define == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8001 | fprintf(stderr, "callback on %s missing info\n", token); |
| 8002 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 8003 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 8004 | ctxt->pstate = -1; |
| 8005 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8006 | } else if (define->type != XML_RELAXNG_ELEMENT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8007 | fprintf(stderr, "callback on %s define is not element\n", token); |
| 8008 | if (ctxt->errNo == XML_RELAXNG_OK) |
| 8009 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 8010 | ctxt->pstate = -1; |
| 8011 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8012 | } |
| 8013 | if (node->type != XML_ELEMENT_NODE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8014 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
| 8015 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8016 | xmlRelaxNGDumpValidError(ctxt); |
| 8017 | ctxt->pstate = -1; |
| 8018 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8019 | } |
| 8020 | if (define->contModel == NULL) { |
| 8021 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8022 | * this node cannot be validated in a streamable fashion |
| 8023 | */ |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8024 | #ifdef DEBUG_PROGRESSIVE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8025 | xmlGenericError(xmlGenericErrorContext, |
| 8026 | "Element '%s' validation is not streamable\n", |
| 8027 | token); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8028 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8029 | ctxt->pstate = 0; |
| 8030 | ctxt->pdef = define; |
| 8031 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8032 | } |
| 8033 | exec = xmlRegNewExecCtxt(define->contModel, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8034 | xmlRelaxNGValidateProgressiveCallback, ctxt); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8035 | if (exec == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8036 | ctxt->pstate = -1; |
| 8037 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8038 | } |
| 8039 | xmlRelaxNGElemPush(ctxt, exec); |
| 8040 | |
| 8041 | /* |
| 8042 | * Validate the attributes part of the content. |
| 8043 | */ |
| 8044 | state = xmlRelaxNGNewValidState(ctxt, node); |
| 8045 | if (state == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8046 | ctxt->pstate = -1; |
| 8047 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8048 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8049 | oldstate = ctxt->state; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8050 | ctxt->state = state; |
| 8051 | if (define->attrs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8052 | ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
| 8053 | if (ret != 0) { |
| 8054 | ctxt->pstate = -1; |
| 8055 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
| 8056 | } |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8057 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8058 | if (ctxt->state != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8059 | ctxt->state->seq = NULL; |
| 8060 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
| 8061 | if (ret != 0) { |
| 8062 | ctxt->pstate = -1; |
| 8063 | } |
| 8064 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8065 | } else if (ctxt->states != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8066 | int tmp = -1, i; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8067 | |
| 8068 | oldflags = ctxt->flags; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8069 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8070 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 8071 | state = ctxt->states->tabState[i]; |
| 8072 | ctxt->state = state; |
| 8073 | ctxt->state->seq = NULL; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8074 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8075 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
| 8076 | tmp = 0; |
| 8077 | break; |
| 8078 | } |
| 8079 | } |
| 8080 | if (tmp != 0) { |
| 8081 | /* |
| 8082 | * validation error, log the message for the "best" one |
| 8083 | */ |
| 8084 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8085 | xmlRelaxNGLogBestError(ctxt); |
| 8086 | } |
| 8087 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 8088 | xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[i]); |
| 8089 | } |
| 8090 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 8091 | ctxt->states = NULL; |
| 8092 | if ((ret == 0) && (tmp == -1)) |
| 8093 | ctxt->pstate = -1; |
| 8094 | ctxt->flags = oldflags; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8095 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8096 | if (ctxt->pstate == -1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8097 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 8098 | xmlRelaxNGDumpValidError(ctxt); |
| 8099 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8100 | } |
| 8101 | ctxt->state = oldstate; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8102 | } |
| 8103 | |
| 8104 | /** |
| 8105 | * xmlRelaxNGValidatePushElement: |
| 8106 | * @ctxt: the validation context |
| 8107 | * @doc: a document instance |
| 8108 | * @elem: an element instance |
| 8109 | * |
| 8110 | * Push a new element start on the RelaxNG validation stack. |
| 8111 | * |
| 8112 | * returns 1 if no validation problem was found or 0 if validating the |
| 8113 | * element requires a full node, and -1 in case of error. |
| 8114 | */ |
| 8115 | int |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8116 | xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8117 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8118 | xmlNodePtr elem) |
| 8119 | { |
| 8120 | int ret = 1; |
| 8121 | |
| 8122 | if ((ctxt == NULL) || (elem == NULL)) |
| 8123 | return (-1); |
| 8124 | |
| 8125 | #ifdef DEBUG_PROGRESSIVE |
| 8126 | xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name); |
| 8127 | #endif |
| 8128 | if (ctxt->elem == 0) { |
| 8129 | xmlRelaxNGPtr schema; |
| 8130 | xmlRelaxNGGrammarPtr grammar; |
| 8131 | xmlRegExecCtxtPtr exec; |
| 8132 | xmlRelaxNGDefinePtr define; |
| 8133 | |
| 8134 | schema = ctxt->schema; |
| 8135 | if (schema == NULL) { |
| 8136 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 8137 | return (-1); |
| 8138 | } |
| 8139 | grammar = schema->topgrammar; |
| 8140 | if ((grammar == NULL) || (grammar->start == NULL)) { |
| 8141 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 8142 | return (-1); |
| 8143 | } |
| 8144 | define = grammar->start; |
| 8145 | if (define->contModel == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8146 | ctxt->pdef = define; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8147 | return (0); |
| 8148 | } |
| 8149 | exec = xmlRegNewExecCtxt(define->contModel, |
| 8150 | xmlRelaxNGValidateProgressiveCallback, |
| 8151 | ctxt); |
| 8152 | if (exec == NULL) { |
| 8153 | return (-1); |
| 8154 | } |
| 8155 | xmlRelaxNGElemPush(ctxt, exec); |
| 8156 | } |
| 8157 | ctxt->pnode = elem; |
| 8158 | ctxt->pstate = 0; |
| 8159 | if (elem->ns != NULL) { |
| 8160 | ret = |
| 8161 | xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href, |
| 8162 | ctxt); |
| 8163 | } else { |
| 8164 | ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt); |
| 8165 | } |
| 8166 | if (ret < 0) { |
| 8167 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name); |
| 8168 | } else { |
| 8169 | if (ctxt->pstate == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8170 | ret = 0; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8171 | else if (ctxt->pstate < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8172 | ret = -1; |
| 8173 | else |
| 8174 | ret = 1; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8175 | } |
| 8176 | #ifdef DEBUG_PROGRESSIVE |
| 8177 | if (ret < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8178 | xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n", |
| 8179 | elem->name); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8180 | #endif |
| 8181 | return (ret); |
| 8182 | } |
| 8183 | |
| 8184 | /** |
| 8185 | * xmlRelaxNGValidatePushCData: |
| 8186 | * @ctxt: the RelaxNG validation context |
| 8187 | * @data: some character data read |
| 8188 | * @len: the lenght of the data |
| 8189 | * |
| 8190 | * check the CData parsed for validation in the current stack |
| 8191 | * |
| 8192 | * returns 1 if no validation problem was found or -1 otherwise |
| 8193 | */ |
| 8194 | int |
| 8195 | xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8196 | const xmlChar * data, int len ATTRIBUTE_UNUSED) |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8197 | { |
| 8198 | int ret = 1; |
| 8199 | |
| 8200 | if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL)) |
| 8201 | return (-1); |
| 8202 | |
| 8203 | #ifdef DEBUG_PROGRESSIVE |
| 8204 | xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len); |
| 8205 | #endif |
| 8206 | |
| 8207 | while (*data != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8208 | if (!IS_BLANK_CH(*data)) |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8209 | break; |
| 8210 | data++; |
| 8211 | } |
| 8212 | if (*data == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8213 | return (1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8214 | |
| 8215 | ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt); |
| 8216 | if (ret < 0) { |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8217 | VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO "); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8218 | #ifdef DEBUG_PROGRESSIVE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8219 | xmlGenericError(xmlGenericErrorContext, "CDATA failed\n"); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8220 | #endif |
| 8221 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8222 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8223 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8224 | return (1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8225 | } |
| 8226 | |
| 8227 | /** |
| 8228 | * xmlRelaxNGValidatePopElement: |
| 8229 | * @ctxt: the RelaxNG validation context |
| 8230 | * @doc: a document instance |
| 8231 | * @elem: an element instance |
| 8232 | * |
| 8233 | * Pop the element end from the RelaxNG validation stack. |
| 8234 | * |
| 8235 | * returns 1 if no validation problem was found or 0 otherwise |
| 8236 | */ |
| 8237 | int |
| 8238 | xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8239 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8240 | xmlNodePtr elem) |
| 8241 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8242 | int ret; |
| 8243 | xmlRegExecCtxtPtr exec; |
| 8244 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8245 | if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL)) |
| 8246 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8247 | #ifdef DEBUG_PROGRESSIVE |
| 8248 | xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name); |
| 8249 | #endif |
| 8250 | /* |
| 8251 | * verify that we reached a terminal state of the content model. |
| 8252 | */ |
| 8253 | exec = xmlRelaxNGElemPop(ctxt); |
| 8254 | ret = xmlRegExecPushString(exec, NULL, NULL); |
| 8255 | if (ret == 0) { |
| 8256 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8257 | * TODO: get some of the names needed to exit the current state of exec |
| 8258 | */ |
| 8259 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST ""); |
| 8260 | ret = -1; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8261 | } else if (ret < 0) { |
| 8262 | ret = -1; |
| 8263 | } else { |
| 8264 | ret = 1; |
| 8265 | } |
| 8266 | xmlRegFreeExecCtxt(exec); |
| 8267 | #ifdef DEBUG_PROGRESSIVE |
| 8268 | if (ret < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8269 | xmlGenericError(xmlGenericErrorContext, "PopElem %s failed\n", |
| 8270 | elem->name); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8271 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8272 | return (ret); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8273 | } |
| 8274 | |
| 8275 | /** |
| 8276 | * xmlRelaxNGValidateFullElement: |
| 8277 | * @ctxt: the validation context |
| 8278 | * @doc: a document instance |
| 8279 | * @elem: an element instance |
| 8280 | * |
| 8281 | * Validate a full subtree when xmlRelaxNGValidatePushElement() returned |
| 8282 | * 0 and the content of the node has been expanded. |
| 8283 | * |
| 8284 | * returns 1 if no validation problem was found or -1 in case of error. |
| 8285 | */ |
| 8286 | int |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8287 | xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8288 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8289 | xmlNodePtr elem) |
| 8290 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8291 | int ret; |
| 8292 | xmlRelaxNGValidStatePtr state; |
| 8293 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8294 | if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL)) |
| 8295 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8296 | #ifdef DEBUG_PROGRESSIVE |
| 8297 | xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name); |
| 8298 | #endif |
| 8299 | state = xmlRelaxNGNewValidState(ctxt, elem->parent); |
| 8300 | if (state == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8301 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8302 | } |
| 8303 | state->seq = elem; |
| 8304 | ctxt->state = state; |
| 8305 | ctxt->errNo = XML_RELAXNG_OK; |
| 8306 | ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8307 | if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK)) |
| 8308 | ret = -1; |
| 8309 | else |
| 8310 | ret = 1; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8311 | xmlRelaxNGFreeValidState(ctxt, state); |
| 8312 | ctxt->state = NULL; |
| 8313 | #ifdef DEBUG_PROGRESSIVE |
| 8314 | if (ret < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8315 | xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n", |
| 8316 | elem->name); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8317 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8318 | return (ret); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8319 | } |
| 8320 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 8321 | /************************************************************************ |
| 8322 | * * |
| 8323 | * Generic interpreted validation implementation * |
| 8324 | * * |
| 8325 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8326 | static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
| 8327 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8328 | |
| 8329 | /** |
| 8330 | * xmlRelaxNGSkipIgnored: |
| 8331 | * @ctxt: a schema validation context |
| 8332 | * @node: the top node. |
| 8333 | * |
| 8334 | * Skip ignorable nodes in that context |
| 8335 | * |
| 8336 | * Returns the new sibling or NULL in case of error. |
| 8337 | */ |
| 8338 | static xmlNodePtr |
| 8339 | xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8340 | xmlNodePtr node) |
| 8341 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8342 | /* |
| 8343 | * TODO complete and handle entities |
| 8344 | */ |
| 8345 | while ((node != NULL) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8346 | ((node->type == XML_COMMENT_NODE) || |
| 8347 | (node->type == XML_PI_NODE) || |
William M. Brack | 7217c86 | 2004-03-15 02:43:56 +0000 | [diff] [blame] | 8348 | (node->type == XML_XINCLUDE_START) || |
| 8349 | (node->type == XML_XINCLUDE_END) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8350 | (((node->type == XML_TEXT_NODE) || |
| 8351 | (node->type == XML_CDATA_SECTION_NODE)) && |
| 8352 | ((ctxt->flags & FLAGS_MIXED_CONTENT) || |
| 8353 | (IS_BLANK_NODE(node)))))) { |
| 8354 | node = node->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8355 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8356 | return (node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8357 | } |
| 8358 | |
| 8359 | /** |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8360 | * xmlRelaxNGNormalize: |
| 8361 | * @ctxt: a schema validation context |
| 8362 | * @str: the string to normalize |
| 8363 | * |
| 8364 | * Implements the normalizeWhiteSpace( s ) function from |
| 8365 | * section 6.2.9 of the spec |
| 8366 | * |
| 8367 | * Returns the new string or NULL in case of error. |
| 8368 | */ |
| 8369 | static xmlChar * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8370 | xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar * str) |
| 8371 | { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8372 | xmlChar *ret, *p; |
| 8373 | const xmlChar *tmp; |
| 8374 | int len; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8375 | |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8376 | if (str == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8377 | return (NULL); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8378 | tmp = str; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8379 | while (*tmp != 0) |
| 8380 | tmp++; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8381 | len = tmp - str; |
| 8382 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 8383 | ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar)); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8384 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8385 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 8386 | return (NULL); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8387 | } |
| 8388 | p = ret; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8389 | while (IS_BLANK_CH(*str)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8390 | str++; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8391 | while (*str != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8392 | if (IS_BLANK_CH(*str)) { |
| 8393 | while (IS_BLANK_CH(*str)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8394 | str++; |
| 8395 | if (*str == 0) |
| 8396 | break; |
| 8397 | *p++ = ' '; |
| 8398 | } else |
| 8399 | *p++ = *str++; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8400 | } |
| 8401 | *p = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8402 | return (ret); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8403 | } |
| 8404 | |
| 8405 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8406 | * xmlRelaxNGValidateDatatype: |
| 8407 | * @ctxt: a Relax-NG validation context |
| 8408 | * @value: the string value |
| 8409 | * @type: the datatype definition |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8410 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8411 | * |
| 8412 | * Validate the given value against the dataype |
| 8413 | * |
| 8414 | * Returns 0 if the validation succeeded or an error code. |
| 8415 | */ |
| 8416 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8417 | xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt, |
| 8418 | const xmlChar * value, |
| 8419 | xmlRelaxNGDefinePtr define, xmlNodePtr node) |
| 8420 | { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8421 | int ret, tmp; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8422 | xmlRelaxNGTypeLibraryPtr lib; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8423 | void *result = NULL; |
| 8424 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8425 | |
| 8426 | if ((define == NULL) || (define->data == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8427 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8428 | } |
| 8429 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8430 | if (lib->check != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8431 | if ((define->attrs != NULL) && |
| 8432 | (define->attrs->type == XML_RELAXNG_PARAM)) { |
| 8433 | ret = |
| 8434 | lib->check(lib->data, define->name, value, &result, node); |
| 8435 | } else { |
| 8436 | ret = lib->check(lib->data, define->name, value, NULL, node); |
| 8437 | } |
| 8438 | } else |
| 8439 | ret = -1; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8440 | if (ret < 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8441 | VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name); |
| 8442 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
| 8443 | lib->freef(lib->data, result); |
| 8444 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8445 | } else if (ret == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8446 | ret = 0; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8447 | } else if (ret == 2) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8448 | VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8449 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8450 | VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value); |
| 8451 | ret = -1; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8452 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8453 | cur = define->attrs; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8454 | while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8455 | if (lib->facet != NULL) { |
| 8456 | tmp = lib->facet(lib->data, define->name, cur->name, |
| 8457 | cur->value, value, result); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8458 | if (tmp != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8459 | ret = -1; |
| 8460 | } |
| 8461 | cur = cur->next; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8462 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8463 | if ((ret == 0) && (define->content != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8464 | const xmlChar *oldvalue, *oldendvalue; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8465 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8466 | oldvalue = ctxt->state->value; |
| 8467 | oldendvalue = ctxt->state->endvalue; |
| 8468 | ctxt->state->value = (xmlChar *) value; |
| 8469 | ctxt->state->endvalue = NULL; |
| 8470 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 8471 | ctxt->state->value = (xmlChar *) oldvalue; |
| 8472 | ctxt->state->endvalue = (xmlChar *) oldendvalue; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8473 | } |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8474 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8475 | lib->freef(lib->data, result); |
| 8476 | return (ret); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8477 | } |
| 8478 | |
| 8479 | /** |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8480 | * xmlRelaxNGNextValue: |
| 8481 | * @ctxt: a Relax-NG validation context |
| 8482 | * |
| 8483 | * Skip to the next value when validating within a list |
| 8484 | * |
| 8485 | * Returns 0 if the operation succeeded or an error code. |
| 8486 | */ |
| 8487 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8488 | xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt) |
| 8489 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8490 | xmlChar *cur; |
| 8491 | |
| 8492 | cur = ctxt->state->value; |
| 8493 | if ((cur == NULL) || (ctxt->state->endvalue == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8494 | ctxt->state->value = NULL; |
| 8495 | ctxt->state->endvalue = NULL; |
| 8496 | return (0); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8497 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8498 | while (*cur != 0) |
| 8499 | cur++; |
| 8500 | while ((cur != ctxt->state->endvalue) && (*cur == 0)) |
| 8501 | cur++; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8502 | if (cur == ctxt->state->endvalue) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8503 | ctxt->state->value = NULL; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8504 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8505 | ctxt->state->value = cur; |
| 8506 | return (0); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8507 | } |
| 8508 | |
| 8509 | /** |
| 8510 | * xmlRelaxNGValidateValueList: |
| 8511 | * @ctxt: a Relax-NG validation context |
| 8512 | * @defines: the list of definitions to verify |
| 8513 | * |
| 8514 | * Validate the given set of definitions for the current value |
| 8515 | * |
| 8516 | * Returns 0 if the validation succeeded or an error code. |
| 8517 | */ |
| 8518 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8519 | xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt, |
| 8520 | xmlRelaxNGDefinePtr defines) |
| 8521 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8522 | int ret = 0; |
| 8523 | |
| 8524 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8525 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
| 8526 | if (ret != 0) |
| 8527 | break; |
| 8528 | defines = defines->next; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8529 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8530 | return (ret); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8531 | } |
| 8532 | |
| 8533 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8534 | * xmlRelaxNGValidateValue: |
| 8535 | * @ctxt: a Relax-NG validation context |
| 8536 | * @define: the definition to verify |
| 8537 | * |
| 8538 | * Validate the given definition for the current value |
| 8539 | * |
| 8540 | * Returns 0 if the validation succeeded or an error code. |
| 8541 | */ |
| 8542 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8543 | xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
| 8544 | xmlRelaxNGDefinePtr define) |
| 8545 | { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8546 | int ret = 0, oldflags; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8547 | xmlChar *value; |
| 8548 | |
| 8549 | value = ctxt->state->value; |
| 8550 | switch (define->type) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8551 | case XML_RELAXNG_EMPTY:{ |
| 8552 | if ((value != NULL) && (value[0] != 0)) { |
| 8553 | int idx = 0; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 8554 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8555 | while (IS_BLANK_CH(value[idx])) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8556 | idx++; |
| 8557 | if (value[idx] != 0) |
| 8558 | ret = -1; |
| 8559 | } |
| 8560 | break; |
| 8561 | } |
| 8562 | case XML_RELAXNG_TEXT: |
| 8563 | break; |
| 8564 | case XML_RELAXNG_VALUE:{ |
| 8565 | if (!xmlStrEqual(value, define->value)) { |
| 8566 | if (define->name != NULL) { |
| 8567 | xmlRelaxNGTypeLibraryPtr lib; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8568 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8569 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
| 8570 | if ((lib != NULL) && (lib->comp != NULL)) { |
| 8571 | ret = lib->comp(lib->data, define->name, |
| 8572 | define->value, define->node, |
| 8573 | (void *) define->attrs, |
| 8574 | value, ctxt->state->node); |
| 8575 | } else |
| 8576 | ret = -1; |
| 8577 | if (ret < 0) { |
| 8578 | VALID_ERR2(XML_RELAXNG_ERR_TYPECMP, |
| 8579 | define->name); |
| 8580 | return (-1); |
| 8581 | } else if (ret == 1) { |
| 8582 | ret = 0; |
| 8583 | } else { |
| 8584 | ret = -1; |
| 8585 | } |
| 8586 | } else { |
| 8587 | xmlChar *nval, *nvalue; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8588 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8589 | /* |
| 8590 | * TODO: trivial optimizations are possible by |
| 8591 | * computing at compile-time |
| 8592 | */ |
| 8593 | nval = xmlRelaxNGNormalize(ctxt, define->value); |
| 8594 | nvalue = xmlRelaxNGNormalize(ctxt, value); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8595 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8596 | if ((nval == NULL) || (nvalue == NULL) || |
| 8597 | (!xmlStrEqual(nval, nvalue))) |
| 8598 | ret = -1; |
| 8599 | if (nval != NULL) |
| 8600 | xmlFree(nval); |
| 8601 | if (nvalue != NULL) |
| 8602 | xmlFree(nvalue); |
| 8603 | } |
| 8604 | } |
| 8605 | if (ret == 0) |
| 8606 | xmlRelaxNGNextValue(ctxt); |
| 8607 | break; |
| 8608 | } |
| 8609 | case XML_RELAXNG_DATATYPE:{ |
| 8610 | ret = xmlRelaxNGValidateDatatype(ctxt, value, define, |
| 8611 | ctxt->state->seq); |
| 8612 | if (ret == 0) |
| 8613 | xmlRelaxNGNextValue(ctxt); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8614 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8615 | break; |
| 8616 | } |
| 8617 | case XML_RELAXNG_CHOICE:{ |
| 8618 | xmlRelaxNGDefinePtr list = define->content; |
| 8619 | xmlChar *oldvalue; |
| 8620 | |
| 8621 | oldflags = ctxt->flags; |
| 8622 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8623 | |
| 8624 | oldvalue = ctxt->state->value; |
| 8625 | while (list != NULL) { |
| 8626 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8627 | if (ret == 0) { |
| 8628 | break; |
| 8629 | } |
| 8630 | ctxt->state->value = oldvalue; |
| 8631 | list = list->next; |
| 8632 | } |
| 8633 | ctxt->flags = oldflags; |
| 8634 | if (ret != 0) { |
| 8635 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8636 | xmlRelaxNGDumpValidError(ctxt); |
| 8637 | } else { |
| 8638 | if (ctxt->errNr > 0) |
| 8639 | xmlRelaxNGPopErrors(ctxt, 0); |
| 8640 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8641 | break; |
| 8642 | } |
| 8643 | case XML_RELAXNG_LIST:{ |
| 8644 | xmlRelaxNGDefinePtr list = define->content; |
| 8645 | xmlChar *oldvalue, *oldend, *val, *cur; |
| 8646 | |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8647 | #ifdef DEBUG_LIST |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8648 | int nb_values = 0; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8649 | #endif |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8650 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8651 | oldvalue = ctxt->state->value; |
| 8652 | oldend = ctxt->state->endvalue; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8653 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8654 | val = xmlStrdup(oldvalue); |
| 8655 | if (val == NULL) { |
| 8656 | val = xmlStrdup(BAD_CAST ""); |
| 8657 | } |
| 8658 | if (val == NULL) { |
| 8659 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 8660 | return (-1); |
| 8661 | } |
| 8662 | cur = val; |
| 8663 | while (*cur != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8664 | if (IS_BLANK_CH(*cur)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8665 | *cur = 0; |
| 8666 | cur++; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8667 | #ifdef DEBUG_LIST |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8668 | nb_values++; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8669 | #endif |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8670 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8671 | *cur++ = 0; |
| 8672 | } else |
| 8673 | cur++; |
| 8674 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8675 | #ifdef DEBUG_LIST |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8676 | xmlGenericError(xmlGenericErrorContext, |
| 8677 | "list value: '%s' found %d items\n", |
| 8678 | oldvalue, nb_values); |
| 8679 | nb_values = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8680 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8681 | ctxt->state->endvalue = cur; |
| 8682 | cur = val; |
| 8683 | while ((*cur == 0) && (cur != ctxt->state->endvalue)) |
| 8684 | cur++; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8685 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8686 | ctxt->state->value = cur; |
| 8687 | |
| 8688 | while (list != NULL) { |
| 8689 | if (ctxt->state->value == ctxt->state->endvalue) |
| 8690 | ctxt->state->value = NULL; |
| 8691 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8692 | if (ret != 0) { |
| 8693 | #ifdef DEBUG_LIST |
| 8694 | xmlGenericError(xmlGenericErrorContext, |
| 8695 | "Failed to validate value: '%s' with %d rule\n", |
| 8696 | ctxt->state->value, nb_values); |
| 8697 | #endif |
| 8698 | break; |
| 8699 | } |
| 8700 | #ifdef DEBUG_LIST |
| 8701 | nb_values++; |
| 8702 | #endif |
| 8703 | list = list->next; |
| 8704 | } |
| 8705 | |
| 8706 | if ((ret == 0) && (ctxt->state->value != NULL) && |
| 8707 | (ctxt->state->value != ctxt->state->endvalue)) { |
| 8708 | VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA, |
| 8709 | ctxt->state->value); |
| 8710 | ret = -1; |
| 8711 | } |
| 8712 | xmlFree(val); |
| 8713 | ctxt->state->value = oldvalue; |
| 8714 | ctxt->state->endvalue = oldend; |
| 8715 | break; |
| 8716 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8717 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8718 | ret = xmlRelaxNGValidateValueList(ctxt, define->content); |
| 8719 | if (ret != 0) { |
| 8720 | break; |
| 8721 | } |
| 8722 | /* no break on purpose */ |
| 8723 | case XML_RELAXNG_ZEROORMORE:{ |
| 8724 | xmlChar *cur, *temp; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8725 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8726 | oldflags = ctxt->flags; |
| 8727 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8728 | cur = ctxt->state->value; |
| 8729 | temp = NULL; |
| 8730 | while ((cur != NULL) && (cur != ctxt->state->endvalue) && |
| 8731 | (temp != cur)) { |
| 8732 | temp = cur; |
| 8733 | ret = |
| 8734 | xmlRelaxNGValidateValueList(ctxt, define->content); |
| 8735 | if (ret != 0) { |
| 8736 | ctxt->state->value = temp; |
| 8737 | ret = 0; |
| 8738 | break; |
| 8739 | } |
| 8740 | cur = ctxt->state->value; |
| 8741 | } |
| 8742 | ctxt->flags = oldflags; |
| 8743 | if (ret != 0) { |
| 8744 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8745 | xmlRelaxNGDumpValidError(ctxt); |
| 8746 | } else { |
| 8747 | if (ctxt->errNr > 0) |
| 8748 | xmlRelaxNGPopErrors(ctxt, 0); |
| 8749 | } |
| 8750 | break; |
| 8751 | } |
| 8752 | case XML_RELAXNG_EXCEPT:{ |
| 8753 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8754 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8755 | list = define->content; |
| 8756 | while (list != NULL) { |
| 8757 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8758 | if (ret == 0) { |
| 8759 | ret = -1; |
| 8760 | break; |
| 8761 | } else |
| 8762 | ret = 0; |
| 8763 | list = list->next; |
| 8764 | } |
| 8765 | break; |
| 8766 | } |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 8767 | case XML_RELAXNG_DEF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8768 | case XML_RELAXNG_GROUP:{ |
| 8769 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8770 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8771 | list = define->content; |
| 8772 | while (list != NULL) { |
| 8773 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8774 | if (ret != 0) { |
| 8775 | ret = -1; |
| 8776 | break; |
| 8777 | } else |
| 8778 | ret = 0; |
| 8779 | list = list->next; |
| 8780 | } |
| 8781 | break; |
| 8782 | } |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 8783 | case XML_RELAXNG_REF: |
| 8784 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8785 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 8786 | break; |
| 8787 | default: |
| 8788 | TODO ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8789 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8790 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8791 | } |
| 8792 | |
| 8793 | /** |
| 8794 | * xmlRelaxNGValidateValueContent: |
| 8795 | * @ctxt: a Relax-NG validation context |
| 8796 | * @defines: the list of definitions to verify |
| 8797 | * |
| 8798 | * Validate the given definitions for the current value |
| 8799 | * |
| 8800 | * Returns 0 if the validation succeeded or an error code. |
| 8801 | */ |
| 8802 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8803 | xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt, |
| 8804 | xmlRelaxNGDefinePtr defines) |
| 8805 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8806 | int ret = 0; |
| 8807 | |
| 8808 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8809 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
| 8810 | if (ret != 0) |
| 8811 | break; |
| 8812 | defines = defines->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8813 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8814 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8815 | } |
| 8816 | |
| 8817 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8818 | * xmlRelaxNGAttributeMatch: |
| 8819 | * @ctxt: a Relax-NG validation context |
| 8820 | * @define: the definition to check |
| 8821 | * @prop: the attribute |
| 8822 | * |
| 8823 | * Check if the attribute matches the definition nameClass |
| 8824 | * |
| 8825 | * Returns 1 if the attribute matches, 0 if no, or -1 in case of error |
| 8826 | */ |
| 8827 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8828 | xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 8829 | xmlRelaxNGDefinePtr define, xmlAttrPtr prop) |
| 8830 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8831 | int ret; |
| 8832 | |
| 8833 | if (define->name != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8834 | if (!xmlStrEqual(define->name, prop->name)) |
| 8835 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8836 | } |
| 8837 | if (define->ns != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8838 | if (define->ns[0] == 0) { |
| 8839 | if (prop->ns != NULL) |
| 8840 | return (0); |
| 8841 | } else { |
| 8842 | if ((prop->ns == NULL) || |
| 8843 | (!xmlStrEqual(define->ns, prop->ns->href))) |
| 8844 | return (0); |
| 8845 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8846 | } |
| 8847 | if (define->nameClass == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8848 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8849 | define = define->nameClass; |
| 8850 | if (define->type == XML_RELAXNG_EXCEPT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8851 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8852 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8853 | list = define->content; |
| 8854 | while (list != NULL) { |
| 8855 | ret = xmlRelaxNGAttributeMatch(ctxt, list, prop); |
| 8856 | if (ret == 1) |
| 8857 | return (0); |
| 8858 | if (ret < 0) |
| 8859 | return (ret); |
| 8860 | list = list->next; |
| 8861 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8862 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8863 | TODO} |
| 8864 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8865 | } |
| 8866 | |
| 8867 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8868 | * xmlRelaxNGValidateAttribute: |
| 8869 | * @ctxt: a Relax-NG validation context |
| 8870 | * @define: the definition to verify |
| 8871 | * |
| 8872 | * Validate the given attribute definition for that node |
| 8873 | * |
| 8874 | * Returns 0 if the validation succeeded or an error code. |
| 8875 | */ |
| 8876 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8877 | xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt, |
| 8878 | xmlRelaxNGDefinePtr define) |
| 8879 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8880 | int ret = 0, i; |
| 8881 | xmlChar *value, *oldvalue; |
| 8882 | xmlAttrPtr prop = NULL, tmp; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8883 | xmlNodePtr oldseq; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8884 | |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 8885 | if (ctxt->state->nbAttrLeft <= 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8886 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8887 | if (define->name != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8888 | for (i = 0; i < ctxt->state->nbAttrs; i++) { |
| 8889 | tmp = ctxt->state->attrs[i]; |
| 8890 | if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) { |
| 8891 | if ((((define->ns == NULL) || (define->ns[0] == 0)) && |
| 8892 | (tmp->ns == NULL)) || |
| 8893 | ((tmp->ns != NULL) && |
| 8894 | (xmlStrEqual(define->ns, tmp->ns->href)))) { |
| 8895 | prop = tmp; |
| 8896 | break; |
| 8897 | } |
| 8898 | } |
| 8899 | } |
| 8900 | if (prop != NULL) { |
| 8901 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 8902 | oldvalue = ctxt->state->value; |
| 8903 | oldseq = ctxt->state->seq; |
| 8904 | ctxt->state->seq = (xmlNodePtr) prop; |
| 8905 | ctxt->state->value = value; |
| 8906 | ctxt->state->endvalue = NULL; |
| 8907 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
| 8908 | if (ctxt->state->value != NULL) |
| 8909 | value = ctxt->state->value; |
| 8910 | if (value != NULL) |
| 8911 | xmlFree(value); |
| 8912 | ctxt->state->value = oldvalue; |
| 8913 | ctxt->state->seq = oldseq; |
| 8914 | if (ret == 0) { |
| 8915 | /* |
| 8916 | * flag the attribute as processed |
| 8917 | */ |
| 8918 | ctxt->state->attrs[i] = NULL; |
| 8919 | ctxt->state->nbAttrLeft--; |
| 8920 | } |
| 8921 | } else { |
| 8922 | ret = -1; |
| 8923 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8924 | #ifdef DEBUG |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8925 | xmlGenericError(xmlGenericErrorContext, |
| 8926 | "xmlRelaxNGValidateAttribute(%s): %d\n", |
| 8927 | define->name, ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8928 | #endif |
| 8929 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8930 | for (i = 0; i < ctxt->state->nbAttrs; i++) { |
| 8931 | tmp = ctxt->state->attrs[i]; |
| 8932 | if ((tmp != NULL) && |
| 8933 | (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) { |
| 8934 | prop = tmp; |
| 8935 | break; |
| 8936 | } |
| 8937 | } |
| 8938 | if (prop != NULL) { |
| 8939 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 8940 | oldvalue = ctxt->state->value; |
| 8941 | oldseq = ctxt->state->seq; |
| 8942 | ctxt->state->seq = (xmlNodePtr) prop; |
| 8943 | ctxt->state->value = value; |
| 8944 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
| 8945 | if (ctxt->state->value != NULL) |
| 8946 | value = ctxt->state->value; |
| 8947 | if (value != NULL) |
| 8948 | xmlFree(value); |
| 8949 | ctxt->state->value = oldvalue; |
| 8950 | ctxt->state->seq = oldseq; |
| 8951 | if (ret == 0) { |
| 8952 | /* |
| 8953 | * flag the attribute as processed |
| 8954 | */ |
| 8955 | ctxt->state->attrs[i] = NULL; |
| 8956 | ctxt->state->nbAttrLeft--; |
| 8957 | } |
| 8958 | } else { |
| 8959 | ret = -1; |
| 8960 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8961 | #ifdef DEBUG |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8962 | if (define->ns != NULL) { |
| 8963 | xmlGenericError(xmlGenericErrorContext, |
| 8964 | "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n", |
| 8965 | define->ns, ret); |
| 8966 | } else { |
| 8967 | xmlGenericError(xmlGenericErrorContext, |
| 8968 | "xmlRelaxNGValidateAttribute(anyName): %d\n", |
| 8969 | ret); |
| 8970 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8971 | #endif |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8972 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8973 | |
| 8974 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8975 | } |
| 8976 | |
| 8977 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8978 | * xmlRelaxNGValidateAttributeList: |
| 8979 | * @ctxt: a Relax-NG validation context |
| 8980 | * @define: the list of definition to verify |
| 8981 | * |
| 8982 | * Validate the given node against the list of attribute definitions |
| 8983 | * |
| 8984 | * Returns 0 if the validation succeeded or an error code. |
| 8985 | */ |
| 8986 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8987 | xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
| 8988 | xmlRelaxNGDefinePtr defines) |
| 8989 | { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8990 | int ret = 0, res; |
| 8991 | int needmore = 0; |
| 8992 | xmlRelaxNGDefinePtr cur; |
| 8993 | |
| 8994 | cur = defines; |
| 8995 | while (cur != NULL) { |
| 8996 | if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8997 | if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0) |
| 8998 | ret = -1; |
| 8999 | } else |
| 9000 | needmore = 1; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9001 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9002 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9003 | if (!needmore) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9004 | return (ret); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9005 | cur = defines; |
| 9006 | while (cur != NULL) { |
| 9007 | if (cur->type != XML_RELAXNG_ATTRIBUTE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9008 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
| 9009 | res = xmlRelaxNGValidateDefinition(ctxt, cur); |
| 9010 | if (res < 0) |
| 9011 | ret = -1; |
| 9012 | } else { |
| 9013 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 9014 | return (-1); |
| 9015 | } |
| 9016 | if (res == -1) /* continues on -2 */ |
| 9017 | break; |
| 9018 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9019 | cur = cur->next; |
| 9020 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9021 | |
| 9022 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9023 | } |
| 9024 | |
| 9025 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 9026 | * xmlRelaxNGNodeMatchesList: |
| 9027 | * @node: the node |
| 9028 | * @list: a NULL terminated array of definitions |
| 9029 | * |
| 9030 | * Check if a node can be matched by one of the definitions |
| 9031 | * |
| 9032 | * Returns 1 if matches 0 otherwise |
| 9033 | */ |
| 9034 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9035 | xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr * list) |
| 9036 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 9037 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9038 | int i = 0, tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 9039 | |
| 9040 | if ((node == NULL) || (list == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9041 | return (0); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 9042 | |
| 9043 | cur = list[i++]; |
| 9044 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9045 | if ((node->type == XML_ELEMENT_NODE) && |
| 9046 | (cur->type == XML_RELAXNG_ELEMENT)) { |
| 9047 | tmp = xmlRelaxNGElementMatch(NULL, cur, node); |
| 9048 | if (tmp == 1) |
| 9049 | return (1); |
| 9050 | } else if (((node->type == XML_TEXT_NODE) || |
| 9051 | (node->type == XML_CDATA_SECTION_NODE)) && |
| 9052 | (cur->type == XML_RELAXNG_TEXT)) { |
| 9053 | return (1); |
| 9054 | } |
| 9055 | cur = list[i++]; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 9056 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9057 | return (0); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 9058 | } |
| 9059 | |
| 9060 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9061 | * xmlRelaxNGValidateInterleave: |
| 9062 | * @ctxt: a Relax-NG validation context |
| 9063 | * @define: the definition to verify |
| 9064 | * |
| 9065 | * Validate an interleave definition for a node. |
| 9066 | * |
| 9067 | * Returns 0 if the validation succeeded or an error code. |
| 9068 | */ |
| 9069 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9070 | xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt, |
| 9071 | xmlRelaxNGDefinePtr define) |
| 9072 | { |
William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 9073 | int ret = 0, i, nbgroups; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9074 | int errNr = ctxt->errNr; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9075 | int oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9076 | |
| 9077 | xmlRelaxNGValidStatePtr oldstate; |
| 9078 | xmlRelaxNGPartitionPtr partitions; |
| 9079 | xmlRelaxNGInterleaveGroupPtr group = NULL; |
| 9080 | xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem; |
| 9081 | xmlNodePtr *list = NULL, *lasts = NULL; |
| 9082 | |
| 9083 | if (define->data != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9084 | partitions = (xmlRelaxNGPartitionPtr) define->data; |
| 9085 | nbgroups = partitions->nbgroups; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9086 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9087 | VALID_ERR(XML_RELAXNG_ERR_INTERNODATA); |
| 9088 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9089 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9090 | /* |
| 9091 | * Optimizations for MIXED |
| 9092 | */ |
| 9093 | oldflags = ctxt->flags; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 9094 | if (define->dflags & IS_MIXED) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9095 | ctxt->flags |= FLAGS_MIXED_CONTENT; |
| 9096 | if (nbgroups == 2) { |
| 9097 | /* |
| 9098 | * this is a pure <mixed> case |
| 9099 | */ |
| 9100 | if (ctxt->state != NULL) |
| 9101 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
| 9102 | ctxt->state->seq); |
| 9103 | if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT) |
| 9104 | ret = xmlRelaxNGValidateDefinition(ctxt, |
| 9105 | partitions->groups[1]-> |
| 9106 | rule); |
| 9107 | else |
| 9108 | ret = xmlRelaxNGValidateDefinition(ctxt, |
| 9109 | partitions->groups[0]-> |
| 9110 | rule); |
| 9111 | if (ret == 0) { |
| 9112 | if (ctxt->state != NULL) |
| 9113 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
| 9114 | ctxt->state-> |
| 9115 | seq); |
| 9116 | } |
| 9117 | ctxt->flags = oldflags; |
| 9118 | return (ret); |
| 9119 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9120 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9121 | |
| 9122 | /* |
| 9123 | * Build arrays to store the first and last node of the chain |
| 9124 | * pertaining to each group |
| 9125 | */ |
| 9126 | list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
| 9127 | if (list == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9128 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 9129 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9130 | } |
| 9131 | memset(list, 0, nbgroups * sizeof(xmlNodePtr)); |
| 9132 | lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
| 9133 | if (lasts == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9134 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 9135 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9136 | } |
| 9137 | memset(lasts, 0, nbgroups * sizeof(xmlNodePtr)); |
| 9138 | |
| 9139 | /* |
| 9140 | * Walk the sequence of children finding the right group and |
| 9141 | * sorting them in sequences. |
| 9142 | */ |
| 9143 | cur = ctxt->state->seq; |
| 9144 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 9145 | start = cur; |
| 9146 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9147 | ctxt->state->seq = cur; |
| 9148 | if ((partitions->triage != NULL) && |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9149 | (partitions->flags & IS_DETERMINIST)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9150 | void *tmp = NULL; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9151 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9152 | if ((cur->type == XML_TEXT_NODE) || |
| 9153 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 9154 | tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text", |
| 9155 | NULL); |
| 9156 | } else if (cur->type == XML_ELEMENT_NODE) { |
| 9157 | if (cur->ns != NULL) { |
| 9158 | tmp = xmlHashLookup2(partitions->triage, cur->name, |
| 9159 | cur->ns->href); |
| 9160 | if (tmp == NULL) |
| 9161 | tmp = xmlHashLookup2(partitions->triage, |
| 9162 | BAD_CAST "#any", |
| 9163 | cur->ns->href); |
| 9164 | } else |
| 9165 | tmp = |
| 9166 | xmlHashLookup2(partitions->triage, cur->name, |
| 9167 | NULL); |
| 9168 | if (tmp == NULL) |
| 9169 | tmp = |
| 9170 | xmlHashLookup2(partitions->triage, BAD_CAST "#any", |
| 9171 | NULL); |
| 9172 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9173 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9174 | if (tmp == NULL) { |
| 9175 | i = nbgroups; |
| 9176 | } else { |
| 9177 | i = ((long) tmp) - 1; |
| 9178 | if (partitions->flags & IS_NEEDCHECK) { |
| 9179 | group = partitions->groups[i]; |
| 9180 | if (!xmlRelaxNGNodeMatchesList(cur, group->defs)) |
| 9181 | i = nbgroups; |
| 9182 | } |
| 9183 | } |
| 9184 | } else { |
| 9185 | for (i = 0; i < nbgroups; i++) { |
| 9186 | group = partitions->groups[i]; |
| 9187 | if (group == NULL) |
| 9188 | continue; |
| 9189 | if (xmlRelaxNGNodeMatchesList(cur, group->defs)) |
| 9190 | break; |
| 9191 | } |
| 9192 | } |
| 9193 | /* |
| 9194 | * We break as soon as an element not matched is found |
| 9195 | */ |
| 9196 | if (i >= nbgroups) { |
| 9197 | break; |
| 9198 | } |
| 9199 | if (lasts[i] != NULL) { |
| 9200 | lasts[i]->next = cur; |
| 9201 | lasts[i] = cur; |
| 9202 | } else { |
| 9203 | list[i] = cur; |
| 9204 | lasts[i] = cur; |
| 9205 | } |
| 9206 | if (cur->next != NULL) |
| 9207 | lastchg = cur->next; |
| 9208 | else |
| 9209 | lastchg = cur; |
| 9210 | cur = xmlRelaxNGSkipIgnored(ctxt, cur->next); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9211 | } |
| 9212 | if (ret != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9213 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
| 9214 | ret = -1; |
| 9215 | goto done; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9216 | } |
| 9217 | lastelem = cur; |
| 9218 | oldstate = ctxt->state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9219 | for (i = 0; i < nbgroups; i++) { |
| 9220 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate); |
| 9221 | group = partitions->groups[i]; |
| 9222 | if (lasts[i] != NULL) { |
| 9223 | last = lasts[i]->next; |
| 9224 | lasts[i]->next = NULL; |
| 9225 | } |
| 9226 | ctxt->state->seq = list[i]; |
| 9227 | ret = xmlRelaxNGValidateDefinition(ctxt, group->rule); |
| 9228 | if (ret != 0) |
| 9229 | break; |
| 9230 | if (ctxt->state != NULL) { |
| 9231 | cur = ctxt->state->seq; |
| 9232 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 9233 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 9234 | oldstate = ctxt->state; |
| 9235 | ctxt->state = NULL; |
| 9236 | if (cur != NULL) { |
| 9237 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
| 9238 | ret = -1; |
| 9239 | ctxt->state = oldstate; |
| 9240 | goto done; |
| 9241 | } |
| 9242 | } else if (ctxt->states != NULL) { |
| 9243 | int j; |
| 9244 | int found = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9245 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9246 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 9247 | cur = ctxt->states->tabState[j]->seq; |
| 9248 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 9249 | if (cur == NULL) { |
| 9250 | found = 1; |
| 9251 | break; |
| 9252 | } |
| 9253 | } |
| 9254 | if (ctxt->states->nbState > 0) { |
| 9255 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 9256 | oldstate = |
| 9257 | ctxt->states->tabState[ctxt->states->nbState - 1]; |
| 9258 | } |
| 9259 | for (j = 0; j < ctxt->states->nbState - 1; j++) { |
| 9260 | xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[j]); |
| 9261 | } |
| 9262 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9263 | ctxt->states = NULL; |
| 9264 | if (found == 0) { |
| 9265 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
| 9266 | ret = -1; |
| 9267 | ctxt->state = oldstate; |
| 9268 | goto done; |
| 9269 | } |
| 9270 | } else { |
| 9271 | ret = -1; |
| 9272 | break; |
| 9273 | } |
| 9274 | if (lasts[i] != NULL) { |
| 9275 | lasts[i]->next = last; |
| 9276 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9277 | } |
| 9278 | if (ctxt->state != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9279 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9280 | ctxt->state = oldstate; |
| 9281 | ctxt->state->seq = lastelem; |
| 9282 | if (ret != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9283 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
| 9284 | ret = -1; |
| 9285 | goto done; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9286 | } |
| 9287 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9288 | done: |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9289 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9290 | /* |
| 9291 | * builds the next links chain from the prev one |
| 9292 | */ |
| 9293 | cur = lastchg; |
| 9294 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9295 | if ((cur == start) || (cur->prev == NULL)) |
| 9296 | break; |
| 9297 | cur->prev->next = cur; |
| 9298 | cur = cur->prev; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9299 | } |
| 9300 | if (ret == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9301 | if (ctxt->errNr > errNr) |
| 9302 | xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9303 | } |
| 9304 | |
| 9305 | xmlFree(list); |
| 9306 | xmlFree(lasts); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9307 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9308 | } |
| 9309 | |
| 9310 | /** |
| 9311 | * xmlRelaxNGValidateDefinitionList: |
| 9312 | * @ctxt: a Relax-NG validation context |
| 9313 | * @define: the list of definition to verify |
| 9314 | * |
| 9315 | * Validate the given node content against the (list) of definitions |
| 9316 | * |
| 9317 | * Returns 0 if the validation succeeded or an error code. |
| 9318 | */ |
| 9319 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9320 | xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt, |
| 9321 | xmlRelaxNGDefinePtr defines) |
| 9322 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9323 | int ret = 0, res; |
| 9324 | |
| 9325 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 9326 | if (defines == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9327 | VALID_ERR2(XML_RELAXNG_ERR_INTERNAL, |
| 9328 | BAD_CAST "NULL definition list"); |
| 9329 | return (-1); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 9330 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9331 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9332 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
| 9333 | res = xmlRelaxNGValidateDefinition(ctxt, defines); |
| 9334 | if (res < 0) |
| 9335 | ret = -1; |
| 9336 | } else { |
| 9337 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 9338 | return (-1); |
| 9339 | } |
| 9340 | if (res == -1) /* continues on -2 */ |
| 9341 | break; |
| 9342 | defines = defines->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9343 | } |
| 9344 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9345 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9346 | } |
| 9347 | |
| 9348 | /** |
| 9349 | * xmlRelaxNGElementMatch: |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9350 | * @ctxt: a Relax-NG validation context |
| 9351 | * @define: the definition to check |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9352 | * @elem: the element |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9353 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9354 | * Check if the element matches the definition nameClass |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9355 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9356 | * 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] | 9357 | */ |
| 9358 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9359 | xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 9360 | xmlRelaxNGDefinePtr define, xmlNodePtr elem) |
| 9361 | { |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 9362 | int ret = 0, oldflags = 0; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9363 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9364 | if (define->name != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9365 | if (!xmlStrEqual(elem->name, define->name)) { |
| 9366 | VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name); |
| 9367 | return (0); |
| 9368 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9369 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9370 | if ((define->ns != NULL) && (define->ns[0] != 0)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9371 | if (elem->ns == NULL) { |
| 9372 | VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, elem->name); |
| 9373 | return (0); |
| 9374 | } else if (!xmlStrEqual(elem->ns->href, define->ns)) { |
| 9375 | VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS, |
| 9376 | elem->name, define->ns); |
| 9377 | return (0); |
| 9378 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9379 | } else if ((elem->ns != NULL) && (define->ns != NULL) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9380 | (define->name == NULL)) { |
| 9381 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, elem->name); |
| 9382 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9383 | } else if ((elem->ns != NULL) && (define->name != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9384 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, define->name); |
| 9385 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9386 | } |
| 9387 | |
| 9388 | if (define->nameClass == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9389 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9390 | |
| 9391 | define = define->nameClass; |
| 9392 | if (define->type == XML_RELAXNG_EXCEPT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9393 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9394 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9395 | if (ctxt != NULL) { |
| 9396 | oldflags = ctxt->flags; |
| 9397 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9398 | } |
| 9399 | |
| 9400 | list = define->content; |
| 9401 | while (list != NULL) { |
| 9402 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
| 9403 | if (ret == 1) { |
| 9404 | if (ctxt != NULL) |
| 9405 | ctxt->flags = oldflags; |
| 9406 | return (0); |
| 9407 | } |
| 9408 | if (ret < 0) { |
| 9409 | if (ctxt != NULL) |
| 9410 | ctxt->flags = oldflags; |
| 9411 | return (ret); |
| 9412 | } |
| 9413 | list = list->next; |
| 9414 | } |
| 9415 | ret = 1; |
| 9416 | if (ctxt != NULL) { |
| 9417 | ctxt->flags = oldflags; |
| 9418 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9419 | } else if (define->type == XML_RELAXNG_CHOICE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9420 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9421 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9422 | if (ctxt != NULL) { |
| 9423 | oldflags = ctxt->flags; |
| 9424 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9425 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9426 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9427 | list = define->nameClass; |
| 9428 | while (list != NULL) { |
| 9429 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
| 9430 | if (ret == 1) { |
| 9431 | if (ctxt != NULL) |
| 9432 | ctxt->flags = oldflags; |
| 9433 | return (1); |
| 9434 | } |
| 9435 | if (ret < 0) { |
| 9436 | if (ctxt != NULL) |
| 9437 | ctxt->flags = oldflags; |
| 9438 | return (ret); |
| 9439 | } |
| 9440 | list = list->next; |
| 9441 | } |
| 9442 | if (ctxt != NULL) { |
| 9443 | if (ret != 0) { |
| 9444 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9445 | xmlRelaxNGDumpValidError(ctxt); |
| 9446 | } else { |
| 9447 | if (ctxt->errNr > 0) |
| 9448 | xmlRelaxNGPopErrors(ctxt, 0); |
| 9449 | } |
| 9450 | } |
| 9451 | ret = 0; |
| 9452 | if (ctxt != NULL) { |
| 9453 | ctxt->flags = oldflags; |
| 9454 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9455 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9456 | TODO ret = -1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9457 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9458 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9459 | } |
| 9460 | |
| 9461 | /** |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9462 | * xmlRelaxNGBestState: |
| 9463 | * @ctxt: a Relax-NG validation context |
| 9464 | * |
| 9465 | * Find the "best" state in the ctxt->states list of states to report |
| 9466 | * errors about. I.e. a state with no element left in the child list |
| 9467 | * or the one with the less attributes left. |
| 9468 | * This is called only if a falidation error was detected |
| 9469 | * |
| 9470 | * Returns the index of the "best" state or -1 in case of error |
| 9471 | */ |
| 9472 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9473 | xmlRelaxNGBestState(xmlRelaxNGValidCtxtPtr ctxt) |
| 9474 | { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9475 | xmlRelaxNGValidStatePtr state; |
| 9476 | int i, tmp; |
| 9477 | int best = -1; |
| 9478 | int value = 1000000; |
| 9479 | |
| 9480 | if ((ctxt == NULL) || (ctxt->states == NULL) || |
| 9481 | (ctxt->states->nbState <= 0)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9482 | return (-1); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9483 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9484 | for (i = 0; i < ctxt->states->nbState; i++) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9485 | state = ctxt->states->tabState[i]; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9486 | if (state == NULL) |
| 9487 | continue; |
| 9488 | if (state->seq != NULL) { |
| 9489 | if ((best == -1) || (value > 100000)) { |
| 9490 | value = 100000; |
| 9491 | best = i; |
| 9492 | } |
| 9493 | } else { |
| 9494 | tmp = state->nbAttrLeft; |
| 9495 | if ((best == -1) || (value > tmp)) { |
| 9496 | value = tmp; |
| 9497 | best = i; |
| 9498 | } |
| 9499 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9500 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9501 | return (best); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9502 | } |
| 9503 | |
| 9504 | /** |
| 9505 | * xmlRelaxNGLogBestError: |
| 9506 | * @ctxt: a Relax-NG validation context |
| 9507 | * |
| 9508 | * Find the "best" state in the ctxt->states list of states to report |
| 9509 | * errors about and log it. |
| 9510 | */ |
| 9511 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9512 | xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt) |
| 9513 | { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9514 | int best; |
| 9515 | |
| 9516 | if ((ctxt == NULL) || (ctxt->states == NULL) || |
| 9517 | (ctxt->states->nbState <= 0)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9518 | return; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9519 | |
| 9520 | best = xmlRelaxNGBestState(ctxt); |
| 9521 | if ((best >= 0) && (best < ctxt->states->nbState)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9522 | ctxt->state = ctxt->states->tabState[best]; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9523 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9524 | xmlRelaxNGValidateElementEnd(ctxt, 1); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9525 | } |
| 9526 | } |
| 9527 | |
| 9528 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9529 | * xmlRelaxNGValidateElementEnd: |
| 9530 | * @ctxt: a Relax-NG validation context |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 9531 | * @dolog: indicate that error logging should be done |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9532 | * |
| 9533 | * Validate the end of the element, implements check that |
| 9534 | * there is nothing left not consumed in the element content |
| 9535 | * or in the attribute list. |
| 9536 | * |
| 9537 | * Returns 0 if the validation succeeded or an error code. |
| 9538 | */ |
| 9539 | static int |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 9540 | xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int dolog) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9541 | { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9542 | int i; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9543 | xmlRelaxNGValidStatePtr state; |
| 9544 | |
| 9545 | state = ctxt->state; |
| 9546 | if (state->seq != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9547 | state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq); |
| 9548 | if (state->seq != NULL) { |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 9549 | if (dolog) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9550 | VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT, |
| 9551 | state->node->name, state->seq->name); |
| 9552 | } |
| 9553 | return (-1); |
| 9554 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9555 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9556 | for (i = 0; i < state->nbAttrs; i++) { |
| 9557 | if (state->attrs[i] != NULL) { |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 9558 | if (dolog) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9559 | VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR, |
| 9560 | state->attrs[i]->name, state->node->name); |
| 9561 | } |
| 9562 | return (-1 - i); |
| 9563 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9564 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9565 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9566 | } |
| 9567 | |
| 9568 | /** |
| 9569 | * xmlRelaxNGValidateState: |
| 9570 | * @ctxt: a Relax-NG validation context |
| 9571 | * @define: the definition to verify |
| 9572 | * |
| 9573 | * Validate the current state against the definition |
| 9574 | * |
| 9575 | * Returns 0 if the validation succeeded or an error code. |
| 9576 | */ |
| 9577 | static int |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9578 | xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt, |
| 9579 | xmlRelaxNGDefinePtr define) |
| 9580 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9581 | xmlNodePtr node; |
| 9582 | int ret = 0, i, tmp, oldflags, errNr; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9583 | xmlRelaxNGValidStatePtr oldstate = NULL, state; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9584 | |
| 9585 | if (define == NULL) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9586 | VALID_ERR(XML_RELAXNG_ERR_NODEFINE); |
| 9587 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9588 | } |
| 9589 | |
| 9590 | if (ctxt->state != NULL) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9591 | node = ctxt->state->seq; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9592 | } else { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9593 | node = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9594 | } |
| 9595 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9596 | for (i = 0; i < ctxt->depth; i++) |
| 9597 | xmlGenericError(xmlGenericErrorContext, " "); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9598 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9599 | "Start validating %s ", xmlRelaxNGDefName(define)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9600 | if (define->name != NULL) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9601 | xmlGenericError(xmlGenericErrorContext, "%s ", define->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9602 | if ((node != NULL) && (node->name != NULL)) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9603 | xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9604 | else |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9605 | xmlGenericError(xmlGenericErrorContext, "\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9606 | #endif |
| 9607 | ctxt->depth++; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9608 | switch (define->type) { |
| 9609 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9610 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 9611 | ret = 0; |
| 9612 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9613 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9614 | ret = -1; |
| 9615 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9616 | case XML_RELAXNG_TEXT: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9617 | while ((node != NULL) && |
| 9618 | ((node->type == XML_TEXT_NODE) || |
| 9619 | (node->type == XML_COMMENT_NODE) || |
| 9620 | (node->type == XML_PI_NODE) || |
| 9621 | (node->type == XML_CDATA_SECTION_NODE))) |
| 9622 | node = node->next; |
| 9623 | ctxt->state->seq = node; |
| 9624 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9625 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9626 | errNr = ctxt->errNr; |
| 9627 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 9628 | if (node == NULL) { |
| 9629 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name); |
| 9630 | ret = -1; |
| 9631 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9632 | xmlRelaxNGDumpValidError(ctxt); |
| 9633 | break; |
| 9634 | } |
| 9635 | if (node->type != XML_ELEMENT_NODE) { |
| 9636 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
| 9637 | ret = -1; |
| 9638 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9639 | xmlRelaxNGDumpValidError(ctxt); |
| 9640 | break; |
| 9641 | } |
| 9642 | /* |
| 9643 | * This node was already validated successfully against |
| 9644 | * this definition. |
| 9645 | */ |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 9646 | if (node->psvi == define) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9647 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
| 9648 | if (ctxt->errNr > errNr) |
| 9649 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9650 | if (ctxt->errNr != 0) { |
| 9651 | while ((ctxt->err != NULL) && |
| 9652 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) |
| 9653 | && (xmlStrEqual(ctxt->err->arg2, node->name))) |
| 9654 | || |
| 9655 | ((ctxt->err->err == |
| 9656 | XML_RELAXNG_ERR_ELEMEXTRANS) |
| 9657 | && (xmlStrEqual(ctxt->err->arg1, node->name))) |
| 9658 | || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) |
| 9659 | || (ctxt->err->err == |
| 9660 | XML_RELAXNG_ERR_NOTELEM))) |
| 9661 | xmlRelaxNGValidErrorPop(ctxt); |
| 9662 | } |
| 9663 | break; |
| 9664 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9665 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9666 | ret = xmlRelaxNGElementMatch(ctxt, define, node); |
| 9667 | if (ret <= 0) { |
| 9668 | ret = -1; |
| 9669 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9670 | xmlRelaxNGDumpValidError(ctxt); |
| 9671 | break; |
| 9672 | } |
| 9673 | ret = 0; |
| 9674 | if (ctxt->errNr != 0) { |
| 9675 | if (ctxt->errNr > errNr) |
| 9676 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9677 | while ((ctxt->err != NULL) && |
| 9678 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) && |
| 9679 | (xmlStrEqual(ctxt->err->arg2, node->name))) || |
| 9680 | ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) && |
| 9681 | (xmlStrEqual(ctxt->err->arg1, node->name))) || |
| 9682 | (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) || |
| 9683 | (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM))) |
| 9684 | xmlRelaxNGValidErrorPop(ctxt); |
| 9685 | } |
| 9686 | errNr = ctxt->errNr; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9687 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9688 | oldflags = ctxt->flags; |
| 9689 | if (ctxt->flags & FLAGS_MIXED_CONTENT) { |
| 9690 | ctxt->flags -= FLAGS_MIXED_CONTENT; |
| 9691 | } |
| 9692 | state = xmlRelaxNGNewValidState(ctxt, node); |
| 9693 | if (state == NULL) { |
| 9694 | ret = -1; |
| 9695 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9696 | xmlRelaxNGDumpValidError(ctxt); |
| 9697 | break; |
| 9698 | } |
Daniel Veillard | 7fe1f3a | 2003-03-31 22:13:33 +0000 | [diff] [blame] | 9699 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9700 | oldstate = ctxt->state; |
| 9701 | ctxt->state = state; |
| 9702 | if (define->attrs != NULL) { |
| 9703 | tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
| 9704 | if (tmp != 0) { |
| 9705 | ret = -1; |
| 9706 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
| 9707 | } |
| 9708 | } |
| 9709 | if (define->contModel != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9710 | xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state; |
| 9711 | xmlRelaxNGStatesPtr tmpstates = ctxt->states; |
| 9712 | xmlNodePtr nseq; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9713 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9714 | nstate = xmlRelaxNGNewValidState(ctxt, node); |
| 9715 | ctxt->state = nstate; |
| 9716 | ctxt->states = NULL; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9717 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9718 | tmp = xmlRelaxNGValidateCompiledContent(ctxt, |
| 9719 | define->contModel, |
| 9720 | ctxt->state->seq); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9721 | nseq = ctxt->state->seq; |
| 9722 | ctxt->state = tmpstate; |
| 9723 | ctxt->states = tmpstates; |
| 9724 | xmlRelaxNGFreeValidState(ctxt, nstate); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9725 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9726 | #ifdef DEBUG_COMPILE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9727 | xmlGenericError(xmlGenericErrorContext, |
| 9728 | "Validating content of '%s' : %d\n", |
| 9729 | define->name, tmp); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9730 | #endif |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9731 | if (tmp != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9732 | ret = -1; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9733 | |
| 9734 | if (ctxt->states != NULL) { |
| 9735 | tmp = -1; |
| 9736 | |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9737 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9738 | state = ctxt->states->tabState[i]; |
| 9739 | ctxt->state = state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9740 | ctxt->state->seq = nseq; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9741 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9742 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9743 | tmp = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9744 | break; |
| 9745 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9746 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9747 | if (tmp != 0) { |
| 9748 | /* |
| 9749 | * validation error, log the message for the "best" one |
| 9750 | */ |
| 9751 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9752 | xmlRelaxNGLogBestError(ctxt); |
| 9753 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9754 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9755 | xmlRelaxNGFreeValidState(ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9756 | ctxt->states-> |
| 9757 | tabState[i]); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9758 | } |
| 9759 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9760 | ctxt->flags = oldflags; |
| 9761 | ctxt->states = NULL; |
| 9762 | if ((ret == 0) && (tmp == -1)) |
| 9763 | ret = -1; |
| 9764 | } else { |
| 9765 | state = ctxt->state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9766 | ctxt->state->seq = nseq; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9767 | if (ret == 0) |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9768 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9769 | xmlRelaxNGFreeValidState(ctxt, state); |
| 9770 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9771 | } else { |
| 9772 | if (define->content != NULL) { |
| 9773 | tmp = xmlRelaxNGValidateDefinitionList(ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9774 | define-> |
| 9775 | content); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9776 | if (tmp != 0) { |
| 9777 | ret = -1; |
| 9778 | if (ctxt->state == NULL) { |
| 9779 | ctxt->state = oldstate; |
| 9780 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, |
| 9781 | node->name); |
| 9782 | ctxt->state = NULL; |
| 9783 | } else { |
| 9784 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, |
| 9785 | node->name); |
| 9786 | } |
| 9787 | |
| 9788 | } |
| 9789 | } |
| 9790 | if (ctxt->states != NULL) { |
| 9791 | tmp = -1; |
| 9792 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9793 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9794 | state = ctxt->states->tabState[i]; |
| 9795 | ctxt->state = state; |
| 9796 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9797 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9798 | tmp = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9799 | break; |
| 9800 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9801 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9802 | if (tmp != 0) { |
| 9803 | /* |
| 9804 | * validation error, log the message for the "best" one |
| 9805 | */ |
| 9806 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9807 | xmlRelaxNGLogBestError(ctxt); |
| 9808 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9809 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9810 | xmlRelaxNGFreeValidState(ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9811 | ctxt->states-> |
| 9812 | tabState[i]); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9813 | } |
| 9814 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9815 | ctxt->flags = oldflags; |
| 9816 | ctxt->states = NULL; |
| 9817 | if ((ret == 0) && (tmp == -1)) |
| 9818 | ret = -1; |
| 9819 | } else { |
| 9820 | state = ctxt->state; |
| 9821 | if (ret == 0) |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9822 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9823 | xmlRelaxNGFreeValidState(ctxt, state); |
| 9824 | } |
| 9825 | } |
| 9826 | if (ret == 0) { |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 9827 | node->psvi = define; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9828 | } |
| 9829 | ctxt->flags = oldflags; |
| 9830 | ctxt->state = oldstate; |
| 9831 | if (oldstate != NULL) |
| 9832 | oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
| 9833 | if (ret != 0) { |
| 9834 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 9835 | xmlRelaxNGDumpValidError(ctxt); |
| 9836 | ret = 0; |
| 9837 | } else { |
| 9838 | ret = -2; |
| 9839 | } |
| 9840 | } else { |
| 9841 | if (ctxt->errNr > errNr) |
| 9842 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9843 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9844 | |
| 9845 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9846 | xmlGenericError(xmlGenericErrorContext, |
| 9847 | "xmlRelaxNGValidateDefinition(): validated %s : %d", |
| 9848 | node->name, ret); |
| 9849 | if (oldstate == NULL) |
| 9850 | xmlGenericError(xmlGenericErrorContext, ": no state\n"); |
| 9851 | else if (oldstate->seq == NULL) |
| 9852 | xmlGenericError(xmlGenericErrorContext, ": done\n"); |
| 9853 | else if (oldstate->seq->type == XML_ELEMENT_NODE) |
| 9854 | xmlGenericError(xmlGenericErrorContext, ": next elem %s\n", |
| 9855 | oldstate->seq->name); |
| 9856 | else |
| 9857 | xmlGenericError(xmlGenericErrorContext, ": next %s %d\n", |
| 9858 | oldstate->seq->name, oldstate->seq->type); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9859 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9860 | break; |
| 9861 | case XML_RELAXNG_OPTIONAL:{ |
| 9862 | errNr = ctxt->errNr; |
| 9863 | oldflags = ctxt->flags; |
| 9864 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9865 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
| 9866 | ret = |
| 9867 | xmlRelaxNGValidateDefinitionList(ctxt, |
| 9868 | define->content); |
| 9869 | if (ret != 0) { |
| 9870 | if (ctxt->state != NULL) |
| 9871 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 9872 | ctxt->state = oldstate; |
| 9873 | ctxt->flags = oldflags; |
| 9874 | ret = 0; |
| 9875 | if (ctxt->errNr > errNr) |
| 9876 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9877 | break; |
| 9878 | } |
| 9879 | if (ctxt->states != NULL) { |
| 9880 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
| 9881 | } else { |
| 9882 | ctxt->states = xmlRelaxNGNewStates(ctxt, 1); |
| 9883 | if (ctxt->states == NULL) { |
| 9884 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 9885 | ctxt->flags = oldflags; |
| 9886 | ret = -1; |
| 9887 | if (ctxt->errNr > errNr) |
| 9888 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9889 | break; |
| 9890 | } |
| 9891 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
| 9892 | xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state); |
| 9893 | ctxt->state = NULL; |
| 9894 | } |
| 9895 | ctxt->flags = oldflags; |
| 9896 | ret = 0; |
| 9897 | if (ctxt->errNr > errNr) |
| 9898 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9899 | break; |
| 9900 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9901 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9902 | errNr = ctxt->errNr; |
| 9903 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 9904 | if (ret != 0) { |
| 9905 | break; |
| 9906 | } |
| 9907 | if (ctxt->errNr > errNr) |
| 9908 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9909 | /* no break on purpose */ |
| 9910 | case XML_RELAXNG_ZEROORMORE:{ |
| 9911 | int progress; |
| 9912 | xmlRelaxNGStatesPtr states = NULL, res = NULL; |
| 9913 | int base, j; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9914 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9915 | errNr = ctxt->errNr; |
| 9916 | res = xmlRelaxNGNewStates(ctxt, 1); |
| 9917 | if (res == NULL) { |
| 9918 | ret = -1; |
| 9919 | break; |
| 9920 | } |
| 9921 | /* |
| 9922 | * All the input states are also exit states |
| 9923 | */ |
| 9924 | if (ctxt->state != NULL) { |
| 9925 | xmlRelaxNGAddStates(ctxt, res, |
| 9926 | xmlRelaxNGCopyValidState(ctxt, |
| 9927 | ctxt-> |
| 9928 | state)); |
| 9929 | } else { |
| 9930 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 9931 | xmlRelaxNGAddStates(ctxt, res, |
| 9932 | xmlRelaxNGCopyValidState(ctxt, |
| 9933 | ctxt-> |
| 9934 | states-> |
| 9935 | tabState |
| 9936 | [j])); |
| 9937 | } |
| 9938 | } |
| 9939 | oldflags = ctxt->flags; |
| 9940 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9941 | do { |
| 9942 | progress = 0; |
| 9943 | base = res->nbState; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9944 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9945 | if (ctxt->states != NULL) { |
| 9946 | states = ctxt->states; |
| 9947 | for (i = 0; i < states->nbState; i++) { |
| 9948 | ctxt->state = states->tabState[i]; |
| 9949 | ctxt->states = NULL; |
| 9950 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
| 9951 | define-> |
| 9952 | content); |
| 9953 | if (ret == 0) { |
| 9954 | if (ctxt->state != NULL) { |
| 9955 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 9956 | ctxt->state); |
| 9957 | ctxt->state = NULL; |
| 9958 | if (tmp == 1) |
| 9959 | progress = 1; |
| 9960 | } else if (ctxt->states != NULL) { |
| 9961 | for (j = 0; j < ctxt->states->nbState; |
| 9962 | j++) { |
| 9963 | tmp = |
| 9964 | xmlRelaxNGAddStates(ctxt, res, |
| 9965 | ctxt-> |
| 9966 | states-> |
| 9967 | tabState |
| 9968 | [j]); |
| 9969 | if (tmp == 1) |
| 9970 | progress = 1; |
| 9971 | } |
| 9972 | xmlRelaxNGFreeStates(ctxt, |
| 9973 | ctxt->states); |
| 9974 | ctxt->states = NULL; |
| 9975 | } |
| 9976 | } else { |
| 9977 | if (ctxt->state != NULL) { |
| 9978 | xmlRelaxNGFreeValidState(ctxt, |
| 9979 | ctxt->state); |
| 9980 | ctxt->state = NULL; |
| 9981 | } |
| 9982 | } |
| 9983 | } |
| 9984 | } else { |
| 9985 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
| 9986 | define-> |
| 9987 | content); |
| 9988 | if (ret != 0) { |
| 9989 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 9990 | ctxt->state = NULL; |
| 9991 | } else { |
| 9992 | base = res->nbState; |
| 9993 | if (ctxt->state != NULL) { |
| 9994 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 9995 | ctxt->state); |
| 9996 | ctxt->state = NULL; |
| 9997 | if (tmp == 1) |
| 9998 | progress = 1; |
| 9999 | } else if (ctxt->states != NULL) { |
| 10000 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 10001 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 10002 | ctxt-> |
| 10003 | states-> |
| 10004 | tabState[j]); |
| 10005 | if (tmp == 1) |
| 10006 | progress = 1; |
| 10007 | } |
| 10008 | if (states == NULL) { |
| 10009 | states = ctxt->states; |
| 10010 | } else { |
| 10011 | xmlRelaxNGFreeStates(ctxt, |
| 10012 | ctxt->states); |
| 10013 | } |
| 10014 | ctxt->states = NULL; |
| 10015 | } |
| 10016 | } |
| 10017 | } |
| 10018 | if (progress) { |
| 10019 | /* |
| 10020 | * Collect all the new nodes added at that step |
| 10021 | * and make them the new node set |
| 10022 | */ |
| 10023 | if (res->nbState - base == 1) { |
| 10024 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, |
| 10025 | res-> |
| 10026 | tabState |
| 10027 | [base]); |
| 10028 | } else { |
| 10029 | if (states == NULL) { |
| 10030 | xmlRelaxNGNewStates(ctxt, |
| 10031 | res->nbState - base); |
| 10032 | } |
| 10033 | states->nbState = 0; |
| 10034 | for (i = base; i < res->nbState; i++) |
| 10035 | xmlRelaxNGAddStates(ctxt, states, |
| 10036 | xmlRelaxNGCopyValidState |
| 10037 | (ctxt, |
| 10038 | res->tabState[i])); |
| 10039 | ctxt->states = states; |
| 10040 | } |
| 10041 | } |
| 10042 | } while (progress == 1); |
| 10043 | if (states != NULL) { |
| 10044 | xmlRelaxNGFreeStates(ctxt, states); |
| 10045 | } |
| 10046 | ctxt->states = res; |
| 10047 | ctxt->flags = oldflags; |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 10048 | #if 0 |
| 10049 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10050 | * errors may have to be propagated back... |
| 10051 | */ |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10052 | if (ctxt->errNr > errNr) |
| 10053 | xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 10054 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10055 | ret = 0; |
| 10056 | break; |
| 10057 | } |
| 10058 | case XML_RELAXNG_CHOICE:{ |
| 10059 | xmlRelaxNGDefinePtr list = NULL; |
| 10060 | xmlRelaxNGStatesPtr states = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10061 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10062 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10063 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10064 | errNr = ctxt->errNr; |
Daniel Veillard | 9186a1f | 2005-01-15 12:38:10 +0000 | [diff] [blame] | 10065 | if ((define->dflags & IS_TRIABLE) && (define->data != NULL) && |
| 10066 | (node != NULL)) { |
| 10067 | /* |
| 10068 | * node == NULL can't be optimized since IS_TRIABLE |
| 10069 | * doesn't account for choice which may lead to |
| 10070 | * only attributes. |
| 10071 | */ |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10072 | xmlHashTablePtr triage = |
| 10073 | (xmlHashTablePtr) define->data; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 10074 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10075 | /* |
| 10076 | * Something we can optimize cleanly there is only one |
| 10077 | * possble branch out ! |
| 10078 | */ |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10079 | if ((node->type == XML_TEXT_NODE) || |
| 10080 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 10081 | list = |
| 10082 | xmlHashLookup2(triage, BAD_CAST "#text", NULL); |
| 10083 | } else if (node->type == XML_ELEMENT_NODE) { |
| 10084 | if (node->ns != NULL) { |
| 10085 | list = xmlHashLookup2(triage, node->name, |
| 10086 | node->ns->href); |
| 10087 | if (list == NULL) |
| 10088 | list = |
| 10089 | xmlHashLookup2(triage, BAD_CAST "#any", |
| 10090 | node->ns->href); |
| 10091 | } else |
| 10092 | list = |
| 10093 | xmlHashLookup2(triage, node->name, NULL); |
| 10094 | if (list == NULL) |
| 10095 | list = |
| 10096 | xmlHashLookup2(triage, BAD_CAST "#any", |
| 10097 | NULL); |
| 10098 | } |
| 10099 | if (list == NULL) { |
| 10100 | ret = -1; |
William M. Brack | 2f07606 | 2004-03-21 11:21:14 +0000 | [diff] [blame] | 10101 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, node->name); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10102 | break; |
| 10103 | } |
| 10104 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
| 10105 | if (ret == 0) { |
| 10106 | } |
| 10107 | break; |
| 10108 | } |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 10109 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10110 | list = define->content; |
| 10111 | oldflags = ctxt->flags; |
| 10112 | ctxt->flags |= FLAGS_IGNORABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10113 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10114 | while (list != NULL) { |
| 10115 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
| 10116 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
| 10117 | if (ret == 0) { |
| 10118 | if (states == NULL) { |
| 10119 | states = xmlRelaxNGNewStates(ctxt, 1); |
| 10120 | } |
| 10121 | if (ctxt->state != NULL) { |
| 10122 | xmlRelaxNGAddStates(ctxt, states, ctxt->state); |
| 10123 | } else if (ctxt->states != NULL) { |
| 10124 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 10125 | xmlRelaxNGAddStates(ctxt, states, |
| 10126 | ctxt->states-> |
| 10127 | tabState[i]); |
| 10128 | } |
| 10129 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10130 | ctxt->states = NULL; |
| 10131 | } |
| 10132 | } else { |
| 10133 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10134 | } |
| 10135 | ctxt->state = oldstate; |
| 10136 | list = list->next; |
| 10137 | } |
| 10138 | if (states != NULL) { |
| 10139 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 10140 | ctxt->states = states; |
| 10141 | ctxt->state = NULL; |
| 10142 | ret = 0; |
| 10143 | } else { |
| 10144 | ctxt->states = NULL; |
| 10145 | } |
| 10146 | ctxt->flags = oldflags; |
| 10147 | if (ret != 0) { |
| 10148 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 10149 | xmlRelaxNGDumpValidError(ctxt); |
| 10150 | } |
| 10151 | } else { |
| 10152 | if (ctxt->errNr > errNr) |
| 10153 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 10154 | } |
| 10155 | break; |
| 10156 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10157 | case XML_RELAXNG_DEF: |
| 10158 | case XML_RELAXNG_GROUP: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10159 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 10160 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10161 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10162 | ret = xmlRelaxNGValidateInterleave(ctxt, define); |
| 10163 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10164 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10165 | ret = xmlRelaxNGValidateAttribute(ctxt, define); |
| 10166 | break; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 10167 | case XML_RELAXNG_START: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10168 | case XML_RELAXNG_NOOP: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10169 | case XML_RELAXNG_REF: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10170 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 10171 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10172 | ret = xmlRelaxNGValidateDefinition(ctxt, define->content); |
| 10173 | break; |
| 10174 | case XML_RELAXNG_DATATYPE:{ |
| 10175 | xmlNodePtr child; |
| 10176 | xmlChar *content = NULL; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10177 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10178 | child = node; |
| 10179 | while (child != NULL) { |
| 10180 | if (child->type == XML_ELEMENT_NODE) { |
| 10181 | VALID_ERR2(XML_RELAXNG_ERR_DATAELEM, |
| 10182 | node->parent->name); |
| 10183 | ret = -1; |
| 10184 | break; |
| 10185 | } else if ((child->type == XML_TEXT_NODE) || |
| 10186 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 10187 | content = xmlStrcat(content, child->content); |
| 10188 | } |
| 10189 | /* TODO: handle entities ... */ |
| 10190 | child = child->next; |
| 10191 | } |
| 10192 | if (ret == -1) { |
| 10193 | if (content != NULL) |
| 10194 | xmlFree(content); |
| 10195 | break; |
| 10196 | } |
| 10197 | if (content == NULL) { |
| 10198 | content = xmlStrdup(BAD_CAST ""); |
| 10199 | if (content == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10200 | xmlRngVErrMemory(ctxt, "validating\n"); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10201 | ret = -1; |
| 10202 | break; |
| 10203 | } |
| 10204 | } |
| 10205 | ret = xmlRelaxNGValidateDatatype(ctxt, content, define, |
| 10206 | ctxt->state->seq); |
| 10207 | if (ret == -1) { |
| 10208 | VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name); |
| 10209 | } else if (ret == 0) { |
| 10210 | ctxt->state->seq = NULL; |
| 10211 | } |
| 10212 | if (content != NULL) |
| 10213 | xmlFree(content); |
| 10214 | break; |
| 10215 | } |
| 10216 | case XML_RELAXNG_VALUE:{ |
| 10217 | xmlChar *content = NULL; |
| 10218 | xmlChar *oldvalue; |
| 10219 | xmlNodePtr child; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10220 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10221 | child = node; |
| 10222 | while (child != NULL) { |
| 10223 | if (child->type == XML_ELEMENT_NODE) { |
| 10224 | VALID_ERR2(XML_RELAXNG_ERR_VALELEM, |
| 10225 | node->parent->name); |
| 10226 | ret = -1; |
| 10227 | break; |
| 10228 | } else if ((child->type == XML_TEXT_NODE) || |
| 10229 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 10230 | content = xmlStrcat(content, child->content); |
| 10231 | } |
| 10232 | /* TODO: handle entities ... */ |
| 10233 | child = child->next; |
| 10234 | } |
| 10235 | if (ret == -1) { |
| 10236 | if (content != NULL) |
| 10237 | xmlFree(content); |
| 10238 | break; |
| 10239 | } |
| 10240 | if (content == NULL) { |
| 10241 | content = xmlStrdup(BAD_CAST ""); |
| 10242 | if (content == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10243 | xmlRngVErrMemory(ctxt, "validating\n"); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10244 | ret = -1; |
| 10245 | break; |
| 10246 | } |
| 10247 | } |
| 10248 | oldvalue = ctxt->state->value; |
| 10249 | ctxt->state->value = content; |
| 10250 | ret = xmlRelaxNGValidateValue(ctxt, define); |
| 10251 | ctxt->state->value = oldvalue; |
| 10252 | if (ret == -1) { |
| 10253 | VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name); |
| 10254 | } else if (ret == 0) { |
| 10255 | ctxt->state->seq = NULL; |
| 10256 | } |
| 10257 | if (content != NULL) |
| 10258 | xmlFree(content); |
| 10259 | break; |
| 10260 | } |
| 10261 | case XML_RELAXNG_LIST:{ |
| 10262 | xmlChar *content; |
| 10263 | xmlNodePtr child; |
| 10264 | xmlChar *oldvalue, *oldendvalue; |
| 10265 | int len; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10266 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10267 | /* |
| 10268 | * Make sure it's only text nodes |
| 10269 | */ |
| 10270 | |
| 10271 | content = NULL; |
| 10272 | child = node; |
| 10273 | while (child != NULL) { |
| 10274 | if (child->type == XML_ELEMENT_NODE) { |
| 10275 | VALID_ERR2(XML_RELAXNG_ERR_LISTELEM, |
| 10276 | node->parent->name); |
| 10277 | ret = -1; |
| 10278 | break; |
| 10279 | } else if ((child->type == XML_TEXT_NODE) || |
| 10280 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 10281 | content = xmlStrcat(content, child->content); |
| 10282 | } |
| 10283 | /* TODO: handle entities ... */ |
| 10284 | child = child->next; |
| 10285 | } |
| 10286 | if (ret == -1) { |
| 10287 | if (content != NULL) |
| 10288 | xmlFree(content); |
| 10289 | break; |
| 10290 | } |
| 10291 | if (content == NULL) { |
| 10292 | content = xmlStrdup(BAD_CAST ""); |
| 10293 | if (content == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10294 | xmlRngVErrMemory(ctxt, "validating\n"); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10295 | ret = -1; |
| 10296 | break; |
| 10297 | } |
| 10298 | } |
| 10299 | len = xmlStrlen(content); |
| 10300 | oldvalue = ctxt->state->value; |
| 10301 | oldendvalue = ctxt->state->endvalue; |
| 10302 | ctxt->state->value = content; |
| 10303 | ctxt->state->endvalue = content + len; |
| 10304 | ret = xmlRelaxNGValidateValue(ctxt, define); |
| 10305 | ctxt->state->value = oldvalue; |
| 10306 | ctxt->state->endvalue = oldendvalue; |
| 10307 | if (ret == -1) { |
| 10308 | VALID_ERR(XML_RELAXNG_ERR_LIST); |
| 10309 | } else if ((ret == 0) && (node != NULL)) { |
| 10310 | ctxt->state->seq = node->next; |
| 10311 | } |
| 10312 | if (content != NULL) |
| 10313 | xmlFree(content); |
| 10314 | break; |
| 10315 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10316 | case XML_RELAXNG_EXCEPT: |
| 10317 | case XML_RELAXNG_PARAM: |
| 10318 | TODO ret = -1; |
| 10319 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10320 | } |
| 10321 | ctxt->depth--; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10322 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10323 | for (i = 0; i < ctxt->depth; i++) |
| 10324 | xmlGenericError(xmlGenericErrorContext, " "); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10325 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10326 | "Validating %s ", xmlRelaxNGDefName(define)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10327 | if (define->name != NULL) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10328 | xmlGenericError(xmlGenericErrorContext, "%s ", define->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10329 | if (ret == 0) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10330 | xmlGenericError(xmlGenericErrorContext, "suceeded\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10331 | else |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10332 | xmlGenericError(xmlGenericErrorContext, "failed\n"); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10333 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10334 | return (ret); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10335 | } |
| 10336 | |
| 10337 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10338 | * xmlRelaxNGValidateDefinition: |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10339 | * @ctxt: a Relax-NG validation context |
| 10340 | * @define: the definition to verify |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10341 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10342 | * Validate the current node lists against the definition |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10343 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10344 | * Returns 0 if the validation succeeded or an error code. |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10345 | */ |
| 10346 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10347 | xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
| 10348 | xmlRelaxNGDefinePtr define) |
| 10349 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10350 | xmlRelaxNGStatesPtr states, res; |
| 10351 | int i, j, k, ret, oldflags; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10352 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10353 | /* |
| 10354 | * We should NOT have both ctxt->state and ctxt->states |
| 10355 | */ |
| 10356 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10357 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10358 | ctxt->state = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10359 | } |
| 10360 | |
| 10361 | if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10362 | if (ctxt->states != NULL) { |
| 10363 | ctxt->state = ctxt->states->tabState[0]; |
| 10364 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10365 | ctxt->states = NULL; |
| 10366 | } |
| 10367 | ret = xmlRelaxNGValidateState(ctxt, define); |
| 10368 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10369 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10370 | ctxt->state = NULL; |
| 10371 | } |
| 10372 | if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) { |
| 10373 | ctxt->state = ctxt->states->tabState[0]; |
| 10374 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10375 | ctxt->states = NULL; |
| 10376 | } |
| 10377 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10378 | } |
| 10379 | |
| 10380 | states = ctxt->states; |
| 10381 | ctxt->states = NULL; |
| 10382 | res = NULL; |
| 10383 | j = 0; |
| 10384 | oldflags = ctxt->flags; |
| 10385 | ctxt->flags |= FLAGS_IGNORABLE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10386 | for (i = 0; i < states->nbState; i++) { |
| 10387 | ctxt->state = states->tabState[i]; |
| 10388 | ctxt->states = NULL; |
| 10389 | ret = xmlRelaxNGValidateState(ctxt, define); |
| 10390 | /* |
| 10391 | * We should NOT have both ctxt->state and ctxt->states |
| 10392 | */ |
| 10393 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10394 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10395 | ctxt->state = NULL; |
| 10396 | } |
| 10397 | if (ret == 0) { |
| 10398 | if (ctxt->states == NULL) { |
| 10399 | if (res != NULL) { |
| 10400 | /* add the state to the container */ |
| 10401 | xmlRelaxNGAddStates(ctxt, res, ctxt->state); |
| 10402 | ctxt->state = NULL; |
| 10403 | } else { |
| 10404 | /* add the state directly in states */ |
| 10405 | states->tabState[j++] = ctxt->state; |
| 10406 | ctxt->state = NULL; |
| 10407 | } |
| 10408 | } else { |
| 10409 | if (res == NULL) { |
| 10410 | /* make it the new container and copy other results */ |
| 10411 | res = ctxt->states; |
| 10412 | ctxt->states = NULL; |
| 10413 | for (k = 0; k < j; k++) |
| 10414 | xmlRelaxNGAddStates(ctxt, res, |
| 10415 | states->tabState[k]); |
| 10416 | } else { |
| 10417 | /* add all the new results to res and reff the container */ |
| 10418 | for (k = 0; k < ctxt->states->nbState; k++) |
| 10419 | xmlRelaxNGAddStates(ctxt, res, |
| 10420 | ctxt->states->tabState[k]); |
| 10421 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10422 | ctxt->states = NULL; |
| 10423 | } |
| 10424 | } |
| 10425 | } else { |
| 10426 | if (ctxt->state != NULL) { |
| 10427 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10428 | ctxt->state = NULL; |
| 10429 | } else if (ctxt->states != NULL) { |
| 10430 | for (k = 0; k < ctxt->states->nbState; k++) |
| 10431 | xmlRelaxNGFreeValidState(ctxt, |
| 10432 | ctxt->states->tabState[k]); |
| 10433 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10434 | ctxt->states = NULL; |
| 10435 | } |
| 10436 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10437 | } |
| 10438 | ctxt->flags = oldflags; |
| 10439 | if (res != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10440 | xmlRelaxNGFreeStates(ctxt, states); |
| 10441 | ctxt->states = res; |
| 10442 | ret = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10443 | } else if (j > 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10444 | states->nbState = j; |
| 10445 | ctxt->states = states; |
| 10446 | ret = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10447 | } else if (j == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10448 | ctxt->state = states->tabState[0]; |
| 10449 | xmlRelaxNGFreeStates(ctxt, states); |
| 10450 | ret = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10451 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10452 | ret = -1; |
| 10453 | xmlRelaxNGFreeStates(ctxt, states); |
| 10454 | if (ctxt->states != NULL) { |
| 10455 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10456 | ctxt->states = NULL; |
| 10457 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10458 | } |
| 10459 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10460 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10461 | ctxt->state = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10462 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10463 | return (ret); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10464 | } |
| 10465 | |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10466 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10467 | * xmlRelaxNGValidateDocument: |
| 10468 | * @ctxt: a Relax-NG validation context |
| 10469 | * @doc: the document |
| 10470 | * |
| 10471 | * Validate the given document |
| 10472 | * |
| 10473 | * Returns 0 if the validation succeeded or an error code. |
| 10474 | */ |
| 10475 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10476 | xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) |
| 10477 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10478 | int ret; |
| 10479 | xmlRelaxNGPtr schema; |
| 10480 | xmlRelaxNGGrammarPtr grammar; |
| 10481 | xmlRelaxNGValidStatePtr state; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10482 | xmlNodePtr node; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10483 | |
| 10484 | if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10485 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10486 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10487 | ctxt->errNo = XML_RELAXNG_OK; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10488 | schema = ctxt->schema; |
| 10489 | grammar = schema->topgrammar; |
| 10490 | if (grammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10491 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 10492 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10493 | } |
| 10494 | state = xmlRelaxNGNewValidState(ctxt, NULL); |
| 10495 | ctxt->state = state; |
| 10496 | ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10497 | if ((ctxt->state != NULL) && (state->seq != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10498 | state = ctxt->state; |
| 10499 | node = state->seq; |
| 10500 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 10501 | if (node != NULL) { |
| 10502 | if (ret != -1) { |
| 10503 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
| 10504 | ret = -1; |
| 10505 | } |
| 10506 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10507 | } else if (ctxt->states != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10508 | int i; |
| 10509 | int tmp = -1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10510 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10511 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 10512 | state = ctxt->states->tabState[i]; |
| 10513 | node = state->seq; |
| 10514 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 10515 | if (node == NULL) |
| 10516 | tmp = 0; |
| 10517 | xmlRelaxNGFreeValidState(ctxt, state); |
| 10518 | } |
| 10519 | if (tmp == -1) { |
| 10520 | if (ret != -1) { |
| 10521 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
| 10522 | ret = -1; |
| 10523 | } |
| 10524 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10525 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 10526 | if (ctxt->state != NULL) { |
| 10527 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10528 | ctxt->state = NULL; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 10529 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10530 | if (ret != 0) |
| 10531 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 10532 | #ifdef DEBUG |
| 10533 | else if (ctxt->errNr != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10534 | ctxt->error(ctxt->userData, |
| 10535 | "%d Extra error messages left on stack !\n", |
| 10536 | ctxt->errNr); |
| 10537 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 10538 | } |
| 10539 | #endif |
Daniel Veillard | f54cd53 | 2004-02-25 11:52:31 +0000 | [diff] [blame] | 10540 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10541 | if (ctxt->idref == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10542 | xmlValidCtxt vctxt; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10543 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10544 | memset(&vctxt, 0, sizeof(xmlValidCtxt)); |
| 10545 | vctxt.valid = 1; |
| 10546 | vctxt.error = ctxt->error; |
| 10547 | vctxt.warning = ctxt->warning; |
| 10548 | vctxt.userData = ctxt->userData; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10549 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10550 | if (xmlValidateDocumentFinal(&vctxt, doc) != 1) |
| 10551 | ret = -1; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10552 | } |
Daniel Veillard | f54cd53 | 2004-02-25 11:52:31 +0000 | [diff] [blame] | 10553 | #endif /* LIBXML_VALID_ENABLED */ |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10554 | if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10555 | ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10556 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10557 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10558 | } |
| 10559 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10560 | /************************************************************************ |
| 10561 | * * |
| 10562 | * Validation interfaces * |
| 10563 | * * |
| 10564 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10565 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10566 | /** |
| 10567 | * xmlRelaxNGNewValidCtxt: |
| 10568 | * @schema: a precompiled XML RelaxNGs |
| 10569 | * |
| 10570 | * Create an XML RelaxNGs validation context based on the given schema |
| 10571 | * |
| 10572 | * Returns the validation context or NULL in case of error |
| 10573 | */ |
| 10574 | xmlRelaxNGValidCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10575 | xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema) |
| 10576 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10577 | xmlRelaxNGValidCtxtPtr ret; |
| 10578 | |
| 10579 | ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt)); |
| 10580 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10581 | xmlRngVErrMemory(NULL, "building context\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10582 | return (NULL); |
| 10583 | } |
| 10584 | memset(ret, 0, sizeof(xmlRelaxNGValidCtxt)); |
| 10585 | ret->schema = schema; |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 10586 | ret->error = xmlGenericError; |
| 10587 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 10588 | ret->errNr = 0; |
| 10589 | ret->errMax = 0; |
| 10590 | ret->err = NULL; |
| 10591 | ret->errTab = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10592 | ret->idref = schema->idref; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10593 | ret->states = NULL; |
| 10594 | ret->freeState = NULL; |
| 10595 | ret->freeStates = NULL; |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10596 | ret->errNo = XML_RELAXNG_OK; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10597 | return (ret); |
| 10598 | } |
| 10599 | |
| 10600 | /** |
| 10601 | * xmlRelaxNGFreeValidCtxt: |
| 10602 | * @ctxt: the schema validation context |
| 10603 | * |
| 10604 | * Free the resources associated to the schema validation context |
| 10605 | */ |
| 10606 | void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10607 | xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt) |
| 10608 | { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10609 | int k; |
| 10610 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10611 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10612 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10613 | if (ctxt->states != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10614 | xmlRelaxNGFreeStates(NULL, ctxt->states); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10615 | if (ctxt->freeState != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10616 | for (k = 0; k < ctxt->freeState->nbState; k++) { |
| 10617 | xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]); |
| 10618 | } |
| 10619 | xmlRelaxNGFreeStates(NULL, ctxt->freeState); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10620 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10621 | if (ctxt->freeStates != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10622 | for (k = 0; k < ctxt->freeStatesNr; k++) { |
| 10623 | xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]); |
| 10624 | } |
| 10625 | xmlFree(ctxt->freeStates); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10626 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 10627 | if (ctxt->errTab != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10628 | xmlFree(ctxt->errTab); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 10629 | if (ctxt->elemTab != NULL) { |
| 10630 | xmlRegExecCtxtPtr exec; |
| 10631 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10632 | exec = xmlRelaxNGElemPop(ctxt); |
| 10633 | while (exec != NULL) { |
| 10634 | xmlRegFreeExecCtxt(exec); |
| 10635 | exec = xmlRelaxNGElemPop(ctxt); |
| 10636 | } |
| 10637 | xmlFree(ctxt->elemTab); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 10638 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10639 | xmlFree(ctxt); |
| 10640 | } |
| 10641 | |
| 10642 | /** |
| 10643 | * xmlRelaxNGSetValidErrors: |
| 10644 | * @ctxt: a Relax-NG validation context |
| 10645 | * @err: the error function |
| 10646 | * @warn: the warning function |
| 10647 | * @ctx: the functions context |
| 10648 | * |
| 10649 | * Set the error and warning callback informations |
| 10650 | */ |
| 10651 | void |
| 10652 | xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10653 | xmlRelaxNGValidityErrorFunc err, |
| 10654 | xmlRelaxNGValidityWarningFunc warn, void *ctx) |
| 10655 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10656 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10657 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10658 | ctxt->error = err; |
| 10659 | ctxt->warning = warn; |
| 10660 | ctxt->userData = ctx; |
| 10661 | } |
| 10662 | |
| 10663 | /** |
Daniel Veillard | da0aa4c | 2005-07-13 23:07:49 +0000 | [diff] [blame] | 10664 | * xmlRelaxNGSetValidStructuredErrors: |
| 10665 | * @ctxt: a Relax-NG validation context |
| 10666 | * @serror: the structured error function |
| 10667 | * @ctx: the functions context |
| 10668 | * |
| 10669 | * Set the structured error callback |
| 10670 | */ |
| 10671 | void |
| 10672 | xmlRelaxNGSetValidStructuredErrors(xmlRelaxNGValidCtxtPtr ctxt, |
| 10673 | xmlStructuredErrorFunc serror, void *ctx) |
| 10674 | { |
| 10675 | if (ctxt == NULL) |
| 10676 | return; |
| 10677 | ctxt->serror = serror; |
| 10678 | ctxt->error = NULL; |
| 10679 | ctxt->warning = NULL; |
| 10680 | ctxt->userData = ctx; |
| 10681 | } |
| 10682 | |
| 10683 | /** |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 10684 | * xmlRelaxNGGetValidErrors: |
| 10685 | * @ctxt: a Relax-NG validation context |
| 10686 | * @err: the error function result |
| 10687 | * @warn: the warning function result |
| 10688 | * @ctx: the functions context result |
| 10689 | * |
| 10690 | * Get the error and warning callback informations |
| 10691 | * |
| 10692 | * Returns -1 in case of error and 0 otherwise |
| 10693 | */ |
| 10694 | int |
| 10695 | xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10696 | xmlRelaxNGValidityErrorFunc * err, |
| 10697 | xmlRelaxNGValidityWarningFunc * warn, void **ctx) |
| 10698 | { |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 10699 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10700 | return (-1); |
| 10701 | if (err != NULL) |
| 10702 | *err = ctxt->error; |
| 10703 | if (warn != NULL) |
| 10704 | *warn = ctxt->warning; |
| 10705 | if (ctx != NULL) |
| 10706 | *ctx = ctxt->userData; |
| 10707 | return (0); |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 10708 | } |
| 10709 | |
| 10710 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10711 | * xmlRelaxNGValidateDoc: |
| 10712 | * @ctxt: a Relax-NG validation context |
| 10713 | * @doc: a parsed document tree |
| 10714 | * |
| 10715 | * Validate a document tree in memory. |
| 10716 | * |
| 10717 | * Returns 0 if the document is valid, a positive error code |
| 10718 | * number otherwise and -1 in case of internal or API error. |
| 10719 | */ |
| 10720 | int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10721 | xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) |
| 10722 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10723 | int ret; |
| 10724 | |
| 10725 | if ((ctxt == NULL) || (doc == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10726 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10727 | |
| 10728 | ctxt->doc = doc; |
| 10729 | |
| 10730 | ret = xmlRelaxNGValidateDocument(ctxt, doc); |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 10731 | /* |
| 10732 | * TODO: build error codes |
| 10733 | */ |
| 10734 | if (ret == -1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10735 | return (1); |
| 10736 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10737 | } |
| 10738 | |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 10739 | #define bottom_relaxng |
| 10740 | #include "elfgcchack.h" |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10741 | #endif /* LIBXML_SCHEMAS_ENABLED */ |