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 | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 205 | xmlRelaxNGValidErr err; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 206 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 207 | xmlRelaxNGPtr schema; /* The schema in use */ |
| 208 | xmlRelaxNGGrammarPtr grammar; /* the current grammar */ |
| 209 | xmlRelaxNGGrammarPtr parentgrammar; /* the parent grammar */ |
| 210 | int flags; /* parser flags */ |
| 211 | int nbErrors; /* number of errors at parse time */ |
| 212 | int nbWarnings; /* number of warnings at parse time */ |
| 213 | const xmlChar *define; /* the current define scope */ |
| 214 | xmlRelaxNGDefinePtr def; /* the current define */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 215 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 216 | int nbInterleaves; |
| 217 | xmlHashTablePtr interleaves; /* keep track of all the interleaves */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 218 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 219 | xmlRelaxNGDocumentPtr documents; /* all the documents loaded */ |
| 220 | xmlRelaxNGIncludePtr includes; /* all the includes loaded */ |
| 221 | xmlChar *URL; |
| 222 | xmlDocPtr document; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 223 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 224 | int defNr; /* number of defines used */ |
| 225 | int defMax; /* number of defines aloocated */ |
| 226 | xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */ |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 227 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 228 | const char *buffer; |
| 229 | int size; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 230 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 231 | /* the document stack */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 232 | xmlRelaxNGDocumentPtr doc; /* Current parsed external ref */ |
| 233 | int docNr; /* Depth of the parsing stack */ |
| 234 | int docMax; /* Max depth of the parsing stack */ |
| 235 | xmlRelaxNGDocumentPtr *docTab; /* array of docs */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 236 | |
| 237 | /* the include stack */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 238 | xmlRelaxNGIncludePtr inc; /* Current parsed include */ |
| 239 | int incNr; /* Depth of the include parsing stack */ |
| 240 | int incMax; /* Max depth of the parsing stack */ |
| 241 | xmlRelaxNGIncludePtr *incTab; /* array of incs */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 242 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 243 | int idref; /* requires idref checking */ |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 244 | |
| 245 | /* used to compile content models */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 246 | xmlAutomataPtr am; /* the automata */ |
| 247 | xmlAutomataStatePtr state; /* used to build the automata */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 248 | }; |
| 249 | |
| 250 | #define FLAGS_IGNORABLE 1 |
| 251 | #define FLAGS_NEGATIVE 2 |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 252 | #define FLAGS_MIXED_CONTENT 4 |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 253 | |
| 254 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 255 | * xmlRelaxNGInterleaveGroup: |
| 256 | * |
| 257 | * A RelaxNGs partition set associated to lists of definitions |
| 258 | */ |
| 259 | typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup; |
| 260 | typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr; |
| 261 | struct _xmlRelaxNGInterleaveGroup { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 262 | xmlRelaxNGDefinePtr rule; /* the rule to satisfy */ |
| 263 | xmlRelaxNGDefinePtr *defs; /* the array of element definitions */ |
| 264 | xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 265 | }; |
| 266 | |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 267 | #define IS_DETERMINIST 1 |
| 268 | #define IS_NEEDCHECK 2 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 269 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 270 | /** |
| 271 | * xmlRelaxNGPartitions: |
| 272 | * |
| 273 | * A RelaxNGs partition associated to an interleave group |
| 274 | */ |
| 275 | typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition; |
| 276 | typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr; |
| 277 | struct _xmlRelaxNGPartition { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 278 | int nbgroups; /* number of groups in the partitions */ |
| 279 | xmlHashTablePtr triage; /* hash table used to direct nodes to the |
| 280 | * right group when possible */ |
| 281 | int flags; /* determinist ? */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 282 | xmlRelaxNGInterleaveGroupPtr *groups; |
| 283 | }; |
| 284 | |
| 285 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 286 | * xmlRelaxNGValidState: |
| 287 | * |
| 288 | * A RelaxNGs validation state |
| 289 | */ |
| 290 | #define MAX_ATTR 20 |
| 291 | typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState; |
| 292 | typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr; |
| 293 | struct _xmlRelaxNGValidState { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 294 | xmlNodePtr node; /* the current node */ |
| 295 | xmlNodePtr seq; /* the sequence of children left to validate */ |
| 296 | int nbAttrs; /* the number of attributes */ |
| 297 | int maxAttrs; /* the size of attrs */ |
| 298 | int nbAttrLeft; /* the number of attributes left to validate */ |
| 299 | xmlChar *value; /* the value when operating on string */ |
| 300 | xmlChar *endvalue; /* the end value when operating on string */ |
| 301 | xmlAttrPtr *attrs; /* the array of attributes */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 302 | }; |
| 303 | |
| 304 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 305 | * xmlRelaxNGStates: |
| 306 | * |
| 307 | * A RelaxNGs container for validation state |
| 308 | */ |
| 309 | typedef struct _xmlRelaxNGStates xmlRelaxNGStates; |
| 310 | typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr; |
| 311 | struct _xmlRelaxNGStates { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 312 | int nbState; /* the number of states */ |
| 313 | int maxState; /* the size of the array */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 314 | xmlRelaxNGValidStatePtr *tabState; |
| 315 | }; |
| 316 | |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 317 | #define ERROR_IS_DUP 1 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 318 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 319 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 320 | * xmlRelaxNGValidError: |
| 321 | * |
| 322 | * A RelaxNGs validation error |
| 323 | */ |
| 324 | typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError; |
| 325 | typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr; |
| 326 | struct _xmlRelaxNGValidError { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 327 | xmlRelaxNGValidErr err; /* the error number */ |
| 328 | int flags; /* flags */ |
| 329 | xmlNodePtr node; /* the current node */ |
| 330 | xmlNodePtr seq; /* the current child */ |
| 331 | const xmlChar *arg1; /* first arg */ |
| 332 | const xmlChar *arg2; /* second arg */ |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 333 | }; |
| 334 | |
| 335 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 336 | * xmlRelaxNGValidCtxt: |
| 337 | * |
| 338 | * A RelaxNGs validation context |
| 339 | */ |
| 340 | |
| 341 | struct _xmlRelaxNGValidCtxt { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 342 | void *userData; /* user specific data block */ |
| 343 | xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */ |
| 344 | xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */ |
| 345 | int nbErrors; /* number of errors in validation */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 346 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 347 | xmlRelaxNGPtr schema; /* The schema in use */ |
| 348 | xmlDocPtr doc; /* the document being validated */ |
| 349 | int flags; /* validation flags */ |
| 350 | int depth; /* validation depth */ |
| 351 | int idref; /* requires idref checking */ |
| 352 | int errNo; /* the first error found */ |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 353 | |
| 354 | /* |
| 355 | * Errors accumulated in branches may have to be stacked to be |
| 356 | * provided back when it's sure they affect validation. |
| 357 | */ |
| 358 | xmlRelaxNGValidErrorPtr err; /* Last error */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 359 | int errNr; /* Depth of the error stack */ |
| 360 | int errMax; /* Max depth of the error stack */ |
| 361 | xmlRelaxNGValidErrorPtr errTab; /* stack of errors */ |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 362 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 363 | xmlRelaxNGValidStatePtr state; /* the current validation state */ |
| 364 | xmlRelaxNGStatesPtr states; /* the accumulated state list */ |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 365 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 366 | xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */ |
| 367 | int freeStatesNr; |
| 368 | int freeStatesMax; |
| 369 | xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */ |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 370 | |
| 371 | /* |
| 372 | * This is used for "progressive" validation |
| 373 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 374 | xmlRegExecCtxtPtr elem; /* the current element regexp */ |
| 375 | int elemNr; /* the number of element validated */ |
| 376 | int elemMax; /* the max depth of elements */ |
| 377 | xmlRegExecCtxtPtr *elemTab; /* the stack of regexp runtime */ |
| 378 | int pstate; /* progressive state */ |
| 379 | xmlNodePtr pnode; /* the current node */ |
| 380 | xmlRelaxNGDefinePtr pdef; /* the non-streamable definition */ |
| 381 | int perr; /* signal error in content model |
| 382 | * outside the regexp */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 383 | }; |
| 384 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 385 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 386 | * xmlRelaxNGInclude: |
| 387 | * |
| 388 | * Structure associated to a RelaxNGs document element |
| 389 | */ |
| 390 | struct _xmlRelaxNGInclude { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 391 | xmlRelaxNGIncludePtr next; /* keep a chain of includes */ |
| 392 | xmlChar *href; /* the normalized href value */ |
| 393 | xmlDocPtr doc; /* the associated XML document */ |
| 394 | xmlRelaxNGDefinePtr content; /* the definitions */ |
| 395 | xmlRelaxNGPtr schema; /* the schema */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 396 | }; |
| 397 | |
| 398 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 399 | * xmlRelaxNGDocument: |
| 400 | * |
| 401 | * Structure associated to a RelaxNGs document element |
| 402 | */ |
| 403 | struct _xmlRelaxNGDocument { |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 404 | xmlRelaxNGDocumentPtr next; /* keep a chain of documents */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 405 | xmlChar *href; /* the normalized href value */ |
| 406 | xmlDocPtr doc; /* the associated XML document */ |
| 407 | xmlRelaxNGDefinePtr content; /* the definitions */ |
| 408 | xmlRelaxNGPtr schema; /* the schema */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 409 | }; |
| 410 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 411 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 412 | /************************************************************************ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 413 | * * |
| 414 | * Some factorized error routines * |
| 415 | * * |
| 416 | ************************************************************************/ |
| 417 | |
| 418 | /** |
| 419 | * xmlRngPErrMemory: |
| 420 | * @ctxt: an Relax-NG parser context |
| 421 | * @extra: extra informations |
| 422 | * |
| 423 | * Handle a redefinition of attribute error |
| 424 | */ |
| 425 | static void |
| 426 | xmlRngPErrMemory(xmlRelaxNGParserCtxtPtr ctxt, const char *extra) |
| 427 | { |
| 428 | xmlGenericErrorFunc channel = NULL; |
| 429 | void *data = NULL; |
| 430 | |
| 431 | if (ctxt != NULL) { |
| 432 | channel = ctxt->error; |
| 433 | data = ctxt->userData; |
| 434 | ctxt->nbErrors++; |
| 435 | } |
| 436 | if (extra) |
| 437 | __xmlRaiseError(channel, data, |
| 438 | NULL, NULL, XML_FROM_RELAXNGP, |
| 439 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra, |
| 440 | NULL, NULL, 0, 0, |
| 441 | "Memory allocation failed : %s\n", extra); |
| 442 | else |
| 443 | __xmlRaiseError(channel, data, |
| 444 | NULL, NULL, XML_FROM_RELAXNGP, |
| 445 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL, |
| 446 | NULL, NULL, 0, 0, "Memory allocation failed\n"); |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * xmlRngVErrMemory: |
| 451 | * @ctxt: a Relax-NG validation context |
| 452 | * @extra: extra informations |
| 453 | * |
| 454 | * Handle a redefinition of attribute error |
| 455 | */ |
| 456 | static void |
| 457 | xmlRngVErrMemory(xmlRelaxNGValidCtxtPtr ctxt, const char *extra) |
| 458 | { |
| 459 | xmlGenericErrorFunc channel = NULL; |
| 460 | void *data = NULL; |
| 461 | |
| 462 | if (ctxt != NULL) { |
| 463 | channel = ctxt->error; |
| 464 | data = ctxt->userData; |
| 465 | ctxt->nbErrors++; |
| 466 | } |
| 467 | if (extra) |
| 468 | __xmlRaiseError(channel, data, |
| 469 | NULL, NULL, XML_FROM_RELAXNGV, |
| 470 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra, |
| 471 | NULL, NULL, 0, 0, |
| 472 | "Memory allocation failed : %s\n", extra); |
| 473 | else |
| 474 | __xmlRaiseError(channel, data, |
| 475 | NULL, NULL, XML_FROM_RELAXNGV, |
| 476 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL, |
| 477 | NULL, NULL, 0, 0, "Memory allocation failed\n"); |
| 478 | } |
| 479 | |
| 480 | /** |
| 481 | * xmlRngPErr: |
| 482 | * @ctxt: a Relax-NG parser context |
| 483 | * @node: the node raising the error |
| 484 | * @error: the error code |
| 485 | * @msg: message |
| 486 | * @str1: extra info |
| 487 | * @str2: extra info |
| 488 | * |
| 489 | * Handle a Relax NG Parsing error |
| 490 | */ |
| 491 | static void |
| 492 | xmlRngPErr(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, int error, |
| 493 | const char *msg, const xmlChar * str1, const xmlChar * str2) |
| 494 | { |
| 495 | xmlGenericErrorFunc channel = NULL; |
| 496 | void *data = NULL; |
| 497 | |
| 498 | if (ctxt != NULL) { |
| 499 | channel = ctxt->error; |
| 500 | data = ctxt->userData; |
| 501 | ctxt->nbErrors++; |
| 502 | } |
| 503 | __xmlRaiseError(channel, data, |
| 504 | NULL, node, XML_FROM_RELAXNGP, |
| 505 | error, XML_ERR_ERROR, NULL, 0, |
| 506 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
| 507 | msg, str1, str2); |
| 508 | } |
| 509 | |
| 510 | /** |
| 511 | * xmlRngVErr: |
| 512 | * @ctxt: a Relax-NG validation context |
| 513 | * @node: the node raising the error |
| 514 | * @error: the error code |
| 515 | * @msg: message |
| 516 | * @str1: extra info |
| 517 | * @str2: extra info |
| 518 | * |
| 519 | * Handle a Relax NG Validation error |
| 520 | */ |
| 521 | static void |
| 522 | xmlRngVErr(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node, int error, |
| 523 | const char *msg, const xmlChar * str1, const xmlChar * str2) |
| 524 | { |
| 525 | xmlGenericErrorFunc channel = NULL; |
| 526 | void *data = NULL; |
| 527 | |
| 528 | if (ctxt != NULL) { |
| 529 | channel = ctxt->error; |
| 530 | data = ctxt->userData; |
| 531 | ctxt->nbErrors++; |
| 532 | } |
| 533 | __xmlRaiseError(channel, data, |
| 534 | NULL, node, XML_FROM_RELAXNGV, |
| 535 | error, XML_ERR_ERROR, NULL, 0, |
| 536 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
| 537 | msg, str1, str2); |
| 538 | } |
| 539 | |
| 540 | /************************************************************************ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 541 | * * |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 542 | * Preliminary type checking interfaces * |
| 543 | * * |
| 544 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 545 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 546 | /** |
| 547 | * xmlRelaxNGTypeHave: |
| 548 | * @data: data needed for the library |
| 549 | * @type: the type name |
| 550 | * @value: the value to check |
| 551 | * |
| 552 | * Function provided by a type library to check if a type is exported |
| 553 | * |
| 554 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 555 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 556 | typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar * type); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 557 | |
| 558 | /** |
| 559 | * xmlRelaxNGTypeCheck: |
| 560 | * @data: data needed for the library |
| 561 | * @type: the type name |
| 562 | * @value: the value to check |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 563 | * @result: place to store the result if needed |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 564 | * |
| 565 | * Function provided by a type library to check if a value match a type |
| 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 (*xmlRelaxNGTypeCheck) (void *data, const xmlChar * type, |
| 570 | const xmlChar * value, void **result, |
| 571 | xmlNodePtr node); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 572 | |
| 573 | /** |
| 574 | * xmlRelaxNGFacetCheck: |
| 575 | * @data: data needed for the library |
| 576 | * @type: the type name |
| 577 | * @facet: the facet name |
| 578 | * @val: the facet value |
| 579 | * @strval: the string value |
| 580 | * @value: the value to check |
| 581 | * |
| 582 | * Function provided by a type library to check a value facet |
| 583 | * |
| 584 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 585 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 586 | typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar * type, |
| 587 | const xmlChar * facet, |
| 588 | const xmlChar * val, |
| 589 | const xmlChar * strval, void *value); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 590 | |
| 591 | /** |
| 592 | * xmlRelaxNGTypeFree: |
| 593 | * @data: data needed for the library |
| 594 | * @result: the value to free |
| 595 | * |
| 596 | * Function provided by a type library to free a returned result |
| 597 | */ |
| 598 | typedef void (*xmlRelaxNGTypeFree) (void *data, void *result); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 599 | |
| 600 | /** |
| 601 | * xmlRelaxNGTypeCompare: |
| 602 | * @data: data needed for the library |
| 603 | * @type: the type name |
| 604 | * @value1: the first value |
| 605 | * @value2: the second value |
| 606 | * |
| 607 | * Function provided by a type library to compare two values accordingly |
| 608 | * to a type. |
| 609 | * |
| 610 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 611 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 612 | typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar * type, |
| 613 | const xmlChar * value1, |
| 614 | xmlNodePtr ctxt1, |
| 615 | void *comp1, |
| 616 | const xmlChar * value2, |
| 617 | xmlNodePtr ctxt2); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 618 | typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary; |
| 619 | typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr; |
| 620 | struct _xmlRelaxNGTypeLibrary { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 621 | const xmlChar *namespace; /* the datatypeLibrary value */ |
| 622 | void *data; /* data needed for the library */ |
| 623 | xmlRelaxNGTypeHave have; /* the export function */ |
| 624 | xmlRelaxNGTypeCheck check; /* the checking function */ |
| 625 | xmlRelaxNGTypeCompare comp; /* the compare function */ |
| 626 | xmlRelaxNGFacetCheck facet; /* the facet check function */ |
| 627 | xmlRelaxNGTypeFree freef; /* the freeing function */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 628 | }; |
| 629 | |
| 630 | /************************************************************************ |
| 631 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 632 | * Allocation functions * |
| 633 | * * |
| 634 | ************************************************************************/ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 635 | static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar); |
| 636 | static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 637 | static void xmlRelaxNGNormExtSpace(xmlChar * value); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 638 | static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 639 | static int xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt |
| 640 | ATTRIBUTE_UNUSED, |
| 641 | xmlRelaxNGValidStatePtr state1, |
| 642 | xmlRelaxNGValidStatePtr state2); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 643 | static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 644 | xmlRelaxNGValidStatePtr state); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 645 | |
| 646 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 647 | * xmlRelaxNGFreeDocument: |
| 648 | * @docu: a document structure |
| 649 | * |
| 650 | * Deallocate a RelaxNG document structure. |
| 651 | */ |
| 652 | static void |
| 653 | xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu) |
| 654 | { |
| 655 | if (docu == NULL) |
| 656 | return; |
| 657 | |
| 658 | if (docu->href != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 659 | xmlFree(docu->href); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 660 | if (docu->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 661 | xmlFreeDoc(docu->doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 662 | if (docu->schema != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 663 | xmlRelaxNGFreeInnerSchema(docu->schema); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 664 | xmlFree(docu); |
| 665 | } |
| 666 | |
| 667 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 668 | * xmlRelaxNGFreeDocumentList: |
| 669 | * @docu: a list of document structure |
| 670 | * |
| 671 | * Deallocate a RelaxNG document structures. |
| 672 | */ |
| 673 | static void |
| 674 | xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu) |
| 675 | { |
| 676 | xmlRelaxNGDocumentPtr next; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 677 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 678 | while (docu != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 679 | next = docu->next; |
| 680 | xmlRelaxNGFreeDocument(docu); |
| 681 | docu = next; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 682 | } |
| 683 | } |
| 684 | |
| 685 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 686 | * xmlRelaxNGFreeInclude: |
| 687 | * @incl: a include structure |
| 688 | * |
| 689 | * Deallocate a RelaxNG include structure. |
| 690 | */ |
| 691 | static void |
| 692 | xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl) |
| 693 | { |
| 694 | if (incl == NULL) |
| 695 | return; |
| 696 | |
| 697 | if (incl->href != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 698 | xmlFree(incl->href); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 699 | if (incl->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 700 | xmlFreeDoc(incl->doc); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 701 | if (incl->schema != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 702 | xmlRelaxNGFree(incl->schema); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 703 | xmlFree(incl); |
| 704 | } |
| 705 | |
| 706 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 707 | * xmlRelaxNGFreeIncludeList: |
| 708 | * @incl: a include structure list |
| 709 | * |
| 710 | * Deallocate a RelaxNG include structure. |
| 711 | */ |
| 712 | static void |
| 713 | xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl) |
| 714 | { |
| 715 | xmlRelaxNGIncludePtr next; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 716 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 717 | while (incl != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 718 | next = incl->next; |
| 719 | xmlRelaxNGFreeInclude(incl); |
| 720 | incl = next; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 721 | } |
| 722 | } |
| 723 | |
| 724 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 725 | * xmlRelaxNGNewRelaxNG: |
| 726 | * @ctxt: a Relax-NG validation context (optional) |
| 727 | * |
| 728 | * Allocate a new RelaxNG structure. |
| 729 | * |
| 730 | * Returns the newly allocated structure or NULL in case or error |
| 731 | */ |
| 732 | static xmlRelaxNGPtr |
| 733 | xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt) |
| 734 | { |
| 735 | xmlRelaxNGPtr ret; |
| 736 | |
| 737 | ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG)); |
| 738 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 739 | xmlRngPErrMemory(ctxt, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 740 | return (NULL); |
| 741 | } |
| 742 | memset(ret, 0, sizeof(xmlRelaxNG)); |
| 743 | |
| 744 | return (ret); |
| 745 | } |
| 746 | |
| 747 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 748 | * xmlRelaxNGFreeInnerSchema: |
| 749 | * @schema: a schema structure |
| 750 | * |
| 751 | * Deallocate a RelaxNG schema structure. |
| 752 | */ |
| 753 | static void |
| 754 | xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema) |
| 755 | { |
| 756 | if (schema == NULL) |
| 757 | return; |
| 758 | |
| 759 | if (schema->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 760 | xmlFreeDoc(schema->doc); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 761 | if (schema->defTab != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 762 | int i; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 763 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 764 | for (i = 0; i < schema->defNr; i++) |
| 765 | xmlRelaxNGFreeDefine(schema->defTab[i]); |
| 766 | xmlFree(schema->defTab); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | xmlFree(schema); |
| 770 | } |
| 771 | |
| 772 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 773 | * xmlRelaxNGFree: |
| 774 | * @schema: a schema structure |
| 775 | * |
| 776 | * Deallocate a RelaxNG structure. |
| 777 | */ |
| 778 | void |
| 779 | xmlRelaxNGFree(xmlRelaxNGPtr schema) |
| 780 | { |
| 781 | if (schema == NULL) |
| 782 | return; |
| 783 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 784 | if (schema->topgrammar != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 785 | xmlRelaxNGFreeGrammar(schema->topgrammar); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 786 | if (schema->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 787 | xmlFreeDoc(schema->doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 788 | if (schema->documents != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 789 | xmlRelaxNGFreeDocumentList(schema->documents); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 790 | if (schema->includes != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 791 | xmlRelaxNGFreeIncludeList(schema->includes); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 792 | if (schema->defTab != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 793 | int i; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 794 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 795 | for (i = 0; i < schema->defNr; i++) |
| 796 | xmlRelaxNGFreeDefine(schema->defTab[i]); |
| 797 | xmlFree(schema->defTab); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 798 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 799 | |
| 800 | xmlFree(schema); |
| 801 | } |
| 802 | |
| 803 | /** |
| 804 | * xmlRelaxNGNewGrammar: |
| 805 | * @ctxt: a Relax-NG validation context (optional) |
| 806 | * |
| 807 | * Allocate a new RelaxNG grammar. |
| 808 | * |
| 809 | * Returns the newly allocated structure or NULL in case or error |
| 810 | */ |
| 811 | static xmlRelaxNGGrammarPtr |
| 812 | xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt) |
| 813 | { |
| 814 | xmlRelaxNGGrammarPtr ret; |
| 815 | |
| 816 | ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar)); |
| 817 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 818 | xmlRngPErrMemory(ctxt, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 819 | return (NULL); |
| 820 | } |
| 821 | memset(ret, 0, sizeof(xmlRelaxNGGrammar)); |
| 822 | |
| 823 | return (ret); |
| 824 | } |
| 825 | |
| 826 | /** |
| 827 | * xmlRelaxNGFreeGrammar: |
| 828 | * @grammar: a grammar structure |
| 829 | * |
| 830 | * Deallocate a RelaxNG grammar structure. |
| 831 | */ |
| 832 | static void |
| 833 | xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar) |
| 834 | { |
| 835 | if (grammar == NULL) |
| 836 | return; |
| 837 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 838 | if (grammar->children != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 839 | xmlRelaxNGFreeGrammar(grammar->children); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 840 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 841 | if (grammar->next != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 842 | xmlRelaxNGFreeGrammar(grammar->next); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 843 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 844 | if (grammar->refs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 845 | xmlHashFree(grammar->refs, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 846 | } |
| 847 | if (grammar->defs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 848 | xmlHashFree(grammar->defs, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | xmlFree(grammar); |
| 852 | } |
| 853 | |
| 854 | /** |
| 855 | * xmlRelaxNGNewDefine: |
| 856 | * @ctxt: a Relax-NG validation context |
| 857 | * @node: the node in the input document. |
| 858 | * |
| 859 | * Allocate a new RelaxNG define. |
| 860 | * |
| 861 | * Returns the newly allocated structure or NULL in case or error |
| 862 | */ |
| 863 | static xmlRelaxNGDefinePtr |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 864 | xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 865 | { |
| 866 | xmlRelaxNGDefinePtr ret; |
| 867 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 868 | if (ctxt->defMax == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 869 | ctxt->defMax = 16; |
| 870 | ctxt->defNr = 0; |
| 871 | ctxt->defTab = (xmlRelaxNGDefinePtr *) |
| 872 | xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr)); |
| 873 | if (ctxt->defTab == NULL) { |
| 874 | xmlRngPErrMemory(ctxt, "allocating define\n"); |
| 875 | return (NULL); |
| 876 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 877 | } else if (ctxt->defMax <= ctxt->defNr) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 878 | xmlRelaxNGDefinePtr *tmp; |
| 879 | |
| 880 | ctxt->defMax *= 2; |
| 881 | tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab, |
| 882 | ctxt->defMax * |
| 883 | sizeof |
| 884 | (xmlRelaxNGDefinePtr)); |
| 885 | if (tmp == NULL) { |
| 886 | xmlRngPErrMemory(ctxt, "allocating define\n"); |
| 887 | return (NULL); |
| 888 | } |
| 889 | ctxt->defTab = tmp; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 890 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 891 | ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine)); |
| 892 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 893 | xmlRngPErrMemory(ctxt, "allocating define\n"); |
| 894 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 895 | } |
| 896 | memset(ret, 0, sizeof(xmlRelaxNGDefine)); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 897 | ctxt->defTab[ctxt->defNr++] = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 898 | ret->node = node; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 899 | ret->depth = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 900 | return (ret); |
| 901 | } |
| 902 | |
| 903 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 904 | * xmlRelaxNGFreePartition: |
| 905 | * @partitions: a partition set structure |
| 906 | * |
| 907 | * Deallocate RelaxNG partition set structures. |
| 908 | */ |
| 909 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 910 | xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions) |
| 911 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 912 | xmlRelaxNGInterleaveGroupPtr group; |
| 913 | int j; |
| 914 | |
| 915 | if (partitions != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 916 | if (partitions->groups != NULL) { |
| 917 | for (j = 0; j < partitions->nbgroups; j++) { |
| 918 | group = partitions->groups[j]; |
| 919 | if (group != NULL) { |
| 920 | if (group->defs != NULL) |
| 921 | xmlFree(group->defs); |
| 922 | if (group->attrs != NULL) |
| 923 | xmlFree(group->attrs); |
| 924 | xmlFree(group); |
| 925 | } |
| 926 | } |
| 927 | xmlFree(partitions->groups); |
| 928 | } |
| 929 | if (partitions->triage != NULL) { |
| 930 | xmlHashFree(partitions->triage, NULL); |
| 931 | } |
| 932 | xmlFree(partitions); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 933 | } |
| 934 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 935 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 936 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 937 | * xmlRelaxNGFreeDefine: |
| 938 | * @define: a define structure |
| 939 | * |
| 940 | * Deallocate a RelaxNG define structure. |
| 941 | */ |
| 942 | static void |
| 943 | xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define) |
| 944 | { |
| 945 | if (define == NULL) |
| 946 | return; |
| 947 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 948 | if ((define->type == XML_RELAXNG_VALUE) && (define->attrs != NULL)) { |
| 949 | xmlRelaxNGTypeLibraryPtr lib; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 950 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 951 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
| 952 | if ((lib != NULL) && (lib->freef != NULL)) |
| 953 | lib->freef(lib->data, (void *) define->attrs); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 954 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 955 | if ((define->data != NULL) && (define->type == XML_RELAXNG_INTERLEAVE)) |
| 956 | xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data); |
| 957 | if ((define->data != NULL) && (define->type == XML_RELAXNG_CHOICE)) |
| 958 | xmlHashFree((xmlHashTablePtr) define->data, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 959 | if (define->name != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 960 | xmlFree(define->name); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 961 | if (define->ns != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 962 | xmlFree(define->ns); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 963 | if (define->value != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 964 | xmlFree(define->value); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 965 | if (define->contModel != NULL) |
| 966 | xmlRegFreeRegexp(define->contModel); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 967 | xmlFree(define); |
| 968 | } |
| 969 | |
| 970 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 971 | * xmlRelaxNGNewStates: |
| 972 | * @ctxt: a Relax-NG validation context |
| 973 | * @size: the default size for the container |
| 974 | * |
| 975 | * Allocate a new RelaxNG validation state container |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 976 | * |
| 977 | * Returns the newly allocated structure or NULL in case or error |
| 978 | */ |
| 979 | static xmlRelaxNGStatesPtr |
| 980 | xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size) |
| 981 | { |
| 982 | xmlRelaxNGStatesPtr ret; |
| 983 | |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 984 | if ((ctxt != NULL) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 985 | (ctxt->freeState != NULL) && (ctxt->freeStatesNr > 0)) { |
| 986 | ctxt->freeStatesNr--; |
| 987 | ret = ctxt->freeStates[ctxt->freeStatesNr]; |
| 988 | ret->nbState = 0; |
| 989 | return (ret); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 990 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 991 | if (size < 16) |
| 992 | size = 16; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 993 | |
| 994 | ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) + |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 995 | (size - |
| 996 | 1) * |
| 997 | sizeof(xmlRelaxNGValidStatePtr)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 998 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 999 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1000 | return (NULL); |
| 1001 | } |
| 1002 | ret->nbState = 0; |
| 1003 | ret->maxState = size; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1004 | ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc((size) * |
| 1005 | sizeof |
| 1006 | (xmlRelaxNGValidStatePtr)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1007 | if (ret->tabState == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1008 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1009 | xmlFree(ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1010 | return (NULL); |
| 1011 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1012 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | /** |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1016 | * xmlRelaxNGAddStateUniq: |
| 1017 | * @ctxt: a Relax-NG validation context |
| 1018 | * @states: the states container |
| 1019 | * @state: the validation state |
| 1020 | * |
| 1021 | * Add a RelaxNG validation state to the container without checking |
| 1022 | * for unicity. |
| 1023 | * |
| 1024 | * Return 1 in case of success and 0 if this is a duplicate and -1 on error |
| 1025 | */ |
| 1026 | static int |
| 1027 | xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1028 | xmlRelaxNGStatesPtr states, |
| 1029 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1030 | { |
| 1031 | if (state == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1032 | return (-1); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1033 | } |
| 1034 | if (states->nbState >= states->maxState) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1035 | xmlRelaxNGValidStatePtr *tmp; |
| 1036 | int size; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1037 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1038 | size = states->maxState * 2; |
| 1039 | tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState, |
| 1040 | (size) * |
| 1041 | sizeof |
| 1042 | (xmlRelaxNGValidStatePtr)); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1043 | if (tmp == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1044 | xmlRngVErrMemory(ctxt, "adding states\n"); |
| 1045 | return (-1); |
| 1046 | } |
| 1047 | states->tabState = tmp; |
| 1048 | states->maxState = size; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1049 | } |
| 1050 | states->tabState[states->nbState++] = state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1051 | return (1); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1055 | * xmlRelaxNGAddState: |
| 1056 | * @ctxt: a Relax-NG validation context |
| 1057 | * @states: the states container |
| 1058 | * @state: the validation state |
| 1059 | * |
| 1060 | * Add a RelaxNG validation state to the container |
| 1061 | * |
| 1062 | * Return 1 in case of success and 0 if this is a duplicate and -1 on error |
| 1063 | */ |
| 1064 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1065 | xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt, |
| 1066 | xmlRelaxNGStatesPtr states, |
| 1067 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1068 | { |
| 1069 | int i; |
| 1070 | |
| 1071 | if (state == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1072 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1073 | } |
| 1074 | if (states->nbState >= states->maxState) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1075 | xmlRelaxNGValidStatePtr *tmp; |
| 1076 | int size; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1077 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1078 | size = states->maxState * 2; |
| 1079 | tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState, |
| 1080 | (size) * |
| 1081 | sizeof |
| 1082 | (xmlRelaxNGValidStatePtr)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1083 | if (tmp == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1084 | xmlRngVErrMemory(ctxt, "adding states\n"); |
| 1085 | return (-1); |
| 1086 | } |
| 1087 | states->tabState = tmp; |
| 1088 | states->maxState = size; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1089 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1090 | for (i = 0; i < states->nbState; i++) { |
| 1091 | if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) { |
| 1092 | xmlRelaxNGFreeValidState(ctxt, state); |
| 1093 | return (0); |
| 1094 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1095 | } |
| 1096 | states->tabState[states->nbState++] = state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1097 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
| 1100 | /** |
| 1101 | * xmlRelaxNGFreeStates: |
| 1102 | * @ctxt: a Relax-NG validation context |
| 1103 | * @states: teh container |
| 1104 | * |
| 1105 | * Free a RelaxNG validation state container |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1106 | */ |
| 1107 | static void |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1108 | xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1109 | xmlRelaxNGStatesPtr states) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1110 | { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1111 | if (states == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1112 | return; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1113 | if ((ctxt != NULL) && (ctxt->freeStates == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1114 | ctxt->freeStatesMax = 40; |
| 1115 | ctxt->freeStatesNr = 0; |
| 1116 | ctxt->freeStates = (xmlRelaxNGStatesPtr *) |
| 1117 | xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr)); |
| 1118 | if (ctxt->freeStates == NULL) { |
| 1119 | xmlRngVErrMemory(ctxt, "storing states\n"); |
| 1120 | } |
| 1121 | } else if ((ctxt != NULL) |
| 1122 | && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) { |
| 1123 | xmlRelaxNGStatesPtr *tmp; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1124 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1125 | tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates, |
| 1126 | 2 * ctxt->freeStatesMax * |
| 1127 | sizeof |
| 1128 | (xmlRelaxNGStatesPtr)); |
| 1129 | if (tmp == NULL) { |
| 1130 | xmlRngVErrMemory(ctxt, "storing states\n"); |
| 1131 | xmlFree(states->tabState); |
| 1132 | xmlFree(states); |
| 1133 | return; |
| 1134 | } |
| 1135 | ctxt->freeStates = tmp; |
| 1136 | ctxt->freeStatesMax *= 2; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1137 | } |
| 1138 | if ((ctxt == NULL) || (ctxt->freeState == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1139 | xmlFree(states->tabState); |
| 1140 | xmlFree(states); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1141 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1142 | ctxt->freeStates[ctxt->freeStatesNr++] = states; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1147 | * xmlRelaxNGNewValidState: |
| 1148 | * @ctxt: a Relax-NG validation context |
| 1149 | * @node: the current node or NULL for the document |
| 1150 | * |
| 1151 | * Allocate a new RelaxNG validation state |
| 1152 | * |
| 1153 | * Returns the newly allocated structure or NULL in case or error |
| 1154 | */ |
| 1155 | static xmlRelaxNGValidStatePtr |
| 1156 | xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node) |
| 1157 | { |
| 1158 | xmlRelaxNGValidStatePtr ret; |
| 1159 | xmlAttrPtr attr; |
| 1160 | xmlAttrPtr attrs[MAX_ATTR]; |
| 1161 | int nbAttrs = 0; |
| 1162 | xmlNodePtr root = NULL; |
| 1163 | |
| 1164 | if (node == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1165 | root = xmlDocGetRootElement(ctxt->doc); |
| 1166 | if (root == NULL) |
| 1167 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1168 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1169 | attr = node->properties; |
| 1170 | while (attr != NULL) { |
| 1171 | if (nbAttrs < MAX_ATTR) |
| 1172 | attrs[nbAttrs++] = attr; |
| 1173 | else |
| 1174 | nbAttrs++; |
| 1175 | attr = attr->next; |
| 1176 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1177 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1178 | if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) { |
| 1179 | ctxt->freeState->nbState--; |
| 1180 | ret = ctxt->freeState->tabState[ctxt->freeState->nbState]; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1181 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1182 | ret = |
| 1183 | (xmlRelaxNGValidStatePtr) |
| 1184 | xmlMalloc(sizeof(xmlRelaxNGValidState)); |
| 1185 | if (ret == NULL) { |
| 1186 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1187 | return (NULL); |
| 1188 | } |
| 1189 | memset(ret, 0, sizeof(xmlRelaxNGValidState)); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1190 | } |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 1191 | ret->value = NULL; |
| 1192 | ret->endvalue = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1193 | if (node == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1194 | ret->node = (xmlNodePtr) ctxt->doc; |
| 1195 | ret->seq = root; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1196 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1197 | ret->node = node; |
| 1198 | ret->seq = node->children; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1199 | } |
| 1200 | ret->nbAttrs = 0; |
| 1201 | if (nbAttrs > 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1202 | if (ret->attrs == NULL) { |
| 1203 | if (nbAttrs < 4) |
| 1204 | ret->maxAttrs = 4; |
| 1205 | else |
| 1206 | ret->maxAttrs = nbAttrs; |
| 1207 | ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs * |
| 1208 | sizeof(xmlAttrPtr)); |
| 1209 | if (ret->attrs == NULL) { |
| 1210 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1211 | return (ret); |
| 1212 | } |
| 1213 | } else if (ret->maxAttrs < nbAttrs) { |
| 1214 | xmlAttrPtr *tmp; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1215 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1216 | tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs * |
| 1217 | sizeof(xmlAttrPtr)); |
| 1218 | if (tmp == NULL) { |
| 1219 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1220 | return (ret); |
| 1221 | } |
| 1222 | ret->attrs = tmp; |
| 1223 | ret->maxAttrs = nbAttrs; |
| 1224 | } |
| 1225 | ret->nbAttrs = nbAttrs; |
| 1226 | if (nbAttrs < MAX_ATTR) { |
| 1227 | memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs); |
| 1228 | } else { |
| 1229 | attr = node->properties; |
| 1230 | nbAttrs = 0; |
| 1231 | while (attr != NULL) { |
| 1232 | ret->attrs[nbAttrs++] = attr; |
| 1233 | attr = attr->next; |
| 1234 | } |
| 1235 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1236 | } |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 1237 | ret->nbAttrLeft = ret->nbAttrs; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1238 | return (ret); |
| 1239 | } |
| 1240 | |
| 1241 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1242 | * xmlRelaxNGCopyValidState: |
| 1243 | * @ctxt: a Relax-NG validation context |
| 1244 | * @state: a validation state |
| 1245 | * |
| 1246 | * Copy the validation state |
| 1247 | * |
| 1248 | * Returns the newly allocated structure or NULL in case or error |
| 1249 | */ |
| 1250 | static xmlRelaxNGValidStatePtr |
| 1251 | xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1252 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1253 | { |
| 1254 | xmlRelaxNGValidStatePtr ret; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1255 | unsigned int maxAttrs; |
| 1256 | xmlAttrPtr *attrs; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1257 | |
| 1258 | if (state == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1259 | return (NULL); |
| 1260 | if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) { |
| 1261 | ctxt->freeState->nbState--; |
| 1262 | ret = ctxt->freeState->tabState[ctxt->freeState->nbState]; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1263 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1264 | ret = |
| 1265 | (xmlRelaxNGValidStatePtr) |
| 1266 | xmlMalloc(sizeof(xmlRelaxNGValidState)); |
| 1267 | if (ret == NULL) { |
| 1268 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1269 | return (NULL); |
| 1270 | } |
| 1271 | memset(ret, 0, sizeof(xmlRelaxNGValidState)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1272 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1273 | attrs = ret->attrs; |
| 1274 | maxAttrs = ret->maxAttrs; |
| 1275 | memcpy(ret, state, sizeof(xmlRelaxNGValidState)); |
| 1276 | ret->attrs = attrs; |
| 1277 | ret->maxAttrs = maxAttrs; |
| 1278 | if (state->nbAttrs > 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1279 | if (ret->attrs == NULL) { |
| 1280 | ret->maxAttrs = state->maxAttrs; |
| 1281 | ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs * |
| 1282 | sizeof(xmlAttrPtr)); |
| 1283 | if (ret->attrs == NULL) { |
| 1284 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1285 | ret->nbAttrs = 0; |
| 1286 | return (ret); |
| 1287 | } |
| 1288 | } else if (ret->maxAttrs < state->nbAttrs) { |
| 1289 | xmlAttrPtr *tmp; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1290 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1291 | tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs * |
| 1292 | sizeof(xmlAttrPtr)); |
| 1293 | if (tmp == NULL) { |
| 1294 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1295 | ret->nbAttrs = 0; |
| 1296 | return (ret); |
| 1297 | } |
| 1298 | ret->maxAttrs = state->maxAttrs; |
| 1299 | ret->attrs = tmp; |
| 1300 | } |
| 1301 | memcpy(ret->attrs, state->attrs, |
| 1302 | state->nbAttrs * sizeof(xmlAttrPtr)); |
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 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1305 | } |
| 1306 | |
| 1307 | /** |
| 1308 | * xmlRelaxNGEqualValidState: |
| 1309 | * @ctxt: a Relax-NG validation context |
| 1310 | * @state1: a validation state |
| 1311 | * @state2: a validation state |
| 1312 | * |
| 1313 | * Compare the validation states for equality |
| 1314 | * |
| 1315 | * Returns 1 if equald, 0 otherwise |
| 1316 | */ |
| 1317 | static int |
| 1318 | xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1319 | xmlRelaxNGValidStatePtr state1, |
| 1320 | xmlRelaxNGValidStatePtr state2) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1321 | { |
| 1322 | int i; |
| 1323 | |
| 1324 | if ((state1 == NULL) || (state2 == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1325 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1326 | if (state1 == state2) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1327 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1328 | if (state1->node != state2->node) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1329 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1330 | if (state1->seq != state2->seq) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1331 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1332 | if (state1->nbAttrLeft != state2->nbAttrLeft) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1333 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1334 | if (state1->nbAttrs != state2->nbAttrs) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1335 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1336 | if (state1->endvalue != state2->endvalue) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1337 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1338 | if ((state1->value != state2->value) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1339 | (!xmlStrEqual(state1->value, state2->value))) |
| 1340 | return (0); |
| 1341 | for (i = 0; i < state1->nbAttrs; i++) { |
| 1342 | if (state1->attrs[i] != state2->attrs[i]) |
| 1343 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1344 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1345 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1349 | * xmlRelaxNGFreeValidState: |
| 1350 | * @state: a validation state structure |
| 1351 | * |
| 1352 | * Deallocate a RelaxNG validation state structure. |
| 1353 | */ |
| 1354 | static void |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1355 | xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1356 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1357 | { |
| 1358 | if (state == NULL) |
| 1359 | return; |
| 1360 | |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1361 | if ((ctxt != NULL) && (ctxt->freeState == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1362 | ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1363 | } |
| 1364 | if ((ctxt == NULL) || (ctxt->freeState == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1365 | if (state->attrs != NULL) |
| 1366 | xmlFree(state->attrs); |
| 1367 | xmlFree(state); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1368 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1369 | xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1370 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
| 1373 | /************************************************************************ |
| 1374 | * * |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1375 | * Document functions * |
| 1376 | * * |
| 1377 | ************************************************************************/ |
| 1378 | static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1379 | xmlDocPtr doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1380 | |
| 1381 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1382 | * xmlRelaxNGIncludePush: |
| 1383 | * @ctxt: the parser context |
| 1384 | * @value: the element doc |
| 1385 | * |
| 1386 | * Pushes a new include on top of the include stack |
| 1387 | * |
| 1388 | * Returns 0 in case of error, the index in the stack otherwise |
| 1389 | */ |
| 1390 | static int |
| 1391 | xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1392 | xmlRelaxNGIncludePtr value) |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1393 | { |
| 1394 | if (ctxt->incTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1395 | ctxt->incMax = 4; |
| 1396 | ctxt->incNr = 0; |
| 1397 | ctxt->incTab = |
| 1398 | (xmlRelaxNGIncludePtr *) xmlMalloc(ctxt->incMax * |
| 1399 | sizeof(ctxt->incTab[0])); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1400 | if (ctxt->incTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1401 | xmlRngPErrMemory(ctxt, "allocating include\n"); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1402 | return (0); |
| 1403 | } |
| 1404 | } |
| 1405 | if (ctxt->incNr >= ctxt->incMax) { |
| 1406 | ctxt->incMax *= 2; |
| 1407 | ctxt->incTab = |
| 1408 | (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1409 | ctxt->incMax * |
| 1410 | sizeof(ctxt->incTab[0])); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1411 | if (ctxt->incTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1412 | xmlRngPErrMemory(ctxt, "allocating include\n"); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1413 | return (0); |
| 1414 | } |
| 1415 | } |
| 1416 | ctxt->incTab[ctxt->incNr] = value; |
| 1417 | ctxt->inc = value; |
| 1418 | return (ctxt->incNr++); |
| 1419 | } |
| 1420 | |
| 1421 | /** |
| 1422 | * xmlRelaxNGIncludePop: |
| 1423 | * @ctxt: the parser context |
| 1424 | * |
| 1425 | * Pops the top include from the include stack |
| 1426 | * |
| 1427 | * Returns the include just removed |
| 1428 | */ |
| 1429 | static xmlRelaxNGIncludePtr |
| 1430 | xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt) |
| 1431 | { |
| 1432 | xmlRelaxNGIncludePtr ret; |
| 1433 | |
| 1434 | if (ctxt->incNr <= 0) |
| 1435 | return (0); |
| 1436 | ctxt->incNr--; |
| 1437 | if (ctxt->incNr > 0) |
| 1438 | ctxt->inc = ctxt->incTab[ctxt->incNr - 1]; |
| 1439 | else |
| 1440 | ctxt->inc = NULL; |
| 1441 | ret = ctxt->incTab[ctxt->incNr]; |
| 1442 | ctxt->incTab[ctxt->incNr] = 0; |
| 1443 | return (ret); |
| 1444 | } |
| 1445 | |
| 1446 | /** |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1447 | * xmlRelaxNGRemoveRedefine: |
| 1448 | * @ctxt: the parser context |
| 1449 | * @URL: the normalized URL |
| 1450 | * @target: the included target |
| 1451 | * @name: the define name to eliminate |
| 1452 | * |
| 1453 | * Applies the elimination algorithm of 4.7 |
| 1454 | * |
| 1455 | * Returns 0 in case of error, 1 in case of success. |
| 1456 | */ |
| 1457 | static int |
| 1458 | xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1459 | const xmlChar * URL ATTRIBUTE_UNUSED, |
| 1460 | xmlNodePtr target, const xmlChar * name) |
| 1461 | { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1462 | int found = 0; |
| 1463 | xmlNodePtr tmp, tmp2; |
| 1464 | xmlChar *name2; |
| 1465 | |
| 1466 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 1467 | if (name == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1468 | xmlGenericError(xmlGenericErrorContext, |
| 1469 | "Elimination of <include> start from %s\n", URL); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 1470 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1471 | xmlGenericError(xmlGenericErrorContext, |
| 1472 | "Elimination of <include> define %s from %s\n", |
| 1473 | name, URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1474 | #endif |
| 1475 | tmp = target; |
| 1476 | while (tmp != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1477 | tmp2 = tmp->next; |
| 1478 | if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) { |
| 1479 | found = 1; |
| 1480 | xmlUnlinkNode(tmp); |
| 1481 | xmlFreeNode(tmp); |
| 1482 | } else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) { |
| 1483 | name2 = xmlGetProp(tmp, BAD_CAST "name"); |
| 1484 | xmlRelaxNGNormExtSpace(name2); |
| 1485 | if (name2 != NULL) { |
| 1486 | if (xmlStrEqual(name, name2)) { |
| 1487 | found = 1; |
| 1488 | xmlUnlinkNode(tmp); |
| 1489 | xmlFreeNode(tmp); |
| 1490 | } |
| 1491 | xmlFree(name2); |
| 1492 | } |
| 1493 | } else if (IS_RELAXNG(tmp, "include")) { |
| 1494 | xmlChar *href = NULL; |
| 1495 | xmlRelaxNGDocumentPtr inc = tmp->_private; |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1496 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1497 | if ((inc != NULL) && (inc->doc != NULL) && |
| 1498 | (inc->doc->children != NULL)) { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1499 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1500 | if (xmlStrEqual |
| 1501 | (inc->doc->children->name, BAD_CAST "grammar")) { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1502 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1503 | href = xmlGetProp(tmp, BAD_CAST "href"); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1504 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1505 | if (xmlRelaxNGRemoveRedefine(ctxt, href, |
| 1506 | inc->doc->children-> |
| 1507 | children, name) == 1) { |
| 1508 | found = 1; |
| 1509 | } |
| 1510 | if (href != NULL) |
| 1511 | xmlFree(href); |
| 1512 | } |
| 1513 | } |
| 1514 | } |
| 1515 | tmp = tmp2; |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1516 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1517 | return (found); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1521 | * xmlRelaxNGLoadInclude: |
| 1522 | * @ctxt: the parser context |
| 1523 | * @URL: the normalized URL |
| 1524 | * @node: the include node. |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1525 | * @ns: the namespace passed from the context. |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1526 | * |
| 1527 | * First lookup if the document is already loaded into the parser context, |
| 1528 | * check against recursion. If not found the resource is loaded and |
| 1529 | * the content is preprocessed before being returned back to the caller. |
| 1530 | * |
| 1531 | * Returns the xmlRelaxNGIncludePtr or NULL in case of error |
| 1532 | */ |
| 1533 | static xmlRelaxNGIncludePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1534 | xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * URL, |
| 1535 | xmlNodePtr node, const xmlChar * ns) |
| 1536 | { |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1537 | xmlRelaxNGIncludePtr ret = NULL; |
| 1538 | xmlDocPtr doc; |
| 1539 | int i; |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1540 | xmlNodePtr root, cur; |
| 1541 | |
| 1542 | #ifdef DEBUG_INCLUDE |
| 1543 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1544 | "xmlRelaxNGLoadInclude(%s)\n", URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1545 | #endif |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1546 | |
| 1547 | /* |
| 1548 | * check against recursion in the stack |
| 1549 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1550 | for (i = 0; i < ctxt->incNr; i++) { |
| 1551 | if (xmlStrEqual(ctxt->incTab[i]->href, URL)) { |
| 1552 | xmlRngPErr(ctxt, NULL, XML_RNGP_INCLUDE_RECURSE, |
| 1553 | "Detected an Include recursion for %s\n", URL, |
| 1554 | NULL); |
| 1555 | return (NULL); |
| 1556 | } |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1557 | } |
| 1558 | |
| 1559 | /* |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1560 | * load the document |
| 1561 | */ |
| 1562 | doc = xmlParseFile((const char *) URL); |
| 1563 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1564 | xmlRngPErr(ctxt, node, XML_RNGP_PARSE_ERROR, |
| 1565 | "xmlRelaxNG: could not load %s\n", URL, NULL); |
| 1566 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1567 | } |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1568 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1569 | xmlGenericError(xmlGenericErrorContext, "Parsed %s Okay\n", URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1570 | #endif |
| 1571 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1572 | /* |
| 1573 | * Allocate the document structures and register it first. |
| 1574 | */ |
| 1575 | ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude)); |
| 1576 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1577 | xmlRngPErrMemory(ctxt, "allocating include\n"); |
| 1578 | xmlFreeDoc(doc); |
| 1579 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1580 | } |
| 1581 | memset(ret, 0, sizeof(xmlRelaxNGInclude)); |
| 1582 | ret->doc = doc; |
| 1583 | ret->href = xmlStrdup(URL); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1584 | ret->next = ctxt->includes; |
| 1585 | ctxt->includes = ret; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1586 | |
| 1587 | /* |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1588 | * transmit the ns if needed |
| 1589 | */ |
| 1590 | if (ns != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1591 | root = xmlDocGetRootElement(doc); |
| 1592 | if (root != NULL) { |
| 1593 | if (xmlHasProp(root, BAD_CAST "ns") == NULL) { |
| 1594 | xmlSetProp(root, BAD_CAST "ns", ns); |
| 1595 | } |
| 1596 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1597 | } |
| 1598 | |
| 1599 | /* |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1600 | * push it on the stack |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1601 | */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1602 | xmlRelaxNGIncludePush(ctxt, ret); |
| 1603 | |
| 1604 | /* |
| 1605 | * Some preprocessing of the document content, this include recursing |
| 1606 | * in the include stack. |
| 1607 | */ |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1608 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1609 | xmlGenericError(xmlGenericErrorContext, "cleanup of %s\n", URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1610 | #endif |
| 1611 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1612 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 1613 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1614 | ctxt->inc = NULL; |
| 1615 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1616 | } |
| 1617 | |
| 1618 | /* |
| 1619 | * Pop up the include from the stack |
| 1620 | */ |
| 1621 | xmlRelaxNGIncludePop(ctxt); |
| 1622 | |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1623 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1624 | xmlGenericError(xmlGenericErrorContext, "Checking of %s\n", URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1625 | #endif |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1626 | /* |
| 1627 | * Check that the top element is a grammar |
| 1628 | */ |
| 1629 | root = xmlDocGetRootElement(doc); |
| 1630 | if (root == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1631 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, |
| 1632 | "xmlRelaxNG: included document is empty %s\n", URL, |
| 1633 | NULL); |
| 1634 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1635 | } |
| 1636 | if (!IS_RELAXNG(root, "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1637 | xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING, |
| 1638 | "xmlRelaxNG: included document %s root is not a grammar\n", |
| 1639 | URL, NULL); |
| 1640 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1641 | } |
| 1642 | |
| 1643 | /* |
| 1644 | * Elimination of redefined rules in the include. |
| 1645 | */ |
| 1646 | cur = node->children; |
| 1647 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1648 | if (IS_RELAXNG(cur, "start")) { |
| 1649 | int found = 0; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1650 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1651 | found = |
| 1652 | xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL); |
| 1653 | if (!found) { |
| 1654 | xmlRngPErr(ctxt, node, XML_RNGP_START_MISSING, |
| 1655 | "xmlRelaxNG: include %s has a start but not the included grammar\n", |
| 1656 | URL, NULL); |
| 1657 | } |
| 1658 | } else if (IS_RELAXNG(cur, "define")) { |
| 1659 | xmlChar *name; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1660 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1661 | name = xmlGetProp(cur, BAD_CAST "name"); |
| 1662 | if (name == NULL) { |
| 1663 | xmlRngPErr(ctxt, node, XML_RNGP_NAME_MISSING, |
| 1664 | "xmlRelaxNG: include %s has define without name\n", |
| 1665 | URL, NULL); |
| 1666 | } else { |
| 1667 | int found; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1668 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1669 | xmlRelaxNGNormExtSpace(name); |
| 1670 | found = xmlRelaxNGRemoveRedefine(ctxt, URL, |
| 1671 | root->children, name); |
| 1672 | if (!found) { |
| 1673 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_MISSING, |
| 1674 | "xmlRelaxNG: include %s has a define %s but not the included grammar\n", |
| 1675 | URL, name); |
| 1676 | } |
| 1677 | xmlFree(name); |
| 1678 | } |
| 1679 | } |
| 1680 | cur = cur->next; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1681 | } |
| 1682 | |
| 1683 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1684 | return (ret); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1685 | } |
| 1686 | |
| 1687 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1688 | * xmlRelaxNGValidErrorPush: |
| 1689 | * @ctxt: the validation context |
| 1690 | * @err: the error code |
| 1691 | * @arg1: the first string argument |
| 1692 | * @arg2: the second string argument |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1693 | * @dup: arg need to be duplicated |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1694 | * |
| 1695 | * Pushes a new error on top of the error stack |
| 1696 | * |
| 1697 | * Returns 0 in case of error, the index in the stack otherwise |
| 1698 | */ |
| 1699 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1700 | xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt, |
| 1701 | xmlRelaxNGValidErr err, const xmlChar * arg1, |
| 1702 | const xmlChar * arg2, int dup) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1703 | { |
| 1704 | xmlRelaxNGValidErrorPtr cur; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1705 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 1706 | #ifdef DEBUG_ERROR |
| 1707 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1708 | "Pushing error %d at %d on stack\n", err, ctxt->errNr); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 1709 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1710 | if (ctxt->errTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1711 | ctxt->errMax = 8; |
| 1712 | ctxt->errNr = 0; |
| 1713 | ctxt->errTab = |
| 1714 | (xmlRelaxNGValidErrorPtr) xmlMalloc(ctxt->errMax * |
| 1715 | sizeof |
| 1716 | (xmlRelaxNGValidError)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1717 | if (ctxt->errTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1718 | xmlRngVErrMemory(ctxt, "pushing error\n"); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1719 | return (0); |
| 1720 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1721 | ctxt->err = NULL; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1722 | } |
| 1723 | if (ctxt->errNr >= ctxt->errMax) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1724 | ctxt->errMax *= 2; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1725 | ctxt->errTab = |
| 1726 | (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1727 | ctxt->errMax * |
| 1728 | sizeof |
| 1729 | (xmlRelaxNGValidError)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1730 | if (ctxt->errTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1731 | xmlRngVErrMemory(ctxt, "pushing error\n"); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1732 | return (0); |
| 1733 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1734 | ctxt->err = &ctxt->errTab[ctxt->errNr - 1]; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1735 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 1736 | if ((ctxt->err != NULL) && (ctxt->state != NULL) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1737 | (ctxt->err->node == ctxt->state->node) && (ctxt->err->err == err)) |
| 1738 | return (ctxt->errNr); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1739 | cur = &ctxt->errTab[ctxt->errNr]; |
| 1740 | cur->err = err; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1741 | if (dup) { |
| 1742 | cur->arg1 = xmlStrdup(arg1); |
| 1743 | cur->arg2 = xmlStrdup(arg2); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1744 | cur->flags = ERROR_IS_DUP; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1745 | } else { |
| 1746 | cur->arg1 = arg1; |
| 1747 | cur->arg2 = arg2; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1748 | cur->flags = 0; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1749 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1750 | if (ctxt->state != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1751 | cur->node = ctxt->state->node; |
| 1752 | cur->seq = ctxt->state->seq; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1753 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1754 | cur->node = NULL; |
| 1755 | cur->seq = NULL; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1756 | } |
| 1757 | ctxt->err = cur; |
| 1758 | return (ctxt->errNr++); |
| 1759 | } |
| 1760 | |
| 1761 | /** |
| 1762 | * xmlRelaxNGValidErrorPop: |
| 1763 | * @ctxt: the validation context |
| 1764 | * |
| 1765 | * Pops the top error from the error stack |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1766 | */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1767 | static void |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1768 | xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt) |
| 1769 | { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1770 | xmlRelaxNGValidErrorPtr cur; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1771 | |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1772 | if (ctxt->errNr <= 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1773 | ctxt->err = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1774 | return; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1775 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1776 | ctxt->errNr--; |
| 1777 | if (ctxt->errNr > 0) |
| 1778 | ctxt->err = &ctxt->errTab[ctxt->errNr - 1]; |
| 1779 | else |
| 1780 | ctxt->err = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1781 | cur = &ctxt->errTab[ctxt->errNr]; |
| 1782 | if (cur->flags & ERROR_IS_DUP) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1783 | if (cur->arg1 != NULL) |
| 1784 | xmlFree((xmlChar *) cur->arg1); |
| 1785 | cur->arg1 = NULL; |
| 1786 | if (cur->arg2 != NULL) |
| 1787 | xmlFree((xmlChar *) cur->arg2); |
| 1788 | cur->arg2 = NULL; |
| 1789 | cur->flags = 0; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1790 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1791 | } |
| 1792 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1793 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1794 | * xmlRelaxNGDocumentPush: |
| 1795 | * @ctxt: the parser context |
| 1796 | * @value: the element doc |
| 1797 | * |
| 1798 | * Pushes a new doc on top of the doc stack |
| 1799 | * |
| 1800 | * Returns 0 in case of error, the index in the stack otherwise |
| 1801 | */ |
| 1802 | static int |
| 1803 | xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1804 | xmlRelaxNGDocumentPtr value) |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1805 | { |
| 1806 | if (ctxt->docTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1807 | ctxt->docMax = 4; |
| 1808 | ctxt->docNr = 0; |
| 1809 | ctxt->docTab = |
| 1810 | (xmlRelaxNGDocumentPtr *) xmlMalloc(ctxt->docMax * |
| 1811 | sizeof(ctxt->docTab[0])); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1812 | if (ctxt->docTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1813 | xmlRngPErrMemory(ctxt, "adding document\n"); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1814 | return (0); |
| 1815 | } |
| 1816 | } |
| 1817 | if (ctxt->docNr >= ctxt->docMax) { |
| 1818 | ctxt->docMax *= 2; |
| 1819 | ctxt->docTab = |
| 1820 | (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1821 | ctxt->docMax * |
| 1822 | sizeof(ctxt->docTab[0])); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1823 | if (ctxt->docTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1824 | xmlRngPErrMemory(ctxt, "adding document\n"); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1825 | return (0); |
| 1826 | } |
| 1827 | } |
| 1828 | ctxt->docTab[ctxt->docNr] = value; |
| 1829 | ctxt->doc = value; |
| 1830 | return (ctxt->docNr++); |
| 1831 | } |
| 1832 | |
| 1833 | /** |
| 1834 | * xmlRelaxNGDocumentPop: |
| 1835 | * @ctxt: the parser context |
| 1836 | * |
| 1837 | * Pops the top doc from the doc stack |
| 1838 | * |
| 1839 | * Returns the doc just removed |
| 1840 | */ |
| 1841 | static xmlRelaxNGDocumentPtr |
| 1842 | xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt) |
| 1843 | { |
| 1844 | xmlRelaxNGDocumentPtr ret; |
| 1845 | |
| 1846 | if (ctxt->docNr <= 0) |
| 1847 | return (0); |
| 1848 | ctxt->docNr--; |
| 1849 | if (ctxt->docNr > 0) |
| 1850 | ctxt->doc = ctxt->docTab[ctxt->docNr - 1]; |
| 1851 | else |
| 1852 | ctxt->doc = NULL; |
| 1853 | ret = ctxt->docTab[ctxt->docNr]; |
| 1854 | ctxt->docTab[ctxt->docNr] = 0; |
| 1855 | return (ret); |
| 1856 | } |
| 1857 | |
| 1858 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1859 | * xmlRelaxNGLoadExternalRef: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1860 | * @ctxt: the parser context |
| 1861 | * @URL: the normalized URL |
| 1862 | * @ns: the inherited ns if any |
| 1863 | * |
| 1864 | * First lookup if the document is already loaded into the parser context, |
| 1865 | * check against recursion. If not found the resource is loaded and |
| 1866 | * the content is preprocessed before being returned back to the caller. |
| 1867 | * |
| 1868 | * Returns the xmlRelaxNGDocumentPtr or NULL in case of error |
| 1869 | */ |
| 1870 | static xmlRelaxNGDocumentPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1871 | xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt, |
| 1872 | const xmlChar * URL, const xmlChar * ns) |
| 1873 | { |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1874 | xmlRelaxNGDocumentPtr ret = NULL; |
| 1875 | xmlDocPtr doc; |
| 1876 | xmlNodePtr root; |
| 1877 | int i; |
| 1878 | |
| 1879 | /* |
| 1880 | * check against recursion in the stack |
| 1881 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1882 | for (i = 0; i < ctxt->docNr; i++) { |
| 1883 | if (xmlStrEqual(ctxt->docTab[i]->href, URL)) { |
| 1884 | xmlRngPErr(ctxt, NULL, XML_RNGP_EXTERNALREF_RECURSE, |
| 1885 | "Detected an externalRef recursion for %s\n", URL, |
| 1886 | NULL); |
| 1887 | return (NULL); |
| 1888 | } |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1889 | } |
| 1890 | |
| 1891 | /* |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1892 | * load the document |
| 1893 | */ |
| 1894 | doc = xmlParseFile((const char *) URL); |
| 1895 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1896 | xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR, |
| 1897 | "xmlRelaxNG: could not load %s\n", URL, NULL); |
| 1898 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1899 | } |
| 1900 | |
| 1901 | /* |
| 1902 | * Allocate the document structures and register it first. |
| 1903 | */ |
| 1904 | ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument)); |
| 1905 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1906 | xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_ERR_NO_MEMORY, |
| 1907 | "xmlRelaxNG: allocate memory for doc %s\n", URL, NULL); |
| 1908 | xmlFreeDoc(doc); |
| 1909 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1910 | } |
| 1911 | memset(ret, 0, sizeof(xmlRelaxNGDocument)); |
| 1912 | ret->doc = doc; |
| 1913 | ret->href = xmlStrdup(URL); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1914 | ret->next = ctxt->documents; |
| 1915 | ctxt->documents = ret; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1916 | |
| 1917 | /* |
| 1918 | * transmit the ns if needed |
| 1919 | */ |
| 1920 | if (ns != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1921 | root = xmlDocGetRootElement(doc); |
| 1922 | if (root != NULL) { |
| 1923 | if (xmlHasProp(root, BAD_CAST "ns") == NULL) { |
| 1924 | xmlSetProp(root, BAD_CAST "ns", ns); |
| 1925 | } |
| 1926 | } |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1927 | } |
| 1928 | |
| 1929 | /* |
| 1930 | * push it on the stack and register it in the hash table |
| 1931 | */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1932 | xmlRelaxNGDocumentPush(ctxt, ret); |
| 1933 | |
| 1934 | /* |
| 1935 | * Some preprocessing of the document content |
| 1936 | */ |
| 1937 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 1938 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1939 | ctxt->doc = NULL; |
| 1940 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1941 | } |
| 1942 | |
| 1943 | xmlRelaxNGDocumentPop(ctxt); |
| 1944 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1945 | return (ret); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1946 | } |
| 1947 | |
| 1948 | /************************************************************************ |
| 1949 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1950 | * Error functions * |
| 1951 | * * |
| 1952 | ************************************************************************/ |
| 1953 | |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1954 | #define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0); |
| 1955 | #define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0); |
| 1956 | #define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0); |
| 1957 | #define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1); |
| 1958 | #define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1959 | |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 1960 | static const char * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1961 | xmlRelaxNGDefName(xmlRelaxNGDefinePtr def) |
| 1962 | { |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 1963 | if (def == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1964 | return ("none"); |
| 1965 | switch (def->type) { |
| 1966 | case XML_RELAXNG_EMPTY: |
| 1967 | return ("empty"); |
| 1968 | case XML_RELAXNG_NOT_ALLOWED: |
| 1969 | return ("notAllowed"); |
| 1970 | case XML_RELAXNG_EXCEPT: |
| 1971 | return ("except"); |
| 1972 | case XML_RELAXNG_TEXT: |
| 1973 | return ("text"); |
| 1974 | case XML_RELAXNG_ELEMENT: |
| 1975 | return ("element"); |
| 1976 | case XML_RELAXNG_DATATYPE: |
| 1977 | return ("datatype"); |
| 1978 | case XML_RELAXNG_VALUE: |
| 1979 | return ("value"); |
| 1980 | case XML_RELAXNG_LIST: |
| 1981 | return ("list"); |
| 1982 | case XML_RELAXNG_ATTRIBUTE: |
| 1983 | return ("attribute"); |
| 1984 | case XML_RELAXNG_DEF: |
| 1985 | return ("def"); |
| 1986 | case XML_RELAXNG_REF: |
| 1987 | return ("ref"); |
| 1988 | case XML_RELAXNG_EXTERNALREF: |
| 1989 | return ("externalRef"); |
| 1990 | case XML_RELAXNG_PARENTREF: |
| 1991 | return ("parentRef"); |
| 1992 | case XML_RELAXNG_OPTIONAL: |
| 1993 | return ("optional"); |
| 1994 | case XML_RELAXNG_ZEROORMORE: |
| 1995 | return ("zeroOrMore"); |
| 1996 | case XML_RELAXNG_ONEORMORE: |
| 1997 | return ("oneOrMore"); |
| 1998 | case XML_RELAXNG_CHOICE: |
| 1999 | return ("choice"); |
| 2000 | case XML_RELAXNG_GROUP: |
| 2001 | return ("group"); |
| 2002 | case XML_RELAXNG_INTERLEAVE: |
| 2003 | return ("interleave"); |
| 2004 | case XML_RELAXNG_START: |
| 2005 | return ("start"); |
| 2006 | case XML_RELAXNG_NOOP: |
| 2007 | return ("noop"); |
| 2008 | case XML_RELAXNG_PARAM: |
| 2009 | return ("param"); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 2010 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2011 | return ("unknown"); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 2012 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 2013 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2014 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2015 | * xmlRelaxNGGetErrorString: |
| 2016 | * @err: the error code |
| 2017 | * @arg1: the first string argument |
| 2018 | * @arg2: the second string argument |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2019 | * |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2020 | * computes a formatted error string for the given error code and args |
| 2021 | * |
| 2022 | * Returns the error string, it must be deallocated by the caller |
| 2023 | */ |
| 2024 | static xmlChar * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2025 | xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar * arg1, |
| 2026 | const xmlChar * arg2) |
| 2027 | { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2028 | char msg[1000]; |
| 2029 | |
| 2030 | if (arg1 == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2031 | arg1 = BAD_CAST ""; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2032 | if (arg2 == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2033 | arg2 = BAD_CAST ""; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2034 | |
| 2035 | msg[0] = 0; |
| 2036 | switch (err) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2037 | case XML_RELAXNG_OK: |
| 2038 | return (NULL); |
| 2039 | case XML_RELAXNG_ERR_MEMORY: |
| 2040 | return (xmlCharStrdup("out of memory\n")); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2041 | case XML_RELAXNG_ERR_TYPE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2042 | snprintf(msg, 1000, "failed to validate type %s\n", arg1); |
| 2043 | break; |
| 2044 | case XML_RELAXNG_ERR_TYPEVAL: |
| 2045 | snprintf(msg, 1000, "Type %s doesn't allow value '%s'\n", arg1, |
| 2046 | arg2); |
| 2047 | break; |
| 2048 | case XML_RELAXNG_ERR_DUPID: |
| 2049 | snprintf(msg, 1000, "ID %s redefined\n", arg1); |
| 2050 | break; |
| 2051 | case XML_RELAXNG_ERR_TYPECMP: |
| 2052 | snprintf(msg, 1000, "failed to compare type %s\n", arg1); |
| 2053 | break; |
| 2054 | case XML_RELAXNG_ERR_NOSTATE: |
| 2055 | return (xmlCharStrdup("Internal error: no state\n")); |
| 2056 | case XML_RELAXNG_ERR_NODEFINE: |
| 2057 | return (xmlCharStrdup("Internal error: no define\n")); |
| 2058 | case XML_RELAXNG_ERR_INTERNAL: |
| 2059 | snprintf(msg, 1000, "Internal error: %s\n", arg1); |
| 2060 | break; |
| 2061 | case XML_RELAXNG_ERR_LISTEXTRA: |
| 2062 | snprintf(msg, 1000, "Extra data in list: %s\n", arg1); |
| 2063 | break; |
| 2064 | case XML_RELAXNG_ERR_INTERNODATA: |
| 2065 | return (xmlCharStrdup |
| 2066 | ("Internal: interleave block has no data\n")); |
| 2067 | case XML_RELAXNG_ERR_INTERSEQ: |
| 2068 | return (xmlCharStrdup("Invalid sequence in interleave\n")); |
| 2069 | case XML_RELAXNG_ERR_INTEREXTRA: |
| 2070 | snprintf(msg, 1000, "Extra element %s in interleave\n", arg1); |
| 2071 | break; |
| 2072 | case XML_RELAXNG_ERR_ELEMNAME: |
| 2073 | snprintf(msg, 1000, "Expecting element %s, got %s\n", arg1, |
| 2074 | arg2); |
| 2075 | break; |
| 2076 | case XML_RELAXNG_ERR_ELEMNONS: |
| 2077 | snprintf(msg, 1000, "Expecting a namespace for element %s\n", |
| 2078 | arg1); |
| 2079 | break; |
| 2080 | case XML_RELAXNG_ERR_ELEMWRONGNS: |
| 2081 | snprintf(msg, 1000, |
| 2082 | "Element %s has wrong namespace: expecting %s\n", arg1, |
| 2083 | arg2); |
| 2084 | break; |
| 2085 | case XML_RELAXNG_ERR_ELEMWRONG: |
| 2086 | snprintf(msg, 1000, "Did not expect element %s there\n", arg1); |
| 2087 | break; |
| 2088 | case XML_RELAXNG_ERR_TEXTWRONG: |
| 2089 | snprintf(msg, 1000, |
| 2090 | "Did not expect text in element %s content\n", arg1); |
| 2091 | break; |
| 2092 | case XML_RELAXNG_ERR_ELEMEXTRANS: |
| 2093 | snprintf(msg, 1000, "Expecting no namespace for element %s\n", |
| 2094 | arg1); |
| 2095 | break; |
| 2096 | case XML_RELAXNG_ERR_ELEMNOTEMPTY: |
| 2097 | snprintf(msg, 1000, "Expecting element %s to be empty\n", arg1); |
| 2098 | break; |
| 2099 | case XML_RELAXNG_ERR_NOELEM: |
| 2100 | snprintf(msg, 1000, "Expecting an element %s, got nothing\n", |
| 2101 | arg1); |
| 2102 | break; |
| 2103 | case XML_RELAXNG_ERR_NOTELEM: |
| 2104 | return (xmlCharStrdup("Expecting an element got text\n")); |
| 2105 | case XML_RELAXNG_ERR_ATTRVALID: |
| 2106 | snprintf(msg, 1000, "Element %s failed to validate attributes\n", |
| 2107 | arg1); |
| 2108 | break; |
| 2109 | case XML_RELAXNG_ERR_CONTENTVALID: |
| 2110 | snprintf(msg, 1000, "Element %s failed to validate content\n", |
| 2111 | arg1); |
| 2112 | break; |
| 2113 | case XML_RELAXNG_ERR_EXTRACONTENT: |
| 2114 | snprintf(msg, 1000, "Element %s has extra content: %s\n", |
| 2115 | arg1, arg2); |
| 2116 | break; |
| 2117 | case XML_RELAXNG_ERR_INVALIDATTR: |
| 2118 | snprintf(msg, 1000, "Invalid attribute %s for element %s\n", |
| 2119 | arg1, arg2); |
| 2120 | break; |
| 2121 | case XML_RELAXNG_ERR_LACKDATA: |
| 2122 | snprintf(msg, 1000, "Datatype element %s contains no data\n", |
| 2123 | arg1); |
| 2124 | break; |
| 2125 | case XML_RELAXNG_ERR_DATAELEM: |
| 2126 | snprintf(msg, 1000, "Datatype element %s has child elements\n", |
| 2127 | arg1); |
| 2128 | break; |
| 2129 | case XML_RELAXNG_ERR_VALELEM: |
| 2130 | snprintf(msg, 1000, "Value element %s has child elements\n", |
| 2131 | arg1); |
| 2132 | break; |
| 2133 | case XML_RELAXNG_ERR_LISTELEM: |
| 2134 | snprintf(msg, 1000, "List element %s has child elements\n", |
| 2135 | arg1); |
| 2136 | break; |
| 2137 | case XML_RELAXNG_ERR_DATATYPE: |
| 2138 | snprintf(msg, 1000, "Error validating datatype %s\n", arg1); |
| 2139 | break; |
| 2140 | case XML_RELAXNG_ERR_VALUE: |
| 2141 | snprintf(msg, 1000, "Error validating value %s\n", arg1); |
| 2142 | break; |
| 2143 | case XML_RELAXNG_ERR_LIST: |
| 2144 | return (xmlCharStrdup("Error validating list\n")); |
| 2145 | case XML_RELAXNG_ERR_NOGRAMMAR: |
| 2146 | return (xmlCharStrdup("No top grammar defined\n")); |
| 2147 | case XML_RELAXNG_ERR_EXTRADATA: |
| 2148 | return (xmlCharStrdup("Extra data in the document\n")); |
| 2149 | default: |
| 2150 | return (xmlCharStrdup("Unknown error !\n")); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2151 | } |
| 2152 | if (msg[0] == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2153 | snprintf(msg, 1000, "Unknown error code %d\n", err); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2154 | } |
Daniel Veillard | adbb0e6 | 2003-05-10 20:02:45 +0000 | [diff] [blame] | 2155 | msg[1000 - 1] = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2156 | return (xmlStrdup((xmlChar *) msg)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2157 | } |
| 2158 | |
| 2159 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2160 | * xmlRelaxNGShowValidError: |
| 2161 | * @ctxt: the validation context |
| 2162 | * @err: the error number |
| 2163 | * @node: the node |
| 2164 | * @child: the node child generating the problem. |
| 2165 | * @arg1: the first argument |
| 2166 | * @arg2: the second argument |
| 2167 | * |
| 2168 | * Show a validation error. |
| 2169 | */ |
| 2170 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2171 | xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt, |
| 2172 | xmlRelaxNGValidErr err, xmlNodePtr node, |
| 2173 | xmlNodePtr child, const xmlChar * arg1, |
| 2174 | const xmlChar * arg2) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2175 | { |
| 2176 | xmlChar *msg; |
| 2177 | |
| 2178 | if (ctxt->error == NULL) |
| 2179 | return; |
| 2180 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2181 | #ifdef DEBUG_ERROR |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2182 | xmlGenericError(xmlGenericErrorContext, "Show error %d\n", err); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2183 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2184 | msg = xmlRelaxNGGetErrorString(err, arg1, arg2); |
| 2185 | if (msg == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2186 | return; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2187 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2188 | if (ctxt->errNo == XML_RELAXNG_OK) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2189 | ctxt->errNo = err; |
| 2190 | xmlRngVErr(ctxt, (child == NULL ? node : child), err, |
| 2191 | (const char *) msg, arg1, arg2); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2192 | xmlFree(msg); |
| 2193 | } |
| 2194 | |
| 2195 | /** |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2196 | * xmlRelaxNGPopErrors: |
| 2197 | * @ctxt: the validation context |
| 2198 | * @level: the error level in the stack |
| 2199 | * |
| 2200 | * pop and discard all errors until the given level is reached |
| 2201 | */ |
| 2202 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2203 | xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level) |
| 2204 | { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2205 | int i; |
| 2206 | xmlRelaxNGValidErrorPtr err; |
| 2207 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2208 | #ifdef DEBUG_ERROR |
| 2209 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2210 | "Pop errors till level %d\n", level); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2211 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2212 | for (i = level; i < ctxt->errNr; i++) { |
| 2213 | err = &ctxt->errTab[i]; |
| 2214 | if (err->flags & ERROR_IS_DUP) { |
| 2215 | if (err->arg1 != NULL) |
| 2216 | xmlFree((xmlChar *) err->arg1); |
| 2217 | err->arg1 = NULL; |
| 2218 | if (err->arg2 != NULL) |
| 2219 | xmlFree((xmlChar *) err->arg2); |
| 2220 | err->arg2 = NULL; |
| 2221 | err->flags = 0; |
| 2222 | } |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2223 | } |
| 2224 | ctxt->errNr = level; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2225 | if (ctxt->errNr <= 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2226 | ctxt->err = NULL; |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2227 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2228 | |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2229 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2230 | * xmlRelaxNGDumpValidError: |
| 2231 | * @ctxt: the validation context |
| 2232 | * |
| 2233 | * Show all validation error over a given index. |
| 2234 | */ |
| 2235 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2236 | xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt) |
| 2237 | { |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 2238 | int i, j, k; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2239 | xmlRelaxNGValidErrorPtr err, dup; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2240 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2241 | #ifdef DEBUG_ERROR |
| 2242 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2243 | "Dumping error stack %d errors\n", ctxt->errNr); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2244 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2245 | for (i = 0, k = 0; i < ctxt->errNr; i++) { |
| 2246 | err = &ctxt->errTab[i]; |
| 2247 | if (k < MAX_ERROR) { |
| 2248 | for (j = 0; j < i; j++) { |
| 2249 | dup = &ctxt->errTab[j]; |
| 2250 | if ((err->err == dup->err) && (err->node == dup->node) && |
| 2251 | (xmlStrEqual(err->arg1, dup->arg1)) && |
| 2252 | (xmlStrEqual(err->arg2, dup->arg2))) { |
| 2253 | goto skip; |
| 2254 | } |
| 2255 | } |
| 2256 | xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq, |
| 2257 | err->arg1, err->arg2); |
| 2258 | k++; |
| 2259 | } |
| 2260 | skip: |
| 2261 | if (err->flags & ERROR_IS_DUP) { |
| 2262 | if (err->arg1 != NULL) |
| 2263 | xmlFree((xmlChar *) err->arg1); |
| 2264 | err->arg1 = NULL; |
| 2265 | if (err->arg2 != NULL) |
| 2266 | xmlFree((xmlChar *) err->arg2); |
| 2267 | err->arg2 = NULL; |
| 2268 | err->flags = 0; |
| 2269 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2270 | } |
| 2271 | ctxt->errNr = 0; |
| 2272 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2273 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2274 | /** |
| 2275 | * xmlRelaxNGAddValidError: |
| 2276 | * @ctxt: the validation context |
| 2277 | * @err: the error number |
| 2278 | * @arg1: the first argument |
| 2279 | * @arg2: the second argument |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2280 | * @dup: need to dup the args |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2281 | * |
| 2282 | * Register a validation error, either generating it if it's sure |
| 2283 | * or stacking it for later handling if unsure. |
| 2284 | */ |
| 2285 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2286 | xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt, |
| 2287 | xmlRelaxNGValidErr err, const xmlChar * arg1, |
| 2288 | const xmlChar * arg2, int dup) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2289 | { |
| 2290 | if ((ctxt == NULL) || (ctxt->error == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2291 | return; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2292 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2293 | #ifdef DEBUG_ERROR |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2294 | xmlGenericError(xmlGenericErrorContext, "Adding error %d\n", err); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2295 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2296 | /* |
| 2297 | * generate the error directly |
| 2298 | */ |
| 2299 | if (((ctxt->flags & 1) == 0) || (ctxt->flags & 2)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2300 | xmlNodePtr node, seq; |
| 2301 | |
| 2302 | /* |
| 2303 | * Flush first any stacked error which might be the |
| 2304 | * real cause of the problem. |
| 2305 | */ |
| 2306 | if (ctxt->errNr != 0) |
| 2307 | xmlRelaxNGDumpValidError(ctxt); |
| 2308 | if (ctxt->state != NULL) { |
| 2309 | node = ctxt->state->node; |
| 2310 | seq = ctxt->state->seq; |
| 2311 | } else { |
| 2312 | node = seq = NULL; |
| 2313 | } |
| 2314 | xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2315 | } |
| 2316 | /* |
| 2317 | * Stack the error for later processing if needed |
| 2318 | */ |
| 2319 | else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2320 | xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2321 | } |
| 2322 | } |
| 2323 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2324 | |
| 2325 | /************************************************************************ |
| 2326 | * * |
| 2327 | * Type library hooks * |
| 2328 | * * |
| 2329 | ************************************************************************/ |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2330 | static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2331 | const xmlChar * str); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2332 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2333 | /** |
| 2334 | * xmlRelaxNGSchemaTypeHave: |
| 2335 | * @data: data needed for the library |
| 2336 | * @type: the type name |
| 2337 | * |
| 2338 | * Check if the given type is provided by |
| 2339 | * the W3C XMLSchema Datatype library. |
| 2340 | * |
| 2341 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2342 | */ |
| 2343 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2344 | xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar * type) |
| 2345 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2346 | xmlSchemaTypePtr typ; |
| 2347 | |
| 2348 | if (type == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2349 | return (-1); |
| 2350 | typ = xmlSchemaGetPredefinedType(type, |
| 2351 | BAD_CAST |
| 2352 | "http://www.w3.org/2001/XMLSchema"); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2353 | if (typ == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2354 | return (0); |
| 2355 | return (1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2356 | } |
| 2357 | |
| 2358 | /** |
| 2359 | * xmlRelaxNGSchemaTypeCheck: |
| 2360 | * @data: data needed for the library |
| 2361 | * @type: the type name |
| 2362 | * @value: the value to check |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2363 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2364 | * |
| 2365 | * Check if the given type and value are validated by |
| 2366 | * the W3C XMLSchema Datatype library. |
| 2367 | * |
| 2368 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2369 | */ |
| 2370 | static int |
| 2371 | xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2372 | const xmlChar * type, |
| 2373 | const xmlChar * value, |
| 2374 | void **result, xmlNodePtr node) |
| 2375 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2376 | xmlSchemaTypePtr typ; |
| 2377 | int ret; |
| 2378 | |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2379 | if ((type == NULL) || (value == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2380 | return (-1); |
| 2381 | typ = xmlSchemaGetPredefinedType(type, |
| 2382 | BAD_CAST |
| 2383 | "http://www.w3.org/2001/XMLSchema"); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2384 | if (typ == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2385 | return (-1); |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2386 | ret = xmlSchemaValPredefTypeNode(typ, value, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2387 | (xmlSchemaValPtr *) result, node); |
| 2388 | if (ret == 2) /* special ID error code */ |
| 2389 | return (2); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2390 | if (ret == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2391 | return (1); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2392 | if (ret > 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2393 | return (0); |
| 2394 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2395 | } |
| 2396 | |
| 2397 | /** |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2398 | * xmlRelaxNGSchemaFacetCheck: |
| 2399 | * @data: data needed for the library |
| 2400 | * @type: the type name |
| 2401 | * @facet: the facet name |
| 2402 | * @val: the facet value |
| 2403 | * @strval: the string value |
| 2404 | * @value: the value to check |
| 2405 | * |
| 2406 | * Function provided by a type library to check a value facet |
| 2407 | * |
| 2408 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2409 | */ |
| 2410 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2411 | xmlRelaxNGSchemaFacetCheck(void *data ATTRIBUTE_UNUSED, |
| 2412 | const xmlChar * type, const xmlChar * facetname, |
| 2413 | const xmlChar * val, const xmlChar * strval, |
| 2414 | void *value) |
| 2415 | { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2416 | xmlSchemaFacetPtr facet; |
| 2417 | xmlSchemaTypePtr typ; |
| 2418 | int ret; |
| 2419 | |
| 2420 | if ((type == NULL) || (strval == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2421 | return (-1); |
| 2422 | typ = xmlSchemaGetPredefinedType(type, |
| 2423 | BAD_CAST |
| 2424 | "http://www.w3.org/2001/XMLSchema"); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2425 | if (typ == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2426 | return (-1); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2427 | |
| 2428 | facet = xmlSchemaNewFacet(); |
| 2429 | if (facet == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2430 | return (-1); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2431 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2432 | if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2433 | facet->type = XML_SCHEMA_FACET_MININCLUSIVE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2434 | } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2435 | facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2436 | } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2437 | facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2438 | } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2439 | facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2440 | } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2441 | facet->type = XML_SCHEMA_FACET_TOTALDIGITS; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2442 | } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2443 | facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2444 | } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2445 | facet->type = XML_SCHEMA_FACET_PATTERN; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2446 | } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2447 | facet->type = XML_SCHEMA_FACET_ENUMERATION; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2448 | } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2449 | facet->type = XML_SCHEMA_FACET_WHITESPACE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2450 | } else if (xmlStrEqual(facetname, BAD_CAST "length")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2451 | facet->type = XML_SCHEMA_FACET_LENGTH; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2452 | } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2453 | facet->type = XML_SCHEMA_FACET_MAXLENGTH; |
| 2454 | } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) { |
| 2455 | facet->type = XML_SCHEMA_FACET_MINLENGTH; |
| 2456 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2457 | xmlSchemaFreeFacet(facet); |
| 2458 | return (-1); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2459 | } |
| 2460 | facet->value = xmlStrdup(val); |
| 2461 | ret = xmlSchemaCheckFacet(facet, typ, NULL, type); |
| 2462 | if (ret != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2463 | xmlSchemaFreeFacet(facet); |
| 2464 | return (-1); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2465 | } |
| 2466 | ret = xmlSchemaValidateFacet(typ, facet, strval, value); |
| 2467 | xmlSchemaFreeFacet(facet); |
| 2468 | if (ret != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2469 | return (-1); |
| 2470 | return (0); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2471 | } |
| 2472 | |
| 2473 | /** |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2474 | * xmlRelaxNGSchemaFreeValue: |
| 2475 | * @data: data needed for the library |
| 2476 | * @value: the value to free |
| 2477 | * |
| 2478 | * Function provided by a type library to free a Schemas value |
| 2479 | * |
| 2480 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2481 | */ |
| 2482 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2483 | xmlRelaxNGSchemaFreeValue(void *data ATTRIBUTE_UNUSED, void *value) |
| 2484 | { |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2485 | xmlSchemaFreeValue(value); |
| 2486 | } |
| 2487 | |
| 2488 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2489 | * xmlRelaxNGSchemaTypeCompare: |
| 2490 | * @data: data needed for the library |
| 2491 | * @type: the type name |
| 2492 | * @value1: the first value |
| 2493 | * @value2: the second value |
| 2494 | * |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2495 | * Compare two values for equality accordingly a type from the W3C XMLSchema |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2496 | * Datatype library. |
| 2497 | * |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2498 | * 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] | 2499 | */ |
| 2500 | static int |
| 2501 | xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2502 | const xmlChar * type, |
| 2503 | const xmlChar * value1, |
| 2504 | xmlNodePtr ctxt1, |
| 2505 | void *comp1, |
| 2506 | const xmlChar * value2, xmlNodePtr ctxt2) |
| 2507 | { |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2508 | int ret; |
| 2509 | xmlSchemaTypePtr typ; |
| 2510 | xmlSchemaValPtr res1 = NULL, res2 = NULL; |
| 2511 | |
| 2512 | if ((type == NULL) || (value1 == NULL) || (value2 == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2513 | return (-1); |
| 2514 | typ = xmlSchemaGetPredefinedType(type, |
| 2515 | BAD_CAST |
| 2516 | "http://www.w3.org/2001/XMLSchema"); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2517 | if (typ == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2518 | return (-1); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2519 | if (comp1 == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2520 | ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1); |
| 2521 | if (ret != 0) |
| 2522 | return (-1); |
| 2523 | if (res1 == NULL) |
| 2524 | return (-1); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2525 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2526 | res1 = (xmlSchemaValPtr) comp1; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2527 | } |
| 2528 | ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2529 | if (ret != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2530 | xmlSchemaFreeValue(res1); |
| 2531 | return (-1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2532 | } |
| 2533 | if (res1 == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2534 | xmlSchemaFreeValue(res1); |
| 2535 | return (-1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2536 | } |
| 2537 | ret = xmlSchemaCompareValues(res1, res2); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2538 | if (res1 != (xmlSchemaValPtr) comp1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2539 | xmlSchemaFreeValue(res1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2540 | xmlSchemaFreeValue(res2); |
| 2541 | if (ret == -2) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2542 | return (-1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2543 | if (ret == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2544 | return (1); |
| 2545 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2546 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2547 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2548 | /** |
| 2549 | * xmlRelaxNGDefaultTypeHave: |
| 2550 | * @data: data needed for the library |
| 2551 | * @type: the type name |
| 2552 | * |
| 2553 | * Check if the given type is provided by |
| 2554 | * the default datatype library. |
| 2555 | * |
| 2556 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2557 | */ |
| 2558 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2559 | xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED, |
| 2560 | const xmlChar * type) |
| 2561 | { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2562 | if (type == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2563 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2564 | if (xmlStrEqual(type, BAD_CAST "string")) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2565 | return (1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2566 | if (xmlStrEqual(type, BAD_CAST "token")) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2567 | return (1); |
| 2568 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2569 | } |
| 2570 | |
| 2571 | /** |
| 2572 | * xmlRelaxNGDefaultTypeCheck: |
| 2573 | * @data: data needed for the library |
| 2574 | * @type: the type name |
| 2575 | * @value: the value to check |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2576 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2577 | * |
| 2578 | * Check if the given type and value are validated by |
| 2579 | * the default datatype library. |
| 2580 | * |
| 2581 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2582 | */ |
| 2583 | static int |
| 2584 | xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2585 | const xmlChar * type ATTRIBUTE_UNUSED, |
| 2586 | const xmlChar * value ATTRIBUTE_UNUSED, |
| 2587 | void **result ATTRIBUTE_UNUSED, |
| 2588 | xmlNodePtr node ATTRIBUTE_UNUSED) |
| 2589 | { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2590 | if (value == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2591 | return (-1); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2592 | if (xmlStrEqual(type, BAD_CAST "string")) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2593 | return (1); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2594 | if (xmlStrEqual(type, BAD_CAST "token")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2595 | return (1); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2596 | } |
| 2597 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2598 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2599 | } |
| 2600 | |
| 2601 | /** |
| 2602 | * xmlRelaxNGDefaultTypeCompare: |
| 2603 | * @data: data needed for the library |
| 2604 | * @type: the type name |
| 2605 | * @value1: the first value |
| 2606 | * @value2: the second value |
| 2607 | * |
| 2608 | * Compare two values accordingly a type from the default |
| 2609 | * datatype library. |
| 2610 | * |
| 2611 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2612 | */ |
| 2613 | static int |
| 2614 | xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2615 | const xmlChar * type, |
| 2616 | const xmlChar * value1, |
| 2617 | xmlNodePtr ctxt1 ATTRIBUTE_UNUSED, |
| 2618 | void *comp1 ATTRIBUTE_UNUSED, |
| 2619 | const xmlChar * value2, |
| 2620 | xmlNodePtr ctxt2 ATTRIBUTE_UNUSED) |
| 2621 | { |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2622 | int ret = -1; |
| 2623 | |
| 2624 | if (xmlStrEqual(type, BAD_CAST "string")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2625 | ret = xmlStrEqual(value1, value2); |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2626 | } else if (xmlStrEqual(type, BAD_CAST "token")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2627 | if (!xmlStrEqual(value1, value2)) { |
| 2628 | xmlChar *nval, *nvalue; |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2629 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2630 | /* |
| 2631 | * TODO: trivial optimizations are possible by |
| 2632 | * computing at compile-time |
| 2633 | */ |
| 2634 | nval = xmlRelaxNGNormalize(NULL, value1); |
| 2635 | nvalue = xmlRelaxNGNormalize(NULL, value2); |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2636 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2637 | if ((nval == NULL) || (nvalue == NULL)) |
| 2638 | ret = -1; |
| 2639 | else if (xmlStrEqual(nval, nvalue)) |
| 2640 | ret = 1; |
| 2641 | else |
| 2642 | ret = 0; |
| 2643 | if (nval != NULL) |
| 2644 | xmlFree(nval); |
| 2645 | if (nvalue != NULL) |
| 2646 | xmlFree(nvalue); |
| 2647 | } else |
| 2648 | ret = 1; |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2649 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2650 | return (ret); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2651 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2652 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2653 | static int xmlRelaxNGTypeInitialized = 0; |
| 2654 | static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL; |
| 2655 | |
| 2656 | /** |
| 2657 | * xmlRelaxNGFreeTypeLibrary: |
| 2658 | * @lib: the type library structure |
| 2659 | * @namespace: the URI bound to the library |
| 2660 | * |
| 2661 | * Free the structure associated to the type library |
| 2662 | */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2663 | static void |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2664 | xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2665 | const xmlChar * namespace ATTRIBUTE_UNUSED) |
| 2666 | { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2667 | if (lib == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2668 | return; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2669 | if (lib->namespace != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2670 | xmlFree((xmlChar *) lib->namespace); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2671 | xmlFree(lib); |
| 2672 | } |
| 2673 | |
| 2674 | /** |
| 2675 | * xmlRelaxNGRegisterTypeLibrary: |
| 2676 | * @namespace: the URI bound to the library |
| 2677 | * @data: data associated to the library |
| 2678 | * @have: the provide function |
| 2679 | * @check: the checking function |
| 2680 | * @comp: the comparison function |
| 2681 | * |
| 2682 | * Register a new type library |
| 2683 | * |
| 2684 | * Returns 0 in case of success and -1 in case of error. |
| 2685 | */ |
| 2686 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2687 | xmlRelaxNGRegisterTypeLibrary(const xmlChar * namespace, void *data, |
| 2688 | xmlRelaxNGTypeHave have, |
| 2689 | xmlRelaxNGTypeCheck check, |
| 2690 | xmlRelaxNGTypeCompare comp, |
| 2691 | xmlRelaxNGFacetCheck facet, |
| 2692 | xmlRelaxNGTypeFree freef) |
| 2693 | { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2694 | xmlRelaxNGTypeLibraryPtr lib; |
| 2695 | int ret; |
| 2696 | |
| 2697 | if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2698 | (check == NULL) || (comp == NULL)) |
| 2699 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2700 | if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2701 | xmlGenericError(xmlGenericErrorContext, |
| 2702 | "Relax-NG types library '%s' already registered\n", |
| 2703 | namespace); |
| 2704 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2705 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2706 | lib = |
| 2707 | (xmlRelaxNGTypeLibraryPtr) |
| 2708 | xmlMalloc(sizeof(xmlRelaxNGTypeLibrary)); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2709 | if (lib == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2710 | xmlRngVErrMemory(NULL, "adding types library\n"); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2711 | return (-1); |
| 2712 | } |
| 2713 | memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary)); |
| 2714 | lib->namespace = xmlStrdup(namespace); |
| 2715 | lib->data = data; |
| 2716 | lib->have = have; |
| 2717 | lib->comp = comp; |
| 2718 | lib->check = check; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2719 | lib->facet = facet; |
| 2720 | lib->freef = freef; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2721 | ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib); |
| 2722 | if (ret < 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2723 | xmlGenericError(xmlGenericErrorContext, |
| 2724 | "Relax-NG types library failed to register '%s'\n", |
| 2725 | namespace); |
| 2726 | xmlRelaxNGFreeTypeLibrary(lib, namespace); |
| 2727 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2728 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2729 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2730 | } |
| 2731 | |
| 2732 | /** |
| 2733 | * xmlRelaxNGInitTypes: |
| 2734 | * |
| 2735 | * Initilize the default type libraries. |
| 2736 | * |
| 2737 | * Returns 0 in case of success and -1 in case of error. |
| 2738 | */ |
| 2739 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2740 | xmlRelaxNGInitTypes(void) |
| 2741 | { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2742 | if (xmlRelaxNGTypeInitialized != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2743 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2744 | xmlRelaxNGRegisteredTypes = xmlHashCreate(10); |
| 2745 | if (xmlRelaxNGRegisteredTypes == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2746 | xmlGenericError(xmlGenericErrorContext, |
| 2747 | "Failed to allocate sh table for Relax-NG types\n"); |
| 2748 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2749 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2750 | xmlRelaxNGRegisterTypeLibrary(BAD_CAST |
| 2751 | "http://www.w3.org/2001/XMLSchema-datatypes", |
| 2752 | NULL, xmlRelaxNGSchemaTypeHave, |
| 2753 | xmlRelaxNGSchemaTypeCheck, |
| 2754 | xmlRelaxNGSchemaTypeCompare, |
| 2755 | xmlRelaxNGSchemaFacetCheck, |
| 2756 | xmlRelaxNGSchemaFreeValue); |
| 2757 | xmlRelaxNGRegisterTypeLibrary(xmlRelaxNGNs, NULL, |
| 2758 | xmlRelaxNGDefaultTypeHave, |
| 2759 | xmlRelaxNGDefaultTypeCheck, |
| 2760 | xmlRelaxNGDefaultTypeCompare, NULL, |
| 2761 | NULL); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2762 | xmlRelaxNGTypeInitialized = 1; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2763 | return (0); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2764 | } |
| 2765 | |
| 2766 | /** |
| 2767 | * xmlRelaxNGCleanupTypes: |
| 2768 | * |
| 2769 | * Cleanup the default Schemas type library associated to RelaxNG |
| 2770 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2771 | void |
| 2772 | xmlRelaxNGCleanupTypes(void) |
| 2773 | { |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 2774 | xmlSchemaCleanupTypes(); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2775 | if (xmlRelaxNGTypeInitialized == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2776 | return; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2777 | xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2778 | xmlRelaxNGFreeTypeLibrary); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2779 | xmlRelaxNGTypeInitialized = 0; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2780 | } |
| 2781 | |
| 2782 | /************************************************************************ |
| 2783 | * * |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2784 | * Compiling element content into regexp * |
| 2785 | * * |
| 2786 | * Sometime the element content can be compiled into a pure regexp, * |
| 2787 | * This allows a faster execution and streamability at that level * |
| 2788 | * * |
| 2789 | ************************************************************************/ |
| 2790 | |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2791 | static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, |
| 2792 | xmlRelaxNGDefinePtr def); |
| 2793 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2794 | /** |
| 2795 | * xmlRelaxNGIsCompileable: |
| 2796 | * @define: the definition to check |
| 2797 | * |
| 2798 | * Check if a definition is nullable. |
| 2799 | * |
| 2800 | * Returns 1 if yes, 0 if no and -1 in case of error |
| 2801 | */ |
| 2802 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2803 | xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def) |
| 2804 | { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2805 | int ret = -1; |
| 2806 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2807 | if (def == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2808 | return (-1); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2809 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2810 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 2811 | (def->dflags & IS_COMPILABLE)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2812 | return (1); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2813 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 2814 | (def->dflags & IS_NOT_COMPILABLE)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2815 | return (0); |
| 2816 | switch (def->type) { |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2817 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2818 | ret = xmlRelaxNGIsCompileable(def->content); |
| 2819 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2820 | case XML_RELAXNG_TEXT: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2821 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2822 | ret = 1; |
| 2823 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2824 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2825 | /* |
| 2826 | * Check if the element content is compileable |
| 2827 | */ |
| 2828 | if (((def->dflags & IS_NOT_COMPILABLE) == 0) && |
| 2829 | ((def->dflags & IS_COMPILABLE) == 0)) { |
| 2830 | xmlRelaxNGDefinePtr list; |
| 2831 | |
| 2832 | list = def->content; |
| 2833 | while (list != NULL) { |
| 2834 | ret = xmlRelaxNGIsCompileable(list); |
| 2835 | if (ret != 1) |
| 2836 | break; |
| 2837 | list = list->next; |
| 2838 | } |
| 2839 | if (ret == 0) |
| 2840 | def->dflags |= IS_NOT_COMPILABLE; |
| 2841 | if (ret == 1) |
| 2842 | def->dflags |= IS_COMPILABLE; |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2843 | #ifdef DEBUG_COMPILE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2844 | if (ret == 1) { |
| 2845 | xmlGenericError(xmlGenericErrorContext, |
| 2846 | "element content for %s is compilable\n", |
| 2847 | def->name); |
| 2848 | } else if (ret == 0) { |
| 2849 | xmlGenericError(xmlGenericErrorContext, |
| 2850 | "element content for %s is not compilable\n", |
| 2851 | def->name); |
| 2852 | } else { |
| 2853 | xmlGenericError(xmlGenericErrorContext, |
| 2854 | "Problem in RelaxNGIsCompileable for element %s\n", |
| 2855 | def->name); |
| 2856 | } |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2857 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2858 | } |
| 2859 | /* |
| 2860 | * All elements return a compileable status unless they |
| 2861 | * are generic like anyName |
| 2862 | */ |
| 2863 | if ((def->nameClass != NULL) || (def->name == NULL)) |
| 2864 | ret = 0; |
| 2865 | else |
| 2866 | ret = 1; |
| 2867 | return (ret); |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2868 | case XML_RELAXNG_REF: |
| 2869 | case XML_RELAXNG_EXTERNALREF: |
| 2870 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2871 | if (def->depth == -20) { |
| 2872 | return (1); |
| 2873 | } else { |
| 2874 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2875 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2876 | def->depth = -20; |
| 2877 | list = def->content; |
| 2878 | while (list != NULL) { |
| 2879 | ret = xmlRelaxNGIsCompileable(list); |
| 2880 | if (ret != 1) |
| 2881 | break; |
| 2882 | list = list->next; |
| 2883 | } |
| 2884 | } |
| 2885 | break; |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2886 | case XML_RELAXNG_START: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2887 | case XML_RELAXNG_OPTIONAL: |
| 2888 | case XML_RELAXNG_ZEROORMORE: |
| 2889 | case XML_RELAXNG_ONEORMORE: |
| 2890 | case XML_RELAXNG_CHOICE: |
| 2891 | case XML_RELAXNG_GROUP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2892 | case XML_RELAXNG_DEF:{ |
| 2893 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2894 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2895 | list = def->content; |
| 2896 | while (list != NULL) { |
| 2897 | ret = xmlRelaxNGIsCompileable(list); |
| 2898 | if (ret != 1) |
| 2899 | break; |
| 2900 | list = list->next; |
| 2901 | } |
| 2902 | break; |
| 2903 | } |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2904 | case XML_RELAXNG_EXCEPT: |
| 2905 | case XML_RELAXNG_ATTRIBUTE: |
| 2906 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2907 | case XML_RELAXNG_DATATYPE: |
| 2908 | case XML_RELAXNG_LIST: |
| 2909 | case XML_RELAXNG_PARAM: |
| 2910 | case XML_RELAXNG_VALUE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2911 | ret = 0; |
| 2912 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2913 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2914 | ret = -1; |
| 2915 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2916 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2917 | if (ret == 0) |
| 2918 | def->dflags |= IS_NOT_COMPILABLE; |
| 2919 | if (ret == 1) |
| 2920 | def->dflags |= IS_COMPILABLE; |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2921 | #ifdef DEBUG_COMPILE |
| 2922 | if (ret == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2923 | xmlGenericError(xmlGenericErrorContext, |
| 2924 | "RelaxNGIsCompileable %s : true\n", |
| 2925 | xmlRelaxNGDefName(def)); |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2926 | } else if (ret == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2927 | xmlGenericError(xmlGenericErrorContext, |
| 2928 | "RelaxNGIsCompileable %s : false\n", |
| 2929 | xmlRelaxNGDefName(def)); |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2930 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2931 | xmlGenericError(xmlGenericErrorContext, |
| 2932 | "Problem in RelaxNGIsCompileable %s\n", |
| 2933 | xmlRelaxNGDefName(def)); |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2934 | } |
| 2935 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2936 | return (ret); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2937 | } |
| 2938 | |
| 2939 | /** |
| 2940 | * xmlRelaxNGCompile: |
| 2941 | * ctxt: the RelaxNG parser context |
| 2942 | * @define: the definition tree to compile |
| 2943 | * |
| 2944 | * Compile the set of definitions, it works recursively, till the |
| 2945 | * element boundaries, where it tries to compile the content if possible |
| 2946 | * |
| 2947 | * Returns 0 if success and -1 in case of error |
| 2948 | */ |
| 2949 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2950 | xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) |
| 2951 | { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2952 | int ret = 0; |
| 2953 | xmlRelaxNGDefinePtr list; |
| 2954 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2955 | if ((ctxt == NULL) || (def == NULL)) |
| 2956 | return (-1); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2957 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2958 | switch (def->type) { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2959 | case XML_RELAXNG_START: |
| 2960 | if ((xmlRelaxNGIsCompileable(def) == 1) && (def->depth != -25)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2961 | xmlAutomataPtr oldam = ctxt->am; |
| 2962 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2963 | |
| 2964 | def->depth = -25; |
| 2965 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2966 | list = def->content; |
| 2967 | ctxt->am = xmlNewAutomata(); |
| 2968 | if (ctxt->am == NULL) |
| 2969 | return (-1); |
| 2970 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
| 2971 | while (list != NULL) { |
| 2972 | xmlRelaxNGCompile(ctxt, list); |
| 2973 | list = list->next; |
| 2974 | } |
| 2975 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
| 2976 | def->contModel = xmlAutomataCompile(ctxt->am); |
| 2977 | xmlRegexpIsDeterminist(def->contModel); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2978 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2979 | xmlFreeAutomata(ctxt->am); |
| 2980 | ctxt->state = oldstate; |
| 2981 | ctxt->am = oldam; |
| 2982 | } |
| 2983 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2984 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2985 | if ((ctxt->am != NULL) && (def->name != NULL)) { |
| 2986 | ctxt->state = xmlAutomataNewTransition2(ctxt->am, |
| 2987 | ctxt->state, NULL, |
| 2988 | def->name, def->ns, |
| 2989 | def); |
| 2990 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2991 | if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2992 | xmlAutomataPtr oldam = ctxt->am; |
| 2993 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2994 | |
| 2995 | def->depth = -25; |
| 2996 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2997 | list = def->content; |
| 2998 | ctxt->am = xmlNewAutomata(); |
| 2999 | if (ctxt->am == NULL) |
| 3000 | return (-1); |
| 3001 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
| 3002 | while (list != NULL) { |
| 3003 | xmlRelaxNGCompile(ctxt, list); |
| 3004 | list = list->next; |
| 3005 | } |
| 3006 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
| 3007 | def->contModel = xmlAutomataCompile(ctxt->am); |
| 3008 | if (!xmlRegexpIsDeterminist(def->contModel)) { |
| 3009 | /* |
| 3010 | * we can only use the automata if it is determinist |
| 3011 | */ |
| 3012 | xmlRegFreeRegexp(def->contModel); |
| 3013 | def->contModel = NULL; |
| 3014 | } |
| 3015 | xmlFreeAutomata(ctxt->am); |
| 3016 | ctxt->state = oldstate; |
| 3017 | ctxt->am = oldam; |
| 3018 | } else { |
| 3019 | xmlAutomataPtr oldam = ctxt->am; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3020 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3021 | /* |
| 3022 | * we can't build the content model for this element content |
| 3023 | * but it still might be possible to build it for some of its |
| 3024 | * children, recurse. |
| 3025 | */ |
| 3026 | ret = xmlRelaxNGTryCompile(ctxt, def); |
| 3027 | ctxt->am = oldam; |
| 3028 | } |
| 3029 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3030 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3031 | ret = xmlRelaxNGCompile(ctxt, def->content); |
| 3032 | break; |
| 3033 | case XML_RELAXNG_OPTIONAL:{ |
| 3034 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3035 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3036 | xmlRelaxNGCompile(ctxt, def->content); |
| 3037 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
| 3038 | break; |
| 3039 | } |
| 3040 | case XML_RELAXNG_ZEROORMORE:{ |
| 3041 | xmlAutomataStatePtr oldstate; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3042 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3043 | ctxt->state = |
| 3044 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 3045 | oldstate = ctxt->state; |
| 3046 | list = def->content; |
| 3047 | while (list != NULL) { |
| 3048 | xmlRelaxNGCompile(ctxt, list); |
| 3049 | list = list->next; |
| 3050 | } |
| 3051 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate); |
| 3052 | ctxt->state = |
| 3053 | xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 3054 | break; |
| 3055 | } |
| 3056 | case XML_RELAXNG_ONEORMORE:{ |
| 3057 | xmlAutomataStatePtr oldstate; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3058 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3059 | list = def->content; |
| 3060 | while (list != NULL) { |
| 3061 | xmlRelaxNGCompile(ctxt, list); |
| 3062 | list = list->next; |
| 3063 | } |
| 3064 | oldstate = ctxt->state; |
| 3065 | list = def->content; |
| 3066 | while (list != NULL) { |
| 3067 | xmlRelaxNGCompile(ctxt, list); |
| 3068 | list = list->next; |
| 3069 | } |
| 3070 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate); |
| 3071 | ctxt->state = |
| 3072 | xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 3073 | break; |
| 3074 | } |
| 3075 | case XML_RELAXNG_CHOICE:{ |
| 3076 | xmlAutomataStatePtr target = NULL; |
| 3077 | xmlAutomataStatePtr oldstate = ctxt->state; |
| 3078 | |
| 3079 | list = def->content; |
| 3080 | while (list != NULL) { |
| 3081 | ctxt->state = oldstate; |
| 3082 | ret = xmlRelaxNGCompile(ctxt, list); |
| 3083 | if (ret != 0) |
| 3084 | break; |
| 3085 | if (target == NULL) |
| 3086 | target = ctxt->state; |
| 3087 | else { |
| 3088 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, |
| 3089 | target); |
| 3090 | } |
| 3091 | list = list->next; |
| 3092 | } |
| 3093 | ctxt->state = target; |
| 3094 | |
| 3095 | break; |
| 3096 | } |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3097 | case XML_RELAXNG_REF: |
| 3098 | case XML_RELAXNG_EXTERNALREF: |
| 3099 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3100 | case XML_RELAXNG_GROUP: |
| 3101 | case XML_RELAXNG_DEF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3102 | list = def->content; |
| 3103 | while (list != NULL) { |
| 3104 | ret = xmlRelaxNGCompile(ctxt, list); |
| 3105 | if (ret != 0) |
| 3106 | break; |
| 3107 | list = list->next; |
| 3108 | } |
| 3109 | break; |
| 3110 | case XML_RELAXNG_TEXT:{ |
| 3111 | xmlAutomataStatePtr oldstate; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3112 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3113 | ctxt->state = |
| 3114 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 3115 | oldstate = ctxt->state; |
| 3116 | xmlRelaxNGCompile(ctxt, def->content); |
| 3117 | xmlAutomataNewTransition(ctxt->am, ctxt->state, |
| 3118 | ctxt->state, BAD_CAST "#text", |
| 3119 | NULL); |
| 3120 | ctxt->state = |
| 3121 | xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 3122 | break; |
| 3123 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3124 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3125 | ctxt->state = |
| 3126 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 3127 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3128 | case XML_RELAXNG_EXCEPT: |
| 3129 | case XML_RELAXNG_ATTRIBUTE: |
| 3130 | case XML_RELAXNG_INTERLEAVE: |
| 3131 | case XML_RELAXNG_NOT_ALLOWED: |
| 3132 | case XML_RELAXNG_DATATYPE: |
| 3133 | case XML_RELAXNG_LIST: |
| 3134 | case XML_RELAXNG_PARAM: |
| 3135 | case XML_RELAXNG_VALUE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3136 | /* This should not happen and generate an internal error */ |
| 3137 | fprintf(stderr, "RNG internal error trying to compile %s\n", |
| 3138 | xmlRelaxNGDefName(def)); |
| 3139 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3140 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3141 | return (ret); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3142 | } |
| 3143 | |
| 3144 | /** |
| 3145 | * xmlRelaxNGTryCompile: |
| 3146 | * ctxt: the RelaxNG parser context |
| 3147 | * @define: the definition tree to compile |
| 3148 | * |
| 3149 | * Try to compile the set of definitions, it works recursively, |
| 3150 | * possibly ignoring parts which cannot be compiled. |
| 3151 | * |
| 3152 | * Returns 0 if success and -1 in case of error |
| 3153 | */ |
| 3154 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3155 | xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) |
| 3156 | { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3157 | int ret = 0; |
| 3158 | xmlRelaxNGDefinePtr list; |
| 3159 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3160 | if ((ctxt == NULL) || (def == NULL)) |
| 3161 | return (-1); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3162 | |
| 3163 | if ((def->type == XML_RELAXNG_START) || |
| 3164 | (def->type == XML_RELAXNG_ELEMENT)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3165 | ret = xmlRelaxNGIsCompileable(def); |
| 3166 | if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) { |
| 3167 | ctxt->am = NULL; |
| 3168 | ret = xmlRelaxNGCompile(ctxt, def); |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3169 | #ifdef DEBUG_PROGRESSIVE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3170 | if (ret == 0) { |
| 3171 | if (def->type == XML_RELAXNG_START) |
| 3172 | xmlGenericError(xmlGenericErrorContext, |
| 3173 | "compiled the start\n"); |
| 3174 | else |
| 3175 | xmlGenericError(xmlGenericErrorContext, |
| 3176 | "compiled element %s\n", def->name); |
| 3177 | } else { |
| 3178 | if (def->type == XML_RELAXNG_START) |
| 3179 | xmlGenericError(xmlGenericErrorContext, |
| 3180 | "failed to compile the start\n"); |
| 3181 | else |
| 3182 | xmlGenericError(xmlGenericErrorContext, |
| 3183 | "failed to compile element %s\n", |
| 3184 | def->name); |
| 3185 | } |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3186 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3187 | return (ret); |
| 3188 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3189 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3190 | switch (def->type) { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3191 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3192 | ret = xmlRelaxNGTryCompile(ctxt, def->content); |
| 3193 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3194 | case XML_RELAXNG_TEXT: |
| 3195 | case XML_RELAXNG_DATATYPE: |
| 3196 | case XML_RELAXNG_LIST: |
| 3197 | case XML_RELAXNG_PARAM: |
| 3198 | case XML_RELAXNG_VALUE: |
| 3199 | case XML_RELAXNG_EMPTY: |
| 3200 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3201 | ret = 0; |
| 3202 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3203 | case XML_RELAXNG_OPTIONAL: |
| 3204 | case XML_RELAXNG_ZEROORMORE: |
| 3205 | case XML_RELAXNG_ONEORMORE: |
| 3206 | case XML_RELAXNG_CHOICE: |
| 3207 | case XML_RELAXNG_GROUP: |
| 3208 | case XML_RELAXNG_DEF: |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3209 | case XML_RELAXNG_START: |
| 3210 | case XML_RELAXNG_REF: |
| 3211 | case XML_RELAXNG_EXTERNALREF: |
| 3212 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3213 | list = def->content; |
| 3214 | while (list != NULL) { |
| 3215 | ret = xmlRelaxNGTryCompile(ctxt, list); |
| 3216 | if (ret != 0) |
| 3217 | break; |
| 3218 | list = list->next; |
| 3219 | } |
| 3220 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3221 | case XML_RELAXNG_EXCEPT: |
| 3222 | case XML_RELAXNG_ATTRIBUTE: |
| 3223 | case XML_RELAXNG_INTERLEAVE: |
| 3224 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3225 | ret = 0; |
| 3226 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3227 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3228 | return (ret); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 3229 | } |
| 3230 | |
| 3231 | /************************************************************************ |
| 3232 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3233 | * Parsing functions * |
| 3234 | * * |
| 3235 | ************************************************************************/ |
| 3236 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3237 | static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr |
| 3238 | ctxt, xmlNodePtr node); |
| 3239 | static xmlRelaxNGDefinePtr xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr |
| 3240 | ctxt, xmlNodePtr node); |
| 3241 | static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr |
| 3242 | ctxt, xmlNodePtr nodes, |
| 3243 | int group); |
| 3244 | static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr |
| 3245 | ctxt, xmlNodePtr node); |
| 3246 | static xmlRelaxNGPtr xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, |
| 3247 | xmlNodePtr node); |
| 3248 | static int xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt, |
| 3249 | xmlNodePtr nodes); |
| 3250 | static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr |
| 3251 | ctxt, xmlNodePtr node, |
| 3252 | xmlRelaxNGDefinePtr |
| 3253 | def); |
| 3254 | static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr |
| 3255 | ctxt, xmlNodePtr nodes); |
| 3256 | static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 3257 | xmlRelaxNGDefinePtr define, |
| 3258 | xmlNodePtr elem); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3259 | |
| 3260 | |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 3261 | #define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content)) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3262 | |
| 3263 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3264 | * xmlRelaxNGIsNullable: |
| 3265 | * @define: the definition to verify |
| 3266 | * |
| 3267 | * Check if a definition is nullable. |
| 3268 | * |
| 3269 | * Returns 1 if yes, 0 if no and -1 in case of error |
| 3270 | */ |
| 3271 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3272 | xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define) |
| 3273 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3274 | int ret; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3275 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3276 | if (define == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3277 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3278 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3279 | if (define->dflags & IS_NULLABLE) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3280 | return (1); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3281 | if (define->dflags & IS_NOT_NULLABLE) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3282 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3283 | switch (define->type) { |
| 3284 | case XML_RELAXNG_EMPTY: |
| 3285 | case XML_RELAXNG_TEXT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3286 | ret = 1; |
| 3287 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3288 | case XML_RELAXNG_NOOP: |
| 3289 | case XML_RELAXNG_DEF: |
| 3290 | case XML_RELAXNG_REF: |
| 3291 | case XML_RELAXNG_EXTERNALREF: |
| 3292 | case XML_RELAXNG_PARENTREF: |
| 3293 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3294 | ret = xmlRelaxNGIsNullable(define->content); |
| 3295 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3296 | case XML_RELAXNG_EXCEPT: |
| 3297 | case XML_RELAXNG_NOT_ALLOWED: |
| 3298 | case XML_RELAXNG_ELEMENT: |
| 3299 | case XML_RELAXNG_DATATYPE: |
| 3300 | case XML_RELAXNG_PARAM: |
| 3301 | case XML_RELAXNG_VALUE: |
| 3302 | case XML_RELAXNG_LIST: |
| 3303 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3304 | ret = 0; |
| 3305 | break; |
| 3306 | case XML_RELAXNG_CHOICE:{ |
| 3307 | xmlRelaxNGDefinePtr list = define->content; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3308 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3309 | while (list != NULL) { |
| 3310 | ret = xmlRelaxNGIsNullable(list); |
| 3311 | if (ret != 0) |
| 3312 | goto done; |
| 3313 | list = list->next; |
| 3314 | } |
| 3315 | ret = 0; |
| 3316 | break; |
| 3317 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3318 | case XML_RELAXNG_START: |
| 3319 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3320 | case XML_RELAXNG_GROUP:{ |
| 3321 | xmlRelaxNGDefinePtr list = define->content; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3322 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3323 | while (list != NULL) { |
| 3324 | ret = xmlRelaxNGIsNullable(list); |
| 3325 | if (ret != 1) |
| 3326 | goto done; |
| 3327 | list = list->next; |
| 3328 | } |
| 3329 | return (1); |
| 3330 | } |
| 3331 | default: |
| 3332 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3333 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3334 | done: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3335 | if (ret == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3336 | define->dflags |= IS_NOT_NULLABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3337 | if (ret == 1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3338 | define->dflags |= IS_NULLABLE; |
| 3339 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3340 | } |
| 3341 | |
| 3342 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3343 | * xmlRelaxNGIsBlank: |
| 3344 | * @str: a string |
| 3345 | * |
| 3346 | * Check if a string is ignorable c.f. 4.2. Whitespace |
| 3347 | * |
| 3348 | * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise |
| 3349 | */ |
| 3350 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3351 | xmlRelaxNGIsBlank(xmlChar * str) |
| 3352 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3353 | if (str == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3354 | return (1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3355 | while (*str != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3356 | if (!(IS_BLANK(*str))) |
| 3357 | return (0); |
| 3358 | str++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3359 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3360 | return (1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3361 | } |
| 3362 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3363 | /** |
| 3364 | * xmlRelaxNGGetDataTypeLibrary: |
| 3365 | * @ctxt: a Relax-NG parser context |
| 3366 | * @node: the current data or value element |
| 3367 | * |
| 3368 | * Applies algorithm from 4.3. datatypeLibrary attribute |
| 3369 | * |
| 3370 | * Returns the datatypeLibary value or NULL if not found |
| 3371 | */ |
| 3372 | static xmlChar * |
| 3373 | xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3374 | xmlNodePtr node) |
| 3375 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3376 | xmlChar *ret, *escape; |
| 3377 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3378 | if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3379 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
| 3380 | if (ret != NULL) { |
| 3381 | if (ret[0] == 0) { |
| 3382 | xmlFree(ret); |
| 3383 | return (NULL); |
| 3384 | } |
| 3385 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
| 3386 | if (escape == NULL) { |
| 3387 | return (ret); |
| 3388 | } |
| 3389 | xmlFree(ret); |
| 3390 | return (escape); |
| 3391 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3392 | } |
| 3393 | node = node->parent; |
| 3394 | while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3395 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
| 3396 | if (ret != NULL) { |
| 3397 | if (ret[0] == 0) { |
| 3398 | xmlFree(ret); |
| 3399 | return (NULL); |
| 3400 | } |
| 3401 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
| 3402 | if (escape == NULL) { |
| 3403 | return (ret); |
| 3404 | } |
| 3405 | xmlFree(ret); |
| 3406 | return (escape); |
| 3407 | } |
| 3408 | node = node->parent; |
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 (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3411 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3412 | |
| 3413 | /** |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3414 | * xmlRelaxNGParseValue: |
| 3415 | * @ctxt: a Relax-NG parser context |
| 3416 | * @node: the data node. |
| 3417 | * |
| 3418 | * parse the content of a RelaxNG value node. |
| 3419 | * |
| 3420 | * Returns the definition pointer or NULL in case of error |
| 3421 | */ |
| 3422 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3423 | xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 3424 | { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3425 | xmlRelaxNGDefinePtr def = NULL; |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 3426 | xmlRelaxNGTypeLibraryPtr lib = NULL; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3427 | xmlChar *type; |
| 3428 | xmlChar *library; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3429 | int success = 0; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3430 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3431 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3432 | if (def == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3433 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3434 | def->type = XML_RELAXNG_VALUE; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3435 | |
| 3436 | type = xmlGetProp(node, BAD_CAST "type"); |
| 3437 | if (type != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3438 | xmlRelaxNGNormExtSpace(type); |
| 3439 | if (xmlValidateNCName(type, 0)) { |
| 3440 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE, |
| 3441 | "value type '%s' is not an NCName\n", type, NULL); |
| 3442 | } |
| 3443 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
| 3444 | if (library == NULL) |
| 3445 | library = |
| 3446 | xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3447 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3448 | def->name = type; |
| 3449 | def->ns = library; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3450 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3451 | lib = (xmlRelaxNGTypeLibraryPtr) |
| 3452 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
| 3453 | if (lib == NULL) { |
| 3454 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB, |
| 3455 | "Use of unregistered type library '%s'\n", library, |
| 3456 | NULL); |
| 3457 | def->data = NULL; |
| 3458 | } else { |
| 3459 | def->data = lib; |
| 3460 | if (lib->have == NULL) { |
| 3461 | xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB, |
| 3462 | "Internal error with type library '%s': no 'have'\n", |
| 3463 | library, NULL); |
| 3464 | } else { |
| 3465 | success = lib->have(lib->data, def->name); |
| 3466 | if (success != 1) { |
| 3467 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND, |
| 3468 | "Error type '%s' is not exported by type library '%s'\n", |
| 3469 | def->name, library); |
| 3470 | } |
| 3471 | } |
| 3472 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3473 | } |
| 3474 | if (node->children == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3475 | def->value = xmlStrdup(BAD_CAST ""); |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 3476 | } else if (((node->children->type != XML_TEXT_NODE) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3477 | (node->children->type != XML_CDATA_SECTION_NODE)) || |
| 3478 | (node->children->next != NULL)) { |
| 3479 | xmlRngPErr(ctxt, node, XML_RNGP_TEXT_EXPECTED, |
| 3480 | "Expecting a single text value for <value>content\n", |
| 3481 | NULL, NULL); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3482 | } else if (def != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3483 | def->value = xmlNodeGetContent(node); |
| 3484 | if (def->value == NULL) { |
| 3485 | xmlRngPErr(ctxt, node, XML_RNGP_VALUE_NO_CONTENT, |
| 3486 | "Element <value> has no content\n", NULL, NULL); |
| 3487 | } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) { |
| 3488 | void *val = NULL; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3489 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3490 | success = |
| 3491 | lib->check(lib->data, def->name, def->value, &val, node); |
| 3492 | if (success != 1) { |
| 3493 | xmlRngPErr(ctxt, node, XML_RNGP_INVALID_VALUE, |
| 3494 | "Value '%s' is not acceptable for type '%s'\n", |
| 3495 | def->value, def->name); |
| 3496 | } else { |
| 3497 | if (val != NULL) |
| 3498 | def->attrs = val; |
| 3499 | } |
| 3500 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3501 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3502 | return (def); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3503 | } |
| 3504 | |
| 3505 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3506 | * xmlRelaxNGParseData: |
| 3507 | * @ctxt: a Relax-NG parser context |
| 3508 | * @node: the data node. |
| 3509 | * |
| 3510 | * parse the content of a RelaxNG data node. |
| 3511 | * |
| 3512 | * Returns the definition pointer or NULL in case of error |
| 3513 | */ |
| 3514 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3515 | xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 3516 | { |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3517 | xmlRelaxNGDefinePtr def = NULL, except, last = NULL; |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 3518 | xmlRelaxNGDefinePtr param, lastparam = NULL; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3519 | xmlRelaxNGTypeLibraryPtr lib; |
| 3520 | xmlChar *type; |
| 3521 | xmlChar *library; |
| 3522 | xmlNodePtr content; |
| 3523 | int tmp; |
| 3524 | |
| 3525 | type = xmlGetProp(node, BAD_CAST "type"); |
| 3526 | if (type == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3527 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_MISSING, "data has no type\n", NULL, |
| 3528 | NULL); |
| 3529 | return (NULL); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3530 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3531 | xmlRelaxNGNormExtSpace(type); |
| 3532 | if (xmlValidateNCName(type, 0)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3533 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE, |
| 3534 | "data type '%s' is not an NCName\n", type, NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3535 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3536 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
| 3537 | if (library == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3538 | library = |
| 3539 | xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3540 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3541 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3542 | if (def == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3543 | xmlFree(type); |
| 3544 | return (NULL); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3545 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3546 | def->type = XML_RELAXNG_DATATYPE; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3547 | def->name = type; |
| 3548 | def->ns = library; |
| 3549 | |
| 3550 | lib = (xmlRelaxNGTypeLibraryPtr) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3551 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3552 | if (lib == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3553 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB, |
| 3554 | "Use of unregistered type library '%s'\n", library, |
| 3555 | NULL); |
| 3556 | def->data = NULL; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3557 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3558 | def->data = lib; |
| 3559 | if (lib->have == NULL) { |
| 3560 | xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB, |
| 3561 | "Internal error with type library '%s': no 'have'\n", |
| 3562 | library, NULL); |
| 3563 | } else { |
| 3564 | tmp = lib->have(lib->data, def->name); |
| 3565 | if (tmp != 1) { |
| 3566 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND, |
| 3567 | "Error type '%s' is not exported by type library '%s'\n", |
| 3568 | def->name, library); |
| 3569 | } else |
| 3570 | if ((xmlStrEqual |
| 3571 | (library, |
| 3572 | BAD_CAST |
| 3573 | "http://www.w3.org/2001/XMLSchema-datatypes")) |
| 3574 | && ((xmlStrEqual(def->name, BAD_CAST "IDREF")) |
| 3575 | || (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) { |
| 3576 | ctxt->idref = 1; |
| 3577 | } |
| 3578 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3579 | } |
| 3580 | content = node->children; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3581 | |
| 3582 | /* |
| 3583 | * Handle optional params |
| 3584 | */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3585 | while (content != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3586 | if (!xmlStrEqual(content->name, BAD_CAST "param")) |
| 3587 | break; |
| 3588 | if (xmlStrEqual(library, |
| 3589 | BAD_CAST "http://relaxng.org/ns/structure/1.0")) { |
| 3590 | xmlRngPErr(ctxt, node, XML_RNGP_PARAM_FORBIDDEN, |
| 3591 | "Type library '%s' does not allow type parameters\n", |
| 3592 | library, NULL); |
| 3593 | content = content->next; |
| 3594 | while ((content != NULL) && |
| 3595 | (xmlStrEqual(content->name, BAD_CAST "param"))) |
| 3596 | content = content->next; |
| 3597 | } else { |
| 3598 | param = xmlRelaxNGNewDefine(ctxt, node); |
| 3599 | if (param != NULL) { |
| 3600 | param->type = XML_RELAXNG_PARAM; |
| 3601 | param->name = xmlGetProp(content, BAD_CAST "name"); |
| 3602 | if (param->name == NULL) { |
| 3603 | xmlRngPErr(ctxt, node, XML_RNGP_PARAM_NAME_MISSING, |
| 3604 | "param has no name\n", NULL, NULL); |
| 3605 | } |
| 3606 | param->value = xmlNodeGetContent(content); |
| 3607 | if (lastparam == NULL) { |
| 3608 | def->attrs = lastparam = param; |
| 3609 | } else { |
| 3610 | lastparam->next = param; |
| 3611 | lastparam = param; |
| 3612 | } |
| 3613 | if (lib != NULL) { |
| 3614 | } |
| 3615 | } |
| 3616 | content = content->next; |
| 3617 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3618 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3619 | /* |
| 3620 | * Handle optional except |
| 3621 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3622 | if ((content != NULL) |
| 3623 | && (xmlStrEqual(content->name, BAD_CAST "except"))) { |
| 3624 | xmlNodePtr child; |
| 3625 | xmlRelaxNGDefinePtr tmp2, last2 = NULL; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3626 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3627 | except = xmlRelaxNGNewDefine(ctxt, node); |
| 3628 | if (except == NULL) { |
| 3629 | return (def); |
| 3630 | } |
| 3631 | except->type = XML_RELAXNG_EXCEPT; |
| 3632 | child = content->children; |
| 3633 | if (last == NULL) { |
| 3634 | def->content = except; |
| 3635 | } else { |
| 3636 | last->next = except; |
| 3637 | } |
| 3638 | if (child == NULL) { |
| 3639 | xmlRngPErr(ctxt, content, XML_RNGP_EXCEPT_NO_CONTENT, |
| 3640 | "except has no content\n", NULL, NULL); |
| 3641 | } |
| 3642 | while (child != NULL) { |
| 3643 | tmp2 = xmlRelaxNGParsePattern(ctxt, child); |
| 3644 | if (tmp2 != NULL) { |
| 3645 | if (last2 == NULL) { |
| 3646 | except->content = last2 = tmp2; |
| 3647 | } else { |
| 3648 | last2->next = tmp2; |
| 3649 | last2 = tmp2; |
| 3650 | } |
| 3651 | } |
| 3652 | child = child->next; |
| 3653 | } |
| 3654 | content = content->next; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3655 | } |
| 3656 | /* |
| 3657 | * Check there is no unhandled data |
| 3658 | */ |
| 3659 | if (content != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3660 | xmlRngPErr(ctxt, content, XML_RNGP_DATA_CONTENT, |
| 3661 | "Element data has unexpected content %s\n", |
| 3662 | content->name, NULL); |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3663 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3664 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3665 | return (def); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3666 | } |
| 3667 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3668 | static const xmlChar *invalidName = BAD_CAST "\1"; |
| 3669 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3670 | /** |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3671 | * xmlRelaxNGCompareNameClasses: |
| 3672 | * @defs1: the first element/attribute defs |
| 3673 | * @defs2: the second element/attribute defs |
| 3674 | * @name: the restriction on the name |
| 3675 | * @ns: the restriction on the namespace |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3676 | * |
| 3677 | * Compare the 2 lists of element definitions. The comparison is |
| 3678 | * that if both lists do not accept the same QNames, it returns 1 |
| 3679 | * If the 2 lists can accept the same QName the comparison returns 0 |
| 3680 | * |
| 3681 | * Returns 1 disttinct, 0 if equal |
| 3682 | */ |
| 3683 | static int |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3684 | xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3685 | xmlRelaxNGDefinePtr def2) |
| 3686 | { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3687 | int ret = 1; |
| 3688 | xmlNode node; |
| 3689 | xmlNs ns; |
| 3690 | xmlRelaxNGValidCtxt ctxt; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3691 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3692 | ctxt.flags = FLAGS_IGNORABLE; |
| 3693 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 3694 | memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt)); |
| 3695 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3696 | if ((def1->type == XML_RELAXNG_ELEMENT) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3697 | (def1->type == XML_RELAXNG_ATTRIBUTE)) { |
| 3698 | if (def2->type == XML_RELAXNG_TEXT) |
| 3699 | return (1); |
| 3700 | if (def1->name != NULL) { |
| 3701 | node.name = def1->name; |
| 3702 | } else { |
| 3703 | node.name = invalidName; |
| 3704 | } |
| 3705 | node.ns = &ns; |
| 3706 | if (def1->ns != NULL) { |
| 3707 | if (def1->ns[0] == 0) { |
| 3708 | node.ns = NULL; |
| 3709 | } else { |
| 3710 | ns.href = def1->ns; |
| 3711 | } |
| 3712 | } else { |
| 3713 | ns.href = invalidName; |
| 3714 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3715 | if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3716 | if (def1->nameClass != NULL) { |
| 3717 | ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2); |
| 3718 | } else { |
| 3719 | ret = 0; |
| 3720 | } |
| 3721 | } else { |
| 3722 | ret = 1; |
| 3723 | } |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3724 | } else if (def1->type == XML_RELAXNG_TEXT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3725 | if (def2->type == XML_RELAXNG_TEXT) |
| 3726 | return (0); |
| 3727 | return (1); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3728 | } else if (def1->type == XML_RELAXNG_EXCEPT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3729 | TODO ret = 0; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3730 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3731 | TODO ret = 0; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3732 | } |
| 3733 | if (ret == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3734 | return (ret); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3735 | if ((def2->type == XML_RELAXNG_ELEMENT) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3736 | (def2->type == XML_RELAXNG_ATTRIBUTE)) { |
| 3737 | if (def2->name != NULL) { |
| 3738 | node.name = def2->name; |
| 3739 | } else { |
| 3740 | node.name = invalidName; |
| 3741 | } |
| 3742 | node.ns = &ns; |
| 3743 | if (def2->ns != NULL) { |
| 3744 | if (def2->ns[0] == 0) { |
| 3745 | node.ns = NULL; |
| 3746 | } else { |
| 3747 | ns.href = def2->ns; |
| 3748 | } |
| 3749 | } else { |
| 3750 | ns.href = invalidName; |
| 3751 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3752 | if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3753 | if (def2->nameClass != NULL) { |
| 3754 | ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1); |
| 3755 | } else { |
| 3756 | ret = 0; |
| 3757 | } |
| 3758 | } else { |
| 3759 | ret = 1; |
| 3760 | } |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3761 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3762 | TODO ret = 0; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3763 | } |
| 3764 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3765 | return (ret); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3766 | } |
| 3767 | |
| 3768 | /** |
| 3769 | * xmlRelaxNGCompareElemDefLists: |
| 3770 | * @ctxt: a Relax-NG parser context |
| 3771 | * @defs1: the first list of element/attribute defs |
| 3772 | * @defs2: the second list of element/attribute defs |
| 3773 | * |
| 3774 | * Compare the 2 lists of element or attribute definitions. The comparison |
| 3775 | * is that if both lists do not accept the same QNames, it returns 1 |
| 3776 | * If the 2 lists can accept the same QName the comparison returns 0 |
| 3777 | * |
| 3778 | * Returns 1 disttinct, 0 if equal |
| 3779 | */ |
| 3780 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3781 | xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt |
| 3782 | ATTRIBUTE_UNUSED, xmlRelaxNGDefinePtr * def1, |
| 3783 | xmlRelaxNGDefinePtr * def2) |
| 3784 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3785 | xmlRelaxNGDefinePtr *basedef2 = def2; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3786 | |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3787 | if ((def1 == NULL) || (def2 == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3788 | return (1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3789 | if ((*def1 == NULL) || (*def2 == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3790 | return (1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3791 | while (*def1 != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3792 | while ((*def2) != NULL) { |
| 3793 | if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0) |
| 3794 | return (0); |
| 3795 | def2++; |
| 3796 | } |
| 3797 | def2 = basedef2; |
| 3798 | def1++; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3799 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3800 | return (1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3801 | } |
| 3802 | |
| 3803 | /** |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3804 | * xmlRelaxNGGenerateAttributes: |
| 3805 | * @ctxt: a Relax-NG parser context |
| 3806 | * @def: the definition definition |
| 3807 | * |
| 3808 | * Check if the definition can only generate attributes |
| 3809 | * |
| 3810 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 3811 | */ |
| 3812 | static int |
| 3813 | xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3814 | xmlRelaxNGDefinePtr def) |
| 3815 | { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3816 | xmlRelaxNGDefinePtr parent, cur, tmp; |
| 3817 | |
| 3818 | /* |
| 3819 | * Don't run that check in case of error. Infinite recursion |
| 3820 | * becomes possible. |
| 3821 | */ |
| 3822 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3823 | return (-1); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3824 | |
| 3825 | parent = NULL; |
| 3826 | cur = def; |
| 3827 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3828 | if ((cur->type == XML_RELAXNG_ELEMENT) || |
| 3829 | (cur->type == XML_RELAXNG_TEXT) || |
| 3830 | (cur->type == XML_RELAXNG_DATATYPE) || |
| 3831 | (cur->type == XML_RELAXNG_PARAM) || |
| 3832 | (cur->type == XML_RELAXNG_LIST) || |
| 3833 | (cur->type == XML_RELAXNG_VALUE) || |
| 3834 | (cur->type == XML_RELAXNG_EMPTY)) |
| 3835 | return (0); |
| 3836 | if ((cur->type == XML_RELAXNG_CHOICE) || |
| 3837 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
| 3838 | (cur->type == XML_RELAXNG_GROUP) || |
| 3839 | (cur->type == XML_RELAXNG_ONEORMORE) || |
| 3840 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
| 3841 | (cur->type == XML_RELAXNG_OPTIONAL) || |
| 3842 | (cur->type == XML_RELAXNG_PARENTREF) || |
| 3843 | (cur->type == XML_RELAXNG_EXTERNALREF) || |
| 3844 | (cur->type == XML_RELAXNG_REF) || |
| 3845 | (cur->type == XML_RELAXNG_DEF)) { |
| 3846 | if (cur->content != NULL) { |
| 3847 | parent = cur; |
| 3848 | cur = cur->content; |
| 3849 | tmp = cur; |
| 3850 | while (tmp != NULL) { |
| 3851 | tmp->parent = parent; |
| 3852 | tmp = tmp->next; |
| 3853 | } |
| 3854 | continue; |
| 3855 | } |
| 3856 | } |
| 3857 | if (cur == def) |
| 3858 | break; |
| 3859 | if (cur->next != NULL) { |
| 3860 | cur = cur->next; |
| 3861 | continue; |
| 3862 | } |
| 3863 | do { |
| 3864 | cur = cur->parent; |
| 3865 | if (cur == NULL) |
| 3866 | break; |
| 3867 | if (cur == def) |
| 3868 | return (1); |
| 3869 | if (cur->next != NULL) { |
| 3870 | cur = cur->next; |
| 3871 | break; |
| 3872 | } |
| 3873 | } while (cur != NULL); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3874 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3875 | return (1); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3876 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3877 | |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3878 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3879 | * xmlRelaxNGGetElements: |
| 3880 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3881 | * @def: the definition definition |
| 3882 | * @eora: gather elements (0) or attributes (1) |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3883 | * |
| 3884 | * Compute the list of top elements a definition can generate |
| 3885 | * |
| 3886 | * Returns a list of elements or NULL if none was found. |
| 3887 | */ |
| 3888 | static xmlRelaxNGDefinePtr * |
| 3889 | xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3890 | xmlRelaxNGDefinePtr def, int eora) |
| 3891 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3892 | xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3893 | int len = 0; |
| 3894 | int max = 0; |
| 3895 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3896 | /* |
| 3897 | * Don't run that check in case of error. Infinite recursion |
| 3898 | * becomes possible. |
| 3899 | */ |
| 3900 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3901 | return (NULL); |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3902 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3903 | parent = NULL; |
| 3904 | cur = def; |
| 3905 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3906 | if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) || |
| 3907 | (cur->type == XML_RELAXNG_TEXT))) || |
| 3908 | ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) { |
| 3909 | if (ret == NULL) { |
| 3910 | max = 10; |
| 3911 | ret = (xmlRelaxNGDefinePtr *) |
| 3912 | xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
| 3913 | if (ret == NULL) { |
| 3914 | xmlRngPErrMemory(ctxt, "getting element list\n"); |
| 3915 | return (NULL); |
| 3916 | } |
| 3917 | } else if (max <= len) { |
| 3918 | max *= 2; |
| 3919 | ret = |
| 3920 | xmlRealloc(ret, |
| 3921 | (max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
| 3922 | if (ret == NULL) { |
| 3923 | xmlRngPErrMemory(ctxt, "getting element list\n"); |
| 3924 | return (NULL); |
| 3925 | } |
| 3926 | } |
| 3927 | ret[len++] = cur; |
| 3928 | ret[len] = NULL; |
| 3929 | } else if ((cur->type == XML_RELAXNG_CHOICE) || |
| 3930 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
| 3931 | (cur->type == XML_RELAXNG_GROUP) || |
| 3932 | (cur->type == XML_RELAXNG_ONEORMORE) || |
| 3933 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
| 3934 | (cur->type == XML_RELAXNG_OPTIONAL) || |
| 3935 | (cur->type == XML_RELAXNG_PARENTREF) || |
| 3936 | (cur->type == XML_RELAXNG_REF) || |
| 3937 | (cur->type == XML_RELAXNG_DEF)) { |
| 3938 | /* |
| 3939 | * Don't go within elements or attributes or string values. |
| 3940 | * Just gather the element top list |
| 3941 | */ |
| 3942 | if (cur->content != NULL) { |
| 3943 | parent = cur; |
| 3944 | cur = cur->content; |
| 3945 | tmp = cur; |
| 3946 | while (tmp != NULL) { |
| 3947 | tmp->parent = parent; |
| 3948 | tmp = tmp->next; |
| 3949 | } |
| 3950 | continue; |
| 3951 | } |
| 3952 | } |
| 3953 | if (cur == def) |
| 3954 | break; |
| 3955 | if (cur->next != NULL) { |
| 3956 | cur = cur->next; |
| 3957 | continue; |
| 3958 | } |
| 3959 | do { |
| 3960 | cur = cur->parent; |
| 3961 | if (cur == NULL) |
| 3962 | break; |
| 3963 | if (cur == def) |
| 3964 | return (ret); |
| 3965 | if (cur->next != NULL) { |
| 3966 | cur = cur->next; |
| 3967 | break; |
| 3968 | } |
| 3969 | } while (cur != NULL); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3970 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3971 | return (ret); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3972 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3973 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3974 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3975 | * xmlRelaxNGCheckChoiceDeterminism: |
| 3976 | * @ctxt: a Relax-NG parser context |
| 3977 | * @def: the choice definition |
| 3978 | * |
| 3979 | * Also used to find indeterministic pattern in choice |
| 3980 | */ |
| 3981 | static void |
| 3982 | xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3983 | xmlRelaxNGDefinePtr def) |
| 3984 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3985 | xmlRelaxNGDefinePtr **list; |
| 3986 | xmlRelaxNGDefinePtr cur; |
| 3987 | int nbchild = 0, i, j, ret; |
| 3988 | int is_nullable = 0; |
| 3989 | int is_indeterminist = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3990 | xmlHashTablePtr triage = NULL; |
| 3991 | int is_triable = 1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3992 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3993 | if ((def == NULL) || (def->type != XML_RELAXNG_CHOICE)) |
| 3994 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3995 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3996 | if (def->dflags & IS_PROCESSED) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3997 | return; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3998 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3999 | /* |
| 4000 | * Don't run that check in case of error. Infinite recursion |
| 4001 | * becomes possible. |
| 4002 | */ |
| 4003 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4004 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4005 | |
| 4006 | is_nullable = xmlRelaxNGIsNullable(def); |
| 4007 | |
| 4008 | cur = def->content; |
| 4009 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4010 | nbchild++; |
| 4011 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4012 | } |
| 4013 | |
| 4014 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4015 | sizeof(xmlRelaxNGDefinePtr |
| 4016 | *)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4017 | if (list == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4018 | xmlRngPErrMemory(ctxt, "building choice\n"); |
| 4019 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4020 | } |
| 4021 | i = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4022 | /* |
| 4023 | * a bit strong but safe |
| 4024 | */ |
| 4025 | if (is_nullable == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4026 | triage = xmlHashCreate(10); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4027 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4028 | is_triable = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4029 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4030 | cur = def->content; |
| 4031 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4032 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 0); |
| 4033 | if ((list[i] == NULL) || (list[i][0] == NULL)) { |
| 4034 | is_triable = 0; |
| 4035 | } else if (is_triable == 1) { |
| 4036 | xmlRelaxNGDefinePtr *tmp; |
| 4037 | int res; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4038 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4039 | tmp = list[i]; |
| 4040 | while ((*tmp != NULL) && (is_triable == 1)) { |
| 4041 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
| 4042 | res = xmlHashAddEntry2(triage, |
| 4043 | BAD_CAST "#text", NULL, |
| 4044 | (void *) cur); |
| 4045 | if (res != 0) |
| 4046 | is_triable = -1; |
| 4047 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
| 4048 | ((*tmp)->name != NULL)) { |
| 4049 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4050 | res = xmlHashAddEntry2(triage, |
| 4051 | (*tmp)->name, NULL, |
| 4052 | (void *) cur); |
| 4053 | else |
| 4054 | res = xmlHashAddEntry2(triage, |
| 4055 | (*tmp)->name, (*tmp)->ns, |
| 4056 | (void *) cur); |
| 4057 | if (res != 0) |
| 4058 | is_triable = -1; |
| 4059 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
| 4060 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4061 | res = xmlHashAddEntry2(triage, |
| 4062 | BAD_CAST "#any", NULL, |
| 4063 | (void *) cur); |
| 4064 | else |
| 4065 | res = xmlHashAddEntry2(triage, |
| 4066 | BAD_CAST "#any", (*tmp)->ns, |
| 4067 | (void *) cur); |
| 4068 | if (res != 0) |
| 4069 | is_triable = -1; |
| 4070 | } else { |
| 4071 | is_triable = -1; |
| 4072 | } |
| 4073 | tmp++; |
| 4074 | } |
| 4075 | } |
| 4076 | i++; |
| 4077 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4078 | } |
| 4079 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4080 | for (i = 0; i < nbchild; i++) { |
| 4081 | if (list[i] == NULL) |
| 4082 | continue; |
| 4083 | for (j = 0; j < i; j++) { |
| 4084 | if (list[j] == NULL) |
| 4085 | continue; |
| 4086 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
| 4087 | if (ret == 0) { |
| 4088 | is_indeterminist = 1; |
| 4089 | } |
| 4090 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4091 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4092 | for (i = 0; i < nbchild; i++) { |
| 4093 | if (list[i] != NULL) |
| 4094 | xmlFree(list[i]); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4095 | } |
| 4096 | |
| 4097 | xmlFree(list); |
| 4098 | if (is_indeterminist) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4099 | def->dflags |= IS_INDETERMINIST; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4100 | } |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4101 | if (is_triable == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4102 | def->dflags |= IS_TRIABLE; |
| 4103 | def->data = triage; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4104 | } else if (triage != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4105 | xmlHashFree(triage, NULL); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4106 | } |
| 4107 | def->dflags |= IS_PROCESSED; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4108 | } |
| 4109 | |
| 4110 | /** |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4111 | * xmlRelaxNGCheckGroupAttrs: |
| 4112 | * @ctxt: a Relax-NG parser context |
| 4113 | * @def: the group definition |
| 4114 | * |
| 4115 | * Detects violations of rule 7.3 |
| 4116 | */ |
| 4117 | static void |
| 4118 | xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4119 | xmlRelaxNGDefinePtr def) |
| 4120 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4121 | xmlRelaxNGDefinePtr **list; |
| 4122 | xmlRelaxNGDefinePtr cur; |
| 4123 | int nbchild = 0, i, j, ret; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4124 | |
| 4125 | if ((def == NULL) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4126 | ((def->type != XML_RELAXNG_GROUP) && |
| 4127 | (def->type != XML_RELAXNG_ELEMENT))) |
| 4128 | return; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4129 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4130 | if (def->dflags & IS_PROCESSED) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4131 | return; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4132 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4133 | /* |
| 4134 | * Don't run that check in case of error. Infinite recursion |
| 4135 | * becomes possible. |
| 4136 | */ |
| 4137 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4138 | return; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4139 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4140 | cur = def->attrs; |
| 4141 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4142 | nbchild++; |
| 4143 | cur = cur->next; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4144 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4145 | cur = def->content; |
| 4146 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4147 | nbchild++; |
| 4148 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4149 | } |
| 4150 | |
| 4151 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4152 | sizeof(xmlRelaxNGDefinePtr |
| 4153 | *)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4154 | if (list == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4155 | xmlRngPErrMemory(ctxt, "building group\n"); |
| 4156 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4157 | } |
| 4158 | i = 0; |
| 4159 | cur = def->attrs; |
| 4160 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4161 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4162 | i++; |
| 4163 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4164 | } |
| 4165 | cur = def->content; |
| 4166 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4167 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4168 | i++; |
| 4169 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4170 | } |
| 4171 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4172 | for (i = 0; i < nbchild; i++) { |
| 4173 | if (list[i] == NULL) |
| 4174 | continue; |
| 4175 | for (j = 0; j < i; j++) { |
| 4176 | if (list[j] == NULL) |
| 4177 | continue; |
| 4178 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
| 4179 | if (ret == 0) { |
| 4180 | xmlRngPErr(ctxt, def->node, XML_RNGP_GROUP_ATTR_CONFLICT, |
| 4181 | "Attributes conflicts in group\n", NULL, NULL); |
| 4182 | } |
| 4183 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4184 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4185 | for (i = 0; i < nbchild; i++) { |
| 4186 | if (list[i] != NULL) |
| 4187 | xmlFree(list[i]); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4188 | } |
| 4189 | |
| 4190 | xmlFree(list); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4191 | def->dflags |= IS_PROCESSED; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4192 | } |
| 4193 | |
| 4194 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4195 | * xmlRelaxNGComputeInterleaves: |
| 4196 | * @def: the interleave definition |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4197 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4198 | * @name: the definition name |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4199 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4200 | * A lot of work for preprocessing interleave definitions |
| 4201 | * is potentially needed to get a decent execution speed at runtime |
| 4202 | * - trying to get a total order on the element nodes generated |
| 4203 | * by the interleaves, order the list of interleave definitions |
| 4204 | * following that order. |
| 4205 | * - if <text/> is used to handle mixed content, it is better to |
| 4206 | * flag this in the define and simplify the runtime checking |
| 4207 | * algorithm |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4208 | */ |
| 4209 | static void |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4210 | xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4211 | xmlRelaxNGParserCtxtPtr ctxt, |
| 4212 | xmlChar * name ATTRIBUTE_UNUSED) |
| 4213 | { |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4214 | xmlRelaxNGDefinePtr cur, *tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4215 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4216 | xmlRelaxNGPartitionPtr partitions = NULL; |
| 4217 | xmlRelaxNGInterleaveGroupPtr *groups = NULL; |
| 4218 | xmlRelaxNGInterleaveGroupPtr group; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4219 | int i, j, ret, res; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4220 | int nbgroups = 0; |
| 4221 | int nbchild = 0; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 4222 | int is_mixed = 0; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4223 | int is_determinist = 1; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4224 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4225 | /* |
| 4226 | * Don't run that check in case of error. Infinite recursion |
| 4227 | * becomes possible. |
| 4228 | */ |
| 4229 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4230 | return; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4231 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4232 | #ifdef DEBUG_INTERLEAVE |
| 4233 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4234 | "xmlRelaxNGComputeInterleaves(%s)\n", name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4235 | #endif |
| 4236 | cur = def->content; |
| 4237 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4238 | nbchild++; |
| 4239 | cur = cur->next; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4240 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4241 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4242 | #ifdef DEBUG_INTERLEAVE |
| 4243 | xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild); |
| 4244 | #endif |
| 4245 | groups = (xmlRelaxNGInterleaveGroupPtr *) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4246 | xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4247 | if (groups == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4248 | goto error; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4249 | cur = def->content; |
| 4250 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4251 | groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr) |
| 4252 | xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup)); |
| 4253 | if (groups[nbgroups] == NULL) |
| 4254 | goto error; |
| 4255 | if (cur->type == XML_RELAXNG_TEXT) |
| 4256 | is_mixed++; |
| 4257 | groups[nbgroups]->rule = cur; |
| 4258 | groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0); |
| 4259 | groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4260 | nbgroups++; |
| 4261 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4262 | } |
| 4263 | #ifdef DEBUG_INTERLEAVE |
| 4264 | xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups); |
| 4265 | #endif |
| 4266 | |
| 4267 | /* |
| 4268 | * Let's check that all rules makes a partitions according to 7.4 |
| 4269 | */ |
| 4270 | partitions = (xmlRelaxNGPartitionPtr) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4271 | xmlMalloc(sizeof(xmlRelaxNGPartition)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4272 | if (partitions == NULL) |
| 4273 | goto error; |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 4274 | memset(partitions, 0, sizeof(xmlRelaxNGPartition)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4275 | partitions->nbgroups = nbgroups; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4276 | partitions->triage = xmlHashCreate(nbgroups); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4277 | for (i = 0; i < nbgroups; i++) { |
| 4278 | group = groups[i]; |
| 4279 | for (j = i + 1; j < nbgroups; j++) { |
| 4280 | if (groups[j] == NULL) |
| 4281 | continue; |
| 4282 | |
| 4283 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs, |
| 4284 | groups[j]->defs); |
| 4285 | if (ret == 0) { |
| 4286 | xmlRngPErr(ctxt, def->node, XML_RNGP_ELEM_TEXT_CONFLICT, |
| 4287 | "Element or text conflicts in interleave\n", |
| 4288 | NULL, NULL); |
| 4289 | } |
| 4290 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs, |
| 4291 | groups[j]->attrs); |
| 4292 | if (ret == 0) { |
| 4293 | xmlRngPErr(ctxt, def->node, XML_RNGP_ATTR_CONFLICT, |
| 4294 | "Attributes conflicts in interleave\n", NULL, |
| 4295 | NULL); |
| 4296 | } |
| 4297 | } |
| 4298 | tmp = group->defs; |
| 4299 | if ((tmp != NULL) && (*tmp != NULL)) { |
| 4300 | while (*tmp != NULL) { |
| 4301 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
| 4302 | res = xmlHashAddEntry2(partitions->triage, |
| 4303 | BAD_CAST "#text", NULL, |
| 4304 | (void *) (long) (i + 1)); |
| 4305 | if (res != 0) |
| 4306 | is_determinist = -1; |
| 4307 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
| 4308 | ((*tmp)->name != NULL)) { |
| 4309 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4310 | res = xmlHashAddEntry2(partitions->triage, |
| 4311 | (*tmp)->name, NULL, |
| 4312 | (void *) (long) (i + 1)); |
| 4313 | else |
| 4314 | res = xmlHashAddEntry2(partitions->triage, |
| 4315 | (*tmp)->name, (*tmp)->ns, |
| 4316 | (void *) (long) (i + 1)); |
| 4317 | if (res != 0) |
| 4318 | is_determinist = -1; |
| 4319 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
| 4320 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4321 | res = xmlHashAddEntry2(partitions->triage, |
| 4322 | BAD_CAST "#any", NULL, |
| 4323 | (void *) (long) (i + 1)); |
| 4324 | else |
| 4325 | res = xmlHashAddEntry2(partitions->triage, |
| 4326 | BAD_CAST "#any", (*tmp)->ns, |
| 4327 | (void *) (long) (i + 1)); |
| 4328 | if ((*tmp)->nameClass != NULL) |
| 4329 | is_determinist = 2; |
| 4330 | if (res != 0) |
| 4331 | is_determinist = -1; |
| 4332 | } else { |
| 4333 | is_determinist = -1; |
| 4334 | } |
| 4335 | tmp++; |
| 4336 | } |
| 4337 | } else { |
| 4338 | is_determinist = 0; |
| 4339 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4340 | } |
| 4341 | partitions->groups = groups; |
| 4342 | |
| 4343 | /* |
| 4344 | * and save the partition list back in the def |
| 4345 | */ |
| 4346 | def->data = partitions; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 4347 | if (is_mixed != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4348 | def->dflags |= IS_MIXED; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4349 | if (is_determinist == 1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4350 | partitions->flags = IS_DETERMINIST; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4351 | if (is_determinist == 2) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4352 | partitions->flags = IS_DETERMINIST | IS_NEEDCHECK; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4353 | return; |
| 4354 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4355 | error: |
| 4356 | xmlRngPErrMemory(ctxt, "in interleave computation\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4357 | if (groups != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4358 | for (i = 0; i < nbgroups; i++) |
| 4359 | if (groups[i] != NULL) { |
| 4360 | if (groups[i]->defs != NULL) |
| 4361 | xmlFree(groups[i]->defs); |
| 4362 | xmlFree(groups[i]); |
| 4363 | } |
| 4364 | xmlFree(groups); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4365 | } |
| 4366 | xmlRelaxNGFreePartition(partitions); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4367 | } |
| 4368 | |
| 4369 | /** |
| 4370 | * xmlRelaxNGParseInterleave: |
| 4371 | * @ctxt: a Relax-NG parser context |
| 4372 | * @node: the data node. |
| 4373 | * |
| 4374 | * parse the content of a RelaxNG interleave node. |
| 4375 | * |
| 4376 | * Returns the definition pointer or NULL in case of error |
| 4377 | */ |
| 4378 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4379 | xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4380 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4381 | xmlRelaxNGDefinePtr def = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4382 | xmlRelaxNGDefinePtr last = NULL, cur; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4383 | xmlNodePtr child; |
| 4384 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4385 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4386 | if (def == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4387 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4388 | } |
| 4389 | def->type = XML_RELAXNG_INTERLEAVE; |
| 4390 | |
| 4391 | if (ctxt->interleaves == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4392 | ctxt->interleaves = xmlHashCreate(10); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4393 | if (ctxt->interleaves == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4394 | xmlRngPErrMemory(ctxt, "create interleaves\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4395 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4396 | char name[32]; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4397 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4398 | snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++); |
| 4399 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) { |
| 4400 | xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_ADD, |
| 4401 | "Failed to add %s to hash table\n", |
| 4402 | (const xmlChar *) name, NULL); |
| 4403 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4404 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4405 | child = node->children; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4406 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4407 | xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_NO_CONTENT, |
| 4408 | "Element interleave is empty\n", NULL, NULL); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 4409 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4410 | while (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4411 | if (IS_RELAXNG(child, "element")) { |
| 4412 | cur = xmlRelaxNGParseElement(ctxt, child); |
| 4413 | } else { |
| 4414 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 4415 | } |
| 4416 | if (cur != NULL) { |
| 4417 | cur->parent = def; |
| 4418 | if (last == NULL) { |
| 4419 | def->content = last = cur; |
| 4420 | } else { |
| 4421 | last->next = cur; |
| 4422 | last = cur; |
| 4423 | } |
| 4424 | } |
| 4425 | child = child->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4426 | } |
| 4427 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4428 | return (def); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4429 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4430 | |
| 4431 | /** |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4432 | * xmlRelaxNGParseInclude: |
| 4433 | * @ctxt: a Relax-NG parser context |
| 4434 | * @node: the include node |
| 4435 | * |
| 4436 | * Integrate the content of an include node in the current grammar |
| 4437 | * |
| 4438 | * Returns 0 in case of success or -1 in case of error |
| 4439 | */ |
| 4440 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4441 | xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4442 | { |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4443 | xmlRelaxNGIncludePtr incl; |
| 4444 | xmlNodePtr root; |
| 4445 | int ret = 0, tmp; |
| 4446 | |
| 4447 | incl = node->_private; |
| 4448 | if (incl == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4449 | xmlRngPErr(ctxt, node, XML_RNGP_INCLUDE_EMPTY, |
| 4450 | "Include node has no data\n", NULL, NULL); |
| 4451 | return (-1); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4452 | } |
| 4453 | root = xmlDocGetRootElement(incl->doc); |
| 4454 | if (root == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4455 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, "Include document is empty\n", |
| 4456 | NULL, NULL); |
| 4457 | return (-1); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4458 | } |
| 4459 | if (!xmlStrEqual(root->name, BAD_CAST "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4460 | xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING, |
| 4461 | "Include document root is not a grammar\n", NULL, NULL); |
| 4462 | return (-1); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4463 | } |
| 4464 | |
| 4465 | /* |
| 4466 | * Merge the definition from both the include and the internal list |
| 4467 | */ |
| 4468 | if (root->children != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4469 | tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children); |
| 4470 | if (tmp != 0) |
| 4471 | ret = -1; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4472 | } |
| 4473 | if (node->children != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4474 | tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children); |
| 4475 | if (tmp != 0) |
| 4476 | ret = -1; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4477 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4478 | return (ret); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4479 | } |
| 4480 | |
| 4481 | /** |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4482 | * xmlRelaxNGParseDefine: |
| 4483 | * @ctxt: a Relax-NG parser context |
| 4484 | * @node: the define node |
| 4485 | * |
| 4486 | * parse the content of a RelaxNG define element node. |
| 4487 | * |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4488 | * Returns 0 in case of success or -1 in case of error |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4489 | */ |
| 4490 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4491 | xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4492 | { |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4493 | xmlChar *name; |
| 4494 | int ret = 0, tmp; |
| 4495 | xmlRelaxNGDefinePtr def; |
| 4496 | const xmlChar *olddefine; |
| 4497 | |
| 4498 | name = xmlGetProp(node, BAD_CAST "name"); |
| 4499 | if (name == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4500 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_NAME_MISSING, |
| 4501 | "define has no name\n", NULL, NULL); |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4502 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4503 | xmlRelaxNGNormExtSpace(name); |
| 4504 | if (xmlValidateNCName(name, 0)) { |
| 4505 | xmlRngPErr(ctxt, node, XML_RNGP_INVALID_DEFINE_NAME, |
| 4506 | "define name '%s' is not an NCName\n", name, NULL); |
| 4507 | } |
| 4508 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4509 | if (def == NULL) { |
| 4510 | xmlFree(name); |
| 4511 | return (-1); |
| 4512 | } |
| 4513 | def->type = XML_RELAXNG_DEF; |
| 4514 | def->name = name; |
| 4515 | if (node->children == NULL) { |
| 4516 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_EMPTY, |
| 4517 | "define has no children\n", NULL, NULL); |
| 4518 | } else { |
| 4519 | olddefine = ctxt->define; |
| 4520 | ctxt->define = name; |
| 4521 | def->content = |
| 4522 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4523 | ctxt->define = olddefine; |
| 4524 | } |
| 4525 | if (ctxt->grammar->defs == NULL) |
| 4526 | ctxt->grammar->defs = xmlHashCreate(10); |
| 4527 | if (ctxt->grammar->defs == NULL) { |
| 4528 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED, |
| 4529 | "Could not create definition hash\n", NULL, NULL); |
| 4530 | ret = -1; |
| 4531 | } else { |
| 4532 | tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def); |
| 4533 | if (tmp < 0) { |
| 4534 | xmlRelaxNGDefinePtr prev; |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 4535 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4536 | prev = xmlHashLookup(ctxt->grammar->defs, name); |
| 4537 | if (prev == NULL) { |
| 4538 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED, |
| 4539 | "Internal error on define aggregation of %s\n", |
| 4540 | name, NULL); |
| 4541 | ret = -1; |
| 4542 | } else { |
| 4543 | while (prev->nextHash != NULL) |
| 4544 | prev = prev->nextHash; |
| 4545 | prev->nextHash = def; |
| 4546 | } |
| 4547 | } |
| 4548 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4549 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4550 | return (ret); |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4551 | } |
| 4552 | |
| 4553 | /** |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4554 | * xmlRelaxNGProcessExternalRef: |
| 4555 | * @ctxt: the parser context |
| 4556 | * @node: the externlRef node |
| 4557 | * |
| 4558 | * Process and compile an externlRef node |
| 4559 | * |
| 4560 | * Returns the xmlRelaxNGDefinePtr or NULL in case of error |
| 4561 | */ |
| 4562 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4563 | xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4564 | { |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4565 | xmlRelaxNGDocumentPtr docu; |
| 4566 | xmlNodePtr root, tmp; |
| 4567 | xmlChar *ns; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4568 | int newNs = 0, oldflags; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4569 | xmlRelaxNGDefinePtr def; |
| 4570 | |
| 4571 | docu = node->_private; |
| 4572 | if (docu != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4573 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4574 | if (def == NULL) |
| 4575 | return (NULL); |
| 4576 | def->type = XML_RELAXNG_EXTERNALREF; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4577 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4578 | if (docu->content == NULL) { |
| 4579 | /* |
| 4580 | * Then do the parsing for good |
| 4581 | */ |
| 4582 | root = xmlDocGetRootElement(docu->doc); |
| 4583 | if (root == NULL) { |
| 4584 | xmlRngPErr(ctxt, node, XML_RNGP_EXTERNALREF_EMTPY, |
| 4585 | "xmlRelaxNGParse: %s is empty\n", ctxt->URL, |
| 4586 | NULL); |
| 4587 | return (NULL); |
| 4588 | } |
| 4589 | /* |
| 4590 | * ns transmission rules |
| 4591 | */ |
| 4592 | ns = xmlGetProp(root, BAD_CAST "ns"); |
| 4593 | if (ns == NULL) { |
| 4594 | tmp = node; |
| 4595 | while ((tmp != NULL) && (tmp->type == XML_ELEMENT_NODE)) { |
| 4596 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 4597 | if (ns != NULL) { |
| 4598 | break; |
| 4599 | } |
| 4600 | tmp = tmp->parent; |
| 4601 | } |
| 4602 | if (ns != NULL) { |
| 4603 | xmlSetProp(root, BAD_CAST "ns", ns); |
| 4604 | newNs = 1; |
| 4605 | xmlFree(ns); |
| 4606 | } |
| 4607 | } else { |
| 4608 | xmlFree(ns); |
| 4609 | } |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4610 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4611 | /* |
| 4612 | * Parsing to get a precompiled schemas. |
| 4613 | */ |
| 4614 | oldflags = ctxt->flags; |
| 4615 | ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF; |
| 4616 | docu->schema = xmlRelaxNGParseDocument(ctxt, root); |
| 4617 | ctxt->flags = oldflags; |
| 4618 | if ((docu->schema != NULL) && |
| 4619 | (docu->schema->topgrammar != NULL)) { |
| 4620 | docu->content = docu->schema->topgrammar->start; |
| 4621 | } |
| 4622 | |
| 4623 | /* |
| 4624 | * the externalRef may be reused in a different ns context |
| 4625 | */ |
| 4626 | if (newNs == 1) { |
| 4627 | xmlUnsetProp(root, BAD_CAST "ns"); |
| 4628 | } |
| 4629 | } |
| 4630 | def->content = docu->content; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4631 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4632 | def = NULL; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4633 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4634 | return (def); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4635 | } |
| 4636 | |
| 4637 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4638 | * xmlRelaxNGParsePattern: |
| 4639 | * @ctxt: a Relax-NG parser context |
| 4640 | * @node: the pattern node. |
| 4641 | * |
| 4642 | * parse the content of a RelaxNG pattern node. |
| 4643 | * |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4644 | * Returns the definition pointer or NULL in case of error or if no |
| 4645 | * pattern is generated. |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4646 | */ |
| 4647 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4648 | xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4649 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4650 | xmlRelaxNGDefinePtr def = NULL; |
| 4651 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4652 | if (node == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4653 | return (NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4654 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4655 | if (IS_RELAXNG(node, "element")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4656 | def = xmlRelaxNGParseElement(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4657 | } else if (IS_RELAXNG(node, "attribute")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4658 | def = xmlRelaxNGParseAttribute(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4659 | } else if (IS_RELAXNG(node, "empty")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4660 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4661 | if (def == NULL) |
| 4662 | return (NULL); |
| 4663 | def->type = XML_RELAXNG_EMPTY; |
| 4664 | if (node->children != NULL) { |
| 4665 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_NOT_EMPTY, |
| 4666 | "empty: had a child node\n", NULL, NULL); |
| 4667 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4668 | } else if (IS_RELAXNG(node, "text")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4669 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4670 | if (def == NULL) |
| 4671 | return (NULL); |
| 4672 | def->type = XML_RELAXNG_TEXT; |
| 4673 | if (node->children != NULL) { |
| 4674 | xmlRngPErr(ctxt, node, XML_RNGP_TEXT_HAS_CHILD, |
| 4675 | "text: had a child node\n", NULL, NULL); |
| 4676 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4677 | } else if (IS_RELAXNG(node, "zeroOrMore")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4678 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4679 | if (def == NULL) |
| 4680 | return (NULL); |
| 4681 | def->type = XML_RELAXNG_ZEROORMORE; |
| 4682 | if (node->children == NULL) { |
| 4683 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4684 | "Element %s is empty\n", node->name, NULL); |
| 4685 | } else { |
| 4686 | def->content = |
| 4687 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4688 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4689 | } else if (IS_RELAXNG(node, "oneOrMore")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4690 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4691 | if (def == NULL) |
| 4692 | return (NULL); |
| 4693 | def->type = XML_RELAXNG_ONEORMORE; |
| 4694 | if (node->children == NULL) { |
| 4695 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4696 | "Element %s is empty\n", node->name, NULL); |
| 4697 | } else { |
| 4698 | def->content = |
| 4699 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4700 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4701 | } else if (IS_RELAXNG(node, "optional")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4702 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4703 | if (def == NULL) |
| 4704 | return (NULL); |
| 4705 | def->type = XML_RELAXNG_OPTIONAL; |
| 4706 | if (node->children == NULL) { |
| 4707 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4708 | "Element %s is empty\n", node->name, NULL); |
| 4709 | } else { |
| 4710 | def->content = |
| 4711 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4712 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4713 | } else if (IS_RELAXNG(node, "choice")) { |
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_CHOICE; |
| 4718 | if (node->children == NULL) { |
| 4719 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4720 | "Element %s is empty\n", node->name, NULL); |
| 4721 | } else { |
| 4722 | def->content = |
| 4723 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4724 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4725 | } else if (IS_RELAXNG(node, "group")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4726 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4727 | if (def == NULL) |
| 4728 | return (NULL); |
| 4729 | def->type = XML_RELAXNG_GROUP; |
| 4730 | if (node->children == NULL) { |
| 4731 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4732 | "Element %s is empty\n", node->name, NULL); |
| 4733 | } else { |
| 4734 | def->content = |
| 4735 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4736 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4737 | } else if (IS_RELAXNG(node, "ref")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4738 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4739 | if (def == NULL) |
| 4740 | return (NULL); |
| 4741 | def->type = XML_RELAXNG_REF; |
| 4742 | def->name = xmlGetProp(node, BAD_CAST "name"); |
| 4743 | if (def->name == NULL) { |
| 4744 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NO_NAME, "ref has no name\n", |
| 4745 | NULL, NULL); |
| 4746 | } else { |
| 4747 | xmlRelaxNGNormExtSpace(def->name); |
| 4748 | if (xmlValidateNCName(def->name, 0)) { |
| 4749 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NAME_INVALID, |
| 4750 | "ref name '%s' is not an NCName\n", def->name, |
| 4751 | NULL); |
| 4752 | } |
| 4753 | } |
| 4754 | if (node->children != NULL) { |
| 4755 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NOT_EMPTY, "ref is not empty\n", |
| 4756 | NULL, NULL); |
| 4757 | } |
| 4758 | if (ctxt->grammar->refs == NULL) |
| 4759 | ctxt->grammar->refs = xmlHashCreate(10); |
| 4760 | if (ctxt->grammar->refs == NULL) { |
| 4761 | xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED, |
| 4762 | "Could not create references hash\n", NULL, NULL); |
| 4763 | def = NULL; |
| 4764 | } else { |
| 4765 | int tmp; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4766 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4767 | tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def); |
| 4768 | if (tmp < 0) { |
| 4769 | xmlRelaxNGDefinePtr prev; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4770 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4771 | prev = (xmlRelaxNGDefinePtr) |
| 4772 | xmlHashLookup(ctxt->grammar->refs, def->name); |
| 4773 | if (prev == NULL) { |
| 4774 | if (def->name != NULL) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 4775 | xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED, |
| 4776 | "Error refs definitions '%s'\n", |
| 4777 | def->name, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4778 | } else { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 4779 | xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED, |
| 4780 | "Error refs definitions\n", |
| 4781 | NULL, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4782 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4783 | def = NULL; |
| 4784 | } else { |
| 4785 | def->nextHash = prev->nextHash; |
| 4786 | prev->nextHash = def; |
| 4787 | } |
| 4788 | } |
| 4789 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 4790 | } else if (IS_RELAXNG(node, "data")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4791 | def = xmlRelaxNGParseData(ctxt, node); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 4792 | } else if (IS_RELAXNG(node, "value")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4793 | def = xmlRelaxNGParseValue(ctxt, node); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 4794 | } else if (IS_RELAXNG(node, "list")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4795 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4796 | if (def == NULL) |
| 4797 | return (NULL); |
| 4798 | def->type = XML_RELAXNG_LIST; |
| 4799 | if (node->children == NULL) { |
| 4800 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4801 | "Element %s is empty\n", node->name, NULL); |
| 4802 | } else { |
| 4803 | def->content = |
| 4804 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4805 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4806 | } else if (IS_RELAXNG(node, "interleave")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4807 | def = xmlRelaxNGParseInterleave(ctxt, node); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4808 | } else if (IS_RELAXNG(node, "externalRef")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4809 | def = xmlRelaxNGProcessExternalRef(ctxt, node); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4810 | } else if (IS_RELAXNG(node, "notAllowed")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4811 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4812 | if (def == NULL) |
| 4813 | return (NULL); |
| 4814 | def->type = XML_RELAXNG_NOT_ALLOWED; |
| 4815 | if (node->children != NULL) { |
| 4816 | xmlRngPErr(ctxt, node, XML_RNGP_NOTALLOWED_NOT_EMPTY, |
| 4817 | "xmlRelaxNGParse: notAllowed element is not empty\n", |
| 4818 | NULL, NULL); |
| 4819 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4820 | } else if (IS_RELAXNG(node, "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4821 | xmlRelaxNGGrammarPtr grammar, old; |
| 4822 | xmlRelaxNGGrammarPtr oldparent; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4823 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4824 | #ifdef DEBUG_GRAMMAR |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4825 | xmlGenericError(xmlGenericErrorContext, |
| 4826 | "Found <grammar> pattern\n"); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4827 | #endif |
| 4828 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4829 | oldparent = ctxt->parentgrammar; |
| 4830 | old = ctxt->grammar; |
| 4831 | ctxt->parentgrammar = old; |
| 4832 | grammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
| 4833 | if (old != NULL) { |
| 4834 | ctxt->grammar = old; |
| 4835 | ctxt->parentgrammar = oldparent; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4836 | #if 0 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4837 | if (grammar != NULL) { |
| 4838 | grammar->next = old->next; |
| 4839 | old->next = grammar; |
| 4840 | } |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4841 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4842 | } |
| 4843 | if (grammar != NULL) |
| 4844 | def = grammar->start; |
| 4845 | else |
| 4846 | def = NULL; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4847 | } else if (IS_RELAXNG(node, "parentRef")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4848 | if (ctxt->parentgrammar == NULL) { |
| 4849 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_PARENT, |
| 4850 | "Use of parentRef without a parent grammar\n", NULL, |
| 4851 | NULL); |
| 4852 | return (NULL); |
| 4853 | } |
| 4854 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4855 | if (def == NULL) |
| 4856 | return (NULL); |
| 4857 | def->type = XML_RELAXNG_PARENTREF; |
| 4858 | def->name = xmlGetProp(node, BAD_CAST "name"); |
| 4859 | if (def->name == NULL) { |
| 4860 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_NAME, |
| 4861 | "parentRef has no name\n", NULL, NULL); |
| 4862 | } else { |
| 4863 | xmlRelaxNGNormExtSpace(def->name); |
| 4864 | if (xmlValidateNCName(def->name, 0)) { |
| 4865 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NAME_INVALID, |
| 4866 | "parentRef name '%s' is not an NCName\n", |
| 4867 | def->name, NULL); |
| 4868 | } |
| 4869 | } |
| 4870 | if (node->children != NULL) { |
| 4871 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NOT_EMPTY, |
| 4872 | "parentRef is not empty\n", NULL, NULL); |
| 4873 | } |
| 4874 | if (ctxt->parentgrammar->refs == NULL) |
| 4875 | ctxt->parentgrammar->refs = xmlHashCreate(10); |
| 4876 | if (ctxt->parentgrammar->refs == NULL) { |
| 4877 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED, |
| 4878 | "Could not create references hash\n", NULL, NULL); |
| 4879 | def = NULL; |
| 4880 | } else if (def->name != NULL) { |
| 4881 | int tmp; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4882 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4883 | tmp = |
| 4884 | xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def); |
| 4885 | if (tmp < 0) { |
| 4886 | xmlRelaxNGDefinePtr prev; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4887 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4888 | prev = (xmlRelaxNGDefinePtr) |
| 4889 | xmlHashLookup(ctxt->parentgrammar->refs, def->name); |
| 4890 | if (prev == NULL) { |
| 4891 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED, |
| 4892 | "Internal error parentRef definitions '%s'\n", |
| 4893 | def->name, NULL); |
| 4894 | def = NULL; |
| 4895 | } else { |
| 4896 | def->nextHash = prev->nextHash; |
| 4897 | prev->nextHash = def; |
| 4898 | } |
| 4899 | } |
| 4900 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4901 | } else if (IS_RELAXNG(node, "mixed")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4902 | if (node->children == NULL) { |
| 4903 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, "Mixed is empty\n", |
| 4904 | NULL, NULL); |
| 4905 | def = NULL; |
| 4906 | } else { |
| 4907 | def = xmlRelaxNGParseInterleave(ctxt, node); |
| 4908 | if (def != NULL) { |
| 4909 | xmlRelaxNGDefinePtr tmp; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4910 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4911 | if ((def->content != NULL) && (def->content->next != NULL)) { |
| 4912 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
| 4913 | if (tmp != NULL) { |
| 4914 | tmp->type = XML_RELAXNG_GROUP; |
| 4915 | tmp->content = def->content; |
| 4916 | def->content = tmp; |
| 4917 | } |
| 4918 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4919 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4920 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
| 4921 | if (tmp == NULL) |
| 4922 | return (def); |
| 4923 | tmp->type = XML_RELAXNG_TEXT; |
| 4924 | tmp->next = def->content; |
| 4925 | def->content = tmp; |
| 4926 | } |
| 4927 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4928 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4929 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_CONSTRUCT, |
| 4930 | "Unexpected node %s is not a pattern\n", node->name, |
| 4931 | NULL); |
| 4932 | def = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4933 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4934 | return (def); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4935 | } |
| 4936 | |
| 4937 | /** |
| 4938 | * xmlRelaxNGParseAttribute: |
| 4939 | * @ctxt: a Relax-NG parser context |
| 4940 | * @node: the element node |
| 4941 | * |
| 4942 | * parse the content of a RelaxNG attribute node. |
| 4943 | * |
| 4944 | * Returns the definition pointer or NULL in case of error. |
| 4945 | */ |
| 4946 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4947 | xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4948 | { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4949 | xmlRelaxNGDefinePtr ret, cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4950 | xmlNodePtr child; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4951 | int old_flags; |
| 4952 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4953 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4954 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4955 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4956 | ret->type = XML_RELAXNG_ATTRIBUTE; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4957 | ret->parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4958 | child = node->children; |
| 4959 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4960 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_EMPTY, |
| 4961 | "xmlRelaxNGParseattribute: attribute has no children\n", |
| 4962 | NULL, NULL); |
| 4963 | return (ret); |
| 4964 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4965 | old_flags = ctxt->flags; |
| 4966 | ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4967 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 4968 | if (cur != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4969 | child = child->next; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4970 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4971 | if (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4972 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 4973 | if (cur != NULL) { |
| 4974 | switch (cur->type) { |
| 4975 | case XML_RELAXNG_EMPTY: |
| 4976 | case XML_RELAXNG_NOT_ALLOWED: |
| 4977 | case XML_RELAXNG_TEXT: |
| 4978 | case XML_RELAXNG_ELEMENT: |
| 4979 | case XML_RELAXNG_DATATYPE: |
| 4980 | case XML_RELAXNG_VALUE: |
| 4981 | case XML_RELAXNG_LIST: |
| 4982 | case XML_RELAXNG_REF: |
| 4983 | case XML_RELAXNG_PARENTREF: |
| 4984 | case XML_RELAXNG_EXTERNALREF: |
| 4985 | case XML_RELAXNG_DEF: |
| 4986 | case XML_RELAXNG_ONEORMORE: |
| 4987 | case XML_RELAXNG_ZEROORMORE: |
| 4988 | case XML_RELAXNG_OPTIONAL: |
| 4989 | case XML_RELAXNG_CHOICE: |
| 4990 | case XML_RELAXNG_GROUP: |
| 4991 | case XML_RELAXNG_INTERLEAVE: |
| 4992 | case XML_RELAXNG_ATTRIBUTE: |
| 4993 | ret->content = cur; |
| 4994 | cur->parent = ret; |
| 4995 | break; |
| 4996 | case XML_RELAXNG_START: |
| 4997 | case XML_RELAXNG_PARAM: |
| 4998 | case XML_RELAXNG_EXCEPT: |
| 4999 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CONTENT, |
| 5000 | "attribute has invalid content\n", NULL, |
| 5001 | NULL); |
| 5002 | break; |
| 5003 | case XML_RELAXNG_NOOP: |
| 5004 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_NOOP, |
| 5005 | "RNG Internal error, noop found in attribute\n", |
| 5006 | NULL, NULL); |
| 5007 | break; |
| 5008 | } |
| 5009 | } |
| 5010 | child = child->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5011 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5012 | if (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5013 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CHILDREN, |
| 5014 | "attribute has multiple children\n", NULL, NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5015 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5016 | ctxt->flags = old_flags; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5017 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5018 | } |
| 5019 | |
| 5020 | /** |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5021 | * xmlRelaxNGParseExceptNameClass: |
| 5022 | * @ctxt: a Relax-NG parser context |
| 5023 | * @node: the except node |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5024 | * @attr: 1 if within an attribute, 0 if within an element |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5025 | * |
| 5026 | * parse the content of a RelaxNG nameClass node. |
| 5027 | * |
| 5028 | * Returns the definition pointer or NULL in case of error. |
| 5029 | */ |
| 5030 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5031 | xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5032 | xmlNodePtr node, int attr) |
| 5033 | { |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5034 | xmlRelaxNGDefinePtr ret, cur, last = NULL; |
| 5035 | xmlNodePtr child; |
| 5036 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5037 | if (!IS_RELAXNG(node, "except")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5038 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MISSING, |
| 5039 | "Expecting an except node\n", NULL, NULL); |
| 5040 | return (NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5041 | } |
| 5042 | if (node->next != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5043 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MULTIPLE, |
| 5044 | "exceptNameClass allows only a single except node\n", |
| 5045 | NULL, NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5046 | } |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5047 | if (node->children == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5048 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_EMPTY, "except has no content\n", |
| 5049 | NULL, NULL); |
| 5050 | return (NULL); |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5051 | } |
| 5052 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5053 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5054 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5055 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5056 | ret->type = XML_RELAXNG_EXCEPT; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5057 | child = node->children; |
| 5058 | while (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5059 | cur = xmlRelaxNGNewDefine(ctxt, child); |
| 5060 | if (cur == NULL) |
| 5061 | break; |
| 5062 | if (attr) |
| 5063 | cur->type = XML_RELAXNG_ATTRIBUTE; |
| 5064 | else |
| 5065 | cur->type = XML_RELAXNG_ELEMENT; |
| 5066 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 5067 | if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5068 | if (last == NULL) { |
| 5069 | ret->content = cur; |
| 5070 | } else { |
| 5071 | last->next = cur; |
| 5072 | } |
| 5073 | last = cur; |
| 5074 | } |
| 5075 | child = child->next; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5076 | } |
| 5077 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5078 | return (ret); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5079 | } |
| 5080 | |
| 5081 | /** |
| 5082 | * xmlRelaxNGParseNameClass: |
| 5083 | * @ctxt: a Relax-NG parser context |
| 5084 | * @node: the nameClass node |
| 5085 | * @def: the current definition |
| 5086 | * |
| 5087 | * parse the content of a RelaxNG nameClass node. |
| 5088 | * |
| 5089 | * Returns the definition pointer or NULL in case of error. |
| 5090 | */ |
| 5091 | static xmlRelaxNGDefinePtr |
| 5092 | xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5093 | xmlRelaxNGDefinePtr def) |
| 5094 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5095 | xmlRelaxNGDefinePtr ret, tmp; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5096 | xmlChar *val; |
| 5097 | |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5098 | ret = def; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5099 | if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) || |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5100 | (IS_RELAXNG(node, "nsName"))) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5101 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 5102 | (def->type != XML_RELAXNG_ATTRIBUTE)) { |
| 5103 | ret = xmlRelaxNGNewDefine(ctxt, node); |
| 5104 | if (ret == NULL) |
| 5105 | return (NULL); |
| 5106 | ret->parent = def; |
| 5107 | if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) |
| 5108 | ret->type = XML_RELAXNG_ATTRIBUTE; |
| 5109 | else |
| 5110 | ret->type = XML_RELAXNG_ELEMENT; |
| 5111 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5112 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5113 | if (IS_RELAXNG(node, "name")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5114 | val = xmlNodeGetContent(node); |
| 5115 | xmlRelaxNGNormExtSpace(val); |
| 5116 | if (xmlValidateNCName(val, 0)) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5117 | if (node->parent != NULL) |
| 5118 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME, |
| 5119 | "Element %s name '%s' is not an NCName\n", |
| 5120 | node->parent->name, val); |
| 5121 | else |
| 5122 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME, |
| 5123 | "name '%s' is not an NCName\n", |
| 5124 | val, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5125 | } |
| 5126 | ret->name = val; |
| 5127 | val = xmlGetProp(node, BAD_CAST "ns"); |
| 5128 | ret->ns = val; |
| 5129 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5130 | (val != NULL) && |
| 5131 | (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5132 | xmlRngPErr(ctxt, node, XML_RNGP_XML_NS, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5133 | "Attribute with namespace '%s' is not allowed\n", |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5134 | val, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5135 | } |
| 5136 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5137 | (val != NULL) && |
| 5138 | (val[0] == 0) && (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5139 | xmlRngPErr(ctxt, node, XML_RNGP_XMLNS_NAME, |
| 5140 | "Attribute with QName 'xmlns' is not allowed\n", |
| 5141 | val, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5142 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5143 | } else if (IS_RELAXNG(node, "anyName")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5144 | ret->name = NULL; |
| 5145 | ret->ns = NULL; |
| 5146 | if (node->children != NULL) { |
| 5147 | ret->nameClass = |
| 5148 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
| 5149 | (def->type == |
| 5150 | XML_RELAXNG_ATTRIBUTE)); |
| 5151 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5152 | } else if (IS_RELAXNG(node, "nsName")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5153 | ret->name = NULL; |
| 5154 | ret->ns = xmlGetProp(node, BAD_CAST "ns"); |
| 5155 | if (ret->ns == NULL) { |
| 5156 | xmlRngPErr(ctxt, node, XML_RNGP_NSNAME_NO_NS, |
| 5157 | "nsName has no ns attribute\n", NULL, NULL); |
| 5158 | } |
| 5159 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5160 | (ret->ns != NULL) && |
| 5161 | (xmlStrEqual |
| 5162 | (ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
| 5163 | xmlRngPErr(ctxt, node, XML_RNGP_XML_NS, |
| 5164 | "Attribute with namespace '%s' is not allowed\n", |
| 5165 | ret->ns, NULL); |
| 5166 | } |
| 5167 | if (node->children != NULL) { |
| 5168 | ret->nameClass = |
| 5169 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
| 5170 | (def->type == |
| 5171 | XML_RELAXNG_ATTRIBUTE)); |
| 5172 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5173 | } else if (IS_RELAXNG(node, "choice")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5174 | xmlNodePtr child; |
| 5175 | xmlRelaxNGDefinePtr last = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5176 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5177 | ret = xmlRelaxNGNewDefine(ctxt, node); |
| 5178 | if (ret == NULL) |
| 5179 | return (NULL); |
| 5180 | ret->parent = def; |
| 5181 | ret->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5182 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5183 | if (node->children == NULL) { |
| 5184 | xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_EMPTY, |
| 5185 | "Element choice is empty\n", NULL, NULL); |
| 5186 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5187 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5188 | child = node->children; |
| 5189 | while (child != NULL) { |
| 5190 | tmp = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5191 | if (tmp != NULL) { |
| 5192 | if (last == NULL) { |
| 5193 | last = ret->nameClass = tmp; |
| 5194 | } else { |
| 5195 | last->next = tmp; |
| 5196 | last = tmp; |
| 5197 | } |
| 5198 | } |
| 5199 | child = child->next; |
| 5200 | } |
| 5201 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5202 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5203 | xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_CONTENT, |
| 5204 | "expecting name, anyName, nsName or choice : got %s\n", |
| 5205 | node->name, NULL); |
| 5206 | return (NULL); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5207 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5208 | if (ret != def) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5209 | if (def->nameClass == NULL) { |
| 5210 | def->nameClass = ret; |
| 5211 | } else { |
| 5212 | tmp = def->nameClass; |
| 5213 | while (tmp->next != NULL) { |
| 5214 | tmp = tmp->next; |
| 5215 | } |
| 5216 | tmp->next = ret; |
| 5217 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5218 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5219 | return (ret); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5220 | } |
| 5221 | |
| 5222 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5223 | * xmlRelaxNGParseElement: |
| 5224 | * @ctxt: a Relax-NG parser context |
| 5225 | * @node: the element node |
| 5226 | * |
| 5227 | * parse the content of a RelaxNG element node. |
| 5228 | * |
| 5229 | * Returns the definition pointer or NULL in case of error. |
| 5230 | */ |
| 5231 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5232 | xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 5233 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5234 | xmlRelaxNGDefinePtr ret, cur, last; |
| 5235 | xmlNodePtr child; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5236 | const xmlChar *olddefine; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5237 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5238 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5239 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5240 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5241 | ret->type = XML_RELAXNG_ELEMENT; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5242 | ret->parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5243 | child = node->children; |
| 5244 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5245 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_EMPTY, |
| 5246 | "xmlRelaxNGParseElement: element has no children\n", |
| 5247 | NULL, NULL); |
| 5248 | return (ret); |
| 5249 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5250 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5251 | if (cur != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5252 | child = child->next; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5253 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5254 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5255 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NO_CONTENT, |
| 5256 | "xmlRelaxNGParseElement: element has no content\n", |
| 5257 | NULL, NULL); |
| 5258 | return (ret); |
| 5259 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5260 | olddefine = ctxt->define; |
| 5261 | ctxt->define = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5262 | last = NULL; |
| 5263 | while (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5264 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 5265 | if (cur != NULL) { |
| 5266 | cur->parent = ret; |
| 5267 | switch (cur->type) { |
| 5268 | case XML_RELAXNG_EMPTY: |
| 5269 | case XML_RELAXNG_NOT_ALLOWED: |
| 5270 | case XML_RELAXNG_TEXT: |
| 5271 | case XML_RELAXNG_ELEMENT: |
| 5272 | case XML_RELAXNG_DATATYPE: |
| 5273 | case XML_RELAXNG_VALUE: |
| 5274 | case XML_RELAXNG_LIST: |
| 5275 | case XML_RELAXNG_REF: |
| 5276 | case XML_RELAXNG_PARENTREF: |
| 5277 | case XML_RELAXNG_EXTERNALREF: |
| 5278 | case XML_RELAXNG_DEF: |
| 5279 | case XML_RELAXNG_ZEROORMORE: |
| 5280 | case XML_RELAXNG_ONEORMORE: |
| 5281 | case XML_RELAXNG_OPTIONAL: |
| 5282 | case XML_RELAXNG_CHOICE: |
| 5283 | case XML_RELAXNG_GROUP: |
| 5284 | case XML_RELAXNG_INTERLEAVE: |
| 5285 | if (last == NULL) { |
| 5286 | ret->content = last = cur; |
| 5287 | } else { |
| 5288 | if ((last->type == XML_RELAXNG_ELEMENT) && |
| 5289 | (ret->content == last)) { |
| 5290 | ret->content = xmlRelaxNGNewDefine(ctxt, node); |
| 5291 | if (ret->content != NULL) { |
| 5292 | ret->content->type = XML_RELAXNG_GROUP; |
| 5293 | ret->content->content = last; |
| 5294 | } else { |
| 5295 | ret->content = last; |
| 5296 | } |
| 5297 | } |
| 5298 | last->next = cur; |
| 5299 | last = cur; |
| 5300 | } |
| 5301 | break; |
| 5302 | case XML_RELAXNG_ATTRIBUTE: |
| 5303 | cur->next = ret->attrs; |
| 5304 | ret->attrs = cur; |
| 5305 | break; |
| 5306 | case XML_RELAXNG_START: |
| 5307 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5308 | "RNG Internal error, start found in element\n", |
| 5309 | NULL, NULL); |
| 5310 | break; |
| 5311 | case XML_RELAXNG_PARAM: |
| 5312 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5313 | "RNG Internal error, param found in element\n", |
| 5314 | NULL, NULL); |
| 5315 | break; |
| 5316 | case XML_RELAXNG_EXCEPT: |
| 5317 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5318 | "RNG Internal error, except found in element\n", |
| 5319 | NULL, NULL); |
| 5320 | break; |
| 5321 | case XML_RELAXNG_NOOP: |
| 5322 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5323 | "RNG Internal error, noop found in element\n", |
| 5324 | NULL, NULL); |
| 5325 | break; |
| 5326 | } |
| 5327 | } |
| 5328 | child = child->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5329 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5330 | ctxt->define = olddefine; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5331 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5332 | } |
| 5333 | |
| 5334 | /** |
| 5335 | * xmlRelaxNGParsePatterns: |
| 5336 | * @ctxt: a Relax-NG parser context |
| 5337 | * @nodes: list of nodes |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5338 | * @group: use an implicit <group> for elements |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5339 | * |
| 5340 | * parse the content of a RelaxNG start node. |
| 5341 | * |
| 5342 | * Returns the definition pointer or NULL in case of error. |
| 5343 | */ |
| 5344 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5345 | xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5346 | int group) |
| 5347 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5348 | xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5349 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5350 | parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5351 | while (nodes != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5352 | if (IS_RELAXNG(nodes, "element")) { |
| 5353 | cur = xmlRelaxNGParseElement(ctxt, nodes); |
| 5354 | if (def == NULL) { |
| 5355 | def = last = cur; |
| 5356 | } else { |
| 5357 | if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) && |
| 5358 | (def == last)) { |
| 5359 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
| 5360 | def->type = XML_RELAXNG_GROUP; |
| 5361 | def->content = last; |
| 5362 | } |
| 5363 | last->next = cur; |
| 5364 | last = cur; |
| 5365 | } |
| 5366 | cur->parent = parent; |
| 5367 | } else { |
| 5368 | cur = xmlRelaxNGParsePattern(ctxt, nodes); |
| 5369 | if (cur != NULL) { |
| 5370 | if (def == NULL) { |
| 5371 | def = last = cur; |
| 5372 | } else { |
| 5373 | last->next = cur; |
| 5374 | last = cur; |
| 5375 | } |
| 5376 | } |
| 5377 | } |
| 5378 | nodes = nodes->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5379 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5380 | return (def); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5381 | } |
| 5382 | |
| 5383 | /** |
| 5384 | * xmlRelaxNGParseStart: |
| 5385 | * @ctxt: a Relax-NG parser context |
| 5386 | * @nodes: start children nodes |
| 5387 | * |
| 5388 | * parse the content of a RelaxNG start node. |
| 5389 | * |
| 5390 | * Returns 0 in case of success, -1 in case of error |
| 5391 | */ |
| 5392 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5393 | xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) |
| 5394 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5395 | int ret = 0; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5396 | xmlRelaxNGDefinePtr def = NULL, last; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5397 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5398 | if (nodes == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5399 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, "start has no children\n", |
| 5400 | NULL, NULL); |
| 5401 | return (-1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5402 | } |
| 5403 | if (IS_RELAXNG(nodes, "empty")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5404 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
| 5405 | if (def == NULL) |
| 5406 | return (-1); |
| 5407 | def->type = XML_RELAXNG_EMPTY; |
| 5408 | if (nodes->children != NULL) { |
| 5409 | xmlRngPErr(ctxt, nodes, XML_RNGP_EMPTY_CONTENT, |
| 5410 | "element empty is not empty\n", NULL, NULL); |
| 5411 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5412 | } else if (IS_RELAXNG(nodes, "notAllowed")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5413 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
| 5414 | if (def == NULL) |
| 5415 | return (-1); |
| 5416 | def->type = XML_RELAXNG_NOT_ALLOWED; |
| 5417 | if (nodes->children != NULL) { |
| 5418 | xmlRngPErr(ctxt, nodes, XML_RNGP_NOTALLOWED_NOT_EMPTY, |
| 5419 | "element notAllowed is not empty\n", NULL, NULL); |
| 5420 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5421 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5422 | def = xmlRelaxNGParsePatterns(ctxt, nodes, 1); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5423 | } |
| 5424 | if (ctxt->grammar->start != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5425 | last = ctxt->grammar->start; |
| 5426 | while (last->next != NULL) |
| 5427 | last = last->next; |
| 5428 | last->next = def; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5429 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5430 | ctxt->grammar->start = def; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5431 | } |
| 5432 | nodes = nodes->next; |
| 5433 | if (nodes != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5434 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_CONTENT, |
| 5435 | "start more than one children\n", NULL, NULL); |
| 5436 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5437 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5438 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5439 | } |
| 5440 | |
| 5441 | /** |
| 5442 | * xmlRelaxNGParseGrammarContent: |
| 5443 | * @ctxt: a Relax-NG parser context |
| 5444 | * @nodes: grammar children nodes |
| 5445 | * |
| 5446 | * parse the content of a RelaxNG grammar node. |
| 5447 | * |
| 5448 | * Returns 0 in case of success, -1 in case of error |
| 5449 | */ |
| 5450 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5451 | xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt, |
| 5452 | xmlNodePtr nodes) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5453 | { |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5454 | int ret = 0, tmp; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5455 | |
| 5456 | if (nodes == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5457 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_EMPTY, |
| 5458 | "grammar has no children\n", NULL, NULL); |
| 5459 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5460 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5461 | while (nodes != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5462 | if (IS_RELAXNG(nodes, "start")) { |
| 5463 | if (nodes->children == NULL) { |
| 5464 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, |
| 5465 | "start has no children\n", NULL, NULL); |
| 5466 | } else { |
| 5467 | tmp = xmlRelaxNGParseStart(ctxt, nodes->children); |
| 5468 | if (tmp != 0) |
| 5469 | ret = -1; |
| 5470 | } |
| 5471 | } else if (IS_RELAXNG(nodes, "define")) { |
| 5472 | tmp = xmlRelaxNGParseDefine(ctxt, nodes); |
| 5473 | if (tmp != 0) |
| 5474 | ret = -1; |
| 5475 | } else if (IS_RELAXNG(nodes, "include")) { |
| 5476 | tmp = xmlRelaxNGParseInclude(ctxt, nodes); |
| 5477 | if (tmp != 0) |
| 5478 | ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5479 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5480 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT, |
| 5481 | "grammar has unexpected child %s\n", nodes->name, |
| 5482 | NULL); |
| 5483 | ret = -1; |
| 5484 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5485 | nodes = nodes->next; |
| 5486 | } |
| 5487 | return (ret); |
| 5488 | } |
| 5489 | |
| 5490 | /** |
| 5491 | * xmlRelaxNGCheckReference: |
| 5492 | * @ref: the ref |
| 5493 | * @ctxt: a Relax-NG parser context |
| 5494 | * @name: the name associated to the defines |
| 5495 | * |
| 5496 | * Applies the 4.17. combine attribute rule for all the define |
| 5497 | * element of a given grammar using the same name. |
| 5498 | */ |
| 5499 | static void |
| 5500 | xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5501 | xmlRelaxNGParserCtxtPtr ctxt, |
| 5502 | const xmlChar * name) |
| 5503 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5504 | xmlRelaxNGGrammarPtr grammar; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5505 | xmlRelaxNGDefinePtr def, cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5506 | |
| 5507 | grammar = ctxt->grammar; |
| 5508 | if (grammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5509 | xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR, |
| 5510 | "Internal error: no grammar in CheckReference %s\n", |
| 5511 | name, NULL); |
| 5512 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5513 | } |
| 5514 | if (ref->content != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5515 | xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR, |
| 5516 | "Internal error: reference has content in CheckReference %s\n", |
| 5517 | name, NULL); |
| 5518 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5519 | } |
| 5520 | if (grammar->defs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5521 | def = xmlHashLookup(grammar->defs, name); |
| 5522 | if (def != NULL) { |
| 5523 | cur = ref; |
| 5524 | while (cur != NULL) { |
| 5525 | cur->content = def; |
| 5526 | cur = cur->nextHash; |
| 5527 | } |
| 5528 | } else { |
| 5529 | xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF, |
| 5530 | "Reference %s has no matching definition\n", name, |
| 5531 | NULL); |
| 5532 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5533 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5534 | xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF, |
| 5535 | "Reference %s has no matching definition\n", name, |
| 5536 | NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5537 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5538 | } |
| 5539 | |
| 5540 | /** |
| 5541 | * xmlRelaxNGCheckCombine: |
| 5542 | * @define: the define(s) list |
| 5543 | * @ctxt: a Relax-NG parser context |
| 5544 | * @name: the name associated to the defines |
| 5545 | * |
| 5546 | * Applies the 4.17. combine attribute rule for all the define |
| 5547 | * element of a given grammar using the same name. |
| 5548 | */ |
| 5549 | static void |
| 5550 | xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5551 | xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * name) |
| 5552 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5553 | xmlChar *combine; |
| 5554 | int choiceOrInterleave = -1; |
| 5555 | int missing = 0; |
| 5556 | xmlRelaxNGDefinePtr cur, last, tmp, tmp2; |
| 5557 | |
| 5558 | if (define->nextHash == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5559 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5560 | cur = define; |
| 5561 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5562 | combine = xmlGetProp(cur->node, BAD_CAST "combine"); |
| 5563 | if (combine != NULL) { |
| 5564 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
| 5565 | if (choiceOrInterleave == -1) |
| 5566 | choiceOrInterleave = 1; |
| 5567 | else if (choiceOrInterleave == 0) { |
| 5568 | xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE, |
| 5569 | "Defines for %s use both 'choice' and 'interleave'\n", |
| 5570 | name, NULL); |
| 5571 | } |
| 5572 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
| 5573 | if (choiceOrInterleave == -1) |
| 5574 | choiceOrInterleave = 0; |
| 5575 | else if (choiceOrInterleave == 1) { |
| 5576 | xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE, |
| 5577 | "Defines for %s use both 'choice' and 'interleave'\n", |
| 5578 | name, NULL); |
| 5579 | } |
| 5580 | } else { |
| 5581 | xmlRngPErr(ctxt, define->node, XML_RNGP_UNKNOWN_COMBINE, |
| 5582 | "Defines for %s use unknown combine value '%s''\n", |
| 5583 | name, combine); |
| 5584 | } |
| 5585 | xmlFree(combine); |
| 5586 | } else { |
| 5587 | if (missing == 0) |
| 5588 | missing = 1; |
| 5589 | else { |
| 5590 | xmlRngPErr(ctxt, define->node, XML_RNGP_NEED_COMBINE, |
| 5591 | "Some defines for %s needs the combine attribute\n", |
| 5592 | name, NULL); |
| 5593 | } |
| 5594 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5595 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5596 | cur = cur->nextHash; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5597 | } |
| 5598 | #ifdef DEBUG |
| 5599 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5600 | "xmlRelaxNGCheckCombine(): merging %s defines: %d\n", |
| 5601 | name, choiceOrInterleave); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5602 | #endif |
| 5603 | if (choiceOrInterleave == -1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5604 | choiceOrInterleave = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5605 | cur = xmlRelaxNGNewDefine(ctxt, define->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5606 | if (cur == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5607 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5608 | if (choiceOrInterleave == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5609 | cur->type = XML_RELAXNG_INTERLEAVE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5610 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5611 | cur->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5612 | tmp = define; |
| 5613 | last = NULL; |
| 5614 | while (tmp != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5615 | if (tmp->content != NULL) { |
| 5616 | if (tmp->content->next != NULL) { |
| 5617 | /* |
| 5618 | * we need first to create a wrapper. |
| 5619 | */ |
| 5620 | tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node); |
| 5621 | if (tmp2 == NULL) |
| 5622 | break; |
| 5623 | tmp2->type = XML_RELAXNG_GROUP; |
| 5624 | tmp2->content = tmp->content; |
| 5625 | } else { |
| 5626 | tmp2 = tmp->content; |
| 5627 | } |
| 5628 | if (last == NULL) { |
| 5629 | cur->content = tmp2; |
| 5630 | } else { |
| 5631 | last->next = tmp2; |
| 5632 | } |
| 5633 | last = tmp2; |
| 5634 | } |
| 5635 | tmp->content = cur; |
| 5636 | tmp = tmp->nextHash; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5637 | } |
| 5638 | define->content = cur; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5639 | if (choiceOrInterleave == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5640 | if (ctxt->interleaves == NULL) |
| 5641 | ctxt->interleaves = xmlHashCreate(10); |
| 5642 | if (ctxt->interleaves == NULL) { |
| 5643 | xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5644 | "Failed to create interleaves hash table\n", NULL, |
| 5645 | NULL); |
| 5646 | } else { |
| 5647 | char tmpname[32]; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5648 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5649 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
| 5650 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < |
| 5651 | 0) { |
| 5652 | xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5653 | "Failed to add %s to hash table\n", |
| 5654 | (const xmlChar *) tmpname, NULL); |
| 5655 | } |
| 5656 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5657 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5658 | } |
| 5659 | |
| 5660 | /** |
| 5661 | * xmlRelaxNGCombineStart: |
| 5662 | * @ctxt: a Relax-NG parser context |
| 5663 | * @grammar: the grammar |
| 5664 | * |
| 5665 | * Applies the 4.17. combine rule for all the start |
| 5666 | * element of a given grammar. |
| 5667 | */ |
| 5668 | static void |
| 5669 | xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5670 | xmlRelaxNGGrammarPtr grammar) |
| 5671 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5672 | xmlRelaxNGDefinePtr starts; |
| 5673 | xmlChar *combine; |
| 5674 | int choiceOrInterleave = -1; |
| 5675 | int missing = 0; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5676 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5677 | |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5678 | starts = grammar->start; |
| 5679 | if ((starts == NULL) || (starts->next == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5680 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5681 | cur = starts; |
| 5682 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5683 | if ((cur->node == NULL) || (cur->node->parent == NULL) || |
| 5684 | (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) { |
| 5685 | combine = NULL; |
| 5686 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_MISSING, |
| 5687 | "Internal error: start element not found\n", NULL, |
| 5688 | NULL); |
| 5689 | } else { |
| 5690 | combine = xmlGetProp(cur->node->parent, BAD_CAST "combine"); |
| 5691 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5692 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5693 | if (combine != NULL) { |
| 5694 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
| 5695 | if (choiceOrInterleave == -1) |
| 5696 | choiceOrInterleave = 1; |
| 5697 | else if (choiceOrInterleave == 0) { |
| 5698 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE, |
| 5699 | "<start> use both 'choice' and 'interleave'\n", |
| 5700 | NULL, NULL); |
| 5701 | } |
| 5702 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
| 5703 | if (choiceOrInterleave == -1) |
| 5704 | choiceOrInterleave = 0; |
| 5705 | else if (choiceOrInterleave == 1) { |
| 5706 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE, |
| 5707 | "<start> use both 'choice' and 'interleave'\n", |
| 5708 | NULL, NULL); |
| 5709 | } |
| 5710 | } else { |
| 5711 | xmlRngPErr(ctxt, cur->node, XML_RNGP_UNKNOWN_COMBINE, |
| 5712 | "<start> uses unknown combine value '%s''\n", |
| 5713 | combine, NULL); |
| 5714 | } |
| 5715 | xmlFree(combine); |
| 5716 | } else { |
| 5717 | if (missing == 0) |
| 5718 | missing = 1; |
| 5719 | else { |
| 5720 | xmlRngPErr(ctxt, cur->node, XML_RNGP_NEED_COMBINE, |
| 5721 | "Some <start> element miss the combine attribute\n", |
| 5722 | NULL, NULL); |
| 5723 | } |
| 5724 | } |
| 5725 | |
| 5726 | cur = cur->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5727 | } |
| 5728 | #ifdef DEBUG |
| 5729 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5730 | "xmlRelaxNGCombineStart(): merging <start>: %d\n", |
| 5731 | choiceOrInterleave); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5732 | #endif |
| 5733 | if (choiceOrInterleave == -1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5734 | choiceOrInterleave = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5735 | cur = xmlRelaxNGNewDefine(ctxt, starts->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5736 | if (cur == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5737 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5738 | if (choiceOrInterleave == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5739 | cur->type = XML_RELAXNG_INTERLEAVE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5740 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5741 | cur->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5742 | cur->content = grammar->start; |
| 5743 | grammar->start = cur; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5744 | if (choiceOrInterleave == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5745 | if (ctxt->interleaves == NULL) |
| 5746 | ctxt->interleaves = xmlHashCreate(10); |
| 5747 | if (ctxt->interleaves == NULL) { |
| 5748 | xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5749 | "Failed to create interleaves hash table\n", NULL, |
| 5750 | NULL); |
| 5751 | } else { |
| 5752 | char tmpname[32]; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5753 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5754 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
| 5755 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < |
| 5756 | 0) { |
| 5757 | xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5758 | "Failed to add %s to hash table\n", |
| 5759 | (const xmlChar *) tmpname, NULL); |
| 5760 | } |
| 5761 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5762 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5763 | } |
| 5764 | |
| 5765 | /** |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5766 | * xmlRelaxNGCheckCycles: |
| 5767 | * @ctxt: a Relax-NG parser context |
| 5768 | * @nodes: grammar children nodes |
| 5769 | * @depth: the counter |
| 5770 | * |
| 5771 | * Check for cycles. |
| 5772 | * |
| 5773 | * Returns 0 if check passed, and -1 in case of error |
| 5774 | */ |
| 5775 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5776 | xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt, |
| 5777 | xmlRelaxNGDefinePtr cur, int depth) |
| 5778 | { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5779 | int ret = 0; |
| 5780 | |
| 5781 | while ((ret == 0) && (cur != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5782 | if ((cur->type == XML_RELAXNG_REF) || |
| 5783 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5784 | if (cur->depth == -1) { |
| 5785 | cur->depth = depth; |
| 5786 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
| 5787 | cur->depth = -2; |
| 5788 | } else if (depth == cur->depth) { |
| 5789 | xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_CYCLE, |
| 5790 | "Detected a cycle in %s references\n", |
| 5791 | cur->name, NULL); |
| 5792 | return (-1); |
| 5793 | } |
| 5794 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
| 5795 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1); |
| 5796 | } else { |
| 5797 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
| 5798 | } |
| 5799 | cur = cur->next; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5800 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5801 | return (ret); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5802 | } |
| 5803 | |
| 5804 | /** |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5805 | * xmlRelaxNGTryUnlink: |
| 5806 | * @ctxt: a Relax-NG parser context |
| 5807 | * @cur: the definition to unlink |
| 5808 | * @parent: the parent definition |
| 5809 | * @prev: the previous sibling definition |
| 5810 | * |
| 5811 | * Try to unlink a definition. If not possble make it a NOOP |
| 5812 | * |
| 5813 | * Returns the new prev definition |
| 5814 | */ |
| 5815 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5816 | xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 5817 | xmlRelaxNGDefinePtr cur, |
| 5818 | xmlRelaxNGDefinePtr parent, xmlRelaxNGDefinePtr prev) |
| 5819 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5820 | if (prev != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5821 | prev->next = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5822 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5823 | if (parent != NULL) { |
| 5824 | if (parent->content == cur) |
| 5825 | parent->content = cur->next; |
| 5826 | else if (parent->attrs == cur) |
| 5827 | parent->attrs = cur->next; |
| 5828 | else if (parent->nameClass == cur) |
| 5829 | parent->nameClass = cur->next; |
| 5830 | } else { |
| 5831 | cur->type = XML_RELAXNG_NOOP; |
| 5832 | prev = cur; |
| 5833 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5834 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5835 | return (prev); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5836 | } |
| 5837 | |
| 5838 | /** |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5839 | * xmlRelaxNGSimplify: |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5840 | * @ctxt: a Relax-NG parser context |
| 5841 | * @nodes: grammar children nodes |
| 5842 | * |
| 5843 | * Check for simplification of empty and notAllowed |
| 5844 | */ |
| 5845 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5846 | xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt, |
| 5847 | xmlRelaxNGDefinePtr cur, xmlRelaxNGDefinePtr parent) |
| 5848 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5849 | xmlRelaxNGDefinePtr prev = NULL; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5850 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5851 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5852 | if ((cur->type == XML_RELAXNG_REF) || |
| 5853 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5854 | if (cur->depth != -3) { |
| 5855 | cur->depth = -3; |
| 5856 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
| 5857 | } |
| 5858 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
| 5859 | cur->parent = parent; |
| 5860 | if ((parent != NULL) && |
| 5861 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
| 5862 | (parent->type == XML_RELAXNG_LIST) || |
| 5863 | (parent->type == XML_RELAXNG_GROUP) || |
| 5864 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 5865 | (parent->type == XML_RELAXNG_ONEORMORE) || |
| 5866 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 5867 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
| 5868 | break; |
| 5869 | } |
| 5870 | if ((parent != NULL) && (parent->type == XML_RELAXNG_CHOICE)) { |
| 5871 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5872 | } else |
| 5873 | prev = cur; |
| 5874 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 5875 | cur->parent = parent; |
| 5876 | if ((parent != NULL) && |
| 5877 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
| 5878 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 5879 | parent->type = XML_RELAXNG_EMPTY; |
| 5880 | break; |
| 5881 | } |
| 5882 | if ((parent != NULL) && |
| 5883 | ((parent->type == XML_RELAXNG_GROUP) || |
| 5884 | (parent->type == XML_RELAXNG_INTERLEAVE))) { |
| 5885 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5886 | } else |
| 5887 | prev = cur; |
| 5888 | } else { |
| 5889 | cur->parent = parent; |
| 5890 | if (cur->content != NULL) |
| 5891 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
| 5892 | if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL)) |
| 5893 | xmlRelaxNGSimplify(ctxt, cur->attrs, cur); |
| 5894 | if (cur->nameClass != NULL) |
| 5895 | xmlRelaxNGSimplify(ctxt, cur->nameClass, cur); |
| 5896 | /* |
| 5897 | * On Elements, try to move attribute only generating rules on |
| 5898 | * the attrs rules. |
| 5899 | */ |
| 5900 | if (cur->type == XML_RELAXNG_ELEMENT) { |
| 5901 | int attronly; |
| 5902 | xmlRelaxNGDefinePtr tmp, pre; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 5903 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5904 | while (cur->content != NULL) { |
| 5905 | attronly = |
| 5906 | xmlRelaxNGGenerateAttributes(ctxt, cur->content); |
| 5907 | if (attronly == 1) { |
| 5908 | /* |
| 5909 | * migrate cur->content to attrs |
| 5910 | */ |
| 5911 | tmp = cur->content; |
| 5912 | cur->content = tmp->next; |
| 5913 | tmp->next = cur->attrs; |
| 5914 | cur->attrs = tmp; |
| 5915 | } else { |
| 5916 | /* |
| 5917 | * cur->content can generate elements or text |
| 5918 | */ |
| 5919 | break; |
| 5920 | } |
| 5921 | } |
| 5922 | pre = cur->content; |
| 5923 | while ((pre != NULL) && (pre->next != NULL)) { |
| 5924 | tmp = pre->next; |
| 5925 | attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp); |
| 5926 | if (attronly == 1) { |
| 5927 | /* |
| 5928 | * migrate tmp to attrs |
| 5929 | */ |
| 5930 | pre->next = tmp->next; |
| 5931 | tmp->next = cur->attrs; |
| 5932 | cur->attrs = tmp; |
| 5933 | } else { |
| 5934 | pre = tmp; |
| 5935 | } |
| 5936 | } |
| 5937 | } |
| 5938 | /* |
| 5939 | * This may result in a simplification |
| 5940 | */ |
| 5941 | if ((cur->type == XML_RELAXNG_GROUP) || |
| 5942 | (cur->type == XML_RELAXNG_INTERLEAVE)) { |
| 5943 | if (cur->content == NULL) |
| 5944 | cur->type = XML_RELAXNG_EMPTY; |
| 5945 | else if (cur->content->next == NULL) { |
| 5946 | if ((parent == NULL) && (prev == NULL)) { |
| 5947 | cur->type = XML_RELAXNG_NOOP; |
| 5948 | } else if (prev == NULL) { |
| 5949 | parent->content = cur->content; |
| 5950 | cur->content->next = cur->next; |
| 5951 | cur = cur->content; |
| 5952 | } else { |
| 5953 | cur->content->next = cur->next; |
| 5954 | prev->next = cur->content; |
| 5955 | cur = cur->content; |
| 5956 | } |
| 5957 | } |
| 5958 | } |
| 5959 | /* |
| 5960 | * the current node may have been transformed back |
| 5961 | */ |
| 5962 | if ((cur->type == XML_RELAXNG_EXCEPT) && |
| 5963 | (cur->content != NULL) && |
| 5964 | (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) { |
| 5965 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5966 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
| 5967 | if ((parent != NULL) && |
| 5968 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
| 5969 | (parent->type == XML_RELAXNG_LIST) || |
| 5970 | (parent->type == XML_RELAXNG_GROUP) || |
| 5971 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 5972 | (parent->type == XML_RELAXNG_ONEORMORE) || |
| 5973 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 5974 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
| 5975 | break; |
| 5976 | } |
| 5977 | if ((parent != NULL) && |
| 5978 | (parent->type == XML_RELAXNG_CHOICE)) { |
| 5979 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5980 | } else |
| 5981 | prev = cur; |
| 5982 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 5983 | if ((parent != NULL) && |
| 5984 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
| 5985 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 5986 | parent->type = XML_RELAXNG_EMPTY; |
| 5987 | break; |
| 5988 | } |
| 5989 | if ((parent != NULL) && |
| 5990 | ((parent->type == XML_RELAXNG_GROUP) || |
| 5991 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 5992 | (parent->type == XML_RELAXNG_CHOICE))) { |
| 5993 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5994 | } else |
| 5995 | prev = cur; |
| 5996 | } else { |
| 5997 | prev = cur; |
| 5998 | } |
| 5999 | } |
| 6000 | cur = cur->next; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6001 | } |
| 6002 | } |
| 6003 | |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6004 | /** |
| 6005 | * xmlRelaxNGGroupContentType: |
| 6006 | * @ct1: the first content type |
| 6007 | * @ct2: the second content type |
| 6008 | * |
| 6009 | * Try to group 2 content types |
| 6010 | * |
| 6011 | * Returns the content type |
| 6012 | */ |
| 6013 | static xmlRelaxNGContentType |
| 6014 | xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6015 | xmlRelaxNGContentType ct2) |
| 6016 | { |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6017 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6018 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
| 6019 | return (XML_RELAXNG_CONTENT_ERROR); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6020 | if (ct1 == XML_RELAXNG_CONTENT_EMPTY) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6021 | return (ct2); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6022 | if (ct2 == XML_RELAXNG_CONTENT_EMPTY) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6023 | return (ct1); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6024 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6025 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
| 6026 | return (XML_RELAXNG_CONTENT_COMPLEX); |
| 6027 | return (XML_RELAXNG_CONTENT_ERROR); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6028 | } |
| 6029 | |
| 6030 | /** |
| 6031 | * xmlRelaxNGMaxContentType: |
| 6032 | * @ct1: the first content type |
| 6033 | * @ct2: the second content type |
| 6034 | * |
| 6035 | * Compute the max content-type |
| 6036 | * |
| 6037 | * Returns the content type |
| 6038 | */ |
| 6039 | static xmlRelaxNGContentType |
| 6040 | xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6041 | xmlRelaxNGContentType ct2) |
| 6042 | { |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6043 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6044 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
| 6045 | return (XML_RELAXNG_CONTENT_ERROR); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6046 | if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6047 | (ct2 == XML_RELAXNG_CONTENT_SIMPLE)) |
| 6048 | return (XML_RELAXNG_CONTENT_SIMPLE); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6049 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6050 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
| 6051 | return (XML_RELAXNG_CONTENT_COMPLEX); |
| 6052 | return (XML_RELAXNG_CONTENT_EMPTY); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6053 | } |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6054 | |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6055 | /** |
| 6056 | * xmlRelaxNGCheckRules: |
| 6057 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6058 | * @cur: the current definition |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6059 | * @flags: some accumulated flags |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6060 | * @ptype: the parent type |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6061 | * |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6062 | * Check for rules in section 7.1 and 7.2 |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6063 | * |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6064 | * Returns the content type of @cur |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6065 | */ |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6066 | static xmlRelaxNGContentType |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6067 | xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt, |
| 6068 | xmlRelaxNGDefinePtr cur, int flags, |
| 6069 | xmlRelaxNGType ptype) |
| 6070 | { |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6071 | int nflags = flags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6072 | xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6073 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6074 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6075 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6076 | if ((cur->type == XML_RELAXNG_REF) || |
| 6077 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 6078 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6079 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_REF, |
| 6080 | "Found forbidden pattern list//ref\n", NULL, |
| 6081 | NULL); |
| 6082 | } |
| 6083 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6084 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_REF, |
| 6085 | "Found forbidden pattern data/except//ref\n", |
| 6086 | NULL, NULL); |
| 6087 | } |
| 6088 | if (cur->depth > -4) { |
| 6089 | cur->depth = -4; |
| 6090 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, |
| 6091 | flags, cur->type); |
| 6092 | cur->depth = ret - 15; |
| 6093 | } else if (cur->depth == -4) { |
| 6094 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6095 | } else { |
| 6096 | ret = (xmlRelaxNGContentType) (cur->depth + 15); |
| 6097 | } |
| 6098 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
| 6099 | /* |
| 6100 | * The 7.3 Attribute derivation rule for groups is plugged there |
| 6101 | */ |
| 6102 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
| 6103 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6104 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ELEM, |
| 6105 | "Found forbidden pattern data/except//element(ref)\n", |
| 6106 | NULL, NULL); |
| 6107 | } |
| 6108 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6109 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ELEM, |
| 6110 | "Found forbidden pattern list//element(ref)\n", |
| 6111 | NULL, NULL); |
| 6112 | } |
| 6113 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6114 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM, |
| 6115 | "Found forbidden pattern attribute//element(ref)\n", |
| 6116 | NULL, NULL); |
| 6117 | } |
| 6118 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6119 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM, |
| 6120 | "Found forbidden pattern attribute//element(ref)\n", |
| 6121 | NULL, NULL); |
| 6122 | } |
| 6123 | /* |
| 6124 | * reset since in the simple form elements are only child |
| 6125 | * of grammar/define |
| 6126 | */ |
| 6127 | nflags = 0; |
| 6128 | ret = |
| 6129 | xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type); |
| 6130 | if (ret != XML_RELAXNG_CONTENT_EMPTY) { |
| 6131 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_EMPTY, |
| 6132 | "Element %s attributes have a content type error\n", |
| 6133 | cur->name, NULL); |
| 6134 | } |
| 6135 | ret = |
| 6136 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6137 | cur->type); |
| 6138 | if (ret == XML_RELAXNG_CONTENT_ERROR) { |
| 6139 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_ERROR, |
| 6140 | "Element %s has a content type error\n", |
| 6141 | cur->name, NULL); |
| 6142 | } else { |
| 6143 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6144 | } |
| 6145 | } else if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
| 6146 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6147 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ATTR, |
| 6148 | "Found forbidden pattern attribute//attribute\n", |
| 6149 | NULL, NULL); |
| 6150 | } |
| 6151 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6152 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ATTR, |
| 6153 | "Found forbidden pattern list//attribute\n", |
| 6154 | NULL, NULL); |
| 6155 | } |
| 6156 | if (flags & XML_RELAXNG_IN_OOMGROUP) { |
| 6157 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_GROUP_ATTR, |
| 6158 | "Found forbidden pattern oneOrMore//group//attribute\n", |
| 6159 | NULL, NULL); |
| 6160 | } |
| 6161 | if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) { |
| 6162 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_INTERLEAVE_ATTR, |
| 6163 | "Found forbidden pattern oneOrMore//interleave//attribute\n", |
| 6164 | NULL, NULL); |
| 6165 | } |
| 6166 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6167 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ATTR, |
| 6168 | "Found forbidden pattern data/except//attribute\n", |
| 6169 | NULL, NULL); |
| 6170 | } |
| 6171 | if (flags & XML_RELAXNG_IN_START) { |
| 6172 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ATTR, |
| 6173 | "Found forbidden pattern start//attribute\n", |
| 6174 | NULL, NULL); |
| 6175 | } |
| 6176 | if ((!(flags & XML_RELAXNG_IN_ONEORMORE)) |
| 6177 | && (cur->name == NULL)) { |
| 6178 | if (cur->ns == NULL) { |
| 6179 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ANYNAME_ATTR_ANCESTOR, |
| 6180 | "Found anyName attribute without oneOrMore ancestor\n", |
| 6181 | NULL, NULL); |
| 6182 | } else { |
| 6183 | xmlRngPErr(ctxt, cur->node, XML_RNGP_NSNAME_ATTR_ANCESTOR, |
| 6184 | "Found nsName attribute without oneOrMore ancestor\n", |
| 6185 | NULL, NULL); |
| 6186 | } |
| 6187 | } |
| 6188 | nflags = flags | XML_RELAXNG_IN_ATTRIBUTE; |
| 6189 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6190 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6191 | } else if ((cur->type == XML_RELAXNG_ONEORMORE) || |
| 6192 | (cur->type == XML_RELAXNG_ZEROORMORE)) { |
| 6193 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6194 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ONEMORE, |
| 6195 | "Found forbidden pattern data/except//oneOrMore\n", |
| 6196 | NULL, NULL); |
| 6197 | } |
| 6198 | if (flags & XML_RELAXNG_IN_START) { |
| 6199 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ONEMORE, |
| 6200 | "Found forbidden pattern start//oneOrMore\n", |
| 6201 | NULL, NULL); |
| 6202 | } |
| 6203 | nflags = flags | XML_RELAXNG_IN_ONEORMORE; |
| 6204 | ret = |
| 6205 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6206 | cur->type); |
| 6207 | ret = xmlRelaxNGGroupContentType(ret, ret); |
| 6208 | } else if (cur->type == XML_RELAXNG_LIST) { |
| 6209 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6210 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_LIST, |
| 6211 | "Found forbidden pattern list//list\n", NULL, |
| 6212 | NULL); |
| 6213 | } |
| 6214 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6215 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_LIST, |
| 6216 | "Found forbidden pattern data/except//list\n", |
| 6217 | NULL, NULL); |
| 6218 | } |
| 6219 | if (flags & XML_RELAXNG_IN_START) { |
| 6220 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_LIST, |
| 6221 | "Found forbidden pattern start//list\n", NULL, |
| 6222 | NULL); |
| 6223 | } |
| 6224 | nflags = flags | XML_RELAXNG_IN_LIST; |
| 6225 | ret = |
| 6226 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6227 | cur->type); |
| 6228 | } else if (cur->type == XML_RELAXNG_GROUP) { |
| 6229 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6230 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_GROUP, |
| 6231 | "Found forbidden pattern data/except//group\n", |
| 6232 | NULL, NULL); |
| 6233 | } |
| 6234 | if (flags & XML_RELAXNG_IN_START) { |
| 6235 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_GROUP, |
| 6236 | "Found forbidden pattern start//group\n", NULL, |
| 6237 | NULL); |
| 6238 | } |
| 6239 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
| 6240 | nflags = flags | XML_RELAXNG_IN_OOMGROUP; |
| 6241 | else |
| 6242 | nflags = flags; |
| 6243 | ret = |
| 6244 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6245 | cur->type); |
| 6246 | /* |
| 6247 | * The 7.3 Attribute derivation rule for groups is plugged there |
| 6248 | */ |
| 6249 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
| 6250 | } else if (cur->type == XML_RELAXNG_INTERLEAVE) { |
| 6251 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6252 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_INTERLEAVE, |
| 6253 | "Found forbidden pattern list//interleave\n", |
| 6254 | NULL, NULL); |
| 6255 | } |
| 6256 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6257 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE, |
| 6258 | "Found forbidden pattern data/except//interleave\n", |
| 6259 | NULL, NULL); |
| 6260 | } |
| 6261 | if (flags & XML_RELAXNG_IN_START) { |
| 6262 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE, |
| 6263 | "Found forbidden pattern start//interleave\n", |
| 6264 | NULL, NULL); |
| 6265 | } |
| 6266 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
| 6267 | nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE; |
| 6268 | else |
| 6269 | nflags = flags; |
| 6270 | ret = |
| 6271 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6272 | cur->type); |
| 6273 | } else if (cur->type == XML_RELAXNG_EXCEPT) { |
| 6274 | if ((cur->parent != NULL) && |
| 6275 | (cur->parent->type == XML_RELAXNG_DATATYPE)) |
| 6276 | nflags = flags | XML_RELAXNG_IN_DATAEXCEPT; |
| 6277 | else |
| 6278 | nflags = flags; |
| 6279 | ret = |
| 6280 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6281 | cur->type); |
| 6282 | } else if (cur->type == XML_RELAXNG_DATATYPE) { |
| 6283 | if (flags & XML_RELAXNG_IN_START) { |
| 6284 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_DATA, |
| 6285 | "Found forbidden pattern start//data\n", NULL, |
| 6286 | NULL); |
| 6287 | } |
| 6288 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6289 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
| 6290 | } else if (cur->type == XML_RELAXNG_VALUE) { |
| 6291 | if (flags & XML_RELAXNG_IN_START) { |
| 6292 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_VALUE, |
| 6293 | "Found forbidden pattern start//value\n", NULL, |
| 6294 | NULL); |
| 6295 | } |
| 6296 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6297 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
| 6298 | } else if (cur->type == XML_RELAXNG_TEXT) { |
| 6299 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6300 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_TEXT, |
| 6301 | "Found forbidden pattern list//text\n", NULL, |
| 6302 | NULL); |
| 6303 | } |
| 6304 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6305 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_TEXT, |
| 6306 | "Found forbidden pattern data/except//text\n", |
| 6307 | NULL, NULL); |
| 6308 | } |
| 6309 | if (flags & XML_RELAXNG_IN_START) { |
| 6310 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_TEXT, |
| 6311 | "Found forbidden pattern start//text\n", NULL, |
| 6312 | NULL); |
| 6313 | } |
| 6314 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6315 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 6316 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6317 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_EMPTY, |
| 6318 | "Found forbidden pattern data/except//empty\n", |
| 6319 | NULL, NULL); |
| 6320 | } |
| 6321 | if (flags & XML_RELAXNG_IN_START) { |
| 6322 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_EMPTY, |
| 6323 | "Found forbidden pattern start//empty\n", NULL, |
| 6324 | NULL); |
| 6325 | } |
| 6326 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6327 | } else if (cur->type == XML_RELAXNG_CHOICE) { |
| 6328 | xmlRelaxNGCheckChoiceDeterminism(ctxt, cur); |
| 6329 | ret = |
| 6330 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6331 | } else { |
| 6332 | ret = |
| 6333 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6334 | } |
| 6335 | cur = cur->next; |
| 6336 | if (ptype == XML_RELAXNG_GROUP) { |
| 6337 | val = xmlRelaxNGGroupContentType(val, ret); |
| 6338 | } else if (ptype == XML_RELAXNG_INTERLEAVE) { |
| 6339 | tmp = xmlRelaxNGGroupContentType(val, ret); |
| 6340 | if (tmp != XML_RELAXNG_CONTENT_ERROR) |
| 6341 | tmp = xmlRelaxNGMaxContentType(val, ret); |
| 6342 | } else if (ptype == XML_RELAXNG_CHOICE) { |
| 6343 | val = xmlRelaxNGMaxContentType(val, ret); |
| 6344 | } else if (ptype == XML_RELAXNG_LIST) { |
| 6345 | val = XML_RELAXNG_CONTENT_SIMPLE; |
| 6346 | } else if (ptype == XML_RELAXNG_EXCEPT) { |
| 6347 | if (ret == XML_RELAXNG_CONTENT_ERROR) |
| 6348 | val = XML_RELAXNG_CONTENT_ERROR; |
| 6349 | else |
| 6350 | val = XML_RELAXNG_CONTENT_SIMPLE; |
| 6351 | } else { |
| 6352 | val = xmlRelaxNGGroupContentType(val, ret); |
| 6353 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6354 | |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6355 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6356 | return (val); |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6357 | } |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6358 | |
| 6359 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6360 | * xmlRelaxNGParseGrammar: |
| 6361 | * @ctxt: a Relax-NG parser context |
| 6362 | * @nodes: grammar children nodes |
| 6363 | * |
| 6364 | * parse a Relax-NG <grammar> node |
| 6365 | * |
| 6366 | * Returns the internal xmlRelaxNGGrammarPtr built or |
| 6367 | * NULL in case of error |
| 6368 | */ |
| 6369 | static xmlRelaxNGGrammarPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6370 | xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) |
| 6371 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6372 | xmlRelaxNGGrammarPtr ret, tmp, old; |
| 6373 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6374 | #ifdef DEBUG_GRAMMAR |
| 6375 | xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n"); |
| 6376 | #endif |
| 6377 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6378 | ret = xmlRelaxNGNewGrammar(ctxt); |
| 6379 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6380 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6381 | |
| 6382 | /* |
| 6383 | * Link the new grammar in the tree |
| 6384 | */ |
| 6385 | ret->parent = ctxt->grammar; |
| 6386 | if (ctxt->grammar != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6387 | tmp = ctxt->grammar->children; |
| 6388 | if (tmp == NULL) { |
| 6389 | ctxt->grammar->children = ret; |
| 6390 | } else { |
| 6391 | while (tmp->next != NULL) |
| 6392 | tmp = tmp->next; |
| 6393 | tmp->next = ret; |
| 6394 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6395 | } |
| 6396 | |
| 6397 | old = ctxt->grammar; |
| 6398 | ctxt->grammar = ret; |
| 6399 | xmlRelaxNGParseGrammarContent(ctxt, nodes); |
| 6400 | ctxt->grammar = ret; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6401 | if (ctxt->grammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6402 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT, |
| 6403 | "Failed to parse <grammar> content\n", NULL, NULL); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6404 | } else if (ctxt->grammar->start == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6405 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_NO_START, |
| 6406 | "Element <grammar> has no <start>\n", NULL, NULL); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6407 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6408 | |
| 6409 | /* |
| 6410 | * Apply 4.17 mergingd rules to defines and starts |
| 6411 | */ |
| 6412 | xmlRelaxNGCombineStart(ctxt, ret); |
| 6413 | if (ret->defs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6414 | xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine, |
| 6415 | ctxt); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6416 | } |
| 6417 | |
| 6418 | /* |
| 6419 | * link together defines and refs in this grammar |
| 6420 | */ |
| 6421 | if (ret->refs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6422 | xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference, |
| 6423 | ctxt); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6424 | } |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 6425 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6426 | ctxt->grammar = old; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6427 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6428 | } |
| 6429 | |
| 6430 | /** |
| 6431 | * xmlRelaxNGParseDocument: |
| 6432 | * @ctxt: a Relax-NG parser context |
| 6433 | * @node: the root node of the RelaxNG schema |
| 6434 | * |
| 6435 | * parse a Relax-NG definition resource and build an internal |
| 6436 | * xmlRelaxNG struture which can be used to validate instances. |
| 6437 | * |
| 6438 | * Returns the internal XML RelaxNG structure built or |
| 6439 | * NULL in case of error |
| 6440 | */ |
| 6441 | static xmlRelaxNGPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6442 | xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 6443 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6444 | xmlRelaxNGPtr schema = NULL; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6445 | const xmlChar *olddefine; |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 6446 | xmlRelaxNGGrammarPtr old; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6447 | |
| 6448 | if ((ctxt == NULL) || (node == NULL)) |
| 6449 | return (NULL); |
| 6450 | |
| 6451 | schema = xmlRelaxNGNewRelaxNG(ctxt); |
| 6452 | if (schema == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6453 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6454 | |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6455 | olddefine = ctxt->define; |
| 6456 | ctxt->define = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6457 | if (IS_RELAXNG(node, "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6458 | schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6459 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6460 | xmlRelaxNGGrammarPtr tmp, ret; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6461 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6462 | schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt); |
| 6463 | if (schema->topgrammar == NULL) { |
| 6464 | return (schema); |
| 6465 | } |
| 6466 | /* |
| 6467 | * Link the new grammar in the tree |
| 6468 | */ |
| 6469 | ret->parent = ctxt->grammar; |
| 6470 | if (ctxt->grammar != NULL) { |
| 6471 | tmp = ctxt->grammar->children; |
| 6472 | if (tmp == NULL) { |
| 6473 | ctxt->grammar->children = ret; |
| 6474 | } else { |
| 6475 | while (tmp->next != NULL) |
| 6476 | tmp = tmp->next; |
| 6477 | tmp->next = ret; |
| 6478 | } |
| 6479 | } |
| 6480 | old = ctxt->grammar; |
| 6481 | ctxt->grammar = ret; |
| 6482 | xmlRelaxNGParseStart(ctxt, node); |
| 6483 | if (old != NULL) |
| 6484 | ctxt->grammar = old; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6485 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6486 | ctxt->define = olddefine; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 6487 | if (schema->topgrammar->start != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6488 | xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0); |
| 6489 | if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) { |
| 6490 | xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL); |
| 6491 | while ((schema->topgrammar->start != NULL) && |
| 6492 | (schema->topgrammar->start->type == XML_RELAXNG_NOOP) && |
| 6493 | (schema->topgrammar->start->next != NULL)) |
| 6494 | schema->topgrammar->start = |
| 6495 | schema->topgrammar->start->content; |
| 6496 | xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start, |
| 6497 | XML_RELAXNG_IN_START, XML_RELAXNG_NOOP); |
| 6498 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 6499 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6500 | #ifdef DEBUG |
| 6501 | if (schema == NULL) |
| 6502 | xmlGenericError(xmlGenericErrorContext, |
| 6503 | "xmlRelaxNGParseDocument() failed\n"); |
| 6504 | #endif |
| 6505 | |
| 6506 | return (schema); |
| 6507 | } |
| 6508 | |
| 6509 | /************************************************************************ |
| 6510 | * * |
| 6511 | * Reading RelaxNGs * |
| 6512 | * * |
| 6513 | ************************************************************************/ |
| 6514 | |
| 6515 | /** |
| 6516 | * xmlRelaxNGNewParserCtxt: |
| 6517 | * @URL: the location of the schema |
| 6518 | * |
| 6519 | * Create an XML RelaxNGs parse context for that file/resource expected |
| 6520 | * to contain an XML RelaxNGs file. |
| 6521 | * |
| 6522 | * Returns the parser context or NULL in case of error |
| 6523 | */ |
| 6524 | xmlRelaxNGParserCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6525 | xmlRelaxNGNewParserCtxt(const char *URL) |
| 6526 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6527 | xmlRelaxNGParserCtxtPtr ret; |
| 6528 | |
| 6529 | if (URL == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6530 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6531 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6532 | ret = |
| 6533 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6534 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6535 | xmlRngPErrMemory(NULL, "building parser\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6536 | return (NULL); |
| 6537 | } |
| 6538 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6539 | ret->URL = xmlStrdup((const xmlChar *) URL); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 6540 | ret->error = xmlGenericError; |
| 6541 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6542 | return (ret); |
| 6543 | } |
| 6544 | |
| 6545 | /** |
| 6546 | * xmlRelaxNGNewMemParserCtxt: |
| 6547 | * @buffer: a pointer to a char array containing the schemas |
| 6548 | * @size: the size of the array |
| 6549 | * |
| 6550 | * Create an XML RelaxNGs parse context for that memory buffer expected |
| 6551 | * to contain an XML RelaxNGs file. |
| 6552 | * |
| 6553 | * Returns the parser context or NULL in case of error |
| 6554 | */ |
| 6555 | xmlRelaxNGParserCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6556 | xmlRelaxNGNewMemParserCtxt(const char *buffer, int size) |
| 6557 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6558 | xmlRelaxNGParserCtxtPtr ret; |
| 6559 | |
| 6560 | if ((buffer == NULL) || (size <= 0)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6561 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6562 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6563 | ret = |
| 6564 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6565 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6566 | xmlRngPErrMemory(NULL, "building parser\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6567 | return (NULL); |
| 6568 | } |
| 6569 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6570 | ret->buffer = buffer; |
| 6571 | ret->size = size; |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 6572 | ret->error = xmlGenericError; |
| 6573 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6574 | return (ret); |
| 6575 | } |
| 6576 | |
| 6577 | /** |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6578 | * xmlRelaxNGNewDocParserCtxt: |
| 6579 | * @doc: a preparsed document tree |
| 6580 | * |
| 6581 | * Create an XML RelaxNGs parser context for that document. |
| 6582 | * Note: since the process of compiling a RelaxNG schemas modifies the |
| 6583 | * document, the @doc parameter is duplicated internally. |
| 6584 | * |
| 6585 | * Returns the parser context or NULL in case of error |
| 6586 | */ |
| 6587 | xmlRelaxNGParserCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6588 | xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc) |
| 6589 | { |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6590 | xmlRelaxNGParserCtxtPtr ret; |
| 6591 | xmlDocPtr copy; |
| 6592 | |
| 6593 | if (doc == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6594 | return (NULL); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6595 | copy = xmlCopyDoc(doc, 1); |
| 6596 | if (copy == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6597 | return (NULL); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6598 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6599 | ret = |
| 6600 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6601 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6602 | xmlRngPErrMemory(NULL, "building parser\n"); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6603 | return (NULL); |
| 6604 | } |
| 6605 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6606 | ret->document = copy; |
| 6607 | ret->userData = xmlGenericErrorContext; |
| 6608 | return (ret); |
| 6609 | } |
| 6610 | |
| 6611 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6612 | * xmlRelaxNGFreeParserCtxt: |
| 6613 | * @ctxt: the schema parser context |
| 6614 | * |
| 6615 | * Free the resources associated to the schema parser context |
| 6616 | */ |
| 6617 | void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6618 | xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt) |
| 6619 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6620 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6621 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6622 | if (ctxt->URL != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6623 | xmlFree(ctxt->URL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6624 | if (ctxt->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6625 | xmlRelaxNGFreeDocument(ctxt->doc); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 6626 | if (ctxt->interleaves != NULL) |
| 6627 | xmlHashFree(ctxt->interleaves, NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6628 | if (ctxt->documents != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6629 | xmlRelaxNGFreeDocumentList(ctxt->documents); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 6630 | if (ctxt->includes != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6631 | xmlRelaxNGFreeIncludeList(ctxt->includes); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6632 | if (ctxt->docTab != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6633 | xmlFree(ctxt->docTab); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 6634 | if (ctxt->incTab != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6635 | xmlFree(ctxt->incTab); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6636 | if (ctxt->defTab != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6637 | int i; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6638 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6639 | for (i = 0; i < ctxt->defNr; i++) |
| 6640 | xmlRelaxNGFreeDefine(ctxt->defTab[i]); |
| 6641 | xmlFree(ctxt->defTab); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6642 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6643 | xmlFree(ctxt); |
| 6644 | } |
| 6645 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6646 | /** |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6647 | * xmlRelaxNGNormExtSpace: |
| 6648 | * @value: a value |
| 6649 | * |
| 6650 | * Removes the leading and ending spaces of the value |
| 6651 | * The string is modified "in situ" |
| 6652 | */ |
| 6653 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6654 | xmlRelaxNGNormExtSpace(xmlChar * value) |
| 6655 | { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6656 | xmlChar *start = value; |
| 6657 | xmlChar *cur = value; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6658 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6659 | if (value == NULL) |
| 6660 | return; |
| 6661 | |
| 6662 | while (IS_BLANK(*cur)) |
| 6663 | cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6664 | if (cur == start) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6665 | do { |
| 6666 | while ((*cur != 0) && (!IS_BLANK(*cur))) |
| 6667 | cur++; |
| 6668 | if (*cur == 0) |
| 6669 | return; |
| 6670 | start = cur; |
| 6671 | while (IS_BLANK(*cur)) |
| 6672 | cur++; |
| 6673 | if (*cur == 0) { |
| 6674 | *start = 0; |
| 6675 | return; |
| 6676 | } |
| 6677 | } while (1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6678 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6679 | do { |
| 6680 | while ((*cur != 0) && (!IS_BLANK(*cur))) |
| 6681 | *start++ = *cur++; |
| 6682 | if (*cur == 0) { |
| 6683 | *start = 0; |
| 6684 | return; |
| 6685 | } |
| 6686 | /* don't try to normalize the inner spaces */ |
| 6687 | while (IS_BLANK(*cur)) |
| 6688 | cur++; |
| 6689 | *start++ = *cur++; |
| 6690 | if (*cur == 0) { |
| 6691 | *start = 0; |
| 6692 | return; |
| 6693 | } |
| 6694 | } while (1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6695 | } |
| 6696 | } |
| 6697 | |
| 6698 | /** |
| 6699 | * xmlRelaxNGCheckAttributes: |
| 6700 | * @ctxt: a Relax-NG parser context |
| 6701 | * @node: a Relax-NG node |
| 6702 | * |
| 6703 | * Check all the attributes on the given node |
| 6704 | */ |
| 6705 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6706 | xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 6707 | { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6708 | xmlAttrPtr cur, next; |
| 6709 | |
| 6710 | cur = node->properties; |
| 6711 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6712 | next = cur->next; |
| 6713 | if ((cur->ns == NULL) || |
| 6714 | (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
| 6715 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
| 6716 | if ((!xmlStrEqual(node->name, BAD_CAST "element")) && |
| 6717 | (!xmlStrEqual(node->name, BAD_CAST "attribute")) && |
| 6718 | (!xmlStrEqual(node->name, BAD_CAST "ref")) && |
| 6719 | (!xmlStrEqual(node->name, BAD_CAST "parentRef")) && |
| 6720 | (!xmlStrEqual(node->name, BAD_CAST "param")) && |
| 6721 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
| 6722 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6723 | "Attribute %s is not allowed on %s\n", |
| 6724 | cur->name, node->name); |
| 6725 | } |
| 6726 | } else if (xmlStrEqual(cur->name, BAD_CAST "type")) { |
| 6727 | if ((!xmlStrEqual(node->name, BAD_CAST "value")) && |
| 6728 | (!xmlStrEqual(node->name, BAD_CAST "data"))) { |
| 6729 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6730 | "Attribute %s is not allowed on %s\n", |
| 6731 | cur->name, node->name); |
| 6732 | } |
| 6733 | } else if (xmlStrEqual(cur->name, BAD_CAST "href")) { |
| 6734 | if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) && |
| 6735 | (!xmlStrEqual(node->name, BAD_CAST "include"))) { |
| 6736 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6737 | "Attribute %s is not allowed on %s\n", |
| 6738 | cur->name, node->name); |
| 6739 | } |
| 6740 | } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) { |
| 6741 | if ((!xmlStrEqual(node->name, BAD_CAST "start")) && |
| 6742 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
| 6743 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6744 | "Attribute %s is not allowed on %s\n", |
| 6745 | cur->name, node->name); |
| 6746 | } |
| 6747 | } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) { |
| 6748 | xmlChar *val; |
| 6749 | xmlURIPtr uri; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6750 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6751 | val = xmlNodeListGetString(node->doc, cur->children, 1); |
| 6752 | if (val != NULL) { |
| 6753 | if (val[0] != 0) { |
| 6754 | uri = xmlParseURI((const char *) val); |
| 6755 | if (uri == NULL) { |
| 6756 | xmlRngPErr(ctxt, node, XML_RNGP_INVALID_URI, |
| 6757 | "Attribute %s contains invalid URI %s\n", |
| 6758 | cur->name, val); |
| 6759 | } else { |
| 6760 | if (uri->scheme == NULL) { |
| 6761 | xmlRngPErr(ctxt, node, XML_RNGP_URI_NOT_ABSOLUTE, |
| 6762 | "Attribute %s URI %s is not absolute\n", |
| 6763 | cur->name, val); |
| 6764 | } |
| 6765 | if (uri->fragment != NULL) { |
| 6766 | xmlRngPErr(ctxt, node, XML_RNGP_URI_FRAGMENT, |
| 6767 | "Attribute %s URI %s has a fragment ID\n", |
| 6768 | cur->name, val); |
| 6769 | } |
| 6770 | xmlFreeURI(uri); |
| 6771 | } |
| 6772 | } |
| 6773 | xmlFree(val); |
| 6774 | } |
| 6775 | } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) { |
| 6776 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_ATTRIBUTE, |
| 6777 | "Unknown attribute %s on %s\n", cur->name, |
| 6778 | node->name); |
| 6779 | } |
| 6780 | } |
| 6781 | cur = next; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6782 | } |
| 6783 | } |
| 6784 | |
| 6785 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6786 | * xmlRelaxNGCleanupTree: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6787 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6788 | * @root: an xmlNodePtr subtree |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6789 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6790 | * Cleanup the subtree from unwanted nodes for parsing, resolve |
| 6791 | * Include and externalRef lookups. |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6792 | */ |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6793 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6794 | xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root) |
| 6795 | { |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6796 | xmlNodePtr cur, delete; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6797 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6798 | delete = NULL; |
| 6799 | cur = root; |
| 6800 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6801 | if (delete != NULL) { |
| 6802 | xmlUnlinkNode(delete); |
| 6803 | xmlFreeNode(delete); |
| 6804 | delete = NULL; |
| 6805 | } |
| 6806 | if (cur->type == XML_ELEMENT_NODE) { |
| 6807 | /* |
| 6808 | * Simplification 4.1. Annotations |
| 6809 | */ |
| 6810 | if ((cur->ns == NULL) || |
| 6811 | (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
| 6812 | if ((cur->parent != NULL) && |
| 6813 | (cur->parent->type == XML_ELEMENT_NODE) && |
| 6814 | ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) || |
| 6815 | (xmlStrEqual(cur->parent->name, BAD_CAST "value")) || |
| 6816 | (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) { |
| 6817 | xmlRngPErr(ctxt, cur, XML_RNGP_FOREIGN_ELEMENT, |
| 6818 | "element %s doesn't allow foreign elements\n", |
| 6819 | cur->parent->name, NULL); |
| 6820 | } |
| 6821 | delete = cur; |
| 6822 | goto skip_children; |
| 6823 | } else { |
| 6824 | xmlRelaxNGCleanupAttributes(ctxt, cur); |
| 6825 | if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) { |
| 6826 | xmlChar *href, *ns, *base, *URL; |
| 6827 | xmlRelaxNGDocumentPtr docu; |
| 6828 | xmlNodePtr tmp; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6829 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6830 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6831 | if (ns == NULL) { |
| 6832 | tmp = cur->parent; |
| 6833 | while ((tmp != NULL) && |
| 6834 | (tmp->type == XML_ELEMENT_NODE)) { |
| 6835 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 6836 | if (ns != NULL) |
| 6837 | break; |
| 6838 | tmp = tmp->parent; |
| 6839 | } |
| 6840 | } |
| 6841 | href = xmlGetProp(cur, BAD_CAST "href"); |
| 6842 | if (href == NULL) { |
| 6843 | xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF, |
| 6844 | "xmlRelaxNGParse: externalRef has no href attribute\n", |
| 6845 | NULL, NULL); |
| 6846 | delete = cur; |
| 6847 | goto skip_children; |
| 6848 | } |
| 6849 | base = xmlNodeGetBase(cur->doc, cur); |
| 6850 | URL = xmlBuildURI(href, base); |
| 6851 | if (URL == NULL) { |
| 6852 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
| 6853 | "Failed to compute URL for externalRef %s\n", |
| 6854 | href, NULL); |
| 6855 | if (href != NULL) |
| 6856 | xmlFree(href); |
| 6857 | if (base != NULL) |
| 6858 | xmlFree(base); |
| 6859 | delete = cur; |
| 6860 | goto skip_children; |
| 6861 | } |
| 6862 | if (href != NULL) |
| 6863 | xmlFree(href); |
| 6864 | if (base != NULL) |
| 6865 | xmlFree(base); |
| 6866 | docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns); |
| 6867 | if (docu == NULL) { |
| 6868 | xmlRngPErr(ctxt, cur, XML_RNGP_EXTERNAL_REF_FAILURE, |
| 6869 | "Failed to load externalRef %s\n", URL, |
| 6870 | NULL); |
| 6871 | xmlFree(URL); |
| 6872 | delete = cur; |
| 6873 | goto skip_children; |
| 6874 | } |
| 6875 | if (ns != NULL) |
| 6876 | xmlFree(ns); |
| 6877 | xmlFree(URL); |
| 6878 | cur->_private = docu; |
| 6879 | } else if (xmlStrEqual(cur->name, BAD_CAST "include")) { |
| 6880 | xmlChar *href, *ns, *base, *URL; |
| 6881 | xmlRelaxNGIncludePtr incl; |
| 6882 | xmlNodePtr tmp; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6883 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6884 | href = xmlGetProp(cur, BAD_CAST "href"); |
| 6885 | if (href == NULL) { |
| 6886 | xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF, |
| 6887 | "xmlRelaxNGParse: include has no href attribute\n", |
| 6888 | NULL, NULL); |
| 6889 | delete = cur; |
| 6890 | goto skip_children; |
| 6891 | } |
| 6892 | base = xmlNodeGetBase(cur->doc, cur); |
| 6893 | URL = xmlBuildURI(href, base); |
| 6894 | if (URL == NULL) { |
| 6895 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
| 6896 | "Failed to compute URL for include %s\n", |
| 6897 | href, NULL); |
| 6898 | if (href != NULL) |
| 6899 | xmlFree(href); |
| 6900 | if (base != NULL) |
| 6901 | xmlFree(base); |
| 6902 | delete = cur; |
| 6903 | goto skip_children; |
| 6904 | } |
| 6905 | if (href != NULL) |
| 6906 | xmlFree(href); |
| 6907 | if (base != NULL) |
| 6908 | xmlFree(base); |
| 6909 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6910 | if (ns == NULL) { |
| 6911 | tmp = cur->parent; |
| 6912 | while ((tmp != NULL) && |
| 6913 | (tmp->type == XML_ELEMENT_NODE)) { |
| 6914 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 6915 | if (ns != NULL) |
| 6916 | break; |
| 6917 | tmp = tmp->parent; |
| 6918 | } |
| 6919 | } |
| 6920 | incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns); |
| 6921 | if (ns != NULL) |
| 6922 | xmlFree(ns); |
| 6923 | if (incl == NULL) { |
| 6924 | xmlRngPErr(ctxt, cur, XML_RNGP_INCLUDE_FAILURE, |
| 6925 | "Failed to load include %s\n", URL, |
| 6926 | NULL); |
| 6927 | xmlFree(URL); |
| 6928 | delete = cur; |
| 6929 | goto skip_children; |
| 6930 | } |
| 6931 | xmlFree(URL); |
| 6932 | cur->_private = incl; |
| 6933 | } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) || |
| 6934 | (xmlStrEqual(cur->name, BAD_CAST "attribute"))) |
| 6935 | { |
| 6936 | xmlChar *name, *ns; |
| 6937 | xmlNodePtr text = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6938 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6939 | /* |
| 6940 | * Simplification 4.8. name attribute of element |
| 6941 | * and attribute elements |
| 6942 | */ |
| 6943 | name = xmlGetProp(cur, BAD_CAST "name"); |
| 6944 | if (name != NULL) { |
| 6945 | if (cur->children == NULL) { |
| 6946 | text = |
| 6947 | xmlNewChild(cur, cur->ns, BAD_CAST "name", |
| 6948 | name); |
| 6949 | } else { |
| 6950 | xmlNodePtr node; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6951 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6952 | node = xmlNewNode(cur->ns, BAD_CAST "name"); |
| 6953 | if (node != NULL) { |
| 6954 | xmlAddPrevSibling(cur->children, node); |
| 6955 | text = xmlNewText(name); |
| 6956 | xmlAddChild(node, text); |
| 6957 | text = node; |
| 6958 | } |
| 6959 | } |
| 6960 | if (text == NULL) { |
| 6961 | xmlRngPErr(ctxt, cur, XML_RNGP_CREATE_FAILURE, |
| 6962 | "Failed to create a name %s element\n", |
| 6963 | name, NULL); |
| 6964 | } |
| 6965 | xmlUnsetProp(cur, BAD_CAST "name"); |
| 6966 | xmlFree(name); |
| 6967 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6968 | if (ns != NULL) { |
| 6969 | if (text != NULL) { |
| 6970 | xmlSetProp(text, BAD_CAST "ns", ns); |
| 6971 | /* xmlUnsetProp(cur, BAD_CAST "ns"); */ |
| 6972 | } |
| 6973 | xmlFree(ns); |
| 6974 | } else if (xmlStrEqual(cur->name, |
| 6975 | BAD_CAST "attribute")) { |
| 6976 | xmlSetProp(text, BAD_CAST "ns", BAD_CAST ""); |
| 6977 | } |
| 6978 | } |
| 6979 | } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) || |
| 6980 | (xmlStrEqual(cur->name, BAD_CAST "nsName")) || |
| 6981 | (xmlStrEqual(cur->name, BAD_CAST "value"))) { |
| 6982 | /* |
| 6983 | * Simplification 4.8. name attribute of element |
| 6984 | * and attribute elements |
| 6985 | */ |
| 6986 | if (xmlHasProp(cur, BAD_CAST "ns") == NULL) { |
| 6987 | xmlNodePtr node; |
| 6988 | xmlChar *ns = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6989 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6990 | node = cur->parent; |
| 6991 | while ((node != NULL) && |
| 6992 | (node->type == XML_ELEMENT_NODE)) { |
| 6993 | ns = xmlGetProp(node, BAD_CAST "ns"); |
| 6994 | if (ns != NULL) { |
| 6995 | break; |
| 6996 | } |
| 6997 | node = node->parent; |
| 6998 | } |
| 6999 | if (ns == NULL) { |
| 7000 | xmlSetProp(cur, BAD_CAST "ns", BAD_CAST ""); |
| 7001 | } else { |
| 7002 | xmlSetProp(cur, BAD_CAST "ns", ns); |
| 7003 | xmlFree(ns); |
| 7004 | } |
| 7005 | } |
| 7006 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
| 7007 | xmlChar *name, *local, *prefix; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7008 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7009 | /* |
| 7010 | * Simplification: 4.10. QNames |
| 7011 | */ |
| 7012 | name = xmlNodeGetContent(cur); |
| 7013 | if (name != NULL) { |
| 7014 | local = xmlSplitQName2(name, &prefix); |
| 7015 | if (local != NULL) { |
| 7016 | xmlNsPtr ns; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7017 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7018 | ns = xmlSearchNs(cur->doc, cur, prefix); |
| 7019 | if (ns == NULL) { |
| 7020 | xmlRngPErr(ctxt, cur, |
| 7021 | XML_RNGP_PREFIX_UNDEFINED, |
| 7022 | "xmlRelaxNGParse: no namespace for prefix %s\n", |
| 7023 | prefix, NULL); |
| 7024 | } else { |
| 7025 | xmlSetProp(cur, BAD_CAST "ns", |
| 7026 | ns->href); |
| 7027 | xmlNodeSetContent(cur, local); |
| 7028 | } |
| 7029 | xmlFree(local); |
| 7030 | xmlFree(prefix); |
| 7031 | } |
| 7032 | xmlFree(name); |
| 7033 | } |
| 7034 | } |
| 7035 | /* |
| 7036 | * 4.16 |
| 7037 | */ |
| 7038 | if (xmlStrEqual(cur->name, BAD_CAST "nsName")) { |
| 7039 | if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
| 7040 | xmlRngPErr(ctxt, cur, |
| 7041 | XML_RNGP_PAT_NSNAME_EXCEPT_NSNAME, |
| 7042 | "Found nsName/except//nsName forbidden construct\n", |
| 7043 | NULL, NULL); |
| 7044 | } |
| 7045 | } |
| 7046 | } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) && |
| 7047 | (cur != root)) { |
| 7048 | int oldflags = ctxt->flags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7049 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7050 | /* |
| 7051 | * 4.16 |
| 7052 | */ |
| 7053 | if ((cur->parent != NULL) && |
| 7054 | (xmlStrEqual |
| 7055 | (cur->parent->name, BAD_CAST "anyName"))) { |
| 7056 | ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT; |
| 7057 | xmlRelaxNGCleanupTree(ctxt, cur); |
| 7058 | ctxt->flags = oldflags; |
| 7059 | goto skip_children; |
| 7060 | } else if ((cur->parent != NULL) && |
| 7061 | (xmlStrEqual |
| 7062 | (cur->parent->name, BAD_CAST "nsName"))) { |
| 7063 | ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT; |
| 7064 | xmlRelaxNGCleanupTree(ctxt, cur); |
| 7065 | ctxt->flags = oldflags; |
| 7066 | goto skip_children; |
| 7067 | } |
| 7068 | } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) { |
| 7069 | /* |
| 7070 | * 4.16 |
| 7071 | */ |
| 7072 | if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) { |
| 7073 | xmlRngPErr(ctxt, cur, |
| 7074 | XML_RNGP_PAT_ANYNAME_EXCEPT_ANYNAME, |
| 7075 | "Found anyName/except//anyName forbidden construct\n", |
| 7076 | NULL, NULL); |
| 7077 | } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
| 7078 | xmlRngPErr(ctxt, cur, |
| 7079 | XML_RNGP_PAT_NSNAME_EXCEPT_ANYNAME, |
| 7080 | "Found nsName/except//anyName forbidden construct\n", |
| 7081 | NULL, NULL); |
| 7082 | } |
| 7083 | } |
| 7084 | /* |
| 7085 | * Thisd is not an else since "include" is transformed |
| 7086 | * into a div |
| 7087 | */ |
| 7088 | if (xmlStrEqual(cur->name, BAD_CAST "div")) { |
| 7089 | xmlChar *ns; |
| 7090 | xmlNodePtr child, ins, tmp; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7091 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7092 | /* |
| 7093 | * implements rule 4.11 |
| 7094 | */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7095 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7096 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 7097 | |
| 7098 | child = cur->children; |
| 7099 | ins = cur; |
| 7100 | while (child != NULL) { |
| 7101 | if (ns != NULL) { |
| 7102 | if (!xmlHasProp(child, BAD_CAST "ns")) { |
| 7103 | xmlSetProp(child, BAD_CAST "ns", ns); |
| 7104 | } |
| 7105 | } |
| 7106 | tmp = child->next; |
| 7107 | xmlUnlinkNode(child); |
| 7108 | ins = xmlAddNextSibling(ins, child); |
| 7109 | child = tmp; |
| 7110 | } |
| 7111 | if (ns != NULL) |
| 7112 | xmlFree(ns); |
| 7113 | delete = cur; |
| 7114 | goto skip_children; |
| 7115 | } |
| 7116 | } |
| 7117 | } |
| 7118 | /* |
| 7119 | * Simplification 4.2 whitespaces |
| 7120 | */ |
| 7121 | else if ((cur->type == XML_TEXT_NODE) || |
| 7122 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 7123 | if (IS_BLANK_NODE(cur)) { |
| 7124 | if (cur->parent->type == XML_ELEMENT_NODE) { |
| 7125 | if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value")) |
| 7126 | && |
| 7127 | (!xmlStrEqual |
| 7128 | (cur->parent->name, BAD_CAST "param"))) |
| 7129 | delete = cur; |
| 7130 | } else { |
| 7131 | delete = cur; |
| 7132 | goto skip_children; |
| 7133 | } |
| 7134 | } |
| 7135 | } else { |
| 7136 | delete = cur; |
| 7137 | goto skip_children; |
| 7138 | } |
| 7139 | |
| 7140 | /* |
| 7141 | * Skip to next node |
| 7142 | */ |
| 7143 | if (cur->children != NULL) { |
| 7144 | if ((cur->children->type != XML_ENTITY_DECL) && |
| 7145 | (cur->children->type != XML_ENTITY_REF_NODE) && |
| 7146 | (cur->children->type != XML_ENTITY_NODE)) { |
| 7147 | cur = cur->children; |
| 7148 | continue; |
| 7149 | } |
| 7150 | } |
| 7151 | skip_children: |
| 7152 | if (cur->next != NULL) { |
| 7153 | cur = cur->next; |
| 7154 | continue; |
| 7155 | } |
| 7156 | |
| 7157 | do { |
| 7158 | cur = cur->parent; |
| 7159 | if (cur == NULL) |
| 7160 | break; |
| 7161 | if (cur == root) { |
| 7162 | cur = NULL; |
| 7163 | break; |
| 7164 | } |
| 7165 | if (cur->next != NULL) { |
| 7166 | cur = cur->next; |
| 7167 | break; |
| 7168 | } |
| 7169 | } while (cur != NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7170 | } |
| 7171 | if (delete != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7172 | xmlUnlinkNode(delete); |
| 7173 | xmlFreeNode(delete); |
| 7174 | delete = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7175 | } |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7176 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7177 | |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7178 | /** |
| 7179 | * xmlRelaxNGCleanupDoc: |
| 7180 | * @ctxt: a Relax-NG parser context |
| 7181 | * @doc: an xmldocPtr document pointer |
| 7182 | * |
| 7183 | * Cleanup the document from unwanted nodes for parsing, resolve |
| 7184 | * Include and externalRef lookups. |
| 7185 | * |
| 7186 | * Returns the cleaned up document or NULL in case of error |
| 7187 | */ |
| 7188 | static xmlDocPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7189 | xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc) |
| 7190 | { |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7191 | xmlNodePtr root; |
| 7192 | |
| 7193 | /* |
| 7194 | * Extract the root |
| 7195 | */ |
| 7196 | root = xmlDocGetRootElement(doc); |
| 7197 | if (root == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7198 | xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n", |
| 7199 | ctxt->URL, NULL); |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7200 | return (NULL); |
| 7201 | } |
| 7202 | xmlRelaxNGCleanupTree(ctxt, root); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7203 | return (doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7204 | } |
| 7205 | |
| 7206 | /** |
| 7207 | * xmlRelaxNGParse: |
| 7208 | * @ctxt: a Relax-NG parser context |
| 7209 | * |
| 7210 | * parse a schema definition resource and build an internal |
| 7211 | * XML Shema struture which can be used to validate instances. |
| 7212 | * *WARNING* this interface is highly subject to change |
| 7213 | * |
| 7214 | * Returns the internal XML RelaxNG structure built from the resource or |
| 7215 | * NULL in case of error |
| 7216 | */ |
| 7217 | xmlRelaxNGPtr |
| 7218 | xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt) |
| 7219 | { |
| 7220 | xmlRelaxNGPtr ret = NULL; |
| 7221 | xmlDocPtr doc; |
| 7222 | xmlNodePtr root; |
| 7223 | |
| 7224 | xmlRelaxNGInitTypes(); |
| 7225 | |
| 7226 | if (ctxt == NULL) |
| 7227 | return (NULL); |
| 7228 | |
| 7229 | /* |
| 7230 | * First step is to parse the input document into an DOM/Infoset |
| 7231 | */ |
| 7232 | if (ctxt->URL != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7233 | doc = xmlParseFile((const char *) ctxt->URL); |
| 7234 | if (doc == NULL) { |
| 7235 | xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR, |
| 7236 | "xmlRelaxNGParse: could not load %s\n", ctxt->URL, |
| 7237 | NULL); |
| 7238 | return (NULL); |
| 7239 | } |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7240 | } else if (ctxt->buffer != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7241 | doc = xmlParseMemory(ctxt->buffer, ctxt->size); |
| 7242 | if (doc == NULL) { |
| 7243 | xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR, |
| 7244 | "xmlRelaxNGParse: could not parse schemas\n", NULL, |
| 7245 | NULL); |
| 7246 | return (NULL); |
| 7247 | } |
| 7248 | doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
| 7249 | ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 7250 | } else if (ctxt->document != NULL) { |
| 7251 | doc = ctxt->document; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7252 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7253 | xmlRngPErr(ctxt, NULL, XML_RNGP_EMPTY, |
| 7254 | "xmlRelaxNGParse: nothing to parse\n", NULL, NULL); |
| 7255 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7256 | } |
| 7257 | ctxt->document = doc; |
| 7258 | |
| 7259 | /* |
| 7260 | * Some preprocessing of the document content |
| 7261 | */ |
| 7262 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 7263 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7264 | xmlFreeDoc(ctxt->document); |
| 7265 | ctxt->document = NULL; |
| 7266 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7267 | } |
| 7268 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7269 | /* |
| 7270 | * Then do the parsing for good |
| 7271 | */ |
| 7272 | root = xmlDocGetRootElement(doc); |
| 7273 | if (root == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7274 | xmlRngPErr(ctxt, (xmlNodePtr) doc, |
| 7275 | XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n", |
| 7276 | ctxt->URL, NULL); |
| 7277 | xmlFreeDoc(doc); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7278 | return (NULL); |
| 7279 | } |
| 7280 | ret = xmlRelaxNGParseDocument(ctxt, root); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7281 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7282 | xmlFreeDoc(doc); |
| 7283 | return (NULL); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7284 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7285 | |
| 7286 | /* |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7287 | * Check the ref/defines links |
| 7288 | */ |
| 7289 | /* |
| 7290 | * try to preprocess interleaves |
| 7291 | */ |
| 7292 | if (ctxt->interleaves != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7293 | xmlHashScan(ctxt->interleaves, |
| 7294 | (xmlHashScanner) xmlRelaxNGComputeInterleaves, ctxt); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7295 | } |
| 7296 | |
| 7297 | /* |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7298 | * if there was a parsing error return NULL |
| 7299 | */ |
| 7300 | if (ctxt->nbErrors > 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7301 | xmlRelaxNGFree(ret); |
| 7302 | ctxt->document = NULL; |
| 7303 | xmlFreeDoc(doc); |
| 7304 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7305 | } |
| 7306 | |
| 7307 | /* |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 7308 | * try to compile (parts of) the schemas |
| 7309 | */ |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7310 | if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) { |
| 7311 | if (ret->topgrammar->start->type != XML_RELAXNG_START) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7312 | xmlRelaxNGDefinePtr def; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7313 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7314 | def = xmlRelaxNGNewDefine(ctxt, NULL); |
| 7315 | if (def != NULL) { |
| 7316 | def->type = XML_RELAXNG_START; |
| 7317 | def->content = ret->topgrammar->start; |
| 7318 | ret->topgrammar->start = def; |
| 7319 | } |
| 7320 | } |
| 7321 | xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7322 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 7323 | |
| 7324 | /* |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7325 | * Transfer the pointer for cleanup at the schema level. |
| 7326 | */ |
| 7327 | ret->doc = doc; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7328 | ctxt->document = NULL; |
| 7329 | ret->documents = ctxt->documents; |
| 7330 | ctxt->documents = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7331 | |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 7332 | ret->includes = ctxt->includes; |
| 7333 | ctxt->includes = NULL; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7334 | ret->defNr = ctxt->defNr; |
| 7335 | ret->defTab = ctxt->defTab; |
| 7336 | ctxt->defTab = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7337 | if (ctxt->idref == 1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7338 | ret->idref = 1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7339 | |
| 7340 | return (ret); |
| 7341 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7342 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7343 | /** |
| 7344 | * xmlRelaxNGSetParserErrors: |
| 7345 | * @ctxt: a Relax-NG validation context |
| 7346 | * @err: the error callback |
| 7347 | * @warn: the warning callback |
| 7348 | * @ctx: contextual data for the callbacks |
| 7349 | * |
| 7350 | * Set the callback functions used to handle errors for a validation context |
| 7351 | */ |
| 7352 | void |
| 7353 | xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7354 | xmlRelaxNGValidityErrorFunc err, |
| 7355 | xmlRelaxNGValidityWarningFunc warn, void *ctx) |
| 7356 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7357 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7358 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7359 | ctxt->error = err; |
| 7360 | ctxt->warning = warn; |
| 7361 | ctxt->userData = ctx; |
| 7362 | } |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 7363 | |
| 7364 | /** |
| 7365 | * xmlRelaxNGGetParserErrors: |
| 7366 | * @ctxt: a Relax-NG validation context |
| 7367 | * @err: the error callback result |
| 7368 | * @warn: the warning callback result |
| 7369 | * @ctx: contextual data for the callbacks result |
| 7370 | * |
| 7371 | * Get the callback information used to handle errors for a validation context |
| 7372 | * |
| 7373 | * Returns -1 in case of failure, 0 otherwise. |
| 7374 | */ |
| 7375 | int |
| 7376 | xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7377 | xmlRelaxNGValidityErrorFunc * err, |
| 7378 | xmlRelaxNGValidityWarningFunc * warn, void **ctx) |
| 7379 | { |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 7380 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7381 | return (-1); |
| 7382 | if (err != NULL) |
| 7383 | *err = ctxt->error; |
| 7384 | if (warn != NULL) |
| 7385 | *warn = ctxt->warning; |
| 7386 | if (ctx != NULL) |
| 7387 | *ctx = ctxt->userData; |
| 7388 | return (0); |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 7389 | } |
| 7390 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 7391 | #ifdef LIBXML_OUTPUT_ENABLED |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7392 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7393 | /************************************************************************ |
| 7394 | * * |
| 7395 | * Dump back a compiled form * |
| 7396 | * * |
| 7397 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7398 | static void xmlRelaxNGDumpDefine(FILE * output, |
| 7399 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7400 | |
| 7401 | /** |
| 7402 | * xmlRelaxNGDumpDefines: |
| 7403 | * @output: the file output |
| 7404 | * @defines: a list of define structures |
| 7405 | * |
| 7406 | * Dump a RelaxNG structure back |
| 7407 | */ |
| 7408 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7409 | xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines) |
| 7410 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7411 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7412 | xmlRelaxNGDumpDefine(output, defines); |
| 7413 | defines = defines->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7414 | } |
| 7415 | } |
| 7416 | |
| 7417 | /** |
| 7418 | * xmlRelaxNGDumpDefine: |
| 7419 | * @output: the file output |
| 7420 | * @define: a define structure |
| 7421 | * |
| 7422 | * Dump a RelaxNG structure back |
| 7423 | */ |
| 7424 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7425 | xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define) |
| 7426 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7427 | if (define == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7428 | return; |
| 7429 | switch (define->type) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7430 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7431 | fprintf(output, "<empty/>\n"); |
| 7432 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7433 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7434 | fprintf(output, "<notAllowed/>\n"); |
| 7435 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7436 | case XML_RELAXNG_TEXT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7437 | fprintf(output, "<text/>\n"); |
| 7438 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7439 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7440 | fprintf(output, "<element>\n"); |
| 7441 | if (define->name != NULL) { |
| 7442 | fprintf(output, "<name"); |
| 7443 | if (define->ns != NULL) |
| 7444 | fprintf(output, " ns=\"%s\"", define->ns); |
| 7445 | fprintf(output, ">%s</name>\n", define->name); |
| 7446 | } |
| 7447 | xmlRelaxNGDumpDefines(output, define->attrs); |
| 7448 | xmlRelaxNGDumpDefines(output, define->content); |
| 7449 | fprintf(output, "</element>\n"); |
| 7450 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7451 | case XML_RELAXNG_LIST: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7452 | fprintf(output, "<list>\n"); |
| 7453 | xmlRelaxNGDumpDefines(output, define->content); |
| 7454 | fprintf(output, "</list>\n"); |
| 7455 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7456 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7457 | fprintf(output, "<oneOrMore>\n"); |
| 7458 | xmlRelaxNGDumpDefines(output, define->content); |
| 7459 | fprintf(output, "</oneOrMore>\n"); |
| 7460 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7461 | case XML_RELAXNG_ZEROORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7462 | fprintf(output, "<zeroOrMore>\n"); |
| 7463 | xmlRelaxNGDumpDefines(output, define->content); |
| 7464 | fprintf(output, "</zeroOrMore>\n"); |
| 7465 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7466 | case XML_RELAXNG_CHOICE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7467 | fprintf(output, "<choice>\n"); |
| 7468 | xmlRelaxNGDumpDefines(output, define->content); |
| 7469 | fprintf(output, "</choice>\n"); |
| 7470 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7471 | case XML_RELAXNG_GROUP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7472 | fprintf(output, "<group>\n"); |
| 7473 | xmlRelaxNGDumpDefines(output, define->content); |
| 7474 | fprintf(output, "</group>\n"); |
| 7475 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7476 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7477 | fprintf(output, "<interleave>\n"); |
| 7478 | xmlRelaxNGDumpDefines(output, define->content); |
| 7479 | fprintf(output, "</interleave>\n"); |
| 7480 | break; |
| 7481 | case XML_RELAXNG_OPTIONAL: |
| 7482 | fprintf(output, "<optional>\n"); |
| 7483 | xmlRelaxNGDumpDefines(output, define->content); |
| 7484 | fprintf(output, "</optional>\n"); |
| 7485 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7486 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7487 | fprintf(output, "<attribute>\n"); |
| 7488 | xmlRelaxNGDumpDefines(output, define->content); |
| 7489 | fprintf(output, "</attribute>\n"); |
| 7490 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7491 | case XML_RELAXNG_DEF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7492 | fprintf(output, "<define"); |
| 7493 | if (define->name != NULL) |
| 7494 | fprintf(output, " name=\"%s\"", define->name); |
| 7495 | fprintf(output, ">\n"); |
| 7496 | xmlRelaxNGDumpDefines(output, define->content); |
| 7497 | fprintf(output, "</define>\n"); |
| 7498 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7499 | case XML_RELAXNG_REF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7500 | fprintf(output, "<ref"); |
| 7501 | if (define->name != NULL) |
| 7502 | fprintf(output, " name=\"%s\"", define->name); |
| 7503 | fprintf(output, ">\n"); |
| 7504 | xmlRelaxNGDumpDefines(output, define->content); |
| 7505 | fprintf(output, "</ref>\n"); |
| 7506 | break; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7507 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7508 | fprintf(output, "<parentRef"); |
| 7509 | if (define->name != NULL) |
| 7510 | fprintf(output, " name=\"%s\"", define->name); |
| 7511 | fprintf(output, ">\n"); |
| 7512 | xmlRelaxNGDumpDefines(output, define->content); |
| 7513 | fprintf(output, "</parentRef>\n"); |
| 7514 | break; |
| 7515 | case XML_RELAXNG_EXTERNALREF: |
| 7516 | fprintf(output, "<externalRef>"); |
| 7517 | xmlRelaxNGDumpDefines(output, define->content); |
| 7518 | fprintf(output, "</externalRef>\n"); |
| 7519 | break; |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 7520 | case XML_RELAXNG_DATATYPE: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7521 | case XML_RELAXNG_VALUE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7522 | TODO break; |
| 7523 | case XML_RELAXNG_START: |
| 7524 | case XML_RELAXNG_EXCEPT: |
| 7525 | case XML_RELAXNG_PARAM: |
| 7526 | TODO break; |
| 7527 | case XML_RELAXNG_NOOP: |
| 7528 | xmlRelaxNGDumpDefines(output, define->content); |
| 7529 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7530 | } |
| 7531 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7532 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7533 | /** |
| 7534 | * xmlRelaxNGDumpGrammar: |
| 7535 | * @output: the file output |
| 7536 | * @grammar: a grammar structure |
| 7537 | * @top: is this a top grammar |
| 7538 | * |
| 7539 | * Dump a RelaxNG structure back |
| 7540 | */ |
| 7541 | static void |
| 7542 | xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top) |
| 7543 | { |
| 7544 | if (grammar == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7545 | return; |
| 7546 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7547 | fprintf(output, "<grammar"); |
| 7548 | if (top) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7549 | fprintf(output, " xmlns=\"http://relaxng.org/ns/structure/1.0\""); |
| 7550 | switch (grammar->combine) { |
| 7551 | case XML_RELAXNG_COMBINE_UNDEFINED: |
| 7552 | break; |
| 7553 | case XML_RELAXNG_COMBINE_CHOICE: |
| 7554 | fprintf(output, " combine=\"choice\""); |
| 7555 | break; |
| 7556 | case XML_RELAXNG_COMBINE_INTERLEAVE: |
| 7557 | fprintf(output, " combine=\"interleave\""); |
| 7558 | break; |
| 7559 | default: |
| 7560 | fprintf(output, " <!-- invalid combine value -->"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7561 | } |
| 7562 | fprintf(output, ">\n"); |
| 7563 | if (grammar->start == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7564 | fprintf(output, " <!-- grammar had no start -->"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7565 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7566 | fprintf(output, "<start>\n"); |
| 7567 | xmlRelaxNGDumpDefine(output, grammar->start); |
| 7568 | fprintf(output, "</start>\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7569 | } |
| 7570 | /* TODO ? Dump the defines ? */ |
| 7571 | fprintf(output, "</grammar>\n"); |
| 7572 | } |
| 7573 | |
| 7574 | /** |
| 7575 | * xmlRelaxNGDump: |
| 7576 | * @output: the file output |
| 7577 | * @schema: a schema structure |
| 7578 | * |
| 7579 | * Dump a RelaxNG structure back |
| 7580 | */ |
| 7581 | void |
| 7582 | xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema) |
| 7583 | { |
| 7584 | if (schema == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7585 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
| 7586 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7587 | } |
| 7588 | fprintf(output, "RelaxNG: "); |
| 7589 | if (schema->doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7590 | fprintf(output, "no document\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7591 | } else if (schema->doc->URL != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7592 | fprintf(output, "%s\n", schema->doc->URL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7593 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7594 | fprintf(output, "\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7595 | } |
| 7596 | if (schema->topgrammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7597 | fprintf(output, "RelaxNG has no top grammar\n"); |
| 7598 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7599 | } |
| 7600 | xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1); |
| 7601 | } |
| 7602 | |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7603 | /** |
| 7604 | * xmlRelaxNGDumpTree: |
| 7605 | * @output: the file output |
| 7606 | * @schema: a schema structure |
| 7607 | * |
| 7608 | * Dump the transformed RelaxNG tree. |
| 7609 | */ |
| 7610 | void |
| 7611 | xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema) |
| 7612 | { |
| 7613 | if (schema == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7614 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
| 7615 | return; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7616 | } |
| 7617 | if (schema->doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7618 | fprintf(output, "no document\n"); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7619 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7620 | xmlDocDump(output, schema->doc); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7621 | } |
| 7622 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 7623 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7624 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7625 | /************************************************************************ |
| 7626 | * * |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7627 | * Validation of compiled content * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7628 | * * |
| 7629 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7630 | static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
| 7631 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7632 | |
| 7633 | /** |
| 7634 | * xmlRelaxNGValidateCompiledCallback: |
| 7635 | * @exec: the regular expression instance |
| 7636 | * @token: the token which matched |
| 7637 | * @transdata: callback data, the define for the subelement if available |
| 7638 | @ @inputdata: callback data, the Relax NG validation context |
| 7639 | * |
| 7640 | * Handle the callback and if needed validate the element children. |
| 7641 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7642 | static void |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7643 | xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7644 | const xmlChar * token, |
| 7645 | void *transdata, void *inputdata) |
| 7646 | { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7647 | xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata; |
| 7648 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata; |
| 7649 | int ret; |
| 7650 | |
| 7651 | #ifdef DEBUG_COMPILE |
| 7652 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7653 | "Compiled callback for: '%s'\n", token); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7654 | #endif |
| 7655 | if (ctxt == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7656 | fprintf(stderr, "callback on %s missing context\n", token); |
| 7657 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7658 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7659 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7660 | } |
| 7661 | if (define == NULL) { |
| 7662 | if (token[0] == '#') |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7663 | return; |
| 7664 | fprintf(stderr, "callback on %s missing define\n", token); |
| 7665 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7666 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7667 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7668 | } |
| 7669 | if ((ctxt == NULL) || (define == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7670 | fprintf(stderr, "callback on %s missing info\n", token); |
| 7671 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7672 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7673 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7674 | } else if (define->type != XML_RELAXNG_ELEMENT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7675 | fprintf(stderr, "callback on %s define is not element\n", token); |
| 7676 | if (ctxt->errNo == XML_RELAXNG_OK) |
| 7677 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7678 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7679 | } |
| 7680 | ret = xmlRelaxNGValidateDefinition(ctxt, define); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7681 | if (ret != 0) |
| 7682 | ctxt->perr = ret; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7683 | } |
| 7684 | |
| 7685 | /** |
| 7686 | * xmlRelaxNGValidateCompiledContent: |
| 7687 | * @ctxt: the RelaxNG validation context |
| 7688 | * @regexp: the regular expression as compiled |
| 7689 | * @content: list of children to test against the regexp |
| 7690 | * |
| 7691 | * Validate the content model of an element or start using the regexp |
| 7692 | * |
| 7693 | * Returns 0 in case of success, -1 in case of error. |
| 7694 | */ |
| 7695 | static int |
| 7696 | xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7697 | xmlRegexpPtr regexp, xmlNodePtr content) |
| 7698 | { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7699 | xmlRegExecCtxtPtr exec; |
| 7700 | xmlNodePtr cur; |
Daniel Veillard | 6216360 | 2003-04-17 09:36:38 +0000 | [diff] [blame] | 7701 | int ret = 0; |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7702 | int oldperr = ctxt->perr; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7703 | |
| 7704 | if ((ctxt == NULL) || (regexp == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7705 | return (-1); |
| 7706 | exec = xmlRegNewExecCtxt(regexp, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7707 | xmlRelaxNGValidateCompiledCallback, ctxt); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7708 | ctxt->perr = 0; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7709 | cur = content; |
| 7710 | while (cur != NULL) { |
| 7711 | ctxt->state->seq = cur; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7712 | switch (cur->type) { |
| 7713 | case XML_TEXT_NODE: |
| 7714 | case XML_CDATA_SECTION_NODE: |
| 7715 | if (xmlIsBlankNode(cur)) |
| 7716 | break; |
| 7717 | ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt); |
| 7718 | if (ret < 0) { |
| 7719 | VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, |
| 7720 | cur->parent->name); |
| 7721 | } |
| 7722 | break; |
| 7723 | case XML_ELEMENT_NODE: |
| 7724 | if (cur->ns != NULL) { |
| 7725 | ret = xmlRegExecPushString2(exec, cur->name, |
| 7726 | cur->ns->href, ctxt); |
| 7727 | } else { |
| 7728 | ret = xmlRegExecPushString(exec, cur->name, ctxt); |
| 7729 | } |
| 7730 | if (ret < 0) { |
| 7731 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name); |
| 7732 | } |
| 7733 | break; |
| 7734 | default: |
| 7735 | break; |
| 7736 | } |
| 7737 | if (ret < 0) |
| 7738 | break; |
| 7739 | /* |
| 7740 | * Switch to next element |
| 7741 | */ |
| 7742 | cur = cur->next; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7743 | } |
| 7744 | ret = xmlRegExecPushString(exec, NULL, NULL); |
| 7745 | if (ret == 1) { |
| 7746 | ret = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7747 | ctxt->state->seq = NULL; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7748 | } else if (ret == 0) { |
| 7749 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7750 | * TODO: get some of the names needed to exit the current state of exec |
| 7751 | */ |
| 7752 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST ""); |
| 7753 | ret = -1; |
| 7754 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 7755 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7756 | } else { |
| 7757 | ret = -1; |
| 7758 | } |
| 7759 | xmlRegFreeExecCtxt(exec); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7760 | /* |
| 7761 | * There might be content model errors outside of the pure |
| 7762 | * regexp validation, e.g. for attribute values. |
| 7763 | */ |
| 7764 | if ((ret == 0) && (ctxt->perr != 0)) { |
| 7765 | ret = ctxt->perr; |
| 7766 | } |
| 7767 | ctxt->perr = oldperr; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7768 | return (ret); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7769 | } |
| 7770 | |
| 7771 | /************************************************************************ |
| 7772 | * * |
| 7773 | * Progressive validation of when possible * |
| 7774 | * * |
| 7775 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7776 | static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
| 7777 | xmlRelaxNGDefinePtr defines); |
| 7778 | static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, |
| 7779 | int log); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 7780 | static void xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7781 | |
| 7782 | /** |
| 7783 | * xmlRelaxNGElemPush: |
| 7784 | * @ctxt: the validation context |
| 7785 | * @exec: the regexp runtime for the new content model |
| 7786 | * |
| 7787 | * Push a new regexp for the current node content model on the stack |
| 7788 | * |
| 7789 | * Returns 0 in case of success and -1 in case of error. |
| 7790 | */ |
| 7791 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7792 | xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec) |
| 7793 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7794 | if (ctxt->elemTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7795 | ctxt->elemMax = 10; |
| 7796 | ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax * |
| 7797 | sizeof |
| 7798 | (xmlRegExecCtxtPtr)); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7799 | if (ctxt->elemTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7800 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 7801 | return (-1); |
| 7802 | } |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7803 | } |
| 7804 | if (ctxt->elemNr >= ctxt->elemMax) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7805 | ctxt->elemMax *= 2; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7806 | ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7807 | ctxt->elemMax * |
| 7808 | sizeof |
| 7809 | (xmlRegExecCtxtPtr)); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7810 | if (ctxt->elemTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7811 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 7812 | return (-1); |
| 7813 | } |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7814 | } |
| 7815 | ctxt->elemTab[ctxt->elemNr++] = exec; |
| 7816 | ctxt->elem = exec; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7817 | return (0); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7818 | } |
| 7819 | |
| 7820 | /** |
| 7821 | * xmlRelaxNGElemPop: |
| 7822 | * @ctxt: the validation context |
| 7823 | * |
| 7824 | * Pop the regexp of the current node content model from the stack |
| 7825 | * |
| 7826 | * Returns the exec or NULL if empty |
| 7827 | */ |
| 7828 | static xmlRegExecCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7829 | xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt) |
| 7830 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7831 | xmlRegExecCtxtPtr ret; |
| 7832 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7833 | if (ctxt->elemNr <= 0) |
| 7834 | return (NULL); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7835 | ctxt->elemNr--; |
| 7836 | ret = ctxt->elemTab[ctxt->elemNr]; |
| 7837 | ctxt->elemTab[ctxt->elemNr] = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7838 | if (ctxt->elemNr > 0) |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7839 | ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1]; |
| 7840 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7841 | ctxt->elem = NULL; |
| 7842 | return (ret); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7843 | } |
| 7844 | |
| 7845 | /** |
| 7846 | * xmlRelaxNGValidateProgressiveCallback: |
| 7847 | * @exec: the regular expression instance |
| 7848 | * @token: the token which matched |
| 7849 | * @transdata: callback data, the define for the subelement if available |
| 7850 | @ @inputdata: callback data, the Relax NG validation context |
| 7851 | * |
| 7852 | * Handle the callback and if needed validate the element children. |
| 7853 | * some of the in/out informations are passed via the context in @inputdata. |
| 7854 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7855 | static void |
| 7856 | xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec |
| 7857 | ATTRIBUTE_UNUSED, |
| 7858 | const xmlChar * token, |
| 7859 | void *transdata, void *inputdata) |
| 7860 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7861 | xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata; |
| 7862 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7863 | xmlRelaxNGValidStatePtr state, oldstate; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7864 | xmlNodePtr node = ctxt->pnode; |
Daniel Veillard | c3ca5ba | 2003-05-09 22:26:28 +0000 | [diff] [blame] | 7865 | int ret = 0, oldflags; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7866 | |
| 7867 | #ifdef DEBUG_PROGRESSIVE |
| 7868 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7869 | "Progressive callback for: '%s'\n", token); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7870 | #endif |
| 7871 | if (ctxt == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7872 | fprintf(stderr, "callback on %s missing context\n", token); |
| 7873 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7874 | } |
| 7875 | ctxt->pstate = 1; |
| 7876 | if (define == NULL) { |
| 7877 | if (token[0] == '#') |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7878 | return; |
| 7879 | fprintf(stderr, "callback on %s missing define\n", token); |
| 7880 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7881 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7882 | ctxt->pstate = -1; |
| 7883 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7884 | } |
| 7885 | if ((ctxt == NULL) || (define == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7886 | fprintf(stderr, "callback on %s missing info\n", token); |
| 7887 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7888 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7889 | ctxt->pstate = -1; |
| 7890 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7891 | } else if (define->type != XML_RELAXNG_ELEMENT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7892 | fprintf(stderr, "callback on %s define is not element\n", token); |
| 7893 | if (ctxt->errNo == XML_RELAXNG_OK) |
| 7894 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7895 | ctxt->pstate = -1; |
| 7896 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7897 | } |
| 7898 | if (node->type != XML_ELEMENT_NODE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7899 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
| 7900 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 7901 | xmlRelaxNGDumpValidError(ctxt); |
| 7902 | ctxt->pstate = -1; |
| 7903 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7904 | } |
| 7905 | if (define->contModel == NULL) { |
| 7906 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7907 | * this node cannot be validated in a streamable fashion |
| 7908 | */ |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7909 | #ifdef DEBUG_PROGRESSIVE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7910 | xmlGenericError(xmlGenericErrorContext, |
| 7911 | "Element '%s' validation is not streamable\n", |
| 7912 | token); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7913 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7914 | ctxt->pstate = 0; |
| 7915 | ctxt->pdef = define; |
| 7916 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7917 | } |
| 7918 | exec = xmlRegNewExecCtxt(define->contModel, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7919 | xmlRelaxNGValidateProgressiveCallback, ctxt); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7920 | if (exec == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7921 | ctxt->pstate = -1; |
| 7922 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7923 | } |
| 7924 | xmlRelaxNGElemPush(ctxt, exec); |
| 7925 | |
| 7926 | /* |
| 7927 | * Validate the attributes part of the content. |
| 7928 | */ |
| 7929 | state = xmlRelaxNGNewValidState(ctxt, node); |
| 7930 | if (state == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7931 | ctxt->pstate = -1; |
| 7932 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7933 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7934 | oldstate = ctxt->state; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7935 | ctxt->state = state; |
| 7936 | if (define->attrs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7937 | ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
| 7938 | if (ret != 0) { |
| 7939 | ctxt->pstate = -1; |
| 7940 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
| 7941 | } |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7942 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7943 | if (ctxt->state != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7944 | ctxt->state->seq = NULL; |
| 7945 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
| 7946 | if (ret != 0) { |
| 7947 | ctxt->pstate = -1; |
| 7948 | } |
| 7949 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7950 | } else if (ctxt->states != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7951 | int tmp = -1, i; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7952 | |
| 7953 | oldflags = ctxt->flags; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7954 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7955 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 7956 | state = ctxt->states->tabState[i]; |
| 7957 | ctxt->state = state; |
| 7958 | ctxt->state->seq = NULL; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7959 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7960 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
| 7961 | tmp = 0; |
| 7962 | break; |
| 7963 | } |
| 7964 | } |
| 7965 | if (tmp != 0) { |
| 7966 | /* |
| 7967 | * validation error, log the message for the "best" one |
| 7968 | */ |
| 7969 | ctxt->flags |= FLAGS_IGNORABLE; |
| 7970 | xmlRelaxNGLogBestError(ctxt); |
| 7971 | } |
| 7972 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 7973 | xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[i]); |
| 7974 | } |
| 7975 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 7976 | ctxt->states = NULL; |
| 7977 | if ((ret == 0) && (tmp == -1)) |
| 7978 | ctxt->pstate = -1; |
| 7979 | ctxt->flags = oldflags; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7980 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7981 | if (ctxt->pstate == -1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7982 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 7983 | xmlRelaxNGDumpValidError(ctxt); |
| 7984 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7985 | } |
| 7986 | ctxt->state = oldstate; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7987 | } |
| 7988 | |
| 7989 | /** |
| 7990 | * xmlRelaxNGValidatePushElement: |
| 7991 | * @ctxt: the validation context |
| 7992 | * @doc: a document instance |
| 7993 | * @elem: an element instance |
| 7994 | * |
| 7995 | * Push a new element start on the RelaxNG validation stack. |
| 7996 | * |
| 7997 | * returns 1 if no validation problem was found or 0 if validating the |
| 7998 | * element requires a full node, and -1 in case of error. |
| 7999 | */ |
| 8000 | int |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8001 | xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8002 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8003 | xmlNodePtr elem) |
| 8004 | { |
| 8005 | int ret = 1; |
| 8006 | |
| 8007 | if ((ctxt == NULL) || (elem == NULL)) |
| 8008 | return (-1); |
| 8009 | |
| 8010 | #ifdef DEBUG_PROGRESSIVE |
| 8011 | xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name); |
| 8012 | #endif |
| 8013 | if (ctxt->elem == 0) { |
| 8014 | xmlRelaxNGPtr schema; |
| 8015 | xmlRelaxNGGrammarPtr grammar; |
| 8016 | xmlRegExecCtxtPtr exec; |
| 8017 | xmlRelaxNGDefinePtr define; |
| 8018 | |
| 8019 | schema = ctxt->schema; |
| 8020 | if (schema == NULL) { |
| 8021 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 8022 | return (-1); |
| 8023 | } |
| 8024 | grammar = schema->topgrammar; |
| 8025 | if ((grammar == NULL) || (grammar->start == NULL)) { |
| 8026 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 8027 | return (-1); |
| 8028 | } |
| 8029 | define = grammar->start; |
| 8030 | if (define->contModel == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8031 | ctxt->pdef = define; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8032 | return (0); |
| 8033 | } |
| 8034 | exec = xmlRegNewExecCtxt(define->contModel, |
| 8035 | xmlRelaxNGValidateProgressiveCallback, |
| 8036 | ctxt); |
| 8037 | if (exec == NULL) { |
| 8038 | return (-1); |
| 8039 | } |
| 8040 | xmlRelaxNGElemPush(ctxt, exec); |
| 8041 | } |
| 8042 | ctxt->pnode = elem; |
| 8043 | ctxt->pstate = 0; |
| 8044 | if (elem->ns != NULL) { |
| 8045 | ret = |
| 8046 | xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href, |
| 8047 | ctxt); |
| 8048 | } else { |
| 8049 | ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt); |
| 8050 | } |
| 8051 | if (ret < 0) { |
| 8052 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name); |
| 8053 | } else { |
| 8054 | if (ctxt->pstate == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8055 | ret = 0; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8056 | else if (ctxt->pstate < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8057 | ret = -1; |
| 8058 | else |
| 8059 | ret = 1; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8060 | } |
| 8061 | #ifdef DEBUG_PROGRESSIVE |
| 8062 | if (ret < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8063 | xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n", |
| 8064 | elem->name); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8065 | #endif |
| 8066 | return (ret); |
| 8067 | } |
| 8068 | |
| 8069 | /** |
| 8070 | * xmlRelaxNGValidatePushCData: |
| 8071 | * @ctxt: the RelaxNG validation context |
| 8072 | * @data: some character data read |
| 8073 | * @len: the lenght of the data |
| 8074 | * |
| 8075 | * check the CData parsed for validation in the current stack |
| 8076 | * |
| 8077 | * returns 1 if no validation problem was found or -1 otherwise |
| 8078 | */ |
| 8079 | int |
| 8080 | xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8081 | const xmlChar * data, int len ATTRIBUTE_UNUSED) |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8082 | { |
| 8083 | int ret = 1; |
| 8084 | |
| 8085 | if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL)) |
| 8086 | return (-1); |
| 8087 | |
| 8088 | #ifdef DEBUG_PROGRESSIVE |
| 8089 | xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len); |
| 8090 | #endif |
| 8091 | |
| 8092 | while (*data != 0) { |
| 8093 | if (!IS_BLANK(*data)) |
| 8094 | break; |
| 8095 | data++; |
| 8096 | } |
| 8097 | if (*data == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8098 | return (1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8099 | |
| 8100 | ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt); |
| 8101 | if (ret < 0) { |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8102 | VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO "); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8103 | #ifdef DEBUG_PROGRESSIVE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8104 | xmlGenericError(xmlGenericErrorContext, "CDATA failed\n"); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8105 | #endif |
| 8106 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8107 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8108 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8109 | return (1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8110 | } |
| 8111 | |
| 8112 | /** |
| 8113 | * xmlRelaxNGValidatePopElement: |
| 8114 | * @ctxt: the RelaxNG validation context |
| 8115 | * @doc: a document instance |
| 8116 | * @elem: an element instance |
| 8117 | * |
| 8118 | * Pop the element end from the RelaxNG validation stack. |
| 8119 | * |
| 8120 | * returns 1 if no validation problem was found or 0 otherwise |
| 8121 | */ |
| 8122 | int |
| 8123 | xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8124 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8125 | xmlNodePtr elem) |
| 8126 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8127 | int ret; |
| 8128 | xmlRegExecCtxtPtr exec; |
| 8129 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8130 | if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL)) |
| 8131 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8132 | #ifdef DEBUG_PROGRESSIVE |
| 8133 | xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name); |
| 8134 | #endif |
| 8135 | /* |
| 8136 | * verify that we reached a terminal state of the content model. |
| 8137 | */ |
| 8138 | exec = xmlRelaxNGElemPop(ctxt); |
| 8139 | ret = xmlRegExecPushString(exec, NULL, NULL); |
| 8140 | if (ret == 0) { |
| 8141 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8142 | * TODO: get some of the names needed to exit the current state of exec |
| 8143 | */ |
| 8144 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST ""); |
| 8145 | ret = -1; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8146 | } else if (ret < 0) { |
| 8147 | ret = -1; |
| 8148 | } else { |
| 8149 | ret = 1; |
| 8150 | } |
| 8151 | xmlRegFreeExecCtxt(exec); |
| 8152 | #ifdef DEBUG_PROGRESSIVE |
| 8153 | if (ret < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8154 | xmlGenericError(xmlGenericErrorContext, "PopElem %s failed\n", |
| 8155 | elem->name); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8156 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8157 | return (ret); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8158 | } |
| 8159 | |
| 8160 | /** |
| 8161 | * xmlRelaxNGValidateFullElement: |
| 8162 | * @ctxt: the validation context |
| 8163 | * @doc: a document instance |
| 8164 | * @elem: an element instance |
| 8165 | * |
| 8166 | * Validate a full subtree when xmlRelaxNGValidatePushElement() returned |
| 8167 | * 0 and the content of the node has been expanded. |
| 8168 | * |
| 8169 | * returns 1 if no validation problem was found or -1 in case of error. |
| 8170 | */ |
| 8171 | int |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8172 | xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8173 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8174 | xmlNodePtr elem) |
| 8175 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8176 | int ret; |
| 8177 | xmlRelaxNGValidStatePtr state; |
| 8178 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8179 | if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL)) |
| 8180 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8181 | #ifdef DEBUG_PROGRESSIVE |
| 8182 | xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name); |
| 8183 | #endif |
| 8184 | state = xmlRelaxNGNewValidState(ctxt, elem->parent); |
| 8185 | if (state == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8186 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8187 | } |
| 8188 | state->seq = elem; |
| 8189 | ctxt->state = state; |
| 8190 | ctxt->errNo = XML_RELAXNG_OK; |
| 8191 | ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8192 | if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK)) |
| 8193 | ret = -1; |
| 8194 | else |
| 8195 | ret = 1; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8196 | xmlRelaxNGFreeValidState(ctxt, state); |
| 8197 | ctxt->state = NULL; |
| 8198 | #ifdef DEBUG_PROGRESSIVE |
| 8199 | if (ret < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8200 | xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n", |
| 8201 | elem->name); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8202 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8203 | return (ret); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8204 | } |
| 8205 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 8206 | /************************************************************************ |
| 8207 | * * |
| 8208 | * Generic interpreted validation implementation * |
| 8209 | * * |
| 8210 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8211 | static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
| 8212 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8213 | |
| 8214 | /** |
| 8215 | * xmlRelaxNGSkipIgnored: |
| 8216 | * @ctxt: a schema validation context |
| 8217 | * @node: the top node. |
| 8218 | * |
| 8219 | * Skip ignorable nodes in that context |
| 8220 | * |
| 8221 | * Returns the new sibling or NULL in case of error. |
| 8222 | */ |
| 8223 | static xmlNodePtr |
| 8224 | xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8225 | xmlNodePtr node) |
| 8226 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8227 | /* |
| 8228 | * TODO complete and handle entities |
| 8229 | */ |
| 8230 | while ((node != NULL) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8231 | ((node->type == XML_COMMENT_NODE) || |
| 8232 | (node->type == XML_PI_NODE) || |
| 8233 | (((node->type == XML_TEXT_NODE) || |
| 8234 | (node->type == XML_CDATA_SECTION_NODE)) && |
| 8235 | ((ctxt->flags & FLAGS_MIXED_CONTENT) || |
| 8236 | (IS_BLANK_NODE(node)))))) { |
| 8237 | node = node->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8238 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8239 | return (node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8240 | } |
| 8241 | |
| 8242 | /** |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8243 | * xmlRelaxNGNormalize: |
| 8244 | * @ctxt: a schema validation context |
| 8245 | * @str: the string to normalize |
| 8246 | * |
| 8247 | * Implements the normalizeWhiteSpace( s ) function from |
| 8248 | * section 6.2.9 of the spec |
| 8249 | * |
| 8250 | * Returns the new string or NULL in case of error. |
| 8251 | */ |
| 8252 | static xmlChar * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8253 | xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar * str) |
| 8254 | { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8255 | xmlChar *ret, *p; |
| 8256 | const xmlChar *tmp; |
| 8257 | int len; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8258 | |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8259 | if (str == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8260 | return (NULL); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8261 | tmp = str; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8262 | while (*tmp != 0) |
| 8263 | tmp++; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8264 | len = tmp - str; |
| 8265 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 8266 | ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar)); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8267 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8268 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 8269 | return (NULL); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8270 | } |
| 8271 | p = ret; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8272 | while (IS_BLANK(*str)) |
| 8273 | str++; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8274 | while (*str != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8275 | if (IS_BLANK(*str)) { |
| 8276 | while (IS_BLANK(*str)) |
| 8277 | str++; |
| 8278 | if (*str == 0) |
| 8279 | break; |
| 8280 | *p++ = ' '; |
| 8281 | } else |
| 8282 | *p++ = *str++; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8283 | } |
| 8284 | *p = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8285 | return (ret); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8286 | } |
| 8287 | |
| 8288 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8289 | * xmlRelaxNGValidateDatatype: |
| 8290 | * @ctxt: a Relax-NG validation context |
| 8291 | * @value: the string value |
| 8292 | * @type: the datatype definition |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8293 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8294 | * |
| 8295 | * Validate the given value against the dataype |
| 8296 | * |
| 8297 | * Returns 0 if the validation succeeded or an error code. |
| 8298 | */ |
| 8299 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8300 | xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt, |
| 8301 | const xmlChar * value, |
| 8302 | xmlRelaxNGDefinePtr define, xmlNodePtr node) |
| 8303 | { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8304 | int ret, tmp; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8305 | xmlRelaxNGTypeLibraryPtr lib; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8306 | void *result = NULL; |
| 8307 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8308 | |
| 8309 | if ((define == NULL) || (define->data == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8310 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8311 | } |
| 8312 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8313 | if (lib->check != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8314 | if ((define->attrs != NULL) && |
| 8315 | (define->attrs->type == XML_RELAXNG_PARAM)) { |
| 8316 | ret = |
| 8317 | lib->check(lib->data, define->name, value, &result, node); |
| 8318 | } else { |
| 8319 | ret = lib->check(lib->data, define->name, value, NULL, node); |
| 8320 | } |
| 8321 | } else |
| 8322 | ret = -1; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8323 | if (ret < 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8324 | VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name); |
| 8325 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
| 8326 | lib->freef(lib->data, result); |
| 8327 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8328 | } else if (ret == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8329 | ret = 0; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8330 | } else if (ret == 2) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8331 | VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8332 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8333 | VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value); |
| 8334 | ret = -1; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8335 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8336 | cur = define->attrs; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8337 | while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8338 | if (lib->facet != NULL) { |
| 8339 | tmp = lib->facet(lib->data, define->name, cur->name, |
| 8340 | cur->value, value, result); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8341 | if (tmp != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8342 | ret = -1; |
| 8343 | } |
| 8344 | cur = cur->next; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8345 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8346 | if ((ret == 0) && (define->content != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8347 | const xmlChar *oldvalue, *oldendvalue; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8348 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8349 | oldvalue = ctxt->state->value; |
| 8350 | oldendvalue = ctxt->state->endvalue; |
| 8351 | ctxt->state->value = (xmlChar *) value; |
| 8352 | ctxt->state->endvalue = NULL; |
| 8353 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 8354 | ctxt->state->value = (xmlChar *) oldvalue; |
| 8355 | ctxt->state->endvalue = (xmlChar *) oldendvalue; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8356 | } |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8357 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8358 | lib->freef(lib->data, result); |
| 8359 | return (ret); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8360 | } |
| 8361 | |
| 8362 | /** |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8363 | * xmlRelaxNGNextValue: |
| 8364 | * @ctxt: a Relax-NG validation context |
| 8365 | * |
| 8366 | * Skip to the next value when validating within a list |
| 8367 | * |
| 8368 | * Returns 0 if the operation succeeded or an error code. |
| 8369 | */ |
| 8370 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8371 | xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt) |
| 8372 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8373 | xmlChar *cur; |
| 8374 | |
| 8375 | cur = ctxt->state->value; |
| 8376 | if ((cur == NULL) || (ctxt->state->endvalue == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8377 | ctxt->state->value = NULL; |
| 8378 | ctxt->state->endvalue = NULL; |
| 8379 | return (0); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8380 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8381 | while (*cur != 0) |
| 8382 | cur++; |
| 8383 | while ((cur != ctxt->state->endvalue) && (*cur == 0)) |
| 8384 | cur++; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8385 | if (cur == ctxt->state->endvalue) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8386 | ctxt->state->value = NULL; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8387 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8388 | ctxt->state->value = cur; |
| 8389 | return (0); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8390 | } |
| 8391 | |
| 8392 | /** |
| 8393 | * xmlRelaxNGValidateValueList: |
| 8394 | * @ctxt: a Relax-NG validation context |
| 8395 | * @defines: the list of definitions to verify |
| 8396 | * |
| 8397 | * Validate the given set of definitions for the current value |
| 8398 | * |
| 8399 | * Returns 0 if the validation succeeded or an error code. |
| 8400 | */ |
| 8401 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8402 | xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt, |
| 8403 | xmlRelaxNGDefinePtr defines) |
| 8404 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8405 | int ret = 0; |
| 8406 | |
| 8407 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8408 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
| 8409 | if (ret != 0) |
| 8410 | break; |
| 8411 | defines = defines->next; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8412 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8413 | return (ret); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8414 | } |
| 8415 | |
| 8416 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8417 | * xmlRelaxNGValidateValue: |
| 8418 | * @ctxt: a Relax-NG validation context |
| 8419 | * @define: the definition to verify |
| 8420 | * |
| 8421 | * Validate the given definition for the current value |
| 8422 | * |
| 8423 | * Returns 0 if the validation succeeded or an error code. |
| 8424 | */ |
| 8425 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8426 | xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
| 8427 | xmlRelaxNGDefinePtr define) |
| 8428 | { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8429 | int ret = 0, oldflags; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8430 | xmlChar *value; |
| 8431 | |
| 8432 | value = ctxt->state->value; |
| 8433 | switch (define->type) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8434 | case XML_RELAXNG_EMPTY:{ |
| 8435 | if ((value != NULL) && (value[0] != 0)) { |
| 8436 | int idx = 0; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 8437 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8438 | while (IS_BLANK(value[idx])) |
| 8439 | idx++; |
| 8440 | if (value[idx] != 0) |
| 8441 | ret = -1; |
| 8442 | } |
| 8443 | break; |
| 8444 | } |
| 8445 | case XML_RELAXNG_TEXT: |
| 8446 | break; |
| 8447 | case XML_RELAXNG_VALUE:{ |
| 8448 | if (!xmlStrEqual(value, define->value)) { |
| 8449 | if (define->name != NULL) { |
| 8450 | xmlRelaxNGTypeLibraryPtr lib; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8451 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8452 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
| 8453 | if ((lib != NULL) && (lib->comp != NULL)) { |
| 8454 | ret = lib->comp(lib->data, define->name, |
| 8455 | define->value, define->node, |
| 8456 | (void *) define->attrs, |
| 8457 | value, ctxt->state->node); |
| 8458 | } else |
| 8459 | ret = -1; |
| 8460 | if (ret < 0) { |
| 8461 | VALID_ERR2(XML_RELAXNG_ERR_TYPECMP, |
| 8462 | define->name); |
| 8463 | return (-1); |
| 8464 | } else if (ret == 1) { |
| 8465 | ret = 0; |
| 8466 | } else { |
| 8467 | ret = -1; |
| 8468 | } |
| 8469 | } else { |
| 8470 | xmlChar *nval, *nvalue; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8471 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8472 | /* |
| 8473 | * TODO: trivial optimizations are possible by |
| 8474 | * computing at compile-time |
| 8475 | */ |
| 8476 | nval = xmlRelaxNGNormalize(ctxt, define->value); |
| 8477 | nvalue = xmlRelaxNGNormalize(ctxt, value); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8478 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8479 | if ((nval == NULL) || (nvalue == NULL) || |
| 8480 | (!xmlStrEqual(nval, nvalue))) |
| 8481 | ret = -1; |
| 8482 | if (nval != NULL) |
| 8483 | xmlFree(nval); |
| 8484 | if (nvalue != NULL) |
| 8485 | xmlFree(nvalue); |
| 8486 | } |
| 8487 | } |
| 8488 | if (ret == 0) |
| 8489 | xmlRelaxNGNextValue(ctxt); |
| 8490 | break; |
| 8491 | } |
| 8492 | case XML_RELAXNG_DATATYPE:{ |
| 8493 | ret = xmlRelaxNGValidateDatatype(ctxt, value, define, |
| 8494 | ctxt->state->seq); |
| 8495 | if (ret == 0) |
| 8496 | xmlRelaxNGNextValue(ctxt); |
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 | break; |
| 8499 | } |
| 8500 | case XML_RELAXNG_CHOICE:{ |
| 8501 | xmlRelaxNGDefinePtr list = define->content; |
| 8502 | xmlChar *oldvalue; |
| 8503 | |
| 8504 | oldflags = ctxt->flags; |
| 8505 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8506 | |
| 8507 | oldvalue = ctxt->state->value; |
| 8508 | while (list != NULL) { |
| 8509 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8510 | if (ret == 0) { |
| 8511 | break; |
| 8512 | } |
| 8513 | ctxt->state->value = oldvalue; |
| 8514 | list = list->next; |
| 8515 | } |
| 8516 | ctxt->flags = oldflags; |
| 8517 | if (ret != 0) { |
| 8518 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8519 | xmlRelaxNGDumpValidError(ctxt); |
| 8520 | } else { |
| 8521 | if (ctxt->errNr > 0) |
| 8522 | xmlRelaxNGPopErrors(ctxt, 0); |
| 8523 | } |
| 8524 | if (ret == 0) |
| 8525 | xmlRelaxNGNextValue(ctxt); |
| 8526 | break; |
| 8527 | } |
| 8528 | case XML_RELAXNG_LIST:{ |
| 8529 | xmlRelaxNGDefinePtr list = define->content; |
| 8530 | xmlChar *oldvalue, *oldend, *val, *cur; |
| 8531 | |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8532 | #ifdef DEBUG_LIST |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8533 | int nb_values = 0; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8534 | #endif |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8535 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8536 | oldvalue = ctxt->state->value; |
| 8537 | oldend = ctxt->state->endvalue; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8538 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8539 | val = xmlStrdup(oldvalue); |
| 8540 | if (val == NULL) { |
| 8541 | val = xmlStrdup(BAD_CAST ""); |
| 8542 | } |
| 8543 | if (val == NULL) { |
| 8544 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 8545 | return (-1); |
| 8546 | } |
| 8547 | cur = val; |
| 8548 | while (*cur != 0) { |
| 8549 | if (IS_BLANK(*cur)) { |
| 8550 | *cur = 0; |
| 8551 | cur++; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8552 | #ifdef DEBUG_LIST |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8553 | nb_values++; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8554 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8555 | while (IS_BLANK(*cur)) |
| 8556 | *cur++ = 0; |
| 8557 | } else |
| 8558 | cur++; |
| 8559 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8560 | #ifdef DEBUG_LIST |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8561 | xmlGenericError(xmlGenericErrorContext, |
| 8562 | "list value: '%s' found %d items\n", |
| 8563 | oldvalue, nb_values); |
| 8564 | nb_values = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8565 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8566 | ctxt->state->endvalue = cur; |
| 8567 | cur = val; |
| 8568 | while ((*cur == 0) && (cur != ctxt->state->endvalue)) |
| 8569 | cur++; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8570 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8571 | ctxt->state->value = cur; |
| 8572 | |
| 8573 | while (list != NULL) { |
| 8574 | if (ctxt->state->value == ctxt->state->endvalue) |
| 8575 | ctxt->state->value = NULL; |
| 8576 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8577 | if (ret != 0) { |
| 8578 | #ifdef DEBUG_LIST |
| 8579 | xmlGenericError(xmlGenericErrorContext, |
| 8580 | "Failed to validate value: '%s' with %d rule\n", |
| 8581 | ctxt->state->value, nb_values); |
| 8582 | #endif |
| 8583 | break; |
| 8584 | } |
| 8585 | #ifdef DEBUG_LIST |
| 8586 | nb_values++; |
| 8587 | #endif |
| 8588 | list = list->next; |
| 8589 | } |
| 8590 | |
| 8591 | if ((ret == 0) && (ctxt->state->value != NULL) && |
| 8592 | (ctxt->state->value != ctxt->state->endvalue)) { |
| 8593 | VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA, |
| 8594 | ctxt->state->value); |
| 8595 | ret = -1; |
| 8596 | } |
| 8597 | xmlFree(val); |
| 8598 | ctxt->state->value = oldvalue; |
| 8599 | ctxt->state->endvalue = oldend; |
| 8600 | break; |
| 8601 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8602 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8603 | ret = xmlRelaxNGValidateValueList(ctxt, define->content); |
| 8604 | if (ret != 0) { |
| 8605 | break; |
| 8606 | } |
| 8607 | /* no break on purpose */ |
| 8608 | case XML_RELAXNG_ZEROORMORE:{ |
| 8609 | xmlChar *cur, *temp; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8610 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8611 | oldflags = ctxt->flags; |
| 8612 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8613 | cur = ctxt->state->value; |
| 8614 | temp = NULL; |
| 8615 | while ((cur != NULL) && (cur != ctxt->state->endvalue) && |
| 8616 | (temp != cur)) { |
| 8617 | temp = cur; |
| 8618 | ret = |
| 8619 | xmlRelaxNGValidateValueList(ctxt, define->content); |
| 8620 | if (ret != 0) { |
| 8621 | ctxt->state->value = temp; |
| 8622 | ret = 0; |
| 8623 | break; |
| 8624 | } |
| 8625 | cur = ctxt->state->value; |
| 8626 | } |
| 8627 | ctxt->flags = oldflags; |
| 8628 | if (ret != 0) { |
| 8629 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8630 | xmlRelaxNGDumpValidError(ctxt); |
| 8631 | } else { |
| 8632 | if (ctxt->errNr > 0) |
| 8633 | xmlRelaxNGPopErrors(ctxt, 0); |
| 8634 | } |
| 8635 | break; |
| 8636 | } |
| 8637 | case XML_RELAXNG_EXCEPT:{ |
| 8638 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8639 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8640 | list = define->content; |
| 8641 | while (list != NULL) { |
| 8642 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8643 | if (ret == 0) { |
| 8644 | ret = -1; |
| 8645 | break; |
| 8646 | } else |
| 8647 | ret = 0; |
| 8648 | list = list->next; |
| 8649 | } |
| 8650 | break; |
| 8651 | } |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 8652 | case XML_RELAXNG_DEF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8653 | case XML_RELAXNG_GROUP:{ |
| 8654 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8655 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8656 | list = define->content; |
| 8657 | while (list != NULL) { |
| 8658 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8659 | if (ret != 0) { |
| 8660 | ret = -1; |
| 8661 | break; |
| 8662 | } else |
| 8663 | ret = 0; |
| 8664 | list = list->next; |
| 8665 | } |
| 8666 | break; |
| 8667 | } |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 8668 | case XML_RELAXNG_REF: |
| 8669 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8670 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 8671 | break; |
| 8672 | default: |
| 8673 | TODO ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8674 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8675 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8676 | } |
| 8677 | |
| 8678 | /** |
| 8679 | * xmlRelaxNGValidateValueContent: |
| 8680 | * @ctxt: a Relax-NG validation context |
| 8681 | * @defines: the list of definitions to verify |
| 8682 | * |
| 8683 | * Validate the given definitions for the current value |
| 8684 | * |
| 8685 | * Returns 0 if the validation succeeded or an error code. |
| 8686 | */ |
| 8687 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8688 | xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt, |
| 8689 | xmlRelaxNGDefinePtr defines) |
| 8690 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8691 | int ret = 0; |
| 8692 | |
| 8693 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8694 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
| 8695 | if (ret != 0) |
| 8696 | break; |
| 8697 | defines = defines->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8698 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8699 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8700 | } |
| 8701 | |
| 8702 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8703 | * xmlRelaxNGAttributeMatch: |
| 8704 | * @ctxt: a Relax-NG validation context |
| 8705 | * @define: the definition to check |
| 8706 | * @prop: the attribute |
| 8707 | * |
| 8708 | * Check if the attribute matches the definition nameClass |
| 8709 | * |
| 8710 | * Returns 1 if the attribute matches, 0 if no, or -1 in case of error |
| 8711 | */ |
| 8712 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8713 | xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 8714 | xmlRelaxNGDefinePtr define, xmlAttrPtr prop) |
| 8715 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8716 | int ret; |
| 8717 | |
| 8718 | if (define->name != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8719 | if (!xmlStrEqual(define->name, prop->name)) |
| 8720 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8721 | } |
| 8722 | if (define->ns != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8723 | if (define->ns[0] == 0) { |
| 8724 | if (prop->ns != NULL) |
| 8725 | return (0); |
| 8726 | } else { |
| 8727 | if ((prop->ns == NULL) || |
| 8728 | (!xmlStrEqual(define->ns, prop->ns->href))) |
| 8729 | return (0); |
| 8730 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8731 | } |
| 8732 | if (define->nameClass == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8733 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8734 | define = define->nameClass; |
| 8735 | if (define->type == XML_RELAXNG_EXCEPT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8736 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8737 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8738 | list = define->content; |
| 8739 | while (list != NULL) { |
| 8740 | ret = xmlRelaxNGAttributeMatch(ctxt, list, prop); |
| 8741 | if (ret == 1) |
| 8742 | return (0); |
| 8743 | if (ret < 0) |
| 8744 | return (ret); |
| 8745 | list = list->next; |
| 8746 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8747 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8748 | TODO} |
| 8749 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8750 | } |
| 8751 | |
| 8752 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8753 | * xmlRelaxNGValidateAttribute: |
| 8754 | * @ctxt: a Relax-NG validation context |
| 8755 | * @define: the definition to verify |
| 8756 | * |
| 8757 | * Validate the given attribute definition for that node |
| 8758 | * |
| 8759 | * Returns 0 if the validation succeeded or an error code. |
| 8760 | */ |
| 8761 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8762 | xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt, |
| 8763 | xmlRelaxNGDefinePtr define) |
| 8764 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8765 | int ret = 0, i; |
| 8766 | xmlChar *value, *oldvalue; |
| 8767 | xmlAttrPtr prop = NULL, tmp; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8768 | xmlNodePtr oldseq; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8769 | |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 8770 | if (ctxt->state->nbAttrLeft <= 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8771 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8772 | if (define->name != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8773 | for (i = 0; i < ctxt->state->nbAttrs; i++) { |
| 8774 | tmp = ctxt->state->attrs[i]; |
| 8775 | if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) { |
| 8776 | if ((((define->ns == NULL) || (define->ns[0] == 0)) && |
| 8777 | (tmp->ns == NULL)) || |
| 8778 | ((tmp->ns != NULL) && |
| 8779 | (xmlStrEqual(define->ns, tmp->ns->href)))) { |
| 8780 | prop = tmp; |
| 8781 | break; |
| 8782 | } |
| 8783 | } |
| 8784 | } |
| 8785 | if (prop != NULL) { |
| 8786 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 8787 | oldvalue = ctxt->state->value; |
| 8788 | oldseq = ctxt->state->seq; |
| 8789 | ctxt->state->seq = (xmlNodePtr) prop; |
| 8790 | ctxt->state->value = value; |
| 8791 | ctxt->state->endvalue = NULL; |
| 8792 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
| 8793 | if (ctxt->state->value != NULL) |
| 8794 | value = ctxt->state->value; |
| 8795 | if (value != NULL) |
| 8796 | xmlFree(value); |
| 8797 | ctxt->state->value = oldvalue; |
| 8798 | ctxt->state->seq = oldseq; |
| 8799 | if (ret == 0) { |
| 8800 | /* |
| 8801 | * flag the attribute as processed |
| 8802 | */ |
| 8803 | ctxt->state->attrs[i] = NULL; |
| 8804 | ctxt->state->nbAttrLeft--; |
| 8805 | } |
| 8806 | } else { |
| 8807 | ret = -1; |
| 8808 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8809 | #ifdef DEBUG |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8810 | xmlGenericError(xmlGenericErrorContext, |
| 8811 | "xmlRelaxNGValidateAttribute(%s): %d\n", |
| 8812 | define->name, ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8813 | #endif |
| 8814 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8815 | for (i = 0; i < ctxt->state->nbAttrs; i++) { |
| 8816 | tmp = ctxt->state->attrs[i]; |
| 8817 | if ((tmp != NULL) && |
| 8818 | (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) { |
| 8819 | prop = tmp; |
| 8820 | break; |
| 8821 | } |
| 8822 | } |
| 8823 | if (prop != NULL) { |
| 8824 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 8825 | oldvalue = ctxt->state->value; |
| 8826 | oldseq = ctxt->state->seq; |
| 8827 | ctxt->state->seq = (xmlNodePtr) prop; |
| 8828 | ctxt->state->value = value; |
| 8829 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
| 8830 | if (ctxt->state->value != NULL) |
| 8831 | value = ctxt->state->value; |
| 8832 | if (value != NULL) |
| 8833 | xmlFree(value); |
| 8834 | ctxt->state->value = oldvalue; |
| 8835 | ctxt->state->seq = oldseq; |
| 8836 | if (ret == 0) { |
| 8837 | /* |
| 8838 | * flag the attribute as processed |
| 8839 | */ |
| 8840 | ctxt->state->attrs[i] = NULL; |
| 8841 | ctxt->state->nbAttrLeft--; |
| 8842 | } |
| 8843 | } else { |
| 8844 | ret = -1; |
| 8845 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8846 | #ifdef DEBUG |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8847 | if (define->ns != NULL) { |
| 8848 | xmlGenericError(xmlGenericErrorContext, |
| 8849 | "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n", |
| 8850 | define->ns, ret); |
| 8851 | } else { |
| 8852 | xmlGenericError(xmlGenericErrorContext, |
| 8853 | "xmlRelaxNGValidateAttribute(anyName): %d\n", |
| 8854 | ret); |
| 8855 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8856 | #endif |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8857 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8858 | |
| 8859 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8860 | } |
| 8861 | |
| 8862 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8863 | * xmlRelaxNGValidateAttributeList: |
| 8864 | * @ctxt: a Relax-NG validation context |
| 8865 | * @define: the list of definition to verify |
| 8866 | * |
| 8867 | * Validate the given node against the list of attribute definitions |
| 8868 | * |
| 8869 | * Returns 0 if the validation succeeded or an error code. |
| 8870 | */ |
| 8871 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8872 | xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
| 8873 | xmlRelaxNGDefinePtr defines) |
| 8874 | { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8875 | int ret = 0, res; |
| 8876 | int needmore = 0; |
| 8877 | xmlRelaxNGDefinePtr cur; |
| 8878 | |
| 8879 | cur = defines; |
| 8880 | while (cur != NULL) { |
| 8881 | if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8882 | if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0) |
| 8883 | ret = -1; |
| 8884 | } else |
| 8885 | needmore = 1; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8886 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8887 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8888 | if (!needmore) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8889 | return (ret); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8890 | cur = defines; |
| 8891 | while (cur != NULL) { |
| 8892 | if (cur->type != XML_RELAXNG_ATTRIBUTE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8893 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
| 8894 | res = xmlRelaxNGValidateDefinition(ctxt, cur); |
| 8895 | if (res < 0) |
| 8896 | ret = -1; |
| 8897 | } else { |
| 8898 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 8899 | return (-1); |
| 8900 | } |
| 8901 | if (res == -1) /* continues on -2 */ |
| 8902 | break; |
| 8903 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8904 | cur = cur->next; |
| 8905 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8906 | |
| 8907 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8908 | } |
| 8909 | |
| 8910 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8911 | * xmlRelaxNGNodeMatchesList: |
| 8912 | * @node: the node |
| 8913 | * @list: a NULL terminated array of definitions |
| 8914 | * |
| 8915 | * Check if a node can be matched by one of the definitions |
| 8916 | * |
| 8917 | * Returns 1 if matches 0 otherwise |
| 8918 | */ |
| 8919 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8920 | xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr * list) |
| 8921 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8922 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8923 | int i = 0, tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8924 | |
| 8925 | if ((node == NULL) || (list == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8926 | return (0); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8927 | |
| 8928 | cur = list[i++]; |
| 8929 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8930 | if ((node->type == XML_ELEMENT_NODE) && |
| 8931 | (cur->type == XML_RELAXNG_ELEMENT)) { |
| 8932 | tmp = xmlRelaxNGElementMatch(NULL, cur, node); |
| 8933 | if (tmp == 1) |
| 8934 | return (1); |
| 8935 | } else if (((node->type == XML_TEXT_NODE) || |
| 8936 | (node->type == XML_CDATA_SECTION_NODE)) && |
| 8937 | (cur->type == XML_RELAXNG_TEXT)) { |
| 8938 | return (1); |
| 8939 | } |
| 8940 | cur = list[i++]; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8941 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8942 | return (0); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8943 | } |
| 8944 | |
| 8945 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8946 | * xmlRelaxNGValidateInterleave: |
| 8947 | * @ctxt: a Relax-NG validation context |
| 8948 | * @define: the definition to verify |
| 8949 | * |
| 8950 | * Validate an interleave definition for a node. |
| 8951 | * |
| 8952 | * Returns 0 if the validation succeeded or an error code. |
| 8953 | */ |
| 8954 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8955 | xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt, |
| 8956 | xmlRelaxNGDefinePtr define) |
| 8957 | { |
William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 8958 | int ret = 0, i, nbgroups; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8959 | int errNr = ctxt->errNr; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8960 | int oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8961 | |
| 8962 | xmlRelaxNGValidStatePtr oldstate; |
| 8963 | xmlRelaxNGPartitionPtr partitions; |
| 8964 | xmlRelaxNGInterleaveGroupPtr group = NULL; |
| 8965 | xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem; |
| 8966 | xmlNodePtr *list = NULL, *lasts = NULL; |
| 8967 | |
| 8968 | if (define->data != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8969 | partitions = (xmlRelaxNGPartitionPtr) define->data; |
| 8970 | nbgroups = partitions->nbgroups; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8971 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8972 | VALID_ERR(XML_RELAXNG_ERR_INTERNODATA); |
| 8973 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8974 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8975 | /* |
| 8976 | * Optimizations for MIXED |
| 8977 | */ |
| 8978 | oldflags = ctxt->flags; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 8979 | if (define->dflags & IS_MIXED) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8980 | ctxt->flags |= FLAGS_MIXED_CONTENT; |
| 8981 | if (nbgroups == 2) { |
| 8982 | /* |
| 8983 | * this is a pure <mixed> case |
| 8984 | */ |
| 8985 | if (ctxt->state != NULL) |
| 8986 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
| 8987 | ctxt->state->seq); |
| 8988 | if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT) |
| 8989 | ret = xmlRelaxNGValidateDefinition(ctxt, |
| 8990 | partitions->groups[1]-> |
| 8991 | rule); |
| 8992 | else |
| 8993 | ret = xmlRelaxNGValidateDefinition(ctxt, |
| 8994 | partitions->groups[0]-> |
| 8995 | rule); |
| 8996 | if (ret == 0) { |
| 8997 | if (ctxt->state != NULL) |
| 8998 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
| 8999 | ctxt->state-> |
| 9000 | seq); |
| 9001 | } |
| 9002 | ctxt->flags = oldflags; |
| 9003 | return (ret); |
| 9004 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9005 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9006 | |
| 9007 | /* |
| 9008 | * Build arrays to store the first and last node of the chain |
| 9009 | * pertaining to each group |
| 9010 | */ |
| 9011 | list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
| 9012 | if (list == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9013 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 9014 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9015 | } |
| 9016 | memset(list, 0, nbgroups * sizeof(xmlNodePtr)); |
| 9017 | lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
| 9018 | if (lasts == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9019 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 9020 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9021 | } |
| 9022 | memset(lasts, 0, nbgroups * sizeof(xmlNodePtr)); |
| 9023 | |
| 9024 | /* |
| 9025 | * Walk the sequence of children finding the right group and |
| 9026 | * sorting them in sequences. |
| 9027 | */ |
| 9028 | cur = ctxt->state->seq; |
| 9029 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 9030 | start = cur; |
| 9031 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9032 | ctxt->state->seq = cur; |
| 9033 | if ((partitions->triage != NULL) && |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9034 | (partitions->flags & IS_DETERMINIST)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9035 | void *tmp = NULL; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9036 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9037 | if ((cur->type == XML_TEXT_NODE) || |
| 9038 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 9039 | tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text", |
| 9040 | NULL); |
| 9041 | } else if (cur->type == XML_ELEMENT_NODE) { |
| 9042 | if (cur->ns != NULL) { |
| 9043 | tmp = xmlHashLookup2(partitions->triage, cur->name, |
| 9044 | cur->ns->href); |
| 9045 | if (tmp == NULL) |
| 9046 | tmp = xmlHashLookup2(partitions->triage, |
| 9047 | BAD_CAST "#any", |
| 9048 | cur->ns->href); |
| 9049 | } else |
| 9050 | tmp = |
| 9051 | xmlHashLookup2(partitions->triage, cur->name, |
| 9052 | NULL); |
| 9053 | if (tmp == NULL) |
| 9054 | tmp = |
| 9055 | xmlHashLookup2(partitions->triage, BAD_CAST "#any", |
| 9056 | NULL); |
| 9057 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9058 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9059 | if (tmp == NULL) { |
| 9060 | i = nbgroups; |
| 9061 | } else { |
| 9062 | i = ((long) tmp) - 1; |
| 9063 | if (partitions->flags & IS_NEEDCHECK) { |
| 9064 | group = partitions->groups[i]; |
| 9065 | if (!xmlRelaxNGNodeMatchesList(cur, group->defs)) |
| 9066 | i = nbgroups; |
| 9067 | } |
| 9068 | } |
| 9069 | } else { |
| 9070 | for (i = 0; i < nbgroups; i++) { |
| 9071 | group = partitions->groups[i]; |
| 9072 | if (group == NULL) |
| 9073 | continue; |
| 9074 | if (xmlRelaxNGNodeMatchesList(cur, group->defs)) |
| 9075 | break; |
| 9076 | } |
| 9077 | } |
| 9078 | /* |
| 9079 | * We break as soon as an element not matched is found |
| 9080 | */ |
| 9081 | if (i >= nbgroups) { |
| 9082 | break; |
| 9083 | } |
| 9084 | if (lasts[i] != NULL) { |
| 9085 | lasts[i]->next = cur; |
| 9086 | lasts[i] = cur; |
| 9087 | } else { |
| 9088 | list[i] = cur; |
| 9089 | lasts[i] = cur; |
| 9090 | } |
| 9091 | if (cur->next != NULL) |
| 9092 | lastchg = cur->next; |
| 9093 | else |
| 9094 | lastchg = cur; |
| 9095 | cur = xmlRelaxNGSkipIgnored(ctxt, cur->next); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9096 | } |
| 9097 | if (ret != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9098 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
| 9099 | ret = -1; |
| 9100 | goto done; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9101 | } |
| 9102 | lastelem = cur; |
| 9103 | oldstate = ctxt->state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9104 | for (i = 0; i < nbgroups; i++) { |
| 9105 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate); |
| 9106 | group = partitions->groups[i]; |
| 9107 | if (lasts[i] != NULL) { |
| 9108 | last = lasts[i]->next; |
| 9109 | lasts[i]->next = NULL; |
| 9110 | } |
| 9111 | ctxt->state->seq = list[i]; |
| 9112 | ret = xmlRelaxNGValidateDefinition(ctxt, group->rule); |
| 9113 | if (ret != 0) |
| 9114 | break; |
| 9115 | if (ctxt->state != NULL) { |
| 9116 | cur = ctxt->state->seq; |
| 9117 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 9118 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 9119 | oldstate = ctxt->state; |
| 9120 | ctxt->state = NULL; |
| 9121 | if (cur != NULL) { |
| 9122 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
| 9123 | ret = -1; |
| 9124 | ctxt->state = oldstate; |
| 9125 | goto done; |
| 9126 | } |
| 9127 | } else if (ctxt->states != NULL) { |
| 9128 | int j; |
| 9129 | int found = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9130 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9131 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 9132 | cur = ctxt->states->tabState[j]->seq; |
| 9133 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 9134 | if (cur == NULL) { |
| 9135 | found = 1; |
| 9136 | break; |
| 9137 | } |
| 9138 | } |
| 9139 | if (ctxt->states->nbState > 0) { |
| 9140 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 9141 | oldstate = |
| 9142 | ctxt->states->tabState[ctxt->states->nbState - 1]; |
| 9143 | } |
| 9144 | for (j = 0; j < ctxt->states->nbState - 1; j++) { |
| 9145 | xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[j]); |
| 9146 | } |
| 9147 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9148 | ctxt->states = NULL; |
| 9149 | if (found == 0) { |
| 9150 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
| 9151 | ret = -1; |
| 9152 | ctxt->state = oldstate; |
| 9153 | goto done; |
| 9154 | } |
| 9155 | } else { |
| 9156 | ret = -1; |
| 9157 | break; |
| 9158 | } |
| 9159 | if (lasts[i] != NULL) { |
| 9160 | lasts[i]->next = last; |
| 9161 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9162 | } |
| 9163 | if (ctxt->state != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9164 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9165 | ctxt->state = oldstate; |
| 9166 | ctxt->state->seq = lastelem; |
| 9167 | if (ret != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9168 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
| 9169 | ret = -1; |
| 9170 | goto done; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9171 | } |
| 9172 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9173 | done: |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9174 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9175 | /* |
| 9176 | * builds the next links chain from the prev one |
| 9177 | */ |
| 9178 | cur = lastchg; |
| 9179 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9180 | if ((cur == start) || (cur->prev == NULL)) |
| 9181 | break; |
| 9182 | cur->prev->next = cur; |
| 9183 | cur = cur->prev; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9184 | } |
| 9185 | if (ret == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9186 | if (ctxt->errNr > errNr) |
| 9187 | xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9188 | } |
| 9189 | |
| 9190 | xmlFree(list); |
| 9191 | xmlFree(lasts); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9192 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9193 | } |
| 9194 | |
| 9195 | /** |
| 9196 | * xmlRelaxNGValidateDefinitionList: |
| 9197 | * @ctxt: a Relax-NG validation context |
| 9198 | * @define: the list of definition to verify |
| 9199 | * |
| 9200 | * Validate the given node content against the (list) of definitions |
| 9201 | * |
| 9202 | * Returns 0 if the validation succeeded or an error code. |
| 9203 | */ |
| 9204 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9205 | xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt, |
| 9206 | xmlRelaxNGDefinePtr defines) |
| 9207 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9208 | int ret = 0, res; |
| 9209 | |
| 9210 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 9211 | if (defines == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9212 | VALID_ERR2(XML_RELAXNG_ERR_INTERNAL, |
| 9213 | BAD_CAST "NULL definition list"); |
| 9214 | return (-1); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 9215 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9216 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9217 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
| 9218 | res = xmlRelaxNGValidateDefinition(ctxt, defines); |
| 9219 | if (res < 0) |
| 9220 | ret = -1; |
| 9221 | } else { |
| 9222 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 9223 | return (-1); |
| 9224 | } |
| 9225 | if (res == -1) /* continues on -2 */ |
| 9226 | break; |
| 9227 | defines = defines->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9228 | } |
| 9229 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9230 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9231 | } |
| 9232 | |
| 9233 | /** |
| 9234 | * xmlRelaxNGElementMatch: |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9235 | * @ctxt: a Relax-NG validation context |
| 9236 | * @define: the definition to check |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9237 | * @elem: the element |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9238 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9239 | * Check if the element matches the definition nameClass |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9240 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9241 | * 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] | 9242 | */ |
| 9243 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9244 | xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 9245 | xmlRelaxNGDefinePtr define, xmlNodePtr elem) |
| 9246 | { |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 9247 | int ret = 0, oldflags = 0; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9248 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9249 | if (define->name != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9250 | if (!xmlStrEqual(elem->name, define->name)) { |
| 9251 | VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name); |
| 9252 | return (0); |
| 9253 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9254 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9255 | if ((define->ns != NULL) && (define->ns[0] != 0)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9256 | if (elem->ns == NULL) { |
| 9257 | VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, elem->name); |
| 9258 | return (0); |
| 9259 | } else if (!xmlStrEqual(elem->ns->href, define->ns)) { |
| 9260 | VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS, |
| 9261 | elem->name, define->ns); |
| 9262 | return (0); |
| 9263 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9264 | } else if ((elem->ns != NULL) && (define->ns != NULL) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9265 | (define->name == NULL)) { |
| 9266 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, elem->name); |
| 9267 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9268 | } else if ((elem->ns != NULL) && (define->name != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9269 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, define->name); |
| 9270 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9271 | } |
| 9272 | |
| 9273 | if (define->nameClass == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9274 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9275 | |
| 9276 | define = define->nameClass; |
| 9277 | if (define->type == XML_RELAXNG_EXCEPT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9278 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9279 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9280 | if (ctxt != NULL) { |
| 9281 | oldflags = ctxt->flags; |
| 9282 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9283 | } |
| 9284 | |
| 9285 | list = define->content; |
| 9286 | while (list != NULL) { |
| 9287 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
| 9288 | if (ret == 1) { |
| 9289 | if (ctxt != NULL) |
| 9290 | ctxt->flags = oldflags; |
| 9291 | return (0); |
| 9292 | } |
| 9293 | if (ret < 0) { |
| 9294 | if (ctxt != NULL) |
| 9295 | ctxt->flags = oldflags; |
| 9296 | return (ret); |
| 9297 | } |
| 9298 | list = list->next; |
| 9299 | } |
| 9300 | ret = 1; |
| 9301 | if (ctxt != NULL) { |
| 9302 | ctxt->flags = oldflags; |
| 9303 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9304 | } else if (define->type == XML_RELAXNG_CHOICE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9305 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9306 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9307 | if (ctxt != NULL) { |
| 9308 | oldflags = ctxt->flags; |
| 9309 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9310 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9311 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9312 | list = define->nameClass; |
| 9313 | while (list != NULL) { |
| 9314 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
| 9315 | if (ret == 1) { |
| 9316 | if (ctxt != NULL) |
| 9317 | ctxt->flags = oldflags; |
| 9318 | return (1); |
| 9319 | } |
| 9320 | if (ret < 0) { |
| 9321 | if (ctxt != NULL) |
| 9322 | ctxt->flags = oldflags; |
| 9323 | return (ret); |
| 9324 | } |
| 9325 | list = list->next; |
| 9326 | } |
| 9327 | if (ctxt != NULL) { |
| 9328 | if (ret != 0) { |
| 9329 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9330 | xmlRelaxNGDumpValidError(ctxt); |
| 9331 | } else { |
| 9332 | if (ctxt->errNr > 0) |
| 9333 | xmlRelaxNGPopErrors(ctxt, 0); |
| 9334 | } |
| 9335 | } |
| 9336 | ret = 0; |
| 9337 | if (ctxt != NULL) { |
| 9338 | ctxt->flags = oldflags; |
| 9339 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9340 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9341 | TODO ret = -1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9342 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9343 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9344 | } |
| 9345 | |
| 9346 | /** |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9347 | * xmlRelaxNGBestState: |
| 9348 | * @ctxt: a Relax-NG validation context |
| 9349 | * |
| 9350 | * Find the "best" state in the ctxt->states list of states to report |
| 9351 | * errors about. I.e. a state with no element left in the child list |
| 9352 | * or the one with the less attributes left. |
| 9353 | * This is called only if a falidation error was detected |
| 9354 | * |
| 9355 | * Returns the index of the "best" state or -1 in case of error |
| 9356 | */ |
| 9357 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9358 | xmlRelaxNGBestState(xmlRelaxNGValidCtxtPtr ctxt) |
| 9359 | { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9360 | xmlRelaxNGValidStatePtr state; |
| 9361 | int i, tmp; |
| 9362 | int best = -1; |
| 9363 | int value = 1000000; |
| 9364 | |
| 9365 | if ((ctxt == NULL) || (ctxt->states == NULL) || |
| 9366 | (ctxt->states->nbState <= 0)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9367 | return (-1); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9368 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9369 | for (i = 0; i < ctxt->states->nbState; i++) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9370 | state = ctxt->states->tabState[i]; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9371 | if (state == NULL) |
| 9372 | continue; |
| 9373 | if (state->seq != NULL) { |
| 9374 | if ((best == -1) || (value > 100000)) { |
| 9375 | value = 100000; |
| 9376 | best = i; |
| 9377 | } |
| 9378 | } else { |
| 9379 | tmp = state->nbAttrLeft; |
| 9380 | if ((best == -1) || (value > tmp)) { |
| 9381 | value = tmp; |
| 9382 | best = i; |
| 9383 | } |
| 9384 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9385 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9386 | return (best); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9387 | } |
| 9388 | |
| 9389 | /** |
| 9390 | * xmlRelaxNGLogBestError: |
| 9391 | * @ctxt: a Relax-NG validation context |
| 9392 | * |
| 9393 | * Find the "best" state in the ctxt->states list of states to report |
| 9394 | * errors about and log it. |
| 9395 | */ |
| 9396 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9397 | xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt) |
| 9398 | { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9399 | int best; |
| 9400 | |
| 9401 | if ((ctxt == NULL) || (ctxt->states == NULL) || |
| 9402 | (ctxt->states->nbState <= 0)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9403 | return; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9404 | |
| 9405 | best = xmlRelaxNGBestState(ctxt); |
| 9406 | if ((best >= 0) && (best < ctxt->states->nbState)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9407 | ctxt->state = ctxt->states->tabState[best]; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9408 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9409 | xmlRelaxNGValidateElementEnd(ctxt, 1); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9410 | } |
| 9411 | } |
| 9412 | |
| 9413 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9414 | * xmlRelaxNGValidateElementEnd: |
| 9415 | * @ctxt: a Relax-NG validation context |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9416 | * @log: indicate that error logging should be done |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9417 | * |
| 9418 | * Validate the end of the element, implements check that |
| 9419 | * there is nothing left not consumed in the element content |
| 9420 | * or in the attribute list. |
| 9421 | * |
| 9422 | * Returns 0 if the validation succeeded or an error code. |
| 9423 | */ |
| 9424 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9425 | xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int log) |
| 9426 | { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9427 | int i; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9428 | xmlRelaxNGValidStatePtr state; |
| 9429 | |
| 9430 | state = ctxt->state; |
| 9431 | if (state->seq != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9432 | state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq); |
| 9433 | if (state->seq != NULL) { |
| 9434 | if (log) { |
| 9435 | VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT, |
| 9436 | state->node->name, state->seq->name); |
| 9437 | } |
| 9438 | return (-1); |
| 9439 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9440 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9441 | for (i = 0; i < state->nbAttrs; i++) { |
| 9442 | if (state->attrs[i] != NULL) { |
| 9443 | if (log) { |
| 9444 | VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR, |
| 9445 | state->attrs[i]->name, state->node->name); |
| 9446 | } |
| 9447 | return (-1 - i); |
| 9448 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9449 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9450 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9451 | } |
| 9452 | |
| 9453 | /** |
| 9454 | * xmlRelaxNGValidateState: |
| 9455 | * @ctxt: a Relax-NG validation context |
| 9456 | * @define: the definition to verify |
| 9457 | * |
| 9458 | * Validate the current state against the definition |
| 9459 | * |
| 9460 | * Returns 0 if the validation succeeded or an error code. |
| 9461 | */ |
| 9462 | static int |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9463 | xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt, |
| 9464 | xmlRelaxNGDefinePtr define) |
| 9465 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9466 | xmlNodePtr node; |
| 9467 | int ret = 0, i, tmp, oldflags, errNr; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9468 | xmlRelaxNGValidStatePtr oldstate = NULL, state; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9469 | |
| 9470 | if (define == NULL) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9471 | VALID_ERR(XML_RELAXNG_ERR_NODEFINE); |
| 9472 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9473 | } |
| 9474 | |
| 9475 | if (ctxt->state != NULL) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9476 | node = ctxt->state->seq; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9477 | } else { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9478 | node = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9479 | } |
| 9480 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9481 | for (i = 0; i < ctxt->depth; i++) |
| 9482 | xmlGenericError(xmlGenericErrorContext, " "); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9483 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9484 | "Start validating %s ", xmlRelaxNGDefName(define)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9485 | if (define->name != NULL) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9486 | xmlGenericError(xmlGenericErrorContext, "%s ", define->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9487 | if ((node != NULL) && (node->name != NULL)) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9488 | xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9489 | else |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9490 | xmlGenericError(xmlGenericErrorContext, "\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9491 | #endif |
| 9492 | ctxt->depth++; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9493 | switch (define->type) { |
| 9494 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9495 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 9496 | ret = 0; |
| 9497 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9498 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9499 | ret = -1; |
| 9500 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9501 | case XML_RELAXNG_TEXT: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9502 | while ((node != NULL) && |
| 9503 | ((node->type == XML_TEXT_NODE) || |
| 9504 | (node->type == XML_COMMENT_NODE) || |
| 9505 | (node->type == XML_PI_NODE) || |
| 9506 | (node->type == XML_CDATA_SECTION_NODE))) |
| 9507 | node = node->next; |
| 9508 | ctxt->state->seq = node; |
| 9509 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9510 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9511 | errNr = ctxt->errNr; |
| 9512 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 9513 | if (node == NULL) { |
| 9514 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name); |
| 9515 | ret = -1; |
| 9516 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9517 | xmlRelaxNGDumpValidError(ctxt); |
| 9518 | break; |
| 9519 | } |
| 9520 | if (node->type != XML_ELEMENT_NODE) { |
| 9521 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
| 9522 | ret = -1; |
| 9523 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9524 | xmlRelaxNGDumpValidError(ctxt); |
| 9525 | break; |
| 9526 | } |
| 9527 | /* |
| 9528 | * This node was already validated successfully against |
| 9529 | * this definition. |
| 9530 | */ |
| 9531 | if (node->_private == define) { |
| 9532 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
| 9533 | if (ctxt->errNr > errNr) |
| 9534 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9535 | if (ctxt->errNr != 0) { |
| 9536 | while ((ctxt->err != NULL) && |
| 9537 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) |
| 9538 | && (xmlStrEqual(ctxt->err->arg2, node->name))) |
| 9539 | || |
| 9540 | ((ctxt->err->err == |
| 9541 | XML_RELAXNG_ERR_ELEMEXTRANS) |
| 9542 | && (xmlStrEqual(ctxt->err->arg1, node->name))) |
| 9543 | || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) |
| 9544 | || (ctxt->err->err == |
| 9545 | XML_RELAXNG_ERR_NOTELEM))) |
| 9546 | xmlRelaxNGValidErrorPop(ctxt); |
| 9547 | } |
| 9548 | break; |
| 9549 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9550 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9551 | ret = xmlRelaxNGElementMatch(ctxt, define, node); |
| 9552 | if (ret <= 0) { |
| 9553 | ret = -1; |
| 9554 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9555 | xmlRelaxNGDumpValidError(ctxt); |
| 9556 | break; |
| 9557 | } |
| 9558 | ret = 0; |
| 9559 | if (ctxt->errNr != 0) { |
| 9560 | if (ctxt->errNr > errNr) |
| 9561 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9562 | while ((ctxt->err != NULL) && |
| 9563 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) && |
| 9564 | (xmlStrEqual(ctxt->err->arg2, node->name))) || |
| 9565 | ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) && |
| 9566 | (xmlStrEqual(ctxt->err->arg1, node->name))) || |
| 9567 | (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) || |
| 9568 | (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM))) |
| 9569 | xmlRelaxNGValidErrorPop(ctxt); |
| 9570 | } |
| 9571 | errNr = ctxt->errNr; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9572 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9573 | oldflags = ctxt->flags; |
| 9574 | if (ctxt->flags & FLAGS_MIXED_CONTENT) { |
| 9575 | ctxt->flags -= FLAGS_MIXED_CONTENT; |
| 9576 | } |
| 9577 | state = xmlRelaxNGNewValidState(ctxt, node); |
| 9578 | if (state == NULL) { |
| 9579 | ret = -1; |
| 9580 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9581 | xmlRelaxNGDumpValidError(ctxt); |
| 9582 | break; |
| 9583 | } |
Daniel Veillard | 7fe1f3a | 2003-03-31 22:13:33 +0000 | [diff] [blame] | 9584 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9585 | oldstate = ctxt->state; |
| 9586 | ctxt->state = state; |
| 9587 | if (define->attrs != NULL) { |
| 9588 | tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
| 9589 | if (tmp != 0) { |
| 9590 | ret = -1; |
| 9591 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
| 9592 | } |
| 9593 | } |
| 9594 | if (define->contModel != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9595 | xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state; |
| 9596 | xmlRelaxNGStatesPtr tmpstates = ctxt->states; |
| 9597 | xmlNodePtr nseq; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9598 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9599 | nstate = xmlRelaxNGNewValidState(ctxt, node); |
| 9600 | ctxt->state = nstate; |
| 9601 | ctxt->states = NULL; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9602 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9603 | tmp = xmlRelaxNGValidateCompiledContent(ctxt, |
| 9604 | define->contModel, |
| 9605 | ctxt->state->seq); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9606 | nseq = ctxt->state->seq; |
| 9607 | ctxt->state = tmpstate; |
| 9608 | ctxt->states = tmpstates; |
| 9609 | xmlRelaxNGFreeValidState(ctxt, nstate); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9610 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9611 | #ifdef DEBUG_COMPILE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9612 | xmlGenericError(xmlGenericErrorContext, |
| 9613 | "Validating content of '%s' : %d\n", |
| 9614 | define->name, tmp); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9615 | #endif |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9616 | if (tmp != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9617 | ret = -1; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9618 | |
| 9619 | if (ctxt->states != NULL) { |
| 9620 | tmp = -1; |
| 9621 | |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9622 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9623 | state = ctxt->states->tabState[i]; |
| 9624 | ctxt->state = state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9625 | ctxt->state->seq = nseq; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9626 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9627 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9628 | tmp = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9629 | break; |
| 9630 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9631 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9632 | if (tmp != 0) { |
| 9633 | /* |
| 9634 | * validation error, log the message for the "best" one |
| 9635 | */ |
| 9636 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9637 | xmlRelaxNGLogBestError(ctxt); |
| 9638 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9639 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9640 | xmlRelaxNGFreeValidState(ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9641 | ctxt->states-> |
| 9642 | tabState[i]); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9643 | } |
| 9644 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9645 | ctxt->flags = oldflags; |
| 9646 | ctxt->states = NULL; |
| 9647 | if ((ret == 0) && (tmp == -1)) |
| 9648 | ret = -1; |
| 9649 | } else { |
| 9650 | state = ctxt->state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9651 | ctxt->state->seq = nseq; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9652 | if (ret == 0) |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9653 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9654 | xmlRelaxNGFreeValidState(ctxt, state); |
| 9655 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9656 | } else { |
| 9657 | if (define->content != NULL) { |
| 9658 | tmp = xmlRelaxNGValidateDefinitionList(ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9659 | define-> |
| 9660 | content); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9661 | if (tmp != 0) { |
| 9662 | ret = -1; |
| 9663 | if (ctxt->state == NULL) { |
| 9664 | ctxt->state = oldstate; |
| 9665 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, |
| 9666 | node->name); |
| 9667 | ctxt->state = NULL; |
| 9668 | } else { |
| 9669 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, |
| 9670 | node->name); |
| 9671 | } |
| 9672 | |
| 9673 | } |
| 9674 | } |
| 9675 | if (ctxt->states != NULL) { |
| 9676 | tmp = -1; |
| 9677 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9678 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9679 | state = ctxt->states->tabState[i]; |
| 9680 | ctxt->state = state; |
| 9681 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9682 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9683 | tmp = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9684 | break; |
| 9685 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9686 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9687 | if (tmp != 0) { |
| 9688 | /* |
| 9689 | * validation error, log the message for the "best" one |
| 9690 | */ |
| 9691 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9692 | xmlRelaxNGLogBestError(ctxt); |
| 9693 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9694 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9695 | xmlRelaxNGFreeValidState(ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9696 | ctxt->states-> |
| 9697 | tabState[i]); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9698 | } |
| 9699 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9700 | ctxt->flags = oldflags; |
| 9701 | ctxt->states = NULL; |
| 9702 | if ((ret == 0) && (tmp == -1)) |
| 9703 | ret = -1; |
| 9704 | } else { |
| 9705 | state = ctxt->state; |
| 9706 | if (ret == 0) |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9707 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9708 | xmlRelaxNGFreeValidState(ctxt, state); |
| 9709 | } |
| 9710 | } |
| 9711 | if (ret == 0) { |
| 9712 | node->_private = define; |
| 9713 | } |
| 9714 | ctxt->flags = oldflags; |
| 9715 | ctxt->state = oldstate; |
| 9716 | if (oldstate != NULL) |
| 9717 | oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
| 9718 | if (ret != 0) { |
| 9719 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 9720 | xmlRelaxNGDumpValidError(ctxt); |
| 9721 | ret = 0; |
| 9722 | } else { |
| 9723 | ret = -2; |
| 9724 | } |
| 9725 | } else { |
| 9726 | if (ctxt->errNr > errNr) |
| 9727 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9728 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9729 | |
| 9730 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9731 | xmlGenericError(xmlGenericErrorContext, |
| 9732 | "xmlRelaxNGValidateDefinition(): validated %s : %d", |
| 9733 | node->name, ret); |
| 9734 | if (oldstate == NULL) |
| 9735 | xmlGenericError(xmlGenericErrorContext, ": no state\n"); |
| 9736 | else if (oldstate->seq == NULL) |
| 9737 | xmlGenericError(xmlGenericErrorContext, ": done\n"); |
| 9738 | else if (oldstate->seq->type == XML_ELEMENT_NODE) |
| 9739 | xmlGenericError(xmlGenericErrorContext, ": next elem %s\n", |
| 9740 | oldstate->seq->name); |
| 9741 | else |
| 9742 | xmlGenericError(xmlGenericErrorContext, ": next %s %d\n", |
| 9743 | oldstate->seq->name, oldstate->seq->type); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9744 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9745 | break; |
| 9746 | case XML_RELAXNG_OPTIONAL:{ |
| 9747 | errNr = ctxt->errNr; |
| 9748 | oldflags = ctxt->flags; |
| 9749 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9750 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
| 9751 | ret = |
| 9752 | xmlRelaxNGValidateDefinitionList(ctxt, |
| 9753 | define->content); |
| 9754 | if (ret != 0) { |
| 9755 | if (ctxt->state != NULL) |
| 9756 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 9757 | ctxt->state = oldstate; |
| 9758 | ctxt->flags = oldflags; |
| 9759 | ret = 0; |
| 9760 | if (ctxt->errNr > errNr) |
| 9761 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9762 | break; |
| 9763 | } |
| 9764 | if (ctxt->states != NULL) { |
| 9765 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
| 9766 | } else { |
| 9767 | ctxt->states = xmlRelaxNGNewStates(ctxt, 1); |
| 9768 | if (ctxt->states == NULL) { |
| 9769 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 9770 | ctxt->flags = oldflags; |
| 9771 | ret = -1; |
| 9772 | if (ctxt->errNr > errNr) |
| 9773 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9774 | break; |
| 9775 | } |
| 9776 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
| 9777 | xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state); |
| 9778 | ctxt->state = NULL; |
| 9779 | } |
| 9780 | ctxt->flags = oldflags; |
| 9781 | ret = 0; |
| 9782 | if (ctxt->errNr > errNr) |
| 9783 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9784 | break; |
| 9785 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9786 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9787 | errNr = ctxt->errNr; |
| 9788 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 9789 | if (ret != 0) { |
| 9790 | break; |
| 9791 | } |
| 9792 | if (ctxt->errNr > errNr) |
| 9793 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9794 | /* no break on purpose */ |
| 9795 | case XML_RELAXNG_ZEROORMORE:{ |
| 9796 | int progress; |
| 9797 | xmlRelaxNGStatesPtr states = NULL, res = NULL; |
| 9798 | int base, j; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9799 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9800 | errNr = ctxt->errNr; |
| 9801 | res = xmlRelaxNGNewStates(ctxt, 1); |
| 9802 | if (res == NULL) { |
| 9803 | ret = -1; |
| 9804 | break; |
| 9805 | } |
| 9806 | /* |
| 9807 | * All the input states are also exit states |
| 9808 | */ |
| 9809 | if (ctxt->state != NULL) { |
| 9810 | xmlRelaxNGAddStates(ctxt, res, |
| 9811 | xmlRelaxNGCopyValidState(ctxt, |
| 9812 | ctxt-> |
| 9813 | state)); |
| 9814 | } else { |
| 9815 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 9816 | xmlRelaxNGAddStates(ctxt, res, |
| 9817 | xmlRelaxNGCopyValidState(ctxt, |
| 9818 | ctxt-> |
| 9819 | states-> |
| 9820 | tabState |
| 9821 | [j])); |
| 9822 | } |
| 9823 | } |
| 9824 | oldflags = ctxt->flags; |
| 9825 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9826 | do { |
| 9827 | progress = 0; |
| 9828 | base = res->nbState; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9829 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9830 | if (ctxt->states != NULL) { |
| 9831 | states = ctxt->states; |
| 9832 | for (i = 0; i < states->nbState; i++) { |
| 9833 | ctxt->state = states->tabState[i]; |
| 9834 | ctxt->states = NULL; |
| 9835 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
| 9836 | define-> |
| 9837 | content); |
| 9838 | if (ret == 0) { |
| 9839 | if (ctxt->state != NULL) { |
| 9840 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 9841 | ctxt->state); |
| 9842 | ctxt->state = NULL; |
| 9843 | if (tmp == 1) |
| 9844 | progress = 1; |
| 9845 | } else if (ctxt->states != NULL) { |
| 9846 | for (j = 0; j < ctxt->states->nbState; |
| 9847 | j++) { |
| 9848 | tmp = |
| 9849 | xmlRelaxNGAddStates(ctxt, res, |
| 9850 | ctxt-> |
| 9851 | states-> |
| 9852 | tabState |
| 9853 | [j]); |
| 9854 | if (tmp == 1) |
| 9855 | progress = 1; |
| 9856 | } |
| 9857 | xmlRelaxNGFreeStates(ctxt, |
| 9858 | ctxt->states); |
| 9859 | ctxt->states = NULL; |
| 9860 | } |
| 9861 | } else { |
| 9862 | if (ctxt->state != NULL) { |
| 9863 | xmlRelaxNGFreeValidState(ctxt, |
| 9864 | ctxt->state); |
| 9865 | ctxt->state = NULL; |
| 9866 | } |
| 9867 | } |
| 9868 | } |
| 9869 | } else { |
| 9870 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
| 9871 | define-> |
| 9872 | content); |
| 9873 | if (ret != 0) { |
| 9874 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 9875 | ctxt->state = NULL; |
| 9876 | } else { |
| 9877 | base = res->nbState; |
| 9878 | if (ctxt->state != NULL) { |
| 9879 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 9880 | ctxt->state); |
| 9881 | ctxt->state = NULL; |
| 9882 | if (tmp == 1) |
| 9883 | progress = 1; |
| 9884 | } else if (ctxt->states != NULL) { |
| 9885 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 9886 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 9887 | ctxt-> |
| 9888 | states-> |
| 9889 | tabState[j]); |
| 9890 | if (tmp == 1) |
| 9891 | progress = 1; |
| 9892 | } |
| 9893 | if (states == NULL) { |
| 9894 | states = ctxt->states; |
| 9895 | } else { |
| 9896 | xmlRelaxNGFreeStates(ctxt, |
| 9897 | ctxt->states); |
| 9898 | } |
| 9899 | ctxt->states = NULL; |
| 9900 | } |
| 9901 | } |
| 9902 | } |
| 9903 | if (progress) { |
| 9904 | /* |
| 9905 | * Collect all the new nodes added at that step |
| 9906 | * and make them the new node set |
| 9907 | */ |
| 9908 | if (res->nbState - base == 1) { |
| 9909 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, |
| 9910 | res-> |
| 9911 | tabState |
| 9912 | [base]); |
| 9913 | } else { |
| 9914 | if (states == NULL) { |
| 9915 | xmlRelaxNGNewStates(ctxt, |
| 9916 | res->nbState - base); |
| 9917 | } |
| 9918 | states->nbState = 0; |
| 9919 | for (i = base; i < res->nbState; i++) |
| 9920 | xmlRelaxNGAddStates(ctxt, states, |
| 9921 | xmlRelaxNGCopyValidState |
| 9922 | (ctxt, |
| 9923 | res->tabState[i])); |
| 9924 | ctxt->states = states; |
| 9925 | } |
| 9926 | } |
| 9927 | } while (progress == 1); |
| 9928 | if (states != NULL) { |
| 9929 | xmlRelaxNGFreeStates(ctxt, states); |
| 9930 | } |
| 9931 | ctxt->states = res; |
| 9932 | ctxt->flags = oldflags; |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 9933 | #if 0 |
| 9934 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9935 | * errors may have to be propagated back... |
| 9936 | */ |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9937 | if (ctxt->errNr > errNr) |
| 9938 | xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 9939 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9940 | ret = 0; |
| 9941 | break; |
| 9942 | } |
| 9943 | case XML_RELAXNG_CHOICE:{ |
| 9944 | xmlRelaxNGDefinePtr list = NULL; |
| 9945 | xmlRelaxNGStatesPtr states = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9946 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9947 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9948 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9949 | errNr = ctxt->errNr; |
| 9950 | if ((define->dflags & IS_TRIABLE) |
| 9951 | && (define->data != NULL)) { |
| 9952 | xmlHashTablePtr triage = |
| 9953 | (xmlHashTablePtr) define->data; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 9954 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9955 | /* |
| 9956 | * Something we can optimize cleanly there is only one |
| 9957 | * possble branch out ! |
| 9958 | */ |
| 9959 | if (node == NULL) { |
| 9960 | ret = -1; |
| 9961 | break; |
| 9962 | } |
| 9963 | if ((node->type == XML_TEXT_NODE) || |
| 9964 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 9965 | list = |
| 9966 | xmlHashLookup2(triage, BAD_CAST "#text", NULL); |
| 9967 | } else if (node->type == XML_ELEMENT_NODE) { |
| 9968 | if (node->ns != NULL) { |
| 9969 | list = xmlHashLookup2(triage, node->name, |
| 9970 | node->ns->href); |
| 9971 | if (list == NULL) |
| 9972 | list = |
| 9973 | xmlHashLookup2(triage, BAD_CAST "#any", |
| 9974 | node->ns->href); |
| 9975 | } else |
| 9976 | list = |
| 9977 | xmlHashLookup2(triage, node->name, NULL); |
| 9978 | if (list == NULL) |
| 9979 | list = |
| 9980 | xmlHashLookup2(triage, BAD_CAST "#any", |
| 9981 | NULL); |
| 9982 | } |
| 9983 | if (list == NULL) { |
| 9984 | ret = -1; |
| 9985 | break; |
| 9986 | } |
| 9987 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
| 9988 | if (ret == 0) { |
| 9989 | } |
| 9990 | break; |
| 9991 | } |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 9992 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9993 | list = define->content; |
| 9994 | oldflags = ctxt->flags; |
| 9995 | ctxt->flags |= FLAGS_IGNORABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9996 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9997 | while (list != NULL) { |
| 9998 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
| 9999 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
| 10000 | if (ret == 0) { |
| 10001 | if (states == NULL) { |
| 10002 | states = xmlRelaxNGNewStates(ctxt, 1); |
| 10003 | } |
| 10004 | if (ctxt->state != NULL) { |
| 10005 | xmlRelaxNGAddStates(ctxt, states, ctxt->state); |
| 10006 | } else if (ctxt->states != NULL) { |
| 10007 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 10008 | xmlRelaxNGAddStates(ctxt, states, |
| 10009 | ctxt->states-> |
| 10010 | tabState[i]); |
| 10011 | } |
| 10012 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10013 | ctxt->states = NULL; |
| 10014 | } |
| 10015 | } else { |
| 10016 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10017 | } |
| 10018 | ctxt->state = oldstate; |
| 10019 | list = list->next; |
| 10020 | } |
| 10021 | if (states != NULL) { |
| 10022 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 10023 | ctxt->states = states; |
| 10024 | ctxt->state = NULL; |
| 10025 | ret = 0; |
| 10026 | } else { |
| 10027 | ctxt->states = NULL; |
| 10028 | } |
| 10029 | ctxt->flags = oldflags; |
| 10030 | if (ret != 0) { |
| 10031 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 10032 | xmlRelaxNGDumpValidError(ctxt); |
| 10033 | } |
| 10034 | } else { |
| 10035 | if (ctxt->errNr > errNr) |
| 10036 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 10037 | } |
| 10038 | break; |
| 10039 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10040 | case XML_RELAXNG_DEF: |
| 10041 | case XML_RELAXNG_GROUP: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10042 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 10043 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10044 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10045 | ret = xmlRelaxNGValidateInterleave(ctxt, define); |
| 10046 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10047 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10048 | ret = xmlRelaxNGValidateAttribute(ctxt, define); |
| 10049 | break; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 10050 | case XML_RELAXNG_START: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10051 | case XML_RELAXNG_NOOP: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10052 | case XML_RELAXNG_REF: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10053 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 10054 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10055 | ret = xmlRelaxNGValidateDefinition(ctxt, define->content); |
| 10056 | break; |
| 10057 | case XML_RELAXNG_DATATYPE:{ |
| 10058 | xmlNodePtr child; |
| 10059 | xmlChar *content = NULL; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10060 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10061 | child = node; |
| 10062 | while (child != NULL) { |
| 10063 | if (child->type == XML_ELEMENT_NODE) { |
| 10064 | VALID_ERR2(XML_RELAXNG_ERR_DATAELEM, |
| 10065 | node->parent->name); |
| 10066 | ret = -1; |
| 10067 | break; |
| 10068 | } else if ((child->type == XML_TEXT_NODE) || |
| 10069 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 10070 | content = xmlStrcat(content, child->content); |
| 10071 | } |
| 10072 | /* TODO: handle entities ... */ |
| 10073 | child = child->next; |
| 10074 | } |
| 10075 | if (ret == -1) { |
| 10076 | if (content != NULL) |
| 10077 | xmlFree(content); |
| 10078 | break; |
| 10079 | } |
| 10080 | if (content == NULL) { |
| 10081 | content = xmlStrdup(BAD_CAST ""); |
| 10082 | if (content == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10083 | xmlRngVErrMemory(ctxt, "validating\n"); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10084 | ret = -1; |
| 10085 | break; |
| 10086 | } |
| 10087 | } |
| 10088 | ret = xmlRelaxNGValidateDatatype(ctxt, content, define, |
| 10089 | ctxt->state->seq); |
| 10090 | if (ret == -1) { |
| 10091 | VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name); |
| 10092 | } else if (ret == 0) { |
| 10093 | ctxt->state->seq = NULL; |
| 10094 | } |
| 10095 | if (content != NULL) |
| 10096 | xmlFree(content); |
| 10097 | break; |
| 10098 | } |
| 10099 | case XML_RELAXNG_VALUE:{ |
| 10100 | xmlChar *content = NULL; |
| 10101 | xmlChar *oldvalue; |
| 10102 | xmlNodePtr child; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10103 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10104 | child = node; |
| 10105 | while (child != NULL) { |
| 10106 | if (child->type == XML_ELEMENT_NODE) { |
| 10107 | VALID_ERR2(XML_RELAXNG_ERR_VALELEM, |
| 10108 | node->parent->name); |
| 10109 | ret = -1; |
| 10110 | break; |
| 10111 | } else if ((child->type == XML_TEXT_NODE) || |
| 10112 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 10113 | content = xmlStrcat(content, child->content); |
| 10114 | } |
| 10115 | /* TODO: handle entities ... */ |
| 10116 | child = child->next; |
| 10117 | } |
| 10118 | if (ret == -1) { |
| 10119 | if (content != NULL) |
| 10120 | xmlFree(content); |
| 10121 | break; |
| 10122 | } |
| 10123 | if (content == NULL) { |
| 10124 | content = xmlStrdup(BAD_CAST ""); |
| 10125 | if (content == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10126 | xmlRngVErrMemory(ctxt, "validating\n"); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10127 | ret = -1; |
| 10128 | break; |
| 10129 | } |
| 10130 | } |
| 10131 | oldvalue = ctxt->state->value; |
| 10132 | ctxt->state->value = content; |
| 10133 | ret = xmlRelaxNGValidateValue(ctxt, define); |
| 10134 | ctxt->state->value = oldvalue; |
| 10135 | if (ret == -1) { |
| 10136 | VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name); |
| 10137 | } else if (ret == 0) { |
| 10138 | ctxt->state->seq = NULL; |
| 10139 | } |
| 10140 | if (content != NULL) |
| 10141 | xmlFree(content); |
| 10142 | break; |
| 10143 | } |
| 10144 | case XML_RELAXNG_LIST:{ |
| 10145 | xmlChar *content; |
| 10146 | xmlNodePtr child; |
| 10147 | xmlChar *oldvalue, *oldendvalue; |
| 10148 | int len; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10149 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10150 | /* |
| 10151 | * Make sure it's only text nodes |
| 10152 | */ |
| 10153 | |
| 10154 | content = NULL; |
| 10155 | child = node; |
| 10156 | while (child != NULL) { |
| 10157 | if (child->type == XML_ELEMENT_NODE) { |
| 10158 | VALID_ERR2(XML_RELAXNG_ERR_LISTELEM, |
| 10159 | node->parent->name); |
| 10160 | ret = -1; |
| 10161 | break; |
| 10162 | } else if ((child->type == XML_TEXT_NODE) || |
| 10163 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 10164 | content = xmlStrcat(content, child->content); |
| 10165 | } |
| 10166 | /* TODO: handle entities ... */ |
| 10167 | child = child->next; |
| 10168 | } |
| 10169 | if (ret == -1) { |
| 10170 | if (content != NULL) |
| 10171 | xmlFree(content); |
| 10172 | break; |
| 10173 | } |
| 10174 | if (content == NULL) { |
| 10175 | content = xmlStrdup(BAD_CAST ""); |
| 10176 | if (content == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10177 | xmlRngVErrMemory(ctxt, "validating\n"); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10178 | ret = -1; |
| 10179 | break; |
| 10180 | } |
| 10181 | } |
| 10182 | len = xmlStrlen(content); |
| 10183 | oldvalue = ctxt->state->value; |
| 10184 | oldendvalue = ctxt->state->endvalue; |
| 10185 | ctxt->state->value = content; |
| 10186 | ctxt->state->endvalue = content + len; |
| 10187 | ret = xmlRelaxNGValidateValue(ctxt, define); |
| 10188 | ctxt->state->value = oldvalue; |
| 10189 | ctxt->state->endvalue = oldendvalue; |
| 10190 | if (ret == -1) { |
| 10191 | VALID_ERR(XML_RELAXNG_ERR_LIST); |
| 10192 | } else if ((ret == 0) && (node != NULL)) { |
| 10193 | ctxt->state->seq = node->next; |
| 10194 | } |
| 10195 | if (content != NULL) |
| 10196 | xmlFree(content); |
| 10197 | break; |
| 10198 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10199 | case XML_RELAXNG_EXCEPT: |
| 10200 | case XML_RELAXNG_PARAM: |
| 10201 | TODO ret = -1; |
| 10202 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10203 | } |
| 10204 | ctxt->depth--; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10205 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10206 | for (i = 0; i < ctxt->depth; i++) |
| 10207 | xmlGenericError(xmlGenericErrorContext, " "); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10208 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10209 | "Validating %s ", xmlRelaxNGDefName(define)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10210 | if (define->name != NULL) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10211 | xmlGenericError(xmlGenericErrorContext, "%s ", define->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10212 | if (ret == 0) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10213 | xmlGenericError(xmlGenericErrorContext, "suceeded\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10214 | else |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10215 | xmlGenericError(xmlGenericErrorContext, "failed\n"); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10216 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10217 | return (ret); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10218 | } |
| 10219 | |
| 10220 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10221 | * xmlRelaxNGValidateDefinition: |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10222 | * @ctxt: a Relax-NG validation context |
| 10223 | * @define: the definition to verify |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10224 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10225 | * Validate the current node lists against the definition |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10226 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10227 | * Returns 0 if the validation succeeded or an error code. |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10228 | */ |
| 10229 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10230 | xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
| 10231 | xmlRelaxNGDefinePtr define) |
| 10232 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10233 | xmlRelaxNGStatesPtr states, res; |
| 10234 | int i, j, k, ret, oldflags; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10235 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10236 | /* |
| 10237 | * We should NOT have both ctxt->state and ctxt->states |
| 10238 | */ |
| 10239 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10240 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10241 | ctxt->state = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10242 | } |
| 10243 | |
| 10244 | if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10245 | if (ctxt->states != NULL) { |
| 10246 | ctxt->state = ctxt->states->tabState[0]; |
| 10247 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10248 | ctxt->states = NULL; |
| 10249 | } |
| 10250 | ret = xmlRelaxNGValidateState(ctxt, define); |
| 10251 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10252 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10253 | ctxt->state = NULL; |
| 10254 | } |
| 10255 | if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) { |
| 10256 | ctxt->state = ctxt->states->tabState[0]; |
| 10257 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10258 | ctxt->states = NULL; |
| 10259 | } |
| 10260 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10261 | } |
| 10262 | |
| 10263 | states = ctxt->states; |
| 10264 | ctxt->states = NULL; |
| 10265 | res = NULL; |
| 10266 | j = 0; |
| 10267 | oldflags = ctxt->flags; |
| 10268 | ctxt->flags |= FLAGS_IGNORABLE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10269 | for (i = 0; i < states->nbState; i++) { |
| 10270 | ctxt->state = states->tabState[i]; |
| 10271 | ctxt->states = NULL; |
| 10272 | ret = xmlRelaxNGValidateState(ctxt, define); |
| 10273 | /* |
| 10274 | * We should NOT have both ctxt->state and ctxt->states |
| 10275 | */ |
| 10276 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10277 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10278 | ctxt->state = NULL; |
| 10279 | } |
| 10280 | if (ret == 0) { |
| 10281 | if (ctxt->states == NULL) { |
| 10282 | if (res != NULL) { |
| 10283 | /* add the state to the container */ |
| 10284 | xmlRelaxNGAddStates(ctxt, res, ctxt->state); |
| 10285 | ctxt->state = NULL; |
| 10286 | } else { |
| 10287 | /* add the state directly in states */ |
| 10288 | states->tabState[j++] = ctxt->state; |
| 10289 | ctxt->state = NULL; |
| 10290 | } |
| 10291 | } else { |
| 10292 | if (res == NULL) { |
| 10293 | /* make it the new container and copy other results */ |
| 10294 | res = ctxt->states; |
| 10295 | ctxt->states = NULL; |
| 10296 | for (k = 0; k < j; k++) |
| 10297 | xmlRelaxNGAddStates(ctxt, res, |
| 10298 | states->tabState[k]); |
| 10299 | } else { |
| 10300 | /* add all the new results to res and reff the container */ |
| 10301 | for (k = 0; k < ctxt->states->nbState; k++) |
| 10302 | xmlRelaxNGAddStates(ctxt, res, |
| 10303 | ctxt->states->tabState[k]); |
| 10304 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10305 | ctxt->states = NULL; |
| 10306 | } |
| 10307 | } |
| 10308 | } else { |
| 10309 | if (ctxt->state != NULL) { |
| 10310 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10311 | ctxt->state = NULL; |
| 10312 | } else if (ctxt->states != NULL) { |
| 10313 | for (k = 0; k < ctxt->states->nbState; k++) |
| 10314 | xmlRelaxNGFreeValidState(ctxt, |
| 10315 | ctxt->states->tabState[k]); |
| 10316 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10317 | ctxt->states = NULL; |
| 10318 | } |
| 10319 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10320 | } |
| 10321 | ctxt->flags = oldflags; |
| 10322 | if (res != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10323 | xmlRelaxNGFreeStates(ctxt, states); |
| 10324 | ctxt->states = res; |
| 10325 | ret = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10326 | } else if (j > 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10327 | states->nbState = j; |
| 10328 | ctxt->states = states; |
| 10329 | ret = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10330 | } else if (j == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10331 | ctxt->state = states->tabState[0]; |
| 10332 | xmlRelaxNGFreeStates(ctxt, states); |
| 10333 | ret = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10334 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10335 | ret = -1; |
| 10336 | xmlRelaxNGFreeStates(ctxt, states); |
| 10337 | if (ctxt->states != NULL) { |
| 10338 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10339 | ctxt->states = NULL; |
| 10340 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10341 | } |
| 10342 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10343 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10344 | ctxt->state = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10345 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10346 | return (ret); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10347 | } |
| 10348 | |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10349 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10350 | * xmlRelaxNGValidateDocument: |
| 10351 | * @ctxt: a Relax-NG validation context |
| 10352 | * @doc: the document |
| 10353 | * |
| 10354 | * Validate the given document |
| 10355 | * |
| 10356 | * Returns 0 if the validation succeeded or an error code. |
| 10357 | */ |
| 10358 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10359 | xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) |
| 10360 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10361 | int ret; |
| 10362 | xmlRelaxNGPtr schema; |
| 10363 | xmlRelaxNGGrammarPtr grammar; |
| 10364 | xmlRelaxNGValidStatePtr state; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10365 | xmlNodePtr node; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10366 | |
| 10367 | if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10368 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10369 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10370 | ctxt->errNo = XML_RELAXNG_OK; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10371 | schema = ctxt->schema; |
| 10372 | grammar = schema->topgrammar; |
| 10373 | if (grammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10374 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 10375 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10376 | } |
| 10377 | state = xmlRelaxNGNewValidState(ctxt, NULL); |
| 10378 | ctxt->state = state; |
| 10379 | ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10380 | if ((ctxt->state != NULL) && (state->seq != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10381 | state = ctxt->state; |
| 10382 | node = state->seq; |
| 10383 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 10384 | if (node != NULL) { |
| 10385 | if (ret != -1) { |
| 10386 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
| 10387 | ret = -1; |
| 10388 | } |
| 10389 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10390 | } else if (ctxt->states != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10391 | int i; |
| 10392 | int tmp = -1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10393 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10394 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 10395 | state = ctxt->states->tabState[i]; |
| 10396 | node = state->seq; |
| 10397 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 10398 | if (node == NULL) |
| 10399 | tmp = 0; |
| 10400 | xmlRelaxNGFreeValidState(ctxt, state); |
| 10401 | } |
| 10402 | if (tmp == -1) { |
| 10403 | if (ret != -1) { |
| 10404 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
| 10405 | ret = -1; |
| 10406 | } |
| 10407 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10408 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 10409 | if (ctxt->state != NULL) { |
| 10410 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10411 | ctxt->state = NULL; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 10412 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10413 | if (ret != 0) |
| 10414 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 10415 | #ifdef DEBUG |
| 10416 | else if (ctxt->errNr != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10417 | ctxt->error(ctxt->userData, |
| 10418 | "%d Extra error messages left on stack !\n", |
| 10419 | ctxt->errNr); |
| 10420 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 10421 | } |
| 10422 | #endif |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10423 | if (ctxt->idref == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10424 | xmlValidCtxt vctxt; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10425 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10426 | memset(&vctxt, 0, sizeof(xmlValidCtxt)); |
| 10427 | vctxt.valid = 1; |
| 10428 | vctxt.error = ctxt->error; |
| 10429 | vctxt.warning = ctxt->warning; |
| 10430 | vctxt.userData = ctxt->userData; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10431 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10432 | if (xmlValidateDocumentFinal(&vctxt, doc) != 1) |
| 10433 | ret = -1; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10434 | } |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10435 | if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10436 | ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10437 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10438 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10439 | } |
| 10440 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10441 | /************************************************************************ |
| 10442 | * * |
| 10443 | * Validation interfaces * |
| 10444 | * * |
| 10445 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10446 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10447 | /** |
| 10448 | * xmlRelaxNGNewValidCtxt: |
| 10449 | * @schema: a precompiled XML RelaxNGs |
| 10450 | * |
| 10451 | * Create an XML RelaxNGs validation context based on the given schema |
| 10452 | * |
| 10453 | * Returns the validation context or NULL in case of error |
| 10454 | */ |
| 10455 | xmlRelaxNGValidCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10456 | xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema) |
| 10457 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10458 | xmlRelaxNGValidCtxtPtr ret; |
| 10459 | |
| 10460 | ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt)); |
| 10461 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10462 | xmlRngVErrMemory(NULL, "building context\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10463 | return (NULL); |
| 10464 | } |
| 10465 | memset(ret, 0, sizeof(xmlRelaxNGValidCtxt)); |
| 10466 | ret->schema = schema; |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 10467 | ret->error = xmlGenericError; |
| 10468 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 10469 | ret->errNr = 0; |
| 10470 | ret->errMax = 0; |
| 10471 | ret->err = NULL; |
| 10472 | ret->errTab = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10473 | ret->idref = schema->idref; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10474 | ret->states = NULL; |
| 10475 | ret->freeState = NULL; |
| 10476 | ret->freeStates = NULL; |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10477 | ret->errNo = XML_RELAXNG_OK; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10478 | return (ret); |
| 10479 | } |
| 10480 | |
| 10481 | /** |
| 10482 | * xmlRelaxNGFreeValidCtxt: |
| 10483 | * @ctxt: the schema validation context |
| 10484 | * |
| 10485 | * Free the resources associated to the schema validation context |
| 10486 | */ |
| 10487 | void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10488 | xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt) |
| 10489 | { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10490 | int k; |
| 10491 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10492 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10493 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10494 | if (ctxt->states != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10495 | xmlRelaxNGFreeStates(NULL, ctxt->states); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10496 | if (ctxt->freeState != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10497 | for (k = 0; k < ctxt->freeState->nbState; k++) { |
| 10498 | xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]); |
| 10499 | } |
| 10500 | xmlRelaxNGFreeStates(NULL, ctxt->freeState); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10501 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10502 | if (ctxt->freeStates != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10503 | for (k = 0; k < ctxt->freeStatesNr; k++) { |
| 10504 | xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]); |
| 10505 | } |
| 10506 | xmlFree(ctxt->freeStates); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10507 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 10508 | if (ctxt->errTab != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10509 | xmlFree(ctxt->errTab); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 10510 | if (ctxt->elemTab != NULL) { |
| 10511 | xmlRegExecCtxtPtr exec; |
| 10512 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10513 | exec = xmlRelaxNGElemPop(ctxt); |
| 10514 | while (exec != NULL) { |
| 10515 | xmlRegFreeExecCtxt(exec); |
| 10516 | exec = xmlRelaxNGElemPop(ctxt); |
| 10517 | } |
| 10518 | xmlFree(ctxt->elemTab); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 10519 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10520 | xmlFree(ctxt); |
| 10521 | } |
| 10522 | |
| 10523 | /** |
| 10524 | * xmlRelaxNGSetValidErrors: |
| 10525 | * @ctxt: a Relax-NG validation context |
| 10526 | * @err: the error function |
| 10527 | * @warn: the warning function |
| 10528 | * @ctx: the functions context |
| 10529 | * |
| 10530 | * Set the error and warning callback informations |
| 10531 | */ |
| 10532 | void |
| 10533 | xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10534 | xmlRelaxNGValidityErrorFunc err, |
| 10535 | xmlRelaxNGValidityWarningFunc warn, void *ctx) |
| 10536 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10537 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10538 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10539 | ctxt->error = err; |
| 10540 | ctxt->warning = warn; |
| 10541 | ctxt->userData = ctx; |
| 10542 | } |
| 10543 | |
| 10544 | /** |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 10545 | * xmlRelaxNGGetValidErrors: |
| 10546 | * @ctxt: a Relax-NG validation context |
| 10547 | * @err: the error function result |
| 10548 | * @warn: the warning function result |
| 10549 | * @ctx: the functions context result |
| 10550 | * |
| 10551 | * Get the error and warning callback informations |
| 10552 | * |
| 10553 | * Returns -1 in case of error and 0 otherwise |
| 10554 | */ |
| 10555 | int |
| 10556 | xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10557 | xmlRelaxNGValidityErrorFunc * err, |
| 10558 | xmlRelaxNGValidityWarningFunc * warn, void **ctx) |
| 10559 | { |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 10560 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10561 | return (-1); |
| 10562 | if (err != NULL) |
| 10563 | *err = ctxt->error; |
| 10564 | if (warn != NULL) |
| 10565 | *warn = ctxt->warning; |
| 10566 | if (ctx != NULL) |
| 10567 | *ctx = ctxt->userData; |
| 10568 | return (0); |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 10569 | } |
| 10570 | |
| 10571 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10572 | * xmlRelaxNGValidateDoc: |
| 10573 | * @ctxt: a Relax-NG validation context |
| 10574 | * @doc: a parsed document tree |
| 10575 | * |
| 10576 | * Validate a document tree in memory. |
| 10577 | * |
| 10578 | * Returns 0 if the document is valid, a positive error code |
| 10579 | * number otherwise and -1 in case of internal or API error. |
| 10580 | */ |
| 10581 | int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10582 | xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) |
| 10583 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10584 | int ret; |
| 10585 | |
| 10586 | if ((ctxt == NULL) || (doc == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10587 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10588 | |
| 10589 | ctxt->doc = doc; |
| 10590 | |
| 10591 | ret = xmlRelaxNGValidateDocument(ctxt, doc); |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 10592 | /* |
| 10593 | * TODO: build error codes |
| 10594 | */ |
| 10595 | if (ret == -1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10596 | return (1); |
| 10597 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10598 | } |
| 10599 | |
| 10600 | #endif /* LIBXML_SCHEMAS_ENABLED */ |