Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1 | /* |
| 2 | * relaxng.c : implementation of the Relax-NG handling and validity checking |
| 3 | * |
| 4 | * See Copyright for the status of this software. |
| 5 | * |
| 6 | * Daniel Veillard <veillard@redhat.com> |
| 7 | */ |
| 8 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 9 | /** |
| 10 | * TODO: |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 11 | * - add support for DTD compatibility spec |
| 12 | * http://www.oasis-open.org/committees/relax-ng/compatibility-20011203.html |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 13 | * - report better mem allocations pbms at runtime and abort immediately. |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 16 | #define IN_LIBXML |
| 17 | #include "libxml.h" |
| 18 | |
| 19 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 20 | |
| 21 | #include <string.h> |
| 22 | #include <stdio.h> |
| 23 | #include <libxml/xmlmemory.h> |
| 24 | #include <libxml/parser.h> |
| 25 | #include <libxml/parserInternals.h> |
| 26 | #include <libxml/hash.h> |
| 27 | #include <libxml/uri.h> |
| 28 | |
| 29 | #include <libxml/relaxng.h> |
| 30 | |
| 31 | #include <libxml/xmlschemastypes.h> |
| 32 | #include <libxml/xmlautomata.h> |
| 33 | #include <libxml/xmlregexp.h> |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 34 | #include <libxml/xmlschemastypes.h> |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | * The Relax-NG namespace |
| 38 | */ |
| 39 | static const xmlChar *xmlRelaxNGNs = (const xmlChar *) |
| 40 | "http://relaxng.org/ns/structure/1.0"; |
| 41 | |
| 42 | #define IS_RELAXNG(node, type) \ |
| 43 | ((node != NULL) && (node->ns != NULL) && \ |
| 44 | (xmlStrEqual(node->name, (const xmlChar *) type)) && \ |
| 45 | (xmlStrEqual(node->ns->href, xmlRelaxNGNs))) |
| 46 | |
| 47 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 48 | /* #define DEBUG 1 */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 49 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 50 | /* #define DEBUG_GRAMMAR 1 */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 51 | |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 52 | /* #define DEBUG_CONTENT 1 */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 53 | |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 54 | /* #define DEBUG_TYPE 1 */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 55 | |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 56 | /* #define DEBUG_VALID 1 */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 57 | |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 58 | /* #define DEBUG_INTERLEAVE 1 */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 59 | |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 60 | /* #define DEBUG_LIST 1 */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 61 | |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 62 | /* #define DEBUG_INCLUDE */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 63 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 64 | /* #define DEBUG_ERROR 1 */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 65 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 66 | /* #define DEBUG_COMPILE 1 */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 67 | |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 68 | /* #define DEBUG_PROGRESSIVE 1 */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 69 | |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 70 | #define MAX_ERROR 5 |
| 71 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 72 | #define TODO \ |
| 73 | xmlGenericError(xmlGenericErrorContext, \ |
| 74 | "Unimplemented block at %s:%d\n", \ |
| 75 | __FILE__, __LINE__); |
| 76 | |
| 77 | typedef struct _xmlRelaxNGSchema xmlRelaxNGSchema; |
| 78 | typedef xmlRelaxNGSchema *xmlRelaxNGSchemaPtr; |
| 79 | |
| 80 | typedef struct _xmlRelaxNGDefine xmlRelaxNGDefine; |
| 81 | typedef xmlRelaxNGDefine *xmlRelaxNGDefinePtr; |
| 82 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 83 | typedef struct _xmlRelaxNGDocument xmlRelaxNGDocument; |
| 84 | typedef xmlRelaxNGDocument *xmlRelaxNGDocumentPtr; |
| 85 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 86 | typedef struct _xmlRelaxNGInclude xmlRelaxNGInclude; |
| 87 | typedef xmlRelaxNGInclude *xmlRelaxNGIncludePtr; |
| 88 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 89 | typedef enum { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 90 | XML_RELAXNG_COMBINE_UNDEFINED = 0, /* undefined */ |
| 91 | XML_RELAXNG_COMBINE_CHOICE, /* choice */ |
| 92 | XML_RELAXNG_COMBINE_INTERLEAVE /* interleave */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 93 | } xmlRelaxNGCombine; |
| 94 | |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 95 | typedef enum { |
| 96 | XML_RELAXNG_CONTENT_ERROR = -1, |
| 97 | XML_RELAXNG_CONTENT_EMPTY = 0, |
| 98 | XML_RELAXNG_CONTENT_SIMPLE, |
| 99 | XML_RELAXNG_CONTENT_COMPLEX |
| 100 | } xmlRelaxNGContentType; |
| 101 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 102 | typedef struct _xmlRelaxNGGrammar xmlRelaxNGGrammar; |
| 103 | typedef xmlRelaxNGGrammar *xmlRelaxNGGrammarPtr; |
| 104 | |
| 105 | struct _xmlRelaxNGGrammar { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 106 | xmlRelaxNGGrammarPtr parent; /* the parent grammar if any */ |
| 107 | xmlRelaxNGGrammarPtr children; /* the children grammar if any */ |
| 108 | xmlRelaxNGGrammarPtr next; /* the next grammar if any */ |
| 109 | xmlRelaxNGDefinePtr start; /* <start> content */ |
| 110 | xmlRelaxNGCombine combine; /* the default combine value */ |
| 111 | xmlRelaxNGDefinePtr startList; /* list of <start> definitions */ |
| 112 | xmlHashTablePtr defs; /* define* */ |
| 113 | xmlHashTablePtr refs; /* references */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 117 | typedef enum { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 118 | XML_RELAXNG_NOOP = -1, /* a no operation from simplification */ |
| 119 | XML_RELAXNG_EMPTY = 0, /* an empty pattern */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 120 | XML_RELAXNG_NOT_ALLOWED, /* not allowed top */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 121 | XML_RELAXNG_EXCEPT, /* except present in nameclass defs */ |
| 122 | XML_RELAXNG_TEXT, /* textual content */ |
| 123 | XML_RELAXNG_ELEMENT, /* an element */ |
| 124 | XML_RELAXNG_DATATYPE, /* extenal data type definition */ |
| 125 | XML_RELAXNG_PARAM, /* extenal data type parameter */ |
| 126 | XML_RELAXNG_VALUE, /* value from an extenal data type definition */ |
| 127 | XML_RELAXNG_LIST, /* a list of patterns */ |
| 128 | XML_RELAXNG_ATTRIBUTE, /* an attrbute following a pattern */ |
| 129 | XML_RELAXNG_DEF, /* a definition */ |
| 130 | XML_RELAXNG_REF, /* reference to a definition */ |
| 131 | XML_RELAXNG_EXTERNALREF, /* reference to an external def */ |
| 132 | XML_RELAXNG_PARENTREF, /* reference to a def in the parent grammar */ |
| 133 | XML_RELAXNG_OPTIONAL, /* optional patterns */ |
| 134 | XML_RELAXNG_ZEROORMORE, /* zero or more non empty patterns */ |
| 135 | XML_RELAXNG_ONEORMORE, /* one or more non empty patterns */ |
| 136 | XML_RELAXNG_CHOICE, /* a choice between non empty patterns */ |
| 137 | XML_RELAXNG_GROUP, /* a pair/group of non empty patterns */ |
| 138 | XML_RELAXNG_INTERLEAVE, /* interleaving choice of non-empty patterns */ |
| 139 | XML_RELAXNG_START /* Used to keep track of starts on grammars */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 140 | } xmlRelaxNGType; |
| 141 | |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 142 | #define IS_NULLABLE (1 << 0) |
| 143 | #define IS_NOT_NULLABLE (1 << 1) |
| 144 | #define IS_INDETERMINIST (1 << 2) |
| 145 | #define IS_MIXED (1 << 3) |
| 146 | #define IS_TRIABLE (1 << 4) |
| 147 | #define IS_PROCESSED (1 << 5) |
| 148 | #define IS_COMPILABLE (1 << 6) |
| 149 | #define IS_NOT_COMPILABLE (1 << 7) |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 150 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 151 | struct _xmlRelaxNGDefine { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 152 | xmlRelaxNGType type; /* the type of definition */ |
| 153 | xmlNodePtr node; /* the node in the source */ |
| 154 | xmlChar *name; /* the element local name if present */ |
| 155 | xmlChar *ns; /* the namespace local name if present */ |
| 156 | xmlChar *value; /* value when available */ |
| 157 | void *data; /* data lib or specific pointer */ |
| 158 | xmlRelaxNGDefinePtr content; /* the expected content */ |
| 159 | xmlRelaxNGDefinePtr parent; /* the parent definition, if any */ |
| 160 | xmlRelaxNGDefinePtr next; /* list within grouping sequences */ |
| 161 | xmlRelaxNGDefinePtr attrs; /* list of attributes for elements */ |
| 162 | xmlRelaxNGDefinePtr nameClass; /* the nameClass definition if any */ |
| 163 | xmlRelaxNGDefinePtr nextHash; /* next define in defs/refs hash tables */ |
| 164 | short depth; /* used for the cycle detection */ |
| 165 | short dflags; /* define related flags */ |
| 166 | xmlRegexpPtr contModel; /* a compiled content model if available */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | /** |
| 170 | * _xmlRelaxNG: |
| 171 | * |
| 172 | * A RelaxNGs definition |
| 173 | */ |
| 174 | struct _xmlRelaxNG { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 175 | void *_private; /* unused by the library for users or bindings */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 176 | xmlRelaxNGGrammarPtr topgrammar; |
| 177 | xmlDocPtr doc; |
| 178 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 179 | int idref; /* requires idref checking */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 180 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 181 | xmlHashTablePtr defs; /* define */ |
| 182 | xmlHashTablePtr refs; /* references */ |
| 183 | xmlRelaxNGDocumentPtr documents; /* all the documents loaded */ |
| 184 | xmlRelaxNGIncludePtr includes; /* all the includes loaded */ |
| 185 | int defNr; /* number of defines used */ |
| 186 | xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 187 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 188 | }; |
| 189 | |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 190 | #define XML_RELAXNG_IN_ATTRIBUTE (1 << 0) |
| 191 | #define XML_RELAXNG_IN_ONEORMORE (1 << 1) |
| 192 | #define XML_RELAXNG_IN_LIST (1 << 2) |
| 193 | #define XML_RELAXNG_IN_DATAEXCEPT (1 << 3) |
| 194 | #define XML_RELAXNG_IN_START (1 << 4) |
| 195 | #define XML_RELAXNG_IN_OOMGROUP (1 << 5) |
| 196 | #define XML_RELAXNG_IN_OOMINTERLEAVE (1 << 6) |
| 197 | #define XML_RELAXNG_IN_EXTERNALREF (1 << 7) |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 198 | #define XML_RELAXNG_IN_ANYEXCEPT (1 << 8) |
| 199 | #define XML_RELAXNG_IN_NSEXCEPT (1 << 9) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 200 | |
| 201 | struct _xmlRelaxNGParserCtxt { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 202 | void *userData; /* user specific data block */ |
| 203 | xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */ |
| 204 | xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */ |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 205 | xmlStructuredErrorFunc serror; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 206 | xmlRelaxNGValidErr err; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 207 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 208 | xmlRelaxNGPtr schema; /* The schema in use */ |
| 209 | xmlRelaxNGGrammarPtr grammar; /* the current grammar */ |
| 210 | xmlRelaxNGGrammarPtr parentgrammar; /* the parent grammar */ |
| 211 | int flags; /* parser flags */ |
| 212 | int nbErrors; /* number of errors at parse time */ |
| 213 | int nbWarnings; /* number of warnings at parse time */ |
| 214 | const xmlChar *define; /* the current define scope */ |
| 215 | xmlRelaxNGDefinePtr def; /* the current define */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 216 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 217 | int nbInterleaves; |
| 218 | xmlHashTablePtr interleaves; /* keep track of all the interleaves */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 219 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 220 | xmlRelaxNGDocumentPtr documents; /* all the documents loaded */ |
| 221 | xmlRelaxNGIncludePtr includes; /* all the includes loaded */ |
| 222 | xmlChar *URL; |
| 223 | xmlDocPtr document; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 224 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 225 | int defNr; /* number of defines used */ |
| 226 | int defMax; /* number of defines aloocated */ |
| 227 | xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */ |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 228 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 229 | const char *buffer; |
| 230 | int size; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 231 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 232 | /* the document stack */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 233 | xmlRelaxNGDocumentPtr doc; /* Current parsed external ref */ |
| 234 | int docNr; /* Depth of the parsing stack */ |
| 235 | int docMax; /* Max depth of the parsing stack */ |
| 236 | xmlRelaxNGDocumentPtr *docTab; /* array of docs */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 237 | |
| 238 | /* the include stack */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 239 | xmlRelaxNGIncludePtr inc; /* Current parsed include */ |
| 240 | int incNr; /* Depth of the include parsing stack */ |
| 241 | int incMax; /* Max depth of the parsing stack */ |
| 242 | xmlRelaxNGIncludePtr *incTab; /* array of incs */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 243 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 244 | int idref; /* requires idref checking */ |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 245 | |
| 246 | /* used to compile content models */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 247 | xmlAutomataPtr am; /* the automata */ |
| 248 | xmlAutomataStatePtr state; /* used to build the automata */ |
Daniel Veillard | 03c2f0a | 2004-01-25 19:54:59 +0000 | [diff] [blame] | 249 | |
| 250 | int crng; /* compact syntax and other flags */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 251 | }; |
| 252 | |
| 253 | #define FLAGS_IGNORABLE 1 |
| 254 | #define FLAGS_NEGATIVE 2 |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 255 | #define FLAGS_MIXED_CONTENT 4 |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 256 | |
| 257 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 258 | * xmlRelaxNGInterleaveGroup: |
| 259 | * |
| 260 | * A RelaxNGs partition set associated to lists of definitions |
| 261 | */ |
| 262 | typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup; |
| 263 | typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr; |
| 264 | struct _xmlRelaxNGInterleaveGroup { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 265 | xmlRelaxNGDefinePtr rule; /* the rule to satisfy */ |
| 266 | xmlRelaxNGDefinePtr *defs; /* the array of element definitions */ |
| 267 | xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 268 | }; |
| 269 | |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 270 | #define IS_DETERMINIST 1 |
| 271 | #define IS_NEEDCHECK 2 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 272 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 273 | /** |
| 274 | * xmlRelaxNGPartitions: |
| 275 | * |
| 276 | * A RelaxNGs partition associated to an interleave group |
| 277 | */ |
| 278 | typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition; |
| 279 | typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr; |
| 280 | struct _xmlRelaxNGPartition { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 281 | int nbgroups; /* number of groups in the partitions */ |
| 282 | xmlHashTablePtr triage; /* hash table used to direct nodes to the |
| 283 | * right group when possible */ |
| 284 | int flags; /* determinist ? */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 285 | xmlRelaxNGInterleaveGroupPtr *groups; |
| 286 | }; |
| 287 | |
| 288 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 289 | * xmlRelaxNGValidState: |
| 290 | * |
| 291 | * A RelaxNGs validation state |
| 292 | */ |
| 293 | #define MAX_ATTR 20 |
| 294 | typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState; |
| 295 | typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr; |
| 296 | struct _xmlRelaxNGValidState { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 297 | xmlNodePtr node; /* the current node */ |
| 298 | xmlNodePtr seq; /* the sequence of children left to validate */ |
| 299 | int nbAttrs; /* the number of attributes */ |
| 300 | int maxAttrs; /* the size of attrs */ |
| 301 | int nbAttrLeft; /* the number of attributes left to validate */ |
| 302 | xmlChar *value; /* the value when operating on string */ |
| 303 | xmlChar *endvalue; /* the end value when operating on string */ |
| 304 | xmlAttrPtr *attrs; /* the array of attributes */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 305 | }; |
| 306 | |
| 307 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 308 | * xmlRelaxNGStates: |
| 309 | * |
| 310 | * A RelaxNGs container for validation state |
| 311 | */ |
| 312 | typedef struct _xmlRelaxNGStates xmlRelaxNGStates; |
| 313 | typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr; |
| 314 | struct _xmlRelaxNGStates { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 315 | int nbState; /* the number of states */ |
| 316 | int maxState; /* the size of the array */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 317 | xmlRelaxNGValidStatePtr *tabState; |
| 318 | }; |
| 319 | |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 320 | #define ERROR_IS_DUP 1 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 321 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 322 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 323 | * xmlRelaxNGValidError: |
| 324 | * |
| 325 | * A RelaxNGs validation error |
| 326 | */ |
| 327 | typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError; |
| 328 | typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr; |
| 329 | struct _xmlRelaxNGValidError { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 330 | xmlRelaxNGValidErr err; /* the error number */ |
| 331 | int flags; /* flags */ |
| 332 | xmlNodePtr node; /* the current node */ |
| 333 | xmlNodePtr seq; /* the current child */ |
| 334 | const xmlChar *arg1; /* first arg */ |
| 335 | const xmlChar *arg2; /* second arg */ |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 336 | }; |
| 337 | |
| 338 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 339 | * xmlRelaxNGValidCtxt: |
| 340 | * |
| 341 | * A RelaxNGs validation context |
| 342 | */ |
| 343 | |
| 344 | struct _xmlRelaxNGValidCtxt { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 345 | void *userData; /* user specific data block */ |
| 346 | xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */ |
| 347 | xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */ |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 348 | xmlStructuredErrorFunc serror; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 349 | int nbErrors; /* number of errors in validation */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 350 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 351 | xmlRelaxNGPtr schema; /* The schema in use */ |
| 352 | xmlDocPtr doc; /* the document being validated */ |
| 353 | int flags; /* validation flags */ |
| 354 | int depth; /* validation depth */ |
| 355 | int idref; /* requires idref checking */ |
| 356 | int errNo; /* the first error found */ |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 357 | |
| 358 | /* |
| 359 | * Errors accumulated in branches may have to be stacked to be |
| 360 | * provided back when it's sure they affect validation. |
| 361 | */ |
| 362 | xmlRelaxNGValidErrorPtr err; /* Last error */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 363 | int errNr; /* Depth of the error stack */ |
| 364 | int errMax; /* Max depth of the error stack */ |
| 365 | xmlRelaxNGValidErrorPtr errTab; /* stack of errors */ |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 366 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 367 | xmlRelaxNGValidStatePtr state; /* the current validation state */ |
| 368 | xmlRelaxNGStatesPtr states; /* the accumulated state list */ |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 369 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 370 | xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */ |
| 371 | int freeStatesNr; |
| 372 | int freeStatesMax; |
| 373 | xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */ |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 374 | |
| 375 | /* |
| 376 | * This is used for "progressive" validation |
| 377 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 378 | xmlRegExecCtxtPtr elem; /* the current element regexp */ |
| 379 | int elemNr; /* the number of element validated */ |
| 380 | int elemMax; /* the max depth of elements */ |
| 381 | xmlRegExecCtxtPtr *elemTab; /* the stack of regexp runtime */ |
| 382 | int pstate; /* progressive state */ |
| 383 | xmlNodePtr pnode; /* the current node */ |
| 384 | xmlRelaxNGDefinePtr pdef; /* the non-streamable definition */ |
| 385 | int perr; /* signal error in content model |
| 386 | * outside the regexp */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 387 | }; |
| 388 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 389 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 390 | * xmlRelaxNGInclude: |
| 391 | * |
| 392 | * Structure associated to a RelaxNGs document element |
| 393 | */ |
| 394 | struct _xmlRelaxNGInclude { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 395 | xmlRelaxNGIncludePtr next; /* keep a chain of includes */ |
| 396 | xmlChar *href; /* the normalized href value */ |
| 397 | xmlDocPtr doc; /* the associated XML document */ |
| 398 | xmlRelaxNGDefinePtr content; /* the definitions */ |
| 399 | xmlRelaxNGPtr schema; /* the schema */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 400 | }; |
| 401 | |
| 402 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 403 | * xmlRelaxNGDocument: |
| 404 | * |
| 405 | * Structure associated to a RelaxNGs document element |
| 406 | */ |
| 407 | struct _xmlRelaxNGDocument { |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 408 | xmlRelaxNGDocumentPtr next; /* keep a chain of documents */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 409 | xmlChar *href; /* the normalized href value */ |
| 410 | xmlDocPtr doc; /* the associated XML document */ |
| 411 | xmlRelaxNGDefinePtr content; /* the definitions */ |
| 412 | xmlRelaxNGPtr schema; /* the schema */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 413 | }; |
| 414 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 415 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 416 | /************************************************************************ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 417 | * * |
| 418 | * Some factorized error routines * |
| 419 | * * |
| 420 | ************************************************************************/ |
| 421 | |
| 422 | /** |
| 423 | * xmlRngPErrMemory: |
| 424 | * @ctxt: an Relax-NG parser context |
| 425 | * @extra: extra informations |
| 426 | * |
| 427 | * Handle a redefinition of attribute error |
| 428 | */ |
| 429 | static void |
| 430 | xmlRngPErrMemory(xmlRelaxNGParserCtxtPtr ctxt, const char *extra) |
| 431 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 432 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 433 | xmlGenericErrorFunc channel = NULL; |
| 434 | void *data = NULL; |
| 435 | |
| 436 | if (ctxt != NULL) { |
| 437 | channel = ctxt->error; |
| 438 | data = ctxt->userData; |
| 439 | ctxt->nbErrors++; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 440 | schannel = ctxt->serror; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 441 | } |
| 442 | if (extra) |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 443 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 444 | NULL, NULL, XML_FROM_RELAXNGP, |
| 445 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra, |
| 446 | NULL, NULL, 0, 0, |
| 447 | "Memory allocation failed : %s\n", extra); |
| 448 | else |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 449 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 450 | NULL, NULL, XML_FROM_RELAXNGP, |
| 451 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL, |
| 452 | NULL, NULL, 0, 0, "Memory allocation failed\n"); |
| 453 | } |
| 454 | |
| 455 | /** |
| 456 | * xmlRngVErrMemory: |
| 457 | * @ctxt: a Relax-NG validation context |
| 458 | * @extra: extra informations |
| 459 | * |
| 460 | * Handle a redefinition of attribute error |
| 461 | */ |
| 462 | static void |
| 463 | xmlRngVErrMemory(xmlRelaxNGValidCtxtPtr ctxt, const char *extra) |
| 464 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 465 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 466 | xmlGenericErrorFunc channel = NULL; |
| 467 | void *data = NULL; |
| 468 | |
| 469 | if (ctxt != NULL) { |
| 470 | channel = ctxt->error; |
| 471 | data = ctxt->userData; |
| 472 | ctxt->nbErrors++; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 473 | schannel = ctxt->serror; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 474 | } |
| 475 | if (extra) |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 476 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 477 | NULL, NULL, XML_FROM_RELAXNGV, |
| 478 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra, |
| 479 | NULL, NULL, 0, 0, |
| 480 | "Memory allocation failed : %s\n", extra); |
| 481 | else |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 482 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 483 | NULL, NULL, XML_FROM_RELAXNGV, |
| 484 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL, |
| 485 | NULL, NULL, 0, 0, "Memory allocation failed\n"); |
| 486 | } |
| 487 | |
| 488 | /** |
| 489 | * xmlRngPErr: |
| 490 | * @ctxt: a Relax-NG parser context |
| 491 | * @node: the node raising the error |
| 492 | * @error: the error code |
| 493 | * @msg: message |
| 494 | * @str1: extra info |
| 495 | * @str2: extra info |
| 496 | * |
| 497 | * Handle a Relax NG Parsing error |
| 498 | */ |
| 499 | static void |
| 500 | xmlRngPErr(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, int error, |
| 501 | const char *msg, const xmlChar * str1, const xmlChar * str2) |
| 502 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 503 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 504 | xmlGenericErrorFunc channel = NULL; |
| 505 | void *data = NULL; |
| 506 | |
| 507 | if (ctxt != NULL) { |
| 508 | channel = ctxt->error; |
| 509 | data = ctxt->userData; |
| 510 | ctxt->nbErrors++; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 511 | schannel = ctxt->serror; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 512 | } |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 513 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 514 | NULL, node, XML_FROM_RELAXNGP, |
| 515 | error, XML_ERR_ERROR, NULL, 0, |
| 516 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
| 517 | msg, str1, str2); |
| 518 | } |
| 519 | |
| 520 | /** |
| 521 | * xmlRngVErr: |
| 522 | * @ctxt: a Relax-NG validation context |
| 523 | * @node: the node raising the error |
| 524 | * @error: the error code |
| 525 | * @msg: message |
| 526 | * @str1: extra info |
| 527 | * @str2: extra info |
| 528 | * |
| 529 | * Handle a Relax NG Validation error |
| 530 | */ |
| 531 | static void |
| 532 | xmlRngVErr(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node, int error, |
| 533 | const char *msg, const xmlChar * str1, const xmlChar * str2) |
| 534 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 535 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 536 | xmlGenericErrorFunc channel = NULL; |
| 537 | void *data = NULL; |
| 538 | |
| 539 | if (ctxt != NULL) { |
| 540 | channel = ctxt->error; |
| 541 | data = ctxt->userData; |
| 542 | ctxt->nbErrors++; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 543 | schannel = ctxt->serror; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 544 | } |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 545 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 546 | NULL, node, XML_FROM_RELAXNGV, |
| 547 | error, XML_ERR_ERROR, NULL, 0, |
| 548 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
| 549 | msg, str1, str2); |
| 550 | } |
| 551 | |
| 552 | /************************************************************************ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 553 | * * |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 554 | * Preliminary type checking interfaces * |
| 555 | * * |
| 556 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 557 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 558 | /** |
| 559 | * xmlRelaxNGTypeHave: |
| 560 | * @data: data needed for the library |
| 561 | * @type: the type name |
| 562 | * @value: the value to check |
| 563 | * |
| 564 | * Function provided by a type library to check if a type is exported |
| 565 | * |
| 566 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 567 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 568 | typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar * type); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 569 | |
| 570 | /** |
| 571 | * xmlRelaxNGTypeCheck: |
| 572 | * @data: data needed for the library |
| 573 | * @type: the type name |
| 574 | * @value: the value to check |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 575 | * @result: place to store the result if needed |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 576 | * |
| 577 | * Function provided by a type library to check if a value match a type |
| 578 | * |
| 579 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 580 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 581 | typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar * type, |
| 582 | const xmlChar * value, void **result, |
| 583 | xmlNodePtr node); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 584 | |
| 585 | /** |
| 586 | * xmlRelaxNGFacetCheck: |
| 587 | * @data: data needed for the library |
| 588 | * @type: the type name |
| 589 | * @facet: the facet name |
| 590 | * @val: the facet value |
| 591 | * @strval: the string value |
| 592 | * @value: the value to check |
| 593 | * |
| 594 | * Function provided by a type library to check a value facet |
| 595 | * |
| 596 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 597 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 598 | typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar * type, |
| 599 | const xmlChar * facet, |
| 600 | const xmlChar * val, |
| 601 | const xmlChar * strval, void *value); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 602 | |
| 603 | /** |
| 604 | * xmlRelaxNGTypeFree: |
| 605 | * @data: data needed for the library |
| 606 | * @result: the value to free |
| 607 | * |
| 608 | * Function provided by a type library to free a returned result |
| 609 | */ |
| 610 | typedef void (*xmlRelaxNGTypeFree) (void *data, void *result); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 611 | |
| 612 | /** |
| 613 | * xmlRelaxNGTypeCompare: |
| 614 | * @data: data needed for the library |
| 615 | * @type: the type name |
| 616 | * @value1: the first value |
| 617 | * @value2: the second value |
| 618 | * |
| 619 | * Function provided by a type library to compare two values accordingly |
| 620 | * to a type. |
| 621 | * |
| 622 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 623 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 624 | typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar * type, |
| 625 | const xmlChar * value1, |
| 626 | xmlNodePtr ctxt1, |
| 627 | void *comp1, |
| 628 | const xmlChar * value2, |
| 629 | xmlNodePtr ctxt2); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 630 | typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary; |
| 631 | typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr; |
| 632 | struct _xmlRelaxNGTypeLibrary { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 633 | const xmlChar *namespace; /* the datatypeLibrary value */ |
| 634 | void *data; /* data needed for the library */ |
| 635 | xmlRelaxNGTypeHave have; /* the export function */ |
| 636 | xmlRelaxNGTypeCheck check; /* the checking function */ |
| 637 | xmlRelaxNGTypeCompare comp; /* the compare function */ |
| 638 | xmlRelaxNGFacetCheck facet; /* the facet check function */ |
| 639 | xmlRelaxNGTypeFree freef; /* the freeing function */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 640 | }; |
| 641 | |
| 642 | /************************************************************************ |
| 643 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 644 | * Allocation functions * |
| 645 | * * |
| 646 | ************************************************************************/ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 647 | static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar); |
| 648 | static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 649 | static void xmlRelaxNGNormExtSpace(xmlChar * value); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 650 | static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 651 | static int xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt |
| 652 | ATTRIBUTE_UNUSED, |
| 653 | xmlRelaxNGValidStatePtr state1, |
| 654 | xmlRelaxNGValidStatePtr state2); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 655 | static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 656 | xmlRelaxNGValidStatePtr state); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 657 | |
| 658 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 659 | * xmlRelaxNGFreeDocument: |
| 660 | * @docu: a document structure |
| 661 | * |
| 662 | * Deallocate a RelaxNG document structure. |
| 663 | */ |
| 664 | static void |
| 665 | xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu) |
| 666 | { |
| 667 | if (docu == NULL) |
| 668 | return; |
| 669 | |
| 670 | if (docu->href != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 671 | xmlFree(docu->href); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 672 | if (docu->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 673 | xmlFreeDoc(docu->doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 674 | if (docu->schema != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 675 | xmlRelaxNGFreeInnerSchema(docu->schema); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 676 | xmlFree(docu); |
| 677 | } |
| 678 | |
| 679 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 680 | * xmlRelaxNGFreeDocumentList: |
| 681 | * @docu: a list of document structure |
| 682 | * |
| 683 | * Deallocate a RelaxNG document structures. |
| 684 | */ |
| 685 | static void |
| 686 | xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu) |
| 687 | { |
| 688 | xmlRelaxNGDocumentPtr next; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 689 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 690 | while (docu != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 691 | next = docu->next; |
| 692 | xmlRelaxNGFreeDocument(docu); |
| 693 | docu = next; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 694 | } |
| 695 | } |
| 696 | |
| 697 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 698 | * xmlRelaxNGFreeInclude: |
| 699 | * @incl: a include structure |
| 700 | * |
| 701 | * Deallocate a RelaxNG include structure. |
| 702 | */ |
| 703 | static void |
| 704 | xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl) |
| 705 | { |
| 706 | if (incl == NULL) |
| 707 | return; |
| 708 | |
| 709 | if (incl->href != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 710 | xmlFree(incl->href); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 711 | if (incl->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 712 | xmlFreeDoc(incl->doc); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 713 | if (incl->schema != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 714 | xmlRelaxNGFree(incl->schema); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 715 | xmlFree(incl); |
| 716 | } |
| 717 | |
| 718 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 719 | * xmlRelaxNGFreeIncludeList: |
| 720 | * @incl: a include structure list |
| 721 | * |
| 722 | * Deallocate a RelaxNG include structure. |
| 723 | */ |
| 724 | static void |
| 725 | xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl) |
| 726 | { |
| 727 | xmlRelaxNGIncludePtr next; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 728 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 729 | while (incl != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 730 | next = incl->next; |
| 731 | xmlRelaxNGFreeInclude(incl); |
| 732 | incl = next; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 733 | } |
| 734 | } |
| 735 | |
| 736 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 737 | * xmlRelaxNGNewRelaxNG: |
| 738 | * @ctxt: a Relax-NG validation context (optional) |
| 739 | * |
| 740 | * Allocate a new RelaxNG structure. |
| 741 | * |
| 742 | * Returns the newly allocated structure or NULL in case or error |
| 743 | */ |
| 744 | static xmlRelaxNGPtr |
| 745 | xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt) |
| 746 | { |
| 747 | xmlRelaxNGPtr ret; |
| 748 | |
| 749 | ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG)); |
| 750 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 751 | xmlRngPErrMemory(ctxt, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 752 | return (NULL); |
| 753 | } |
| 754 | memset(ret, 0, sizeof(xmlRelaxNG)); |
| 755 | |
| 756 | return (ret); |
| 757 | } |
| 758 | |
| 759 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 760 | * xmlRelaxNGFreeInnerSchema: |
| 761 | * @schema: a schema structure |
| 762 | * |
| 763 | * Deallocate a RelaxNG schema structure. |
| 764 | */ |
| 765 | static void |
| 766 | xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema) |
| 767 | { |
| 768 | if (schema == NULL) |
| 769 | return; |
| 770 | |
| 771 | if (schema->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 772 | xmlFreeDoc(schema->doc); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 773 | if (schema->defTab != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 774 | int i; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 775 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 776 | for (i = 0; i < schema->defNr; i++) |
| 777 | xmlRelaxNGFreeDefine(schema->defTab[i]); |
| 778 | xmlFree(schema->defTab); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | xmlFree(schema); |
| 782 | } |
| 783 | |
| 784 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 785 | * xmlRelaxNGFree: |
| 786 | * @schema: a schema structure |
| 787 | * |
| 788 | * Deallocate a RelaxNG structure. |
| 789 | */ |
| 790 | void |
| 791 | xmlRelaxNGFree(xmlRelaxNGPtr schema) |
| 792 | { |
| 793 | if (schema == NULL) |
| 794 | return; |
| 795 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 796 | if (schema->topgrammar != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 797 | xmlRelaxNGFreeGrammar(schema->topgrammar); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 798 | if (schema->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 799 | xmlFreeDoc(schema->doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 800 | if (schema->documents != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 801 | xmlRelaxNGFreeDocumentList(schema->documents); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 802 | if (schema->includes != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 803 | xmlRelaxNGFreeIncludeList(schema->includes); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 804 | if (schema->defTab != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 805 | int i; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 806 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 807 | for (i = 0; i < schema->defNr; i++) |
| 808 | xmlRelaxNGFreeDefine(schema->defTab[i]); |
| 809 | xmlFree(schema->defTab); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 810 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 811 | |
| 812 | xmlFree(schema); |
| 813 | } |
| 814 | |
| 815 | /** |
| 816 | * xmlRelaxNGNewGrammar: |
| 817 | * @ctxt: a Relax-NG validation context (optional) |
| 818 | * |
| 819 | * Allocate a new RelaxNG grammar. |
| 820 | * |
| 821 | * Returns the newly allocated structure or NULL in case or error |
| 822 | */ |
| 823 | static xmlRelaxNGGrammarPtr |
| 824 | xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt) |
| 825 | { |
| 826 | xmlRelaxNGGrammarPtr ret; |
| 827 | |
| 828 | ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar)); |
| 829 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 830 | xmlRngPErrMemory(ctxt, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 831 | return (NULL); |
| 832 | } |
| 833 | memset(ret, 0, sizeof(xmlRelaxNGGrammar)); |
| 834 | |
| 835 | return (ret); |
| 836 | } |
| 837 | |
| 838 | /** |
| 839 | * xmlRelaxNGFreeGrammar: |
| 840 | * @grammar: a grammar structure |
| 841 | * |
| 842 | * Deallocate a RelaxNG grammar structure. |
| 843 | */ |
| 844 | static void |
| 845 | xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar) |
| 846 | { |
| 847 | if (grammar == NULL) |
| 848 | return; |
| 849 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 850 | if (grammar->children != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 851 | xmlRelaxNGFreeGrammar(grammar->children); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 852 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 853 | if (grammar->next != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 854 | xmlRelaxNGFreeGrammar(grammar->next); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 855 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 856 | if (grammar->refs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 857 | xmlHashFree(grammar->refs, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 858 | } |
| 859 | if (grammar->defs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 860 | xmlHashFree(grammar->defs, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | xmlFree(grammar); |
| 864 | } |
| 865 | |
| 866 | /** |
| 867 | * xmlRelaxNGNewDefine: |
| 868 | * @ctxt: a Relax-NG validation context |
| 869 | * @node: the node in the input document. |
| 870 | * |
| 871 | * Allocate a new RelaxNG define. |
| 872 | * |
| 873 | * Returns the newly allocated structure or NULL in case or error |
| 874 | */ |
| 875 | static xmlRelaxNGDefinePtr |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 876 | xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 877 | { |
| 878 | xmlRelaxNGDefinePtr ret; |
| 879 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 880 | if (ctxt->defMax == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 881 | ctxt->defMax = 16; |
| 882 | ctxt->defNr = 0; |
| 883 | ctxt->defTab = (xmlRelaxNGDefinePtr *) |
| 884 | xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr)); |
| 885 | if (ctxt->defTab == NULL) { |
| 886 | xmlRngPErrMemory(ctxt, "allocating define\n"); |
| 887 | return (NULL); |
| 888 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 889 | } else if (ctxt->defMax <= ctxt->defNr) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 890 | xmlRelaxNGDefinePtr *tmp; |
| 891 | |
| 892 | ctxt->defMax *= 2; |
| 893 | tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab, |
| 894 | ctxt->defMax * |
| 895 | sizeof |
| 896 | (xmlRelaxNGDefinePtr)); |
| 897 | if (tmp == NULL) { |
| 898 | xmlRngPErrMemory(ctxt, "allocating define\n"); |
| 899 | return (NULL); |
| 900 | } |
| 901 | ctxt->defTab = tmp; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 902 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 903 | ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine)); |
| 904 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 905 | xmlRngPErrMemory(ctxt, "allocating define\n"); |
| 906 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 907 | } |
| 908 | memset(ret, 0, sizeof(xmlRelaxNGDefine)); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 909 | ctxt->defTab[ctxt->defNr++] = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 910 | ret->node = node; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 911 | ret->depth = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 912 | return (ret); |
| 913 | } |
| 914 | |
| 915 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 916 | * xmlRelaxNGFreePartition: |
| 917 | * @partitions: a partition set structure |
| 918 | * |
| 919 | * Deallocate RelaxNG partition set structures. |
| 920 | */ |
| 921 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 922 | xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions) |
| 923 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 924 | xmlRelaxNGInterleaveGroupPtr group; |
| 925 | int j; |
| 926 | |
| 927 | if (partitions != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 928 | if (partitions->groups != NULL) { |
| 929 | for (j = 0; j < partitions->nbgroups; j++) { |
| 930 | group = partitions->groups[j]; |
| 931 | if (group != NULL) { |
| 932 | if (group->defs != NULL) |
| 933 | xmlFree(group->defs); |
| 934 | if (group->attrs != NULL) |
| 935 | xmlFree(group->attrs); |
| 936 | xmlFree(group); |
| 937 | } |
| 938 | } |
| 939 | xmlFree(partitions->groups); |
| 940 | } |
| 941 | if (partitions->triage != NULL) { |
| 942 | xmlHashFree(partitions->triage, NULL); |
| 943 | } |
| 944 | xmlFree(partitions); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 945 | } |
| 946 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 947 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 948 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 949 | * xmlRelaxNGFreeDefine: |
| 950 | * @define: a define structure |
| 951 | * |
| 952 | * Deallocate a RelaxNG define structure. |
| 953 | */ |
| 954 | static void |
| 955 | xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define) |
| 956 | { |
| 957 | if (define == NULL) |
| 958 | return; |
| 959 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 960 | if ((define->type == XML_RELAXNG_VALUE) && (define->attrs != NULL)) { |
| 961 | xmlRelaxNGTypeLibraryPtr lib; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 962 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 963 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
| 964 | if ((lib != NULL) && (lib->freef != NULL)) |
| 965 | lib->freef(lib->data, (void *) define->attrs); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 966 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 967 | if ((define->data != NULL) && (define->type == XML_RELAXNG_INTERLEAVE)) |
| 968 | xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data); |
| 969 | if ((define->data != NULL) && (define->type == XML_RELAXNG_CHOICE)) |
| 970 | xmlHashFree((xmlHashTablePtr) define->data, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 971 | if (define->name != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 972 | xmlFree(define->name); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 973 | if (define->ns != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 974 | xmlFree(define->ns); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 975 | if (define->value != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 976 | xmlFree(define->value); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 977 | if (define->contModel != NULL) |
| 978 | xmlRegFreeRegexp(define->contModel); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 979 | xmlFree(define); |
| 980 | } |
| 981 | |
| 982 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 983 | * xmlRelaxNGNewStates: |
| 984 | * @ctxt: a Relax-NG validation context |
| 985 | * @size: the default size for the container |
| 986 | * |
| 987 | * Allocate a new RelaxNG validation state container |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 988 | * |
| 989 | * Returns the newly allocated structure or NULL in case or error |
| 990 | */ |
| 991 | static xmlRelaxNGStatesPtr |
| 992 | xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size) |
| 993 | { |
| 994 | xmlRelaxNGStatesPtr ret; |
| 995 | |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 996 | if ((ctxt != NULL) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 997 | (ctxt->freeState != NULL) && (ctxt->freeStatesNr > 0)) { |
| 998 | ctxt->freeStatesNr--; |
| 999 | ret = ctxt->freeStates[ctxt->freeStatesNr]; |
| 1000 | ret->nbState = 0; |
| 1001 | return (ret); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1002 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1003 | if (size < 16) |
| 1004 | size = 16; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1005 | |
| 1006 | ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) + |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1007 | (size - |
| 1008 | 1) * |
| 1009 | sizeof(xmlRelaxNGValidStatePtr)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1010 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1011 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1012 | return (NULL); |
| 1013 | } |
| 1014 | ret->nbState = 0; |
| 1015 | ret->maxState = size; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1016 | ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc((size) * |
| 1017 | sizeof |
| 1018 | (xmlRelaxNGValidStatePtr)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1019 | if (ret->tabState == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1020 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1021 | xmlFree(ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1022 | return (NULL); |
| 1023 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1024 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | /** |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1028 | * xmlRelaxNGAddStateUniq: |
| 1029 | * @ctxt: a Relax-NG validation context |
| 1030 | * @states: the states container |
| 1031 | * @state: the validation state |
| 1032 | * |
| 1033 | * Add a RelaxNG validation state to the container without checking |
| 1034 | * for unicity. |
| 1035 | * |
| 1036 | * Return 1 in case of success and 0 if this is a duplicate and -1 on error |
| 1037 | */ |
| 1038 | static int |
| 1039 | xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1040 | xmlRelaxNGStatesPtr states, |
| 1041 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1042 | { |
| 1043 | if (state == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1044 | return (-1); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1045 | } |
| 1046 | if (states->nbState >= states->maxState) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1047 | xmlRelaxNGValidStatePtr *tmp; |
| 1048 | int size; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1049 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1050 | size = states->maxState * 2; |
| 1051 | tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState, |
| 1052 | (size) * |
| 1053 | sizeof |
| 1054 | (xmlRelaxNGValidStatePtr)); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1055 | if (tmp == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1056 | xmlRngVErrMemory(ctxt, "adding states\n"); |
| 1057 | return (-1); |
| 1058 | } |
| 1059 | states->tabState = tmp; |
| 1060 | states->maxState = size; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1061 | } |
| 1062 | states->tabState[states->nbState++] = state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1063 | return (1); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1067 | * xmlRelaxNGAddState: |
| 1068 | * @ctxt: a Relax-NG validation context |
| 1069 | * @states: the states container |
| 1070 | * @state: the validation state |
| 1071 | * |
| 1072 | * Add a RelaxNG validation state to the container |
| 1073 | * |
| 1074 | * Return 1 in case of success and 0 if this is a duplicate and -1 on error |
| 1075 | */ |
| 1076 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1077 | xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt, |
| 1078 | xmlRelaxNGStatesPtr states, |
| 1079 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1080 | { |
| 1081 | int i; |
| 1082 | |
| 1083 | if (state == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1084 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1085 | } |
| 1086 | if (states->nbState >= states->maxState) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1087 | xmlRelaxNGValidStatePtr *tmp; |
| 1088 | int size; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1089 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1090 | size = states->maxState * 2; |
| 1091 | tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState, |
| 1092 | (size) * |
| 1093 | sizeof |
| 1094 | (xmlRelaxNGValidStatePtr)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1095 | if (tmp == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1096 | xmlRngVErrMemory(ctxt, "adding states\n"); |
| 1097 | return (-1); |
| 1098 | } |
| 1099 | states->tabState = tmp; |
| 1100 | states->maxState = size; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1101 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1102 | for (i = 0; i < states->nbState; i++) { |
| 1103 | if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) { |
| 1104 | xmlRelaxNGFreeValidState(ctxt, state); |
| 1105 | return (0); |
| 1106 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1107 | } |
| 1108 | states->tabState[states->nbState++] = state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1109 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | /** |
| 1113 | * xmlRelaxNGFreeStates: |
| 1114 | * @ctxt: a Relax-NG validation context |
| 1115 | * @states: teh container |
| 1116 | * |
| 1117 | * Free a RelaxNG validation state container |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1118 | */ |
| 1119 | static void |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1120 | xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1121 | xmlRelaxNGStatesPtr states) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1122 | { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1123 | if (states == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1124 | return; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1125 | if ((ctxt != NULL) && (ctxt->freeStates == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1126 | ctxt->freeStatesMax = 40; |
| 1127 | ctxt->freeStatesNr = 0; |
| 1128 | ctxt->freeStates = (xmlRelaxNGStatesPtr *) |
| 1129 | xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr)); |
| 1130 | if (ctxt->freeStates == NULL) { |
| 1131 | xmlRngVErrMemory(ctxt, "storing states\n"); |
| 1132 | } |
| 1133 | } else if ((ctxt != NULL) |
| 1134 | && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) { |
| 1135 | xmlRelaxNGStatesPtr *tmp; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1136 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1137 | tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates, |
| 1138 | 2 * ctxt->freeStatesMax * |
| 1139 | sizeof |
| 1140 | (xmlRelaxNGStatesPtr)); |
| 1141 | if (tmp == NULL) { |
| 1142 | xmlRngVErrMemory(ctxt, "storing states\n"); |
| 1143 | xmlFree(states->tabState); |
| 1144 | xmlFree(states); |
| 1145 | return; |
| 1146 | } |
| 1147 | ctxt->freeStates = tmp; |
| 1148 | ctxt->freeStatesMax *= 2; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1149 | } |
| 1150 | if ((ctxt == NULL) || (ctxt->freeState == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1151 | xmlFree(states->tabState); |
| 1152 | xmlFree(states); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1153 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1154 | ctxt->freeStates[ctxt->freeStatesNr++] = states; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1155 | } |
| 1156 | } |
| 1157 | |
| 1158 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1159 | * xmlRelaxNGNewValidState: |
| 1160 | * @ctxt: a Relax-NG validation context |
| 1161 | * @node: the current node or NULL for the document |
| 1162 | * |
| 1163 | * Allocate a new RelaxNG validation state |
| 1164 | * |
| 1165 | * Returns the newly allocated structure or NULL in case or error |
| 1166 | */ |
| 1167 | static xmlRelaxNGValidStatePtr |
| 1168 | xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node) |
| 1169 | { |
| 1170 | xmlRelaxNGValidStatePtr ret; |
| 1171 | xmlAttrPtr attr; |
| 1172 | xmlAttrPtr attrs[MAX_ATTR]; |
| 1173 | int nbAttrs = 0; |
| 1174 | xmlNodePtr root = NULL; |
| 1175 | |
| 1176 | if (node == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1177 | root = xmlDocGetRootElement(ctxt->doc); |
| 1178 | if (root == NULL) |
| 1179 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1180 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1181 | attr = node->properties; |
| 1182 | while (attr != NULL) { |
| 1183 | if (nbAttrs < MAX_ATTR) |
| 1184 | attrs[nbAttrs++] = attr; |
| 1185 | else |
| 1186 | nbAttrs++; |
| 1187 | attr = attr->next; |
| 1188 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1189 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1190 | if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) { |
| 1191 | ctxt->freeState->nbState--; |
| 1192 | ret = ctxt->freeState->tabState[ctxt->freeState->nbState]; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1193 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1194 | ret = |
| 1195 | (xmlRelaxNGValidStatePtr) |
| 1196 | xmlMalloc(sizeof(xmlRelaxNGValidState)); |
| 1197 | if (ret == NULL) { |
| 1198 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1199 | return (NULL); |
| 1200 | } |
| 1201 | memset(ret, 0, sizeof(xmlRelaxNGValidState)); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1202 | } |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 1203 | ret->value = NULL; |
| 1204 | ret->endvalue = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1205 | if (node == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1206 | ret->node = (xmlNodePtr) ctxt->doc; |
| 1207 | ret->seq = root; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1208 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1209 | ret->node = node; |
| 1210 | ret->seq = node->children; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1211 | } |
| 1212 | ret->nbAttrs = 0; |
| 1213 | if (nbAttrs > 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1214 | if (ret->attrs == NULL) { |
| 1215 | if (nbAttrs < 4) |
| 1216 | ret->maxAttrs = 4; |
| 1217 | else |
| 1218 | ret->maxAttrs = nbAttrs; |
| 1219 | ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs * |
| 1220 | sizeof(xmlAttrPtr)); |
| 1221 | if (ret->attrs == NULL) { |
| 1222 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1223 | return (ret); |
| 1224 | } |
| 1225 | } else if (ret->maxAttrs < nbAttrs) { |
| 1226 | xmlAttrPtr *tmp; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1227 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1228 | tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs * |
| 1229 | sizeof(xmlAttrPtr)); |
| 1230 | if (tmp == NULL) { |
| 1231 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1232 | return (ret); |
| 1233 | } |
| 1234 | ret->attrs = tmp; |
| 1235 | ret->maxAttrs = nbAttrs; |
| 1236 | } |
| 1237 | ret->nbAttrs = nbAttrs; |
| 1238 | if (nbAttrs < MAX_ATTR) { |
| 1239 | memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs); |
| 1240 | } else { |
| 1241 | attr = node->properties; |
| 1242 | nbAttrs = 0; |
| 1243 | while (attr != NULL) { |
| 1244 | ret->attrs[nbAttrs++] = attr; |
| 1245 | attr = attr->next; |
| 1246 | } |
| 1247 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1248 | } |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 1249 | ret->nbAttrLeft = ret->nbAttrs; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1250 | return (ret); |
| 1251 | } |
| 1252 | |
| 1253 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1254 | * xmlRelaxNGCopyValidState: |
| 1255 | * @ctxt: a Relax-NG validation context |
| 1256 | * @state: a validation state |
| 1257 | * |
| 1258 | * Copy the validation state |
| 1259 | * |
| 1260 | * Returns the newly allocated structure or NULL in case or error |
| 1261 | */ |
| 1262 | static xmlRelaxNGValidStatePtr |
| 1263 | xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1264 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1265 | { |
| 1266 | xmlRelaxNGValidStatePtr ret; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1267 | unsigned int maxAttrs; |
| 1268 | xmlAttrPtr *attrs; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1269 | |
| 1270 | if (state == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1271 | return (NULL); |
| 1272 | if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) { |
| 1273 | ctxt->freeState->nbState--; |
| 1274 | ret = ctxt->freeState->tabState[ctxt->freeState->nbState]; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1275 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1276 | ret = |
| 1277 | (xmlRelaxNGValidStatePtr) |
| 1278 | xmlMalloc(sizeof(xmlRelaxNGValidState)); |
| 1279 | if (ret == NULL) { |
| 1280 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1281 | return (NULL); |
| 1282 | } |
| 1283 | memset(ret, 0, sizeof(xmlRelaxNGValidState)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1284 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1285 | attrs = ret->attrs; |
| 1286 | maxAttrs = ret->maxAttrs; |
| 1287 | memcpy(ret, state, sizeof(xmlRelaxNGValidState)); |
| 1288 | ret->attrs = attrs; |
| 1289 | ret->maxAttrs = maxAttrs; |
| 1290 | if (state->nbAttrs > 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1291 | if (ret->attrs == NULL) { |
| 1292 | ret->maxAttrs = state->maxAttrs; |
| 1293 | ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs * |
| 1294 | sizeof(xmlAttrPtr)); |
| 1295 | if (ret->attrs == NULL) { |
| 1296 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1297 | ret->nbAttrs = 0; |
| 1298 | return (ret); |
| 1299 | } |
| 1300 | } else if (ret->maxAttrs < state->nbAttrs) { |
| 1301 | xmlAttrPtr *tmp; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1302 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1303 | tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs * |
| 1304 | sizeof(xmlAttrPtr)); |
| 1305 | if (tmp == NULL) { |
| 1306 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1307 | ret->nbAttrs = 0; |
| 1308 | return (ret); |
| 1309 | } |
| 1310 | ret->maxAttrs = state->maxAttrs; |
| 1311 | ret->attrs = tmp; |
| 1312 | } |
| 1313 | memcpy(ret->attrs, state->attrs, |
| 1314 | state->nbAttrs * sizeof(xmlAttrPtr)); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1315 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1316 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | /** |
| 1320 | * xmlRelaxNGEqualValidState: |
| 1321 | * @ctxt: a Relax-NG validation context |
| 1322 | * @state1: a validation state |
| 1323 | * @state2: a validation state |
| 1324 | * |
| 1325 | * Compare the validation states for equality |
| 1326 | * |
| 1327 | * Returns 1 if equald, 0 otherwise |
| 1328 | */ |
| 1329 | static int |
| 1330 | xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1331 | xmlRelaxNGValidStatePtr state1, |
| 1332 | xmlRelaxNGValidStatePtr state2) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1333 | { |
| 1334 | int i; |
| 1335 | |
| 1336 | if ((state1 == NULL) || (state2 == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1337 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1338 | if (state1 == state2) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1339 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1340 | if (state1->node != state2->node) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1341 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1342 | if (state1->seq != state2->seq) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1343 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1344 | if (state1->nbAttrLeft != state2->nbAttrLeft) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1345 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1346 | if (state1->nbAttrs != state2->nbAttrs) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1347 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1348 | if (state1->endvalue != state2->endvalue) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1349 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1350 | if ((state1->value != state2->value) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1351 | (!xmlStrEqual(state1->value, state2->value))) |
| 1352 | return (0); |
| 1353 | for (i = 0; i < state1->nbAttrs; i++) { |
| 1354 | if (state1->attrs[i] != state2->attrs[i]) |
| 1355 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1356 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1357 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
| 1360 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1361 | * xmlRelaxNGFreeValidState: |
| 1362 | * @state: a validation state structure |
| 1363 | * |
| 1364 | * Deallocate a RelaxNG validation state structure. |
| 1365 | */ |
| 1366 | static void |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1367 | xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1368 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1369 | { |
| 1370 | if (state == NULL) |
| 1371 | return; |
| 1372 | |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1373 | if ((ctxt != NULL) && (ctxt->freeState == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1374 | ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1375 | } |
| 1376 | if ((ctxt == NULL) || (ctxt->freeState == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1377 | if (state->attrs != NULL) |
| 1378 | xmlFree(state->attrs); |
| 1379 | xmlFree(state); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1380 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1381 | xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1382 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | /************************************************************************ |
| 1386 | * * |
Daniel Veillard | 03c2f0a | 2004-01-25 19:54:59 +0000 | [diff] [blame] | 1387 | * Semi internal functions * |
| 1388 | * * |
| 1389 | ************************************************************************/ |
| 1390 | |
| 1391 | /** |
| 1392 | * xmlRelaxParserSetFlag: |
| 1393 | * @ctxt: a RelaxNG parser context |
| 1394 | * @flags: a set of flags values |
| 1395 | * |
| 1396 | * Semi private function used to pass informations to a parser context |
| 1397 | * which are a combination of xmlRelaxNGParserFlag . |
| 1398 | * |
| 1399 | * Returns 0 if success and -1 in case of error |
| 1400 | */ |
| 1401 | int |
| 1402 | xmlRelaxParserSetFlag(xmlRelaxNGParserCtxtPtr ctxt, int flags) |
| 1403 | { |
| 1404 | if (ctxt == NULL) return(-1); |
| 1405 | if (flags & XML_RELAXNGP_FREE_DOC) { |
| 1406 | ctxt->crng |= XML_RELAXNGP_FREE_DOC; |
| 1407 | flags -= XML_RELAXNGP_FREE_DOC; |
| 1408 | } |
| 1409 | if (flags & XML_RELAXNGP_CRNG) { |
| 1410 | ctxt->crng |= XML_RELAXNGP_CRNG; |
| 1411 | flags -= XML_RELAXNGP_CRNG; |
| 1412 | } |
| 1413 | if (flags != 0) return(-1); |
| 1414 | return(0); |
| 1415 | } |
| 1416 | |
| 1417 | /************************************************************************ |
| 1418 | * * |
| 1419 | * Document functions * |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1420 | * * |
| 1421 | ************************************************************************/ |
| 1422 | static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1423 | xmlDocPtr doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1424 | |
| 1425 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1426 | * xmlRelaxNGIncludePush: |
| 1427 | * @ctxt: the parser context |
| 1428 | * @value: the element doc |
| 1429 | * |
| 1430 | * Pushes a new include on top of the include stack |
| 1431 | * |
| 1432 | * Returns 0 in case of error, the index in the stack otherwise |
| 1433 | */ |
| 1434 | static int |
| 1435 | xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1436 | xmlRelaxNGIncludePtr value) |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1437 | { |
| 1438 | if (ctxt->incTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1439 | ctxt->incMax = 4; |
| 1440 | ctxt->incNr = 0; |
| 1441 | ctxt->incTab = |
| 1442 | (xmlRelaxNGIncludePtr *) xmlMalloc(ctxt->incMax * |
| 1443 | sizeof(ctxt->incTab[0])); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1444 | if (ctxt->incTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1445 | xmlRngPErrMemory(ctxt, "allocating include\n"); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1446 | return (0); |
| 1447 | } |
| 1448 | } |
| 1449 | if (ctxt->incNr >= ctxt->incMax) { |
| 1450 | ctxt->incMax *= 2; |
| 1451 | ctxt->incTab = |
| 1452 | (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1453 | ctxt->incMax * |
| 1454 | sizeof(ctxt->incTab[0])); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1455 | if (ctxt->incTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1456 | xmlRngPErrMemory(ctxt, "allocating include\n"); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1457 | return (0); |
| 1458 | } |
| 1459 | } |
| 1460 | ctxt->incTab[ctxt->incNr] = value; |
| 1461 | ctxt->inc = value; |
| 1462 | return (ctxt->incNr++); |
| 1463 | } |
| 1464 | |
| 1465 | /** |
| 1466 | * xmlRelaxNGIncludePop: |
| 1467 | * @ctxt: the parser context |
| 1468 | * |
| 1469 | * Pops the top include from the include stack |
| 1470 | * |
| 1471 | * Returns the include just removed |
| 1472 | */ |
| 1473 | static xmlRelaxNGIncludePtr |
| 1474 | xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt) |
| 1475 | { |
| 1476 | xmlRelaxNGIncludePtr ret; |
| 1477 | |
| 1478 | if (ctxt->incNr <= 0) |
| 1479 | return (0); |
| 1480 | ctxt->incNr--; |
| 1481 | if (ctxt->incNr > 0) |
| 1482 | ctxt->inc = ctxt->incTab[ctxt->incNr - 1]; |
| 1483 | else |
| 1484 | ctxt->inc = NULL; |
| 1485 | ret = ctxt->incTab[ctxt->incNr]; |
| 1486 | ctxt->incTab[ctxt->incNr] = 0; |
| 1487 | return (ret); |
| 1488 | } |
| 1489 | |
| 1490 | /** |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1491 | * xmlRelaxNGRemoveRedefine: |
| 1492 | * @ctxt: the parser context |
| 1493 | * @URL: the normalized URL |
| 1494 | * @target: the included target |
| 1495 | * @name: the define name to eliminate |
| 1496 | * |
| 1497 | * Applies the elimination algorithm of 4.7 |
| 1498 | * |
| 1499 | * Returns 0 in case of error, 1 in case of success. |
| 1500 | */ |
| 1501 | static int |
| 1502 | xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1503 | const xmlChar * URL ATTRIBUTE_UNUSED, |
| 1504 | xmlNodePtr target, const xmlChar * name) |
| 1505 | { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1506 | int found = 0; |
| 1507 | xmlNodePtr tmp, tmp2; |
| 1508 | xmlChar *name2; |
| 1509 | |
| 1510 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 1511 | if (name == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1512 | xmlGenericError(xmlGenericErrorContext, |
| 1513 | "Elimination of <include> start from %s\n", URL); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 1514 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1515 | xmlGenericError(xmlGenericErrorContext, |
| 1516 | "Elimination of <include> define %s from %s\n", |
| 1517 | name, URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1518 | #endif |
| 1519 | tmp = target; |
| 1520 | while (tmp != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1521 | tmp2 = tmp->next; |
| 1522 | if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) { |
| 1523 | found = 1; |
| 1524 | xmlUnlinkNode(tmp); |
| 1525 | xmlFreeNode(tmp); |
| 1526 | } else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) { |
| 1527 | name2 = xmlGetProp(tmp, BAD_CAST "name"); |
| 1528 | xmlRelaxNGNormExtSpace(name2); |
| 1529 | if (name2 != NULL) { |
| 1530 | if (xmlStrEqual(name, name2)) { |
| 1531 | found = 1; |
| 1532 | xmlUnlinkNode(tmp); |
| 1533 | xmlFreeNode(tmp); |
| 1534 | } |
| 1535 | xmlFree(name2); |
| 1536 | } |
| 1537 | } else if (IS_RELAXNG(tmp, "include")) { |
| 1538 | xmlChar *href = NULL; |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 1539 | xmlRelaxNGDocumentPtr inc = tmp->psvi; |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1540 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1541 | if ((inc != NULL) && (inc->doc != NULL) && |
| 1542 | (inc->doc->children != NULL)) { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1543 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1544 | if (xmlStrEqual |
| 1545 | (inc->doc->children->name, BAD_CAST "grammar")) { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1546 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1547 | href = xmlGetProp(tmp, BAD_CAST "href"); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1548 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1549 | if (xmlRelaxNGRemoveRedefine(ctxt, href, |
| 1550 | inc->doc->children-> |
| 1551 | children, name) == 1) { |
| 1552 | found = 1; |
| 1553 | } |
| 1554 | if (href != NULL) |
| 1555 | xmlFree(href); |
| 1556 | } |
| 1557 | } |
| 1558 | } |
| 1559 | tmp = tmp2; |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1560 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1561 | return (found); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
| 1564 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1565 | * xmlRelaxNGLoadInclude: |
| 1566 | * @ctxt: the parser context |
| 1567 | * @URL: the normalized URL |
| 1568 | * @node: the include node. |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1569 | * @ns: the namespace passed from the context. |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1570 | * |
| 1571 | * First lookup if the document is already loaded into the parser context, |
| 1572 | * check against recursion. If not found the resource is loaded and |
| 1573 | * the content is preprocessed before being returned back to the caller. |
| 1574 | * |
| 1575 | * Returns the xmlRelaxNGIncludePtr or NULL in case of error |
| 1576 | */ |
| 1577 | static xmlRelaxNGIncludePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1578 | xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * URL, |
| 1579 | xmlNodePtr node, const xmlChar * ns) |
| 1580 | { |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1581 | xmlRelaxNGIncludePtr ret = NULL; |
| 1582 | xmlDocPtr doc; |
| 1583 | int i; |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1584 | xmlNodePtr root, cur; |
| 1585 | |
| 1586 | #ifdef DEBUG_INCLUDE |
| 1587 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1588 | "xmlRelaxNGLoadInclude(%s)\n", URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1589 | #endif |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1590 | |
| 1591 | /* |
| 1592 | * check against recursion in the stack |
| 1593 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1594 | for (i = 0; i < ctxt->incNr; i++) { |
| 1595 | if (xmlStrEqual(ctxt->incTab[i]->href, URL)) { |
| 1596 | xmlRngPErr(ctxt, NULL, XML_RNGP_INCLUDE_RECURSE, |
| 1597 | "Detected an Include recursion for %s\n", URL, |
| 1598 | NULL); |
| 1599 | return (NULL); |
| 1600 | } |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1601 | } |
| 1602 | |
| 1603 | /* |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1604 | * load the document |
| 1605 | */ |
Daniel Veillard | 87247e8 | 2004-01-13 20:42:02 +0000 | [diff] [blame] | 1606 | doc = xmlReadFile((const char *) URL,NULL,0); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1607 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1608 | xmlRngPErr(ctxt, node, XML_RNGP_PARSE_ERROR, |
| 1609 | "xmlRelaxNG: could not load %s\n", URL, NULL); |
| 1610 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1611 | } |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1612 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1613 | xmlGenericError(xmlGenericErrorContext, "Parsed %s Okay\n", URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1614 | #endif |
| 1615 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1616 | /* |
| 1617 | * Allocate the document structures and register it first. |
| 1618 | */ |
| 1619 | ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude)); |
| 1620 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1621 | xmlRngPErrMemory(ctxt, "allocating include\n"); |
| 1622 | xmlFreeDoc(doc); |
| 1623 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1624 | } |
| 1625 | memset(ret, 0, sizeof(xmlRelaxNGInclude)); |
| 1626 | ret->doc = doc; |
| 1627 | ret->href = xmlStrdup(URL); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1628 | ret->next = ctxt->includes; |
| 1629 | ctxt->includes = ret; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1630 | |
| 1631 | /* |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1632 | * transmit the ns if needed |
| 1633 | */ |
| 1634 | if (ns != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1635 | root = xmlDocGetRootElement(doc); |
| 1636 | if (root != NULL) { |
| 1637 | if (xmlHasProp(root, BAD_CAST "ns") == NULL) { |
| 1638 | xmlSetProp(root, BAD_CAST "ns", ns); |
| 1639 | } |
| 1640 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1641 | } |
| 1642 | |
| 1643 | /* |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1644 | * push it on the stack |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1645 | */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1646 | xmlRelaxNGIncludePush(ctxt, ret); |
| 1647 | |
| 1648 | /* |
| 1649 | * Some preprocessing of the document content, this include recursing |
| 1650 | * in the include stack. |
| 1651 | */ |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1652 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1653 | xmlGenericError(xmlGenericErrorContext, "cleanup of %s\n", URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1654 | #endif |
| 1655 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1656 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 1657 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1658 | ctxt->inc = NULL; |
| 1659 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
| 1662 | /* |
| 1663 | * Pop up the include from the stack |
| 1664 | */ |
| 1665 | xmlRelaxNGIncludePop(ctxt); |
| 1666 | |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1667 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1668 | xmlGenericError(xmlGenericErrorContext, "Checking of %s\n", URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1669 | #endif |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1670 | /* |
| 1671 | * Check that the top element is a grammar |
| 1672 | */ |
| 1673 | root = xmlDocGetRootElement(doc); |
| 1674 | if (root == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1675 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, |
| 1676 | "xmlRelaxNG: included document is empty %s\n", URL, |
| 1677 | NULL); |
| 1678 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1679 | } |
| 1680 | if (!IS_RELAXNG(root, "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1681 | xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING, |
| 1682 | "xmlRelaxNG: included document %s root is not a grammar\n", |
| 1683 | URL, NULL); |
| 1684 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1685 | } |
| 1686 | |
| 1687 | /* |
| 1688 | * Elimination of redefined rules in the include. |
| 1689 | */ |
| 1690 | cur = node->children; |
| 1691 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1692 | if (IS_RELAXNG(cur, "start")) { |
| 1693 | int found = 0; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1694 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1695 | found = |
| 1696 | xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL); |
| 1697 | if (!found) { |
| 1698 | xmlRngPErr(ctxt, node, XML_RNGP_START_MISSING, |
| 1699 | "xmlRelaxNG: include %s has a start but not the included grammar\n", |
| 1700 | URL, NULL); |
| 1701 | } |
| 1702 | } else if (IS_RELAXNG(cur, "define")) { |
| 1703 | xmlChar *name; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1704 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1705 | name = xmlGetProp(cur, BAD_CAST "name"); |
| 1706 | if (name == NULL) { |
| 1707 | xmlRngPErr(ctxt, node, XML_RNGP_NAME_MISSING, |
| 1708 | "xmlRelaxNG: include %s has define without name\n", |
| 1709 | URL, NULL); |
| 1710 | } else { |
| 1711 | int found; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1712 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1713 | xmlRelaxNGNormExtSpace(name); |
| 1714 | found = xmlRelaxNGRemoveRedefine(ctxt, URL, |
| 1715 | root->children, name); |
| 1716 | if (!found) { |
| 1717 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_MISSING, |
| 1718 | "xmlRelaxNG: include %s has a define %s but not the included grammar\n", |
| 1719 | URL, name); |
| 1720 | } |
| 1721 | xmlFree(name); |
| 1722 | } |
| 1723 | } |
| 1724 | cur = cur->next; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1725 | } |
| 1726 | |
| 1727 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1728 | return (ret); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
| 1731 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1732 | * xmlRelaxNGValidErrorPush: |
| 1733 | * @ctxt: the validation context |
| 1734 | * @err: the error code |
| 1735 | * @arg1: the first string argument |
| 1736 | * @arg2: the second string argument |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1737 | * @dup: arg need to be duplicated |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1738 | * |
| 1739 | * Pushes a new error on top of the error stack |
| 1740 | * |
| 1741 | * Returns 0 in case of error, the index in the stack otherwise |
| 1742 | */ |
| 1743 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1744 | xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt, |
| 1745 | xmlRelaxNGValidErr err, const xmlChar * arg1, |
| 1746 | const xmlChar * arg2, int dup) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1747 | { |
| 1748 | xmlRelaxNGValidErrorPtr cur; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1749 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 1750 | #ifdef DEBUG_ERROR |
| 1751 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1752 | "Pushing error %d at %d on stack\n", err, ctxt->errNr); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 1753 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1754 | if (ctxt->errTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1755 | ctxt->errMax = 8; |
| 1756 | ctxt->errNr = 0; |
| 1757 | ctxt->errTab = |
| 1758 | (xmlRelaxNGValidErrorPtr) xmlMalloc(ctxt->errMax * |
| 1759 | sizeof |
| 1760 | (xmlRelaxNGValidError)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1761 | if (ctxt->errTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1762 | xmlRngVErrMemory(ctxt, "pushing error\n"); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1763 | return (0); |
| 1764 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1765 | ctxt->err = NULL; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1766 | } |
| 1767 | if (ctxt->errNr >= ctxt->errMax) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1768 | ctxt->errMax *= 2; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1769 | ctxt->errTab = |
| 1770 | (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1771 | ctxt->errMax * |
| 1772 | sizeof |
| 1773 | (xmlRelaxNGValidError)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1774 | if (ctxt->errTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1775 | xmlRngVErrMemory(ctxt, "pushing error\n"); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1776 | return (0); |
| 1777 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1778 | ctxt->err = &ctxt->errTab[ctxt->errNr - 1]; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1779 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 1780 | if ((ctxt->err != NULL) && (ctxt->state != NULL) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1781 | (ctxt->err->node == ctxt->state->node) && (ctxt->err->err == err)) |
| 1782 | return (ctxt->errNr); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1783 | cur = &ctxt->errTab[ctxt->errNr]; |
| 1784 | cur->err = err; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1785 | if (dup) { |
| 1786 | cur->arg1 = xmlStrdup(arg1); |
| 1787 | cur->arg2 = xmlStrdup(arg2); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1788 | cur->flags = ERROR_IS_DUP; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1789 | } else { |
| 1790 | cur->arg1 = arg1; |
| 1791 | cur->arg2 = arg2; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1792 | cur->flags = 0; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1793 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1794 | if (ctxt->state != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1795 | cur->node = ctxt->state->node; |
| 1796 | cur->seq = ctxt->state->seq; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1797 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1798 | cur->node = NULL; |
| 1799 | cur->seq = NULL; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1800 | } |
| 1801 | ctxt->err = cur; |
| 1802 | return (ctxt->errNr++); |
| 1803 | } |
| 1804 | |
| 1805 | /** |
| 1806 | * xmlRelaxNGValidErrorPop: |
| 1807 | * @ctxt: the validation context |
| 1808 | * |
| 1809 | * Pops the top error from the error stack |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1810 | */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1811 | static void |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1812 | xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt) |
| 1813 | { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1814 | xmlRelaxNGValidErrorPtr cur; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1815 | |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1816 | if (ctxt->errNr <= 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1817 | ctxt->err = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1818 | return; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1819 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1820 | ctxt->errNr--; |
| 1821 | if (ctxt->errNr > 0) |
| 1822 | ctxt->err = &ctxt->errTab[ctxt->errNr - 1]; |
| 1823 | else |
| 1824 | ctxt->err = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1825 | cur = &ctxt->errTab[ctxt->errNr]; |
| 1826 | if (cur->flags & ERROR_IS_DUP) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1827 | if (cur->arg1 != NULL) |
| 1828 | xmlFree((xmlChar *) cur->arg1); |
| 1829 | cur->arg1 = NULL; |
| 1830 | if (cur->arg2 != NULL) |
| 1831 | xmlFree((xmlChar *) cur->arg2); |
| 1832 | cur->arg2 = NULL; |
| 1833 | cur->flags = 0; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1834 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1835 | } |
| 1836 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1837 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1838 | * xmlRelaxNGDocumentPush: |
| 1839 | * @ctxt: the parser context |
| 1840 | * @value: the element doc |
| 1841 | * |
| 1842 | * Pushes a new doc on top of the doc stack |
| 1843 | * |
| 1844 | * Returns 0 in case of error, the index in the stack otherwise |
| 1845 | */ |
| 1846 | static int |
| 1847 | xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1848 | xmlRelaxNGDocumentPtr value) |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1849 | { |
| 1850 | if (ctxt->docTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1851 | ctxt->docMax = 4; |
| 1852 | ctxt->docNr = 0; |
| 1853 | ctxt->docTab = |
| 1854 | (xmlRelaxNGDocumentPtr *) xmlMalloc(ctxt->docMax * |
| 1855 | sizeof(ctxt->docTab[0])); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1856 | if (ctxt->docTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1857 | xmlRngPErrMemory(ctxt, "adding document\n"); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1858 | return (0); |
| 1859 | } |
| 1860 | } |
| 1861 | if (ctxt->docNr >= ctxt->docMax) { |
| 1862 | ctxt->docMax *= 2; |
| 1863 | ctxt->docTab = |
| 1864 | (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1865 | ctxt->docMax * |
| 1866 | sizeof(ctxt->docTab[0])); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1867 | if (ctxt->docTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1868 | xmlRngPErrMemory(ctxt, "adding document\n"); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1869 | return (0); |
| 1870 | } |
| 1871 | } |
| 1872 | ctxt->docTab[ctxt->docNr] = value; |
| 1873 | ctxt->doc = value; |
| 1874 | return (ctxt->docNr++); |
| 1875 | } |
| 1876 | |
| 1877 | /** |
| 1878 | * xmlRelaxNGDocumentPop: |
| 1879 | * @ctxt: the parser context |
| 1880 | * |
| 1881 | * Pops the top doc from the doc stack |
| 1882 | * |
| 1883 | * Returns the doc just removed |
| 1884 | */ |
| 1885 | static xmlRelaxNGDocumentPtr |
| 1886 | xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt) |
| 1887 | { |
| 1888 | xmlRelaxNGDocumentPtr ret; |
| 1889 | |
| 1890 | if (ctxt->docNr <= 0) |
| 1891 | return (0); |
| 1892 | ctxt->docNr--; |
| 1893 | if (ctxt->docNr > 0) |
| 1894 | ctxt->doc = ctxt->docTab[ctxt->docNr - 1]; |
| 1895 | else |
| 1896 | ctxt->doc = NULL; |
| 1897 | ret = ctxt->docTab[ctxt->docNr]; |
| 1898 | ctxt->docTab[ctxt->docNr] = 0; |
| 1899 | return (ret); |
| 1900 | } |
| 1901 | |
| 1902 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1903 | * xmlRelaxNGLoadExternalRef: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1904 | * @ctxt: the parser context |
| 1905 | * @URL: the normalized URL |
| 1906 | * @ns: the inherited ns if any |
| 1907 | * |
| 1908 | * First lookup if the document is already loaded into the parser context, |
| 1909 | * check against recursion. If not found the resource is loaded and |
| 1910 | * the content is preprocessed before being returned back to the caller. |
| 1911 | * |
| 1912 | * Returns the xmlRelaxNGDocumentPtr or NULL in case of error |
| 1913 | */ |
| 1914 | static xmlRelaxNGDocumentPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1915 | xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt, |
| 1916 | const xmlChar * URL, const xmlChar * ns) |
| 1917 | { |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1918 | xmlRelaxNGDocumentPtr ret = NULL; |
| 1919 | xmlDocPtr doc; |
| 1920 | xmlNodePtr root; |
| 1921 | int i; |
| 1922 | |
| 1923 | /* |
| 1924 | * check against recursion in the stack |
| 1925 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1926 | for (i = 0; i < ctxt->docNr; i++) { |
| 1927 | if (xmlStrEqual(ctxt->docTab[i]->href, URL)) { |
| 1928 | xmlRngPErr(ctxt, NULL, XML_RNGP_EXTERNALREF_RECURSE, |
| 1929 | "Detected an externalRef recursion for %s\n", URL, |
| 1930 | NULL); |
| 1931 | return (NULL); |
| 1932 | } |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1933 | } |
| 1934 | |
| 1935 | /* |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1936 | * load the document |
| 1937 | */ |
Daniel Veillard | 87247e8 | 2004-01-13 20:42:02 +0000 | [diff] [blame] | 1938 | doc = xmlReadFile((const char *) URL,NULL,0); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1939 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1940 | xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR, |
| 1941 | "xmlRelaxNG: could not load %s\n", URL, NULL); |
| 1942 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1943 | } |
| 1944 | |
| 1945 | /* |
| 1946 | * Allocate the document structures and register it first. |
| 1947 | */ |
| 1948 | ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument)); |
| 1949 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1950 | xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_ERR_NO_MEMORY, |
| 1951 | "xmlRelaxNG: allocate memory for doc %s\n", URL, NULL); |
| 1952 | xmlFreeDoc(doc); |
| 1953 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1954 | } |
| 1955 | memset(ret, 0, sizeof(xmlRelaxNGDocument)); |
| 1956 | ret->doc = doc; |
| 1957 | ret->href = xmlStrdup(URL); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1958 | ret->next = ctxt->documents; |
| 1959 | ctxt->documents = ret; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1960 | |
| 1961 | /* |
| 1962 | * transmit the ns if needed |
| 1963 | */ |
| 1964 | if (ns != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1965 | root = xmlDocGetRootElement(doc); |
| 1966 | if (root != NULL) { |
| 1967 | if (xmlHasProp(root, BAD_CAST "ns") == NULL) { |
| 1968 | xmlSetProp(root, BAD_CAST "ns", ns); |
| 1969 | } |
| 1970 | } |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1971 | } |
| 1972 | |
| 1973 | /* |
| 1974 | * push it on the stack and register it in the hash table |
| 1975 | */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1976 | xmlRelaxNGDocumentPush(ctxt, ret); |
| 1977 | |
| 1978 | /* |
| 1979 | * Some preprocessing of the document content |
| 1980 | */ |
| 1981 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 1982 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1983 | ctxt->doc = NULL; |
| 1984 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1985 | } |
| 1986 | |
| 1987 | xmlRelaxNGDocumentPop(ctxt); |
| 1988 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1989 | return (ret); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1990 | } |
| 1991 | |
| 1992 | /************************************************************************ |
| 1993 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1994 | * Error functions * |
| 1995 | * * |
| 1996 | ************************************************************************/ |
| 1997 | |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1998 | #define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0); |
| 1999 | #define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0); |
| 2000 | #define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0); |
| 2001 | #define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1); |
| 2002 | #define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2003 | |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 2004 | static const char * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2005 | xmlRelaxNGDefName(xmlRelaxNGDefinePtr def) |
| 2006 | { |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 2007 | if (def == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2008 | return ("none"); |
| 2009 | switch (def->type) { |
| 2010 | case XML_RELAXNG_EMPTY: |
| 2011 | return ("empty"); |
| 2012 | case XML_RELAXNG_NOT_ALLOWED: |
| 2013 | return ("notAllowed"); |
| 2014 | case XML_RELAXNG_EXCEPT: |
| 2015 | return ("except"); |
| 2016 | case XML_RELAXNG_TEXT: |
| 2017 | return ("text"); |
| 2018 | case XML_RELAXNG_ELEMENT: |
| 2019 | return ("element"); |
| 2020 | case XML_RELAXNG_DATATYPE: |
| 2021 | return ("datatype"); |
| 2022 | case XML_RELAXNG_VALUE: |
| 2023 | return ("value"); |
| 2024 | case XML_RELAXNG_LIST: |
| 2025 | return ("list"); |
| 2026 | case XML_RELAXNG_ATTRIBUTE: |
| 2027 | return ("attribute"); |
| 2028 | case XML_RELAXNG_DEF: |
| 2029 | return ("def"); |
| 2030 | case XML_RELAXNG_REF: |
| 2031 | return ("ref"); |
| 2032 | case XML_RELAXNG_EXTERNALREF: |
| 2033 | return ("externalRef"); |
| 2034 | case XML_RELAXNG_PARENTREF: |
| 2035 | return ("parentRef"); |
| 2036 | case XML_RELAXNG_OPTIONAL: |
| 2037 | return ("optional"); |
| 2038 | case XML_RELAXNG_ZEROORMORE: |
| 2039 | return ("zeroOrMore"); |
| 2040 | case XML_RELAXNG_ONEORMORE: |
| 2041 | return ("oneOrMore"); |
| 2042 | case XML_RELAXNG_CHOICE: |
| 2043 | return ("choice"); |
| 2044 | case XML_RELAXNG_GROUP: |
| 2045 | return ("group"); |
| 2046 | case XML_RELAXNG_INTERLEAVE: |
| 2047 | return ("interleave"); |
| 2048 | case XML_RELAXNG_START: |
| 2049 | return ("start"); |
| 2050 | case XML_RELAXNG_NOOP: |
| 2051 | return ("noop"); |
| 2052 | case XML_RELAXNG_PARAM: |
| 2053 | return ("param"); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 2054 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2055 | return ("unknown"); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 2056 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 2057 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2058 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2059 | * xmlRelaxNGGetErrorString: |
| 2060 | * @err: the error code |
| 2061 | * @arg1: the first string argument |
| 2062 | * @arg2: the second string argument |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2063 | * |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2064 | * computes a formatted error string for the given error code and args |
| 2065 | * |
| 2066 | * Returns the error string, it must be deallocated by the caller |
| 2067 | */ |
| 2068 | static xmlChar * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2069 | xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar * arg1, |
| 2070 | const xmlChar * arg2) |
| 2071 | { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2072 | char msg[1000]; |
| 2073 | |
| 2074 | if (arg1 == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2075 | arg1 = BAD_CAST ""; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2076 | if (arg2 == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2077 | arg2 = BAD_CAST ""; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2078 | |
| 2079 | msg[0] = 0; |
| 2080 | switch (err) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2081 | case XML_RELAXNG_OK: |
| 2082 | return (NULL); |
| 2083 | case XML_RELAXNG_ERR_MEMORY: |
| 2084 | return (xmlCharStrdup("out of memory\n")); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2085 | case XML_RELAXNG_ERR_TYPE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2086 | snprintf(msg, 1000, "failed to validate type %s\n", arg1); |
| 2087 | break; |
| 2088 | case XML_RELAXNG_ERR_TYPEVAL: |
| 2089 | snprintf(msg, 1000, "Type %s doesn't allow value '%s'\n", arg1, |
| 2090 | arg2); |
| 2091 | break; |
| 2092 | case XML_RELAXNG_ERR_DUPID: |
| 2093 | snprintf(msg, 1000, "ID %s redefined\n", arg1); |
| 2094 | break; |
| 2095 | case XML_RELAXNG_ERR_TYPECMP: |
| 2096 | snprintf(msg, 1000, "failed to compare type %s\n", arg1); |
| 2097 | break; |
| 2098 | case XML_RELAXNG_ERR_NOSTATE: |
| 2099 | return (xmlCharStrdup("Internal error: no state\n")); |
| 2100 | case XML_RELAXNG_ERR_NODEFINE: |
| 2101 | return (xmlCharStrdup("Internal error: no define\n")); |
| 2102 | case XML_RELAXNG_ERR_INTERNAL: |
| 2103 | snprintf(msg, 1000, "Internal error: %s\n", arg1); |
| 2104 | break; |
| 2105 | case XML_RELAXNG_ERR_LISTEXTRA: |
| 2106 | snprintf(msg, 1000, "Extra data in list: %s\n", arg1); |
| 2107 | break; |
| 2108 | case XML_RELAXNG_ERR_INTERNODATA: |
| 2109 | return (xmlCharStrdup |
| 2110 | ("Internal: interleave block has no data\n")); |
| 2111 | case XML_RELAXNG_ERR_INTERSEQ: |
| 2112 | return (xmlCharStrdup("Invalid sequence in interleave\n")); |
| 2113 | case XML_RELAXNG_ERR_INTEREXTRA: |
| 2114 | snprintf(msg, 1000, "Extra element %s in interleave\n", arg1); |
| 2115 | break; |
| 2116 | case XML_RELAXNG_ERR_ELEMNAME: |
| 2117 | snprintf(msg, 1000, "Expecting element %s, got %s\n", arg1, |
| 2118 | arg2); |
| 2119 | break; |
| 2120 | case XML_RELAXNG_ERR_ELEMNONS: |
| 2121 | snprintf(msg, 1000, "Expecting a namespace for element %s\n", |
| 2122 | arg1); |
| 2123 | break; |
| 2124 | case XML_RELAXNG_ERR_ELEMWRONGNS: |
| 2125 | snprintf(msg, 1000, |
| 2126 | "Element %s has wrong namespace: expecting %s\n", arg1, |
| 2127 | arg2); |
| 2128 | break; |
| 2129 | case XML_RELAXNG_ERR_ELEMWRONG: |
| 2130 | snprintf(msg, 1000, "Did not expect element %s there\n", arg1); |
| 2131 | break; |
| 2132 | case XML_RELAXNG_ERR_TEXTWRONG: |
| 2133 | snprintf(msg, 1000, |
| 2134 | "Did not expect text in element %s content\n", arg1); |
| 2135 | break; |
| 2136 | case XML_RELAXNG_ERR_ELEMEXTRANS: |
| 2137 | snprintf(msg, 1000, "Expecting no namespace for element %s\n", |
| 2138 | arg1); |
| 2139 | break; |
| 2140 | case XML_RELAXNG_ERR_ELEMNOTEMPTY: |
| 2141 | snprintf(msg, 1000, "Expecting element %s to be empty\n", arg1); |
| 2142 | break; |
| 2143 | case XML_RELAXNG_ERR_NOELEM: |
| 2144 | snprintf(msg, 1000, "Expecting an element %s, got nothing\n", |
| 2145 | arg1); |
| 2146 | break; |
| 2147 | case XML_RELAXNG_ERR_NOTELEM: |
| 2148 | return (xmlCharStrdup("Expecting an element got text\n")); |
| 2149 | case XML_RELAXNG_ERR_ATTRVALID: |
| 2150 | snprintf(msg, 1000, "Element %s failed to validate attributes\n", |
| 2151 | arg1); |
| 2152 | break; |
| 2153 | case XML_RELAXNG_ERR_CONTENTVALID: |
| 2154 | snprintf(msg, 1000, "Element %s failed to validate content\n", |
| 2155 | arg1); |
| 2156 | break; |
| 2157 | case XML_RELAXNG_ERR_EXTRACONTENT: |
| 2158 | snprintf(msg, 1000, "Element %s has extra content: %s\n", |
| 2159 | arg1, arg2); |
| 2160 | break; |
| 2161 | case XML_RELAXNG_ERR_INVALIDATTR: |
| 2162 | snprintf(msg, 1000, "Invalid attribute %s for element %s\n", |
| 2163 | arg1, arg2); |
| 2164 | break; |
| 2165 | case XML_RELAXNG_ERR_LACKDATA: |
| 2166 | snprintf(msg, 1000, "Datatype element %s contains no data\n", |
| 2167 | arg1); |
| 2168 | break; |
| 2169 | case XML_RELAXNG_ERR_DATAELEM: |
| 2170 | snprintf(msg, 1000, "Datatype element %s has child elements\n", |
| 2171 | arg1); |
| 2172 | break; |
| 2173 | case XML_RELAXNG_ERR_VALELEM: |
| 2174 | snprintf(msg, 1000, "Value element %s has child elements\n", |
| 2175 | arg1); |
| 2176 | break; |
| 2177 | case XML_RELAXNG_ERR_LISTELEM: |
| 2178 | snprintf(msg, 1000, "List element %s has child elements\n", |
| 2179 | arg1); |
| 2180 | break; |
| 2181 | case XML_RELAXNG_ERR_DATATYPE: |
| 2182 | snprintf(msg, 1000, "Error validating datatype %s\n", arg1); |
| 2183 | break; |
| 2184 | case XML_RELAXNG_ERR_VALUE: |
| 2185 | snprintf(msg, 1000, "Error validating value %s\n", arg1); |
| 2186 | break; |
| 2187 | case XML_RELAXNG_ERR_LIST: |
| 2188 | return (xmlCharStrdup("Error validating list\n")); |
| 2189 | case XML_RELAXNG_ERR_NOGRAMMAR: |
| 2190 | return (xmlCharStrdup("No top grammar defined\n")); |
| 2191 | case XML_RELAXNG_ERR_EXTRADATA: |
| 2192 | return (xmlCharStrdup("Extra data in the document\n")); |
| 2193 | default: |
| 2194 | return (xmlCharStrdup("Unknown error !\n")); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2195 | } |
| 2196 | if (msg[0] == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2197 | snprintf(msg, 1000, "Unknown error code %d\n", err); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2198 | } |
Daniel Veillard | adbb0e6 | 2003-05-10 20:02:45 +0000 | [diff] [blame] | 2199 | msg[1000 - 1] = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2200 | return (xmlStrdup((xmlChar *) msg)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2201 | } |
| 2202 | |
| 2203 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2204 | * xmlRelaxNGShowValidError: |
| 2205 | * @ctxt: the validation context |
| 2206 | * @err: the error number |
| 2207 | * @node: the node |
| 2208 | * @child: the node child generating the problem. |
| 2209 | * @arg1: the first argument |
| 2210 | * @arg2: the second argument |
| 2211 | * |
| 2212 | * Show a validation error. |
| 2213 | */ |
| 2214 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2215 | xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt, |
| 2216 | xmlRelaxNGValidErr err, xmlNodePtr node, |
| 2217 | xmlNodePtr child, const xmlChar * arg1, |
| 2218 | const xmlChar * arg2) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2219 | { |
| 2220 | xmlChar *msg; |
| 2221 | |
| 2222 | if (ctxt->error == NULL) |
| 2223 | return; |
| 2224 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2225 | #ifdef DEBUG_ERROR |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2226 | xmlGenericError(xmlGenericErrorContext, "Show error %d\n", err); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2227 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2228 | msg = xmlRelaxNGGetErrorString(err, arg1, arg2); |
| 2229 | if (msg == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2230 | return; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2231 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2232 | if (ctxt->errNo == XML_RELAXNG_OK) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2233 | ctxt->errNo = err; |
| 2234 | xmlRngVErr(ctxt, (child == NULL ? node : child), err, |
| 2235 | (const char *) msg, arg1, arg2); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2236 | xmlFree(msg); |
| 2237 | } |
| 2238 | |
| 2239 | /** |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2240 | * xmlRelaxNGPopErrors: |
| 2241 | * @ctxt: the validation context |
| 2242 | * @level: the error level in the stack |
| 2243 | * |
| 2244 | * pop and discard all errors until the given level is reached |
| 2245 | */ |
| 2246 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2247 | xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level) |
| 2248 | { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2249 | int i; |
| 2250 | xmlRelaxNGValidErrorPtr err; |
| 2251 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2252 | #ifdef DEBUG_ERROR |
| 2253 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2254 | "Pop errors till level %d\n", level); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2255 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2256 | for (i = level; i < ctxt->errNr; i++) { |
| 2257 | err = &ctxt->errTab[i]; |
| 2258 | if (err->flags & ERROR_IS_DUP) { |
| 2259 | if (err->arg1 != NULL) |
| 2260 | xmlFree((xmlChar *) err->arg1); |
| 2261 | err->arg1 = NULL; |
| 2262 | if (err->arg2 != NULL) |
| 2263 | xmlFree((xmlChar *) err->arg2); |
| 2264 | err->arg2 = NULL; |
| 2265 | err->flags = 0; |
| 2266 | } |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2267 | } |
| 2268 | ctxt->errNr = level; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2269 | if (ctxt->errNr <= 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2270 | ctxt->err = NULL; |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2271 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2272 | |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2273 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2274 | * xmlRelaxNGDumpValidError: |
| 2275 | * @ctxt: the validation context |
| 2276 | * |
| 2277 | * Show all validation error over a given index. |
| 2278 | */ |
| 2279 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2280 | xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt) |
| 2281 | { |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 2282 | int i, j, k; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2283 | xmlRelaxNGValidErrorPtr err, dup; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2284 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2285 | #ifdef DEBUG_ERROR |
| 2286 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2287 | "Dumping error stack %d errors\n", ctxt->errNr); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2288 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2289 | for (i = 0, k = 0; i < ctxt->errNr; i++) { |
| 2290 | err = &ctxt->errTab[i]; |
| 2291 | if (k < MAX_ERROR) { |
| 2292 | for (j = 0; j < i; j++) { |
| 2293 | dup = &ctxt->errTab[j]; |
| 2294 | if ((err->err == dup->err) && (err->node == dup->node) && |
| 2295 | (xmlStrEqual(err->arg1, dup->arg1)) && |
| 2296 | (xmlStrEqual(err->arg2, dup->arg2))) { |
| 2297 | goto skip; |
| 2298 | } |
| 2299 | } |
| 2300 | xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq, |
| 2301 | err->arg1, err->arg2); |
| 2302 | k++; |
| 2303 | } |
| 2304 | skip: |
| 2305 | if (err->flags & ERROR_IS_DUP) { |
| 2306 | if (err->arg1 != NULL) |
| 2307 | xmlFree((xmlChar *) err->arg1); |
| 2308 | err->arg1 = NULL; |
| 2309 | if (err->arg2 != NULL) |
| 2310 | xmlFree((xmlChar *) err->arg2); |
| 2311 | err->arg2 = NULL; |
| 2312 | err->flags = 0; |
| 2313 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2314 | } |
| 2315 | ctxt->errNr = 0; |
| 2316 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2317 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2318 | /** |
| 2319 | * xmlRelaxNGAddValidError: |
| 2320 | * @ctxt: the validation context |
| 2321 | * @err: the error number |
| 2322 | * @arg1: the first argument |
| 2323 | * @arg2: the second argument |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2324 | * @dup: need to dup the args |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2325 | * |
| 2326 | * Register a validation error, either generating it if it's sure |
| 2327 | * or stacking it for later handling if unsure. |
| 2328 | */ |
| 2329 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2330 | xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt, |
| 2331 | xmlRelaxNGValidErr err, const xmlChar * arg1, |
| 2332 | const xmlChar * arg2, int dup) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2333 | { |
| 2334 | if ((ctxt == NULL) || (ctxt->error == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2335 | return; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2336 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2337 | #ifdef DEBUG_ERROR |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2338 | xmlGenericError(xmlGenericErrorContext, "Adding error %d\n", err); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2339 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2340 | /* |
| 2341 | * generate the error directly |
| 2342 | */ |
| 2343 | if (((ctxt->flags & 1) == 0) || (ctxt->flags & 2)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2344 | xmlNodePtr node, seq; |
| 2345 | |
| 2346 | /* |
| 2347 | * Flush first any stacked error which might be the |
| 2348 | * real cause of the problem. |
| 2349 | */ |
| 2350 | if (ctxt->errNr != 0) |
| 2351 | xmlRelaxNGDumpValidError(ctxt); |
| 2352 | if (ctxt->state != NULL) { |
| 2353 | node = ctxt->state->node; |
| 2354 | seq = ctxt->state->seq; |
| 2355 | } else { |
| 2356 | node = seq = NULL; |
| 2357 | } |
| 2358 | xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2359 | } |
| 2360 | /* |
| 2361 | * Stack the error for later processing if needed |
| 2362 | */ |
| 2363 | else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2364 | xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2365 | } |
| 2366 | } |
| 2367 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2368 | |
| 2369 | /************************************************************************ |
| 2370 | * * |
| 2371 | * Type library hooks * |
| 2372 | * * |
| 2373 | ************************************************************************/ |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2374 | static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2375 | const xmlChar * str); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2376 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2377 | /** |
| 2378 | * xmlRelaxNGSchemaTypeHave: |
| 2379 | * @data: data needed for the library |
| 2380 | * @type: the type name |
| 2381 | * |
| 2382 | * Check if the given type is provided by |
| 2383 | * the W3C XMLSchema Datatype library. |
| 2384 | * |
| 2385 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2386 | */ |
| 2387 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2388 | xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar * type) |
| 2389 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2390 | xmlSchemaTypePtr typ; |
| 2391 | |
| 2392 | if (type == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2393 | return (-1); |
| 2394 | typ = xmlSchemaGetPredefinedType(type, |
| 2395 | BAD_CAST |
| 2396 | "http://www.w3.org/2001/XMLSchema"); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2397 | if (typ == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2398 | return (0); |
| 2399 | return (1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2400 | } |
| 2401 | |
| 2402 | /** |
| 2403 | * xmlRelaxNGSchemaTypeCheck: |
| 2404 | * @data: data needed for the library |
| 2405 | * @type: the type name |
| 2406 | * @value: the value to check |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2407 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2408 | * |
| 2409 | * Check if the given type and value are validated by |
| 2410 | * the W3C XMLSchema Datatype library. |
| 2411 | * |
| 2412 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2413 | */ |
| 2414 | static int |
| 2415 | xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2416 | const xmlChar * type, |
| 2417 | const xmlChar * value, |
| 2418 | void **result, xmlNodePtr node) |
| 2419 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2420 | xmlSchemaTypePtr typ; |
| 2421 | int ret; |
| 2422 | |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2423 | if ((type == NULL) || (value == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2424 | return (-1); |
| 2425 | typ = xmlSchemaGetPredefinedType(type, |
| 2426 | BAD_CAST |
| 2427 | "http://www.w3.org/2001/XMLSchema"); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2428 | if (typ == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2429 | return (-1); |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2430 | ret = xmlSchemaValPredefTypeNode(typ, value, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2431 | (xmlSchemaValPtr *) result, node); |
| 2432 | if (ret == 2) /* special ID error code */ |
| 2433 | return (2); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2434 | if (ret == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2435 | return (1); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2436 | if (ret > 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2437 | return (0); |
| 2438 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2439 | } |
| 2440 | |
| 2441 | /** |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2442 | * xmlRelaxNGSchemaFacetCheck: |
| 2443 | * @data: data needed for the library |
| 2444 | * @type: the type name |
| 2445 | * @facet: the facet name |
| 2446 | * @val: the facet value |
| 2447 | * @strval: the string value |
| 2448 | * @value: the value to check |
| 2449 | * |
| 2450 | * Function provided by a type library to check a value facet |
| 2451 | * |
| 2452 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2453 | */ |
| 2454 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2455 | xmlRelaxNGSchemaFacetCheck(void *data ATTRIBUTE_UNUSED, |
| 2456 | const xmlChar * type, const xmlChar * facetname, |
| 2457 | const xmlChar * val, const xmlChar * strval, |
| 2458 | void *value) |
| 2459 | { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2460 | xmlSchemaFacetPtr facet; |
| 2461 | xmlSchemaTypePtr typ; |
| 2462 | int ret; |
| 2463 | |
| 2464 | if ((type == NULL) || (strval == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2465 | return (-1); |
| 2466 | typ = xmlSchemaGetPredefinedType(type, |
| 2467 | BAD_CAST |
| 2468 | "http://www.w3.org/2001/XMLSchema"); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2469 | if (typ == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2470 | return (-1); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2471 | |
| 2472 | facet = xmlSchemaNewFacet(); |
| 2473 | if (facet == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2474 | return (-1); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2475 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2476 | if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2477 | facet->type = XML_SCHEMA_FACET_MININCLUSIVE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2478 | } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2479 | facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2480 | } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2481 | facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2482 | } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2483 | facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2484 | } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2485 | facet->type = XML_SCHEMA_FACET_TOTALDIGITS; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2486 | } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2487 | facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2488 | } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2489 | facet->type = XML_SCHEMA_FACET_PATTERN; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2490 | } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2491 | facet->type = XML_SCHEMA_FACET_ENUMERATION; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2492 | } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2493 | facet->type = XML_SCHEMA_FACET_WHITESPACE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2494 | } else if (xmlStrEqual(facetname, BAD_CAST "length")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2495 | facet->type = XML_SCHEMA_FACET_LENGTH; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2496 | } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2497 | facet->type = XML_SCHEMA_FACET_MAXLENGTH; |
| 2498 | } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) { |
| 2499 | facet->type = XML_SCHEMA_FACET_MINLENGTH; |
| 2500 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2501 | xmlSchemaFreeFacet(facet); |
| 2502 | return (-1); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2503 | } |
| 2504 | facet->value = xmlStrdup(val); |
| 2505 | ret = xmlSchemaCheckFacet(facet, typ, NULL, type); |
| 2506 | if (ret != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2507 | xmlSchemaFreeFacet(facet); |
| 2508 | return (-1); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2509 | } |
| 2510 | ret = xmlSchemaValidateFacet(typ, facet, strval, value); |
| 2511 | xmlSchemaFreeFacet(facet); |
| 2512 | if (ret != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2513 | return (-1); |
| 2514 | return (0); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2515 | } |
| 2516 | |
| 2517 | /** |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2518 | * xmlRelaxNGSchemaFreeValue: |
| 2519 | * @data: data needed for the library |
| 2520 | * @value: the value to free |
| 2521 | * |
| 2522 | * Function provided by a type library to free a Schemas value |
| 2523 | * |
| 2524 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2525 | */ |
| 2526 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2527 | xmlRelaxNGSchemaFreeValue(void *data ATTRIBUTE_UNUSED, void *value) |
| 2528 | { |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2529 | xmlSchemaFreeValue(value); |
| 2530 | } |
| 2531 | |
| 2532 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2533 | * xmlRelaxNGSchemaTypeCompare: |
| 2534 | * @data: data needed for the library |
| 2535 | * @type: the type name |
| 2536 | * @value1: the first value |
| 2537 | * @value2: the second value |
| 2538 | * |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2539 | * Compare two values for equality accordingly a type from the W3C XMLSchema |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2540 | * Datatype library. |
| 2541 | * |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2542 | * Returns 1 if equal, 0 if no and -1 in case of error. |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2543 | */ |
| 2544 | static int |
| 2545 | xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2546 | const xmlChar * type, |
| 2547 | const xmlChar * value1, |
| 2548 | xmlNodePtr ctxt1, |
| 2549 | void *comp1, |
| 2550 | const xmlChar * value2, xmlNodePtr ctxt2) |
| 2551 | { |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2552 | int ret; |
| 2553 | xmlSchemaTypePtr typ; |
| 2554 | xmlSchemaValPtr res1 = NULL, res2 = NULL; |
| 2555 | |
| 2556 | if ((type == NULL) || (value1 == NULL) || (value2 == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2557 | return (-1); |
| 2558 | typ = xmlSchemaGetPredefinedType(type, |
| 2559 | BAD_CAST |
| 2560 | "http://www.w3.org/2001/XMLSchema"); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2561 | if (typ == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2562 | return (-1); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2563 | if (comp1 == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2564 | ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1); |
| 2565 | if (ret != 0) |
| 2566 | return (-1); |
| 2567 | if (res1 == NULL) |
| 2568 | return (-1); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2569 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2570 | res1 = (xmlSchemaValPtr) comp1; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2571 | } |
| 2572 | ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2573 | if (ret != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2574 | xmlSchemaFreeValue(res1); |
| 2575 | return (-1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2576 | } |
| 2577 | if (res1 == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2578 | xmlSchemaFreeValue(res1); |
| 2579 | return (-1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2580 | } |
| 2581 | ret = xmlSchemaCompareValues(res1, res2); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2582 | if (res1 != (xmlSchemaValPtr) comp1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2583 | xmlSchemaFreeValue(res1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2584 | xmlSchemaFreeValue(res2); |
| 2585 | if (ret == -2) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2586 | return (-1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2587 | if (ret == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2588 | return (1); |
| 2589 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2590 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2591 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2592 | /** |
| 2593 | * xmlRelaxNGDefaultTypeHave: |
| 2594 | * @data: data needed for the library |
| 2595 | * @type: the type name |
| 2596 | * |
| 2597 | * Check if the given type is provided by |
| 2598 | * the default datatype library. |
| 2599 | * |
| 2600 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2601 | */ |
| 2602 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2603 | xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED, |
| 2604 | const xmlChar * type) |
| 2605 | { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2606 | if (type == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2607 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2608 | if (xmlStrEqual(type, BAD_CAST "string")) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2609 | return (1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2610 | if (xmlStrEqual(type, BAD_CAST "token")) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2611 | return (1); |
| 2612 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2613 | } |
| 2614 | |
| 2615 | /** |
| 2616 | * xmlRelaxNGDefaultTypeCheck: |
| 2617 | * @data: data needed for the library |
| 2618 | * @type: the type name |
| 2619 | * @value: the value to check |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2620 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2621 | * |
| 2622 | * Check if the given type and value are validated by |
| 2623 | * the default datatype library. |
| 2624 | * |
| 2625 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2626 | */ |
| 2627 | static int |
| 2628 | xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2629 | const xmlChar * type ATTRIBUTE_UNUSED, |
| 2630 | const xmlChar * value ATTRIBUTE_UNUSED, |
| 2631 | void **result ATTRIBUTE_UNUSED, |
| 2632 | xmlNodePtr node ATTRIBUTE_UNUSED) |
| 2633 | { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2634 | if (value == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2635 | return (-1); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2636 | if (xmlStrEqual(type, BAD_CAST "string")) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2637 | return (1); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2638 | if (xmlStrEqual(type, BAD_CAST "token")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2639 | return (1); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2640 | } |
| 2641 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2642 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2643 | } |
| 2644 | |
| 2645 | /** |
| 2646 | * xmlRelaxNGDefaultTypeCompare: |
| 2647 | * @data: data needed for the library |
| 2648 | * @type: the type name |
| 2649 | * @value1: the first value |
| 2650 | * @value2: the second value |
| 2651 | * |
| 2652 | * Compare two values accordingly a type from the default |
| 2653 | * datatype library. |
| 2654 | * |
| 2655 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2656 | */ |
| 2657 | static int |
| 2658 | xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2659 | const xmlChar * type, |
| 2660 | const xmlChar * value1, |
| 2661 | xmlNodePtr ctxt1 ATTRIBUTE_UNUSED, |
| 2662 | void *comp1 ATTRIBUTE_UNUSED, |
| 2663 | const xmlChar * value2, |
| 2664 | xmlNodePtr ctxt2 ATTRIBUTE_UNUSED) |
| 2665 | { |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2666 | int ret = -1; |
| 2667 | |
| 2668 | if (xmlStrEqual(type, BAD_CAST "string")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2669 | ret = xmlStrEqual(value1, value2); |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2670 | } else if (xmlStrEqual(type, BAD_CAST "token")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2671 | if (!xmlStrEqual(value1, value2)) { |
| 2672 | xmlChar *nval, *nvalue; |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2673 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2674 | /* |
| 2675 | * TODO: trivial optimizations are possible by |
| 2676 | * computing at compile-time |
| 2677 | */ |
| 2678 | nval = xmlRelaxNGNormalize(NULL, value1); |
| 2679 | nvalue = xmlRelaxNGNormalize(NULL, value2); |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2680 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2681 | if ((nval == NULL) || (nvalue == NULL)) |
| 2682 | ret = -1; |
| 2683 | else if (xmlStrEqual(nval, nvalue)) |
| 2684 | ret = 1; |
| 2685 | else |
| 2686 | ret = 0; |
| 2687 | if (nval != NULL) |
| 2688 | xmlFree(nval); |
| 2689 | if (nvalue != NULL) |
| 2690 | xmlFree(nvalue); |
| 2691 | } else |
| 2692 | ret = 1; |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2693 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2694 | return (ret); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2695 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2696 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2697 | static int xmlRelaxNGTypeInitialized = 0; |
| 2698 | static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL; |
| 2699 | |
| 2700 | /** |
| 2701 | * xmlRelaxNGFreeTypeLibrary: |
| 2702 | * @lib: the type library structure |
| 2703 | * @namespace: the URI bound to the library |
| 2704 | * |
| 2705 | * Free the structure associated to the type library |
| 2706 | */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2707 | static void |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2708 | xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2709 | const xmlChar * namespace ATTRIBUTE_UNUSED) |
| 2710 | { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2711 | if (lib == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2712 | return; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2713 | if (lib->namespace != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2714 | xmlFree((xmlChar *) lib->namespace); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2715 | xmlFree(lib); |
| 2716 | } |
| 2717 | |
| 2718 | /** |
| 2719 | * xmlRelaxNGRegisterTypeLibrary: |
| 2720 | * @namespace: the URI bound to the library |
| 2721 | * @data: data associated to the library |
| 2722 | * @have: the provide function |
| 2723 | * @check: the checking function |
| 2724 | * @comp: the comparison function |
| 2725 | * |
| 2726 | * Register a new type library |
| 2727 | * |
| 2728 | * Returns 0 in case of success and -1 in case of error. |
| 2729 | */ |
| 2730 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2731 | xmlRelaxNGRegisterTypeLibrary(const xmlChar * namespace, void *data, |
| 2732 | xmlRelaxNGTypeHave have, |
| 2733 | xmlRelaxNGTypeCheck check, |
| 2734 | xmlRelaxNGTypeCompare comp, |
| 2735 | xmlRelaxNGFacetCheck facet, |
| 2736 | xmlRelaxNGTypeFree freef) |
| 2737 | { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2738 | xmlRelaxNGTypeLibraryPtr lib; |
| 2739 | int ret; |
| 2740 | |
| 2741 | if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2742 | (check == NULL) || (comp == NULL)) |
| 2743 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2744 | if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2745 | xmlGenericError(xmlGenericErrorContext, |
| 2746 | "Relax-NG types library '%s' already registered\n", |
| 2747 | namespace); |
| 2748 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2749 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2750 | lib = |
| 2751 | (xmlRelaxNGTypeLibraryPtr) |
| 2752 | xmlMalloc(sizeof(xmlRelaxNGTypeLibrary)); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2753 | if (lib == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2754 | xmlRngVErrMemory(NULL, "adding types library\n"); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2755 | return (-1); |
| 2756 | } |
| 2757 | memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary)); |
| 2758 | lib->namespace = xmlStrdup(namespace); |
| 2759 | lib->data = data; |
| 2760 | lib->have = have; |
| 2761 | lib->comp = comp; |
| 2762 | lib->check = check; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2763 | lib->facet = facet; |
| 2764 | lib->freef = freef; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2765 | ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib); |
| 2766 | if (ret < 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2767 | xmlGenericError(xmlGenericErrorContext, |
| 2768 | "Relax-NG types library failed to register '%s'\n", |
| 2769 | namespace); |
| 2770 | xmlRelaxNGFreeTypeLibrary(lib, namespace); |
| 2771 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2772 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2773 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2774 | } |
| 2775 | |
| 2776 | /** |
| 2777 | * xmlRelaxNGInitTypes: |
| 2778 | * |
| 2779 | * Initilize the default type libraries. |
| 2780 | * |
| 2781 | * Returns 0 in case of success and -1 in case of error. |
| 2782 | */ |
| 2783 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2784 | xmlRelaxNGInitTypes(void) |
| 2785 | { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2786 | if (xmlRelaxNGTypeInitialized != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2787 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2788 | xmlRelaxNGRegisteredTypes = xmlHashCreate(10); |
| 2789 | if (xmlRelaxNGRegisteredTypes == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2790 | xmlGenericError(xmlGenericErrorContext, |
| 2791 | "Failed to allocate sh table for Relax-NG types\n"); |
| 2792 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2793 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2794 | xmlRelaxNGRegisterTypeLibrary(BAD_CAST |
| 2795 | "http://www.w3.org/2001/XMLSchema-datatypes", |
| 2796 | NULL, xmlRelaxNGSchemaTypeHave, |
| 2797 | xmlRelaxNGSchemaTypeCheck, |
| 2798 | xmlRelaxNGSchemaTypeCompare, |
| 2799 | xmlRelaxNGSchemaFacetCheck, |
| 2800 | xmlRelaxNGSchemaFreeValue); |
| 2801 | xmlRelaxNGRegisterTypeLibrary(xmlRelaxNGNs, NULL, |
| 2802 | xmlRelaxNGDefaultTypeHave, |
| 2803 | xmlRelaxNGDefaultTypeCheck, |
| 2804 | xmlRelaxNGDefaultTypeCompare, NULL, |
| 2805 | NULL); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2806 | xmlRelaxNGTypeInitialized = 1; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2807 | return (0); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2808 | } |
| 2809 | |
| 2810 | /** |
| 2811 | * xmlRelaxNGCleanupTypes: |
| 2812 | * |
| 2813 | * Cleanup the default Schemas type library associated to RelaxNG |
| 2814 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2815 | void |
| 2816 | xmlRelaxNGCleanupTypes(void) |
| 2817 | { |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 2818 | xmlSchemaCleanupTypes(); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2819 | if (xmlRelaxNGTypeInitialized == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2820 | return; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2821 | xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2822 | xmlRelaxNGFreeTypeLibrary); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2823 | xmlRelaxNGTypeInitialized = 0; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2824 | } |
| 2825 | |
| 2826 | /************************************************************************ |
| 2827 | * * |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2828 | * Compiling element content into regexp * |
| 2829 | * * |
| 2830 | * Sometime the element content can be compiled into a pure regexp, * |
| 2831 | * This allows a faster execution and streamability at that level * |
| 2832 | * * |
| 2833 | ************************************************************************/ |
| 2834 | |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2835 | static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, |
| 2836 | xmlRelaxNGDefinePtr def); |
| 2837 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2838 | /** |
| 2839 | * xmlRelaxNGIsCompileable: |
| 2840 | * @define: the definition to check |
| 2841 | * |
| 2842 | * Check if a definition is nullable. |
| 2843 | * |
| 2844 | * Returns 1 if yes, 0 if no and -1 in case of error |
| 2845 | */ |
| 2846 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2847 | xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def) |
| 2848 | { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2849 | int ret = -1; |
| 2850 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2851 | if (def == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2852 | return (-1); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2853 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2854 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 2855 | (def->dflags & IS_COMPILABLE)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2856 | return (1); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2857 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 2858 | (def->dflags & IS_NOT_COMPILABLE)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2859 | return (0); |
| 2860 | switch (def->type) { |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2861 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2862 | ret = xmlRelaxNGIsCompileable(def->content); |
| 2863 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2864 | case XML_RELAXNG_TEXT: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2865 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2866 | ret = 1; |
| 2867 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2868 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2869 | /* |
| 2870 | * Check if the element content is compileable |
| 2871 | */ |
| 2872 | if (((def->dflags & IS_NOT_COMPILABLE) == 0) && |
| 2873 | ((def->dflags & IS_COMPILABLE) == 0)) { |
| 2874 | xmlRelaxNGDefinePtr list; |
| 2875 | |
| 2876 | list = def->content; |
| 2877 | while (list != NULL) { |
| 2878 | ret = xmlRelaxNGIsCompileable(list); |
| 2879 | if (ret != 1) |
| 2880 | break; |
| 2881 | list = list->next; |
| 2882 | } |
| 2883 | if (ret == 0) |
| 2884 | def->dflags |= IS_NOT_COMPILABLE; |
| 2885 | if (ret == 1) |
| 2886 | def->dflags |= IS_COMPILABLE; |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2887 | #ifdef DEBUG_COMPILE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2888 | if (ret == 1) { |
| 2889 | xmlGenericError(xmlGenericErrorContext, |
| 2890 | "element content for %s is compilable\n", |
| 2891 | def->name); |
| 2892 | } else if (ret == 0) { |
| 2893 | xmlGenericError(xmlGenericErrorContext, |
| 2894 | "element content for %s is not compilable\n", |
| 2895 | def->name); |
| 2896 | } else { |
| 2897 | xmlGenericError(xmlGenericErrorContext, |
| 2898 | "Problem in RelaxNGIsCompileable for element %s\n", |
| 2899 | def->name); |
| 2900 | } |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2901 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2902 | } |
| 2903 | /* |
| 2904 | * All elements return a compileable status unless they |
| 2905 | * are generic like anyName |
| 2906 | */ |
| 2907 | if ((def->nameClass != NULL) || (def->name == NULL)) |
| 2908 | ret = 0; |
| 2909 | else |
| 2910 | ret = 1; |
| 2911 | return (ret); |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2912 | case XML_RELAXNG_REF: |
| 2913 | case XML_RELAXNG_EXTERNALREF: |
| 2914 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2915 | if (def->depth == -20) { |
| 2916 | return (1); |
| 2917 | } else { |
| 2918 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2919 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2920 | def->depth = -20; |
| 2921 | list = def->content; |
| 2922 | while (list != NULL) { |
| 2923 | ret = xmlRelaxNGIsCompileable(list); |
| 2924 | if (ret != 1) |
| 2925 | break; |
| 2926 | list = list->next; |
| 2927 | } |
| 2928 | } |
| 2929 | break; |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2930 | case XML_RELAXNG_START: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2931 | case XML_RELAXNG_OPTIONAL: |
| 2932 | case XML_RELAXNG_ZEROORMORE: |
| 2933 | case XML_RELAXNG_ONEORMORE: |
| 2934 | case XML_RELAXNG_CHOICE: |
| 2935 | case XML_RELAXNG_GROUP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2936 | case XML_RELAXNG_DEF:{ |
| 2937 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2938 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2939 | list = def->content; |
| 2940 | while (list != NULL) { |
| 2941 | ret = xmlRelaxNGIsCompileable(list); |
| 2942 | if (ret != 1) |
| 2943 | break; |
| 2944 | list = list->next; |
| 2945 | } |
| 2946 | break; |
| 2947 | } |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2948 | case XML_RELAXNG_EXCEPT: |
| 2949 | case XML_RELAXNG_ATTRIBUTE: |
| 2950 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2951 | case XML_RELAXNG_DATATYPE: |
| 2952 | case XML_RELAXNG_LIST: |
| 2953 | case XML_RELAXNG_PARAM: |
| 2954 | case XML_RELAXNG_VALUE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2955 | ret = 0; |
| 2956 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2957 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2958 | ret = -1; |
| 2959 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2960 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2961 | if (ret == 0) |
| 2962 | def->dflags |= IS_NOT_COMPILABLE; |
| 2963 | if (ret == 1) |
| 2964 | def->dflags |= IS_COMPILABLE; |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2965 | #ifdef DEBUG_COMPILE |
| 2966 | if (ret == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2967 | xmlGenericError(xmlGenericErrorContext, |
| 2968 | "RelaxNGIsCompileable %s : true\n", |
| 2969 | xmlRelaxNGDefName(def)); |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2970 | } else if (ret == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2971 | xmlGenericError(xmlGenericErrorContext, |
| 2972 | "RelaxNGIsCompileable %s : false\n", |
| 2973 | xmlRelaxNGDefName(def)); |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2974 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2975 | xmlGenericError(xmlGenericErrorContext, |
| 2976 | "Problem in RelaxNGIsCompileable %s\n", |
| 2977 | xmlRelaxNGDefName(def)); |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2978 | } |
| 2979 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2980 | return (ret); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2981 | } |
| 2982 | |
| 2983 | /** |
| 2984 | * xmlRelaxNGCompile: |
| 2985 | * ctxt: the RelaxNG parser context |
| 2986 | * @define: the definition tree to compile |
| 2987 | * |
| 2988 | * Compile the set of definitions, it works recursively, till the |
| 2989 | * element boundaries, where it tries to compile the content if possible |
| 2990 | * |
| 2991 | * Returns 0 if success and -1 in case of error |
| 2992 | */ |
| 2993 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2994 | xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) |
| 2995 | { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2996 | int ret = 0; |
| 2997 | xmlRelaxNGDefinePtr list; |
| 2998 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2999 | if ((ctxt == NULL) || (def == NULL)) |
| 3000 | return (-1); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3001 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3002 | switch (def->type) { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3003 | case XML_RELAXNG_START: |
| 3004 | if ((xmlRelaxNGIsCompileable(def) == 1) && (def->depth != -25)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3005 | xmlAutomataPtr oldam = ctxt->am; |
| 3006 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3007 | |
| 3008 | def->depth = -25; |
| 3009 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3010 | list = def->content; |
| 3011 | ctxt->am = xmlNewAutomata(); |
| 3012 | if (ctxt->am == NULL) |
| 3013 | return (-1); |
| 3014 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
| 3015 | while (list != NULL) { |
| 3016 | xmlRelaxNGCompile(ctxt, list); |
| 3017 | list = list->next; |
| 3018 | } |
| 3019 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
| 3020 | def->contModel = xmlAutomataCompile(ctxt->am); |
| 3021 | xmlRegexpIsDeterminist(def->contModel); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3022 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3023 | xmlFreeAutomata(ctxt->am); |
| 3024 | ctxt->state = oldstate; |
| 3025 | ctxt->am = oldam; |
| 3026 | } |
| 3027 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3028 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3029 | if ((ctxt->am != NULL) && (def->name != NULL)) { |
| 3030 | ctxt->state = xmlAutomataNewTransition2(ctxt->am, |
| 3031 | ctxt->state, NULL, |
| 3032 | def->name, def->ns, |
| 3033 | def); |
| 3034 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3035 | if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3036 | xmlAutomataPtr oldam = ctxt->am; |
| 3037 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3038 | |
| 3039 | def->depth = -25; |
| 3040 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3041 | list = def->content; |
| 3042 | ctxt->am = xmlNewAutomata(); |
| 3043 | if (ctxt->am == NULL) |
| 3044 | return (-1); |
| 3045 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
| 3046 | while (list != NULL) { |
| 3047 | xmlRelaxNGCompile(ctxt, list); |
| 3048 | list = list->next; |
| 3049 | } |
| 3050 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
| 3051 | def->contModel = xmlAutomataCompile(ctxt->am); |
| 3052 | if (!xmlRegexpIsDeterminist(def->contModel)) { |
| 3053 | /* |
| 3054 | * we can only use the automata if it is determinist |
| 3055 | */ |
| 3056 | xmlRegFreeRegexp(def->contModel); |
| 3057 | def->contModel = NULL; |
| 3058 | } |
| 3059 | xmlFreeAutomata(ctxt->am); |
| 3060 | ctxt->state = oldstate; |
| 3061 | ctxt->am = oldam; |
| 3062 | } else { |
| 3063 | xmlAutomataPtr oldam = ctxt->am; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3064 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3065 | /* |
| 3066 | * we can't build the content model for this element content |
| 3067 | * but it still might be possible to build it for some of its |
| 3068 | * children, recurse. |
| 3069 | */ |
| 3070 | ret = xmlRelaxNGTryCompile(ctxt, def); |
| 3071 | ctxt->am = oldam; |
| 3072 | } |
| 3073 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3074 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3075 | ret = xmlRelaxNGCompile(ctxt, def->content); |
| 3076 | break; |
| 3077 | case XML_RELAXNG_OPTIONAL:{ |
| 3078 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3079 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3080 | xmlRelaxNGCompile(ctxt, def->content); |
| 3081 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
| 3082 | break; |
| 3083 | } |
| 3084 | case XML_RELAXNG_ZEROORMORE:{ |
| 3085 | xmlAutomataStatePtr oldstate; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3086 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3087 | ctxt->state = |
| 3088 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 3089 | oldstate = ctxt->state; |
| 3090 | list = def->content; |
| 3091 | while (list != NULL) { |
| 3092 | xmlRelaxNGCompile(ctxt, list); |
| 3093 | list = list->next; |
| 3094 | } |
| 3095 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate); |
| 3096 | ctxt->state = |
| 3097 | xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 3098 | break; |
| 3099 | } |
| 3100 | case XML_RELAXNG_ONEORMORE:{ |
| 3101 | xmlAutomataStatePtr oldstate; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3102 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3103 | list = def->content; |
| 3104 | while (list != NULL) { |
| 3105 | xmlRelaxNGCompile(ctxt, list); |
| 3106 | list = list->next; |
| 3107 | } |
| 3108 | oldstate = ctxt->state; |
| 3109 | list = def->content; |
| 3110 | while (list != NULL) { |
| 3111 | xmlRelaxNGCompile(ctxt, list); |
| 3112 | list = list->next; |
| 3113 | } |
| 3114 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate); |
| 3115 | ctxt->state = |
| 3116 | xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 3117 | break; |
| 3118 | } |
| 3119 | case XML_RELAXNG_CHOICE:{ |
| 3120 | xmlAutomataStatePtr target = NULL; |
| 3121 | xmlAutomataStatePtr oldstate = ctxt->state; |
| 3122 | |
| 3123 | list = def->content; |
| 3124 | while (list != NULL) { |
| 3125 | ctxt->state = oldstate; |
| 3126 | ret = xmlRelaxNGCompile(ctxt, list); |
| 3127 | if (ret != 0) |
| 3128 | break; |
| 3129 | if (target == NULL) |
| 3130 | target = ctxt->state; |
| 3131 | else { |
| 3132 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, |
| 3133 | target); |
| 3134 | } |
| 3135 | list = list->next; |
| 3136 | } |
| 3137 | ctxt->state = target; |
| 3138 | |
| 3139 | break; |
| 3140 | } |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3141 | case XML_RELAXNG_REF: |
| 3142 | case XML_RELAXNG_EXTERNALREF: |
| 3143 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3144 | case XML_RELAXNG_GROUP: |
| 3145 | case XML_RELAXNG_DEF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3146 | list = def->content; |
| 3147 | while (list != NULL) { |
| 3148 | ret = xmlRelaxNGCompile(ctxt, list); |
| 3149 | if (ret != 0) |
| 3150 | break; |
| 3151 | list = list->next; |
| 3152 | } |
| 3153 | break; |
| 3154 | case XML_RELAXNG_TEXT:{ |
| 3155 | xmlAutomataStatePtr oldstate; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3156 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3157 | ctxt->state = |
| 3158 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 3159 | oldstate = ctxt->state; |
| 3160 | xmlRelaxNGCompile(ctxt, def->content); |
| 3161 | xmlAutomataNewTransition(ctxt->am, ctxt->state, |
| 3162 | ctxt->state, BAD_CAST "#text", |
| 3163 | NULL); |
| 3164 | ctxt->state = |
| 3165 | xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 3166 | break; |
| 3167 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3168 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3169 | ctxt->state = |
| 3170 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 3171 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3172 | case XML_RELAXNG_EXCEPT: |
| 3173 | case XML_RELAXNG_ATTRIBUTE: |
| 3174 | case XML_RELAXNG_INTERLEAVE: |
| 3175 | case XML_RELAXNG_NOT_ALLOWED: |
| 3176 | case XML_RELAXNG_DATATYPE: |
| 3177 | case XML_RELAXNG_LIST: |
| 3178 | case XML_RELAXNG_PARAM: |
| 3179 | case XML_RELAXNG_VALUE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3180 | /* This should not happen and generate an internal error */ |
| 3181 | fprintf(stderr, "RNG internal error trying to compile %s\n", |
| 3182 | xmlRelaxNGDefName(def)); |
| 3183 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3184 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3185 | return (ret); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3186 | } |
| 3187 | |
| 3188 | /** |
| 3189 | * xmlRelaxNGTryCompile: |
| 3190 | * ctxt: the RelaxNG parser context |
| 3191 | * @define: the definition tree to compile |
| 3192 | * |
| 3193 | * Try to compile the set of definitions, it works recursively, |
| 3194 | * possibly ignoring parts which cannot be compiled. |
| 3195 | * |
| 3196 | * Returns 0 if success and -1 in case of error |
| 3197 | */ |
| 3198 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3199 | xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) |
| 3200 | { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3201 | int ret = 0; |
| 3202 | xmlRelaxNGDefinePtr list; |
| 3203 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3204 | if ((ctxt == NULL) || (def == NULL)) |
| 3205 | return (-1); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3206 | |
| 3207 | if ((def->type == XML_RELAXNG_START) || |
| 3208 | (def->type == XML_RELAXNG_ELEMENT)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3209 | ret = xmlRelaxNGIsCompileable(def); |
| 3210 | if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) { |
| 3211 | ctxt->am = NULL; |
| 3212 | ret = xmlRelaxNGCompile(ctxt, def); |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3213 | #ifdef DEBUG_PROGRESSIVE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3214 | if (ret == 0) { |
| 3215 | if (def->type == XML_RELAXNG_START) |
| 3216 | xmlGenericError(xmlGenericErrorContext, |
| 3217 | "compiled the start\n"); |
| 3218 | else |
| 3219 | xmlGenericError(xmlGenericErrorContext, |
| 3220 | "compiled element %s\n", def->name); |
| 3221 | } else { |
| 3222 | if (def->type == XML_RELAXNG_START) |
| 3223 | xmlGenericError(xmlGenericErrorContext, |
| 3224 | "failed to compile the start\n"); |
| 3225 | else |
| 3226 | xmlGenericError(xmlGenericErrorContext, |
| 3227 | "failed to compile element %s\n", |
| 3228 | def->name); |
| 3229 | } |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3230 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3231 | return (ret); |
| 3232 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3233 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3234 | switch (def->type) { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3235 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3236 | ret = xmlRelaxNGTryCompile(ctxt, def->content); |
| 3237 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3238 | case XML_RELAXNG_TEXT: |
| 3239 | case XML_RELAXNG_DATATYPE: |
| 3240 | case XML_RELAXNG_LIST: |
| 3241 | case XML_RELAXNG_PARAM: |
| 3242 | case XML_RELAXNG_VALUE: |
| 3243 | case XML_RELAXNG_EMPTY: |
| 3244 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3245 | ret = 0; |
| 3246 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3247 | case XML_RELAXNG_OPTIONAL: |
| 3248 | case XML_RELAXNG_ZEROORMORE: |
| 3249 | case XML_RELAXNG_ONEORMORE: |
| 3250 | case XML_RELAXNG_CHOICE: |
| 3251 | case XML_RELAXNG_GROUP: |
| 3252 | case XML_RELAXNG_DEF: |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3253 | case XML_RELAXNG_START: |
| 3254 | case XML_RELAXNG_REF: |
| 3255 | case XML_RELAXNG_EXTERNALREF: |
| 3256 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3257 | list = def->content; |
| 3258 | while (list != NULL) { |
| 3259 | ret = xmlRelaxNGTryCompile(ctxt, list); |
| 3260 | if (ret != 0) |
| 3261 | break; |
| 3262 | list = list->next; |
| 3263 | } |
| 3264 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3265 | case XML_RELAXNG_EXCEPT: |
| 3266 | case XML_RELAXNG_ATTRIBUTE: |
| 3267 | case XML_RELAXNG_INTERLEAVE: |
| 3268 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3269 | ret = 0; |
| 3270 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3271 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3272 | return (ret); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 3273 | } |
| 3274 | |
| 3275 | /************************************************************************ |
| 3276 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3277 | * Parsing functions * |
| 3278 | * * |
| 3279 | ************************************************************************/ |
| 3280 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3281 | static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr |
| 3282 | ctxt, xmlNodePtr node); |
| 3283 | static xmlRelaxNGDefinePtr xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr |
| 3284 | ctxt, xmlNodePtr node); |
| 3285 | static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr |
| 3286 | ctxt, xmlNodePtr nodes, |
| 3287 | int group); |
| 3288 | static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr |
| 3289 | ctxt, xmlNodePtr node); |
| 3290 | static xmlRelaxNGPtr xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, |
| 3291 | xmlNodePtr node); |
| 3292 | static int xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt, |
| 3293 | xmlNodePtr nodes); |
| 3294 | static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr |
| 3295 | ctxt, xmlNodePtr node, |
| 3296 | xmlRelaxNGDefinePtr |
| 3297 | def); |
| 3298 | static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr |
| 3299 | ctxt, xmlNodePtr nodes); |
| 3300 | static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 3301 | xmlRelaxNGDefinePtr define, |
| 3302 | xmlNodePtr elem); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3303 | |
| 3304 | |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 3305 | #define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content)) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3306 | |
| 3307 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3308 | * xmlRelaxNGIsNullable: |
| 3309 | * @define: the definition to verify |
| 3310 | * |
| 3311 | * Check if a definition is nullable. |
| 3312 | * |
| 3313 | * Returns 1 if yes, 0 if no and -1 in case of error |
| 3314 | */ |
| 3315 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3316 | xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define) |
| 3317 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3318 | int ret; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3319 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3320 | if (define == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3321 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3322 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3323 | if (define->dflags & IS_NULLABLE) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3324 | return (1); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3325 | if (define->dflags & IS_NOT_NULLABLE) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3326 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3327 | switch (define->type) { |
| 3328 | case XML_RELAXNG_EMPTY: |
| 3329 | case XML_RELAXNG_TEXT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3330 | ret = 1; |
| 3331 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3332 | case XML_RELAXNG_NOOP: |
| 3333 | case XML_RELAXNG_DEF: |
| 3334 | case XML_RELAXNG_REF: |
| 3335 | case XML_RELAXNG_EXTERNALREF: |
| 3336 | case XML_RELAXNG_PARENTREF: |
| 3337 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3338 | ret = xmlRelaxNGIsNullable(define->content); |
| 3339 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3340 | case XML_RELAXNG_EXCEPT: |
| 3341 | case XML_RELAXNG_NOT_ALLOWED: |
| 3342 | case XML_RELAXNG_ELEMENT: |
| 3343 | case XML_RELAXNG_DATATYPE: |
| 3344 | case XML_RELAXNG_PARAM: |
| 3345 | case XML_RELAXNG_VALUE: |
| 3346 | case XML_RELAXNG_LIST: |
| 3347 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3348 | ret = 0; |
| 3349 | break; |
| 3350 | case XML_RELAXNG_CHOICE:{ |
| 3351 | xmlRelaxNGDefinePtr list = define->content; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3352 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3353 | while (list != NULL) { |
| 3354 | ret = xmlRelaxNGIsNullable(list); |
| 3355 | if (ret != 0) |
| 3356 | goto done; |
| 3357 | list = list->next; |
| 3358 | } |
| 3359 | ret = 0; |
| 3360 | break; |
| 3361 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3362 | case XML_RELAXNG_START: |
| 3363 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3364 | case XML_RELAXNG_GROUP:{ |
| 3365 | xmlRelaxNGDefinePtr list = define->content; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3366 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3367 | while (list != NULL) { |
| 3368 | ret = xmlRelaxNGIsNullable(list); |
| 3369 | if (ret != 1) |
| 3370 | goto done; |
| 3371 | list = list->next; |
| 3372 | } |
| 3373 | return (1); |
| 3374 | } |
| 3375 | default: |
| 3376 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3377 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3378 | done: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3379 | if (ret == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3380 | define->dflags |= IS_NOT_NULLABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3381 | if (ret == 1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3382 | define->dflags |= IS_NULLABLE; |
| 3383 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3384 | } |
| 3385 | |
| 3386 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3387 | * xmlRelaxNGIsBlank: |
| 3388 | * @str: a string |
| 3389 | * |
| 3390 | * Check if a string is ignorable c.f. 4.2. Whitespace |
| 3391 | * |
| 3392 | * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise |
| 3393 | */ |
| 3394 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3395 | xmlRelaxNGIsBlank(xmlChar * str) |
| 3396 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3397 | if (str == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3398 | return (1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3399 | while (*str != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3400 | if (!(IS_BLANK_CH(*str))) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3401 | return (0); |
| 3402 | str++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3403 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3404 | return (1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3405 | } |
| 3406 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3407 | /** |
| 3408 | * xmlRelaxNGGetDataTypeLibrary: |
| 3409 | * @ctxt: a Relax-NG parser context |
| 3410 | * @node: the current data or value element |
| 3411 | * |
| 3412 | * Applies algorithm from 4.3. datatypeLibrary attribute |
| 3413 | * |
| 3414 | * Returns the datatypeLibary value or NULL if not found |
| 3415 | */ |
| 3416 | static xmlChar * |
| 3417 | xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3418 | xmlNodePtr node) |
| 3419 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3420 | xmlChar *ret, *escape; |
| 3421 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3422 | if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3423 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
| 3424 | if (ret != NULL) { |
| 3425 | if (ret[0] == 0) { |
| 3426 | xmlFree(ret); |
| 3427 | return (NULL); |
| 3428 | } |
| 3429 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
| 3430 | if (escape == NULL) { |
| 3431 | return (ret); |
| 3432 | } |
| 3433 | xmlFree(ret); |
| 3434 | return (escape); |
| 3435 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3436 | } |
| 3437 | node = node->parent; |
| 3438 | while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3439 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
| 3440 | if (ret != NULL) { |
| 3441 | if (ret[0] == 0) { |
| 3442 | xmlFree(ret); |
| 3443 | return (NULL); |
| 3444 | } |
| 3445 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
| 3446 | if (escape == NULL) { |
| 3447 | return (ret); |
| 3448 | } |
| 3449 | xmlFree(ret); |
| 3450 | return (escape); |
| 3451 | } |
| 3452 | node = node->parent; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3453 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3454 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3455 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3456 | |
| 3457 | /** |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3458 | * xmlRelaxNGParseValue: |
| 3459 | * @ctxt: a Relax-NG parser context |
| 3460 | * @node: the data node. |
| 3461 | * |
| 3462 | * parse the content of a RelaxNG value node. |
| 3463 | * |
| 3464 | * Returns the definition pointer or NULL in case of error |
| 3465 | */ |
| 3466 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3467 | xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 3468 | { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3469 | xmlRelaxNGDefinePtr def = NULL; |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 3470 | xmlRelaxNGTypeLibraryPtr lib = NULL; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3471 | xmlChar *type; |
| 3472 | xmlChar *library; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3473 | int success = 0; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3474 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3475 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3476 | if (def == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3477 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3478 | def->type = XML_RELAXNG_VALUE; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3479 | |
| 3480 | type = xmlGetProp(node, BAD_CAST "type"); |
| 3481 | if (type != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3482 | xmlRelaxNGNormExtSpace(type); |
| 3483 | if (xmlValidateNCName(type, 0)) { |
| 3484 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE, |
| 3485 | "value type '%s' is not an NCName\n", type, NULL); |
| 3486 | } |
| 3487 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
| 3488 | if (library == NULL) |
| 3489 | library = |
| 3490 | xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3491 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3492 | def->name = type; |
| 3493 | def->ns = library; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3494 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3495 | lib = (xmlRelaxNGTypeLibraryPtr) |
| 3496 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
| 3497 | if (lib == NULL) { |
| 3498 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB, |
| 3499 | "Use of unregistered type library '%s'\n", library, |
| 3500 | NULL); |
| 3501 | def->data = NULL; |
| 3502 | } else { |
| 3503 | def->data = lib; |
| 3504 | if (lib->have == NULL) { |
| 3505 | xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB, |
| 3506 | "Internal error with type library '%s': no 'have'\n", |
| 3507 | library, NULL); |
| 3508 | } else { |
| 3509 | success = lib->have(lib->data, def->name); |
| 3510 | if (success != 1) { |
| 3511 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND, |
| 3512 | "Error type '%s' is not exported by type library '%s'\n", |
| 3513 | def->name, library); |
| 3514 | } |
| 3515 | } |
| 3516 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3517 | } |
| 3518 | if (node->children == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3519 | def->value = xmlStrdup(BAD_CAST ""); |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 3520 | } else if (((node->children->type != XML_TEXT_NODE) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3521 | (node->children->type != XML_CDATA_SECTION_NODE)) || |
| 3522 | (node->children->next != NULL)) { |
| 3523 | xmlRngPErr(ctxt, node, XML_RNGP_TEXT_EXPECTED, |
| 3524 | "Expecting a single text value for <value>content\n", |
| 3525 | NULL, NULL); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3526 | } else if (def != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3527 | def->value = xmlNodeGetContent(node); |
| 3528 | if (def->value == NULL) { |
| 3529 | xmlRngPErr(ctxt, node, XML_RNGP_VALUE_NO_CONTENT, |
| 3530 | "Element <value> has no content\n", NULL, NULL); |
| 3531 | } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) { |
| 3532 | void *val = NULL; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3533 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3534 | success = |
| 3535 | lib->check(lib->data, def->name, def->value, &val, node); |
| 3536 | if (success != 1) { |
| 3537 | xmlRngPErr(ctxt, node, XML_RNGP_INVALID_VALUE, |
| 3538 | "Value '%s' is not acceptable for type '%s'\n", |
| 3539 | def->value, def->name); |
| 3540 | } else { |
| 3541 | if (val != NULL) |
| 3542 | def->attrs = val; |
| 3543 | } |
| 3544 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3545 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3546 | return (def); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3547 | } |
| 3548 | |
| 3549 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3550 | * xmlRelaxNGParseData: |
| 3551 | * @ctxt: a Relax-NG parser context |
| 3552 | * @node: the data node. |
| 3553 | * |
| 3554 | * parse the content of a RelaxNG data node. |
| 3555 | * |
| 3556 | * Returns the definition pointer or NULL in case of error |
| 3557 | */ |
| 3558 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3559 | xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 3560 | { |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3561 | xmlRelaxNGDefinePtr def = NULL, except, last = NULL; |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 3562 | xmlRelaxNGDefinePtr param, lastparam = NULL; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3563 | xmlRelaxNGTypeLibraryPtr lib; |
| 3564 | xmlChar *type; |
| 3565 | xmlChar *library; |
| 3566 | xmlNodePtr content; |
| 3567 | int tmp; |
| 3568 | |
| 3569 | type = xmlGetProp(node, BAD_CAST "type"); |
| 3570 | if (type == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3571 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_MISSING, "data has no type\n", NULL, |
| 3572 | NULL); |
| 3573 | return (NULL); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3574 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3575 | xmlRelaxNGNormExtSpace(type); |
| 3576 | if (xmlValidateNCName(type, 0)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3577 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE, |
| 3578 | "data type '%s' is not an NCName\n", type, NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3579 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3580 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
| 3581 | if (library == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3582 | library = |
| 3583 | xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3584 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3585 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3586 | if (def == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3587 | xmlFree(type); |
| 3588 | return (NULL); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3589 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3590 | def->type = XML_RELAXNG_DATATYPE; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3591 | def->name = type; |
| 3592 | def->ns = library; |
| 3593 | |
| 3594 | lib = (xmlRelaxNGTypeLibraryPtr) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3595 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3596 | if (lib == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3597 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB, |
| 3598 | "Use of unregistered type library '%s'\n", library, |
| 3599 | NULL); |
| 3600 | def->data = NULL; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3601 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3602 | def->data = lib; |
| 3603 | if (lib->have == NULL) { |
| 3604 | xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB, |
| 3605 | "Internal error with type library '%s': no 'have'\n", |
| 3606 | library, NULL); |
| 3607 | } else { |
| 3608 | tmp = lib->have(lib->data, def->name); |
| 3609 | if (tmp != 1) { |
| 3610 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND, |
| 3611 | "Error type '%s' is not exported by type library '%s'\n", |
| 3612 | def->name, library); |
| 3613 | } else |
| 3614 | if ((xmlStrEqual |
| 3615 | (library, |
| 3616 | BAD_CAST |
| 3617 | "http://www.w3.org/2001/XMLSchema-datatypes")) |
| 3618 | && ((xmlStrEqual(def->name, BAD_CAST "IDREF")) |
| 3619 | || (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) { |
| 3620 | ctxt->idref = 1; |
| 3621 | } |
| 3622 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3623 | } |
| 3624 | content = node->children; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3625 | |
| 3626 | /* |
| 3627 | * Handle optional params |
| 3628 | */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3629 | while (content != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3630 | if (!xmlStrEqual(content->name, BAD_CAST "param")) |
| 3631 | break; |
| 3632 | if (xmlStrEqual(library, |
| 3633 | BAD_CAST "http://relaxng.org/ns/structure/1.0")) { |
| 3634 | xmlRngPErr(ctxt, node, XML_RNGP_PARAM_FORBIDDEN, |
| 3635 | "Type library '%s' does not allow type parameters\n", |
| 3636 | library, NULL); |
| 3637 | content = content->next; |
| 3638 | while ((content != NULL) && |
| 3639 | (xmlStrEqual(content->name, BAD_CAST "param"))) |
| 3640 | content = content->next; |
| 3641 | } else { |
| 3642 | param = xmlRelaxNGNewDefine(ctxt, node); |
| 3643 | if (param != NULL) { |
| 3644 | param->type = XML_RELAXNG_PARAM; |
| 3645 | param->name = xmlGetProp(content, BAD_CAST "name"); |
| 3646 | if (param->name == NULL) { |
| 3647 | xmlRngPErr(ctxt, node, XML_RNGP_PARAM_NAME_MISSING, |
| 3648 | "param has no name\n", NULL, NULL); |
| 3649 | } |
| 3650 | param->value = xmlNodeGetContent(content); |
| 3651 | if (lastparam == NULL) { |
| 3652 | def->attrs = lastparam = param; |
| 3653 | } else { |
| 3654 | lastparam->next = param; |
| 3655 | lastparam = param; |
| 3656 | } |
| 3657 | if (lib != NULL) { |
| 3658 | } |
| 3659 | } |
| 3660 | content = content->next; |
| 3661 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3662 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3663 | /* |
| 3664 | * Handle optional except |
| 3665 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3666 | if ((content != NULL) |
| 3667 | && (xmlStrEqual(content->name, BAD_CAST "except"))) { |
| 3668 | xmlNodePtr child; |
| 3669 | xmlRelaxNGDefinePtr tmp2, last2 = NULL; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3670 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3671 | except = xmlRelaxNGNewDefine(ctxt, node); |
| 3672 | if (except == NULL) { |
| 3673 | return (def); |
| 3674 | } |
| 3675 | except->type = XML_RELAXNG_EXCEPT; |
| 3676 | child = content->children; |
| 3677 | if (last == NULL) { |
| 3678 | def->content = except; |
| 3679 | } else { |
| 3680 | last->next = except; |
| 3681 | } |
| 3682 | if (child == NULL) { |
| 3683 | xmlRngPErr(ctxt, content, XML_RNGP_EXCEPT_NO_CONTENT, |
| 3684 | "except has no content\n", NULL, NULL); |
| 3685 | } |
| 3686 | while (child != NULL) { |
| 3687 | tmp2 = xmlRelaxNGParsePattern(ctxt, child); |
| 3688 | if (tmp2 != NULL) { |
| 3689 | if (last2 == NULL) { |
| 3690 | except->content = last2 = tmp2; |
| 3691 | } else { |
| 3692 | last2->next = tmp2; |
| 3693 | last2 = tmp2; |
| 3694 | } |
| 3695 | } |
| 3696 | child = child->next; |
| 3697 | } |
| 3698 | content = content->next; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3699 | } |
| 3700 | /* |
| 3701 | * Check there is no unhandled data |
| 3702 | */ |
| 3703 | if (content != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3704 | xmlRngPErr(ctxt, content, XML_RNGP_DATA_CONTENT, |
| 3705 | "Element data has unexpected content %s\n", |
| 3706 | content->name, NULL); |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3707 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3708 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3709 | return (def); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3710 | } |
| 3711 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3712 | static const xmlChar *invalidName = BAD_CAST "\1"; |
| 3713 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3714 | /** |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3715 | * xmlRelaxNGCompareNameClasses: |
| 3716 | * @defs1: the first element/attribute defs |
| 3717 | * @defs2: the second element/attribute defs |
| 3718 | * @name: the restriction on the name |
| 3719 | * @ns: the restriction on the namespace |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3720 | * |
| 3721 | * Compare the 2 lists of element definitions. The comparison is |
| 3722 | * that if both lists do not accept the same QNames, it returns 1 |
| 3723 | * If the 2 lists can accept the same QName the comparison returns 0 |
| 3724 | * |
| 3725 | * Returns 1 disttinct, 0 if equal |
| 3726 | */ |
| 3727 | static int |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3728 | xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3729 | xmlRelaxNGDefinePtr def2) |
| 3730 | { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3731 | int ret = 1; |
| 3732 | xmlNode node; |
| 3733 | xmlNs ns; |
| 3734 | xmlRelaxNGValidCtxt ctxt; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3735 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3736 | ctxt.flags = FLAGS_IGNORABLE; |
| 3737 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 3738 | memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt)); |
| 3739 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3740 | if ((def1->type == XML_RELAXNG_ELEMENT) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3741 | (def1->type == XML_RELAXNG_ATTRIBUTE)) { |
| 3742 | if (def2->type == XML_RELAXNG_TEXT) |
| 3743 | return (1); |
| 3744 | if (def1->name != NULL) { |
| 3745 | node.name = def1->name; |
| 3746 | } else { |
| 3747 | node.name = invalidName; |
| 3748 | } |
| 3749 | node.ns = &ns; |
| 3750 | if (def1->ns != NULL) { |
| 3751 | if (def1->ns[0] == 0) { |
| 3752 | node.ns = NULL; |
| 3753 | } else { |
| 3754 | ns.href = def1->ns; |
| 3755 | } |
| 3756 | } else { |
| 3757 | ns.href = invalidName; |
| 3758 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3759 | if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3760 | if (def1->nameClass != NULL) { |
| 3761 | ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2); |
| 3762 | } else { |
| 3763 | ret = 0; |
| 3764 | } |
| 3765 | } else { |
| 3766 | ret = 1; |
| 3767 | } |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3768 | } else if (def1->type == XML_RELAXNG_TEXT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3769 | if (def2->type == XML_RELAXNG_TEXT) |
| 3770 | return (0); |
| 3771 | return (1); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3772 | } else if (def1->type == XML_RELAXNG_EXCEPT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3773 | TODO ret = 0; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3774 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3775 | TODO ret = 0; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3776 | } |
| 3777 | if (ret == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3778 | return (ret); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3779 | if ((def2->type == XML_RELAXNG_ELEMENT) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3780 | (def2->type == XML_RELAXNG_ATTRIBUTE)) { |
| 3781 | if (def2->name != NULL) { |
| 3782 | node.name = def2->name; |
| 3783 | } else { |
| 3784 | node.name = invalidName; |
| 3785 | } |
| 3786 | node.ns = &ns; |
| 3787 | if (def2->ns != NULL) { |
| 3788 | if (def2->ns[0] == 0) { |
| 3789 | node.ns = NULL; |
| 3790 | } else { |
| 3791 | ns.href = def2->ns; |
| 3792 | } |
| 3793 | } else { |
| 3794 | ns.href = invalidName; |
| 3795 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3796 | if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3797 | if (def2->nameClass != NULL) { |
| 3798 | ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1); |
| 3799 | } else { |
| 3800 | ret = 0; |
| 3801 | } |
| 3802 | } else { |
| 3803 | ret = 1; |
| 3804 | } |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3805 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3806 | TODO ret = 0; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3807 | } |
| 3808 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3809 | return (ret); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3810 | } |
| 3811 | |
| 3812 | /** |
| 3813 | * xmlRelaxNGCompareElemDefLists: |
| 3814 | * @ctxt: a Relax-NG parser context |
| 3815 | * @defs1: the first list of element/attribute defs |
| 3816 | * @defs2: the second list of element/attribute defs |
| 3817 | * |
| 3818 | * Compare the 2 lists of element or attribute definitions. The comparison |
| 3819 | * is that if both lists do not accept the same QNames, it returns 1 |
| 3820 | * If the 2 lists can accept the same QName the comparison returns 0 |
| 3821 | * |
| 3822 | * Returns 1 disttinct, 0 if equal |
| 3823 | */ |
| 3824 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3825 | xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt |
| 3826 | ATTRIBUTE_UNUSED, xmlRelaxNGDefinePtr * def1, |
| 3827 | xmlRelaxNGDefinePtr * def2) |
| 3828 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3829 | xmlRelaxNGDefinePtr *basedef2 = def2; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3830 | |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3831 | if ((def1 == NULL) || (def2 == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3832 | return (1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3833 | if ((*def1 == NULL) || (*def2 == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3834 | return (1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3835 | while (*def1 != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3836 | while ((*def2) != NULL) { |
| 3837 | if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0) |
| 3838 | return (0); |
| 3839 | def2++; |
| 3840 | } |
| 3841 | def2 = basedef2; |
| 3842 | def1++; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3843 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3844 | return (1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3845 | } |
| 3846 | |
| 3847 | /** |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3848 | * xmlRelaxNGGenerateAttributes: |
| 3849 | * @ctxt: a Relax-NG parser context |
| 3850 | * @def: the definition definition |
| 3851 | * |
| 3852 | * Check if the definition can only generate attributes |
| 3853 | * |
| 3854 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 3855 | */ |
| 3856 | static int |
| 3857 | xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3858 | xmlRelaxNGDefinePtr def) |
| 3859 | { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3860 | xmlRelaxNGDefinePtr parent, cur, tmp; |
| 3861 | |
| 3862 | /* |
| 3863 | * Don't run that check in case of error. Infinite recursion |
| 3864 | * becomes possible. |
| 3865 | */ |
| 3866 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3867 | return (-1); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3868 | |
| 3869 | parent = NULL; |
| 3870 | cur = def; |
| 3871 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3872 | if ((cur->type == XML_RELAXNG_ELEMENT) || |
| 3873 | (cur->type == XML_RELAXNG_TEXT) || |
| 3874 | (cur->type == XML_RELAXNG_DATATYPE) || |
| 3875 | (cur->type == XML_RELAXNG_PARAM) || |
| 3876 | (cur->type == XML_RELAXNG_LIST) || |
| 3877 | (cur->type == XML_RELAXNG_VALUE) || |
| 3878 | (cur->type == XML_RELAXNG_EMPTY)) |
| 3879 | return (0); |
| 3880 | if ((cur->type == XML_RELAXNG_CHOICE) || |
| 3881 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
| 3882 | (cur->type == XML_RELAXNG_GROUP) || |
| 3883 | (cur->type == XML_RELAXNG_ONEORMORE) || |
| 3884 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
| 3885 | (cur->type == XML_RELAXNG_OPTIONAL) || |
| 3886 | (cur->type == XML_RELAXNG_PARENTREF) || |
| 3887 | (cur->type == XML_RELAXNG_EXTERNALREF) || |
| 3888 | (cur->type == XML_RELAXNG_REF) || |
| 3889 | (cur->type == XML_RELAXNG_DEF)) { |
| 3890 | if (cur->content != NULL) { |
| 3891 | parent = cur; |
| 3892 | cur = cur->content; |
| 3893 | tmp = cur; |
| 3894 | while (tmp != NULL) { |
| 3895 | tmp->parent = parent; |
| 3896 | tmp = tmp->next; |
| 3897 | } |
| 3898 | continue; |
| 3899 | } |
| 3900 | } |
| 3901 | if (cur == def) |
| 3902 | break; |
| 3903 | if (cur->next != NULL) { |
| 3904 | cur = cur->next; |
| 3905 | continue; |
| 3906 | } |
| 3907 | do { |
| 3908 | cur = cur->parent; |
| 3909 | if (cur == NULL) |
| 3910 | break; |
| 3911 | if (cur == def) |
| 3912 | return (1); |
| 3913 | if (cur->next != NULL) { |
| 3914 | cur = cur->next; |
| 3915 | break; |
| 3916 | } |
| 3917 | } while (cur != NULL); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3918 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3919 | return (1); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3920 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3921 | |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3922 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3923 | * xmlRelaxNGGetElements: |
| 3924 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3925 | * @def: the definition definition |
| 3926 | * @eora: gather elements (0) or attributes (1) |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3927 | * |
| 3928 | * Compute the list of top elements a definition can generate |
| 3929 | * |
| 3930 | * Returns a list of elements or NULL if none was found. |
| 3931 | */ |
| 3932 | static xmlRelaxNGDefinePtr * |
| 3933 | xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3934 | xmlRelaxNGDefinePtr def, int eora) |
| 3935 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3936 | xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3937 | int len = 0; |
| 3938 | int max = 0; |
| 3939 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3940 | /* |
| 3941 | * Don't run that check in case of error. Infinite recursion |
| 3942 | * becomes possible. |
| 3943 | */ |
| 3944 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3945 | return (NULL); |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3946 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3947 | parent = NULL; |
| 3948 | cur = def; |
| 3949 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3950 | if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) || |
| 3951 | (cur->type == XML_RELAXNG_TEXT))) || |
| 3952 | ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) { |
| 3953 | if (ret == NULL) { |
| 3954 | max = 10; |
| 3955 | ret = (xmlRelaxNGDefinePtr *) |
| 3956 | xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
| 3957 | if (ret == NULL) { |
| 3958 | xmlRngPErrMemory(ctxt, "getting element list\n"); |
| 3959 | return (NULL); |
| 3960 | } |
| 3961 | } else if (max <= len) { |
| 3962 | max *= 2; |
| 3963 | ret = |
| 3964 | xmlRealloc(ret, |
| 3965 | (max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
| 3966 | if (ret == NULL) { |
| 3967 | xmlRngPErrMemory(ctxt, "getting element list\n"); |
| 3968 | return (NULL); |
| 3969 | } |
| 3970 | } |
| 3971 | ret[len++] = cur; |
| 3972 | ret[len] = NULL; |
| 3973 | } else if ((cur->type == XML_RELAXNG_CHOICE) || |
| 3974 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
| 3975 | (cur->type == XML_RELAXNG_GROUP) || |
| 3976 | (cur->type == XML_RELAXNG_ONEORMORE) || |
| 3977 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
| 3978 | (cur->type == XML_RELAXNG_OPTIONAL) || |
| 3979 | (cur->type == XML_RELAXNG_PARENTREF) || |
| 3980 | (cur->type == XML_RELAXNG_REF) || |
| 3981 | (cur->type == XML_RELAXNG_DEF)) { |
| 3982 | /* |
| 3983 | * Don't go within elements or attributes or string values. |
| 3984 | * Just gather the element top list |
| 3985 | */ |
| 3986 | if (cur->content != NULL) { |
| 3987 | parent = cur; |
| 3988 | cur = cur->content; |
| 3989 | tmp = cur; |
| 3990 | while (tmp != NULL) { |
| 3991 | tmp->parent = parent; |
| 3992 | tmp = tmp->next; |
| 3993 | } |
| 3994 | continue; |
| 3995 | } |
| 3996 | } |
| 3997 | if (cur == def) |
| 3998 | break; |
| 3999 | if (cur->next != NULL) { |
| 4000 | cur = cur->next; |
| 4001 | continue; |
| 4002 | } |
| 4003 | do { |
| 4004 | cur = cur->parent; |
| 4005 | if (cur == NULL) |
| 4006 | break; |
| 4007 | if (cur == def) |
| 4008 | return (ret); |
| 4009 | if (cur->next != NULL) { |
| 4010 | cur = cur->next; |
| 4011 | break; |
| 4012 | } |
| 4013 | } while (cur != NULL); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4014 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4015 | return (ret); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4016 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4017 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4018 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4019 | * xmlRelaxNGCheckChoiceDeterminism: |
| 4020 | * @ctxt: a Relax-NG parser context |
| 4021 | * @def: the choice definition |
| 4022 | * |
| 4023 | * Also used to find indeterministic pattern in choice |
| 4024 | */ |
| 4025 | static void |
| 4026 | xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4027 | xmlRelaxNGDefinePtr def) |
| 4028 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4029 | xmlRelaxNGDefinePtr **list; |
| 4030 | xmlRelaxNGDefinePtr cur; |
| 4031 | int nbchild = 0, i, j, ret; |
| 4032 | int is_nullable = 0; |
| 4033 | int is_indeterminist = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4034 | xmlHashTablePtr triage = NULL; |
| 4035 | int is_triable = 1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4036 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4037 | if ((def == NULL) || (def->type != XML_RELAXNG_CHOICE)) |
| 4038 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4039 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4040 | if (def->dflags & IS_PROCESSED) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4041 | return; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4042 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4043 | /* |
| 4044 | * Don't run that check in case of error. Infinite recursion |
| 4045 | * becomes possible. |
| 4046 | */ |
| 4047 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4048 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4049 | |
| 4050 | is_nullable = xmlRelaxNGIsNullable(def); |
| 4051 | |
| 4052 | cur = def->content; |
| 4053 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4054 | nbchild++; |
| 4055 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4056 | } |
| 4057 | |
| 4058 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4059 | sizeof(xmlRelaxNGDefinePtr |
| 4060 | *)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4061 | if (list == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4062 | xmlRngPErrMemory(ctxt, "building choice\n"); |
| 4063 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4064 | } |
| 4065 | i = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4066 | /* |
| 4067 | * a bit strong but safe |
| 4068 | */ |
| 4069 | if (is_nullable == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4070 | triage = xmlHashCreate(10); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4071 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4072 | is_triable = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4073 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4074 | cur = def->content; |
| 4075 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4076 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 0); |
| 4077 | if ((list[i] == NULL) || (list[i][0] == NULL)) { |
| 4078 | is_triable = 0; |
| 4079 | } else if (is_triable == 1) { |
| 4080 | xmlRelaxNGDefinePtr *tmp; |
| 4081 | int res; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4082 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4083 | tmp = list[i]; |
| 4084 | while ((*tmp != NULL) && (is_triable == 1)) { |
| 4085 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
| 4086 | res = xmlHashAddEntry2(triage, |
| 4087 | BAD_CAST "#text", NULL, |
| 4088 | (void *) cur); |
| 4089 | if (res != 0) |
| 4090 | is_triable = -1; |
| 4091 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
| 4092 | ((*tmp)->name != NULL)) { |
| 4093 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4094 | res = xmlHashAddEntry2(triage, |
| 4095 | (*tmp)->name, NULL, |
| 4096 | (void *) cur); |
| 4097 | else |
| 4098 | res = xmlHashAddEntry2(triage, |
| 4099 | (*tmp)->name, (*tmp)->ns, |
| 4100 | (void *) cur); |
| 4101 | if (res != 0) |
| 4102 | is_triable = -1; |
| 4103 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
| 4104 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4105 | res = xmlHashAddEntry2(triage, |
| 4106 | BAD_CAST "#any", NULL, |
| 4107 | (void *) cur); |
| 4108 | else |
| 4109 | res = xmlHashAddEntry2(triage, |
| 4110 | BAD_CAST "#any", (*tmp)->ns, |
| 4111 | (void *) cur); |
| 4112 | if (res != 0) |
| 4113 | is_triable = -1; |
| 4114 | } else { |
| 4115 | is_triable = -1; |
| 4116 | } |
| 4117 | tmp++; |
| 4118 | } |
| 4119 | } |
| 4120 | i++; |
| 4121 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4122 | } |
| 4123 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4124 | for (i = 0; i < nbchild; i++) { |
| 4125 | if (list[i] == NULL) |
| 4126 | continue; |
| 4127 | for (j = 0; j < i; j++) { |
| 4128 | if (list[j] == NULL) |
| 4129 | continue; |
| 4130 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
| 4131 | if (ret == 0) { |
| 4132 | is_indeterminist = 1; |
| 4133 | } |
| 4134 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4135 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4136 | for (i = 0; i < nbchild; i++) { |
| 4137 | if (list[i] != NULL) |
| 4138 | xmlFree(list[i]); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4139 | } |
| 4140 | |
| 4141 | xmlFree(list); |
| 4142 | if (is_indeterminist) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4143 | def->dflags |= IS_INDETERMINIST; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4144 | } |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4145 | if (is_triable == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4146 | def->dflags |= IS_TRIABLE; |
| 4147 | def->data = triage; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4148 | } else if (triage != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4149 | xmlHashFree(triage, NULL); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4150 | } |
| 4151 | def->dflags |= IS_PROCESSED; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4152 | } |
| 4153 | |
| 4154 | /** |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4155 | * xmlRelaxNGCheckGroupAttrs: |
| 4156 | * @ctxt: a Relax-NG parser context |
| 4157 | * @def: the group definition |
| 4158 | * |
| 4159 | * Detects violations of rule 7.3 |
| 4160 | */ |
| 4161 | static void |
| 4162 | xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4163 | xmlRelaxNGDefinePtr def) |
| 4164 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4165 | xmlRelaxNGDefinePtr **list; |
| 4166 | xmlRelaxNGDefinePtr cur; |
| 4167 | int nbchild = 0, i, j, ret; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4168 | |
| 4169 | if ((def == NULL) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4170 | ((def->type != XML_RELAXNG_GROUP) && |
| 4171 | (def->type != XML_RELAXNG_ELEMENT))) |
| 4172 | return; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4173 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4174 | if (def->dflags & IS_PROCESSED) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4175 | return; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4176 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4177 | /* |
| 4178 | * Don't run that check in case of error. Infinite recursion |
| 4179 | * becomes possible. |
| 4180 | */ |
| 4181 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4182 | return; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4183 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4184 | cur = def->attrs; |
| 4185 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4186 | nbchild++; |
| 4187 | cur = cur->next; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4188 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4189 | cur = def->content; |
| 4190 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4191 | nbchild++; |
| 4192 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4193 | } |
| 4194 | |
| 4195 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4196 | sizeof(xmlRelaxNGDefinePtr |
| 4197 | *)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4198 | if (list == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4199 | xmlRngPErrMemory(ctxt, "building group\n"); |
| 4200 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4201 | } |
| 4202 | i = 0; |
| 4203 | cur = def->attrs; |
| 4204 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4205 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4206 | i++; |
| 4207 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4208 | } |
| 4209 | cur = def->content; |
| 4210 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4211 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4212 | i++; |
| 4213 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4214 | } |
| 4215 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4216 | for (i = 0; i < nbchild; i++) { |
| 4217 | if (list[i] == NULL) |
| 4218 | continue; |
| 4219 | for (j = 0; j < i; j++) { |
| 4220 | if (list[j] == NULL) |
| 4221 | continue; |
| 4222 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
| 4223 | if (ret == 0) { |
| 4224 | xmlRngPErr(ctxt, def->node, XML_RNGP_GROUP_ATTR_CONFLICT, |
| 4225 | "Attributes conflicts in group\n", NULL, NULL); |
| 4226 | } |
| 4227 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4228 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4229 | for (i = 0; i < nbchild; i++) { |
| 4230 | if (list[i] != NULL) |
| 4231 | xmlFree(list[i]); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4232 | } |
| 4233 | |
| 4234 | xmlFree(list); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4235 | def->dflags |= IS_PROCESSED; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4236 | } |
| 4237 | |
| 4238 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4239 | * xmlRelaxNGComputeInterleaves: |
| 4240 | * @def: the interleave definition |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4241 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4242 | * @name: the definition name |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4243 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4244 | * A lot of work for preprocessing interleave definitions |
| 4245 | * is potentially needed to get a decent execution speed at runtime |
| 4246 | * - trying to get a total order on the element nodes generated |
| 4247 | * by the interleaves, order the list of interleave definitions |
| 4248 | * following that order. |
| 4249 | * - if <text/> is used to handle mixed content, it is better to |
| 4250 | * flag this in the define and simplify the runtime checking |
| 4251 | * algorithm |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4252 | */ |
| 4253 | static void |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4254 | xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4255 | xmlRelaxNGParserCtxtPtr ctxt, |
| 4256 | xmlChar * name ATTRIBUTE_UNUSED) |
| 4257 | { |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4258 | xmlRelaxNGDefinePtr cur, *tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4259 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4260 | xmlRelaxNGPartitionPtr partitions = NULL; |
| 4261 | xmlRelaxNGInterleaveGroupPtr *groups = NULL; |
| 4262 | xmlRelaxNGInterleaveGroupPtr group; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4263 | int i, j, ret, res; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4264 | int nbgroups = 0; |
| 4265 | int nbchild = 0; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 4266 | int is_mixed = 0; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4267 | int is_determinist = 1; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4268 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4269 | /* |
| 4270 | * Don't run that check in case of error. Infinite recursion |
| 4271 | * becomes possible. |
| 4272 | */ |
| 4273 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4274 | return; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4275 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4276 | #ifdef DEBUG_INTERLEAVE |
| 4277 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4278 | "xmlRelaxNGComputeInterleaves(%s)\n", name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4279 | #endif |
| 4280 | cur = def->content; |
| 4281 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4282 | nbchild++; |
| 4283 | cur = cur->next; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4284 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4285 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4286 | #ifdef DEBUG_INTERLEAVE |
| 4287 | xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild); |
| 4288 | #endif |
| 4289 | groups = (xmlRelaxNGInterleaveGroupPtr *) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4290 | xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4291 | if (groups == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4292 | goto error; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4293 | cur = def->content; |
| 4294 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4295 | groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr) |
| 4296 | xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup)); |
| 4297 | if (groups[nbgroups] == NULL) |
| 4298 | goto error; |
| 4299 | if (cur->type == XML_RELAXNG_TEXT) |
| 4300 | is_mixed++; |
| 4301 | groups[nbgroups]->rule = cur; |
| 4302 | groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0); |
| 4303 | groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4304 | nbgroups++; |
| 4305 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4306 | } |
| 4307 | #ifdef DEBUG_INTERLEAVE |
| 4308 | xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups); |
| 4309 | #endif |
| 4310 | |
| 4311 | /* |
| 4312 | * Let's check that all rules makes a partitions according to 7.4 |
| 4313 | */ |
| 4314 | partitions = (xmlRelaxNGPartitionPtr) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4315 | xmlMalloc(sizeof(xmlRelaxNGPartition)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4316 | if (partitions == NULL) |
| 4317 | goto error; |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 4318 | memset(partitions, 0, sizeof(xmlRelaxNGPartition)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4319 | partitions->nbgroups = nbgroups; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4320 | partitions->triage = xmlHashCreate(nbgroups); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4321 | for (i = 0; i < nbgroups; i++) { |
| 4322 | group = groups[i]; |
| 4323 | for (j = i + 1; j < nbgroups; j++) { |
| 4324 | if (groups[j] == NULL) |
| 4325 | continue; |
| 4326 | |
| 4327 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs, |
| 4328 | groups[j]->defs); |
| 4329 | if (ret == 0) { |
| 4330 | xmlRngPErr(ctxt, def->node, XML_RNGP_ELEM_TEXT_CONFLICT, |
| 4331 | "Element or text conflicts in interleave\n", |
| 4332 | NULL, NULL); |
| 4333 | } |
| 4334 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs, |
| 4335 | groups[j]->attrs); |
| 4336 | if (ret == 0) { |
| 4337 | xmlRngPErr(ctxt, def->node, XML_RNGP_ATTR_CONFLICT, |
| 4338 | "Attributes conflicts in interleave\n", NULL, |
| 4339 | NULL); |
| 4340 | } |
| 4341 | } |
| 4342 | tmp = group->defs; |
| 4343 | if ((tmp != NULL) && (*tmp != NULL)) { |
| 4344 | while (*tmp != NULL) { |
| 4345 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
| 4346 | res = xmlHashAddEntry2(partitions->triage, |
| 4347 | BAD_CAST "#text", NULL, |
| 4348 | (void *) (long) (i + 1)); |
| 4349 | if (res != 0) |
| 4350 | is_determinist = -1; |
| 4351 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
| 4352 | ((*tmp)->name != NULL)) { |
| 4353 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4354 | res = xmlHashAddEntry2(partitions->triage, |
| 4355 | (*tmp)->name, NULL, |
| 4356 | (void *) (long) (i + 1)); |
| 4357 | else |
| 4358 | res = xmlHashAddEntry2(partitions->triage, |
| 4359 | (*tmp)->name, (*tmp)->ns, |
| 4360 | (void *) (long) (i + 1)); |
| 4361 | if (res != 0) |
| 4362 | is_determinist = -1; |
| 4363 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
| 4364 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4365 | res = xmlHashAddEntry2(partitions->triage, |
| 4366 | BAD_CAST "#any", NULL, |
| 4367 | (void *) (long) (i + 1)); |
| 4368 | else |
| 4369 | res = xmlHashAddEntry2(partitions->triage, |
| 4370 | BAD_CAST "#any", (*tmp)->ns, |
| 4371 | (void *) (long) (i + 1)); |
| 4372 | if ((*tmp)->nameClass != NULL) |
| 4373 | is_determinist = 2; |
| 4374 | if (res != 0) |
| 4375 | is_determinist = -1; |
| 4376 | } else { |
| 4377 | is_determinist = -1; |
| 4378 | } |
| 4379 | tmp++; |
| 4380 | } |
| 4381 | } else { |
| 4382 | is_determinist = 0; |
| 4383 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4384 | } |
| 4385 | partitions->groups = groups; |
| 4386 | |
| 4387 | /* |
| 4388 | * and save the partition list back in the def |
| 4389 | */ |
| 4390 | def->data = partitions; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 4391 | if (is_mixed != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4392 | def->dflags |= IS_MIXED; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4393 | if (is_determinist == 1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4394 | partitions->flags = IS_DETERMINIST; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4395 | if (is_determinist == 2) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4396 | partitions->flags = IS_DETERMINIST | IS_NEEDCHECK; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4397 | return; |
| 4398 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4399 | error: |
| 4400 | xmlRngPErrMemory(ctxt, "in interleave computation\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4401 | if (groups != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4402 | for (i = 0; i < nbgroups; i++) |
| 4403 | if (groups[i] != NULL) { |
| 4404 | if (groups[i]->defs != NULL) |
| 4405 | xmlFree(groups[i]->defs); |
| 4406 | xmlFree(groups[i]); |
| 4407 | } |
| 4408 | xmlFree(groups); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4409 | } |
| 4410 | xmlRelaxNGFreePartition(partitions); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4411 | } |
| 4412 | |
| 4413 | /** |
| 4414 | * xmlRelaxNGParseInterleave: |
| 4415 | * @ctxt: a Relax-NG parser context |
| 4416 | * @node: the data node. |
| 4417 | * |
| 4418 | * parse the content of a RelaxNG interleave node. |
| 4419 | * |
| 4420 | * Returns the definition pointer or NULL in case of error |
| 4421 | */ |
| 4422 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4423 | xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4424 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4425 | xmlRelaxNGDefinePtr def = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4426 | xmlRelaxNGDefinePtr last = NULL, cur; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4427 | xmlNodePtr child; |
| 4428 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4429 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4430 | if (def == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4431 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4432 | } |
| 4433 | def->type = XML_RELAXNG_INTERLEAVE; |
| 4434 | |
| 4435 | if (ctxt->interleaves == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4436 | ctxt->interleaves = xmlHashCreate(10); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4437 | if (ctxt->interleaves == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4438 | xmlRngPErrMemory(ctxt, "create interleaves\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4439 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4440 | char name[32]; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4441 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4442 | snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++); |
| 4443 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) { |
| 4444 | xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_ADD, |
| 4445 | "Failed to add %s to hash table\n", |
| 4446 | (const xmlChar *) name, NULL); |
| 4447 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4448 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4449 | child = node->children; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4450 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4451 | xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_NO_CONTENT, |
| 4452 | "Element interleave is empty\n", NULL, NULL); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 4453 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4454 | while (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4455 | if (IS_RELAXNG(child, "element")) { |
| 4456 | cur = xmlRelaxNGParseElement(ctxt, child); |
| 4457 | } else { |
| 4458 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 4459 | } |
| 4460 | if (cur != NULL) { |
| 4461 | cur->parent = def; |
| 4462 | if (last == NULL) { |
| 4463 | def->content = last = cur; |
| 4464 | } else { |
| 4465 | last->next = cur; |
| 4466 | last = cur; |
| 4467 | } |
| 4468 | } |
| 4469 | child = child->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4470 | } |
| 4471 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4472 | return (def); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4473 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4474 | |
| 4475 | /** |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4476 | * xmlRelaxNGParseInclude: |
| 4477 | * @ctxt: a Relax-NG parser context |
| 4478 | * @node: the include node |
| 4479 | * |
| 4480 | * Integrate the content of an include node in the current grammar |
| 4481 | * |
| 4482 | * Returns 0 in case of success or -1 in case of error |
| 4483 | */ |
| 4484 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4485 | xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4486 | { |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4487 | xmlRelaxNGIncludePtr incl; |
| 4488 | xmlNodePtr root; |
| 4489 | int ret = 0, tmp; |
| 4490 | |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 4491 | incl = node->psvi; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4492 | if (incl == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4493 | xmlRngPErr(ctxt, node, XML_RNGP_INCLUDE_EMPTY, |
| 4494 | "Include node has no data\n", NULL, NULL); |
| 4495 | return (-1); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4496 | } |
| 4497 | root = xmlDocGetRootElement(incl->doc); |
| 4498 | if (root == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4499 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, "Include document is empty\n", |
| 4500 | NULL, NULL); |
| 4501 | return (-1); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4502 | } |
| 4503 | if (!xmlStrEqual(root->name, BAD_CAST "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4504 | xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING, |
| 4505 | "Include document root is not a grammar\n", NULL, NULL); |
| 4506 | return (-1); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4507 | } |
| 4508 | |
| 4509 | /* |
| 4510 | * Merge the definition from both the include and the internal list |
| 4511 | */ |
| 4512 | if (root->children != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4513 | tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children); |
| 4514 | if (tmp != 0) |
| 4515 | ret = -1; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4516 | } |
| 4517 | if (node->children != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4518 | tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children); |
| 4519 | if (tmp != 0) |
| 4520 | ret = -1; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4521 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4522 | return (ret); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4523 | } |
| 4524 | |
| 4525 | /** |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4526 | * xmlRelaxNGParseDefine: |
| 4527 | * @ctxt: a Relax-NG parser context |
| 4528 | * @node: the define node |
| 4529 | * |
| 4530 | * parse the content of a RelaxNG define element node. |
| 4531 | * |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4532 | * Returns 0 in case of success or -1 in case of error |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4533 | */ |
| 4534 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4535 | xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4536 | { |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4537 | xmlChar *name; |
| 4538 | int ret = 0, tmp; |
| 4539 | xmlRelaxNGDefinePtr def; |
| 4540 | const xmlChar *olddefine; |
| 4541 | |
| 4542 | name = xmlGetProp(node, BAD_CAST "name"); |
| 4543 | if (name == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4544 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_NAME_MISSING, |
| 4545 | "define has no name\n", NULL, NULL); |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4546 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4547 | xmlRelaxNGNormExtSpace(name); |
| 4548 | if (xmlValidateNCName(name, 0)) { |
| 4549 | xmlRngPErr(ctxt, node, XML_RNGP_INVALID_DEFINE_NAME, |
| 4550 | "define name '%s' is not an NCName\n", name, NULL); |
| 4551 | } |
| 4552 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4553 | if (def == NULL) { |
| 4554 | xmlFree(name); |
| 4555 | return (-1); |
| 4556 | } |
| 4557 | def->type = XML_RELAXNG_DEF; |
| 4558 | def->name = name; |
| 4559 | if (node->children == NULL) { |
| 4560 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_EMPTY, |
| 4561 | "define has no children\n", NULL, NULL); |
| 4562 | } else { |
| 4563 | olddefine = ctxt->define; |
| 4564 | ctxt->define = name; |
| 4565 | def->content = |
| 4566 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4567 | ctxt->define = olddefine; |
| 4568 | } |
| 4569 | if (ctxt->grammar->defs == NULL) |
| 4570 | ctxt->grammar->defs = xmlHashCreate(10); |
| 4571 | if (ctxt->grammar->defs == NULL) { |
| 4572 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED, |
| 4573 | "Could not create definition hash\n", NULL, NULL); |
| 4574 | ret = -1; |
| 4575 | } else { |
| 4576 | tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def); |
| 4577 | if (tmp < 0) { |
| 4578 | xmlRelaxNGDefinePtr prev; |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 4579 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4580 | prev = xmlHashLookup(ctxt->grammar->defs, name); |
| 4581 | if (prev == NULL) { |
| 4582 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED, |
| 4583 | "Internal error on define aggregation of %s\n", |
| 4584 | name, NULL); |
| 4585 | ret = -1; |
| 4586 | } else { |
| 4587 | while (prev->nextHash != NULL) |
| 4588 | prev = prev->nextHash; |
| 4589 | prev->nextHash = def; |
| 4590 | } |
| 4591 | } |
| 4592 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4593 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4594 | return (ret); |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4595 | } |
| 4596 | |
| 4597 | /** |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4598 | * xmlRelaxNGProcessExternalRef: |
| 4599 | * @ctxt: the parser context |
| 4600 | * @node: the externlRef node |
| 4601 | * |
| 4602 | * Process and compile an externlRef node |
| 4603 | * |
| 4604 | * Returns the xmlRelaxNGDefinePtr or NULL in case of error |
| 4605 | */ |
| 4606 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4607 | xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4608 | { |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4609 | xmlRelaxNGDocumentPtr docu; |
| 4610 | xmlNodePtr root, tmp; |
| 4611 | xmlChar *ns; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4612 | int newNs = 0, oldflags; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4613 | xmlRelaxNGDefinePtr def; |
| 4614 | |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 4615 | docu = node->psvi; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4616 | if (docu != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4617 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4618 | if (def == NULL) |
| 4619 | return (NULL); |
| 4620 | def->type = XML_RELAXNG_EXTERNALREF; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4621 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4622 | if (docu->content == NULL) { |
| 4623 | /* |
| 4624 | * Then do the parsing for good |
| 4625 | */ |
| 4626 | root = xmlDocGetRootElement(docu->doc); |
| 4627 | if (root == NULL) { |
| 4628 | xmlRngPErr(ctxt, node, XML_RNGP_EXTERNALREF_EMTPY, |
| 4629 | "xmlRelaxNGParse: %s is empty\n", ctxt->URL, |
| 4630 | NULL); |
| 4631 | return (NULL); |
| 4632 | } |
| 4633 | /* |
| 4634 | * ns transmission rules |
| 4635 | */ |
| 4636 | ns = xmlGetProp(root, BAD_CAST "ns"); |
| 4637 | if (ns == NULL) { |
| 4638 | tmp = node; |
| 4639 | while ((tmp != NULL) && (tmp->type == XML_ELEMENT_NODE)) { |
| 4640 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 4641 | if (ns != NULL) { |
| 4642 | break; |
| 4643 | } |
| 4644 | tmp = tmp->parent; |
| 4645 | } |
| 4646 | if (ns != NULL) { |
| 4647 | xmlSetProp(root, BAD_CAST "ns", ns); |
| 4648 | newNs = 1; |
| 4649 | xmlFree(ns); |
| 4650 | } |
| 4651 | } else { |
| 4652 | xmlFree(ns); |
| 4653 | } |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4654 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4655 | /* |
| 4656 | * Parsing to get a precompiled schemas. |
| 4657 | */ |
| 4658 | oldflags = ctxt->flags; |
| 4659 | ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF; |
| 4660 | docu->schema = xmlRelaxNGParseDocument(ctxt, root); |
| 4661 | ctxt->flags = oldflags; |
| 4662 | if ((docu->schema != NULL) && |
| 4663 | (docu->schema->topgrammar != NULL)) { |
| 4664 | docu->content = docu->schema->topgrammar->start; |
| 4665 | } |
| 4666 | |
| 4667 | /* |
| 4668 | * the externalRef may be reused in a different ns context |
| 4669 | */ |
| 4670 | if (newNs == 1) { |
| 4671 | xmlUnsetProp(root, BAD_CAST "ns"); |
| 4672 | } |
| 4673 | } |
| 4674 | def->content = docu->content; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4675 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4676 | def = NULL; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4677 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4678 | return (def); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4679 | } |
| 4680 | |
| 4681 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4682 | * xmlRelaxNGParsePattern: |
| 4683 | * @ctxt: a Relax-NG parser context |
| 4684 | * @node: the pattern node. |
| 4685 | * |
| 4686 | * parse the content of a RelaxNG pattern node. |
| 4687 | * |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4688 | * Returns the definition pointer or NULL in case of error or if no |
| 4689 | * pattern is generated. |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4690 | */ |
| 4691 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4692 | xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4693 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4694 | xmlRelaxNGDefinePtr def = NULL; |
| 4695 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4696 | if (node == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4697 | return (NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4698 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4699 | if (IS_RELAXNG(node, "element")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4700 | def = xmlRelaxNGParseElement(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4701 | } else if (IS_RELAXNG(node, "attribute")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4702 | def = xmlRelaxNGParseAttribute(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4703 | } else if (IS_RELAXNG(node, "empty")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4704 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4705 | if (def == NULL) |
| 4706 | return (NULL); |
| 4707 | def->type = XML_RELAXNG_EMPTY; |
| 4708 | if (node->children != NULL) { |
| 4709 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_NOT_EMPTY, |
| 4710 | "empty: had a child node\n", NULL, NULL); |
| 4711 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4712 | } else if (IS_RELAXNG(node, "text")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4713 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4714 | if (def == NULL) |
| 4715 | return (NULL); |
| 4716 | def->type = XML_RELAXNG_TEXT; |
| 4717 | if (node->children != NULL) { |
| 4718 | xmlRngPErr(ctxt, node, XML_RNGP_TEXT_HAS_CHILD, |
| 4719 | "text: had a child node\n", NULL, NULL); |
| 4720 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4721 | } else if (IS_RELAXNG(node, "zeroOrMore")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4722 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4723 | if (def == NULL) |
| 4724 | return (NULL); |
| 4725 | def->type = XML_RELAXNG_ZEROORMORE; |
| 4726 | if (node->children == NULL) { |
| 4727 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4728 | "Element %s is empty\n", node->name, NULL); |
| 4729 | } else { |
| 4730 | def->content = |
| 4731 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4732 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4733 | } else if (IS_RELAXNG(node, "oneOrMore")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4734 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4735 | if (def == NULL) |
| 4736 | return (NULL); |
| 4737 | def->type = XML_RELAXNG_ONEORMORE; |
| 4738 | if (node->children == NULL) { |
| 4739 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4740 | "Element %s is empty\n", node->name, NULL); |
| 4741 | } else { |
| 4742 | def->content = |
| 4743 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4744 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4745 | } else if (IS_RELAXNG(node, "optional")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4746 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4747 | if (def == NULL) |
| 4748 | return (NULL); |
| 4749 | def->type = XML_RELAXNG_OPTIONAL; |
| 4750 | if (node->children == NULL) { |
| 4751 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4752 | "Element %s is empty\n", node->name, NULL); |
| 4753 | } else { |
| 4754 | def->content = |
| 4755 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4756 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4757 | } else if (IS_RELAXNG(node, "choice")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4758 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4759 | if (def == NULL) |
| 4760 | return (NULL); |
| 4761 | def->type = XML_RELAXNG_CHOICE; |
| 4762 | if (node->children == NULL) { |
| 4763 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4764 | "Element %s is empty\n", node->name, NULL); |
| 4765 | } else { |
| 4766 | def->content = |
| 4767 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4768 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4769 | } else if (IS_RELAXNG(node, "group")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4770 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4771 | if (def == NULL) |
| 4772 | return (NULL); |
| 4773 | def->type = XML_RELAXNG_GROUP; |
| 4774 | if (node->children == NULL) { |
| 4775 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4776 | "Element %s is empty\n", node->name, NULL); |
| 4777 | } else { |
| 4778 | def->content = |
| 4779 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4780 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4781 | } else if (IS_RELAXNG(node, "ref")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4782 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4783 | if (def == NULL) |
| 4784 | return (NULL); |
| 4785 | def->type = XML_RELAXNG_REF; |
| 4786 | def->name = xmlGetProp(node, BAD_CAST "name"); |
| 4787 | if (def->name == NULL) { |
| 4788 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NO_NAME, "ref has no name\n", |
| 4789 | NULL, NULL); |
| 4790 | } else { |
| 4791 | xmlRelaxNGNormExtSpace(def->name); |
| 4792 | if (xmlValidateNCName(def->name, 0)) { |
| 4793 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NAME_INVALID, |
| 4794 | "ref name '%s' is not an NCName\n", def->name, |
| 4795 | NULL); |
| 4796 | } |
| 4797 | } |
| 4798 | if (node->children != NULL) { |
| 4799 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NOT_EMPTY, "ref is not empty\n", |
| 4800 | NULL, NULL); |
| 4801 | } |
| 4802 | if (ctxt->grammar->refs == NULL) |
| 4803 | ctxt->grammar->refs = xmlHashCreate(10); |
| 4804 | if (ctxt->grammar->refs == NULL) { |
| 4805 | xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED, |
| 4806 | "Could not create references hash\n", NULL, NULL); |
| 4807 | def = NULL; |
| 4808 | } else { |
| 4809 | int tmp; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4810 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4811 | tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def); |
| 4812 | if (tmp < 0) { |
| 4813 | xmlRelaxNGDefinePtr prev; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4814 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4815 | prev = (xmlRelaxNGDefinePtr) |
| 4816 | xmlHashLookup(ctxt->grammar->refs, def->name); |
| 4817 | if (prev == NULL) { |
| 4818 | if (def->name != NULL) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 4819 | xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED, |
| 4820 | "Error refs definitions '%s'\n", |
| 4821 | def->name, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4822 | } else { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 4823 | xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED, |
| 4824 | "Error refs definitions\n", |
| 4825 | NULL, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4826 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4827 | def = NULL; |
| 4828 | } else { |
| 4829 | def->nextHash = prev->nextHash; |
| 4830 | prev->nextHash = def; |
| 4831 | } |
| 4832 | } |
| 4833 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 4834 | } else if (IS_RELAXNG(node, "data")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4835 | def = xmlRelaxNGParseData(ctxt, node); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 4836 | } else if (IS_RELAXNG(node, "value")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4837 | def = xmlRelaxNGParseValue(ctxt, node); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 4838 | } else if (IS_RELAXNG(node, "list")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4839 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4840 | if (def == NULL) |
| 4841 | return (NULL); |
| 4842 | def->type = XML_RELAXNG_LIST; |
| 4843 | if (node->children == NULL) { |
| 4844 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4845 | "Element %s is empty\n", node->name, NULL); |
| 4846 | } else { |
| 4847 | def->content = |
| 4848 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4849 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4850 | } else if (IS_RELAXNG(node, "interleave")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4851 | def = xmlRelaxNGParseInterleave(ctxt, node); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4852 | } else if (IS_RELAXNG(node, "externalRef")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4853 | def = xmlRelaxNGProcessExternalRef(ctxt, node); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4854 | } else if (IS_RELAXNG(node, "notAllowed")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4855 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4856 | if (def == NULL) |
| 4857 | return (NULL); |
| 4858 | def->type = XML_RELAXNG_NOT_ALLOWED; |
| 4859 | if (node->children != NULL) { |
| 4860 | xmlRngPErr(ctxt, node, XML_RNGP_NOTALLOWED_NOT_EMPTY, |
| 4861 | "xmlRelaxNGParse: notAllowed element is not empty\n", |
| 4862 | NULL, NULL); |
| 4863 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4864 | } else if (IS_RELAXNG(node, "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4865 | xmlRelaxNGGrammarPtr grammar, old; |
| 4866 | xmlRelaxNGGrammarPtr oldparent; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4867 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4868 | #ifdef DEBUG_GRAMMAR |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4869 | xmlGenericError(xmlGenericErrorContext, |
| 4870 | "Found <grammar> pattern\n"); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4871 | #endif |
| 4872 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4873 | oldparent = ctxt->parentgrammar; |
| 4874 | old = ctxt->grammar; |
| 4875 | ctxt->parentgrammar = old; |
| 4876 | grammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
| 4877 | if (old != NULL) { |
| 4878 | ctxt->grammar = old; |
| 4879 | ctxt->parentgrammar = oldparent; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4880 | #if 0 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4881 | if (grammar != NULL) { |
| 4882 | grammar->next = old->next; |
| 4883 | old->next = grammar; |
| 4884 | } |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4885 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4886 | } |
| 4887 | if (grammar != NULL) |
| 4888 | def = grammar->start; |
| 4889 | else |
| 4890 | def = NULL; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4891 | } else if (IS_RELAXNG(node, "parentRef")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4892 | if (ctxt->parentgrammar == NULL) { |
| 4893 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_PARENT, |
| 4894 | "Use of parentRef without a parent grammar\n", NULL, |
| 4895 | NULL); |
| 4896 | return (NULL); |
| 4897 | } |
| 4898 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4899 | if (def == NULL) |
| 4900 | return (NULL); |
| 4901 | def->type = XML_RELAXNG_PARENTREF; |
| 4902 | def->name = xmlGetProp(node, BAD_CAST "name"); |
| 4903 | if (def->name == NULL) { |
| 4904 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_NAME, |
| 4905 | "parentRef has no name\n", NULL, NULL); |
| 4906 | } else { |
| 4907 | xmlRelaxNGNormExtSpace(def->name); |
| 4908 | if (xmlValidateNCName(def->name, 0)) { |
| 4909 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NAME_INVALID, |
| 4910 | "parentRef name '%s' is not an NCName\n", |
| 4911 | def->name, NULL); |
| 4912 | } |
| 4913 | } |
| 4914 | if (node->children != NULL) { |
| 4915 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NOT_EMPTY, |
| 4916 | "parentRef is not empty\n", NULL, NULL); |
| 4917 | } |
| 4918 | if (ctxt->parentgrammar->refs == NULL) |
| 4919 | ctxt->parentgrammar->refs = xmlHashCreate(10); |
| 4920 | if (ctxt->parentgrammar->refs == NULL) { |
| 4921 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED, |
| 4922 | "Could not create references hash\n", NULL, NULL); |
| 4923 | def = NULL; |
| 4924 | } else if (def->name != NULL) { |
| 4925 | int tmp; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4926 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4927 | tmp = |
| 4928 | xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def); |
| 4929 | if (tmp < 0) { |
| 4930 | xmlRelaxNGDefinePtr prev; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4931 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4932 | prev = (xmlRelaxNGDefinePtr) |
| 4933 | xmlHashLookup(ctxt->parentgrammar->refs, def->name); |
| 4934 | if (prev == NULL) { |
| 4935 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED, |
| 4936 | "Internal error parentRef definitions '%s'\n", |
| 4937 | def->name, NULL); |
| 4938 | def = NULL; |
| 4939 | } else { |
| 4940 | def->nextHash = prev->nextHash; |
| 4941 | prev->nextHash = def; |
| 4942 | } |
| 4943 | } |
| 4944 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4945 | } else if (IS_RELAXNG(node, "mixed")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4946 | if (node->children == NULL) { |
| 4947 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, "Mixed is empty\n", |
| 4948 | NULL, NULL); |
| 4949 | def = NULL; |
| 4950 | } else { |
| 4951 | def = xmlRelaxNGParseInterleave(ctxt, node); |
| 4952 | if (def != NULL) { |
| 4953 | xmlRelaxNGDefinePtr tmp; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4954 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4955 | if ((def->content != NULL) && (def->content->next != NULL)) { |
| 4956 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
| 4957 | if (tmp != NULL) { |
| 4958 | tmp->type = XML_RELAXNG_GROUP; |
| 4959 | tmp->content = def->content; |
| 4960 | def->content = tmp; |
| 4961 | } |
| 4962 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4963 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4964 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
| 4965 | if (tmp == NULL) |
| 4966 | return (def); |
| 4967 | tmp->type = XML_RELAXNG_TEXT; |
| 4968 | tmp->next = def->content; |
| 4969 | def->content = tmp; |
| 4970 | } |
| 4971 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4972 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4973 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_CONSTRUCT, |
| 4974 | "Unexpected node %s is not a pattern\n", node->name, |
| 4975 | NULL); |
| 4976 | def = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4977 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4978 | return (def); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4979 | } |
| 4980 | |
| 4981 | /** |
| 4982 | * xmlRelaxNGParseAttribute: |
| 4983 | * @ctxt: a Relax-NG parser context |
| 4984 | * @node: the element node |
| 4985 | * |
| 4986 | * parse the content of a RelaxNG attribute node. |
| 4987 | * |
| 4988 | * Returns the definition pointer or NULL in case of error. |
| 4989 | */ |
| 4990 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4991 | xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4992 | { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4993 | xmlRelaxNGDefinePtr ret, cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4994 | xmlNodePtr child; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4995 | int old_flags; |
| 4996 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4997 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4998 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4999 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5000 | ret->type = XML_RELAXNG_ATTRIBUTE; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5001 | ret->parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5002 | child = node->children; |
| 5003 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5004 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_EMPTY, |
| 5005 | "xmlRelaxNGParseattribute: attribute has no children\n", |
| 5006 | NULL, NULL); |
| 5007 | return (ret); |
| 5008 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5009 | old_flags = ctxt->flags; |
| 5010 | ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5011 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5012 | if (cur != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5013 | child = child->next; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5014 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5015 | if (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5016 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 5017 | if (cur != NULL) { |
| 5018 | switch (cur->type) { |
| 5019 | case XML_RELAXNG_EMPTY: |
| 5020 | case XML_RELAXNG_NOT_ALLOWED: |
| 5021 | case XML_RELAXNG_TEXT: |
| 5022 | case XML_RELAXNG_ELEMENT: |
| 5023 | case XML_RELAXNG_DATATYPE: |
| 5024 | case XML_RELAXNG_VALUE: |
| 5025 | case XML_RELAXNG_LIST: |
| 5026 | case XML_RELAXNG_REF: |
| 5027 | case XML_RELAXNG_PARENTREF: |
| 5028 | case XML_RELAXNG_EXTERNALREF: |
| 5029 | case XML_RELAXNG_DEF: |
| 5030 | case XML_RELAXNG_ONEORMORE: |
| 5031 | case XML_RELAXNG_ZEROORMORE: |
| 5032 | case XML_RELAXNG_OPTIONAL: |
| 5033 | case XML_RELAXNG_CHOICE: |
| 5034 | case XML_RELAXNG_GROUP: |
| 5035 | case XML_RELAXNG_INTERLEAVE: |
| 5036 | case XML_RELAXNG_ATTRIBUTE: |
| 5037 | ret->content = cur; |
| 5038 | cur->parent = ret; |
| 5039 | break; |
| 5040 | case XML_RELAXNG_START: |
| 5041 | case XML_RELAXNG_PARAM: |
| 5042 | case XML_RELAXNG_EXCEPT: |
| 5043 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CONTENT, |
| 5044 | "attribute has invalid content\n", NULL, |
| 5045 | NULL); |
| 5046 | break; |
| 5047 | case XML_RELAXNG_NOOP: |
| 5048 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_NOOP, |
| 5049 | "RNG Internal error, noop found in attribute\n", |
| 5050 | NULL, NULL); |
| 5051 | break; |
| 5052 | } |
| 5053 | } |
| 5054 | child = child->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5055 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5056 | if (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5057 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CHILDREN, |
| 5058 | "attribute has multiple children\n", NULL, NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5059 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5060 | ctxt->flags = old_flags; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5061 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5062 | } |
| 5063 | |
| 5064 | /** |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5065 | * xmlRelaxNGParseExceptNameClass: |
| 5066 | * @ctxt: a Relax-NG parser context |
| 5067 | * @node: the except node |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5068 | * @attr: 1 if within an attribute, 0 if within an element |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5069 | * |
| 5070 | * parse the content of a RelaxNG nameClass node. |
| 5071 | * |
| 5072 | * Returns the definition pointer or NULL in case of error. |
| 5073 | */ |
| 5074 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5075 | xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5076 | xmlNodePtr node, int attr) |
| 5077 | { |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5078 | xmlRelaxNGDefinePtr ret, cur, last = NULL; |
| 5079 | xmlNodePtr child; |
| 5080 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5081 | if (!IS_RELAXNG(node, "except")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5082 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MISSING, |
| 5083 | "Expecting an except node\n", NULL, NULL); |
| 5084 | return (NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5085 | } |
| 5086 | if (node->next != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5087 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MULTIPLE, |
| 5088 | "exceptNameClass allows only a single except node\n", |
| 5089 | NULL, NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5090 | } |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5091 | if (node->children == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5092 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_EMPTY, "except has no content\n", |
| 5093 | NULL, NULL); |
| 5094 | return (NULL); |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5095 | } |
| 5096 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5097 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5098 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5099 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5100 | ret->type = XML_RELAXNG_EXCEPT; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5101 | child = node->children; |
| 5102 | while (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5103 | cur = xmlRelaxNGNewDefine(ctxt, child); |
| 5104 | if (cur == NULL) |
| 5105 | break; |
| 5106 | if (attr) |
| 5107 | cur->type = XML_RELAXNG_ATTRIBUTE; |
| 5108 | else |
| 5109 | cur->type = XML_RELAXNG_ELEMENT; |
| 5110 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 5111 | if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5112 | if (last == NULL) { |
| 5113 | ret->content = cur; |
| 5114 | } else { |
| 5115 | last->next = cur; |
| 5116 | } |
| 5117 | last = cur; |
| 5118 | } |
| 5119 | child = child->next; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5120 | } |
| 5121 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5122 | return (ret); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5123 | } |
| 5124 | |
| 5125 | /** |
| 5126 | * xmlRelaxNGParseNameClass: |
| 5127 | * @ctxt: a Relax-NG parser context |
| 5128 | * @node: the nameClass node |
| 5129 | * @def: the current definition |
| 5130 | * |
| 5131 | * parse the content of a RelaxNG nameClass node. |
| 5132 | * |
| 5133 | * Returns the definition pointer or NULL in case of error. |
| 5134 | */ |
| 5135 | static xmlRelaxNGDefinePtr |
| 5136 | xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5137 | xmlRelaxNGDefinePtr def) |
| 5138 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5139 | xmlRelaxNGDefinePtr ret, tmp; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5140 | xmlChar *val; |
| 5141 | |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5142 | ret = def; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5143 | if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) || |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5144 | (IS_RELAXNG(node, "nsName"))) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5145 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 5146 | (def->type != XML_RELAXNG_ATTRIBUTE)) { |
| 5147 | ret = xmlRelaxNGNewDefine(ctxt, node); |
| 5148 | if (ret == NULL) |
| 5149 | return (NULL); |
| 5150 | ret->parent = def; |
| 5151 | if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) |
| 5152 | ret->type = XML_RELAXNG_ATTRIBUTE; |
| 5153 | else |
| 5154 | ret->type = XML_RELAXNG_ELEMENT; |
| 5155 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5156 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5157 | if (IS_RELAXNG(node, "name")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5158 | val = xmlNodeGetContent(node); |
| 5159 | xmlRelaxNGNormExtSpace(val); |
| 5160 | if (xmlValidateNCName(val, 0)) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5161 | if (node->parent != NULL) |
| 5162 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME, |
| 5163 | "Element %s name '%s' is not an NCName\n", |
| 5164 | node->parent->name, val); |
| 5165 | else |
| 5166 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME, |
| 5167 | "name '%s' is not an NCName\n", |
| 5168 | val, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5169 | } |
| 5170 | ret->name = val; |
| 5171 | val = xmlGetProp(node, BAD_CAST "ns"); |
| 5172 | ret->ns = val; |
| 5173 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5174 | (val != NULL) && |
| 5175 | (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5176 | xmlRngPErr(ctxt, node, XML_RNGP_XML_NS, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5177 | "Attribute with namespace '%s' is not allowed\n", |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5178 | val, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5179 | } |
| 5180 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5181 | (val != NULL) && |
| 5182 | (val[0] == 0) && (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5183 | xmlRngPErr(ctxt, node, XML_RNGP_XMLNS_NAME, |
| 5184 | "Attribute with QName 'xmlns' is not allowed\n", |
| 5185 | val, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5186 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5187 | } else if (IS_RELAXNG(node, "anyName")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5188 | ret->name = NULL; |
| 5189 | ret->ns = NULL; |
| 5190 | if (node->children != NULL) { |
| 5191 | ret->nameClass = |
| 5192 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
| 5193 | (def->type == |
| 5194 | XML_RELAXNG_ATTRIBUTE)); |
| 5195 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5196 | } else if (IS_RELAXNG(node, "nsName")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5197 | ret->name = NULL; |
| 5198 | ret->ns = xmlGetProp(node, BAD_CAST "ns"); |
| 5199 | if (ret->ns == NULL) { |
| 5200 | xmlRngPErr(ctxt, node, XML_RNGP_NSNAME_NO_NS, |
| 5201 | "nsName has no ns attribute\n", NULL, NULL); |
| 5202 | } |
| 5203 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5204 | (ret->ns != NULL) && |
| 5205 | (xmlStrEqual |
| 5206 | (ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
| 5207 | xmlRngPErr(ctxt, node, XML_RNGP_XML_NS, |
| 5208 | "Attribute with namespace '%s' is not allowed\n", |
| 5209 | ret->ns, NULL); |
| 5210 | } |
| 5211 | if (node->children != NULL) { |
| 5212 | ret->nameClass = |
| 5213 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
| 5214 | (def->type == |
| 5215 | XML_RELAXNG_ATTRIBUTE)); |
| 5216 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5217 | } else if (IS_RELAXNG(node, "choice")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5218 | xmlNodePtr child; |
| 5219 | xmlRelaxNGDefinePtr last = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5220 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5221 | ret = xmlRelaxNGNewDefine(ctxt, node); |
| 5222 | if (ret == NULL) |
| 5223 | return (NULL); |
| 5224 | ret->parent = def; |
| 5225 | ret->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5226 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5227 | if (node->children == NULL) { |
| 5228 | xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_EMPTY, |
| 5229 | "Element choice is empty\n", NULL, NULL); |
| 5230 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5231 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5232 | child = node->children; |
| 5233 | while (child != NULL) { |
| 5234 | tmp = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5235 | if (tmp != NULL) { |
| 5236 | if (last == NULL) { |
| 5237 | last = ret->nameClass = tmp; |
| 5238 | } else { |
| 5239 | last->next = tmp; |
| 5240 | last = tmp; |
| 5241 | } |
| 5242 | } |
| 5243 | child = child->next; |
| 5244 | } |
| 5245 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5246 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5247 | xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_CONTENT, |
| 5248 | "expecting name, anyName, nsName or choice : got %s\n", |
| 5249 | node->name, NULL); |
| 5250 | return (NULL); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5251 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5252 | if (ret != def) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5253 | if (def->nameClass == NULL) { |
| 5254 | def->nameClass = ret; |
| 5255 | } else { |
| 5256 | tmp = def->nameClass; |
| 5257 | while (tmp->next != NULL) { |
| 5258 | tmp = tmp->next; |
| 5259 | } |
| 5260 | tmp->next = ret; |
| 5261 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5262 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5263 | return (ret); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5264 | } |
| 5265 | |
| 5266 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5267 | * xmlRelaxNGParseElement: |
| 5268 | * @ctxt: a Relax-NG parser context |
| 5269 | * @node: the element node |
| 5270 | * |
| 5271 | * parse the content of a RelaxNG element node. |
| 5272 | * |
| 5273 | * Returns the definition pointer or NULL in case of error. |
| 5274 | */ |
| 5275 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5276 | xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 5277 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5278 | xmlRelaxNGDefinePtr ret, cur, last; |
| 5279 | xmlNodePtr child; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5280 | const xmlChar *olddefine; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5281 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5282 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5283 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5284 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5285 | ret->type = XML_RELAXNG_ELEMENT; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5286 | ret->parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5287 | child = node->children; |
| 5288 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5289 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_EMPTY, |
| 5290 | "xmlRelaxNGParseElement: element has no children\n", |
| 5291 | NULL, NULL); |
| 5292 | return (ret); |
| 5293 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5294 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5295 | if (cur != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5296 | child = child->next; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5297 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5298 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5299 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NO_CONTENT, |
| 5300 | "xmlRelaxNGParseElement: element has no content\n", |
| 5301 | NULL, NULL); |
| 5302 | return (ret); |
| 5303 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5304 | olddefine = ctxt->define; |
| 5305 | ctxt->define = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5306 | last = NULL; |
| 5307 | while (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5308 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 5309 | if (cur != NULL) { |
| 5310 | cur->parent = ret; |
| 5311 | switch (cur->type) { |
| 5312 | case XML_RELAXNG_EMPTY: |
| 5313 | case XML_RELAXNG_NOT_ALLOWED: |
| 5314 | case XML_RELAXNG_TEXT: |
| 5315 | case XML_RELAXNG_ELEMENT: |
| 5316 | case XML_RELAXNG_DATATYPE: |
| 5317 | case XML_RELAXNG_VALUE: |
| 5318 | case XML_RELAXNG_LIST: |
| 5319 | case XML_RELAXNG_REF: |
| 5320 | case XML_RELAXNG_PARENTREF: |
| 5321 | case XML_RELAXNG_EXTERNALREF: |
| 5322 | case XML_RELAXNG_DEF: |
| 5323 | case XML_RELAXNG_ZEROORMORE: |
| 5324 | case XML_RELAXNG_ONEORMORE: |
| 5325 | case XML_RELAXNG_OPTIONAL: |
| 5326 | case XML_RELAXNG_CHOICE: |
| 5327 | case XML_RELAXNG_GROUP: |
| 5328 | case XML_RELAXNG_INTERLEAVE: |
| 5329 | if (last == NULL) { |
| 5330 | ret->content = last = cur; |
| 5331 | } else { |
| 5332 | if ((last->type == XML_RELAXNG_ELEMENT) && |
| 5333 | (ret->content == last)) { |
| 5334 | ret->content = xmlRelaxNGNewDefine(ctxt, node); |
| 5335 | if (ret->content != NULL) { |
| 5336 | ret->content->type = XML_RELAXNG_GROUP; |
| 5337 | ret->content->content = last; |
| 5338 | } else { |
| 5339 | ret->content = last; |
| 5340 | } |
| 5341 | } |
| 5342 | last->next = cur; |
| 5343 | last = cur; |
| 5344 | } |
| 5345 | break; |
| 5346 | case XML_RELAXNG_ATTRIBUTE: |
| 5347 | cur->next = ret->attrs; |
| 5348 | ret->attrs = cur; |
| 5349 | break; |
| 5350 | case XML_RELAXNG_START: |
| 5351 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5352 | "RNG Internal error, start found in element\n", |
| 5353 | NULL, NULL); |
| 5354 | break; |
| 5355 | case XML_RELAXNG_PARAM: |
| 5356 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5357 | "RNG Internal error, param found in element\n", |
| 5358 | NULL, NULL); |
| 5359 | break; |
| 5360 | case XML_RELAXNG_EXCEPT: |
| 5361 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5362 | "RNG Internal error, except found in element\n", |
| 5363 | NULL, NULL); |
| 5364 | break; |
| 5365 | case XML_RELAXNG_NOOP: |
| 5366 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5367 | "RNG Internal error, noop found in element\n", |
| 5368 | NULL, NULL); |
| 5369 | break; |
| 5370 | } |
| 5371 | } |
| 5372 | child = child->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5373 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5374 | ctxt->define = olddefine; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5375 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5376 | } |
| 5377 | |
| 5378 | /** |
| 5379 | * xmlRelaxNGParsePatterns: |
| 5380 | * @ctxt: a Relax-NG parser context |
| 5381 | * @nodes: list of nodes |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5382 | * @group: use an implicit <group> for elements |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5383 | * |
| 5384 | * parse the content of a RelaxNG start node. |
| 5385 | * |
| 5386 | * Returns the definition pointer or NULL in case of error. |
| 5387 | */ |
| 5388 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5389 | xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5390 | int group) |
| 5391 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5392 | xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5393 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5394 | parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5395 | while (nodes != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5396 | if (IS_RELAXNG(nodes, "element")) { |
| 5397 | cur = xmlRelaxNGParseElement(ctxt, nodes); |
| 5398 | if (def == NULL) { |
| 5399 | def = last = cur; |
| 5400 | } else { |
| 5401 | if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) && |
| 5402 | (def == last)) { |
| 5403 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
| 5404 | def->type = XML_RELAXNG_GROUP; |
| 5405 | def->content = last; |
| 5406 | } |
| 5407 | last->next = cur; |
| 5408 | last = cur; |
| 5409 | } |
| 5410 | cur->parent = parent; |
| 5411 | } else { |
| 5412 | cur = xmlRelaxNGParsePattern(ctxt, nodes); |
| 5413 | if (cur != NULL) { |
| 5414 | if (def == NULL) { |
| 5415 | def = last = cur; |
| 5416 | } else { |
| 5417 | last->next = cur; |
| 5418 | last = cur; |
| 5419 | } |
| 5420 | } |
| 5421 | } |
| 5422 | nodes = nodes->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5423 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5424 | return (def); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5425 | } |
| 5426 | |
| 5427 | /** |
| 5428 | * xmlRelaxNGParseStart: |
| 5429 | * @ctxt: a Relax-NG parser context |
| 5430 | * @nodes: start children nodes |
| 5431 | * |
| 5432 | * parse the content of a RelaxNG start node. |
| 5433 | * |
| 5434 | * Returns 0 in case of success, -1 in case of error |
| 5435 | */ |
| 5436 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5437 | xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) |
| 5438 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5439 | int ret = 0; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5440 | xmlRelaxNGDefinePtr def = NULL, last; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5441 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5442 | if (nodes == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5443 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, "start has no children\n", |
| 5444 | NULL, NULL); |
| 5445 | return (-1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5446 | } |
| 5447 | if (IS_RELAXNG(nodes, "empty")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5448 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
| 5449 | if (def == NULL) |
| 5450 | return (-1); |
| 5451 | def->type = XML_RELAXNG_EMPTY; |
| 5452 | if (nodes->children != NULL) { |
| 5453 | xmlRngPErr(ctxt, nodes, XML_RNGP_EMPTY_CONTENT, |
| 5454 | "element empty is not empty\n", NULL, NULL); |
| 5455 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5456 | } else if (IS_RELAXNG(nodes, "notAllowed")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5457 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
| 5458 | if (def == NULL) |
| 5459 | return (-1); |
| 5460 | def->type = XML_RELAXNG_NOT_ALLOWED; |
| 5461 | if (nodes->children != NULL) { |
| 5462 | xmlRngPErr(ctxt, nodes, XML_RNGP_NOTALLOWED_NOT_EMPTY, |
| 5463 | "element notAllowed is not empty\n", NULL, NULL); |
| 5464 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5465 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5466 | def = xmlRelaxNGParsePatterns(ctxt, nodes, 1); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5467 | } |
| 5468 | if (ctxt->grammar->start != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5469 | last = ctxt->grammar->start; |
| 5470 | while (last->next != NULL) |
| 5471 | last = last->next; |
| 5472 | last->next = def; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5473 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5474 | ctxt->grammar->start = def; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5475 | } |
| 5476 | nodes = nodes->next; |
| 5477 | if (nodes != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5478 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_CONTENT, |
| 5479 | "start more than one children\n", NULL, NULL); |
| 5480 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5481 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5482 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5483 | } |
| 5484 | |
| 5485 | /** |
| 5486 | * xmlRelaxNGParseGrammarContent: |
| 5487 | * @ctxt: a Relax-NG parser context |
| 5488 | * @nodes: grammar children nodes |
| 5489 | * |
| 5490 | * parse the content of a RelaxNG grammar node. |
| 5491 | * |
| 5492 | * Returns 0 in case of success, -1 in case of error |
| 5493 | */ |
| 5494 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5495 | xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt, |
| 5496 | xmlNodePtr nodes) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5497 | { |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5498 | int ret = 0, tmp; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5499 | |
| 5500 | if (nodes == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5501 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_EMPTY, |
| 5502 | "grammar has no children\n", NULL, NULL); |
| 5503 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5504 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5505 | while (nodes != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5506 | if (IS_RELAXNG(nodes, "start")) { |
| 5507 | if (nodes->children == NULL) { |
| 5508 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, |
| 5509 | "start has no children\n", NULL, NULL); |
| 5510 | } else { |
| 5511 | tmp = xmlRelaxNGParseStart(ctxt, nodes->children); |
| 5512 | if (tmp != 0) |
| 5513 | ret = -1; |
| 5514 | } |
| 5515 | } else if (IS_RELAXNG(nodes, "define")) { |
| 5516 | tmp = xmlRelaxNGParseDefine(ctxt, nodes); |
| 5517 | if (tmp != 0) |
| 5518 | ret = -1; |
| 5519 | } else if (IS_RELAXNG(nodes, "include")) { |
| 5520 | tmp = xmlRelaxNGParseInclude(ctxt, nodes); |
| 5521 | if (tmp != 0) |
| 5522 | ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5523 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5524 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT, |
| 5525 | "grammar has unexpected child %s\n", nodes->name, |
| 5526 | NULL); |
| 5527 | ret = -1; |
| 5528 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5529 | nodes = nodes->next; |
| 5530 | } |
| 5531 | return (ret); |
| 5532 | } |
| 5533 | |
| 5534 | /** |
| 5535 | * xmlRelaxNGCheckReference: |
| 5536 | * @ref: the ref |
| 5537 | * @ctxt: a Relax-NG parser context |
| 5538 | * @name: the name associated to the defines |
| 5539 | * |
| 5540 | * Applies the 4.17. combine attribute rule for all the define |
| 5541 | * element of a given grammar using the same name. |
| 5542 | */ |
| 5543 | static void |
| 5544 | xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5545 | xmlRelaxNGParserCtxtPtr ctxt, |
| 5546 | const xmlChar * name) |
| 5547 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5548 | xmlRelaxNGGrammarPtr grammar; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5549 | xmlRelaxNGDefinePtr def, cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5550 | |
| 5551 | grammar = ctxt->grammar; |
| 5552 | if (grammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5553 | xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR, |
| 5554 | "Internal error: no grammar in CheckReference %s\n", |
| 5555 | name, NULL); |
| 5556 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5557 | } |
| 5558 | if (ref->content != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5559 | xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR, |
| 5560 | "Internal error: reference has content in CheckReference %s\n", |
| 5561 | name, NULL); |
| 5562 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5563 | } |
| 5564 | if (grammar->defs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5565 | def = xmlHashLookup(grammar->defs, name); |
| 5566 | if (def != NULL) { |
| 5567 | cur = ref; |
| 5568 | while (cur != NULL) { |
| 5569 | cur->content = def; |
| 5570 | cur = cur->nextHash; |
| 5571 | } |
| 5572 | } else { |
| 5573 | xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF, |
| 5574 | "Reference %s has no matching definition\n", name, |
| 5575 | NULL); |
| 5576 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5577 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5578 | xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF, |
| 5579 | "Reference %s has no matching definition\n", name, |
| 5580 | NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5581 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5582 | } |
| 5583 | |
| 5584 | /** |
| 5585 | * xmlRelaxNGCheckCombine: |
| 5586 | * @define: the define(s) list |
| 5587 | * @ctxt: a Relax-NG parser context |
| 5588 | * @name: the name associated to the defines |
| 5589 | * |
| 5590 | * Applies the 4.17. combine attribute rule for all the define |
| 5591 | * element of a given grammar using the same name. |
| 5592 | */ |
| 5593 | static void |
| 5594 | xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5595 | xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * name) |
| 5596 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5597 | xmlChar *combine; |
| 5598 | int choiceOrInterleave = -1; |
| 5599 | int missing = 0; |
| 5600 | xmlRelaxNGDefinePtr cur, last, tmp, tmp2; |
| 5601 | |
| 5602 | if (define->nextHash == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5603 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5604 | cur = define; |
| 5605 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5606 | combine = xmlGetProp(cur->node, BAD_CAST "combine"); |
| 5607 | if (combine != NULL) { |
| 5608 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
| 5609 | if (choiceOrInterleave == -1) |
| 5610 | choiceOrInterleave = 1; |
| 5611 | else if (choiceOrInterleave == 0) { |
| 5612 | xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE, |
| 5613 | "Defines for %s use both 'choice' and 'interleave'\n", |
| 5614 | name, NULL); |
| 5615 | } |
| 5616 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
| 5617 | if (choiceOrInterleave == -1) |
| 5618 | choiceOrInterleave = 0; |
| 5619 | else if (choiceOrInterleave == 1) { |
| 5620 | xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE, |
| 5621 | "Defines for %s use both 'choice' and 'interleave'\n", |
| 5622 | name, NULL); |
| 5623 | } |
| 5624 | } else { |
| 5625 | xmlRngPErr(ctxt, define->node, XML_RNGP_UNKNOWN_COMBINE, |
| 5626 | "Defines for %s use unknown combine value '%s''\n", |
| 5627 | name, combine); |
| 5628 | } |
| 5629 | xmlFree(combine); |
| 5630 | } else { |
| 5631 | if (missing == 0) |
| 5632 | missing = 1; |
| 5633 | else { |
| 5634 | xmlRngPErr(ctxt, define->node, XML_RNGP_NEED_COMBINE, |
| 5635 | "Some defines for %s needs the combine attribute\n", |
| 5636 | name, NULL); |
| 5637 | } |
| 5638 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5639 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5640 | cur = cur->nextHash; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5641 | } |
| 5642 | #ifdef DEBUG |
| 5643 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5644 | "xmlRelaxNGCheckCombine(): merging %s defines: %d\n", |
| 5645 | name, choiceOrInterleave); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5646 | #endif |
| 5647 | if (choiceOrInterleave == -1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5648 | choiceOrInterleave = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5649 | cur = xmlRelaxNGNewDefine(ctxt, define->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5650 | if (cur == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5651 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5652 | if (choiceOrInterleave == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5653 | cur->type = XML_RELAXNG_INTERLEAVE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5654 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5655 | cur->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5656 | tmp = define; |
| 5657 | last = NULL; |
| 5658 | while (tmp != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5659 | if (tmp->content != NULL) { |
| 5660 | if (tmp->content->next != NULL) { |
| 5661 | /* |
| 5662 | * we need first to create a wrapper. |
| 5663 | */ |
| 5664 | tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node); |
| 5665 | if (tmp2 == NULL) |
| 5666 | break; |
| 5667 | tmp2->type = XML_RELAXNG_GROUP; |
| 5668 | tmp2->content = tmp->content; |
| 5669 | } else { |
| 5670 | tmp2 = tmp->content; |
| 5671 | } |
| 5672 | if (last == NULL) { |
| 5673 | cur->content = tmp2; |
| 5674 | } else { |
| 5675 | last->next = tmp2; |
| 5676 | } |
| 5677 | last = tmp2; |
| 5678 | } |
| 5679 | tmp->content = cur; |
| 5680 | tmp = tmp->nextHash; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5681 | } |
| 5682 | define->content = cur; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5683 | if (choiceOrInterleave == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5684 | if (ctxt->interleaves == NULL) |
| 5685 | ctxt->interleaves = xmlHashCreate(10); |
| 5686 | if (ctxt->interleaves == NULL) { |
| 5687 | xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5688 | "Failed to create interleaves hash table\n", NULL, |
| 5689 | NULL); |
| 5690 | } else { |
| 5691 | char tmpname[32]; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5692 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5693 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
| 5694 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < |
| 5695 | 0) { |
| 5696 | xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5697 | "Failed to add %s to hash table\n", |
| 5698 | (const xmlChar *) tmpname, NULL); |
| 5699 | } |
| 5700 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5701 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5702 | } |
| 5703 | |
| 5704 | /** |
| 5705 | * xmlRelaxNGCombineStart: |
| 5706 | * @ctxt: a Relax-NG parser context |
| 5707 | * @grammar: the grammar |
| 5708 | * |
| 5709 | * Applies the 4.17. combine rule for all the start |
| 5710 | * element of a given grammar. |
| 5711 | */ |
| 5712 | static void |
| 5713 | xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5714 | xmlRelaxNGGrammarPtr grammar) |
| 5715 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5716 | xmlRelaxNGDefinePtr starts; |
| 5717 | xmlChar *combine; |
| 5718 | int choiceOrInterleave = -1; |
| 5719 | int missing = 0; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5720 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5721 | |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5722 | starts = grammar->start; |
| 5723 | if ((starts == NULL) || (starts->next == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5724 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5725 | cur = starts; |
| 5726 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5727 | if ((cur->node == NULL) || (cur->node->parent == NULL) || |
| 5728 | (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) { |
| 5729 | combine = NULL; |
| 5730 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_MISSING, |
| 5731 | "Internal error: start element not found\n", NULL, |
| 5732 | NULL); |
| 5733 | } else { |
| 5734 | combine = xmlGetProp(cur->node->parent, BAD_CAST "combine"); |
| 5735 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5736 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5737 | if (combine != NULL) { |
| 5738 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
| 5739 | if (choiceOrInterleave == -1) |
| 5740 | choiceOrInterleave = 1; |
| 5741 | else if (choiceOrInterleave == 0) { |
| 5742 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE, |
| 5743 | "<start> use both 'choice' and 'interleave'\n", |
| 5744 | NULL, NULL); |
| 5745 | } |
| 5746 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
| 5747 | if (choiceOrInterleave == -1) |
| 5748 | choiceOrInterleave = 0; |
| 5749 | else if (choiceOrInterleave == 1) { |
| 5750 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE, |
| 5751 | "<start> use both 'choice' and 'interleave'\n", |
| 5752 | NULL, NULL); |
| 5753 | } |
| 5754 | } else { |
| 5755 | xmlRngPErr(ctxt, cur->node, XML_RNGP_UNKNOWN_COMBINE, |
| 5756 | "<start> uses unknown combine value '%s''\n", |
| 5757 | combine, NULL); |
| 5758 | } |
| 5759 | xmlFree(combine); |
| 5760 | } else { |
| 5761 | if (missing == 0) |
| 5762 | missing = 1; |
| 5763 | else { |
| 5764 | xmlRngPErr(ctxt, cur->node, XML_RNGP_NEED_COMBINE, |
| 5765 | "Some <start> element miss the combine attribute\n", |
| 5766 | NULL, NULL); |
| 5767 | } |
| 5768 | } |
| 5769 | |
| 5770 | cur = cur->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5771 | } |
| 5772 | #ifdef DEBUG |
| 5773 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5774 | "xmlRelaxNGCombineStart(): merging <start>: %d\n", |
| 5775 | choiceOrInterleave); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5776 | #endif |
| 5777 | if (choiceOrInterleave == -1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5778 | choiceOrInterleave = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5779 | cur = xmlRelaxNGNewDefine(ctxt, starts->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5780 | if (cur == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5781 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5782 | if (choiceOrInterleave == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5783 | cur->type = XML_RELAXNG_INTERLEAVE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5784 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5785 | cur->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5786 | cur->content = grammar->start; |
| 5787 | grammar->start = cur; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5788 | if (choiceOrInterleave == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5789 | if (ctxt->interleaves == NULL) |
| 5790 | ctxt->interleaves = xmlHashCreate(10); |
| 5791 | if (ctxt->interleaves == NULL) { |
| 5792 | xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5793 | "Failed to create interleaves hash table\n", NULL, |
| 5794 | NULL); |
| 5795 | } else { |
| 5796 | char tmpname[32]; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5797 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5798 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
| 5799 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < |
| 5800 | 0) { |
| 5801 | xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5802 | "Failed to add %s to hash table\n", |
| 5803 | (const xmlChar *) tmpname, NULL); |
| 5804 | } |
| 5805 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5806 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5807 | } |
| 5808 | |
| 5809 | /** |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5810 | * xmlRelaxNGCheckCycles: |
| 5811 | * @ctxt: a Relax-NG parser context |
| 5812 | * @nodes: grammar children nodes |
| 5813 | * @depth: the counter |
| 5814 | * |
| 5815 | * Check for cycles. |
| 5816 | * |
| 5817 | * Returns 0 if check passed, and -1 in case of error |
| 5818 | */ |
| 5819 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5820 | xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt, |
| 5821 | xmlRelaxNGDefinePtr cur, int depth) |
| 5822 | { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5823 | int ret = 0; |
| 5824 | |
| 5825 | while ((ret == 0) && (cur != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5826 | if ((cur->type == XML_RELAXNG_REF) || |
| 5827 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5828 | if (cur->depth == -1) { |
| 5829 | cur->depth = depth; |
| 5830 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
| 5831 | cur->depth = -2; |
| 5832 | } else if (depth == cur->depth) { |
| 5833 | xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_CYCLE, |
| 5834 | "Detected a cycle in %s references\n", |
| 5835 | cur->name, NULL); |
| 5836 | return (-1); |
| 5837 | } |
| 5838 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
| 5839 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1); |
| 5840 | } else { |
| 5841 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
| 5842 | } |
| 5843 | cur = cur->next; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5844 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5845 | return (ret); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5846 | } |
| 5847 | |
| 5848 | /** |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5849 | * xmlRelaxNGTryUnlink: |
| 5850 | * @ctxt: a Relax-NG parser context |
| 5851 | * @cur: the definition to unlink |
| 5852 | * @parent: the parent definition |
| 5853 | * @prev: the previous sibling definition |
| 5854 | * |
| 5855 | * Try to unlink a definition. If not possble make it a NOOP |
| 5856 | * |
| 5857 | * Returns the new prev definition |
| 5858 | */ |
| 5859 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5860 | xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 5861 | xmlRelaxNGDefinePtr cur, |
| 5862 | xmlRelaxNGDefinePtr parent, xmlRelaxNGDefinePtr prev) |
| 5863 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5864 | if (prev != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5865 | prev->next = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5866 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5867 | if (parent != NULL) { |
| 5868 | if (parent->content == cur) |
| 5869 | parent->content = cur->next; |
| 5870 | else if (parent->attrs == cur) |
| 5871 | parent->attrs = cur->next; |
| 5872 | else if (parent->nameClass == cur) |
| 5873 | parent->nameClass = cur->next; |
| 5874 | } else { |
| 5875 | cur->type = XML_RELAXNG_NOOP; |
| 5876 | prev = cur; |
| 5877 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5878 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5879 | return (prev); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5880 | } |
| 5881 | |
| 5882 | /** |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5883 | * xmlRelaxNGSimplify: |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5884 | * @ctxt: a Relax-NG parser context |
| 5885 | * @nodes: grammar children nodes |
| 5886 | * |
| 5887 | * Check for simplification of empty and notAllowed |
| 5888 | */ |
| 5889 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5890 | xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt, |
| 5891 | xmlRelaxNGDefinePtr cur, xmlRelaxNGDefinePtr parent) |
| 5892 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5893 | xmlRelaxNGDefinePtr prev = NULL; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5894 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5895 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5896 | if ((cur->type == XML_RELAXNG_REF) || |
| 5897 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5898 | if (cur->depth != -3) { |
| 5899 | cur->depth = -3; |
| 5900 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
| 5901 | } |
| 5902 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
| 5903 | cur->parent = parent; |
| 5904 | if ((parent != NULL) && |
| 5905 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
| 5906 | (parent->type == XML_RELAXNG_LIST) || |
| 5907 | (parent->type == XML_RELAXNG_GROUP) || |
| 5908 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 5909 | (parent->type == XML_RELAXNG_ONEORMORE) || |
| 5910 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 5911 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
| 5912 | break; |
| 5913 | } |
| 5914 | if ((parent != NULL) && (parent->type == XML_RELAXNG_CHOICE)) { |
| 5915 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5916 | } else |
| 5917 | prev = cur; |
| 5918 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 5919 | cur->parent = parent; |
| 5920 | if ((parent != NULL) && |
| 5921 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
| 5922 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 5923 | parent->type = XML_RELAXNG_EMPTY; |
| 5924 | break; |
| 5925 | } |
| 5926 | if ((parent != NULL) && |
| 5927 | ((parent->type == XML_RELAXNG_GROUP) || |
| 5928 | (parent->type == XML_RELAXNG_INTERLEAVE))) { |
| 5929 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5930 | } else |
| 5931 | prev = cur; |
| 5932 | } else { |
| 5933 | cur->parent = parent; |
| 5934 | if (cur->content != NULL) |
| 5935 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
| 5936 | if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL)) |
| 5937 | xmlRelaxNGSimplify(ctxt, cur->attrs, cur); |
| 5938 | if (cur->nameClass != NULL) |
| 5939 | xmlRelaxNGSimplify(ctxt, cur->nameClass, cur); |
| 5940 | /* |
| 5941 | * On Elements, try to move attribute only generating rules on |
| 5942 | * the attrs rules. |
| 5943 | */ |
| 5944 | if (cur->type == XML_RELAXNG_ELEMENT) { |
| 5945 | int attronly; |
| 5946 | xmlRelaxNGDefinePtr tmp, pre; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 5947 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5948 | while (cur->content != NULL) { |
| 5949 | attronly = |
| 5950 | xmlRelaxNGGenerateAttributes(ctxt, cur->content); |
| 5951 | if (attronly == 1) { |
| 5952 | /* |
| 5953 | * migrate cur->content to attrs |
| 5954 | */ |
| 5955 | tmp = cur->content; |
| 5956 | cur->content = tmp->next; |
| 5957 | tmp->next = cur->attrs; |
| 5958 | cur->attrs = tmp; |
| 5959 | } else { |
| 5960 | /* |
| 5961 | * cur->content can generate elements or text |
| 5962 | */ |
| 5963 | break; |
| 5964 | } |
| 5965 | } |
| 5966 | pre = cur->content; |
| 5967 | while ((pre != NULL) && (pre->next != NULL)) { |
| 5968 | tmp = pre->next; |
| 5969 | attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp); |
| 5970 | if (attronly == 1) { |
| 5971 | /* |
| 5972 | * migrate tmp to attrs |
| 5973 | */ |
| 5974 | pre->next = tmp->next; |
| 5975 | tmp->next = cur->attrs; |
| 5976 | cur->attrs = tmp; |
| 5977 | } else { |
| 5978 | pre = tmp; |
| 5979 | } |
| 5980 | } |
| 5981 | } |
| 5982 | /* |
| 5983 | * This may result in a simplification |
| 5984 | */ |
| 5985 | if ((cur->type == XML_RELAXNG_GROUP) || |
| 5986 | (cur->type == XML_RELAXNG_INTERLEAVE)) { |
| 5987 | if (cur->content == NULL) |
| 5988 | cur->type = XML_RELAXNG_EMPTY; |
| 5989 | else if (cur->content->next == NULL) { |
| 5990 | if ((parent == NULL) && (prev == NULL)) { |
| 5991 | cur->type = XML_RELAXNG_NOOP; |
| 5992 | } else if (prev == NULL) { |
| 5993 | parent->content = cur->content; |
| 5994 | cur->content->next = cur->next; |
| 5995 | cur = cur->content; |
| 5996 | } else { |
| 5997 | cur->content->next = cur->next; |
| 5998 | prev->next = cur->content; |
| 5999 | cur = cur->content; |
| 6000 | } |
| 6001 | } |
| 6002 | } |
| 6003 | /* |
| 6004 | * the current node may have been transformed back |
| 6005 | */ |
| 6006 | if ((cur->type == XML_RELAXNG_EXCEPT) && |
| 6007 | (cur->content != NULL) && |
| 6008 | (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) { |
| 6009 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 6010 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
| 6011 | if ((parent != NULL) && |
| 6012 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
| 6013 | (parent->type == XML_RELAXNG_LIST) || |
| 6014 | (parent->type == XML_RELAXNG_GROUP) || |
| 6015 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 6016 | (parent->type == XML_RELAXNG_ONEORMORE) || |
| 6017 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 6018 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
| 6019 | break; |
| 6020 | } |
| 6021 | if ((parent != NULL) && |
| 6022 | (parent->type == XML_RELAXNG_CHOICE)) { |
| 6023 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 6024 | } else |
| 6025 | prev = cur; |
| 6026 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 6027 | if ((parent != NULL) && |
| 6028 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
| 6029 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 6030 | parent->type = XML_RELAXNG_EMPTY; |
| 6031 | break; |
| 6032 | } |
| 6033 | if ((parent != NULL) && |
| 6034 | ((parent->type == XML_RELAXNG_GROUP) || |
| 6035 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 6036 | (parent->type == XML_RELAXNG_CHOICE))) { |
| 6037 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 6038 | } else |
| 6039 | prev = cur; |
| 6040 | } else { |
| 6041 | prev = cur; |
| 6042 | } |
| 6043 | } |
| 6044 | cur = cur->next; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6045 | } |
| 6046 | } |
| 6047 | |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6048 | /** |
| 6049 | * xmlRelaxNGGroupContentType: |
| 6050 | * @ct1: the first content type |
| 6051 | * @ct2: the second content type |
| 6052 | * |
| 6053 | * Try to group 2 content types |
| 6054 | * |
| 6055 | * Returns the content type |
| 6056 | */ |
| 6057 | static xmlRelaxNGContentType |
| 6058 | xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6059 | xmlRelaxNGContentType ct2) |
| 6060 | { |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6061 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6062 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
| 6063 | return (XML_RELAXNG_CONTENT_ERROR); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6064 | if (ct1 == XML_RELAXNG_CONTENT_EMPTY) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6065 | return (ct2); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6066 | if (ct2 == XML_RELAXNG_CONTENT_EMPTY) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6067 | return (ct1); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6068 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6069 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
| 6070 | return (XML_RELAXNG_CONTENT_COMPLEX); |
| 6071 | return (XML_RELAXNG_CONTENT_ERROR); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6072 | } |
| 6073 | |
| 6074 | /** |
| 6075 | * xmlRelaxNGMaxContentType: |
| 6076 | * @ct1: the first content type |
| 6077 | * @ct2: the second content type |
| 6078 | * |
| 6079 | * Compute the max content-type |
| 6080 | * |
| 6081 | * Returns the content type |
| 6082 | */ |
| 6083 | static xmlRelaxNGContentType |
| 6084 | xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6085 | xmlRelaxNGContentType ct2) |
| 6086 | { |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6087 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6088 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
| 6089 | return (XML_RELAXNG_CONTENT_ERROR); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6090 | if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6091 | (ct2 == XML_RELAXNG_CONTENT_SIMPLE)) |
| 6092 | return (XML_RELAXNG_CONTENT_SIMPLE); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6093 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6094 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
| 6095 | return (XML_RELAXNG_CONTENT_COMPLEX); |
| 6096 | return (XML_RELAXNG_CONTENT_EMPTY); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6097 | } |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6098 | |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6099 | /** |
| 6100 | * xmlRelaxNGCheckRules: |
| 6101 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6102 | * @cur: the current definition |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6103 | * @flags: some accumulated flags |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6104 | * @ptype: the parent type |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6105 | * |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6106 | * Check for rules in section 7.1 and 7.2 |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6107 | * |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6108 | * Returns the content type of @cur |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6109 | */ |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6110 | static xmlRelaxNGContentType |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6111 | xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt, |
| 6112 | xmlRelaxNGDefinePtr cur, int flags, |
| 6113 | xmlRelaxNGType ptype) |
| 6114 | { |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6115 | int nflags = flags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6116 | xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6117 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6118 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6119 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6120 | if ((cur->type == XML_RELAXNG_REF) || |
| 6121 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 6122 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6123 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_REF, |
| 6124 | "Found forbidden pattern list//ref\n", NULL, |
| 6125 | NULL); |
| 6126 | } |
| 6127 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6128 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_REF, |
| 6129 | "Found forbidden pattern data/except//ref\n", |
| 6130 | NULL, NULL); |
| 6131 | } |
| 6132 | if (cur->depth > -4) { |
| 6133 | cur->depth = -4; |
| 6134 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, |
| 6135 | flags, cur->type); |
| 6136 | cur->depth = ret - 15; |
| 6137 | } else if (cur->depth == -4) { |
| 6138 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6139 | } else { |
| 6140 | ret = (xmlRelaxNGContentType) (cur->depth + 15); |
| 6141 | } |
| 6142 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
| 6143 | /* |
| 6144 | * The 7.3 Attribute derivation rule for groups is plugged there |
| 6145 | */ |
| 6146 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
| 6147 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6148 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ELEM, |
| 6149 | "Found forbidden pattern data/except//element(ref)\n", |
| 6150 | NULL, NULL); |
| 6151 | } |
| 6152 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6153 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ELEM, |
| 6154 | "Found forbidden pattern list//element(ref)\n", |
| 6155 | NULL, NULL); |
| 6156 | } |
| 6157 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6158 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM, |
| 6159 | "Found forbidden pattern attribute//element(ref)\n", |
| 6160 | NULL, NULL); |
| 6161 | } |
| 6162 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6163 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM, |
| 6164 | "Found forbidden pattern attribute//element(ref)\n", |
| 6165 | NULL, NULL); |
| 6166 | } |
| 6167 | /* |
| 6168 | * reset since in the simple form elements are only child |
| 6169 | * of grammar/define |
| 6170 | */ |
| 6171 | nflags = 0; |
| 6172 | ret = |
| 6173 | xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type); |
| 6174 | if (ret != XML_RELAXNG_CONTENT_EMPTY) { |
| 6175 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_EMPTY, |
| 6176 | "Element %s attributes have a content type error\n", |
| 6177 | cur->name, NULL); |
| 6178 | } |
| 6179 | ret = |
| 6180 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6181 | cur->type); |
| 6182 | if (ret == XML_RELAXNG_CONTENT_ERROR) { |
| 6183 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_ERROR, |
| 6184 | "Element %s has a content type error\n", |
| 6185 | cur->name, NULL); |
| 6186 | } else { |
| 6187 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6188 | } |
| 6189 | } else if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
| 6190 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6191 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ATTR, |
| 6192 | "Found forbidden pattern attribute//attribute\n", |
| 6193 | NULL, NULL); |
| 6194 | } |
| 6195 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6196 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ATTR, |
| 6197 | "Found forbidden pattern list//attribute\n", |
| 6198 | NULL, NULL); |
| 6199 | } |
| 6200 | if (flags & XML_RELAXNG_IN_OOMGROUP) { |
| 6201 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_GROUP_ATTR, |
| 6202 | "Found forbidden pattern oneOrMore//group//attribute\n", |
| 6203 | NULL, NULL); |
| 6204 | } |
| 6205 | if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) { |
| 6206 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_INTERLEAVE_ATTR, |
| 6207 | "Found forbidden pattern oneOrMore//interleave//attribute\n", |
| 6208 | NULL, NULL); |
| 6209 | } |
| 6210 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6211 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ATTR, |
| 6212 | "Found forbidden pattern data/except//attribute\n", |
| 6213 | NULL, NULL); |
| 6214 | } |
| 6215 | if (flags & XML_RELAXNG_IN_START) { |
| 6216 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ATTR, |
| 6217 | "Found forbidden pattern start//attribute\n", |
| 6218 | NULL, NULL); |
| 6219 | } |
| 6220 | if ((!(flags & XML_RELAXNG_IN_ONEORMORE)) |
| 6221 | && (cur->name == NULL)) { |
| 6222 | if (cur->ns == NULL) { |
| 6223 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ANYNAME_ATTR_ANCESTOR, |
| 6224 | "Found anyName attribute without oneOrMore ancestor\n", |
| 6225 | NULL, NULL); |
| 6226 | } else { |
| 6227 | xmlRngPErr(ctxt, cur->node, XML_RNGP_NSNAME_ATTR_ANCESTOR, |
| 6228 | "Found nsName attribute without oneOrMore ancestor\n", |
| 6229 | NULL, NULL); |
| 6230 | } |
| 6231 | } |
| 6232 | nflags = flags | XML_RELAXNG_IN_ATTRIBUTE; |
| 6233 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6234 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6235 | } else if ((cur->type == XML_RELAXNG_ONEORMORE) || |
| 6236 | (cur->type == XML_RELAXNG_ZEROORMORE)) { |
| 6237 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6238 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ONEMORE, |
| 6239 | "Found forbidden pattern data/except//oneOrMore\n", |
| 6240 | NULL, NULL); |
| 6241 | } |
| 6242 | if (flags & XML_RELAXNG_IN_START) { |
| 6243 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ONEMORE, |
| 6244 | "Found forbidden pattern start//oneOrMore\n", |
| 6245 | NULL, NULL); |
| 6246 | } |
| 6247 | nflags = flags | XML_RELAXNG_IN_ONEORMORE; |
| 6248 | ret = |
| 6249 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6250 | cur->type); |
| 6251 | ret = xmlRelaxNGGroupContentType(ret, ret); |
| 6252 | } else if (cur->type == XML_RELAXNG_LIST) { |
| 6253 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6254 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_LIST, |
| 6255 | "Found forbidden pattern list//list\n", NULL, |
| 6256 | NULL); |
| 6257 | } |
| 6258 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6259 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_LIST, |
| 6260 | "Found forbidden pattern data/except//list\n", |
| 6261 | NULL, NULL); |
| 6262 | } |
| 6263 | if (flags & XML_RELAXNG_IN_START) { |
| 6264 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_LIST, |
| 6265 | "Found forbidden pattern start//list\n", NULL, |
| 6266 | NULL); |
| 6267 | } |
| 6268 | nflags = flags | XML_RELAXNG_IN_LIST; |
| 6269 | ret = |
| 6270 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6271 | cur->type); |
| 6272 | } else if (cur->type == XML_RELAXNG_GROUP) { |
| 6273 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6274 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_GROUP, |
| 6275 | "Found forbidden pattern data/except//group\n", |
| 6276 | NULL, NULL); |
| 6277 | } |
| 6278 | if (flags & XML_RELAXNG_IN_START) { |
| 6279 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_GROUP, |
| 6280 | "Found forbidden pattern start//group\n", NULL, |
| 6281 | NULL); |
| 6282 | } |
| 6283 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
| 6284 | nflags = flags | XML_RELAXNG_IN_OOMGROUP; |
| 6285 | else |
| 6286 | nflags = flags; |
| 6287 | ret = |
| 6288 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6289 | cur->type); |
| 6290 | /* |
| 6291 | * The 7.3 Attribute derivation rule for groups is plugged there |
| 6292 | */ |
| 6293 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
| 6294 | } else if (cur->type == XML_RELAXNG_INTERLEAVE) { |
| 6295 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6296 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_INTERLEAVE, |
| 6297 | "Found forbidden pattern list//interleave\n", |
| 6298 | NULL, NULL); |
| 6299 | } |
| 6300 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6301 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE, |
| 6302 | "Found forbidden pattern data/except//interleave\n", |
| 6303 | NULL, NULL); |
| 6304 | } |
| 6305 | if (flags & XML_RELAXNG_IN_START) { |
| 6306 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE, |
| 6307 | "Found forbidden pattern start//interleave\n", |
| 6308 | NULL, NULL); |
| 6309 | } |
| 6310 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
| 6311 | nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE; |
| 6312 | else |
| 6313 | nflags = flags; |
| 6314 | ret = |
| 6315 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6316 | cur->type); |
| 6317 | } else if (cur->type == XML_RELAXNG_EXCEPT) { |
| 6318 | if ((cur->parent != NULL) && |
| 6319 | (cur->parent->type == XML_RELAXNG_DATATYPE)) |
| 6320 | nflags = flags | XML_RELAXNG_IN_DATAEXCEPT; |
| 6321 | else |
| 6322 | nflags = flags; |
| 6323 | ret = |
| 6324 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6325 | cur->type); |
| 6326 | } else if (cur->type == XML_RELAXNG_DATATYPE) { |
| 6327 | if (flags & XML_RELAXNG_IN_START) { |
| 6328 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_DATA, |
| 6329 | "Found forbidden pattern start//data\n", NULL, |
| 6330 | NULL); |
| 6331 | } |
| 6332 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6333 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
| 6334 | } else if (cur->type == XML_RELAXNG_VALUE) { |
| 6335 | if (flags & XML_RELAXNG_IN_START) { |
| 6336 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_VALUE, |
| 6337 | "Found forbidden pattern start//value\n", NULL, |
| 6338 | NULL); |
| 6339 | } |
| 6340 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6341 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
| 6342 | } else if (cur->type == XML_RELAXNG_TEXT) { |
| 6343 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6344 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_TEXT, |
| 6345 | "Found forbidden pattern list//text\n", NULL, |
| 6346 | NULL); |
| 6347 | } |
| 6348 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6349 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_TEXT, |
| 6350 | "Found forbidden pattern data/except//text\n", |
| 6351 | NULL, NULL); |
| 6352 | } |
| 6353 | if (flags & XML_RELAXNG_IN_START) { |
| 6354 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_TEXT, |
| 6355 | "Found forbidden pattern start//text\n", NULL, |
| 6356 | NULL); |
| 6357 | } |
| 6358 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6359 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 6360 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6361 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_EMPTY, |
| 6362 | "Found forbidden pattern data/except//empty\n", |
| 6363 | NULL, NULL); |
| 6364 | } |
| 6365 | if (flags & XML_RELAXNG_IN_START) { |
| 6366 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_EMPTY, |
| 6367 | "Found forbidden pattern start//empty\n", NULL, |
| 6368 | NULL); |
| 6369 | } |
| 6370 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6371 | } else if (cur->type == XML_RELAXNG_CHOICE) { |
| 6372 | xmlRelaxNGCheckChoiceDeterminism(ctxt, cur); |
| 6373 | ret = |
| 6374 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6375 | } else { |
| 6376 | ret = |
| 6377 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6378 | } |
| 6379 | cur = cur->next; |
| 6380 | if (ptype == XML_RELAXNG_GROUP) { |
| 6381 | val = xmlRelaxNGGroupContentType(val, ret); |
| 6382 | } else if (ptype == XML_RELAXNG_INTERLEAVE) { |
| 6383 | tmp = xmlRelaxNGGroupContentType(val, ret); |
| 6384 | if (tmp != XML_RELAXNG_CONTENT_ERROR) |
| 6385 | tmp = xmlRelaxNGMaxContentType(val, ret); |
| 6386 | } else if (ptype == XML_RELAXNG_CHOICE) { |
| 6387 | val = xmlRelaxNGMaxContentType(val, ret); |
| 6388 | } else if (ptype == XML_RELAXNG_LIST) { |
| 6389 | val = XML_RELAXNG_CONTENT_SIMPLE; |
| 6390 | } else if (ptype == XML_RELAXNG_EXCEPT) { |
| 6391 | if (ret == XML_RELAXNG_CONTENT_ERROR) |
| 6392 | val = XML_RELAXNG_CONTENT_ERROR; |
| 6393 | else |
| 6394 | val = XML_RELAXNG_CONTENT_SIMPLE; |
| 6395 | } else { |
| 6396 | val = xmlRelaxNGGroupContentType(val, ret); |
| 6397 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6398 | |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6399 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6400 | return (val); |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6401 | } |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6402 | |
| 6403 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6404 | * xmlRelaxNGParseGrammar: |
| 6405 | * @ctxt: a Relax-NG parser context |
| 6406 | * @nodes: grammar children nodes |
| 6407 | * |
| 6408 | * parse a Relax-NG <grammar> node |
| 6409 | * |
| 6410 | * Returns the internal xmlRelaxNGGrammarPtr built or |
| 6411 | * NULL in case of error |
| 6412 | */ |
| 6413 | static xmlRelaxNGGrammarPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6414 | xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) |
| 6415 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6416 | xmlRelaxNGGrammarPtr ret, tmp, old; |
| 6417 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6418 | #ifdef DEBUG_GRAMMAR |
| 6419 | xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n"); |
| 6420 | #endif |
| 6421 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6422 | ret = xmlRelaxNGNewGrammar(ctxt); |
| 6423 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6424 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6425 | |
| 6426 | /* |
| 6427 | * Link the new grammar in the tree |
| 6428 | */ |
| 6429 | ret->parent = ctxt->grammar; |
| 6430 | if (ctxt->grammar != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6431 | tmp = ctxt->grammar->children; |
| 6432 | if (tmp == NULL) { |
| 6433 | ctxt->grammar->children = ret; |
| 6434 | } else { |
| 6435 | while (tmp->next != NULL) |
| 6436 | tmp = tmp->next; |
| 6437 | tmp->next = ret; |
| 6438 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6439 | } |
| 6440 | |
| 6441 | old = ctxt->grammar; |
| 6442 | ctxt->grammar = ret; |
| 6443 | xmlRelaxNGParseGrammarContent(ctxt, nodes); |
| 6444 | ctxt->grammar = ret; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6445 | if (ctxt->grammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6446 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT, |
| 6447 | "Failed to parse <grammar> content\n", NULL, NULL); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6448 | } else if (ctxt->grammar->start == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6449 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_NO_START, |
| 6450 | "Element <grammar> has no <start>\n", NULL, NULL); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6451 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6452 | |
| 6453 | /* |
| 6454 | * Apply 4.17 mergingd rules to defines and starts |
| 6455 | */ |
| 6456 | xmlRelaxNGCombineStart(ctxt, ret); |
| 6457 | if (ret->defs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6458 | xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine, |
| 6459 | ctxt); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6460 | } |
| 6461 | |
| 6462 | /* |
| 6463 | * link together defines and refs in this grammar |
| 6464 | */ |
| 6465 | if (ret->refs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6466 | xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference, |
| 6467 | ctxt); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6468 | } |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 6469 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6470 | ctxt->grammar = old; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6471 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6472 | } |
| 6473 | |
| 6474 | /** |
| 6475 | * xmlRelaxNGParseDocument: |
| 6476 | * @ctxt: a Relax-NG parser context |
| 6477 | * @node: the root node of the RelaxNG schema |
| 6478 | * |
| 6479 | * parse a Relax-NG definition resource and build an internal |
| 6480 | * xmlRelaxNG struture which can be used to validate instances. |
| 6481 | * |
| 6482 | * Returns the internal XML RelaxNG structure built or |
| 6483 | * NULL in case of error |
| 6484 | */ |
| 6485 | static xmlRelaxNGPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6486 | xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 6487 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6488 | xmlRelaxNGPtr schema = NULL; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6489 | const xmlChar *olddefine; |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 6490 | xmlRelaxNGGrammarPtr old; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6491 | |
| 6492 | if ((ctxt == NULL) || (node == NULL)) |
| 6493 | return (NULL); |
| 6494 | |
| 6495 | schema = xmlRelaxNGNewRelaxNG(ctxt); |
| 6496 | if (schema == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6497 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6498 | |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6499 | olddefine = ctxt->define; |
| 6500 | ctxt->define = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6501 | if (IS_RELAXNG(node, "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6502 | schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6503 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6504 | xmlRelaxNGGrammarPtr tmp, ret; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6505 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6506 | schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt); |
| 6507 | if (schema->topgrammar == NULL) { |
| 6508 | return (schema); |
| 6509 | } |
| 6510 | /* |
| 6511 | * Link the new grammar in the tree |
| 6512 | */ |
| 6513 | ret->parent = ctxt->grammar; |
| 6514 | if (ctxt->grammar != NULL) { |
| 6515 | tmp = ctxt->grammar->children; |
| 6516 | if (tmp == NULL) { |
| 6517 | ctxt->grammar->children = ret; |
| 6518 | } else { |
| 6519 | while (tmp->next != NULL) |
| 6520 | tmp = tmp->next; |
| 6521 | tmp->next = ret; |
| 6522 | } |
| 6523 | } |
| 6524 | old = ctxt->grammar; |
| 6525 | ctxt->grammar = ret; |
| 6526 | xmlRelaxNGParseStart(ctxt, node); |
| 6527 | if (old != NULL) |
| 6528 | ctxt->grammar = old; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6529 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6530 | ctxt->define = olddefine; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 6531 | if (schema->topgrammar->start != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6532 | xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0); |
| 6533 | if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) { |
| 6534 | xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL); |
| 6535 | while ((schema->topgrammar->start != NULL) && |
| 6536 | (schema->topgrammar->start->type == XML_RELAXNG_NOOP) && |
| 6537 | (schema->topgrammar->start->next != NULL)) |
| 6538 | schema->topgrammar->start = |
| 6539 | schema->topgrammar->start->content; |
| 6540 | xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start, |
| 6541 | XML_RELAXNG_IN_START, XML_RELAXNG_NOOP); |
| 6542 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 6543 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6544 | #ifdef DEBUG |
| 6545 | if (schema == NULL) |
| 6546 | xmlGenericError(xmlGenericErrorContext, |
| 6547 | "xmlRelaxNGParseDocument() failed\n"); |
| 6548 | #endif |
| 6549 | |
| 6550 | return (schema); |
| 6551 | } |
| 6552 | |
| 6553 | /************************************************************************ |
| 6554 | * * |
| 6555 | * Reading RelaxNGs * |
| 6556 | * * |
| 6557 | ************************************************************************/ |
| 6558 | |
| 6559 | /** |
| 6560 | * xmlRelaxNGNewParserCtxt: |
| 6561 | * @URL: the location of the schema |
| 6562 | * |
| 6563 | * Create an XML RelaxNGs parse context for that file/resource expected |
| 6564 | * to contain an XML RelaxNGs file. |
| 6565 | * |
| 6566 | * Returns the parser context or NULL in case of error |
| 6567 | */ |
| 6568 | xmlRelaxNGParserCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6569 | xmlRelaxNGNewParserCtxt(const char *URL) |
| 6570 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6571 | xmlRelaxNGParserCtxtPtr ret; |
| 6572 | |
| 6573 | if (URL == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6574 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6575 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6576 | ret = |
| 6577 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6578 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6579 | xmlRngPErrMemory(NULL, "building parser\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6580 | return (NULL); |
| 6581 | } |
| 6582 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6583 | ret->URL = xmlStrdup((const xmlChar *) URL); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 6584 | ret->error = xmlGenericError; |
| 6585 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6586 | return (ret); |
| 6587 | } |
| 6588 | |
| 6589 | /** |
| 6590 | * xmlRelaxNGNewMemParserCtxt: |
| 6591 | * @buffer: a pointer to a char array containing the schemas |
| 6592 | * @size: the size of the array |
| 6593 | * |
| 6594 | * Create an XML RelaxNGs parse context for that memory buffer expected |
| 6595 | * to contain an XML RelaxNGs file. |
| 6596 | * |
| 6597 | * Returns the parser context or NULL in case of error |
| 6598 | */ |
| 6599 | xmlRelaxNGParserCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6600 | xmlRelaxNGNewMemParserCtxt(const char *buffer, int size) |
| 6601 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6602 | xmlRelaxNGParserCtxtPtr ret; |
| 6603 | |
| 6604 | if ((buffer == NULL) || (size <= 0)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6605 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6606 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6607 | ret = |
| 6608 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6609 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6610 | xmlRngPErrMemory(NULL, "building parser\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6611 | return (NULL); |
| 6612 | } |
| 6613 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6614 | ret->buffer = buffer; |
| 6615 | ret->size = size; |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 6616 | ret->error = xmlGenericError; |
| 6617 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6618 | return (ret); |
| 6619 | } |
| 6620 | |
| 6621 | /** |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6622 | * xmlRelaxNGNewDocParserCtxt: |
| 6623 | * @doc: a preparsed document tree |
| 6624 | * |
| 6625 | * Create an XML RelaxNGs parser context for that document. |
| 6626 | * Note: since the process of compiling a RelaxNG schemas modifies the |
| 6627 | * document, the @doc parameter is duplicated internally. |
| 6628 | * |
| 6629 | * Returns the parser context or NULL in case of error |
| 6630 | */ |
| 6631 | xmlRelaxNGParserCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6632 | xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc) |
| 6633 | { |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6634 | xmlRelaxNGParserCtxtPtr ret; |
| 6635 | xmlDocPtr copy; |
| 6636 | |
| 6637 | if (doc == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6638 | return (NULL); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6639 | copy = xmlCopyDoc(doc, 1); |
| 6640 | if (copy == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6641 | return (NULL); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6642 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6643 | ret = |
| 6644 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6645 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6646 | xmlRngPErrMemory(NULL, "building parser\n"); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6647 | return (NULL); |
| 6648 | } |
| 6649 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6650 | ret->document = copy; |
| 6651 | ret->userData = xmlGenericErrorContext; |
| 6652 | return (ret); |
| 6653 | } |
| 6654 | |
| 6655 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6656 | * xmlRelaxNGFreeParserCtxt: |
| 6657 | * @ctxt: the schema parser context |
| 6658 | * |
| 6659 | * Free the resources associated to the schema parser context |
| 6660 | */ |
| 6661 | void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6662 | xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt) |
| 6663 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6664 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6665 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6666 | if (ctxt->URL != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6667 | xmlFree(ctxt->URL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6668 | if (ctxt->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6669 | xmlRelaxNGFreeDocument(ctxt->doc); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 6670 | if (ctxt->interleaves != NULL) |
| 6671 | xmlHashFree(ctxt->interleaves, NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6672 | if (ctxt->documents != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6673 | xmlRelaxNGFreeDocumentList(ctxt->documents); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 6674 | if (ctxt->includes != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6675 | xmlRelaxNGFreeIncludeList(ctxt->includes); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6676 | if (ctxt->docTab != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6677 | xmlFree(ctxt->docTab); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 6678 | if (ctxt->incTab != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6679 | xmlFree(ctxt->incTab); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6680 | if (ctxt->defTab != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6681 | int i; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6682 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6683 | for (i = 0; i < ctxt->defNr; i++) |
| 6684 | xmlRelaxNGFreeDefine(ctxt->defTab[i]); |
| 6685 | xmlFree(ctxt->defTab); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6686 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6687 | xmlFree(ctxt); |
| 6688 | } |
| 6689 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6690 | /** |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6691 | * xmlRelaxNGNormExtSpace: |
| 6692 | * @value: a value |
| 6693 | * |
| 6694 | * Removes the leading and ending spaces of the value |
| 6695 | * The string is modified "in situ" |
| 6696 | */ |
| 6697 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6698 | xmlRelaxNGNormExtSpace(xmlChar * value) |
| 6699 | { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6700 | xmlChar *start = value; |
| 6701 | xmlChar *cur = value; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6702 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6703 | if (value == NULL) |
| 6704 | return; |
| 6705 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6706 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6707 | cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6708 | if (cur == start) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6709 | do { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6710 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6711 | cur++; |
| 6712 | if (*cur == 0) |
| 6713 | return; |
| 6714 | start = cur; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6715 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6716 | cur++; |
| 6717 | if (*cur == 0) { |
| 6718 | *start = 0; |
| 6719 | return; |
| 6720 | } |
| 6721 | } while (1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6722 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6723 | do { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6724 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6725 | *start++ = *cur++; |
| 6726 | if (*cur == 0) { |
| 6727 | *start = 0; |
| 6728 | return; |
| 6729 | } |
| 6730 | /* don't try to normalize the inner spaces */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6731 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6732 | cur++; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6733 | if (*cur == 0) { |
| 6734 | *start = 0; |
| 6735 | return; |
| 6736 | } |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 6737 | *start++ = *cur++; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6738 | } while (1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6739 | } |
| 6740 | } |
| 6741 | |
| 6742 | /** |
| 6743 | * xmlRelaxNGCheckAttributes: |
| 6744 | * @ctxt: a Relax-NG parser context |
| 6745 | * @node: a Relax-NG node |
| 6746 | * |
| 6747 | * Check all the attributes on the given node |
| 6748 | */ |
| 6749 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6750 | xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 6751 | { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6752 | xmlAttrPtr cur, next; |
| 6753 | |
| 6754 | cur = node->properties; |
| 6755 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6756 | next = cur->next; |
| 6757 | if ((cur->ns == NULL) || |
| 6758 | (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
| 6759 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
| 6760 | if ((!xmlStrEqual(node->name, BAD_CAST "element")) && |
| 6761 | (!xmlStrEqual(node->name, BAD_CAST "attribute")) && |
| 6762 | (!xmlStrEqual(node->name, BAD_CAST "ref")) && |
| 6763 | (!xmlStrEqual(node->name, BAD_CAST "parentRef")) && |
| 6764 | (!xmlStrEqual(node->name, BAD_CAST "param")) && |
| 6765 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
| 6766 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6767 | "Attribute %s is not allowed on %s\n", |
| 6768 | cur->name, node->name); |
| 6769 | } |
| 6770 | } else if (xmlStrEqual(cur->name, BAD_CAST "type")) { |
| 6771 | if ((!xmlStrEqual(node->name, BAD_CAST "value")) && |
| 6772 | (!xmlStrEqual(node->name, BAD_CAST "data"))) { |
| 6773 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6774 | "Attribute %s is not allowed on %s\n", |
| 6775 | cur->name, node->name); |
| 6776 | } |
| 6777 | } else if (xmlStrEqual(cur->name, BAD_CAST "href")) { |
| 6778 | if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) && |
| 6779 | (!xmlStrEqual(node->name, BAD_CAST "include"))) { |
| 6780 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6781 | "Attribute %s is not allowed on %s\n", |
| 6782 | cur->name, node->name); |
| 6783 | } |
| 6784 | } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) { |
| 6785 | if ((!xmlStrEqual(node->name, BAD_CAST "start")) && |
| 6786 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
| 6787 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6788 | "Attribute %s is not allowed on %s\n", |
| 6789 | cur->name, node->name); |
| 6790 | } |
| 6791 | } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) { |
| 6792 | xmlChar *val; |
| 6793 | xmlURIPtr uri; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6794 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6795 | val = xmlNodeListGetString(node->doc, cur->children, 1); |
| 6796 | if (val != NULL) { |
| 6797 | if (val[0] != 0) { |
| 6798 | uri = xmlParseURI((const char *) val); |
| 6799 | if (uri == NULL) { |
| 6800 | xmlRngPErr(ctxt, node, XML_RNGP_INVALID_URI, |
| 6801 | "Attribute %s contains invalid URI %s\n", |
| 6802 | cur->name, val); |
| 6803 | } else { |
| 6804 | if (uri->scheme == NULL) { |
| 6805 | xmlRngPErr(ctxt, node, XML_RNGP_URI_NOT_ABSOLUTE, |
| 6806 | "Attribute %s URI %s is not absolute\n", |
| 6807 | cur->name, val); |
| 6808 | } |
| 6809 | if (uri->fragment != NULL) { |
| 6810 | xmlRngPErr(ctxt, node, XML_RNGP_URI_FRAGMENT, |
| 6811 | "Attribute %s URI %s has a fragment ID\n", |
| 6812 | cur->name, val); |
| 6813 | } |
| 6814 | xmlFreeURI(uri); |
| 6815 | } |
| 6816 | } |
| 6817 | xmlFree(val); |
| 6818 | } |
| 6819 | } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) { |
| 6820 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_ATTRIBUTE, |
| 6821 | "Unknown attribute %s on %s\n", cur->name, |
| 6822 | node->name); |
| 6823 | } |
| 6824 | } |
| 6825 | cur = next; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6826 | } |
| 6827 | } |
| 6828 | |
| 6829 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6830 | * xmlRelaxNGCleanupTree: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6831 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6832 | * @root: an xmlNodePtr subtree |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6833 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6834 | * Cleanup the subtree from unwanted nodes for parsing, resolve |
| 6835 | * Include and externalRef lookups. |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6836 | */ |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6837 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6838 | xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root) |
| 6839 | { |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6840 | xmlNodePtr cur, delete; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6841 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6842 | delete = NULL; |
| 6843 | cur = root; |
| 6844 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6845 | if (delete != NULL) { |
| 6846 | xmlUnlinkNode(delete); |
| 6847 | xmlFreeNode(delete); |
| 6848 | delete = NULL; |
| 6849 | } |
| 6850 | if (cur->type == XML_ELEMENT_NODE) { |
| 6851 | /* |
| 6852 | * Simplification 4.1. Annotations |
| 6853 | */ |
| 6854 | if ((cur->ns == NULL) || |
| 6855 | (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
| 6856 | if ((cur->parent != NULL) && |
| 6857 | (cur->parent->type == XML_ELEMENT_NODE) && |
| 6858 | ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) || |
| 6859 | (xmlStrEqual(cur->parent->name, BAD_CAST "value")) || |
| 6860 | (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) { |
| 6861 | xmlRngPErr(ctxt, cur, XML_RNGP_FOREIGN_ELEMENT, |
| 6862 | "element %s doesn't allow foreign elements\n", |
| 6863 | cur->parent->name, NULL); |
| 6864 | } |
| 6865 | delete = cur; |
| 6866 | goto skip_children; |
| 6867 | } else { |
| 6868 | xmlRelaxNGCleanupAttributes(ctxt, cur); |
| 6869 | if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) { |
| 6870 | xmlChar *href, *ns, *base, *URL; |
| 6871 | xmlRelaxNGDocumentPtr docu; |
| 6872 | xmlNodePtr tmp; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6873 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6874 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6875 | if (ns == NULL) { |
| 6876 | tmp = cur->parent; |
| 6877 | while ((tmp != NULL) && |
| 6878 | (tmp->type == XML_ELEMENT_NODE)) { |
| 6879 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 6880 | if (ns != NULL) |
| 6881 | break; |
| 6882 | tmp = tmp->parent; |
| 6883 | } |
| 6884 | } |
| 6885 | href = xmlGetProp(cur, BAD_CAST "href"); |
| 6886 | if (href == NULL) { |
| 6887 | xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF, |
| 6888 | "xmlRelaxNGParse: externalRef has no href attribute\n", |
| 6889 | NULL, NULL); |
| 6890 | delete = cur; |
| 6891 | goto skip_children; |
| 6892 | } |
| 6893 | base = xmlNodeGetBase(cur->doc, cur); |
| 6894 | URL = xmlBuildURI(href, base); |
| 6895 | if (URL == NULL) { |
| 6896 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
| 6897 | "Failed to compute URL for externalRef %s\n", |
| 6898 | href, NULL); |
| 6899 | if (href != NULL) |
| 6900 | xmlFree(href); |
| 6901 | if (base != NULL) |
| 6902 | xmlFree(base); |
| 6903 | delete = cur; |
| 6904 | goto skip_children; |
| 6905 | } |
| 6906 | if (href != NULL) |
| 6907 | xmlFree(href); |
| 6908 | if (base != NULL) |
| 6909 | xmlFree(base); |
| 6910 | docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns); |
| 6911 | if (docu == NULL) { |
| 6912 | xmlRngPErr(ctxt, cur, XML_RNGP_EXTERNAL_REF_FAILURE, |
| 6913 | "Failed to load externalRef %s\n", URL, |
| 6914 | NULL); |
| 6915 | xmlFree(URL); |
| 6916 | delete = cur; |
| 6917 | goto skip_children; |
| 6918 | } |
| 6919 | if (ns != NULL) |
| 6920 | xmlFree(ns); |
| 6921 | xmlFree(URL); |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 6922 | cur->psvi = docu; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6923 | } else if (xmlStrEqual(cur->name, BAD_CAST "include")) { |
| 6924 | xmlChar *href, *ns, *base, *URL; |
| 6925 | xmlRelaxNGIncludePtr incl; |
| 6926 | xmlNodePtr tmp; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6927 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6928 | href = xmlGetProp(cur, BAD_CAST "href"); |
| 6929 | if (href == NULL) { |
| 6930 | xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF, |
| 6931 | "xmlRelaxNGParse: include has no href attribute\n", |
| 6932 | NULL, NULL); |
| 6933 | delete = cur; |
| 6934 | goto skip_children; |
| 6935 | } |
| 6936 | base = xmlNodeGetBase(cur->doc, cur); |
| 6937 | URL = xmlBuildURI(href, base); |
| 6938 | if (URL == NULL) { |
| 6939 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
| 6940 | "Failed to compute URL for include %s\n", |
| 6941 | href, NULL); |
| 6942 | if (href != NULL) |
| 6943 | xmlFree(href); |
| 6944 | if (base != NULL) |
| 6945 | xmlFree(base); |
| 6946 | delete = cur; |
| 6947 | goto skip_children; |
| 6948 | } |
| 6949 | if (href != NULL) |
| 6950 | xmlFree(href); |
| 6951 | if (base != NULL) |
| 6952 | xmlFree(base); |
| 6953 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6954 | if (ns == NULL) { |
| 6955 | tmp = cur->parent; |
| 6956 | while ((tmp != NULL) && |
| 6957 | (tmp->type == XML_ELEMENT_NODE)) { |
| 6958 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 6959 | if (ns != NULL) |
| 6960 | break; |
| 6961 | tmp = tmp->parent; |
| 6962 | } |
| 6963 | } |
| 6964 | incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns); |
| 6965 | if (ns != NULL) |
| 6966 | xmlFree(ns); |
| 6967 | if (incl == NULL) { |
| 6968 | xmlRngPErr(ctxt, cur, XML_RNGP_INCLUDE_FAILURE, |
| 6969 | "Failed to load include %s\n", URL, |
| 6970 | NULL); |
| 6971 | xmlFree(URL); |
| 6972 | delete = cur; |
| 6973 | goto skip_children; |
| 6974 | } |
| 6975 | xmlFree(URL); |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 6976 | cur->psvi = incl; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6977 | } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) || |
| 6978 | (xmlStrEqual(cur->name, BAD_CAST "attribute"))) |
| 6979 | { |
| 6980 | xmlChar *name, *ns; |
| 6981 | xmlNodePtr text = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6982 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6983 | /* |
| 6984 | * Simplification 4.8. name attribute of element |
| 6985 | * and attribute elements |
| 6986 | */ |
| 6987 | name = xmlGetProp(cur, BAD_CAST "name"); |
| 6988 | if (name != NULL) { |
| 6989 | if (cur->children == NULL) { |
| 6990 | text = |
| 6991 | xmlNewChild(cur, cur->ns, BAD_CAST "name", |
| 6992 | name); |
| 6993 | } else { |
| 6994 | xmlNodePtr node; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6995 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6996 | node = xmlNewNode(cur->ns, BAD_CAST "name"); |
| 6997 | if (node != NULL) { |
| 6998 | xmlAddPrevSibling(cur->children, node); |
| 6999 | text = xmlNewText(name); |
| 7000 | xmlAddChild(node, text); |
| 7001 | text = node; |
| 7002 | } |
| 7003 | } |
| 7004 | if (text == NULL) { |
| 7005 | xmlRngPErr(ctxt, cur, XML_RNGP_CREATE_FAILURE, |
| 7006 | "Failed to create a name %s element\n", |
| 7007 | name, NULL); |
| 7008 | } |
| 7009 | xmlUnsetProp(cur, BAD_CAST "name"); |
| 7010 | xmlFree(name); |
| 7011 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 7012 | if (ns != NULL) { |
| 7013 | if (text != NULL) { |
| 7014 | xmlSetProp(text, BAD_CAST "ns", ns); |
| 7015 | /* xmlUnsetProp(cur, BAD_CAST "ns"); */ |
| 7016 | } |
| 7017 | xmlFree(ns); |
| 7018 | } else if (xmlStrEqual(cur->name, |
| 7019 | BAD_CAST "attribute")) { |
| 7020 | xmlSetProp(text, BAD_CAST "ns", BAD_CAST ""); |
| 7021 | } |
| 7022 | } |
| 7023 | } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) || |
| 7024 | (xmlStrEqual(cur->name, BAD_CAST "nsName")) || |
| 7025 | (xmlStrEqual(cur->name, BAD_CAST "value"))) { |
| 7026 | /* |
| 7027 | * Simplification 4.8. name attribute of element |
| 7028 | * and attribute elements |
| 7029 | */ |
| 7030 | if (xmlHasProp(cur, BAD_CAST "ns") == NULL) { |
| 7031 | xmlNodePtr node; |
| 7032 | xmlChar *ns = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7033 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7034 | node = cur->parent; |
| 7035 | while ((node != NULL) && |
| 7036 | (node->type == XML_ELEMENT_NODE)) { |
| 7037 | ns = xmlGetProp(node, BAD_CAST "ns"); |
| 7038 | if (ns != NULL) { |
| 7039 | break; |
| 7040 | } |
| 7041 | node = node->parent; |
| 7042 | } |
| 7043 | if (ns == NULL) { |
| 7044 | xmlSetProp(cur, BAD_CAST "ns", BAD_CAST ""); |
| 7045 | } else { |
| 7046 | xmlSetProp(cur, BAD_CAST "ns", ns); |
| 7047 | xmlFree(ns); |
| 7048 | } |
| 7049 | } |
| 7050 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
| 7051 | xmlChar *name, *local, *prefix; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7052 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7053 | /* |
| 7054 | * Simplification: 4.10. QNames |
| 7055 | */ |
| 7056 | name = xmlNodeGetContent(cur); |
| 7057 | if (name != NULL) { |
| 7058 | local = xmlSplitQName2(name, &prefix); |
| 7059 | if (local != NULL) { |
| 7060 | xmlNsPtr ns; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7061 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7062 | ns = xmlSearchNs(cur->doc, cur, prefix); |
| 7063 | if (ns == NULL) { |
| 7064 | xmlRngPErr(ctxt, cur, |
| 7065 | XML_RNGP_PREFIX_UNDEFINED, |
| 7066 | "xmlRelaxNGParse: no namespace for prefix %s\n", |
| 7067 | prefix, NULL); |
| 7068 | } else { |
| 7069 | xmlSetProp(cur, BAD_CAST "ns", |
| 7070 | ns->href); |
| 7071 | xmlNodeSetContent(cur, local); |
| 7072 | } |
| 7073 | xmlFree(local); |
| 7074 | xmlFree(prefix); |
| 7075 | } |
| 7076 | xmlFree(name); |
| 7077 | } |
| 7078 | } |
| 7079 | /* |
| 7080 | * 4.16 |
| 7081 | */ |
| 7082 | if (xmlStrEqual(cur->name, BAD_CAST "nsName")) { |
| 7083 | if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
| 7084 | xmlRngPErr(ctxt, cur, |
| 7085 | XML_RNGP_PAT_NSNAME_EXCEPT_NSNAME, |
| 7086 | "Found nsName/except//nsName forbidden construct\n", |
| 7087 | NULL, NULL); |
| 7088 | } |
| 7089 | } |
| 7090 | } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) && |
| 7091 | (cur != root)) { |
| 7092 | int oldflags = ctxt->flags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7093 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7094 | /* |
| 7095 | * 4.16 |
| 7096 | */ |
| 7097 | if ((cur->parent != NULL) && |
| 7098 | (xmlStrEqual |
| 7099 | (cur->parent->name, BAD_CAST "anyName"))) { |
| 7100 | ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT; |
| 7101 | xmlRelaxNGCleanupTree(ctxt, cur); |
| 7102 | ctxt->flags = oldflags; |
| 7103 | goto skip_children; |
| 7104 | } else if ((cur->parent != NULL) && |
| 7105 | (xmlStrEqual |
| 7106 | (cur->parent->name, BAD_CAST "nsName"))) { |
| 7107 | ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT; |
| 7108 | xmlRelaxNGCleanupTree(ctxt, cur); |
| 7109 | ctxt->flags = oldflags; |
| 7110 | goto skip_children; |
| 7111 | } |
| 7112 | } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) { |
| 7113 | /* |
| 7114 | * 4.16 |
| 7115 | */ |
| 7116 | if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) { |
| 7117 | xmlRngPErr(ctxt, cur, |
| 7118 | XML_RNGP_PAT_ANYNAME_EXCEPT_ANYNAME, |
| 7119 | "Found anyName/except//anyName forbidden construct\n", |
| 7120 | NULL, NULL); |
| 7121 | } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
| 7122 | xmlRngPErr(ctxt, cur, |
| 7123 | XML_RNGP_PAT_NSNAME_EXCEPT_ANYNAME, |
| 7124 | "Found nsName/except//anyName forbidden construct\n", |
| 7125 | NULL, NULL); |
| 7126 | } |
| 7127 | } |
| 7128 | /* |
| 7129 | * Thisd is not an else since "include" is transformed |
| 7130 | * into a div |
| 7131 | */ |
| 7132 | if (xmlStrEqual(cur->name, BAD_CAST "div")) { |
| 7133 | xmlChar *ns; |
| 7134 | xmlNodePtr child, ins, tmp; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7135 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7136 | /* |
| 7137 | * implements rule 4.11 |
| 7138 | */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7139 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7140 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 7141 | |
| 7142 | child = cur->children; |
| 7143 | ins = cur; |
| 7144 | while (child != NULL) { |
| 7145 | if (ns != NULL) { |
| 7146 | if (!xmlHasProp(child, BAD_CAST "ns")) { |
| 7147 | xmlSetProp(child, BAD_CAST "ns", ns); |
| 7148 | } |
| 7149 | } |
| 7150 | tmp = child->next; |
| 7151 | xmlUnlinkNode(child); |
| 7152 | ins = xmlAddNextSibling(ins, child); |
| 7153 | child = tmp; |
| 7154 | } |
| 7155 | if (ns != NULL) |
| 7156 | xmlFree(ns); |
| 7157 | delete = cur; |
| 7158 | goto skip_children; |
| 7159 | } |
| 7160 | } |
| 7161 | } |
| 7162 | /* |
| 7163 | * Simplification 4.2 whitespaces |
| 7164 | */ |
| 7165 | else if ((cur->type == XML_TEXT_NODE) || |
| 7166 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 7167 | if (IS_BLANK_NODE(cur)) { |
| 7168 | if (cur->parent->type == XML_ELEMENT_NODE) { |
| 7169 | if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value")) |
| 7170 | && |
| 7171 | (!xmlStrEqual |
| 7172 | (cur->parent->name, BAD_CAST "param"))) |
| 7173 | delete = cur; |
| 7174 | } else { |
| 7175 | delete = cur; |
| 7176 | goto skip_children; |
| 7177 | } |
| 7178 | } |
| 7179 | } else { |
| 7180 | delete = cur; |
| 7181 | goto skip_children; |
| 7182 | } |
| 7183 | |
| 7184 | /* |
| 7185 | * Skip to next node |
| 7186 | */ |
| 7187 | if (cur->children != NULL) { |
| 7188 | if ((cur->children->type != XML_ENTITY_DECL) && |
| 7189 | (cur->children->type != XML_ENTITY_REF_NODE) && |
| 7190 | (cur->children->type != XML_ENTITY_NODE)) { |
| 7191 | cur = cur->children; |
| 7192 | continue; |
| 7193 | } |
| 7194 | } |
| 7195 | skip_children: |
| 7196 | if (cur->next != NULL) { |
| 7197 | cur = cur->next; |
| 7198 | continue; |
| 7199 | } |
| 7200 | |
| 7201 | do { |
| 7202 | cur = cur->parent; |
| 7203 | if (cur == NULL) |
| 7204 | break; |
| 7205 | if (cur == root) { |
| 7206 | cur = NULL; |
| 7207 | break; |
| 7208 | } |
| 7209 | if (cur->next != NULL) { |
| 7210 | cur = cur->next; |
| 7211 | break; |
| 7212 | } |
| 7213 | } while (cur != NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7214 | } |
| 7215 | if (delete != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7216 | xmlUnlinkNode(delete); |
| 7217 | xmlFreeNode(delete); |
| 7218 | delete = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7219 | } |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7220 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7221 | |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7222 | /** |
| 7223 | * xmlRelaxNGCleanupDoc: |
| 7224 | * @ctxt: a Relax-NG parser context |
| 7225 | * @doc: an xmldocPtr document pointer |
| 7226 | * |
| 7227 | * Cleanup the document from unwanted nodes for parsing, resolve |
| 7228 | * Include and externalRef lookups. |
| 7229 | * |
| 7230 | * Returns the cleaned up document or NULL in case of error |
| 7231 | */ |
| 7232 | static xmlDocPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7233 | xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc) |
| 7234 | { |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7235 | xmlNodePtr root; |
| 7236 | |
| 7237 | /* |
| 7238 | * Extract the root |
| 7239 | */ |
| 7240 | root = xmlDocGetRootElement(doc); |
| 7241 | if (root == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7242 | xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n", |
| 7243 | ctxt->URL, NULL); |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7244 | return (NULL); |
| 7245 | } |
| 7246 | xmlRelaxNGCleanupTree(ctxt, root); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7247 | return (doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7248 | } |
| 7249 | |
| 7250 | /** |
| 7251 | * xmlRelaxNGParse: |
| 7252 | * @ctxt: a Relax-NG parser context |
| 7253 | * |
| 7254 | * parse a schema definition resource and build an internal |
| 7255 | * XML Shema struture which can be used to validate instances. |
| 7256 | * *WARNING* this interface is highly subject to change |
| 7257 | * |
| 7258 | * Returns the internal XML RelaxNG structure built from the resource or |
| 7259 | * NULL in case of error |
| 7260 | */ |
| 7261 | xmlRelaxNGPtr |
| 7262 | xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt) |
| 7263 | { |
| 7264 | xmlRelaxNGPtr ret = NULL; |
| 7265 | xmlDocPtr doc; |
| 7266 | xmlNodePtr root; |
| 7267 | |
| 7268 | xmlRelaxNGInitTypes(); |
| 7269 | |
| 7270 | if (ctxt == NULL) |
| 7271 | return (NULL); |
| 7272 | |
| 7273 | /* |
| 7274 | * First step is to parse the input document into an DOM/Infoset |
| 7275 | */ |
| 7276 | if (ctxt->URL != NULL) { |
Daniel Veillard | 87247e8 | 2004-01-13 20:42:02 +0000 | [diff] [blame] | 7277 | doc = xmlReadFile((const char *) ctxt->URL,NULL,0); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7278 | if (doc == NULL) { |
| 7279 | xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR, |
| 7280 | "xmlRelaxNGParse: could not load %s\n", ctxt->URL, |
| 7281 | NULL); |
| 7282 | return (NULL); |
| 7283 | } |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7284 | } else if (ctxt->buffer != NULL) { |
Daniel Veillard | 87247e8 | 2004-01-13 20:42:02 +0000 | [diff] [blame] | 7285 | doc = xmlReadMemory(ctxt->buffer, ctxt->size,NULL,NULL,0); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7286 | if (doc == NULL) { |
| 7287 | xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR, |
| 7288 | "xmlRelaxNGParse: could not parse schemas\n", NULL, |
| 7289 | NULL); |
| 7290 | return (NULL); |
| 7291 | } |
| 7292 | doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
| 7293 | ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 7294 | } else if (ctxt->document != NULL) { |
| 7295 | doc = ctxt->document; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7296 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7297 | xmlRngPErr(ctxt, NULL, XML_RNGP_EMPTY, |
| 7298 | "xmlRelaxNGParse: nothing to parse\n", NULL, NULL); |
| 7299 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7300 | } |
| 7301 | ctxt->document = doc; |
| 7302 | |
| 7303 | /* |
| 7304 | * Some preprocessing of the document content |
| 7305 | */ |
| 7306 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 7307 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7308 | xmlFreeDoc(ctxt->document); |
| 7309 | ctxt->document = NULL; |
| 7310 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7311 | } |
| 7312 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7313 | /* |
| 7314 | * Then do the parsing for good |
| 7315 | */ |
| 7316 | root = xmlDocGetRootElement(doc); |
| 7317 | if (root == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7318 | xmlRngPErr(ctxt, (xmlNodePtr) doc, |
| 7319 | XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n", |
| 7320 | ctxt->URL, NULL); |
| 7321 | xmlFreeDoc(doc); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7322 | return (NULL); |
| 7323 | } |
| 7324 | ret = xmlRelaxNGParseDocument(ctxt, root); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7325 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7326 | xmlFreeDoc(doc); |
| 7327 | return (NULL); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7328 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7329 | |
| 7330 | /* |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7331 | * Check the ref/defines links |
| 7332 | */ |
| 7333 | /* |
| 7334 | * try to preprocess interleaves |
| 7335 | */ |
| 7336 | if (ctxt->interleaves != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7337 | xmlHashScan(ctxt->interleaves, |
| 7338 | (xmlHashScanner) xmlRelaxNGComputeInterleaves, ctxt); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7339 | } |
| 7340 | |
| 7341 | /* |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7342 | * if there was a parsing error return NULL |
| 7343 | */ |
| 7344 | if (ctxt->nbErrors > 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7345 | xmlRelaxNGFree(ret); |
| 7346 | ctxt->document = NULL; |
| 7347 | xmlFreeDoc(doc); |
| 7348 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7349 | } |
| 7350 | |
| 7351 | /* |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 7352 | * try to compile (parts of) the schemas |
| 7353 | */ |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7354 | if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) { |
| 7355 | if (ret->topgrammar->start->type != XML_RELAXNG_START) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7356 | xmlRelaxNGDefinePtr def; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7357 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7358 | def = xmlRelaxNGNewDefine(ctxt, NULL); |
| 7359 | if (def != NULL) { |
| 7360 | def->type = XML_RELAXNG_START; |
| 7361 | def->content = ret->topgrammar->start; |
| 7362 | ret->topgrammar->start = def; |
| 7363 | } |
| 7364 | } |
| 7365 | xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7366 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 7367 | |
| 7368 | /* |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7369 | * Transfer the pointer for cleanup at the schema level. |
| 7370 | */ |
| 7371 | ret->doc = doc; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7372 | ctxt->document = NULL; |
| 7373 | ret->documents = ctxt->documents; |
| 7374 | ctxt->documents = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7375 | |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 7376 | ret->includes = ctxt->includes; |
| 7377 | ctxt->includes = NULL; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7378 | ret->defNr = ctxt->defNr; |
| 7379 | ret->defTab = ctxt->defTab; |
| 7380 | ctxt->defTab = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7381 | if (ctxt->idref == 1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7382 | ret->idref = 1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7383 | |
| 7384 | return (ret); |
| 7385 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7386 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7387 | /** |
| 7388 | * xmlRelaxNGSetParserErrors: |
| 7389 | * @ctxt: a Relax-NG validation context |
| 7390 | * @err: the error callback |
| 7391 | * @warn: the warning callback |
| 7392 | * @ctx: contextual data for the callbacks |
| 7393 | * |
| 7394 | * Set the callback functions used to handle errors for a validation context |
| 7395 | */ |
| 7396 | void |
| 7397 | xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7398 | xmlRelaxNGValidityErrorFunc err, |
| 7399 | xmlRelaxNGValidityWarningFunc warn, void *ctx) |
| 7400 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7401 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7402 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7403 | ctxt->error = err; |
| 7404 | ctxt->warning = warn; |
| 7405 | ctxt->userData = ctx; |
| 7406 | } |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 7407 | |
| 7408 | /** |
| 7409 | * xmlRelaxNGGetParserErrors: |
| 7410 | * @ctxt: a Relax-NG validation context |
| 7411 | * @err: the error callback result |
| 7412 | * @warn: the warning callback result |
| 7413 | * @ctx: contextual data for the callbacks result |
| 7414 | * |
| 7415 | * Get the callback information used to handle errors for a validation context |
| 7416 | * |
| 7417 | * Returns -1 in case of failure, 0 otherwise. |
| 7418 | */ |
| 7419 | int |
| 7420 | xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7421 | xmlRelaxNGValidityErrorFunc * err, |
| 7422 | xmlRelaxNGValidityWarningFunc * warn, void **ctx) |
| 7423 | { |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 7424 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7425 | return (-1); |
| 7426 | if (err != NULL) |
| 7427 | *err = ctxt->error; |
| 7428 | if (warn != NULL) |
| 7429 | *warn = ctxt->warning; |
| 7430 | if (ctx != NULL) |
| 7431 | *ctx = ctxt->userData; |
| 7432 | return (0); |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 7433 | } |
| 7434 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 7435 | #ifdef LIBXML_OUTPUT_ENABLED |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7436 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7437 | /************************************************************************ |
| 7438 | * * |
| 7439 | * Dump back a compiled form * |
| 7440 | * * |
| 7441 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7442 | static void xmlRelaxNGDumpDefine(FILE * output, |
| 7443 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7444 | |
| 7445 | /** |
| 7446 | * xmlRelaxNGDumpDefines: |
| 7447 | * @output: the file output |
| 7448 | * @defines: a list of define structures |
| 7449 | * |
| 7450 | * Dump a RelaxNG structure back |
| 7451 | */ |
| 7452 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7453 | xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines) |
| 7454 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7455 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7456 | xmlRelaxNGDumpDefine(output, defines); |
| 7457 | defines = defines->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7458 | } |
| 7459 | } |
| 7460 | |
| 7461 | /** |
| 7462 | * xmlRelaxNGDumpDefine: |
| 7463 | * @output: the file output |
| 7464 | * @define: a define structure |
| 7465 | * |
| 7466 | * Dump a RelaxNG structure back |
| 7467 | */ |
| 7468 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7469 | xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define) |
| 7470 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7471 | if (define == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7472 | return; |
| 7473 | switch (define->type) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7474 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7475 | fprintf(output, "<empty/>\n"); |
| 7476 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7477 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7478 | fprintf(output, "<notAllowed/>\n"); |
| 7479 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7480 | case XML_RELAXNG_TEXT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7481 | fprintf(output, "<text/>\n"); |
| 7482 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7483 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7484 | fprintf(output, "<element>\n"); |
| 7485 | if (define->name != NULL) { |
| 7486 | fprintf(output, "<name"); |
| 7487 | if (define->ns != NULL) |
| 7488 | fprintf(output, " ns=\"%s\"", define->ns); |
| 7489 | fprintf(output, ">%s</name>\n", define->name); |
| 7490 | } |
| 7491 | xmlRelaxNGDumpDefines(output, define->attrs); |
| 7492 | xmlRelaxNGDumpDefines(output, define->content); |
| 7493 | fprintf(output, "</element>\n"); |
| 7494 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7495 | case XML_RELAXNG_LIST: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7496 | fprintf(output, "<list>\n"); |
| 7497 | xmlRelaxNGDumpDefines(output, define->content); |
| 7498 | fprintf(output, "</list>\n"); |
| 7499 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7500 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7501 | fprintf(output, "<oneOrMore>\n"); |
| 7502 | xmlRelaxNGDumpDefines(output, define->content); |
| 7503 | fprintf(output, "</oneOrMore>\n"); |
| 7504 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7505 | case XML_RELAXNG_ZEROORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7506 | fprintf(output, "<zeroOrMore>\n"); |
| 7507 | xmlRelaxNGDumpDefines(output, define->content); |
| 7508 | fprintf(output, "</zeroOrMore>\n"); |
| 7509 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7510 | case XML_RELAXNG_CHOICE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7511 | fprintf(output, "<choice>\n"); |
| 7512 | xmlRelaxNGDumpDefines(output, define->content); |
| 7513 | fprintf(output, "</choice>\n"); |
| 7514 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7515 | case XML_RELAXNG_GROUP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7516 | fprintf(output, "<group>\n"); |
| 7517 | xmlRelaxNGDumpDefines(output, define->content); |
| 7518 | fprintf(output, "</group>\n"); |
| 7519 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7520 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7521 | fprintf(output, "<interleave>\n"); |
| 7522 | xmlRelaxNGDumpDefines(output, define->content); |
| 7523 | fprintf(output, "</interleave>\n"); |
| 7524 | break; |
| 7525 | case XML_RELAXNG_OPTIONAL: |
| 7526 | fprintf(output, "<optional>\n"); |
| 7527 | xmlRelaxNGDumpDefines(output, define->content); |
| 7528 | fprintf(output, "</optional>\n"); |
| 7529 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7530 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7531 | fprintf(output, "<attribute>\n"); |
| 7532 | xmlRelaxNGDumpDefines(output, define->content); |
| 7533 | fprintf(output, "</attribute>\n"); |
| 7534 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7535 | case XML_RELAXNG_DEF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7536 | fprintf(output, "<define"); |
| 7537 | if (define->name != NULL) |
| 7538 | fprintf(output, " name=\"%s\"", define->name); |
| 7539 | fprintf(output, ">\n"); |
| 7540 | xmlRelaxNGDumpDefines(output, define->content); |
| 7541 | fprintf(output, "</define>\n"); |
| 7542 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7543 | case XML_RELAXNG_REF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7544 | fprintf(output, "<ref"); |
| 7545 | if (define->name != NULL) |
| 7546 | fprintf(output, " name=\"%s\"", define->name); |
| 7547 | fprintf(output, ">\n"); |
| 7548 | xmlRelaxNGDumpDefines(output, define->content); |
| 7549 | fprintf(output, "</ref>\n"); |
| 7550 | break; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7551 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7552 | fprintf(output, "<parentRef"); |
| 7553 | if (define->name != NULL) |
| 7554 | fprintf(output, " name=\"%s\"", define->name); |
| 7555 | fprintf(output, ">\n"); |
| 7556 | xmlRelaxNGDumpDefines(output, define->content); |
| 7557 | fprintf(output, "</parentRef>\n"); |
| 7558 | break; |
| 7559 | case XML_RELAXNG_EXTERNALREF: |
| 7560 | fprintf(output, "<externalRef>"); |
| 7561 | xmlRelaxNGDumpDefines(output, define->content); |
| 7562 | fprintf(output, "</externalRef>\n"); |
| 7563 | break; |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 7564 | case XML_RELAXNG_DATATYPE: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7565 | case XML_RELAXNG_VALUE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7566 | TODO break; |
| 7567 | case XML_RELAXNG_START: |
| 7568 | case XML_RELAXNG_EXCEPT: |
| 7569 | case XML_RELAXNG_PARAM: |
| 7570 | TODO break; |
| 7571 | case XML_RELAXNG_NOOP: |
| 7572 | xmlRelaxNGDumpDefines(output, define->content); |
| 7573 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7574 | } |
| 7575 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7576 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7577 | /** |
| 7578 | * xmlRelaxNGDumpGrammar: |
| 7579 | * @output: the file output |
| 7580 | * @grammar: a grammar structure |
| 7581 | * @top: is this a top grammar |
| 7582 | * |
| 7583 | * Dump a RelaxNG structure back |
| 7584 | */ |
| 7585 | static void |
| 7586 | xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top) |
| 7587 | { |
| 7588 | if (grammar == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7589 | return; |
| 7590 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7591 | fprintf(output, "<grammar"); |
| 7592 | if (top) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7593 | fprintf(output, " xmlns=\"http://relaxng.org/ns/structure/1.0\""); |
| 7594 | switch (grammar->combine) { |
| 7595 | case XML_RELAXNG_COMBINE_UNDEFINED: |
| 7596 | break; |
| 7597 | case XML_RELAXNG_COMBINE_CHOICE: |
| 7598 | fprintf(output, " combine=\"choice\""); |
| 7599 | break; |
| 7600 | case XML_RELAXNG_COMBINE_INTERLEAVE: |
| 7601 | fprintf(output, " combine=\"interleave\""); |
| 7602 | break; |
| 7603 | default: |
| 7604 | fprintf(output, " <!-- invalid combine value -->"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7605 | } |
| 7606 | fprintf(output, ">\n"); |
| 7607 | if (grammar->start == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7608 | fprintf(output, " <!-- grammar had no start -->"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7609 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7610 | fprintf(output, "<start>\n"); |
| 7611 | xmlRelaxNGDumpDefine(output, grammar->start); |
| 7612 | fprintf(output, "</start>\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7613 | } |
| 7614 | /* TODO ? Dump the defines ? */ |
| 7615 | fprintf(output, "</grammar>\n"); |
| 7616 | } |
| 7617 | |
| 7618 | /** |
| 7619 | * xmlRelaxNGDump: |
| 7620 | * @output: the file output |
| 7621 | * @schema: a schema structure |
| 7622 | * |
| 7623 | * Dump a RelaxNG structure back |
| 7624 | */ |
| 7625 | void |
| 7626 | xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema) |
| 7627 | { |
| 7628 | if (schema == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7629 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
| 7630 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7631 | } |
| 7632 | fprintf(output, "RelaxNG: "); |
| 7633 | if (schema->doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7634 | fprintf(output, "no document\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7635 | } else if (schema->doc->URL != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7636 | fprintf(output, "%s\n", schema->doc->URL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7637 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7638 | fprintf(output, "\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7639 | } |
| 7640 | if (schema->topgrammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7641 | fprintf(output, "RelaxNG has no top grammar\n"); |
| 7642 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7643 | } |
| 7644 | xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1); |
| 7645 | } |
| 7646 | |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7647 | /** |
| 7648 | * xmlRelaxNGDumpTree: |
| 7649 | * @output: the file output |
| 7650 | * @schema: a schema structure |
| 7651 | * |
| 7652 | * Dump the transformed RelaxNG tree. |
| 7653 | */ |
| 7654 | void |
| 7655 | xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema) |
| 7656 | { |
| 7657 | if (schema == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7658 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
| 7659 | return; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7660 | } |
| 7661 | if (schema->doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7662 | fprintf(output, "no document\n"); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7663 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7664 | xmlDocDump(output, schema->doc); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7665 | } |
| 7666 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 7667 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7668 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7669 | /************************************************************************ |
| 7670 | * * |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7671 | * Validation of compiled content * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7672 | * * |
| 7673 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7674 | static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
| 7675 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7676 | |
| 7677 | /** |
| 7678 | * xmlRelaxNGValidateCompiledCallback: |
| 7679 | * @exec: the regular expression instance |
| 7680 | * @token: the token which matched |
| 7681 | * @transdata: callback data, the define for the subelement if available |
| 7682 | @ @inputdata: callback data, the Relax NG validation context |
| 7683 | * |
| 7684 | * Handle the callback and if needed validate the element children. |
| 7685 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7686 | static void |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7687 | xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7688 | const xmlChar * token, |
| 7689 | void *transdata, void *inputdata) |
| 7690 | { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7691 | xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata; |
| 7692 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata; |
| 7693 | int ret; |
| 7694 | |
| 7695 | #ifdef DEBUG_COMPILE |
| 7696 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7697 | "Compiled callback for: '%s'\n", token); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7698 | #endif |
| 7699 | if (ctxt == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7700 | fprintf(stderr, "callback on %s missing context\n", token); |
| 7701 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7702 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7703 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7704 | } |
| 7705 | if (define == NULL) { |
| 7706 | if (token[0] == '#') |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7707 | return; |
| 7708 | fprintf(stderr, "callback on %s missing define\n", token); |
| 7709 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7710 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7711 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7712 | } |
| 7713 | if ((ctxt == NULL) || (define == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7714 | fprintf(stderr, "callback on %s missing info\n", token); |
| 7715 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7716 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7717 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7718 | } else if (define->type != XML_RELAXNG_ELEMENT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7719 | fprintf(stderr, "callback on %s define is not element\n", token); |
| 7720 | if (ctxt->errNo == XML_RELAXNG_OK) |
| 7721 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7722 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7723 | } |
| 7724 | ret = xmlRelaxNGValidateDefinition(ctxt, define); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7725 | if (ret != 0) |
| 7726 | ctxt->perr = ret; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7727 | } |
| 7728 | |
| 7729 | /** |
| 7730 | * xmlRelaxNGValidateCompiledContent: |
| 7731 | * @ctxt: the RelaxNG validation context |
| 7732 | * @regexp: the regular expression as compiled |
| 7733 | * @content: list of children to test against the regexp |
| 7734 | * |
| 7735 | * Validate the content model of an element or start using the regexp |
| 7736 | * |
| 7737 | * Returns 0 in case of success, -1 in case of error. |
| 7738 | */ |
| 7739 | static int |
| 7740 | xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7741 | xmlRegexpPtr regexp, xmlNodePtr content) |
| 7742 | { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7743 | xmlRegExecCtxtPtr exec; |
| 7744 | xmlNodePtr cur; |
Daniel Veillard | 6216360 | 2003-04-17 09:36:38 +0000 | [diff] [blame] | 7745 | int ret = 0; |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7746 | int oldperr = ctxt->perr; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7747 | |
| 7748 | if ((ctxt == NULL) || (regexp == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7749 | return (-1); |
| 7750 | exec = xmlRegNewExecCtxt(regexp, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7751 | xmlRelaxNGValidateCompiledCallback, ctxt); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7752 | ctxt->perr = 0; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7753 | cur = content; |
| 7754 | while (cur != NULL) { |
| 7755 | ctxt->state->seq = cur; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7756 | switch (cur->type) { |
| 7757 | case XML_TEXT_NODE: |
| 7758 | case XML_CDATA_SECTION_NODE: |
| 7759 | if (xmlIsBlankNode(cur)) |
| 7760 | break; |
| 7761 | ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt); |
| 7762 | if (ret < 0) { |
| 7763 | VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, |
| 7764 | cur->parent->name); |
| 7765 | } |
| 7766 | break; |
| 7767 | case XML_ELEMENT_NODE: |
| 7768 | if (cur->ns != NULL) { |
| 7769 | ret = xmlRegExecPushString2(exec, cur->name, |
| 7770 | cur->ns->href, ctxt); |
| 7771 | } else { |
| 7772 | ret = xmlRegExecPushString(exec, cur->name, ctxt); |
| 7773 | } |
| 7774 | if (ret < 0) { |
| 7775 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name); |
| 7776 | } |
| 7777 | break; |
| 7778 | default: |
| 7779 | break; |
| 7780 | } |
| 7781 | if (ret < 0) |
| 7782 | break; |
| 7783 | /* |
| 7784 | * Switch to next element |
| 7785 | */ |
| 7786 | cur = cur->next; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7787 | } |
| 7788 | ret = xmlRegExecPushString(exec, NULL, NULL); |
| 7789 | if (ret == 1) { |
| 7790 | ret = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7791 | ctxt->state->seq = NULL; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7792 | } else if (ret == 0) { |
| 7793 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7794 | * TODO: get some of the names needed to exit the current state of exec |
| 7795 | */ |
| 7796 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST ""); |
| 7797 | ret = -1; |
| 7798 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 7799 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7800 | } else { |
| 7801 | ret = -1; |
| 7802 | } |
| 7803 | xmlRegFreeExecCtxt(exec); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7804 | /* |
| 7805 | * There might be content model errors outside of the pure |
| 7806 | * regexp validation, e.g. for attribute values. |
| 7807 | */ |
| 7808 | if ((ret == 0) && (ctxt->perr != 0)) { |
| 7809 | ret = ctxt->perr; |
| 7810 | } |
| 7811 | ctxt->perr = oldperr; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7812 | return (ret); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7813 | } |
| 7814 | |
| 7815 | /************************************************************************ |
| 7816 | * * |
| 7817 | * Progressive validation of when possible * |
| 7818 | * * |
| 7819 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7820 | static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
| 7821 | xmlRelaxNGDefinePtr defines); |
| 7822 | static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 7823 | int dolog); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 7824 | static void xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7825 | |
| 7826 | /** |
| 7827 | * xmlRelaxNGElemPush: |
| 7828 | * @ctxt: the validation context |
| 7829 | * @exec: the regexp runtime for the new content model |
| 7830 | * |
| 7831 | * Push a new regexp for the current node content model on the stack |
| 7832 | * |
| 7833 | * Returns 0 in case of success and -1 in case of error. |
| 7834 | */ |
| 7835 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7836 | xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec) |
| 7837 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7838 | if (ctxt->elemTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7839 | ctxt->elemMax = 10; |
| 7840 | ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax * |
| 7841 | sizeof |
| 7842 | (xmlRegExecCtxtPtr)); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7843 | if (ctxt->elemTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7844 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 7845 | return (-1); |
| 7846 | } |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7847 | } |
| 7848 | if (ctxt->elemNr >= ctxt->elemMax) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7849 | ctxt->elemMax *= 2; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7850 | ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7851 | ctxt->elemMax * |
| 7852 | sizeof |
| 7853 | (xmlRegExecCtxtPtr)); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7854 | if (ctxt->elemTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7855 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 7856 | return (-1); |
| 7857 | } |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7858 | } |
| 7859 | ctxt->elemTab[ctxt->elemNr++] = exec; |
| 7860 | ctxt->elem = exec; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7861 | return (0); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7862 | } |
| 7863 | |
| 7864 | /** |
| 7865 | * xmlRelaxNGElemPop: |
| 7866 | * @ctxt: the validation context |
| 7867 | * |
| 7868 | * Pop the regexp of the current node content model from the stack |
| 7869 | * |
| 7870 | * Returns the exec or NULL if empty |
| 7871 | */ |
| 7872 | static xmlRegExecCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7873 | xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt) |
| 7874 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7875 | xmlRegExecCtxtPtr ret; |
| 7876 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7877 | if (ctxt->elemNr <= 0) |
| 7878 | return (NULL); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7879 | ctxt->elemNr--; |
| 7880 | ret = ctxt->elemTab[ctxt->elemNr]; |
| 7881 | ctxt->elemTab[ctxt->elemNr] = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7882 | if (ctxt->elemNr > 0) |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7883 | ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1]; |
| 7884 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7885 | ctxt->elem = NULL; |
| 7886 | return (ret); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7887 | } |
| 7888 | |
| 7889 | /** |
| 7890 | * xmlRelaxNGValidateProgressiveCallback: |
| 7891 | * @exec: the regular expression instance |
| 7892 | * @token: the token which matched |
| 7893 | * @transdata: callback data, the define for the subelement if available |
| 7894 | @ @inputdata: callback data, the Relax NG validation context |
| 7895 | * |
| 7896 | * Handle the callback and if needed validate the element children. |
| 7897 | * some of the in/out informations are passed via the context in @inputdata. |
| 7898 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7899 | static void |
| 7900 | xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec |
| 7901 | ATTRIBUTE_UNUSED, |
| 7902 | const xmlChar * token, |
| 7903 | void *transdata, void *inputdata) |
| 7904 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7905 | xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata; |
| 7906 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7907 | xmlRelaxNGValidStatePtr state, oldstate; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7908 | xmlNodePtr node = ctxt->pnode; |
Daniel Veillard | c3ca5ba | 2003-05-09 22:26:28 +0000 | [diff] [blame] | 7909 | int ret = 0, oldflags; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7910 | |
| 7911 | #ifdef DEBUG_PROGRESSIVE |
| 7912 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7913 | "Progressive callback for: '%s'\n", token); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7914 | #endif |
| 7915 | if (ctxt == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7916 | fprintf(stderr, "callback on %s missing context\n", token); |
| 7917 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7918 | } |
| 7919 | ctxt->pstate = 1; |
| 7920 | if (define == NULL) { |
| 7921 | if (token[0] == '#') |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7922 | return; |
| 7923 | fprintf(stderr, "callback on %s missing define\n", token); |
| 7924 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7925 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7926 | ctxt->pstate = -1; |
| 7927 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7928 | } |
| 7929 | if ((ctxt == NULL) || (define == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7930 | fprintf(stderr, "callback on %s missing info\n", token); |
| 7931 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7932 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7933 | ctxt->pstate = -1; |
| 7934 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7935 | } else if (define->type != XML_RELAXNG_ELEMENT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7936 | fprintf(stderr, "callback on %s define is not element\n", token); |
| 7937 | if (ctxt->errNo == XML_RELAXNG_OK) |
| 7938 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7939 | ctxt->pstate = -1; |
| 7940 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7941 | } |
| 7942 | if (node->type != XML_ELEMENT_NODE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7943 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
| 7944 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 7945 | xmlRelaxNGDumpValidError(ctxt); |
| 7946 | ctxt->pstate = -1; |
| 7947 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7948 | } |
| 7949 | if (define->contModel == NULL) { |
| 7950 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7951 | * this node cannot be validated in a streamable fashion |
| 7952 | */ |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7953 | #ifdef DEBUG_PROGRESSIVE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7954 | xmlGenericError(xmlGenericErrorContext, |
| 7955 | "Element '%s' validation is not streamable\n", |
| 7956 | token); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7957 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7958 | ctxt->pstate = 0; |
| 7959 | ctxt->pdef = define; |
| 7960 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7961 | } |
| 7962 | exec = xmlRegNewExecCtxt(define->contModel, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7963 | xmlRelaxNGValidateProgressiveCallback, ctxt); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7964 | if (exec == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7965 | ctxt->pstate = -1; |
| 7966 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7967 | } |
| 7968 | xmlRelaxNGElemPush(ctxt, exec); |
| 7969 | |
| 7970 | /* |
| 7971 | * Validate the attributes part of the content. |
| 7972 | */ |
| 7973 | state = xmlRelaxNGNewValidState(ctxt, node); |
| 7974 | if (state == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7975 | ctxt->pstate = -1; |
| 7976 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7977 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7978 | oldstate = ctxt->state; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7979 | ctxt->state = state; |
| 7980 | if (define->attrs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7981 | ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
| 7982 | if (ret != 0) { |
| 7983 | ctxt->pstate = -1; |
| 7984 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
| 7985 | } |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7986 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7987 | if (ctxt->state != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7988 | ctxt->state->seq = NULL; |
| 7989 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
| 7990 | if (ret != 0) { |
| 7991 | ctxt->pstate = -1; |
| 7992 | } |
| 7993 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7994 | } else if (ctxt->states != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7995 | int tmp = -1, i; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7996 | |
| 7997 | oldflags = ctxt->flags; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7998 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7999 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 8000 | state = ctxt->states->tabState[i]; |
| 8001 | ctxt->state = state; |
| 8002 | ctxt->state->seq = NULL; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8003 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8004 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
| 8005 | tmp = 0; |
| 8006 | break; |
| 8007 | } |
| 8008 | } |
| 8009 | if (tmp != 0) { |
| 8010 | /* |
| 8011 | * validation error, log the message for the "best" one |
| 8012 | */ |
| 8013 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8014 | xmlRelaxNGLogBestError(ctxt); |
| 8015 | } |
| 8016 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 8017 | xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[i]); |
| 8018 | } |
| 8019 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 8020 | ctxt->states = NULL; |
| 8021 | if ((ret == 0) && (tmp == -1)) |
| 8022 | ctxt->pstate = -1; |
| 8023 | ctxt->flags = oldflags; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8024 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8025 | if (ctxt->pstate == -1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8026 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 8027 | xmlRelaxNGDumpValidError(ctxt); |
| 8028 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8029 | } |
| 8030 | ctxt->state = oldstate; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8031 | } |
| 8032 | |
| 8033 | /** |
| 8034 | * xmlRelaxNGValidatePushElement: |
| 8035 | * @ctxt: the validation context |
| 8036 | * @doc: a document instance |
| 8037 | * @elem: an element instance |
| 8038 | * |
| 8039 | * Push a new element start on the RelaxNG validation stack. |
| 8040 | * |
| 8041 | * returns 1 if no validation problem was found or 0 if validating the |
| 8042 | * element requires a full node, and -1 in case of error. |
| 8043 | */ |
| 8044 | int |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8045 | xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8046 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8047 | xmlNodePtr elem) |
| 8048 | { |
| 8049 | int ret = 1; |
| 8050 | |
| 8051 | if ((ctxt == NULL) || (elem == NULL)) |
| 8052 | return (-1); |
| 8053 | |
| 8054 | #ifdef DEBUG_PROGRESSIVE |
| 8055 | xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name); |
| 8056 | #endif |
| 8057 | if (ctxt->elem == 0) { |
| 8058 | xmlRelaxNGPtr schema; |
| 8059 | xmlRelaxNGGrammarPtr grammar; |
| 8060 | xmlRegExecCtxtPtr exec; |
| 8061 | xmlRelaxNGDefinePtr define; |
| 8062 | |
| 8063 | schema = ctxt->schema; |
| 8064 | if (schema == NULL) { |
| 8065 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 8066 | return (-1); |
| 8067 | } |
| 8068 | grammar = schema->topgrammar; |
| 8069 | if ((grammar == NULL) || (grammar->start == NULL)) { |
| 8070 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 8071 | return (-1); |
| 8072 | } |
| 8073 | define = grammar->start; |
| 8074 | if (define->contModel == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8075 | ctxt->pdef = define; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8076 | return (0); |
| 8077 | } |
| 8078 | exec = xmlRegNewExecCtxt(define->contModel, |
| 8079 | xmlRelaxNGValidateProgressiveCallback, |
| 8080 | ctxt); |
| 8081 | if (exec == NULL) { |
| 8082 | return (-1); |
| 8083 | } |
| 8084 | xmlRelaxNGElemPush(ctxt, exec); |
| 8085 | } |
| 8086 | ctxt->pnode = elem; |
| 8087 | ctxt->pstate = 0; |
| 8088 | if (elem->ns != NULL) { |
| 8089 | ret = |
| 8090 | xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href, |
| 8091 | ctxt); |
| 8092 | } else { |
| 8093 | ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt); |
| 8094 | } |
| 8095 | if (ret < 0) { |
| 8096 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name); |
| 8097 | } else { |
| 8098 | if (ctxt->pstate == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8099 | ret = 0; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8100 | else if (ctxt->pstate < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8101 | ret = -1; |
| 8102 | else |
| 8103 | ret = 1; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8104 | } |
| 8105 | #ifdef DEBUG_PROGRESSIVE |
| 8106 | if (ret < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8107 | xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n", |
| 8108 | elem->name); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8109 | #endif |
| 8110 | return (ret); |
| 8111 | } |
| 8112 | |
| 8113 | /** |
| 8114 | * xmlRelaxNGValidatePushCData: |
| 8115 | * @ctxt: the RelaxNG validation context |
| 8116 | * @data: some character data read |
| 8117 | * @len: the lenght of the data |
| 8118 | * |
| 8119 | * check the CData parsed for validation in the current stack |
| 8120 | * |
| 8121 | * returns 1 if no validation problem was found or -1 otherwise |
| 8122 | */ |
| 8123 | int |
| 8124 | xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8125 | const xmlChar * data, int len ATTRIBUTE_UNUSED) |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8126 | { |
| 8127 | int ret = 1; |
| 8128 | |
| 8129 | if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL)) |
| 8130 | return (-1); |
| 8131 | |
| 8132 | #ifdef DEBUG_PROGRESSIVE |
| 8133 | xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len); |
| 8134 | #endif |
| 8135 | |
| 8136 | while (*data != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8137 | if (!IS_BLANK_CH(*data)) |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8138 | break; |
| 8139 | data++; |
| 8140 | } |
| 8141 | if (*data == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8142 | return (1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8143 | |
| 8144 | ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt); |
| 8145 | if (ret < 0) { |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8146 | VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO "); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8147 | #ifdef DEBUG_PROGRESSIVE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8148 | xmlGenericError(xmlGenericErrorContext, "CDATA failed\n"); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8149 | #endif |
| 8150 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8151 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8152 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8153 | return (1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8154 | } |
| 8155 | |
| 8156 | /** |
| 8157 | * xmlRelaxNGValidatePopElement: |
| 8158 | * @ctxt: the RelaxNG validation context |
| 8159 | * @doc: a document instance |
| 8160 | * @elem: an element instance |
| 8161 | * |
| 8162 | * Pop the element end from the RelaxNG validation stack. |
| 8163 | * |
| 8164 | * returns 1 if no validation problem was found or 0 otherwise |
| 8165 | */ |
| 8166 | int |
| 8167 | xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8168 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8169 | xmlNodePtr elem) |
| 8170 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8171 | int ret; |
| 8172 | xmlRegExecCtxtPtr exec; |
| 8173 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8174 | if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL)) |
| 8175 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8176 | #ifdef DEBUG_PROGRESSIVE |
| 8177 | xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name); |
| 8178 | #endif |
| 8179 | /* |
| 8180 | * verify that we reached a terminal state of the content model. |
| 8181 | */ |
| 8182 | exec = xmlRelaxNGElemPop(ctxt); |
| 8183 | ret = xmlRegExecPushString(exec, NULL, NULL); |
| 8184 | if (ret == 0) { |
| 8185 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8186 | * TODO: get some of the names needed to exit the current state of exec |
| 8187 | */ |
| 8188 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST ""); |
| 8189 | ret = -1; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8190 | } else if (ret < 0) { |
| 8191 | ret = -1; |
| 8192 | } else { |
| 8193 | ret = 1; |
| 8194 | } |
| 8195 | xmlRegFreeExecCtxt(exec); |
| 8196 | #ifdef DEBUG_PROGRESSIVE |
| 8197 | if (ret < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8198 | xmlGenericError(xmlGenericErrorContext, "PopElem %s failed\n", |
| 8199 | elem->name); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8200 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8201 | return (ret); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8202 | } |
| 8203 | |
| 8204 | /** |
| 8205 | * xmlRelaxNGValidateFullElement: |
| 8206 | * @ctxt: the validation context |
| 8207 | * @doc: a document instance |
| 8208 | * @elem: an element instance |
| 8209 | * |
| 8210 | * Validate a full subtree when xmlRelaxNGValidatePushElement() returned |
| 8211 | * 0 and the content of the node has been expanded. |
| 8212 | * |
| 8213 | * returns 1 if no validation problem was found or -1 in case of error. |
| 8214 | */ |
| 8215 | int |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8216 | xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8217 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8218 | xmlNodePtr elem) |
| 8219 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8220 | int ret; |
| 8221 | xmlRelaxNGValidStatePtr state; |
| 8222 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8223 | if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL)) |
| 8224 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8225 | #ifdef DEBUG_PROGRESSIVE |
| 8226 | xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name); |
| 8227 | #endif |
| 8228 | state = xmlRelaxNGNewValidState(ctxt, elem->parent); |
| 8229 | if (state == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8230 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8231 | } |
| 8232 | state->seq = elem; |
| 8233 | ctxt->state = state; |
| 8234 | ctxt->errNo = XML_RELAXNG_OK; |
| 8235 | ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8236 | if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK)) |
| 8237 | ret = -1; |
| 8238 | else |
| 8239 | ret = 1; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8240 | xmlRelaxNGFreeValidState(ctxt, state); |
| 8241 | ctxt->state = NULL; |
| 8242 | #ifdef DEBUG_PROGRESSIVE |
| 8243 | if (ret < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8244 | xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n", |
| 8245 | elem->name); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8246 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8247 | return (ret); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8248 | } |
| 8249 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 8250 | /************************************************************************ |
| 8251 | * * |
| 8252 | * Generic interpreted validation implementation * |
| 8253 | * * |
| 8254 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8255 | static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
| 8256 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8257 | |
| 8258 | /** |
| 8259 | * xmlRelaxNGSkipIgnored: |
| 8260 | * @ctxt: a schema validation context |
| 8261 | * @node: the top node. |
| 8262 | * |
| 8263 | * Skip ignorable nodes in that context |
| 8264 | * |
| 8265 | * Returns the new sibling or NULL in case of error. |
| 8266 | */ |
| 8267 | static xmlNodePtr |
| 8268 | xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8269 | xmlNodePtr node) |
| 8270 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8271 | /* |
| 8272 | * TODO complete and handle entities |
| 8273 | */ |
| 8274 | while ((node != NULL) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8275 | ((node->type == XML_COMMENT_NODE) || |
| 8276 | (node->type == XML_PI_NODE) || |
William M. Brack | 7217c86 | 2004-03-15 02:43:56 +0000 | [diff] [blame] | 8277 | (node->type == XML_XINCLUDE_START) || |
| 8278 | (node->type == XML_XINCLUDE_END) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8279 | (((node->type == XML_TEXT_NODE) || |
| 8280 | (node->type == XML_CDATA_SECTION_NODE)) && |
| 8281 | ((ctxt->flags & FLAGS_MIXED_CONTENT) || |
| 8282 | (IS_BLANK_NODE(node)))))) { |
| 8283 | node = node->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8284 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8285 | return (node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8286 | } |
| 8287 | |
| 8288 | /** |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8289 | * xmlRelaxNGNormalize: |
| 8290 | * @ctxt: a schema validation context |
| 8291 | * @str: the string to normalize |
| 8292 | * |
| 8293 | * Implements the normalizeWhiteSpace( s ) function from |
| 8294 | * section 6.2.9 of the spec |
| 8295 | * |
| 8296 | * Returns the new string or NULL in case of error. |
| 8297 | */ |
| 8298 | static xmlChar * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8299 | xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar * str) |
| 8300 | { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8301 | xmlChar *ret, *p; |
| 8302 | const xmlChar *tmp; |
| 8303 | int len; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8304 | |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8305 | if (str == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8306 | return (NULL); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8307 | tmp = str; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8308 | while (*tmp != 0) |
| 8309 | tmp++; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8310 | len = tmp - str; |
| 8311 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 8312 | ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar)); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8313 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8314 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 8315 | return (NULL); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8316 | } |
| 8317 | p = ret; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8318 | while (IS_BLANK_CH(*str)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8319 | str++; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8320 | while (*str != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8321 | if (IS_BLANK_CH(*str)) { |
| 8322 | while (IS_BLANK_CH(*str)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8323 | str++; |
| 8324 | if (*str == 0) |
| 8325 | break; |
| 8326 | *p++ = ' '; |
| 8327 | } else |
| 8328 | *p++ = *str++; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8329 | } |
| 8330 | *p = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8331 | return (ret); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8332 | } |
| 8333 | |
| 8334 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8335 | * xmlRelaxNGValidateDatatype: |
| 8336 | * @ctxt: a Relax-NG validation context |
| 8337 | * @value: the string value |
| 8338 | * @type: the datatype definition |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8339 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8340 | * |
| 8341 | * Validate the given value against the dataype |
| 8342 | * |
| 8343 | * Returns 0 if the validation succeeded or an error code. |
| 8344 | */ |
| 8345 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8346 | xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt, |
| 8347 | const xmlChar * value, |
| 8348 | xmlRelaxNGDefinePtr define, xmlNodePtr node) |
| 8349 | { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8350 | int ret, tmp; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8351 | xmlRelaxNGTypeLibraryPtr lib; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8352 | void *result = NULL; |
| 8353 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8354 | |
| 8355 | if ((define == NULL) || (define->data == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8356 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8357 | } |
| 8358 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8359 | if (lib->check != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8360 | if ((define->attrs != NULL) && |
| 8361 | (define->attrs->type == XML_RELAXNG_PARAM)) { |
| 8362 | ret = |
| 8363 | lib->check(lib->data, define->name, value, &result, node); |
| 8364 | } else { |
| 8365 | ret = lib->check(lib->data, define->name, value, NULL, node); |
| 8366 | } |
| 8367 | } else |
| 8368 | ret = -1; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8369 | if (ret < 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8370 | VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name); |
| 8371 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
| 8372 | lib->freef(lib->data, result); |
| 8373 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8374 | } else if (ret == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8375 | ret = 0; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8376 | } else if (ret == 2) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8377 | VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8378 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8379 | VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value); |
| 8380 | ret = -1; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8381 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8382 | cur = define->attrs; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8383 | while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8384 | if (lib->facet != NULL) { |
| 8385 | tmp = lib->facet(lib->data, define->name, cur->name, |
| 8386 | cur->value, value, result); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8387 | if (tmp != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8388 | ret = -1; |
| 8389 | } |
| 8390 | cur = cur->next; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8391 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8392 | if ((ret == 0) && (define->content != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8393 | const xmlChar *oldvalue, *oldendvalue; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8394 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8395 | oldvalue = ctxt->state->value; |
| 8396 | oldendvalue = ctxt->state->endvalue; |
| 8397 | ctxt->state->value = (xmlChar *) value; |
| 8398 | ctxt->state->endvalue = NULL; |
| 8399 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 8400 | ctxt->state->value = (xmlChar *) oldvalue; |
| 8401 | ctxt->state->endvalue = (xmlChar *) oldendvalue; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8402 | } |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8403 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8404 | lib->freef(lib->data, result); |
| 8405 | return (ret); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8406 | } |
| 8407 | |
| 8408 | /** |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8409 | * xmlRelaxNGNextValue: |
| 8410 | * @ctxt: a Relax-NG validation context |
| 8411 | * |
| 8412 | * Skip to the next value when validating within a list |
| 8413 | * |
| 8414 | * Returns 0 if the operation succeeded or an error code. |
| 8415 | */ |
| 8416 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8417 | xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt) |
| 8418 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8419 | xmlChar *cur; |
| 8420 | |
| 8421 | cur = ctxt->state->value; |
| 8422 | if ((cur == NULL) || (ctxt->state->endvalue == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8423 | ctxt->state->value = NULL; |
| 8424 | ctxt->state->endvalue = NULL; |
| 8425 | return (0); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8426 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8427 | while (*cur != 0) |
| 8428 | cur++; |
| 8429 | while ((cur != ctxt->state->endvalue) && (*cur == 0)) |
| 8430 | cur++; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8431 | if (cur == ctxt->state->endvalue) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8432 | ctxt->state->value = NULL; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8433 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8434 | ctxt->state->value = cur; |
| 8435 | return (0); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8436 | } |
| 8437 | |
| 8438 | /** |
| 8439 | * xmlRelaxNGValidateValueList: |
| 8440 | * @ctxt: a Relax-NG validation context |
| 8441 | * @defines: the list of definitions to verify |
| 8442 | * |
| 8443 | * Validate the given set of definitions for the current value |
| 8444 | * |
| 8445 | * Returns 0 if the validation succeeded or an error code. |
| 8446 | */ |
| 8447 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8448 | xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt, |
| 8449 | xmlRelaxNGDefinePtr defines) |
| 8450 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8451 | int ret = 0; |
| 8452 | |
| 8453 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8454 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
| 8455 | if (ret != 0) |
| 8456 | break; |
| 8457 | defines = defines->next; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8458 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8459 | return (ret); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8460 | } |
| 8461 | |
| 8462 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8463 | * xmlRelaxNGValidateValue: |
| 8464 | * @ctxt: a Relax-NG validation context |
| 8465 | * @define: the definition to verify |
| 8466 | * |
| 8467 | * Validate the given definition for the current value |
| 8468 | * |
| 8469 | * Returns 0 if the validation succeeded or an error code. |
| 8470 | */ |
| 8471 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8472 | xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
| 8473 | xmlRelaxNGDefinePtr define) |
| 8474 | { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8475 | int ret = 0, oldflags; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8476 | xmlChar *value; |
| 8477 | |
| 8478 | value = ctxt->state->value; |
| 8479 | switch (define->type) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8480 | case XML_RELAXNG_EMPTY:{ |
| 8481 | if ((value != NULL) && (value[0] != 0)) { |
| 8482 | int idx = 0; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 8483 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8484 | while (IS_BLANK_CH(value[idx])) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8485 | idx++; |
| 8486 | if (value[idx] != 0) |
| 8487 | ret = -1; |
| 8488 | } |
| 8489 | break; |
| 8490 | } |
| 8491 | case XML_RELAXNG_TEXT: |
| 8492 | break; |
| 8493 | case XML_RELAXNG_VALUE:{ |
| 8494 | if (!xmlStrEqual(value, define->value)) { |
| 8495 | if (define->name != NULL) { |
| 8496 | xmlRelaxNGTypeLibraryPtr lib; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8497 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8498 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
| 8499 | if ((lib != NULL) && (lib->comp != NULL)) { |
| 8500 | ret = lib->comp(lib->data, define->name, |
| 8501 | define->value, define->node, |
| 8502 | (void *) define->attrs, |
| 8503 | value, ctxt->state->node); |
| 8504 | } else |
| 8505 | ret = -1; |
| 8506 | if (ret < 0) { |
| 8507 | VALID_ERR2(XML_RELAXNG_ERR_TYPECMP, |
| 8508 | define->name); |
| 8509 | return (-1); |
| 8510 | } else if (ret == 1) { |
| 8511 | ret = 0; |
| 8512 | } else { |
| 8513 | ret = -1; |
| 8514 | } |
| 8515 | } else { |
| 8516 | xmlChar *nval, *nvalue; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8517 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8518 | /* |
| 8519 | * TODO: trivial optimizations are possible by |
| 8520 | * computing at compile-time |
| 8521 | */ |
| 8522 | nval = xmlRelaxNGNormalize(ctxt, define->value); |
| 8523 | nvalue = xmlRelaxNGNormalize(ctxt, value); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8524 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8525 | if ((nval == NULL) || (nvalue == NULL) || |
| 8526 | (!xmlStrEqual(nval, nvalue))) |
| 8527 | ret = -1; |
| 8528 | if (nval != NULL) |
| 8529 | xmlFree(nval); |
| 8530 | if (nvalue != NULL) |
| 8531 | xmlFree(nvalue); |
| 8532 | } |
| 8533 | } |
| 8534 | if (ret == 0) |
| 8535 | xmlRelaxNGNextValue(ctxt); |
| 8536 | break; |
| 8537 | } |
| 8538 | case XML_RELAXNG_DATATYPE:{ |
| 8539 | ret = xmlRelaxNGValidateDatatype(ctxt, value, define, |
| 8540 | ctxt->state->seq); |
| 8541 | if (ret == 0) |
| 8542 | xmlRelaxNGNextValue(ctxt); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8543 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8544 | break; |
| 8545 | } |
| 8546 | case XML_RELAXNG_CHOICE:{ |
| 8547 | xmlRelaxNGDefinePtr list = define->content; |
| 8548 | xmlChar *oldvalue; |
| 8549 | |
| 8550 | oldflags = ctxt->flags; |
| 8551 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8552 | |
| 8553 | oldvalue = ctxt->state->value; |
| 8554 | while (list != NULL) { |
| 8555 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8556 | if (ret == 0) { |
| 8557 | break; |
| 8558 | } |
| 8559 | ctxt->state->value = oldvalue; |
| 8560 | list = list->next; |
| 8561 | } |
| 8562 | ctxt->flags = oldflags; |
| 8563 | if (ret != 0) { |
| 8564 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8565 | xmlRelaxNGDumpValidError(ctxt); |
| 8566 | } else { |
| 8567 | if (ctxt->errNr > 0) |
| 8568 | xmlRelaxNGPopErrors(ctxt, 0); |
| 8569 | } |
| 8570 | if (ret == 0) |
| 8571 | xmlRelaxNGNextValue(ctxt); |
| 8572 | break; |
| 8573 | } |
| 8574 | case XML_RELAXNG_LIST:{ |
| 8575 | xmlRelaxNGDefinePtr list = define->content; |
| 8576 | xmlChar *oldvalue, *oldend, *val, *cur; |
| 8577 | |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8578 | #ifdef DEBUG_LIST |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8579 | int nb_values = 0; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8580 | #endif |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8581 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8582 | oldvalue = ctxt->state->value; |
| 8583 | oldend = ctxt->state->endvalue; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8584 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8585 | val = xmlStrdup(oldvalue); |
| 8586 | if (val == NULL) { |
| 8587 | val = xmlStrdup(BAD_CAST ""); |
| 8588 | } |
| 8589 | if (val == NULL) { |
| 8590 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 8591 | return (-1); |
| 8592 | } |
| 8593 | cur = val; |
| 8594 | while (*cur != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8595 | if (IS_BLANK_CH(*cur)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8596 | *cur = 0; |
| 8597 | cur++; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8598 | #ifdef DEBUG_LIST |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8599 | nb_values++; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8600 | #endif |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8601 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8602 | *cur++ = 0; |
| 8603 | } else |
| 8604 | cur++; |
| 8605 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8606 | #ifdef DEBUG_LIST |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8607 | xmlGenericError(xmlGenericErrorContext, |
| 8608 | "list value: '%s' found %d items\n", |
| 8609 | oldvalue, nb_values); |
| 8610 | nb_values = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8611 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8612 | ctxt->state->endvalue = cur; |
| 8613 | cur = val; |
| 8614 | while ((*cur == 0) && (cur != ctxt->state->endvalue)) |
| 8615 | cur++; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8616 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8617 | ctxt->state->value = cur; |
| 8618 | |
| 8619 | while (list != NULL) { |
| 8620 | if (ctxt->state->value == ctxt->state->endvalue) |
| 8621 | ctxt->state->value = NULL; |
| 8622 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8623 | if (ret != 0) { |
| 8624 | #ifdef DEBUG_LIST |
| 8625 | xmlGenericError(xmlGenericErrorContext, |
| 8626 | "Failed to validate value: '%s' with %d rule\n", |
| 8627 | ctxt->state->value, nb_values); |
| 8628 | #endif |
| 8629 | break; |
| 8630 | } |
| 8631 | #ifdef DEBUG_LIST |
| 8632 | nb_values++; |
| 8633 | #endif |
| 8634 | list = list->next; |
| 8635 | } |
| 8636 | |
| 8637 | if ((ret == 0) && (ctxt->state->value != NULL) && |
| 8638 | (ctxt->state->value != ctxt->state->endvalue)) { |
| 8639 | VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA, |
| 8640 | ctxt->state->value); |
| 8641 | ret = -1; |
| 8642 | } |
| 8643 | xmlFree(val); |
| 8644 | ctxt->state->value = oldvalue; |
| 8645 | ctxt->state->endvalue = oldend; |
| 8646 | break; |
| 8647 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8648 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8649 | ret = xmlRelaxNGValidateValueList(ctxt, define->content); |
| 8650 | if (ret != 0) { |
| 8651 | break; |
| 8652 | } |
| 8653 | /* no break on purpose */ |
| 8654 | case XML_RELAXNG_ZEROORMORE:{ |
| 8655 | xmlChar *cur, *temp; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8656 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8657 | oldflags = ctxt->flags; |
| 8658 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8659 | cur = ctxt->state->value; |
| 8660 | temp = NULL; |
| 8661 | while ((cur != NULL) && (cur != ctxt->state->endvalue) && |
| 8662 | (temp != cur)) { |
| 8663 | temp = cur; |
| 8664 | ret = |
| 8665 | xmlRelaxNGValidateValueList(ctxt, define->content); |
| 8666 | if (ret != 0) { |
| 8667 | ctxt->state->value = temp; |
| 8668 | ret = 0; |
| 8669 | break; |
| 8670 | } |
| 8671 | cur = ctxt->state->value; |
| 8672 | } |
| 8673 | ctxt->flags = oldflags; |
| 8674 | if (ret != 0) { |
| 8675 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8676 | xmlRelaxNGDumpValidError(ctxt); |
| 8677 | } else { |
| 8678 | if (ctxt->errNr > 0) |
| 8679 | xmlRelaxNGPopErrors(ctxt, 0); |
| 8680 | } |
| 8681 | break; |
| 8682 | } |
| 8683 | case XML_RELAXNG_EXCEPT:{ |
| 8684 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8685 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8686 | list = define->content; |
| 8687 | while (list != NULL) { |
| 8688 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8689 | if (ret == 0) { |
| 8690 | ret = -1; |
| 8691 | break; |
| 8692 | } else |
| 8693 | ret = 0; |
| 8694 | list = list->next; |
| 8695 | } |
| 8696 | break; |
| 8697 | } |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 8698 | case XML_RELAXNG_DEF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8699 | case XML_RELAXNG_GROUP:{ |
| 8700 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8701 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8702 | list = define->content; |
| 8703 | while (list != NULL) { |
| 8704 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8705 | if (ret != 0) { |
| 8706 | ret = -1; |
| 8707 | break; |
| 8708 | } else |
| 8709 | ret = 0; |
| 8710 | list = list->next; |
| 8711 | } |
| 8712 | break; |
| 8713 | } |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 8714 | case XML_RELAXNG_REF: |
| 8715 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8716 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 8717 | break; |
| 8718 | default: |
| 8719 | TODO ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8720 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8721 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8722 | } |
| 8723 | |
| 8724 | /** |
| 8725 | * xmlRelaxNGValidateValueContent: |
| 8726 | * @ctxt: a Relax-NG validation context |
| 8727 | * @defines: the list of definitions to verify |
| 8728 | * |
| 8729 | * Validate the given definitions for the current value |
| 8730 | * |
| 8731 | * Returns 0 if the validation succeeded or an error code. |
| 8732 | */ |
| 8733 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8734 | xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt, |
| 8735 | xmlRelaxNGDefinePtr defines) |
| 8736 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8737 | int ret = 0; |
| 8738 | |
| 8739 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8740 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
| 8741 | if (ret != 0) |
| 8742 | break; |
| 8743 | defines = defines->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8744 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8745 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8746 | } |
| 8747 | |
| 8748 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8749 | * xmlRelaxNGAttributeMatch: |
| 8750 | * @ctxt: a Relax-NG validation context |
| 8751 | * @define: the definition to check |
| 8752 | * @prop: the attribute |
| 8753 | * |
| 8754 | * Check if the attribute matches the definition nameClass |
| 8755 | * |
| 8756 | * Returns 1 if the attribute matches, 0 if no, or -1 in case of error |
| 8757 | */ |
| 8758 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8759 | xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 8760 | xmlRelaxNGDefinePtr define, xmlAttrPtr prop) |
| 8761 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8762 | int ret; |
| 8763 | |
| 8764 | if (define->name != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8765 | if (!xmlStrEqual(define->name, prop->name)) |
| 8766 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8767 | } |
| 8768 | if (define->ns != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8769 | if (define->ns[0] == 0) { |
| 8770 | if (prop->ns != NULL) |
| 8771 | return (0); |
| 8772 | } else { |
| 8773 | if ((prop->ns == NULL) || |
| 8774 | (!xmlStrEqual(define->ns, prop->ns->href))) |
| 8775 | return (0); |
| 8776 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8777 | } |
| 8778 | if (define->nameClass == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8779 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8780 | define = define->nameClass; |
| 8781 | if (define->type == XML_RELAXNG_EXCEPT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8782 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8783 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8784 | list = define->content; |
| 8785 | while (list != NULL) { |
| 8786 | ret = xmlRelaxNGAttributeMatch(ctxt, list, prop); |
| 8787 | if (ret == 1) |
| 8788 | return (0); |
| 8789 | if (ret < 0) |
| 8790 | return (ret); |
| 8791 | list = list->next; |
| 8792 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8793 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8794 | TODO} |
| 8795 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8796 | } |
| 8797 | |
| 8798 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8799 | * xmlRelaxNGValidateAttribute: |
| 8800 | * @ctxt: a Relax-NG validation context |
| 8801 | * @define: the definition to verify |
| 8802 | * |
| 8803 | * Validate the given attribute definition for that node |
| 8804 | * |
| 8805 | * Returns 0 if the validation succeeded or an error code. |
| 8806 | */ |
| 8807 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8808 | xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt, |
| 8809 | xmlRelaxNGDefinePtr define) |
| 8810 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8811 | int ret = 0, i; |
| 8812 | xmlChar *value, *oldvalue; |
| 8813 | xmlAttrPtr prop = NULL, tmp; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8814 | xmlNodePtr oldseq; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8815 | |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 8816 | if (ctxt->state->nbAttrLeft <= 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8817 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8818 | if (define->name != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8819 | for (i = 0; i < ctxt->state->nbAttrs; i++) { |
| 8820 | tmp = ctxt->state->attrs[i]; |
| 8821 | if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) { |
| 8822 | if ((((define->ns == NULL) || (define->ns[0] == 0)) && |
| 8823 | (tmp->ns == NULL)) || |
| 8824 | ((tmp->ns != NULL) && |
| 8825 | (xmlStrEqual(define->ns, tmp->ns->href)))) { |
| 8826 | prop = tmp; |
| 8827 | break; |
| 8828 | } |
| 8829 | } |
| 8830 | } |
| 8831 | if (prop != NULL) { |
| 8832 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 8833 | oldvalue = ctxt->state->value; |
| 8834 | oldseq = ctxt->state->seq; |
| 8835 | ctxt->state->seq = (xmlNodePtr) prop; |
| 8836 | ctxt->state->value = value; |
| 8837 | ctxt->state->endvalue = NULL; |
| 8838 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
| 8839 | if (ctxt->state->value != NULL) |
| 8840 | value = ctxt->state->value; |
| 8841 | if (value != NULL) |
| 8842 | xmlFree(value); |
| 8843 | ctxt->state->value = oldvalue; |
| 8844 | ctxt->state->seq = oldseq; |
| 8845 | if (ret == 0) { |
| 8846 | /* |
| 8847 | * flag the attribute as processed |
| 8848 | */ |
| 8849 | ctxt->state->attrs[i] = NULL; |
| 8850 | ctxt->state->nbAttrLeft--; |
| 8851 | } |
| 8852 | } else { |
| 8853 | ret = -1; |
| 8854 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8855 | #ifdef DEBUG |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8856 | xmlGenericError(xmlGenericErrorContext, |
| 8857 | "xmlRelaxNGValidateAttribute(%s): %d\n", |
| 8858 | define->name, ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8859 | #endif |
| 8860 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8861 | for (i = 0; i < ctxt->state->nbAttrs; i++) { |
| 8862 | tmp = ctxt->state->attrs[i]; |
| 8863 | if ((tmp != NULL) && |
| 8864 | (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) { |
| 8865 | prop = tmp; |
| 8866 | break; |
| 8867 | } |
| 8868 | } |
| 8869 | if (prop != NULL) { |
| 8870 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 8871 | oldvalue = ctxt->state->value; |
| 8872 | oldseq = ctxt->state->seq; |
| 8873 | ctxt->state->seq = (xmlNodePtr) prop; |
| 8874 | ctxt->state->value = value; |
| 8875 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
| 8876 | if (ctxt->state->value != NULL) |
| 8877 | value = ctxt->state->value; |
| 8878 | if (value != NULL) |
| 8879 | xmlFree(value); |
| 8880 | ctxt->state->value = oldvalue; |
| 8881 | ctxt->state->seq = oldseq; |
| 8882 | if (ret == 0) { |
| 8883 | /* |
| 8884 | * flag the attribute as processed |
| 8885 | */ |
| 8886 | ctxt->state->attrs[i] = NULL; |
| 8887 | ctxt->state->nbAttrLeft--; |
| 8888 | } |
| 8889 | } else { |
| 8890 | ret = -1; |
| 8891 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8892 | #ifdef DEBUG |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8893 | if (define->ns != NULL) { |
| 8894 | xmlGenericError(xmlGenericErrorContext, |
| 8895 | "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n", |
| 8896 | define->ns, ret); |
| 8897 | } else { |
| 8898 | xmlGenericError(xmlGenericErrorContext, |
| 8899 | "xmlRelaxNGValidateAttribute(anyName): %d\n", |
| 8900 | ret); |
| 8901 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8902 | #endif |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8903 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8904 | |
| 8905 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8906 | } |
| 8907 | |
| 8908 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8909 | * xmlRelaxNGValidateAttributeList: |
| 8910 | * @ctxt: a Relax-NG validation context |
| 8911 | * @define: the list of definition to verify |
| 8912 | * |
| 8913 | * Validate the given node against the list of attribute definitions |
| 8914 | * |
| 8915 | * Returns 0 if the validation succeeded or an error code. |
| 8916 | */ |
| 8917 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8918 | xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
| 8919 | xmlRelaxNGDefinePtr defines) |
| 8920 | { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8921 | int ret = 0, res; |
| 8922 | int needmore = 0; |
| 8923 | xmlRelaxNGDefinePtr cur; |
| 8924 | |
| 8925 | cur = defines; |
| 8926 | while (cur != NULL) { |
| 8927 | if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8928 | if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0) |
| 8929 | ret = -1; |
| 8930 | } else |
| 8931 | needmore = 1; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8932 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8933 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8934 | if (!needmore) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8935 | return (ret); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8936 | cur = defines; |
| 8937 | while (cur != NULL) { |
| 8938 | if (cur->type != XML_RELAXNG_ATTRIBUTE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8939 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
| 8940 | res = xmlRelaxNGValidateDefinition(ctxt, cur); |
| 8941 | if (res < 0) |
| 8942 | ret = -1; |
| 8943 | } else { |
| 8944 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 8945 | return (-1); |
| 8946 | } |
| 8947 | if (res == -1) /* continues on -2 */ |
| 8948 | break; |
| 8949 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8950 | cur = cur->next; |
| 8951 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8952 | |
| 8953 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8954 | } |
| 8955 | |
| 8956 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8957 | * xmlRelaxNGNodeMatchesList: |
| 8958 | * @node: the node |
| 8959 | * @list: a NULL terminated array of definitions |
| 8960 | * |
| 8961 | * Check if a node can be matched by one of the definitions |
| 8962 | * |
| 8963 | * Returns 1 if matches 0 otherwise |
| 8964 | */ |
| 8965 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8966 | xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr * list) |
| 8967 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8968 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8969 | int i = 0, tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8970 | |
| 8971 | if ((node == NULL) || (list == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8972 | return (0); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8973 | |
| 8974 | cur = list[i++]; |
| 8975 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8976 | if ((node->type == XML_ELEMENT_NODE) && |
| 8977 | (cur->type == XML_RELAXNG_ELEMENT)) { |
| 8978 | tmp = xmlRelaxNGElementMatch(NULL, cur, node); |
| 8979 | if (tmp == 1) |
| 8980 | return (1); |
| 8981 | } else if (((node->type == XML_TEXT_NODE) || |
| 8982 | (node->type == XML_CDATA_SECTION_NODE)) && |
| 8983 | (cur->type == XML_RELAXNG_TEXT)) { |
| 8984 | return (1); |
| 8985 | } |
| 8986 | cur = list[i++]; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8987 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8988 | return (0); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8989 | } |
| 8990 | |
| 8991 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8992 | * xmlRelaxNGValidateInterleave: |
| 8993 | * @ctxt: a Relax-NG validation context |
| 8994 | * @define: the definition to verify |
| 8995 | * |
| 8996 | * Validate an interleave definition for a node. |
| 8997 | * |
| 8998 | * Returns 0 if the validation succeeded or an error code. |
| 8999 | */ |
| 9000 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9001 | xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt, |
| 9002 | xmlRelaxNGDefinePtr define) |
| 9003 | { |
William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 9004 | int ret = 0, i, nbgroups; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9005 | int errNr = ctxt->errNr; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9006 | int oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9007 | |
| 9008 | xmlRelaxNGValidStatePtr oldstate; |
| 9009 | xmlRelaxNGPartitionPtr partitions; |
| 9010 | xmlRelaxNGInterleaveGroupPtr group = NULL; |
| 9011 | xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem; |
| 9012 | xmlNodePtr *list = NULL, *lasts = NULL; |
| 9013 | |
| 9014 | if (define->data != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9015 | partitions = (xmlRelaxNGPartitionPtr) define->data; |
| 9016 | nbgroups = partitions->nbgroups; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9017 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9018 | VALID_ERR(XML_RELAXNG_ERR_INTERNODATA); |
| 9019 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9020 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9021 | /* |
| 9022 | * Optimizations for MIXED |
| 9023 | */ |
| 9024 | oldflags = ctxt->flags; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 9025 | if (define->dflags & IS_MIXED) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9026 | ctxt->flags |= FLAGS_MIXED_CONTENT; |
| 9027 | if (nbgroups == 2) { |
| 9028 | /* |
| 9029 | * this is a pure <mixed> case |
| 9030 | */ |
| 9031 | if (ctxt->state != NULL) |
| 9032 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
| 9033 | ctxt->state->seq); |
| 9034 | if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT) |
| 9035 | ret = xmlRelaxNGValidateDefinition(ctxt, |
| 9036 | partitions->groups[1]-> |
| 9037 | rule); |
| 9038 | else |
| 9039 | ret = xmlRelaxNGValidateDefinition(ctxt, |
| 9040 | partitions->groups[0]-> |
| 9041 | rule); |
| 9042 | if (ret == 0) { |
| 9043 | if (ctxt->state != NULL) |
| 9044 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
| 9045 | ctxt->state-> |
| 9046 | seq); |
| 9047 | } |
| 9048 | ctxt->flags = oldflags; |
| 9049 | return (ret); |
| 9050 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9051 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9052 | |
| 9053 | /* |
| 9054 | * Build arrays to store the first and last node of the chain |
| 9055 | * pertaining to each group |
| 9056 | */ |
| 9057 | list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
| 9058 | if (list == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9059 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 9060 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9061 | } |
| 9062 | memset(list, 0, nbgroups * sizeof(xmlNodePtr)); |
| 9063 | lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
| 9064 | if (lasts == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9065 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 9066 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9067 | } |
| 9068 | memset(lasts, 0, nbgroups * sizeof(xmlNodePtr)); |
| 9069 | |
| 9070 | /* |
| 9071 | * Walk the sequence of children finding the right group and |
| 9072 | * sorting them in sequences. |
| 9073 | */ |
| 9074 | cur = ctxt->state->seq; |
| 9075 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 9076 | start = cur; |
| 9077 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9078 | ctxt->state->seq = cur; |
| 9079 | if ((partitions->triage != NULL) && |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9080 | (partitions->flags & IS_DETERMINIST)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9081 | void *tmp = NULL; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9082 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9083 | if ((cur->type == XML_TEXT_NODE) || |
| 9084 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 9085 | tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text", |
| 9086 | NULL); |
| 9087 | } else if (cur->type == XML_ELEMENT_NODE) { |
| 9088 | if (cur->ns != NULL) { |
| 9089 | tmp = xmlHashLookup2(partitions->triage, cur->name, |
| 9090 | cur->ns->href); |
| 9091 | if (tmp == NULL) |
| 9092 | tmp = xmlHashLookup2(partitions->triage, |
| 9093 | BAD_CAST "#any", |
| 9094 | cur->ns->href); |
| 9095 | } else |
| 9096 | tmp = |
| 9097 | xmlHashLookup2(partitions->triage, cur->name, |
| 9098 | NULL); |
| 9099 | if (tmp == NULL) |
| 9100 | tmp = |
| 9101 | xmlHashLookup2(partitions->triage, BAD_CAST "#any", |
| 9102 | NULL); |
| 9103 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9104 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9105 | if (tmp == NULL) { |
| 9106 | i = nbgroups; |
| 9107 | } else { |
| 9108 | i = ((long) tmp) - 1; |
| 9109 | if (partitions->flags & IS_NEEDCHECK) { |
| 9110 | group = partitions->groups[i]; |
| 9111 | if (!xmlRelaxNGNodeMatchesList(cur, group->defs)) |
| 9112 | i = nbgroups; |
| 9113 | } |
| 9114 | } |
| 9115 | } else { |
| 9116 | for (i = 0; i < nbgroups; i++) { |
| 9117 | group = partitions->groups[i]; |
| 9118 | if (group == NULL) |
| 9119 | continue; |
| 9120 | if (xmlRelaxNGNodeMatchesList(cur, group->defs)) |
| 9121 | break; |
| 9122 | } |
| 9123 | } |
| 9124 | /* |
| 9125 | * We break as soon as an element not matched is found |
| 9126 | */ |
| 9127 | if (i >= nbgroups) { |
| 9128 | break; |
| 9129 | } |
| 9130 | if (lasts[i] != NULL) { |
| 9131 | lasts[i]->next = cur; |
| 9132 | lasts[i] = cur; |
| 9133 | } else { |
| 9134 | list[i] = cur; |
| 9135 | lasts[i] = cur; |
| 9136 | } |
| 9137 | if (cur->next != NULL) |
| 9138 | lastchg = cur->next; |
| 9139 | else |
| 9140 | lastchg = cur; |
| 9141 | cur = xmlRelaxNGSkipIgnored(ctxt, cur->next); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9142 | } |
| 9143 | if (ret != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9144 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
| 9145 | ret = -1; |
| 9146 | goto done; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9147 | } |
| 9148 | lastelem = cur; |
| 9149 | oldstate = ctxt->state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9150 | for (i = 0; i < nbgroups; i++) { |
| 9151 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate); |
| 9152 | group = partitions->groups[i]; |
| 9153 | if (lasts[i] != NULL) { |
| 9154 | last = lasts[i]->next; |
| 9155 | lasts[i]->next = NULL; |
| 9156 | } |
| 9157 | ctxt->state->seq = list[i]; |
| 9158 | ret = xmlRelaxNGValidateDefinition(ctxt, group->rule); |
| 9159 | if (ret != 0) |
| 9160 | break; |
| 9161 | if (ctxt->state != NULL) { |
| 9162 | cur = ctxt->state->seq; |
| 9163 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 9164 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 9165 | oldstate = ctxt->state; |
| 9166 | ctxt->state = NULL; |
| 9167 | if (cur != NULL) { |
| 9168 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
| 9169 | ret = -1; |
| 9170 | ctxt->state = oldstate; |
| 9171 | goto done; |
| 9172 | } |
| 9173 | } else if (ctxt->states != NULL) { |
| 9174 | int j; |
| 9175 | int found = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9176 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9177 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 9178 | cur = ctxt->states->tabState[j]->seq; |
| 9179 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 9180 | if (cur == NULL) { |
| 9181 | found = 1; |
| 9182 | break; |
| 9183 | } |
| 9184 | } |
| 9185 | if (ctxt->states->nbState > 0) { |
| 9186 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 9187 | oldstate = |
| 9188 | ctxt->states->tabState[ctxt->states->nbState - 1]; |
| 9189 | } |
| 9190 | for (j = 0; j < ctxt->states->nbState - 1; j++) { |
| 9191 | xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[j]); |
| 9192 | } |
| 9193 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9194 | ctxt->states = NULL; |
| 9195 | if (found == 0) { |
| 9196 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
| 9197 | ret = -1; |
| 9198 | ctxt->state = oldstate; |
| 9199 | goto done; |
| 9200 | } |
| 9201 | } else { |
| 9202 | ret = -1; |
| 9203 | break; |
| 9204 | } |
| 9205 | if (lasts[i] != NULL) { |
| 9206 | lasts[i]->next = last; |
| 9207 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9208 | } |
| 9209 | if (ctxt->state != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9210 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9211 | ctxt->state = oldstate; |
| 9212 | ctxt->state->seq = lastelem; |
| 9213 | if (ret != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9214 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
| 9215 | ret = -1; |
| 9216 | goto done; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9217 | } |
| 9218 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9219 | done: |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9220 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9221 | /* |
| 9222 | * builds the next links chain from the prev one |
| 9223 | */ |
| 9224 | cur = lastchg; |
| 9225 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9226 | if ((cur == start) || (cur->prev == NULL)) |
| 9227 | break; |
| 9228 | cur->prev->next = cur; |
| 9229 | cur = cur->prev; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9230 | } |
| 9231 | if (ret == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9232 | if (ctxt->errNr > errNr) |
| 9233 | xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9234 | } |
| 9235 | |
| 9236 | xmlFree(list); |
| 9237 | xmlFree(lasts); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9238 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9239 | } |
| 9240 | |
| 9241 | /** |
| 9242 | * xmlRelaxNGValidateDefinitionList: |
| 9243 | * @ctxt: a Relax-NG validation context |
| 9244 | * @define: the list of definition to verify |
| 9245 | * |
| 9246 | * Validate the given node content against the (list) of definitions |
| 9247 | * |
| 9248 | * Returns 0 if the validation succeeded or an error code. |
| 9249 | */ |
| 9250 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9251 | xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt, |
| 9252 | xmlRelaxNGDefinePtr defines) |
| 9253 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9254 | int ret = 0, res; |
| 9255 | |
| 9256 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 9257 | if (defines == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9258 | VALID_ERR2(XML_RELAXNG_ERR_INTERNAL, |
| 9259 | BAD_CAST "NULL definition list"); |
| 9260 | return (-1); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 9261 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9262 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9263 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
| 9264 | res = xmlRelaxNGValidateDefinition(ctxt, defines); |
| 9265 | if (res < 0) |
| 9266 | ret = -1; |
| 9267 | } else { |
| 9268 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 9269 | return (-1); |
| 9270 | } |
| 9271 | if (res == -1) /* continues on -2 */ |
| 9272 | break; |
| 9273 | defines = defines->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9274 | } |
| 9275 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9276 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9277 | } |
| 9278 | |
| 9279 | /** |
| 9280 | * xmlRelaxNGElementMatch: |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9281 | * @ctxt: a Relax-NG validation context |
| 9282 | * @define: the definition to check |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9283 | * @elem: the element |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9284 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9285 | * Check if the element matches the definition nameClass |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9286 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9287 | * 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] | 9288 | */ |
| 9289 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9290 | xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 9291 | xmlRelaxNGDefinePtr define, xmlNodePtr elem) |
| 9292 | { |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 9293 | int ret = 0, oldflags = 0; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9294 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9295 | if (define->name != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9296 | if (!xmlStrEqual(elem->name, define->name)) { |
| 9297 | VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name); |
| 9298 | return (0); |
| 9299 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9300 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9301 | if ((define->ns != NULL) && (define->ns[0] != 0)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9302 | if (elem->ns == NULL) { |
| 9303 | VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, elem->name); |
| 9304 | return (0); |
| 9305 | } else if (!xmlStrEqual(elem->ns->href, define->ns)) { |
| 9306 | VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS, |
| 9307 | elem->name, define->ns); |
| 9308 | return (0); |
| 9309 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9310 | } else if ((elem->ns != NULL) && (define->ns != NULL) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9311 | (define->name == NULL)) { |
| 9312 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, elem->name); |
| 9313 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9314 | } else if ((elem->ns != NULL) && (define->name != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9315 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, define->name); |
| 9316 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9317 | } |
| 9318 | |
| 9319 | if (define->nameClass == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9320 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9321 | |
| 9322 | define = define->nameClass; |
| 9323 | if (define->type == XML_RELAXNG_EXCEPT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9324 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9325 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9326 | if (ctxt != NULL) { |
| 9327 | oldflags = ctxt->flags; |
| 9328 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9329 | } |
| 9330 | |
| 9331 | list = define->content; |
| 9332 | while (list != NULL) { |
| 9333 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
| 9334 | if (ret == 1) { |
| 9335 | if (ctxt != NULL) |
| 9336 | ctxt->flags = oldflags; |
| 9337 | return (0); |
| 9338 | } |
| 9339 | if (ret < 0) { |
| 9340 | if (ctxt != NULL) |
| 9341 | ctxt->flags = oldflags; |
| 9342 | return (ret); |
| 9343 | } |
| 9344 | list = list->next; |
| 9345 | } |
| 9346 | ret = 1; |
| 9347 | if (ctxt != NULL) { |
| 9348 | ctxt->flags = oldflags; |
| 9349 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9350 | } else if (define->type == XML_RELAXNG_CHOICE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9351 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9352 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9353 | if (ctxt != NULL) { |
| 9354 | oldflags = ctxt->flags; |
| 9355 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9356 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9357 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9358 | list = define->nameClass; |
| 9359 | while (list != NULL) { |
| 9360 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
| 9361 | if (ret == 1) { |
| 9362 | if (ctxt != NULL) |
| 9363 | ctxt->flags = oldflags; |
| 9364 | return (1); |
| 9365 | } |
| 9366 | if (ret < 0) { |
| 9367 | if (ctxt != NULL) |
| 9368 | ctxt->flags = oldflags; |
| 9369 | return (ret); |
| 9370 | } |
| 9371 | list = list->next; |
| 9372 | } |
| 9373 | if (ctxt != NULL) { |
| 9374 | if (ret != 0) { |
| 9375 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9376 | xmlRelaxNGDumpValidError(ctxt); |
| 9377 | } else { |
| 9378 | if (ctxt->errNr > 0) |
| 9379 | xmlRelaxNGPopErrors(ctxt, 0); |
| 9380 | } |
| 9381 | } |
| 9382 | ret = 0; |
| 9383 | if (ctxt != NULL) { |
| 9384 | ctxt->flags = oldflags; |
| 9385 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9386 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9387 | TODO ret = -1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9388 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9389 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9390 | } |
| 9391 | |
| 9392 | /** |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9393 | * xmlRelaxNGBestState: |
| 9394 | * @ctxt: a Relax-NG validation context |
| 9395 | * |
| 9396 | * Find the "best" state in the ctxt->states list of states to report |
| 9397 | * errors about. I.e. a state with no element left in the child list |
| 9398 | * or the one with the less attributes left. |
| 9399 | * This is called only if a falidation error was detected |
| 9400 | * |
| 9401 | * Returns the index of the "best" state or -1 in case of error |
| 9402 | */ |
| 9403 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9404 | xmlRelaxNGBestState(xmlRelaxNGValidCtxtPtr ctxt) |
| 9405 | { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9406 | xmlRelaxNGValidStatePtr state; |
| 9407 | int i, tmp; |
| 9408 | int best = -1; |
| 9409 | int value = 1000000; |
| 9410 | |
| 9411 | if ((ctxt == NULL) || (ctxt->states == NULL) || |
| 9412 | (ctxt->states->nbState <= 0)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9413 | return (-1); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9414 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9415 | for (i = 0; i < ctxt->states->nbState; i++) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9416 | state = ctxt->states->tabState[i]; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9417 | if (state == NULL) |
| 9418 | continue; |
| 9419 | if (state->seq != NULL) { |
| 9420 | if ((best == -1) || (value > 100000)) { |
| 9421 | value = 100000; |
| 9422 | best = i; |
| 9423 | } |
| 9424 | } else { |
| 9425 | tmp = state->nbAttrLeft; |
| 9426 | if ((best == -1) || (value > tmp)) { |
| 9427 | value = tmp; |
| 9428 | best = i; |
| 9429 | } |
| 9430 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9431 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9432 | return (best); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9433 | } |
| 9434 | |
| 9435 | /** |
| 9436 | * xmlRelaxNGLogBestError: |
| 9437 | * @ctxt: a Relax-NG validation context |
| 9438 | * |
| 9439 | * Find the "best" state in the ctxt->states list of states to report |
| 9440 | * errors about and log it. |
| 9441 | */ |
| 9442 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9443 | xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt) |
| 9444 | { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9445 | int best; |
| 9446 | |
| 9447 | if ((ctxt == NULL) || (ctxt->states == NULL) || |
| 9448 | (ctxt->states->nbState <= 0)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9449 | return; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9450 | |
| 9451 | best = xmlRelaxNGBestState(ctxt); |
| 9452 | if ((best >= 0) && (best < ctxt->states->nbState)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9453 | ctxt->state = ctxt->states->tabState[best]; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9454 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9455 | xmlRelaxNGValidateElementEnd(ctxt, 1); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9456 | } |
| 9457 | } |
| 9458 | |
| 9459 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9460 | * xmlRelaxNGValidateElementEnd: |
| 9461 | * @ctxt: a Relax-NG validation context |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 9462 | * @dolog: indicate that error logging should be done |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9463 | * |
| 9464 | * Validate the end of the element, implements check that |
| 9465 | * there is nothing left not consumed in the element content |
| 9466 | * or in the attribute list. |
| 9467 | * |
| 9468 | * Returns 0 if the validation succeeded or an error code. |
| 9469 | */ |
| 9470 | static int |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 9471 | xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int dolog) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9472 | { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9473 | int i; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9474 | xmlRelaxNGValidStatePtr state; |
| 9475 | |
| 9476 | state = ctxt->state; |
| 9477 | if (state->seq != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9478 | state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq); |
| 9479 | if (state->seq != NULL) { |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 9480 | if (dolog) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9481 | VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT, |
| 9482 | state->node->name, state->seq->name); |
| 9483 | } |
| 9484 | return (-1); |
| 9485 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9486 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9487 | for (i = 0; i < state->nbAttrs; i++) { |
| 9488 | if (state->attrs[i] != NULL) { |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 9489 | if (dolog) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9490 | VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR, |
| 9491 | state->attrs[i]->name, state->node->name); |
| 9492 | } |
| 9493 | return (-1 - i); |
| 9494 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9495 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9496 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9497 | } |
| 9498 | |
| 9499 | /** |
| 9500 | * xmlRelaxNGValidateState: |
| 9501 | * @ctxt: a Relax-NG validation context |
| 9502 | * @define: the definition to verify |
| 9503 | * |
| 9504 | * Validate the current state against the definition |
| 9505 | * |
| 9506 | * Returns 0 if the validation succeeded or an error code. |
| 9507 | */ |
| 9508 | static int |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9509 | xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt, |
| 9510 | xmlRelaxNGDefinePtr define) |
| 9511 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9512 | xmlNodePtr node; |
| 9513 | int ret = 0, i, tmp, oldflags, errNr; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9514 | xmlRelaxNGValidStatePtr oldstate = NULL, state; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9515 | |
| 9516 | if (define == NULL) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9517 | VALID_ERR(XML_RELAXNG_ERR_NODEFINE); |
| 9518 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9519 | } |
| 9520 | |
| 9521 | if (ctxt->state != NULL) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9522 | node = ctxt->state->seq; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9523 | } else { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9524 | node = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9525 | } |
| 9526 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9527 | for (i = 0; i < ctxt->depth; i++) |
| 9528 | xmlGenericError(xmlGenericErrorContext, " "); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9529 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9530 | "Start validating %s ", xmlRelaxNGDefName(define)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9531 | if (define->name != NULL) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9532 | xmlGenericError(xmlGenericErrorContext, "%s ", define->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9533 | if ((node != NULL) && (node->name != NULL)) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9534 | xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9535 | else |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9536 | xmlGenericError(xmlGenericErrorContext, "\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9537 | #endif |
| 9538 | ctxt->depth++; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9539 | switch (define->type) { |
| 9540 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9541 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 9542 | ret = 0; |
| 9543 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9544 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9545 | ret = -1; |
| 9546 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9547 | case XML_RELAXNG_TEXT: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9548 | while ((node != NULL) && |
| 9549 | ((node->type == XML_TEXT_NODE) || |
| 9550 | (node->type == XML_COMMENT_NODE) || |
| 9551 | (node->type == XML_PI_NODE) || |
| 9552 | (node->type == XML_CDATA_SECTION_NODE))) |
| 9553 | node = node->next; |
| 9554 | ctxt->state->seq = node; |
| 9555 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9556 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9557 | errNr = ctxt->errNr; |
| 9558 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 9559 | if (node == NULL) { |
| 9560 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name); |
| 9561 | ret = -1; |
| 9562 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9563 | xmlRelaxNGDumpValidError(ctxt); |
| 9564 | break; |
| 9565 | } |
| 9566 | if (node->type != XML_ELEMENT_NODE) { |
| 9567 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
| 9568 | ret = -1; |
| 9569 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9570 | xmlRelaxNGDumpValidError(ctxt); |
| 9571 | break; |
| 9572 | } |
| 9573 | /* |
| 9574 | * This node was already validated successfully against |
| 9575 | * this definition. |
| 9576 | */ |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 9577 | if (node->psvi == define) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9578 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
| 9579 | if (ctxt->errNr > errNr) |
| 9580 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9581 | if (ctxt->errNr != 0) { |
| 9582 | while ((ctxt->err != NULL) && |
| 9583 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) |
| 9584 | && (xmlStrEqual(ctxt->err->arg2, node->name))) |
| 9585 | || |
| 9586 | ((ctxt->err->err == |
| 9587 | XML_RELAXNG_ERR_ELEMEXTRANS) |
| 9588 | && (xmlStrEqual(ctxt->err->arg1, node->name))) |
| 9589 | || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) |
| 9590 | || (ctxt->err->err == |
| 9591 | XML_RELAXNG_ERR_NOTELEM))) |
| 9592 | xmlRelaxNGValidErrorPop(ctxt); |
| 9593 | } |
| 9594 | break; |
| 9595 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9596 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9597 | ret = xmlRelaxNGElementMatch(ctxt, define, node); |
| 9598 | if (ret <= 0) { |
| 9599 | ret = -1; |
| 9600 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9601 | xmlRelaxNGDumpValidError(ctxt); |
| 9602 | break; |
| 9603 | } |
| 9604 | ret = 0; |
| 9605 | if (ctxt->errNr != 0) { |
| 9606 | if (ctxt->errNr > errNr) |
| 9607 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9608 | while ((ctxt->err != NULL) && |
| 9609 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) && |
| 9610 | (xmlStrEqual(ctxt->err->arg2, node->name))) || |
| 9611 | ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) && |
| 9612 | (xmlStrEqual(ctxt->err->arg1, node->name))) || |
| 9613 | (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) || |
| 9614 | (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM))) |
| 9615 | xmlRelaxNGValidErrorPop(ctxt); |
| 9616 | } |
| 9617 | errNr = ctxt->errNr; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9618 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9619 | oldflags = ctxt->flags; |
| 9620 | if (ctxt->flags & FLAGS_MIXED_CONTENT) { |
| 9621 | ctxt->flags -= FLAGS_MIXED_CONTENT; |
| 9622 | } |
| 9623 | state = xmlRelaxNGNewValidState(ctxt, node); |
| 9624 | if (state == NULL) { |
| 9625 | ret = -1; |
| 9626 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9627 | xmlRelaxNGDumpValidError(ctxt); |
| 9628 | break; |
| 9629 | } |
Daniel Veillard | 7fe1f3a | 2003-03-31 22:13:33 +0000 | [diff] [blame] | 9630 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9631 | oldstate = ctxt->state; |
| 9632 | ctxt->state = state; |
| 9633 | if (define->attrs != NULL) { |
| 9634 | tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
| 9635 | if (tmp != 0) { |
| 9636 | ret = -1; |
| 9637 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
| 9638 | } |
| 9639 | } |
| 9640 | if (define->contModel != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9641 | xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state; |
| 9642 | xmlRelaxNGStatesPtr tmpstates = ctxt->states; |
| 9643 | xmlNodePtr nseq; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9644 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9645 | nstate = xmlRelaxNGNewValidState(ctxt, node); |
| 9646 | ctxt->state = nstate; |
| 9647 | ctxt->states = NULL; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9648 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9649 | tmp = xmlRelaxNGValidateCompiledContent(ctxt, |
| 9650 | define->contModel, |
| 9651 | ctxt->state->seq); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9652 | nseq = ctxt->state->seq; |
| 9653 | ctxt->state = tmpstate; |
| 9654 | ctxt->states = tmpstates; |
| 9655 | xmlRelaxNGFreeValidState(ctxt, nstate); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9656 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9657 | #ifdef DEBUG_COMPILE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9658 | xmlGenericError(xmlGenericErrorContext, |
| 9659 | "Validating content of '%s' : %d\n", |
| 9660 | define->name, tmp); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9661 | #endif |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9662 | if (tmp != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9663 | ret = -1; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9664 | |
| 9665 | if (ctxt->states != NULL) { |
| 9666 | tmp = -1; |
| 9667 | |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9668 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9669 | state = ctxt->states->tabState[i]; |
| 9670 | ctxt->state = state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9671 | ctxt->state->seq = nseq; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9672 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9673 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9674 | tmp = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9675 | break; |
| 9676 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9677 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9678 | if (tmp != 0) { |
| 9679 | /* |
| 9680 | * validation error, log the message for the "best" one |
| 9681 | */ |
| 9682 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9683 | xmlRelaxNGLogBestError(ctxt); |
| 9684 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9685 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9686 | xmlRelaxNGFreeValidState(ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9687 | ctxt->states-> |
| 9688 | tabState[i]); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9689 | } |
| 9690 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9691 | ctxt->flags = oldflags; |
| 9692 | ctxt->states = NULL; |
| 9693 | if ((ret == 0) && (tmp == -1)) |
| 9694 | ret = -1; |
| 9695 | } else { |
| 9696 | state = ctxt->state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9697 | ctxt->state->seq = nseq; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9698 | if (ret == 0) |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9699 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9700 | xmlRelaxNGFreeValidState(ctxt, state); |
| 9701 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9702 | } else { |
| 9703 | if (define->content != NULL) { |
| 9704 | tmp = xmlRelaxNGValidateDefinitionList(ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9705 | define-> |
| 9706 | content); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9707 | if (tmp != 0) { |
| 9708 | ret = -1; |
| 9709 | if (ctxt->state == NULL) { |
| 9710 | ctxt->state = oldstate; |
| 9711 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, |
| 9712 | node->name); |
| 9713 | ctxt->state = NULL; |
| 9714 | } else { |
| 9715 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, |
| 9716 | node->name); |
| 9717 | } |
| 9718 | |
| 9719 | } |
| 9720 | } |
| 9721 | if (ctxt->states != NULL) { |
| 9722 | tmp = -1; |
| 9723 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9724 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9725 | state = ctxt->states->tabState[i]; |
| 9726 | ctxt->state = state; |
| 9727 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9728 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9729 | tmp = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9730 | break; |
| 9731 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9732 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9733 | if (tmp != 0) { |
| 9734 | /* |
| 9735 | * validation error, log the message for the "best" one |
| 9736 | */ |
| 9737 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9738 | xmlRelaxNGLogBestError(ctxt); |
| 9739 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9740 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9741 | xmlRelaxNGFreeValidState(ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9742 | ctxt->states-> |
| 9743 | tabState[i]); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9744 | } |
| 9745 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9746 | ctxt->flags = oldflags; |
| 9747 | ctxt->states = NULL; |
| 9748 | if ((ret == 0) && (tmp == -1)) |
| 9749 | ret = -1; |
| 9750 | } else { |
| 9751 | state = ctxt->state; |
| 9752 | if (ret == 0) |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9753 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9754 | xmlRelaxNGFreeValidState(ctxt, state); |
| 9755 | } |
| 9756 | } |
| 9757 | if (ret == 0) { |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 9758 | node->psvi = define; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9759 | } |
| 9760 | ctxt->flags = oldflags; |
| 9761 | ctxt->state = oldstate; |
| 9762 | if (oldstate != NULL) |
| 9763 | oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
| 9764 | if (ret != 0) { |
| 9765 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 9766 | xmlRelaxNGDumpValidError(ctxt); |
| 9767 | ret = 0; |
| 9768 | } else { |
| 9769 | ret = -2; |
| 9770 | } |
| 9771 | } else { |
| 9772 | if (ctxt->errNr > errNr) |
| 9773 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9774 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9775 | |
| 9776 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9777 | xmlGenericError(xmlGenericErrorContext, |
| 9778 | "xmlRelaxNGValidateDefinition(): validated %s : %d", |
| 9779 | node->name, ret); |
| 9780 | if (oldstate == NULL) |
| 9781 | xmlGenericError(xmlGenericErrorContext, ": no state\n"); |
| 9782 | else if (oldstate->seq == NULL) |
| 9783 | xmlGenericError(xmlGenericErrorContext, ": done\n"); |
| 9784 | else if (oldstate->seq->type == XML_ELEMENT_NODE) |
| 9785 | xmlGenericError(xmlGenericErrorContext, ": next elem %s\n", |
| 9786 | oldstate->seq->name); |
| 9787 | else |
| 9788 | xmlGenericError(xmlGenericErrorContext, ": next %s %d\n", |
| 9789 | oldstate->seq->name, oldstate->seq->type); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9790 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9791 | break; |
| 9792 | case XML_RELAXNG_OPTIONAL:{ |
| 9793 | errNr = ctxt->errNr; |
| 9794 | oldflags = ctxt->flags; |
| 9795 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9796 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
| 9797 | ret = |
| 9798 | xmlRelaxNGValidateDefinitionList(ctxt, |
| 9799 | define->content); |
| 9800 | if (ret != 0) { |
| 9801 | if (ctxt->state != NULL) |
| 9802 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 9803 | ctxt->state = oldstate; |
| 9804 | ctxt->flags = oldflags; |
| 9805 | ret = 0; |
| 9806 | if (ctxt->errNr > errNr) |
| 9807 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9808 | break; |
| 9809 | } |
| 9810 | if (ctxt->states != NULL) { |
| 9811 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
| 9812 | } else { |
| 9813 | ctxt->states = xmlRelaxNGNewStates(ctxt, 1); |
| 9814 | if (ctxt->states == NULL) { |
| 9815 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 9816 | ctxt->flags = oldflags; |
| 9817 | ret = -1; |
| 9818 | if (ctxt->errNr > errNr) |
| 9819 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9820 | break; |
| 9821 | } |
| 9822 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
| 9823 | xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state); |
| 9824 | ctxt->state = NULL; |
| 9825 | } |
| 9826 | ctxt->flags = oldflags; |
| 9827 | ret = 0; |
| 9828 | if (ctxt->errNr > errNr) |
| 9829 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9830 | break; |
| 9831 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9832 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9833 | errNr = ctxt->errNr; |
| 9834 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 9835 | if (ret != 0) { |
| 9836 | break; |
| 9837 | } |
| 9838 | if (ctxt->errNr > errNr) |
| 9839 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9840 | /* no break on purpose */ |
| 9841 | case XML_RELAXNG_ZEROORMORE:{ |
| 9842 | int progress; |
| 9843 | xmlRelaxNGStatesPtr states = NULL, res = NULL; |
| 9844 | int base, j; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9845 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9846 | errNr = ctxt->errNr; |
| 9847 | res = xmlRelaxNGNewStates(ctxt, 1); |
| 9848 | if (res == NULL) { |
| 9849 | ret = -1; |
| 9850 | break; |
| 9851 | } |
| 9852 | /* |
| 9853 | * All the input states are also exit states |
| 9854 | */ |
| 9855 | if (ctxt->state != NULL) { |
| 9856 | xmlRelaxNGAddStates(ctxt, res, |
| 9857 | xmlRelaxNGCopyValidState(ctxt, |
| 9858 | ctxt-> |
| 9859 | state)); |
| 9860 | } else { |
| 9861 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 9862 | xmlRelaxNGAddStates(ctxt, res, |
| 9863 | xmlRelaxNGCopyValidState(ctxt, |
| 9864 | ctxt-> |
| 9865 | states-> |
| 9866 | tabState |
| 9867 | [j])); |
| 9868 | } |
| 9869 | } |
| 9870 | oldflags = ctxt->flags; |
| 9871 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9872 | do { |
| 9873 | progress = 0; |
| 9874 | base = res->nbState; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9875 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9876 | if (ctxt->states != NULL) { |
| 9877 | states = ctxt->states; |
| 9878 | for (i = 0; i < states->nbState; i++) { |
| 9879 | ctxt->state = states->tabState[i]; |
| 9880 | ctxt->states = NULL; |
| 9881 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
| 9882 | define-> |
| 9883 | content); |
| 9884 | if (ret == 0) { |
| 9885 | if (ctxt->state != NULL) { |
| 9886 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 9887 | ctxt->state); |
| 9888 | ctxt->state = NULL; |
| 9889 | if (tmp == 1) |
| 9890 | progress = 1; |
| 9891 | } else if (ctxt->states != NULL) { |
| 9892 | for (j = 0; j < ctxt->states->nbState; |
| 9893 | j++) { |
| 9894 | tmp = |
| 9895 | xmlRelaxNGAddStates(ctxt, res, |
| 9896 | ctxt-> |
| 9897 | states-> |
| 9898 | tabState |
| 9899 | [j]); |
| 9900 | if (tmp == 1) |
| 9901 | progress = 1; |
| 9902 | } |
| 9903 | xmlRelaxNGFreeStates(ctxt, |
| 9904 | ctxt->states); |
| 9905 | ctxt->states = NULL; |
| 9906 | } |
| 9907 | } else { |
| 9908 | if (ctxt->state != NULL) { |
| 9909 | xmlRelaxNGFreeValidState(ctxt, |
| 9910 | ctxt->state); |
| 9911 | ctxt->state = NULL; |
| 9912 | } |
| 9913 | } |
| 9914 | } |
| 9915 | } else { |
| 9916 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
| 9917 | define-> |
| 9918 | content); |
| 9919 | if (ret != 0) { |
| 9920 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 9921 | ctxt->state = NULL; |
| 9922 | } else { |
| 9923 | base = res->nbState; |
| 9924 | if (ctxt->state != NULL) { |
| 9925 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 9926 | ctxt->state); |
| 9927 | ctxt->state = NULL; |
| 9928 | if (tmp == 1) |
| 9929 | progress = 1; |
| 9930 | } else if (ctxt->states != NULL) { |
| 9931 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 9932 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 9933 | ctxt-> |
| 9934 | states-> |
| 9935 | tabState[j]); |
| 9936 | if (tmp == 1) |
| 9937 | progress = 1; |
| 9938 | } |
| 9939 | if (states == NULL) { |
| 9940 | states = ctxt->states; |
| 9941 | } else { |
| 9942 | xmlRelaxNGFreeStates(ctxt, |
| 9943 | ctxt->states); |
| 9944 | } |
| 9945 | ctxt->states = NULL; |
| 9946 | } |
| 9947 | } |
| 9948 | } |
| 9949 | if (progress) { |
| 9950 | /* |
| 9951 | * Collect all the new nodes added at that step |
| 9952 | * and make them the new node set |
| 9953 | */ |
| 9954 | if (res->nbState - base == 1) { |
| 9955 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, |
| 9956 | res-> |
| 9957 | tabState |
| 9958 | [base]); |
| 9959 | } else { |
| 9960 | if (states == NULL) { |
| 9961 | xmlRelaxNGNewStates(ctxt, |
| 9962 | res->nbState - base); |
| 9963 | } |
| 9964 | states->nbState = 0; |
| 9965 | for (i = base; i < res->nbState; i++) |
| 9966 | xmlRelaxNGAddStates(ctxt, states, |
| 9967 | xmlRelaxNGCopyValidState |
| 9968 | (ctxt, |
| 9969 | res->tabState[i])); |
| 9970 | ctxt->states = states; |
| 9971 | } |
| 9972 | } |
| 9973 | } while (progress == 1); |
| 9974 | if (states != NULL) { |
| 9975 | xmlRelaxNGFreeStates(ctxt, states); |
| 9976 | } |
| 9977 | ctxt->states = res; |
| 9978 | ctxt->flags = oldflags; |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 9979 | #if 0 |
| 9980 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9981 | * errors may have to be propagated back... |
| 9982 | */ |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9983 | if (ctxt->errNr > errNr) |
| 9984 | xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 9985 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9986 | ret = 0; |
| 9987 | break; |
| 9988 | } |
| 9989 | case XML_RELAXNG_CHOICE:{ |
| 9990 | xmlRelaxNGDefinePtr list = NULL; |
| 9991 | xmlRelaxNGStatesPtr states = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9992 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9993 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9994 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9995 | errNr = ctxt->errNr; |
| 9996 | if ((define->dflags & IS_TRIABLE) |
| 9997 | && (define->data != NULL)) { |
| 9998 | xmlHashTablePtr triage = |
| 9999 | (xmlHashTablePtr) define->data; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 10000 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10001 | /* |
| 10002 | * Something we can optimize cleanly there is only one |
| 10003 | * possble branch out ! |
| 10004 | */ |
| 10005 | if (node == NULL) { |
| 10006 | ret = -1; |
| 10007 | break; |
| 10008 | } |
| 10009 | if ((node->type == XML_TEXT_NODE) || |
| 10010 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 10011 | list = |
| 10012 | xmlHashLookup2(triage, BAD_CAST "#text", NULL); |
| 10013 | } else if (node->type == XML_ELEMENT_NODE) { |
| 10014 | if (node->ns != NULL) { |
| 10015 | list = xmlHashLookup2(triage, node->name, |
| 10016 | node->ns->href); |
| 10017 | if (list == NULL) |
| 10018 | list = |
| 10019 | xmlHashLookup2(triage, BAD_CAST "#any", |
| 10020 | node->ns->href); |
| 10021 | } else |
| 10022 | list = |
| 10023 | xmlHashLookup2(triage, node->name, NULL); |
| 10024 | if (list == NULL) |
| 10025 | list = |
| 10026 | xmlHashLookup2(triage, BAD_CAST "#any", |
| 10027 | NULL); |
| 10028 | } |
| 10029 | if (list == NULL) { |
| 10030 | ret = -1; |
| 10031 | break; |
| 10032 | } |
| 10033 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
| 10034 | if (ret == 0) { |
| 10035 | } |
| 10036 | break; |
| 10037 | } |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 10038 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10039 | list = define->content; |
| 10040 | oldflags = ctxt->flags; |
| 10041 | ctxt->flags |= FLAGS_IGNORABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10042 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10043 | while (list != NULL) { |
| 10044 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
| 10045 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
| 10046 | if (ret == 0) { |
| 10047 | if (states == NULL) { |
| 10048 | states = xmlRelaxNGNewStates(ctxt, 1); |
| 10049 | } |
| 10050 | if (ctxt->state != NULL) { |
| 10051 | xmlRelaxNGAddStates(ctxt, states, ctxt->state); |
| 10052 | } else if (ctxt->states != NULL) { |
| 10053 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 10054 | xmlRelaxNGAddStates(ctxt, states, |
| 10055 | ctxt->states-> |
| 10056 | tabState[i]); |
| 10057 | } |
| 10058 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10059 | ctxt->states = NULL; |
| 10060 | } |
| 10061 | } else { |
| 10062 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10063 | } |
| 10064 | ctxt->state = oldstate; |
| 10065 | list = list->next; |
| 10066 | } |
| 10067 | if (states != NULL) { |
| 10068 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 10069 | ctxt->states = states; |
| 10070 | ctxt->state = NULL; |
| 10071 | ret = 0; |
| 10072 | } else { |
| 10073 | ctxt->states = NULL; |
| 10074 | } |
| 10075 | ctxt->flags = oldflags; |
| 10076 | if (ret != 0) { |
| 10077 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 10078 | xmlRelaxNGDumpValidError(ctxt); |
| 10079 | } |
| 10080 | } else { |
| 10081 | if (ctxt->errNr > errNr) |
| 10082 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 10083 | } |
| 10084 | break; |
| 10085 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10086 | case XML_RELAXNG_DEF: |
| 10087 | case XML_RELAXNG_GROUP: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10088 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 10089 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10090 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10091 | ret = xmlRelaxNGValidateInterleave(ctxt, define); |
| 10092 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10093 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10094 | ret = xmlRelaxNGValidateAttribute(ctxt, define); |
| 10095 | break; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 10096 | case XML_RELAXNG_START: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10097 | case XML_RELAXNG_NOOP: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10098 | case XML_RELAXNG_REF: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10099 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 10100 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10101 | ret = xmlRelaxNGValidateDefinition(ctxt, define->content); |
| 10102 | break; |
| 10103 | case XML_RELAXNG_DATATYPE:{ |
| 10104 | xmlNodePtr child; |
| 10105 | xmlChar *content = NULL; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10106 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10107 | child = node; |
| 10108 | while (child != NULL) { |
| 10109 | if (child->type == XML_ELEMENT_NODE) { |
| 10110 | VALID_ERR2(XML_RELAXNG_ERR_DATAELEM, |
| 10111 | node->parent->name); |
| 10112 | ret = -1; |
| 10113 | break; |
| 10114 | } else if ((child->type == XML_TEXT_NODE) || |
| 10115 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 10116 | content = xmlStrcat(content, child->content); |
| 10117 | } |
| 10118 | /* TODO: handle entities ... */ |
| 10119 | child = child->next; |
| 10120 | } |
| 10121 | if (ret == -1) { |
| 10122 | if (content != NULL) |
| 10123 | xmlFree(content); |
| 10124 | break; |
| 10125 | } |
| 10126 | if (content == NULL) { |
| 10127 | content = xmlStrdup(BAD_CAST ""); |
| 10128 | if (content == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10129 | xmlRngVErrMemory(ctxt, "validating\n"); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10130 | ret = -1; |
| 10131 | break; |
| 10132 | } |
| 10133 | } |
| 10134 | ret = xmlRelaxNGValidateDatatype(ctxt, content, define, |
| 10135 | ctxt->state->seq); |
| 10136 | if (ret == -1) { |
| 10137 | VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name); |
| 10138 | } else if (ret == 0) { |
| 10139 | ctxt->state->seq = NULL; |
| 10140 | } |
| 10141 | if (content != NULL) |
| 10142 | xmlFree(content); |
| 10143 | break; |
| 10144 | } |
| 10145 | case XML_RELAXNG_VALUE:{ |
| 10146 | xmlChar *content = NULL; |
| 10147 | xmlChar *oldvalue; |
| 10148 | xmlNodePtr child; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10149 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10150 | child = node; |
| 10151 | while (child != NULL) { |
| 10152 | if (child->type == XML_ELEMENT_NODE) { |
| 10153 | VALID_ERR2(XML_RELAXNG_ERR_VALELEM, |
| 10154 | node->parent->name); |
| 10155 | ret = -1; |
| 10156 | break; |
| 10157 | } else if ((child->type == XML_TEXT_NODE) || |
| 10158 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 10159 | content = xmlStrcat(content, child->content); |
| 10160 | } |
| 10161 | /* TODO: handle entities ... */ |
| 10162 | child = child->next; |
| 10163 | } |
| 10164 | if (ret == -1) { |
| 10165 | if (content != NULL) |
| 10166 | xmlFree(content); |
| 10167 | break; |
| 10168 | } |
| 10169 | if (content == NULL) { |
| 10170 | content = xmlStrdup(BAD_CAST ""); |
| 10171 | if (content == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10172 | xmlRngVErrMemory(ctxt, "validating\n"); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10173 | ret = -1; |
| 10174 | break; |
| 10175 | } |
| 10176 | } |
| 10177 | oldvalue = ctxt->state->value; |
| 10178 | ctxt->state->value = content; |
| 10179 | ret = xmlRelaxNGValidateValue(ctxt, define); |
| 10180 | ctxt->state->value = oldvalue; |
| 10181 | if (ret == -1) { |
| 10182 | VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name); |
| 10183 | } else if (ret == 0) { |
| 10184 | ctxt->state->seq = NULL; |
| 10185 | } |
| 10186 | if (content != NULL) |
| 10187 | xmlFree(content); |
| 10188 | break; |
| 10189 | } |
| 10190 | case XML_RELAXNG_LIST:{ |
| 10191 | xmlChar *content; |
| 10192 | xmlNodePtr child; |
| 10193 | xmlChar *oldvalue, *oldendvalue; |
| 10194 | int len; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10195 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10196 | /* |
| 10197 | * Make sure it's only text nodes |
| 10198 | */ |
| 10199 | |
| 10200 | content = NULL; |
| 10201 | child = node; |
| 10202 | while (child != NULL) { |
| 10203 | if (child->type == XML_ELEMENT_NODE) { |
| 10204 | VALID_ERR2(XML_RELAXNG_ERR_LISTELEM, |
| 10205 | node->parent->name); |
| 10206 | ret = -1; |
| 10207 | break; |
| 10208 | } else if ((child->type == XML_TEXT_NODE) || |
| 10209 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 10210 | content = xmlStrcat(content, child->content); |
| 10211 | } |
| 10212 | /* TODO: handle entities ... */ |
| 10213 | child = child->next; |
| 10214 | } |
| 10215 | if (ret == -1) { |
| 10216 | if (content != NULL) |
| 10217 | xmlFree(content); |
| 10218 | break; |
| 10219 | } |
| 10220 | if (content == NULL) { |
| 10221 | content = xmlStrdup(BAD_CAST ""); |
| 10222 | if (content == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10223 | xmlRngVErrMemory(ctxt, "validating\n"); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10224 | ret = -1; |
| 10225 | break; |
| 10226 | } |
| 10227 | } |
| 10228 | len = xmlStrlen(content); |
| 10229 | oldvalue = ctxt->state->value; |
| 10230 | oldendvalue = ctxt->state->endvalue; |
| 10231 | ctxt->state->value = content; |
| 10232 | ctxt->state->endvalue = content + len; |
| 10233 | ret = xmlRelaxNGValidateValue(ctxt, define); |
| 10234 | ctxt->state->value = oldvalue; |
| 10235 | ctxt->state->endvalue = oldendvalue; |
| 10236 | if (ret == -1) { |
| 10237 | VALID_ERR(XML_RELAXNG_ERR_LIST); |
| 10238 | } else if ((ret == 0) && (node != NULL)) { |
| 10239 | ctxt->state->seq = node->next; |
| 10240 | } |
| 10241 | if (content != NULL) |
| 10242 | xmlFree(content); |
| 10243 | break; |
| 10244 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10245 | case XML_RELAXNG_EXCEPT: |
| 10246 | case XML_RELAXNG_PARAM: |
| 10247 | TODO ret = -1; |
| 10248 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10249 | } |
| 10250 | ctxt->depth--; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10251 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10252 | for (i = 0; i < ctxt->depth; i++) |
| 10253 | xmlGenericError(xmlGenericErrorContext, " "); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10254 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10255 | "Validating %s ", xmlRelaxNGDefName(define)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10256 | if (define->name != NULL) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10257 | xmlGenericError(xmlGenericErrorContext, "%s ", define->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10258 | if (ret == 0) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10259 | xmlGenericError(xmlGenericErrorContext, "suceeded\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10260 | else |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10261 | xmlGenericError(xmlGenericErrorContext, "failed\n"); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10262 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10263 | return (ret); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10264 | } |
| 10265 | |
| 10266 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10267 | * xmlRelaxNGValidateDefinition: |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10268 | * @ctxt: a Relax-NG validation context |
| 10269 | * @define: the definition to verify |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10270 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10271 | * Validate the current node lists against the definition |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10272 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10273 | * Returns 0 if the validation succeeded or an error code. |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10274 | */ |
| 10275 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10276 | xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
| 10277 | xmlRelaxNGDefinePtr define) |
| 10278 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10279 | xmlRelaxNGStatesPtr states, res; |
| 10280 | int i, j, k, ret, oldflags; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10281 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10282 | /* |
| 10283 | * We should NOT have both ctxt->state and ctxt->states |
| 10284 | */ |
| 10285 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10286 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10287 | ctxt->state = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10288 | } |
| 10289 | |
| 10290 | if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10291 | if (ctxt->states != NULL) { |
| 10292 | ctxt->state = ctxt->states->tabState[0]; |
| 10293 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10294 | ctxt->states = NULL; |
| 10295 | } |
| 10296 | ret = xmlRelaxNGValidateState(ctxt, define); |
| 10297 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10298 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10299 | ctxt->state = NULL; |
| 10300 | } |
| 10301 | if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) { |
| 10302 | ctxt->state = ctxt->states->tabState[0]; |
| 10303 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10304 | ctxt->states = NULL; |
| 10305 | } |
| 10306 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10307 | } |
| 10308 | |
| 10309 | states = ctxt->states; |
| 10310 | ctxt->states = NULL; |
| 10311 | res = NULL; |
| 10312 | j = 0; |
| 10313 | oldflags = ctxt->flags; |
| 10314 | ctxt->flags |= FLAGS_IGNORABLE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10315 | for (i = 0; i < states->nbState; i++) { |
| 10316 | ctxt->state = states->tabState[i]; |
| 10317 | ctxt->states = NULL; |
| 10318 | ret = xmlRelaxNGValidateState(ctxt, define); |
| 10319 | /* |
| 10320 | * We should NOT have both ctxt->state and ctxt->states |
| 10321 | */ |
| 10322 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10323 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10324 | ctxt->state = NULL; |
| 10325 | } |
| 10326 | if (ret == 0) { |
| 10327 | if (ctxt->states == NULL) { |
| 10328 | if (res != NULL) { |
| 10329 | /* add the state to the container */ |
| 10330 | xmlRelaxNGAddStates(ctxt, res, ctxt->state); |
| 10331 | ctxt->state = NULL; |
| 10332 | } else { |
| 10333 | /* add the state directly in states */ |
| 10334 | states->tabState[j++] = ctxt->state; |
| 10335 | ctxt->state = NULL; |
| 10336 | } |
| 10337 | } else { |
| 10338 | if (res == NULL) { |
| 10339 | /* make it the new container and copy other results */ |
| 10340 | res = ctxt->states; |
| 10341 | ctxt->states = NULL; |
| 10342 | for (k = 0; k < j; k++) |
| 10343 | xmlRelaxNGAddStates(ctxt, res, |
| 10344 | states->tabState[k]); |
| 10345 | } else { |
| 10346 | /* add all the new results to res and reff the container */ |
| 10347 | for (k = 0; k < ctxt->states->nbState; k++) |
| 10348 | xmlRelaxNGAddStates(ctxt, res, |
| 10349 | ctxt->states->tabState[k]); |
| 10350 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10351 | ctxt->states = NULL; |
| 10352 | } |
| 10353 | } |
| 10354 | } else { |
| 10355 | if (ctxt->state != NULL) { |
| 10356 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10357 | ctxt->state = NULL; |
| 10358 | } else if (ctxt->states != NULL) { |
| 10359 | for (k = 0; k < ctxt->states->nbState; k++) |
| 10360 | xmlRelaxNGFreeValidState(ctxt, |
| 10361 | ctxt->states->tabState[k]); |
| 10362 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10363 | ctxt->states = NULL; |
| 10364 | } |
| 10365 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10366 | } |
| 10367 | ctxt->flags = oldflags; |
| 10368 | if (res != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10369 | xmlRelaxNGFreeStates(ctxt, states); |
| 10370 | ctxt->states = res; |
| 10371 | ret = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10372 | } else if (j > 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10373 | states->nbState = j; |
| 10374 | ctxt->states = states; |
| 10375 | ret = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10376 | } else if (j == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10377 | ctxt->state = states->tabState[0]; |
| 10378 | xmlRelaxNGFreeStates(ctxt, states); |
| 10379 | ret = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10380 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10381 | ret = -1; |
| 10382 | xmlRelaxNGFreeStates(ctxt, states); |
| 10383 | if (ctxt->states != NULL) { |
| 10384 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10385 | ctxt->states = NULL; |
| 10386 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10387 | } |
| 10388 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10389 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10390 | ctxt->state = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10391 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10392 | return (ret); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10393 | } |
| 10394 | |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10395 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10396 | * xmlRelaxNGValidateDocument: |
| 10397 | * @ctxt: a Relax-NG validation context |
| 10398 | * @doc: the document |
| 10399 | * |
| 10400 | * Validate the given document |
| 10401 | * |
| 10402 | * Returns 0 if the validation succeeded or an error code. |
| 10403 | */ |
| 10404 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10405 | xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) |
| 10406 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10407 | int ret; |
| 10408 | xmlRelaxNGPtr schema; |
| 10409 | xmlRelaxNGGrammarPtr grammar; |
| 10410 | xmlRelaxNGValidStatePtr state; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10411 | xmlNodePtr node; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10412 | |
| 10413 | if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10414 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10415 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10416 | ctxt->errNo = XML_RELAXNG_OK; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10417 | schema = ctxt->schema; |
| 10418 | grammar = schema->topgrammar; |
| 10419 | if (grammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10420 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 10421 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10422 | } |
| 10423 | state = xmlRelaxNGNewValidState(ctxt, NULL); |
| 10424 | ctxt->state = state; |
| 10425 | ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10426 | if ((ctxt->state != NULL) && (state->seq != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10427 | state = ctxt->state; |
| 10428 | node = state->seq; |
| 10429 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 10430 | if (node != NULL) { |
| 10431 | if (ret != -1) { |
| 10432 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
| 10433 | ret = -1; |
| 10434 | } |
| 10435 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10436 | } else if (ctxt->states != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10437 | int i; |
| 10438 | int tmp = -1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10439 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10440 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 10441 | state = ctxt->states->tabState[i]; |
| 10442 | node = state->seq; |
| 10443 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 10444 | if (node == NULL) |
| 10445 | tmp = 0; |
| 10446 | xmlRelaxNGFreeValidState(ctxt, state); |
| 10447 | } |
| 10448 | if (tmp == -1) { |
| 10449 | if (ret != -1) { |
| 10450 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
| 10451 | ret = -1; |
| 10452 | } |
| 10453 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10454 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 10455 | if (ctxt->state != NULL) { |
| 10456 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10457 | ctxt->state = NULL; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 10458 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10459 | if (ret != 0) |
| 10460 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 10461 | #ifdef DEBUG |
| 10462 | else if (ctxt->errNr != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10463 | ctxt->error(ctxt->userData, |
| 10464 | "%d Extra error messages left on stack !\n", |
| 10465 | ctxt->errNr); |
| 10466 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 10467 | } |
| 10468 | #endif |
Daniel Veillard | f54cd53 | 2004-02-25 11:52:31 +0000 | [diff] [blame] | 10469 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10470 | if (ctxt->idref == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10471 | xmlValidCtxt vctxt; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10472 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10473 | memset(&vctxt, 0, sizeof(xmlValidCtxt)); |
| 10474 | vctxt.valid = 1; |
| 10475 | vctxt.error = ctxt->error; |
| 10476 | vctxt.warning = ctxt->warning; |
| 10477 | vctxt.userData = ctxt->userData; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10478 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10479 | if (xmlValidateDocumentFinal(&vctxt, doc) != 1) |
| 10480 | ret = -1; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10481 | } |
Daniel Veillard | f54cd53 | 2004-02-25 11:52:31 +0000 | [diff] [blame] | 10482 | #endif /* LIBXML_VALID_ENABLED */ |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10483 | if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10484 | ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10485 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10486 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10487 | } |
| 10488 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10489 | /************************************************************************ |
| 10490 | * * |
| 10491 | * Validation interfaces * |
| 10492 | * * |
| 10493 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10494 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10495 | /** |
| 10496 | * xmlRelaxNGNewValidCtxt: |
| 10497 | * @schema: a precompiled XML RelaxNGs |
| 10498 | * |
| 10499 | * Create an XML RelaxNGs validation context based on the given schema |
| 10500 | * |
| 10501 | * Returns the validation context or NULL in case of error |
| 10502 | */ |
| 10503 | xmlRelaxNGValidCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10504 | xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema) |
| 10505 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10506 | xmlRelaxNGValidCtxtPtr ret; |
| 10507 | |
| 10508 | ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt)); |
| 10509 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10510 | xmlRngVErrMemory(NULL, "building context\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10511 | return (NULL); |
| 10512 | } |
| 10513 | memset(ret, 0, sizeof(xmlRelaxNGValidCtxt)); |
| 10514 | ret->schema = schema; |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 10515 | ret->error = xmlGenericError; |
| 10516 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 10517 | ret->errNr = 0; |
| 10518 | ret->errMax = 0; |
| 10519 | ret->err = NULL; |
| 10520 | ret->errTab = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10521 | ret->idref = schema->idref; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10522 | ret->states = NULL; |
| 10523 | ret->freeState = NULL; |
| 10524 | ret->freeStates = NULL; |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10525 | ret->errNo = XML_RELAXNG_OK; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10526 | return (ret); |
| 10527 | } |
| 10528 | |
| 10529 | /** |
| 10530 | * xmlRelaxNGFreeValidCtxt: |
| 10531 | * @ctxt: the schema validation context |
| 10532 | * |
| 10533 | * Free the resources associated to the schema validation context |
| 10534 | */ |
| 10535 | void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10536 | xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt) |
| 10537 | { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10538 | int k; |
| 10539 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10540 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10541 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10542 | if (ctxt->states != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10543 | xmlRelaxNGFreeStates(NULL, ctxt->states); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10544 | if (ctxt->freeState != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10545 | for (k = 0; k < ctxt->freeState->nbState; k++) { |
| 10546 | xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]); |
| 10547 | } |
| 10548 | xmlRelaxNGFreeStates(NULL, ctxt->freeState); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10549 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10550 | if (ctxt->freeStates != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10551 | for (k = 0; k < ctxt->freeStatesNr; k++) { |
| 10552 | xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]); |
| 10553 | } |
| 10554 | xmlFree(ctxt->freeStates); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10555 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 10556 | if (ctxt->errTab != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10557 | xmlFree(ctxt->errTab); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 10558 | if (ctxt->elemTab != NULL) { |
| 10559 | xmlRegExecCtxtPtr exec; |
| 10560 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10561 | exec = xmlRelaxNGElemPop(ctxt); |
| 10562 | while (exec != NULL) { |
| 10563 | xmlRegFreeExecCtxt(exec); |
| 10564 | exec = xmlRelaxNGElemPop(ctxt); |
| 10565 | } |
| 10566 | xmlFree(ctxt->elemTab); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 10567 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10568 | xmlFree(ctxt); |
| 10569 | } |
| 10570 | |
| 10571 | /** |
| 10572 | * xmlRelaxNGSetValidErrors: |
| 10573 | * @ctxt: a Relax-NG validation context |
| 10574 | * @err: the error function |
| 10575 | * @warn: the warning function |
| 10576 | * @ctx: the functions context |
| 10577 | * |
| 10578 | * Set the error and warning callback informations |
| 10579 | */ |
| 10580 | void |
| 10581 | xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10582 | xmlRelaxNGValidityErrorFunc err, |
| 10583 | xmlRelaxNGValidityWarningFunc warn, void *ctx) |
| 10584 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10585 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10586 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10587 | ctxt->error = err; |
| 10588 | ctxt->warning = warn; |
| 10589 | ctxt->userData = ctx; |
| 10590 | } |
| 10591 | |
| 10592 | /** |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 10593 | * xmlRelaxNGGetValidErrors: |
| 10594 | * @ctxt: a Relax-NG validation context |
| 10595 | * @err: the error function result |
| 10596 | * @warn: the warning function result |
| 10597 | * @ctx: the functions context result |
| 10598 | * |
| 10599 | * Get the error and warning callback informations |
| 10600 | * |
| 10601 | * Returns -1 in case of error and 0 otherwise |
| 10602 | */ |
| 10603 | int |
| 10604 | xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10605 | xmlRelaxNGValidityErrorFunc * err, |
| 10606 | xmlRelaxNGValidityWarningFunc * warn, void **ctx) |
| 10607 | { |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 10608 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10609 | return (-1); |
| 10610 | if (err != NULL) |
| 10611 | *err = ctxt->error; |
| 10612 | if (warn != NULL) |
| 10613 | *warn = ctxt->warning; |
| 10614 | if (ctx != NULL) |
| 10615 | *ctx = ctxt->userData; |
| 10616 | return (0); |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 10617 | } |
| 10618 | |
| 10619 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10620 | * xmlRelaxNGValidateDoc: |
| 10621 | * @ctxt: a Relax-NG validation context |
| 10622 | * @doc: a parsed document tree |
| 10623 | * |
| 10624 | * Validate a document tree in memory. |
| 10625 | * |
| 10626 | * Returns 0 if the document is valid, a positive error code |
| 10627 | * number otherwise and -1 in case of internal or API error. |
| 10628 | */ |
| 10629 | int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10630 | xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) |
| 10631 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10632 | int ret; |
| 10633 | |
| 10634 | if ((ctxt == NULL) || (doc == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10635 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10636 | |
| 10637 | ctxt->doc = doc; |
| 10638 | |
| 10639 | ret = xmlRelaxNGValidateDocument(ctxt, doc); |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 10640 | /* |
| 10641 | * TODO: build error codes |
| 10642 | */ |
| 10643 | if (ret == -1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10644 | return (1); |
| 10645 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10646 | } |
| 10647 | |
| 10648 | #endif /* LIBXML_SCHEMAS_ENABLED */ |