Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1 | /* |
| 2 | * relaxng.c : implementation of the Relax-NG handling and validity checking |
| 3 | * |
| 4 | * See Copyright for the status of this software. |
| 5 | * |
| 6 | * Daniel Veillard <veillard@redhat.com> |
| 7 | */ |
| 8 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 9 | /** |
| 10 | * TODO: |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 11 | * - add support for DTD compatibility spec |
| 12 | * http://www.oasis-open.org/committees/relax-ng/compatibility-20011203.html |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 13 | * - report better mem allocations pbms at runtime and abort immediately. |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 16 | #define IN_LIBXML |
| 17 | #include "libxml.h" |
| 18 | |
| 19 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 20 | |
| 21 | #include <string.h> |
| 22 | #include <stdio.h> |
| 23 | #include <libxml/xmlmemory.h> |
| 24 | #include <libxml/parser.h> |
| 25 | #include <libxml/parserInternals.h> |
| 26 | #include <libxml/hash.h> |
| 27 | #include <libxml/uri.h> |
| 28 | |
| 29 | #include <libxml/relaxng.h> |
| 30 | |
| 31 | #include <libxml/xmlschemastypes.h> |
| 32 | #include <libxml/xmlautomata.h> |
| 33 | #include <libxml/xmlregexp.h> |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 34 | #include <libxml/xmlschemastypes.h> |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | * The Relax-NG namespace |
| 38 | */ |
| 39 | static const xmlChar *xmlRelaxNGNs = (const xmlChar *) |
| 40 | "http://relaxng.org/ns/structure/1.0"; |
| 41 | |
| 42 | #define IS_RELAXNG(node, type) \ |
| 43 | ((node != NULL) && (node->ns != NULL) && \ |
| 44 | (xmlStrEqual(node->name, (const xmlChar *) type)) && \ |
| 45 | (xmlStrEqual(node->ns->href, xmlRelaxNGNs))) |
| 46 | |
| 47 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 48 | /* #define DEBUG 1 */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 49 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 50 | /* #define DEBUG_GRAMMAR 1 */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 51 | |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 52 | /* #define DEBUG_CONTENT 1 */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 53 | |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 54 | /* #define DEBUG_TYPE 1 */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 55 | |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 56 | /* #define DEBUG_VALID 1 */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 57 | |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 58 | /* #define DEBUG_INTERLEAVE 1 */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 59 | |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 60 | /* #define DEBUG_LIST 1 */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 61 | |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 62 | /* #define DEBUG_INCLUDE */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 63 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 64 | /* #define DEBUG_ERROR 1 */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 65 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 66 | /* #define DEBUG_COMPILE 1 */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 67 | |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 68 | /* #define DEBUG_PROGRESSIVE 1 */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 69 | |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 70 | #define MAX_ERROR 5 |
| 71 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 72 | #define TODO \ |
| 73 | xmlGenericError(xmlGenericErrorContext, \ |
| 74 | "Unimplemented block at %s:%d\n", \ |
| 75 | __FILE__, __LINE__); |
| 76 | |
| 77 | typedef struct _xmlRelaxNGSchema xmlRelaxNGSchema; |
| 78 | typedef xmlRelaxNGSchema *xmlRelaxNGSchemaPtr; |
| 79 | |
| 80 | typedef struct _xmlRelaxNGDefine xmlRelaxNGDefine; |
| 81 | typedef xmlRelaxNGDefine *xmlRelaxNGDefinePtr; |
| 82 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 83 | typedef struct _xmlRelaxNGDocument xmlRelaxNGDocument; |
| 84 | typedef xmlRelaxNGDocument *xmlRelaxNGDocumentPtr; |
| 85 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 86 | typedef struct _xmlRelaxNGInclude xmlRelaxNGInclude; |
| 87 | typedef xmlRelaxNGInclude *xmlRelaxNGIncludePtr; |
| 88 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 89 | typedef enum { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 90 | XML_RELAXNG_COMBINE_UNDEFINED = 0, /* undefined */ |
| 91 | XML_RELAXNG_COMBINE_CHOICE, /* choice */ |
| 92 | XML_RELAXNG_COMBINE_INTERLEAVE /* interleave */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 93 | } xmlRelaxNGCombine; |
| 94 | |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 95 | typedef enum { |
| 96 | XML_RELAXNG_CONTENT_ERROR = -1, |
| 97 | XML_RELAXNG_CONTENT_EMPTY = 0, |
| 98 | XML_RELAXNG_CONTENT_SIMPLE, |
| 99 | XML_RELAXNG_CONTENT_COMPLEX |
| 100 | } xmlRelaxNGContentType; |
| 101 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 102 | typedef struct _xmlRelaxNGGrammar xmlRelaxNGGrammar; |
| 103 | typedef xmlRelaxNGGrammar *xmlRelaxNGGrammarPtr; |
| 104 | |
| 105 | struct _xmlRelaxNGGrammar { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 106 | xmlRelaxNGGrammarPtr parent; /* the parent grammar if any */ |
| 107 | xmlRelaxNGGrammarPtr children; /* the children grammar if any */ |
| 108 | xmlRelaxNGGrammarPtr next; /* the next grammar if any */ |
| 109 | xmlRelaxNGDefinePtr start; /* <start> content */ |
| 110 | xmlRelaxNGCombine combine; /* the default combine value */ |
| 111 | xmlRelaxNGDefinePtr startList; /* list of <start> definitions */ |
| 112 | xmlHashTablePtr defs; /* define* */ |
| 113 | xmlHashTablePtr refs; /* references */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 117 | typedef enum { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 118 | XML_RELAXNG_NOOP = -1, /* a no operation from simplification */ |
| 119 | XML_RELAXNG_EMPTY = 0, /* an empty pattern */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 120 | XML_RELAXNG_NOT_ALLOWED, /* not allowed top */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 121 | XML_RELAXNG_EXCEPT, /* except present in nameclass defs */ |
| 122 | XML_RELAXNG_TEXT, /* textual content */ |
| 123 | XML_RELAXNG_ELEMENT, /* an element */ |
| 124 | XML_RELAXNG_DATATYPE, /* extenal data type definition */ |
| 125 | XML_RELAXNG_PARAM, /* extenal data type parameter */ |
| 126 | XML_RELAXNG_VALUE, /* value from an extenal data type definition */ |
| 127 | XML_RELAXNG_LIST, /* a list of patterns */ |
| 128 | XML_RELAXNG_ATTRIBUTE, /* an attrbute following a pattern */ |
| 129 | XML_RELAXNG_DEF, /* a definition */ |
| 130 | XML_RELAXNG_REF, /* reference to a definition */ |
| 131 | XML_RELAXNG_EXTERNALREF, /* reference to an external def */ |
| 132 | XML_RELAXNG_PARENTREF, /* reference to a def in the parent grammar */ |
| 133 | XML_RELAXNG_OPTIONAL, /* optional patterns */ |
| 134 | XML_RELAXNG_ZEROORMORE, /* zero or more non empty patterns */ |
| 135 | XML_RELAXNG_ONEORMORE, /* one or more non empty patterns */ |
| 136 | XML_RELAXNG_CHOICE, /* a choice between non empty patterns */ |
| 137 | XML_RELAXNG_GROUP, /* a pair/group of non empty patterns */ |
| 138 | XML_RELAXNG_INTERLEAVE, /* interleaving choice of non-empty patterns */ |
| 139 | XML_RELAXNG_START /* Used to keep track of starts on grammars */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 140 | } xmlRelaxNGType; |
| 141 | |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 142 | #define IS_NULLABLE (1 << 0) |
| 143 | #define IS_NOT_NULLABLE (1 << 1) |
| 144 | #define IS_INDETERMINIST (1 << 2) |
| 145 | #define IS_MIXED (1 << 3) |
| 146 | #define IS_TRIABLE (1 << 4) |
| 147 | #define IS_PROCESSED (1 << 5) |
| 148 | #define IS_COMPILABLE (1 << 6) |
| 149 | #define IS_NOT_COMPILABLE (1 << 7) |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 150 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 151 | struct _xmlRelaxNGDefine { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 152 | xmlRelaxNGType type; /* the type of definition */ |
| 153 | xmlNodePtr node; /* the node in the source */ |
| 154 | xmlChar *name; /* the element local name if present */ |
| 155 | xmlChar *ns; /* the namespace local name if present */ |
| 156 | xmlChar *value; /* value when available */ |
| 157 | void *data; /* data lib or specific pointer */ |
| 158 | xmlRelaxNGDefinePtr content; /* the expected content */ |
| 159 | xmlRelaxNGDefinePtr parent; /* the parent definition, if any */ |
| 160 | xmlRelaxNGDefinePtr next; /* list within grouping sequences */ |
| 161 | xmlRelaxNGDefinePtr attrs; /* list of attributes for elements */ |
| 162 | xmlRelaxNGDefinePtr nameClass; /* the nameClass definition if any */ |
| 163 | xmlRelaxNGDefinePtr nextHash; /* next define in defs/refs hash tables */ |
| 164 | short depth; /* used for the cycle detection */ |
| 165 | short dflags; /* define related flags */ |
| 166 | xmlRegexpPtr contModel; /* a compiled content model if available */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | /** |
| 170 | * _xmlRelaxNG: |
| 171 | * |
| 172 | * A RelaxNGs definition |
| 173 | */ |
| 174 | struct _xmlRelaxNG { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 175 | void *_private; /* unused by the library for users or bindings */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 176 | xmlRelaxNGGrammarPtr topgrammar; |
| 177 | xmlDocPtr doc; |
| 178 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 179 | int idref; /* requires idref checking */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 180 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 181 | xmlHashTablePtr defs; /* define */ |
| 182 | xmlHashTablePtr refs; /* references */ |
| 183 | xmlRelaxNGDocumentPtr documents; /* all the documents loaded */ |
| 184 | xmlRelaxNGIncludePtr includes; /* all the includes loaded */ |
| 185 | int defNr; /* number of defines used */ |
| 186 | xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 187 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 188 | }; |
| 189 | |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 190 | #define XML_RELAXNG_IN_ATTRIBUTE (1 << 0) |
| 191 | #define XML_RELAXNG_IN_ONEORMORE (1 << 1) |
| 192 | #define XML_RELAXNG_IN_LIST (1 << 2) |
| 193 | #define XML_RELAXNG_IN_DATAEXCEPT (1 << 3) |
| 194 | #define XML_RELAXNG_IN_START (1 << 4) |
| 195 | #define XML_RELAXNG_IN_OOMGROUP (1 << 5) |
| 196 | #define XML_RELAXNG_IN_OOMINTERLEAVE (1 << 6) |
| 197 | #define XML_RELAXNG_IN_EXTERNALREF (1 << 7) |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 198 | #define XML_RELAXNG_IN_ANYEXCEPT (1 << 8) |
| 199 | #define XML_RELAXNG_IN_NSEXCEPT (1 << 9) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 200 | |
| 201 | struct _xmlRelaxNGParserCtxt { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 202 | void *userData; /* user specific data block */ |
| 203 | xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */ |
| 204 | xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */ |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 205 | xmlStructuredErrorFunc serror; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 206 | xmlRelaxNGValidErr err; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 207 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 208 | xmlRelaxNGPtr schema; /* The schema in use */ |
| 209 | xmlRelaxNGGrammarPtr grammar; /* the current grammar */ |
| 210 | xmlRelaxNGGrammarPtr parentgrammar; /* the parent grammar */ |
| 211 | int flags; /* parser flags */ |
| 212 | int nbErrors; /* number of errors at parse time */ |
| 213 | int nbWarnings; /* number of warnings at parse time */ |
| 214 | const xmlChar *define; /* the current define scope */ |
| 215 | xmlRelaxNGDefinePtr def; /* the current define */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 216 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 217 | int nbInterleaves; |
| 218 | xmlHashTablePtr interleaves; /* keep track of all the interleaves */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 219 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 220 | xmlRelaxNGDocumentPtr documents; /* all the documents loaded */ |
| 221 | xmlRelaxNGIncludePtr includes; /* all the includes loaded */ |
| 222 | xmlChar *URL; |
| 223 | xmlDocPtr document; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 224 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 225 | int defNr; /* number of defines used */ |
| 226 | int defMax; /* number of defines aloocated */ |
| 227 | xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */ |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 228 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 229 | const char *buffer; |
| 230 | int size; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 231 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 232 | /* the document stack */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 233 | xmlRelaxNGDocumentPtr doc; /* Current parsed external ref */ |
| 234 | int docNr; /* Depth of the parsing stack */ |
| 235 | int docMax; /* Max depth of the parsing stack */ |
| 236 | xmlRelaxNGDocumentPtr *docTab; /* array of docs */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 237 | |
| 238 | /* the include stack */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 239 | xmlRelaxNGIncludePtr inc; /* Current parsed include */ |
| 240 | int incNr; /* Depth of the include parsing stack */ |
| 241 | int incMax; /* Max depth of the parsing stack */ |
| 242 | xmlRelaxNGIncludePtr *incTab; /* array of incs */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 243 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 244 | int idref; /* requires idref checking */ |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 245 | |
| 246 | /* used to compile content models */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 247 | xmlAutomataPtr am; /* the automata */ |
| 248 | xmlAutomataStatePtr state; /* used to build the automata */ |
Daniel Veillard | 03c2f0a | 2004-01-25 19:54:59 +0000 | [diff] [blame] | 249 | |
| 250 | int crng; /* compact syntax and other flags */ |
Daniel Veillard | 4259532 | 2004-11-08 10:52:06 +0000 | [diff] [blame] | 251 | int freedoc; /* need to free the document */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 252 | }; |
| 253 | |
| 254 | #define FLAGS_IGNORABLE 1 |
| 255 | #define FLAGS_NEGATIVE 2 |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 256 | #define FLAGS_MIXED_CONTENT 4 |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 257 | #define FLAGS_NOERROR 8 |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 258 | |
| 259 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 260 | * xmlRelaxNGInterleaveGroup: |
| 261 | * |
| 262 | * A RelaxNGs partition set associated to lists of definitions |
| 263 | */ |
| 264 | typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup; |
| 265 | typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr; |
| 266 | struct _xmlRelaxNGInterleaveGroup { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 267 | xmlRelaxNGDefinePtr rule; /* the rule to satisfy */ |
| 268 | xmlRelaxNGDefinePtr *defs; /* the array of element definitions */ |
| 269 | xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 270 | }; |
| 271 | |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 272 | #define IS_DETERMINIST 1 |
| 273 | #define IS_NEEDCHECK 2 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 274 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 275 | /** |
| 276 | * xmlRelaxNGPartitions: |
| 277 | * |
| 278 | * A RelaxNGs partition associated to an interleave group |
| 279 | */ |
| 280 | typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition; |
| 281 | typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr; |
| 282 | struct _xmlRelaxNGPartition { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 283 | int nbgroups; /* number of groups in the partitions */ |
| 284 | xmlHashTablePtr triage; /* hash table used to direct nodes to the |
| 285 | * right group when possible */ |
| 286 | int flags; /* determinist ? */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 287 | xmlRelaxNGInterleaveGroupPtr *groups; |
| 288 | }; |
| 289 | |
| 290 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 291 | * xmlRelaxNGValidState: |
| 292 | * |
| 293 | * A RelaxNGs validation state |
| 294 | */ |
| 295 | #define MAX_ATTR 20 |
| 296 | typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState; |
| 297 | typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr; |
| 298 | struct _xmlRelaxNGValidState { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 299 | xmlNodePtr node; /* the current node */ |
| 300 | xmlNodePtr seq; /* the sequence of children left to validate */ |
| 301 | int nbAttrs; /* the number of attributes */ |
| 302 | int maxAttrs; /* the size of attrs */ |
| 303 | int nbAttrLeft; /* the number of attributes left to validate */ |
| 304 | xmlChar *value; /* the value when operating on string */ |
| 305 | xmlChar *endvalue; /* the end value when operating on string */ |
| 306 | xmlAttrPtr *attrs; /* the array of attributes */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 307 | }; |
| 308 | |
| 309 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 310 | * xmlRelaxNGStates: |
| 311 | * |
| 312 | * A RelaxNGs container for validation state |
| 313 | */ |
| 314 | typedef struct _xmlRelaxNGStates xmlRelaxNGStates; |
| 315 | typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr; |
| 316 | struct _xmlRelaxNGStates { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 317 | int nbState; /* the number of states */ |
| 318 | int maxState; /* the size of the array */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 319 | xmlRelaxNGValidStatePtr *tabState; |
| 320 | }; |
| 321 | |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 322 | #define ERROR_IS_DUP 1 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 323 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 324 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 325 | * xmlRelaxNGValidError: |
| 326 | * |
| 327 | * A RelaxNGs validation error |
| 328 | */ |
| 329 | typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError; |
| 330 | typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr; |
| 331 | struct _xmlRelaxNGValidError { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 332 | xmlRelaxNGValidErr err; /* the error number */ |
| 333 | int flags; /* flags */ |
| 334 | xmlNodePtr node; /* the current node */ |
| 335 | xmlNodePtr seq; /* the current child */ |
| 336 | const xmlChar *arg1; /* first arg */ |
| 337 | const xmlChar *arg2; /* second arg */ |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 338 | }; |
| 339 | |
| 340 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 341 | * xmlRelaxNGValidCtxt: |
| 342 | * |
| 343 | * A RelaxNGs validation context |
| 344 | */ |
| 345 | |
| 346 | struct _xmlRelaxNGValidCtxt { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 347 | void *userData; /* user specific data block */ |
| 348 | xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */ |
| 349 | xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */ |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 350 | xmlStructuredErrorFunc serror; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 351 | int nbErrors; /* number of errors in validation */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 352 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 353 | xmlRelaxNGPtr schema; /* The schema in use */ |
| 354 | xmlDocPtr doc; /* the document being validated */ |
| 355 | int flags; /* validation flags */ |
| 356 | int depth; /* validation depth */ |
| 357 | int idref; /* requires idref checking */ |
| 358 | int errNo; /* the first error found */ |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 359 | |
| 360 | /* |
| 361 | * Errors accumulated in branches may have to be stacked to be |
| 362 | * provided back when it's sure they affect validation. |
| 363 | */ |
| 364 | xmlRelaxNGValidErrorPtr err; /* Last error */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 365 | int errNr; /* Depth of the error stack */ |
| 366 | int errMax; /* Max depth of the error stack */ |
| 367 | xmlRelaxNGValidErrorPtr errTab; /* stack of errors */ |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 368 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 369 | xmlRelaxNGValidStatePtr state; /* the current validation state */ |
| 370 | xmlRelaxNGStatesPtr states; /* the accumulated state list */ |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 371 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 372 | xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */ |
| 373 | int freeStatesNr; |
| 374 | int freeStatesMax; |
| 375 | xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */ |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 376 | |
| 377 | /* |
| 378 | * This is used for "progressive" validation |
| 379 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 380 | xmlRegExecCtxtPtr elem; /* the current element regexp */ |
| 381 | int elemNr; /* the number of element validated */ |
| 382 | int elemMax; /* the max depth of elements */ |
| 383 | xmlRegExecCtxtPtr *elemTab; /* the stack of regexp runtime */ |
| 384 | int pstate; /* progressive state */ |
| 385 | xmlNodePtr pnode; /* the current node */ |
| 386 | xmlRelaxNGDefinePtr pdef; /* the non-streamable definition */ |
| 387 | int perr; /* signal error in content model |
| 388 | * outside the regexp */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 389 | }; |
| 390 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 391 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 392 | * xmlRelaxNGInclude: |
| 393 | * |
| 394 | * Structure associated to a RelaxNGs document element |
| 395 | */ |
| 396 | struct _xmlRelaxNGInclude { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 397 | xmlRelaxNGIncludePtr next; /* keep a chain of includes */ |
| 398 | xmlChar *href; /* the normalized href value */ |
| 399 | xmlDocPtr doc; /* the associated XML document */ |
| 400 | xmlRelaxNGDefinePtr content; /* the definitions */ |
| 401 | xmlRelaxNGPtr schema; /* the schema */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 402 | }; |
| 403 | |
| 404 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 405 | * xmlRelaxNGDocument: |
| 406 | * |
| 407 | * Structure associated to a RelaxNGs document element |
| 408 | */ |
| 409 | struct _xmlRelaxNGDocument { |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 410 | xmlRelaxNGDocumentPtr next; /* keep a chain of documents */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 411 | xmlChar *href; /* the normalized href value */ |
| 412 | xmlDocPtr doc; /* the associated XML document */ |
| 413 | xmlRelaxNGDefinePtr content; /* the definitions */ |
| 414 | xmlRelaxNGPtr schema; /* the schema */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 415 | }; |
| 416 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 417 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 418 | /************************************************************************ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 419 | * * |
| 420 | * Some factorized error routines * |
| 421 | * * |
| 422 | ************************************************************************/ |
| 423 | |
| 424 | /** |
| 425 | * xmlRngPErrMemory: |
| 426 | * @ctxt: an Relax-NG parser context |
| 427 | * @extra: extra informations |
| 428 | * |
| 429 | * Handle a redefinition of attribute error |
| 430 | */ |
| 431 | static void |
| 432 | xmlRngPErrMemory(xmlRelaxNGParserCtxtPtr ctxt, const char *extra) |
| 433 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 434 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 435 | xmlGenericErrorFunc channel = NULL; |
| 436 | void *data = NULL; |
| 437 | |
| 438 | if (ctxt != NULL) { |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 439 | if (ctxt->serror != NULL) |
| 440 | schannel = ctxt->serror; |
| 441 | else |
| 442 | channel = ctxt->error; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 443 | data = ctxt->userData; |
| 444 | ctxt->nbErrors++; |
| 445 | } |
| 446 | if (extra) |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 447 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 448 | NULL, NULL, XML_FROM_RELAXNGP, |
| 449 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra, |
| 450 | NULL, NULL, 0, 0, |
| 451 | "Memory allocation failed : %s\n", extra); |
| 452 | else |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 453 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 454 | NULL, NULL, XML_FROM_RELAXNGP, |
| 455 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL, |
| 456 | NULL, NULL, 0, 0, "Memory allocation failed\n"); |
| 457 | } |
| 458 | |
| 459 | /** |
| 460 | * xmlRngVErrMemory: |
| 461 | * @ctxt: a Relax-NG validation context |
| 462 | * @extra: extra informations |
| 463 | * |
| 464 | * Handle a redefinition of attribute error |
| 465 | */ |
| 466 | static void |
| 467 | xmlRngVErrMemory(xmlRelaxNGValidCtxtPtr ctxt, const char *extra) |
| 468 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 469 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 470 | xmlGenericErrorFunc channel = NULL; |
| 471 | void *data = NULL; |
| 472 | |
| 473 | if (ctxt != NULL) { |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 474 | if (ctxt->serror != NULL) |
| 475 | schannel = ctxt->serror; |
| 476 | else |
| 477 | channel = ctxt->error; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 478 | data = ctxt->userData; |
| 479 | ctxt->nbErrors++; |
| 480 | } |
| 481 | if (extra) |
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, extra, |
| 485 | NULL, NULL, 0, 0, |
| 486 | "Memory allocation failed : %s\n", extra); |
| 487 | else |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 488 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 489 | NULL, NULL, XML_FROM_RELAXNGV, |
| 490 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL, |
| 491 | NULL, NULL, 0, 0, "Memory allocation failed\n"); |
| 492 | } |
| 493 | |
| 494 | /** |
| 495 | * xmlRngPErr: |
| 496 | * @ctxt: a Relax-NG parser context |
| 497 | * @node: the node raising the error |
| 498 | * @error: the error code |
| 499 | * @msg: message |
| 500 | * @str1: extra info |
| 501 | * @str2: extra info |
| 502 | * |
| 503 | * Handle a Relax NG Parsing error |
| 504 | */ |
| 505 | static void |
| 506 | xmlRngPErr(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, int error, |
| 507 | const char *msg, const xmlChar * str1, const xmlChar * str2) |
| 508 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 509 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 510 | xmlGenericErrorFunc channel = NULL; |
| 511 | void *data = NULL; |
| 512 | |
| 513 | if (ctxt != NULL) { |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 514 | if (ctxt->serror != NULL) |
| 515 | schannel = ctxt->serror; |
| 516 | else |
| 517 | channel = ctxt->error; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 518 | data = ctxt->userData; |
| 519 | ctxt->nbErrors++; |
| 520 | } |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 521 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 522 | NULL, node, XML_FROM_RELAXNGP, |
| 523 | error, XML_ERR_ERROR, NULL, 0, |
| 524 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
| 525 | msg, str1, str2); |
| 526 | } |
| 527 | |
| 528 | /** |
| 529 | * xmlRngVErr: |
| 530 | * @ctxt: a Relax-NG validation context |
| 531 | * @node: the node raising the error |
| 532 | * @error: the error code |
| 533 | * @msg: message |
| 534 | * @str1: extra info |
| 535 | * @str2: extra info |
| 536 | * |
| 537 | * Handle a Relax NG Validation error |
| 538 | */ |
| 539 | static void |
| 540 | xmlRngVErr(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node, int error, |
| 541 | const char *msg, const xmlChar * str1, const xmlChar * str2) |
| 542 | { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 543 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 544 | xmlGenericErrorFunc channel = NULL; |
| 545 | void *data = NULL; |
| 546 | |
| 547 | if (ctxt != NULL) { |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 548 | if (ctxt->serror != NULL) |
| 549 | schannel = ctxt->serror; |
| 550 | else |
| 551 | channel = ctxt->error; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 552 | data = ctxt->userData; |
| 553 | ctxt->nbErrors++; |
| 554 | } |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 555 | __xmlRaiseError(schannel, channel, data, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 556 | NULL, node, XML_FROM_RELAXNGV, |
| 557 | error, XML_ERR_ERROR, NULL, 0, |
| 558 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
| 559 | msg, str1, str2); |
| 560 | } |
| 561 | |
| 562 | /************************************************************************ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 563 | * * |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 564 | * Preliminary type checking interfaces * |
| 565 | * * |
| 566 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 567 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 568 | /** |
| 569 | * xmlRelaxNGTypeHave: |
| 570 | * @data: data needed for the library |
| 571 | * @type: the type name |
| 572 | * @value: the value to check |
| 573 | * |
| 574 | * Function provided by a type library to check if a type is exported |
| 575 | * |
| 576 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 577 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 578 | typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar * type); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 579 | |
| 580 | /** |
| 581 | * xmlRelaxNGTypeCheck: |
| 582 | * @data: data needed for the library |
| 583 | * @type: the type name |
| 584 | * @value: the value to check |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 585 | * @result: place to store the result if needed |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 586 | * |
| 587 | * Function provided by a type library to check if a value match a type |
| 588 | * |
| 589 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 590 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 591 | typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar * type, |
| 592 | const xmlChar * value, void **result, |
| 593 | xmlNodePtr node); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 594 | |
| 595 | /** |
| 596 | * xmlRelaxNGFacetCheck: |
| 597 | * @data: data needed for the library |
| 598 | * @type: the type name |
| 599 | * @facet: the facet name |
| 600 | * @val: the facet value |
| 601 | * @strval: the string value |
| 602 | * @value: the value to check |
| 603 | * |
| 604 | * Function provided by a type library to check a value facet |
| 605 | * |
| 606 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 607 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 608 | typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar * type, |
| 609 | const xmlChar * facet, |
| 610 | const xmlChar * val, |
| 611 | const xmlChar * strval, void *value); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 612 | |
| 613 | /** |
| 614 | * xmlRelaxNGTypeFree: |
| 615 | * @data: data needed for the library |
| 616 | * @result: the value to free |
| 617 | * |
| 618 | * Function provided by a type library to free a returned result |
| 619 | */ |
| 620 | typedef void (*xmlRelaxNGTypeFree) (void *data, void *result); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 621 | |
| 622 | /** |
| 623 | * xmlRelaxNGTypeCompare: |
| 624 | * @data: data needed for the library |
| 625 | * @type: the type name |
| 626 | * @value1: the first value |
| 627 | * @value2: the second value |
| 628 | * |
| 629 | * Function provided by a type library to compare two values accordingly |
| 630 | * to a type. |
| 631 | * |
| 632 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 633 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 634 | typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar * type, |
| 635 | const xmlChar * value1, |
| 636 | xmlNodePtr ctxt1, |
| 637 | void *comp1, |
| 638 | const xmlChar * value2, |
| 639 | xmlNodePtr ctxt2); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 640 | typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary; |
| 641 | typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr; |
| 642 | struct _xmlRelaxNGTypeLibrary { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 643 | const xmlChar *namespace; /* the datatypeLibrary value */ |
| 644 | void *data; /* data needed for the library */ |
| 645 | xmlRelaxNGTypeHave have; /* the export function */ |
| 646 | xmlRelaxNGTypeCheck check; /* the checking function */ |
| 647 | xmlRelaxNGTypeCompare comp; /* the compare function */ |
| 648 | xmlRelaxNGFacetCheck facet; /* the facet check function */ |
| 649 | xmlRelaxNGTypeFree freef; /* the freeing function */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 650 | }; |
| 651 | |
| 652 | /************************************************************************ |
| 653 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 654 | * Allocation functions * |
| 655 | * * |
| 656 | ************************************************************************/ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 657 | static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar); |
| 658 | static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 659 | static void xmlRelaxNGNormExtSpace(xmlChar * value); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 660 | static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 661 | static int xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt |
| 662 | ATTRIBUTE_UNUSED, |
| 663 | xmlRelaxNGValidStatePtr state1, |
| 664 | xmlRelaxNGValidStatePtr state2); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 665 | static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 666 | xmlRelaxNGValidStatePtr state); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 667 | |
| 668 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 669 | * xmlRelaxNGFreeDocument: |
| 670 | * @docu: a document structure |
| 671 | * |
| 672 | * Deallocate a RelaxNG document structure. |
| 673 | */ |
| 674 | static void |
| 675 | xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu) |
| 676 | { |
| 677 | if (docu == NULL) |
| 678 | return; |
| 679 | |
| 680 | if (docu->href != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 681 | xmlFree(docu->href); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 682 | if (docu->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 683 | xmlFreeDoc(docu->doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 684 | if (docu->schema != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 685 | xmlRelaxNGFreeInnerSchema(docu->schema); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 686 | xmlFree(docu); |
| 687 | } |
| 688 | |
| 689 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 690 | * xmlRelaxNGFreeDocumentList: |
| 691 | * @docu: a list of document structure |
| 692 | * |
| 693 | * Deallocate a RelaxNG document structures. |
| 694 | */ |
| 695 | static void |
| 696 | xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu) |
| 697 | { |
| 698 | xmlRelaxNGDocumentPtr next; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 699 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 700 | while (docu != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 701 | next = docu->next; |
| 702 | xmlRelaxNGFreeDocument(docu); |
| 703 | docu = next; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 704 | } |
| 705 | } |
| 706 | |
| 707 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 708 | * xmlRelaxNGFreeInclude: |
| 709 | * @incl: a include structure |
| 710 | * |
| 711 | * Deallocate a RelaxNG include structure. |
| 712 | */ |
| 713 | static void |
| 714 | xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl) |
| 715 | { |
| 716 | if (incl == NULL) |
| 717 | return; |
| 718 | |
| 719 | if (incl->href != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 720 | xmlFree(incl->href); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 721 | if (incl->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 722 | xmlFreeDoc(incl->doc); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 723 | if (incl->schema != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 724 | xmlRelaxNGFree(incl->schema); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 725 | xmlFree(incl); |
| 726 | } |
| 727 | |
| 728 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 729 | * xmlRelaxNGFreeIncludeList: |
| 730 | * @incl: a include structure list |
| 731 | * |
| 732 | * Deallocate a RelaxNG include structure. |
| 733 | */ |
| 734 | static void |
| 735 | xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl) |
| 736 | { |
| 737 | xmlRelaxNGIncludePtr next; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 738 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 739 | while (incl != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 740 | next = incl->next; |
| 741 | xmlRelaxNGFreeInclude(incl); |
| 742 | incl = next; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 743 | } |
| 744 | } |
| 745 | |
| 746 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 747 | * xmlRelaxNGNewRelaxNG: |
| 748 | * @ctxt: a Relax-NG validation context (optional) |
| 749 | * |
| 750 | * Allocate a new RelaxNG structure. |
| 751 | * |
| 752 | * Returns the newly allocated structure or NULL in case or error |
| 753 | */ |
| 754 | static xmlRelaxNGPtr |
| 755 | xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt) |
| 756 | { |
| 757 | xmlRelaxNGPtr ret; |
| 758 | |
| 759 | ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG)); |
| 760 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 761 | xmlRngPErrMemory(ctxt, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 762 | return (NULL); |
| 763 | } |
| 764 | memset(ret, 0, sizeof(xmlRelaxNG)); |
| 765 | |
| 766 | return (ret); |
| 767 | } |
| 768 | |
| 769 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 770 | * xmlRelaxNGFreeInnerSchema: |
| 771 | * @schema: a schema structure |
| 772 | * |
| 773 | * Deallocate a RelaxNG schema structure. |
| 774 | */ |
| 775 | static void |
| 776 | xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema) |
| 777 | { |
| 778 | if (schema == NULL) |
| 779 | return; |
| 780 | |
| 781 | if (schema->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 782 | xmlFreeDoc(schema->doc); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 783 | if (schema->defTab != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 784 | int i; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 785 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 786 | for (i = 0; i < schema->defNr; i++) |
| 787 | xmlRelaxNGFreeDefine(schema->defTab[i]); |
| 788 | xmlFree(schema->defTab); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | xmlFree(schema); |
| 792 | } |
| 793 | |
| 794 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 795 | * xmlRelaxNGFree: |
| 796 | * @schema: a schema structure |
| 797 | * |
| 798 | * Deallocate a RelaxNG structure. |
| 799 | */ |
| 800 | void |
| 801 | xmlRelaxNGFree(xmlRelaxNGPtr schema) |
| 802 | { |
| 803 | if (schema == NULL) |
| 804 | return; |
| 805 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 806 | if (schema->topgrammar != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 807 | xmlRelaxNGFreeGrammar(schema->topgrammar); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 808 | if (schema->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 809 | xmlFreeDoc(schema->doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 810 | if (schema->documents != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 811 | xmlRelaxNGFreeDocumentList(schema->documents); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 812 | if (schema->includes != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 813 | xmlRelaxNGFreeIncludeList(schema->includes); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 814 | if (schema->defTab != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 815 | int i; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 816 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 817 | for (i = 0; i < schema->defNr; i++) |
| 818 | xmlRelaxNGFreeDefine(schema->defTab[i]); |
| 819 | xmlFree(schema->defTab); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 820 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 821 | |
| 822 | xmlFree(schema); |
| 823 | } |
| 824 | |
| 825 | /** |
| 826 | * xmlRelaxNGNewGrammar: |
| 827 | * @ctxt: a Relax-NG validation context (optional) |
| 828 | * |
| 829 | * Allocate a new RelaxNG grammar. |
| 830 | * |
| 831 | * Returns the newly allocated structure or NULL in case or error |
| 832 | */ |
| 833 | static xmlRelaxNGGrammarPtr |
| 834 | xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt) |
| 835 | { |
| 836 | xmlRelaxNGGrammarPtr ret; |
| 837 | |
| 838 | ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar)); |
| 839 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 840 | xmlRngPErrMemory(ctxt, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 841 | return (NULL); |
| 842 | } |
| 843 | memset(ret, 0, sizeof(xmlRelaxNGGrammar)); |
| 844 | |
| 845 | return (ret); |
| 846 | } |
| 847 | |
| 848 | /** |
| 849 | * xmlRelaxNGFreeGrammar: |
| 850 | * @grammar: a grammar structure |
| 851 | * |
| 852 | * Deallocate a RelaxNG grammar structure. |
| 853 | */ |
| 854 | static void |
| 855 | xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar) |
| 856 | { |
| 857 | if (grammar == NULL) |
| 858 | return; |
| 859 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 860 | if (grammar->children != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 861 | xmlRelaxNGFreeGrammar(grammar->children); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 862 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 863 | if (grammar->next != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 864 | xmlRelaxNGFreeGrammar(grammar->next); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 865 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 866 | if (grammar->refs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 867 | xmlHashFree(grammar->refs, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 868 | } |
| 869 | if (grammar->defs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 870 | xmlHashFree(grammar->defs, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | xmlFree(grammar); |
| 874 | } |
| 875 | |
| 876 | /** |
| 877 | * xmlRelaxNGNewDefine: |
| 878 | * @ctxt: a Relax-NG validation context |
| 879 | * @node: the node in the input document. |
| 880 | * |
| 881 | * Allocate a new RelaxNG define. |
| 882 | * |
| 883 | * Returns the newly allocated structure or NULL in case or error |
| 884 | */ |
| 885 | static xmlRelaxNGDefinePtr |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 886 | xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 887 | { |
| 888 | xmlRelaxNGDefinePtr ret; |
| 889 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 890 | if (ctxt->defMax == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 891 | ctxt->defMax = 16; |
| 892 | ctxt->defNr = 0; |
| 893 | ctxt->defTab = (xmlRelaxNGDefinePtr *) |
| 894 | xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr)); |
| 895 | if (ctxt->defTab == NULL) { |
| 896 | xmlRngPErrMemory(ctxt, "allocating define\n"); |
| 897 | return (NULL); |
| 898 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 899 | } else if (ctxt->defMax <= ctxt->defNr) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 900 | xmlRelaxNGDefinePtr *tmp; |
| 901 | |
| 902 | ctxt->defMax *= 2; |
| 903 | tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab, |
| 904 | ctxt->defMax * |
| 905 | sizeof |
| 906 | (xmlRelaxNGDefinePtr)); |
| 907 | if (tmp == NULL) { |
| 908 | xmlRngPErrMemory(ctxt, "allocating define\n"); |
| 909 | return (NULL); |
| 910 | } |
| 911 | ctxt->defTab = tmp; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 912 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 913 | ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine)); |
| 914 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 915 | xmlRngPErrMemory(ctxt, "allocating define\n"); |
| 916 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 917 | } |
| 918 | memset(ret, 0, sizeof(xmlRelaxNGDefine)); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 919 | ctxt->defTab[ctxt->defNr++] = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 920 | ret->node = node; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 921 | ret->depth = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 922 | return (ret); |
| 923 | } |
| 924 | |
| 925 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 926 | * xmlRelaxNGFreePartition: |
| 927 | * @partitions: a partition set structure |
| 928 | * |
| 929 | * Deallocate RelaxNG partition set structures. |
| 930 | */ |
| 931 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 932 | xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions) |
| 933 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 934 | xmlRelaxNGInterleaveGroupPtr group; |
| 935 | int j; |
| 936 | |
| 937 | if (partitions != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 938 | if (partitions->groups != NULL) { |
| 939 | for (j = 0; j < partitions->nbgroups; j++) { |
| 940 | group = partitions->groups[j]; |
| 941 | if (group != NULL) { |
| 942 | if (group->defs != NULL) |
| 943 | xmlFree(group->defs); |
| 944 | if (group->attrs != NULL) |
| 945 | xmlFree(group->attrs); |
| 946 | xmlFree(group); |
| 947 | } |
| 948 | } |
| 949 | xmlFree(partitions->groups); |
| 950 | } |
| 951 | if (partitions->triage != NULL) { |
| 952 | xmlHashFree(partitions->triage, NULL); |
| 953 | } |
| 954 | xmlFree(partitions); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 955 | } |
| 956 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 957 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 958 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 959 | * xmlRelaxNGFreeDefine: |
| 960 | * @define: a define structure |
| 961 | * |
| 962 | * Deallocate a RelaxNG define structure. |
| 963 | */ |
| 964 | static void |
| 965 | xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define) |
| 966 | { |
| 967 | if (define == NULL) |
| 968 | return; |
| 969 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 970 | if ((define->type == XML_RELAXNG_VALUE) && (define->attrs != NULL)) { |
| 971 | xmlRelaxNGTypeLibraryPtr lib; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 972 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 973 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
| 974 | if ((lib != NULL) && (lib->freef != NULL)) |
| 975 | lib->freef(lib->data, (void *) define->attrs); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 976 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 977 | if ((define->data != NULL) && (define->type == XML_RELAXNG_INTERLEAVE)) |
| 978 | xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data); |
| 979 | if ((define->data != NULL) && (define->type == XML_RELAXNG_CHOICE)) |
| 980 | xmlHashFree((xmlHashTablePtr) define->data, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 981 | if (define->name != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 982 | xmlFree(define->name); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 983 | if (define->ns != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 984 | xmlFree(define->ns); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 985 | if (define->value != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 986 | xmlFree(define->value); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 987 | if (define->contModel != NULL) |
| 988 | xmlRegFreeRegexp(define->contModel); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 989 | xmlFree(define); |
| 990 | } |
| 991 | |
| 992 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 993 | * xmlRelaxNGNewStates: |
| 994 | * @ctxt: a Relax-NG validation context |
| 995 | * @size: the default size for the container |
| 996 | * |
| 997 | * Allocate a new RelaxNG validation state container |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 998 | * |
| 999 | * Returns the newly allocated structure or NULL in case or error |
| 1000 | */ |
| 1001 | static xmlRelaxNGStatesPtr |
| 1002 | xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size) |
| 1003 | { |
| 1004 | xmlRelaxNGStatesPtr ret; |
| 1005 | |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1006 | if ((ctxt != NULL) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1007 | (ctxt->freeState != NULL) && (ctxt->freeStatesNr > 0)) { |
| 1008 | ctxt->freeStatesNr--; |
| 1009 | ret = ctxt->freeStates[ctxt->freeStatesNr]; |
| 1010 | ret->nbState = 0; |
| 1011 | return (ret); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1012 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1013 | if (size < 16) |
| 1014 | size = 16; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1015 | |
| 1016 | ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) + |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1017 | (size - |
| 1018 | 1) * |
| 1019 | sizeof(xmlRelaxNGValidStatePtr)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1020 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1021 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1022 | return (NULL); |
| 1023 | } |
| 1024 | ret->nbState = 0; |
| 1025 | ret->maxState = size; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1026 | ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc((size) * |
| 1027 | sizeof |
| 1028 | (xmlRelaxNGValidStatePtr)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1029 | if (ret->tabState == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1030 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1031 | xmlFree(ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1032 | return (NULL); |
| 1033 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1034 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | /** |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1038 | * xmlRelaxNGAddStateUniq: |
| 1039 | * @ctxt: a Relax-NG validation context |
| 1040 | * @states: the states container |
| 1041 | * @state: the validation state |
| 1042 | * |
| 1043 | * Add a RelaxNG validation state to the container without checking |
| 1044 | * for unicity. |
| 1045 | * |
| 1046 | * Return 1 in case of success and 0 if this is a duplicate and -1 on error |
| 1047 | */ |
| 1048 | static int |
| 1049 | xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1050 | xmlRelaxNGStatesPtr states, |
| 1051 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1052 | { |
| 1053 | if (state == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1054 | return (-1); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1055 | } |
| 1056 | if (states->nbState >= states->maxState) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1057 | xmlRelaxNGValidStatePtr *tmp; |
| 1058 | int size; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1059 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1060 | size = states->maxState * 2; |
| 1061 | tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState, |
| 1062 | (size) * |
| 1063 | sizeof |
| 1064 | (xmlRelaxNGValidStatePtr)); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1065 | if (tmp == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1066 | xmlRngVErrMemory(ctxt, "adding states\n"); |
| 1067 | return (-1); |
| 1068 | } |
| 1069 | states->tabState = tmp; |
| 1070 | states->maxState = size; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1071 | } |
| 1072 | states->tabState[states->nbState++] = state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1073 | return (1); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1077 | * xmlRelaxNGAddState: |
| 1078 | * @ctxt: a Relax-NG validation context |
| 1079 | * @states: the states container |
| 1080 | * @state: the validation state |
| 1081 | * |
| 1082 | * Add a RelaxNG validation state to the container |
| 1083 | * |
| 1084 | * Return 1 in case of success and 0 if this is a duplicate and -1 on error |
| 1085 | */ |
| 1086 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1087 | xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt, |
| 1088 | xmlRelaxNGStatesPtr states, |
| 1089 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1090 | { |
| 1091 | int i; |
| 1092 | |
| 1093 | if (state == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1094 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1095 | } |
| 1096 | if (states->nbState >= states->maxState) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1097 | xmlRelaxNGValidStatePtr *tmp; |
| 1098 | int size; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1099 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1100 | size = states->maxState * 2; |
| 1101 | tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState, |
| 1102 | (size) * |
| 1103 | sizeof |
| 1104 | (xmlRelaxNGValidStatePtr)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1105 | if (tmp == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1106 | xmlRngVErrMemory(ctxt, "adding states\n"); |
| 1107 | return (-1); |
| 1108 | } |
| 1109 | states->tabState = tmp; |
| 1110 | states->maxState = size; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1111 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1112 | for (i = 0; i < states->nbState; i++) { |
| 1113 | if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) { |
| 1114 | xmlRelaxNGFreeValidState(ctxt, state); |
| 1115 | return (0); |
| 1116 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1117 | } |
| 1118 | states->tabState[states->nbState++] = state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1119 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | /** |
| 1123 | * xmlRelaxNGFreeStates: |
| 1124 | * @ctxt: a Relax-NG validation context |
| 1125 | * @states: teh container |
| 1126 | * |
| 1127 | * Free a RelaxNG validation state container |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1128 | */ |
| 1129 | static void |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1130 | xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1131 | xmlRelaxNGStatesPtr states) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1132 | { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1133 | if (states == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1134 | return; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1135 | if ((ctxt != NULL) && (ctxt->freeStates == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1136 | ctxt->freeStatesMax = 40; |
| 1137 | ctxt->freeStatesNr = 0; |
| 1138 | ctxt->freeStates = (xmlRelaxNGStatesPtr *) |
| 1139 | xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr)); |
| 1140 | if (ctxt->freeStates == NULL) { |
| 1141 | xmlRngVErrMemory(ctxt, "storing states\n"); |
| 1142 | } |
| 1143 | } else if ((ctxt != NULL) |
| 1144 | && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) { |
| 1145 | xmlRelaxNGStatesPtr *tmp; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1146 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1147 | tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates, |
| 1148 | 2 * ctxt->freeStatesMax * |
| 1149 | sizeof |
| 1150 | (xmlRelaxNGStatesPtr)); |
| 1151 | if (tmp == NULL) { |
| 1152 | xmlRngVErrMemory(ctxt, "storing states\n"); |
| 1153 | xmlFree(states->tabState); |
| 1154 | xmlFree(states); |
| 1155 | return; |
| 1156 | } |
| 1157 | ctxt->freeStates = tmp; |
| 1158 | ctxt->freeStatesMax *= 2; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1159 | } |
| 1160 | if ((ctxt == NULL) || (ctxt->freeState == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1161 | xmlFree(states->tabState); |
| 1162 | xmlFree(states); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1163 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1164 | ctxt->freeStates[ctxt->freeStatesNr++] = states; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1169 | * xmlRelaxNGNewValidState: |
| 1170 | * @ctxt: a Relax-NG validation context |
| 1171 | * @node: the current node or NULL for the document |
| 1172 | * |
| 1173 | * Allocate a new RelaxNG validation state |
| 1174 | * |
| 1175 | * Returns the newly allocated structure or NULL in case or error |
| 1176 | */ |
| 1177 | static xmlRelaxNGValidStatePtr |
| 1178 | xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node) |
| 1179 | { |
| 1180 | xmlRelaxNGValidStatePtr ret; |
| 1181 | xmlAttrPtr attr; |
| 1182 | xmlAttrPtr attrs[MAX_ATTR]; |
| 1183 | int nbAttrs = 0; |
| 1184 | xmlNodePtr root = NULL; |
| 1185 | |
| 1186 | if (node == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1187 | root = xmlDocGetRootElement(ctxt->doc); |
| 1188 | if (root == NULL) |
| 1189 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1190 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1191 | attr = node->properties; |
| 1192 | while (attr != NULL) { |
| 1193 | if (nbAttrs < MAX_ATTR) |
| 1194 | attrs[nbAttrs++] = attr; |
| 1195 | else |
| 1196 | nbAttrs++; |
| 1197 | attr = attr->next; |
| 1198 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1199 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1200 | if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) { |
| 1201 | ctxt->freeState->nbState--; |
| 1202 | ret = ctxt->freeState->tabState[ctxt->freeState->nbState]; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1203 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1204 | ret = |
| 1205 | (xmlRelaxNGValidStatePtr) |
| 1206 | xmlMalloc(sizeof(xmlRelaxNGValidState)); |
| 1207 | if (ret == NULL) { |
| 1208 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1209 | return (NULL); |
| 1210 | } |
| 1211 | memset(ret, 0, sizeof(xmlRelaxNGValidState)); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1212 | } |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 1213 | ret->value = NULL; |
| 1214 | ret->endvalue = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1215 | if (node == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1216 | ret->node = (xmlNodePtr) ctxt->doc; |
| 1217 | ret->seq = root; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1218 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1219 | ret->node = node; |
| 1220 | ret->seq = node->children; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1221 | } |
| 1222 | ret->nbAttrs = 0; |
| 1223 | if (nbAttrs > 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1224 | if (ret->attrs == NULL) { |
| 1225 | if (nbAttrs < 4) |
| 1226 | ret->maxAttrs = 4; |
| 1227 | else |
| 1228 | ret->maxAttrs = nbAttrs; |
| 1229 | ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs * |
| 1230 | sizeof(xmlAttrPtr)); |
| 1231 | if (ret->attrs == NULL) { |
| 1232 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1233 | return (ret); |
| 1234 | } |
| 1235 | } else if (ret->maxAttrs < nbAttrs) { |
| 1236 | xmlAttrPtr *tmp; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1237 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1238 | tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs * |
| 1239 | sizeof(xmlAttrPtr)); |
| 1240 | if (tmp == NULL) { |
| 1241 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1242 | return (ret); |
| 1243 | } |
| 1244 | ret->attrs = tmp; |
| 1245 | ret->maxAttrs = nbAttrs; |
| 1246 | } |
| 1247 | ret->nbAttrs = nbAttrs; |
| 1248 | if (nbAttrs < MAX_ATTR) { |
| 1249 | memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs); |
| 1250 | } else { |
| 1251 | attr = node->properties; |
| 1252 | nbAttrs = 0; |
| 1253 | while (attr != NULL) { |
| 1254 | ret->attrs[nbAttrs++] = attr; |
| 1255 | attr = attr->next; |
| 1256 | } |
| 1257 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1258 | } |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 1259 | ret->nbAttrLeft = ret->nbAttrs; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1260 | return (ret); |
| 1261 | } |
| 1262 | |
| 1263 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1264 | * xmlRelaxNGCopyValidState: |
| 1265 | * @ctxt: a Relax-NG validation context |
| 1266 | * @state: a validation state |
| 1267 | * |
| 1268 | * Copy the validation state |
| 1269 | * |
| 1270 | * Returns the newly allocated structure or NULL in case or error |
| 1271 | */ |
| 1272 | static xmlRelaxNGValidStatePtr |
| 1273 | xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1274 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1275 | { |
| 1276 | xmlRelaxNGValidStatePtr ret; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1277 | unsigned int maxAttrs; |
| 1278 | xmlAttrPtr *attrs; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1279 | |
| 1280 | if (state == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1281 | return (NULL); |
| 1282 | if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) { |
| 1283 | ctxt->freeState->nbState--; |
| 1284 | ret = ctxt->freeState->tabState[ctxt->freeState->nbState]; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1285 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1286 | ret = |
| 1287 | (xmlRelaxNGValidStatePtr) |
| 1288 | xmlMalloc(sizeof(xmlRelaxNGValidState)); |
| 1289 | if (ret == NULL) { |
| 1290 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1291 | return (NULL); |
| 1292 | } |
| 1293 | memset(ret, 0, sizeof(xmlRelaxNGValidState)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1294 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1295 | attrs = ret->attrs; |
| 1296 | maxAttrs = ret->maxAttrs; |
| 1297 | memcpy(ret, state, sizeof(xmlRelaxNGValidState)); |
| 1298 | ret->attrs = attrs; |
| 1299 | ret->maxAttrs = maxAttrs; |
| 1300 | if (state->nbAttrs > 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1301 | if (ret->attrs == NULL) { |
| 1302 | ret->maxAttrs = state->maxAttrs; |
| 1303 | ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs * |
| 1304 | sizeof(xmlAttrPtr)); |
| 1305 | if (ret->attrs == NULL) { |
| 1306 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1307 | ret->nbAttrs = 0; |
| 1308 | return (ret); |
| 1309 | } |
| 1310 | } else if (ret->maxAttrs < state->nbAttrs) { |
| 1311 | xmlAttrPtr *tmp; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1312 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1313 | tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs * |
| 1314 | sizeof(xmlAttrPtr)); |
| 1315 | if (tmp == NULL) { |
| 1316 | xmlRngVErrMemory(ctxt, "allocating states\n"); |
| 1317 | ret->nbAttrs = 0; |
| 1318 | return (ret); |
| 1319 | } |
| 1320 | ret->maxAttrs = state->maxAttrs; |
| 1321 | ret->attrs = tmp; |
| 1322 | } |
| 1323 | memcpy(ret->attrs, state->attrs, |
| 1324 | state->nbAttrs * sizeof(xmlAttrPtr)); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1325 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1326 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1327 | } |
| 1328 | |
| 1329 | /** |
| 1330 | * xmlRelaxNGEqualValidState: |
| 1331 | * @ctxt: a Relax-NG validation context |
| 1332 | * @state1: a validation state |
| 1333 | * @state2: a validation state |
| 1334 | * |
| 1335 | * Compare the validation states for equality |
| 1336 | * |
| 1337 | * Returns 1 if equald, 0 otherwise |
| 1338 | */ |
| 1339 | static int |
| 1340 | xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1341 | xmlRelaxNGValidStatePtr state1, |
| 1342 | xmlRelaxNGValidStatePtr state2) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1343 | { |
| 1344 | int i; |
| 1345 | |
| 1346 | if ((state1 == NULL) || (state2 == NULL)) |
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 == state2) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1349 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1350 | if (state1->node != state2->node) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1351 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1352 | if (state1->seq != state2->seq) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1353 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1354 | if (state1->nbAttrLeft != state2->nbAttrLeft) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1355 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1356 | if (state1->nbAttrs != state2->nbAttrs) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1357 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1358 | if (state1->endvalue != state2->endvalue) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1359 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1360 | if ((state1->value != state2->value) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1361 | (!xmlStrEqual(state1->value, state2->value))) |
| 1362 | return (0); |
| 1363 | for (i = 0; i < state1->nbAttrs; i++) { |
| 1364 | if (state1->attrs[i] != state2->attrs[i]) |
| 1365 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1366 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1367 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1368 | } |
| 1369 | |
| 1370 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1371 | * xmlRelaxNGFreeValidState: |
| 1372 | * @state: a validation state structure |
| 1373 | * |
| 1374 | * Deallocate a RelaxNG validation state structure. |
| 1375 | */ |
| 1376 | static void |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1377 | xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1378 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1379 | { |
| 1380 | if (state == NULL) |
| 1381 | return; |
| 1382 | |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1383 | if ((ctxt != NULL) && (ctxt->freeState == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1384 | ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1385 | } |
| 1386 | if ((ctxt == NULL) || (ctxt->freeState == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1387 | if (state->attrs != NULL) |
| 1388 | xmlFree(state->attrs); |
| 1389 | xmlFree(state); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1390 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1391 | xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1392 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1393 | } |
| 1394 | |
| 1395 | /************************************************************************ |
| 1396 | * * |
Daniel Veillard | 03c2f0a | 2004-01-25 19:54:59 +0000 | [diff] [blame] | 1397 | * Semi internal functions * |
| 1398 | * * |
| 1399 | ************************************************************************/ |
| 1400 | |
| 1401 | /** |
| 1402 | * xmlRelaxParserSetFlag: |
| 1403 | * @ctxt: a RelaxNG parser context |
| 1404 | * @flags: a set of flags values |
| 1405 | * |
| 1406 | * Semi private function used to pass informations to a parser context |
| 1407 | * which are a combination of xmlRelaxNGParserFlag . |
| 1408 | * |
| 1409 | * Returns 0 if success and -1 in case of error |
| 1410 | */ |
| 1411 | int |
| 1412 | xmlRelaxParserSetFlag(xmlRelaxNGParserCtxtPtr ctxt, int flags) |
| 1413 | { |
| 1414 | if (ctxt == NULL) return(-1); |
| 1415 | if (flags & XML_RELAXNGP_FREE_DOC) { |
| 1416 | ctxt->crng |= XML_RELAXNGP_FREE_DOC; |
| 1417 | flags -= XML_RELAXNGP_FREE_DOC; |
| 1418 | } |
| 1419 | if (flags & XML_RELAXNGP_CRNG) { |
| 1420 | ctxt->crng |= XML_RELAXNGP_CRNG; |
| 1421 | flags -= XML_RELAXNGP_CRNG; |
| 1422 | } |
| 1423 | if (flags != 0) return(-1); |
| 1424 | return(0); |
| 1425 | } |
| 1426 | |
| 1427 | /************************************************************************ |
| 1428 | * * |
| 1429 | * Document functions * |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1430 | * * |
| 1431 | ************************************************************************/ |
| 1432 | static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1433 | xmlDocPtr doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1434 | |
| 1435 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1436 | * xmlRelaxNGIncludePush: |
| 1437 | * @ctxt: the parser context |
| 1438 | * @value: the element doc |
| 1439 | * |
| 1440 | * Pushes a new include on top of the include stack |
| 1441 | * |
| 1442 | * Returns 0 in case of error, the index in the stack otherwise |
| 1443 | */ |
| 1444 | static int |
| 1445 | xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1446 | xmlRelaxNGIncludePtr value) |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1447 | { |
| 1448 | if (ctxt->incTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1449 | ctxt->incMax = 4; |
| 1450 | ctxt->incNr = 0; |
| 1451 | ctxt->incTab = |
| 1452 | (xmlRelaxNGIncludePtr *) xmlMalloc(ctxt->incMax * |
| 1453 | sizeof(ctxt->incTab[0])); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1454 | if (ctxt->incTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1455 | xmlRngPErrMemory(ctxt, "allocating include\n"); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1456 | return (0); |
| 1457 | } |
| 1458 | } |
| 1459 | if (ctxt->incNr >= ctxt->incMax) { |
| 1460 | ctxt->incMax *= 2; |
| 1461 | ctxt->incTab = |
| 1462 | (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1463 | ctxt->incMax * |
| 1464 | sizeof(ctxt->incTab[0])); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1465 | if (ctxt->incTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1466 | xmlRngPErrMemory(ctxt, "allocating include\n"); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1467 | return (0); |
| 1468 | } |
| 1469 | } |
| 1470 | ctxt->incTab[ctxt->incNr] = value; |
| 1471 | ctxt->inc = value; |
| 1472 | return (ctxt->incNr++); |
| 1473 | } |
| 1474 | |
| 1475 | /** |
| 1476 | * xmlRelaxNGIncludePop: |
| 1477 | * @ctxt: the parser context |
| 1478 | * |
| 1479 | * Pops the top include from the include stack |
| 1480 | * |
| 1481 | * Returns the include just removed |
| 1482 | */ |
| 1483 | static xmlRelaxNGIncludePtr |
| 1484 | xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt) |
| 1485 | { |
| 1486 | xmlRelaxNGIncludePtr ret; |
| 1487 | |
| 1488 | if (ctxt->incNr <= 0) |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1489 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1490 | ctxt->incNr--; |
| 1491 | if (ctxt->incNr > 0) |
| 1492 | ctxt->inc = ctxt->incTab[ctxt->incNr - 1]; |
| 1493 | else |
| 1494 | ctxt->inc = NULL; |
| 1495 | ret = ctxt->incTab[ctxt->incNr]; |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1496 | ctxt->incTab[ctxt->incNr] = NULL; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1497 | return (ret); |
| 1498 | } |
| 1499 | |
| 1500 | /** |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1501 | * xmlRelaxNGRemoveRedefine: |
| 1502 | * @ctxt: the parser context |
| 1503 | * @URL: the normalized URL |
| 1504 | * @target: the included target |
| 1505 | * @name: the define name to eliminate |
| 1506 | * |
| 1507 | * Applies the elimination algorithm of 4.7 |
| 1508 | * |
| 1509 | * Returns 0 in case of error, 1 in case of success. |
| 1510 | */ |
| 1511 | static int |
| 1512 | xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1513 | const xmlChar * URL ATTRIBUTE_UNUSED, |
| 1514 | xmlNodePtr target, const xmlChar * name) |
| 1515 | { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1516 | int found = 0; |
| 1517 | xmlNodePtr tmp, tmp2; |
| 1518 | xmlChar *name2; |
| 1519 | |
| 1520 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 1521 | if (name == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1522 | xmlGenericError(xmlGenericErrorContext, |
| 1523 | "Elimination of <include> start from %s\n", URL); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 1524 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1525 | xmlGenericError(xmlGenericErrorContext, |
| 1526 | "Elimination of <include> define %s from %s\n", |
| 1527 | name, URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1528 | #endif |
| 1529 | tmp = target; |
| 1530 | while (tmp != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1531 | tmp2 = tmp->next; |
| 1532 | if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) { |
| 1533 | found = 1; |
| 1534 | xmlUnlinkNode(tmp); |
| 1535 | xmlFreeNode(tmp); |
| 1536 | } else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) { |
| 1537 | name2 = xmlGetProp(tmp, BAD_CAST "name"); |
| 1538 | xmlRelaxNGNormExtSpace(name2); |
| 1539 | if (name2 != NULL) { |
| 1540 | if (xmlStrEqual(name, name2)) { |
| 1541 | found = 1; |
| 1542 | xmlUnlinkNode(tmp); |
| 1543 | xmlFreeNode(tmp); |
| 1544 | } |
| 1545 | xmlFree(name2); |
| 1546 | } |
| 1547 | } else if (IS_RELAXNG(tmp, "include")) { |
| 1548 | xmlChar *href = NULL; |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 1549 | xmlRelaxNGDocumentPtr inc = tmp->psvi; |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1550 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1551 | if ((inc != NULL) && (inc->doc != NULL) && |
| 1552 | (inc->doc->children != NULL)) { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1553 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1554 | if (xmlStrEqual |
| 1555 | (inc->doc->children->name, BAD_CAST "grammar")) { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1556 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1557 | href = xmlGetProp(tmp, BAD_CAST "href"); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1558 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1559 | if (xmlRelaxNGRemoveRedefine(ctxt, href, |
| 1560 | inc->doc->children-> |
| 1561 | children, name) == 1) { |
| 1562 | found = 1; |
| 1563 | } |
| 1564 | if (href != NULL) |
| 1565 | xmlFree(href); |
| 1566 | } |
| 1567 | } |
| 1568 | } |
| 1569 | tmp = tmp2; |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1570 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1571 | return (found); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1572 | } |
| 1573 | |
| 1574 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1575 | * xmlRelaxNGLoadInclude: |
| 1576 | * @ctxt: the parser context |
| 1577 | * @URL: the normalized URL |
| 1578 | * @node: the include node. |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1579 | * @ns: the namespace passed from the context. |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1580 | * |
| 1581 | * First lookup if the document is already loaded into the parser context, |
| 1582 | * check against recursion. If not found the resource is loaded and |
| 1583 | * the content is preprocessed before being returned back to the caller. |
| 1584 | * |
| 1585 | * Returns the xmlRelaxNGIncludePtr or NULL in case of error |
| 1586 | */ |
| 1587 | static xmlRelaxNGIncludePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1588 | xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * URL, |
| 1589 | xmlNodePtr node, const xmlChar * ns) |
| 1590 | { |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1591 | xmlRelaxNGIncludePtr ret = NULL; |
| 1592 | xmlDocPtr doc; |
| 1593 | int i; |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1594 | xmlNodePtr root, cur; |
| 1595 | |
| 1596 | #ifdef DEBUG_INCLUDE |
| 1597 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1598 | "xmlRelaxNGLoadInclude(%s)\n", URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1599 | #endif |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1600 | |
| 1601 | /* |
| 1602 | * check against recursion in the stack |
| 1603 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1604 | for (i = 0; i < ctxt->incNr; i++) { |
| 1605 | if (xmlStrEqual(ctxt->incTab[i]->href, URL)) { |
| 1606 | xmlRngPErr(ctxt, NULL, XML_RNGP_INCLUDE_RECURSE, |
| 1607 | "Detected an Include recursion for %s\n", URL, |
| 1608 | NULL); |
| 1609 | return (NULL); |
| 1610 | } |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | /* |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1614 | * load the document |
| 1615 | */ |
Daniel Veillard | 87247e8 | 2004-01-13 20:42:02 +0000 | [diff] [blame] | 1616 | doc = xmlReadFile((const char *) URL,NULL,0); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1617 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1618 | xmlRngPErr(ctxt, node, XML_RNGP_PARSE_ERROR, |
| 1619 | "xmlRelaxNG: could not load %s\n", URL, NULL); |
| 1620 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1621 | } |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1622 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1623 | xmlGenericError(xmlGenericErrorContext, "Parsed %s Okay\n", URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1624 | #endif |
| 1625 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1626 | /* |
| 1627 | * Allocate the document structures and register it first. |
| 1628 | */ |
| 1629 | ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude)); |
| 1630 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1631 | xmlRngPErrMemory(ctxt, "allocating include\n"); |
| 1632 | xmlFreeDoc(doc); |
| 1633 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1634 | } |
| 1635 | memset(ret, 0, sizeof(xmlRelaxNGInclude)); |
| 1636 | ret->doc = doc; |
| 1637 | ret->href = xmlStrdup(URL); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1638 | ret->next = ctxt->includes; |
| 1639 | ctxt->includes = ret; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1640 | |
| 1641 | /* |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1642 | * transmit the ns if needed |
| 1643 | */ |
| 1644 | if (ns != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1645 | root = xmlDocGetRootElement(doc); |
| 1646 | if (root != NULL) { |
| 1647 | if (xmlHasProp(root, BAD_CAST "ns") == NULL) { |
| 1648 | xmlSetProp(root, BAD_CAST "ns", ns); |
| 1649 | } |
| 1650 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1651 | } |
| 1652 | |
| 1653 | /* |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1654 | * push it on the stack |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1655 | */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1656 | xmlRelaxNGIncludePush(ctxt, ret); |
| 1657 | |
| 1658 | /* |
| 1659 | * Some preprocessing of the document content, this include recursing |
| 1660 | * in the include stack. |
| 1661 | */ |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1662 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1663 | xmlGenericError(xmlGenericErrorContext, "cleanup of %s\n", URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1664 | #endif |
| 1665 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1666 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 1667 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1668 | ctxt->inc = NULL; |
| 1669 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1670 | } |
| 1671 | |
| 1672 | /* |
| 1673 | * Pop up the include from the stack |
| 1674 | */ |
| 1675 | xmlRelaxNGIncludePop(ctxt); |
| 1676 | |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1677 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1678 | xmlGenericError(xmlGenericErrorContext, "Checking of %s\n", URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1679 | #endif |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1680 | /* |
| 1681 | * Check that the top element is a grammar |
| 1682 | */ |
| 1683 | root = xmlDocGetRootElement(doc); |
| 1684 | if (root == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1685 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, |
| 1686 | "xmlRelaxNG: included document is empty %s\n", URL, |
| 1687 | NULL); |
| 1688 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1689 | } |
| 1690 | if (!IS_RELAXNG(root, "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1691 | xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING, |
| 1692 | "xmlRelaxNG: included document %s root is not a grammar\n", |
| 1693 | URL, NULL); |
| 1694 | return (NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1695 | } |
| 1696 | |
| 1697 | /* |
| 1698 | * Elimination of redefined rules in the include. |
| 1699 | */ |
| 1700 | cur = node->children; |
| 1701 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1702 | if (IS_RELAXNG(cur, "start")) { |
| 1703 | int found = 0; |
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 | found = |
| 1706 | xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL); |
| 1707 | if (!found) { |
| 1708 | xmlRngPErr(ctxt, node, XML_RNGP_START_MISSING, |
| 1709 | "xmlRelaxNG: include %s has a start but not the included grammar\n", |
| 1710 | URL, NULL); |
| 1711 | } |
| 1712 | } else if (IS_RELAXNG(cur, "define")) { |
| 1713 | xmlChar *name; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1714 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1715 | name = xmlGetProp(cur, BAD_CAST "name"); |
| 1716 | if (name == NULL) { |
| 1717 | xmlRngPErr(ctxt, node, XML_RNGP_NAME_MISSING, |
| 1718 | "xmlRelaxNG: include %s has define without name\n", |
| 1719 | URL, NULL); |
| 1720 | } else { |
| 1721 | int found; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1722 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1723 | xmlRelaxNGNormExtSpace(name); |
| 1724 | found = xmlRelaxNGRemoveRedefine(ctxt, URL, |
| 1725 | root->children, name); |
| 1726 | if (!found) { |
| 1727 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_MISSING, |
| 1728 | "xmlRelaxNG: include %s has a define %s but not the included grammar\n", |
| 1729 | URL, name); |
| 1730 | } |
| 1731 | xmlFree(name); |
| 1732 | } |
| 1733 | } |
| 1734 | cur = cur->next; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1735 | } |
| 1736 | |
| 1737 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1738 | return (ret); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1739 | } |
| 1740 | |
| 1741 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1742 | * xmlRelaxNGValidErrorPush: |
| 1743 | * @ctxt: the validation context |
| 1744 | * @err: the error code |
| 1745 | * @arg1: the first string argument |
| 1746 | * @arg2: the second string argument |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1747 | * @dup: arg need to be duplicated |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1748 | * |
| 1749 | * Pushes a new error on top of the error stack |
| 1750 | * |
| 1751 | * Returns 0 in case of error, the index in the stack otherwise |
| 1752 | */ |
| 1753 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1754 | xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt, |
| 1755 | xmlRelaxNGValidErr err, const xmlChar * arg1, |
| 1756 | const xmlChar * arg2, int dup) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1757 | { |
| 1758 | xmlRelaxNGValidErrorPtr cur; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1759 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 1760 | #ifdef DEBUG_ERROR |
| 1761 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1762 | "Pushing error %d at %d on stack\n", err, ctxt->errNr); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 1763 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1764 | if (ctxt->errTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1765 | ctxt->errMax = 8; |
| 1766 | ctxt->errNr = 0; |
| 1767 | ctxt->errTab = |
| 1768 | (xmlRelaxNGValidErrorPtr) xmlMalloc(ctxt->errMax * |
| 1769 | sizeof |
| 1770 | (xmlRelaxNGValidError)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1771 | if (ctxt->errTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1772 | xmlRngVErrMemory(ctxt, "pushing error\n"); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1773 | return (0); |
| 1774 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1775 | ctxt->err = NULL; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1776 | } |
| 1777 | if (ctxt->errNr >= ctxt->errMax) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1778 | ctxt->errMax *= 2; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1779 | ctxt->errTab = |
| 1780 | (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1781 | ctxt->errMax * |
| 1782 | sizeof |
| 1783 | (xmlRelaxNGValidError)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1784 | if (ctxt->errTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1785 | xmlRngVErrMemory(ctxt, "pushing error\n"); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1786 | return (0); |
| 1787 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1788 | ctxt->err = &ctxt->errTab[ctxt->errNr - 1]; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1789 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 1790 | if ((ctxt->err != NULL) && (ctxt->state != NULL) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1791 | (ctxt->err->node == ctxt->state->node) && (ctxt->err->err == err)) |
| 1792 | return (ctxt->errNr); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1793 | cur = &ctxt->errTab[ctxt->errNr]; |
| 1794 | cur->err = err; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1795 | if (dup) { |
| 1796 | cur->arg1 = xmlStrdup(arg1); |
| 1797 | cur->arg2 = xmlStrdup(arg2); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1798 | cur->flags = ERROR_IS_DUP; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1799 | } else { |
| 1800 | cur->arg1 = arg1; |
| 1801 | cur->arg2 = arg2; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1802 | cur->flags = 0; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1803 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1804 | if (ctxt->state != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1805 | cur->node = ctxt->state->node; |
| 1806 | cur->seq = ctxt->state->seq; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1807 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1808 | cur->node = NULL; |
| 1809 | cur->seq = NULL; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1810 | } |
| 1811 | ctxt->err = cur; |
| 1812 | return (ctxt->errNr++); |
| 1813 | } |
| 1814 | |
| 1815 | /** |
| 1816 | * xmlRelaxNGValidErrorPop: |
| 1817 | * @ctxt: the validation context |
| 1818 | * |
| 1819 | * Pops the top error from the error stack |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1820 | */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1821 | static void |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1822 | xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt) |
| 1823 | { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1824 | xmlRelaxNGValidErrorPtr cur; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1825 | |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1826 | if (ctxt->errNr <= 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1827 | ctxt->err = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1828 | return; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1829 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1830 | ctxt->errNr--; |
| 1831 | if (ctxt->errNr > 0) |
| 1832 | ctxt->err = &ctxt->errTab[ctxt->errNr - 1]; |
| 1833 | else |
| 1834 | ctxt->err = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1835 | cur = &ctxt->errTab[ctxt->errNr]; |
| 1836 | if (cur->flags & ERROR_IS_DUP) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1837 | if (cur->arg1 != NULL) |
| 1838 | xmlFree((xmlChar *) cur->arg1); |
| 1839 | cur->arg1 = NULL; |
| 1840 | if (cur->arg2 != NULL) |
| 1841 | xmlFree((xmlChar *) cur->arg2); |
| 1842 | cur->arg2 = NULL; |
| 1843 | cur->flags = 0; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1844 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1845 | } |
| 1846 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1847 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1848 | * xmlRelaxNGDocumentPush: |
| 1849 | * @ctxt: the parser context |
| 1850 | * @value: the element doc |
| 1851 | * |
| 1852 | * Pushes a new doc on top of the doc stack |
| 1853 | * |
| 1854 | * Returns 0 in case of error, the index in the stack otherwise |
| 1855 | */ |
| 1856 | static int |
| 1857 | xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1858 | xmlRelaxNGDocumentPtr value) |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1859 | { |
| 1860 | if (ctxt->docTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1861 | ctxt->docMax = 4; |
| 1862 | ctxt->docNr = 0; |
| 1863 | ctxt->docTab = |
| 1864 | (xmlRelaxNGDocumentPtr *) xmlMalloc(ctxt->docMax * |
| 1865 | sizeof(ctxt->docTab[0])); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1866 | if (ctxt->docTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1867 | xmlRngPErrMemory(ctxt, "adding document\n"); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1868 | return (0); |
| 1869 | } |
| 1870 | } |
| 1871 | if (ctxt->docNr >= ctxt->docMax) { |
| 1872 | ctxt->docMax *= 2; |
| 1873 | ctxt->docTab = |
| 1874 | (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1875 | ctxt->docMax * |
| 1876 | sizeof(ctxt->docTab[0])); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1877 | if (ctxt->docTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1878 | xmlRngPErrMemory(ctxt, "adding document\n"); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1879 | return (0); |
| 1880 | } |
| 1881 | } |
| 1882 | ctxt->docTab[ctxt->docNr] = value; |
| 1883 | ctxt->doc = value; |
| 1884 | return (ctxt->docNr++); |
| 1885 | } |
| 1886 | |
| 1887 | /** |
| 1888 | * xmlRelaxNGDocumentPop: |
| 1889 | * @ctxt: the parser context |
| 1890 | * |
| 1891 | * Pops the top doc from the doc stack |
| 1892 | * |
| 1893 | * Returns the doc just removed |
| 1894 | */ |
| 1895 | static xmlRelaxNGDocumentPtr |
| 1896 | xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt) |
| 1897 | { |
| 1898 | xmlRelaxNGDocumentPtr ret; |
| 1899 | |
| 1900 | if (ctxt->docNr <= 0) |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1901 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1902 | ctxt->docNr--; |
| 1903 | if (ctxt->docNr > 0) |
| 1904 | ctxt->doc = ctxt->docTab[ctxt->docNr - 1]; |
| 1905 | else |
| 1906 | ctxt->doc = NULL; |
| 1907 | ret = ctxt->docTab[ctxt->docNr]; |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1908 | ctxt->docTab[ctxt->docNr] = NULL; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1909 | return (ret); |
| 1910 | } |
| 1911 | |
| 1912 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1913 | * xmlRelaxNGLoadExternalRef: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1914 | * @ctxt: the parser context |
| 1915 | * @URL: the normalized URL |
| 1916 | * @ns: the inherited ns if any |
| 1917 | * |
| 1918 | * First lookup if the document is already loaded into the parser context, |
| 1919 | * check against recursion. If not found the resource is loaded and |
| 1920 | * the content is preprocessed before being returned back to the caller. |
| 1921 | * |
| 1922 | * Returns the xmlRelaxNGDocumentPtr or NULL in case of error |
| 1923 | */ |
| 1924 | static xmlRelaxNGDocumentPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1925 | xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt, |
| 1926 | const xmlChar * URL, const xmlChar * ns) |
| 1927 | { |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1928 | xmlRelaxNGDocumentPtr ret = NULL; |
| 1929 | xmlDocPtr doc; |
| 1930 | xmlNodePtr root; |
| 1931 | int i; |
| 1932 | |
| 1933 | /* |
| 1934 | * check against recursion in the stack |
| 1935 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1936 | for (i = 0; i < ctxt->docNr; i++) { |
| 1937 | if (xmlStrEqual(ctxt->docTab[i]->href, URL)) { |
| 1938 | xmlRngPErr(ctxt, NULL, XML_RNGP_EXTERNALREF_RECURSE, |
| 1939 | "Detected an externalRef recursion for %s\n", URL, |
| 1940 | NULL); |
| 1941 | return (NULL); |
| 1942 | } |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1943 | } |
| 1944 | |
| 1945 | /* |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1946 | * load the document |
| 1947 | */ |
Daniel Veillard | 87247e8 | 2004-01-13 20:42:02 +0000 | [diff] [blame] | 1948 | doc = xmlReadFile((const char *) URL,NULL,0); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1949 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1950 | xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR, |
| 1951 | "xmlRelaxNG: could not load %s\n", URL, NULL); |
| 1952 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1953 | } |
| 1954 | |
| 1955 | /* |
| 1956 | * Allocate the document structures and register it first. |
| 1957 | */ |
| 1958 | ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument)); |
| 1959 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1960 | xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_ERR_NO_MEMORY, |
| 1961 | "xmlRelaxNG: allocate memory for doc %s\n", URL, NULL); |
| 1962 | xmlFreeDoc(doc); |
| 1963 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1964 | } |
| 1965 | memset(ret, 0, sizeof(xmlRelaxNGDocument)); |
| 1966 | ret->doc = doc; |
| 1967 | ret->href = xmlStrdup(URL); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1968 | ret->next = ctxt->documents; |
| 1969 | ctxt->documents = ret; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1970 | |
| 1971 | /* |
| 1972 | * transmit the ns if needed |
| 1973 | */ |
| 1974 | if (ns != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1975 | root = xmlDocGetRootElement(doc); |
| 1976 | if (root != NULL) { |
| 1977 | if (xmlHasProp(root, BAD_CAST "ns") == NULL) { |
| 1978 | xmlSetProp(root, BAD_CAST "ns", ns); |
| 1979 | } |
| 1980 | } |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1981 | } |
| 1982 | |
| 1983 | /* |
| 1984 | * push it on the stack and register it in the hash table |
| 1985 | */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1986 | xmlRelaxNGDocumentPush(ctxt, ret); |
| 1987 | |
| 1988 | /* |
| 1989 | * Some preprocessing of the document content |
| 1990 | */ |
| 1991 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 1992 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1993 | ctxt->doc = NULL; |
| 1994 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1995 | } |
| 1996 | |
| 1997 | xmlRelaxNGDocumentPop(ctxt); |
| 1998 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 1999 | return (ret); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 2000 | } |
| 2001 | |
| 2002 | /************************************************************************ |
| 2003 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2004 | * Error functions * |
| 2005 | * * |
| 2006 | ************************************************************************/ |
| 2007 | |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2008 | #define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0); |
| 2009 | #define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0); |
| 2010 | #define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0); |
| 2011 | #define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1); |
| 2012 | #define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2013 | |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 2014 | static const char * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2015 | xmlRelaxNGDefName(xmlRelaxNGDefinePtr def) |
| 2016 | { |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 2017 | if (def == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2018 | return ("none"); |
| 2019 | switch (def->type) { |
| 2020 | case XML_RELAXNG_EMPTY: |
| 2021 | return ("empty"); |
| 2022 | case XML_RELAXNG_NOT_ALLOWED: |
| 2023 | return ("notAllowed"); |
| 2024 | case XML_RELAXNG_EXCEPT: |
| 2025 | return ("except"); |
| 2026 | case XML_RELAXNG_TEXT: |
| 2027 | return ("text"); |
| 2028 | case XML_RELAXNG_ELEMENT: |
| 2029 | return ("element"); |
| 2030 | case XML_RELAXNG_DATATYPE: |
| 2031 | return ("datatype"); |
| 2032 | case XML_RELAXNG_VALUE: |
| 2033 | return ("value"); |
| 2034 | case XML_RELAXNG_LIST: |
| 2035 | return ("list"); |
| 2036 | case XML_RELAXNG_ATTRIBUTE: |
| 2037 | return ("attribute"); |
| 2038 | case XML_RELAXNG_DEF: |
| 2039 | return ("def"); |
| 2040 | case XML_RELAXNG_REF: |
| 2041 | return ("ref"); |
| 2042 | case XML_RELAXNG_EXTERNALREF: |
| 2043 | return ("externalRef"); |
| 2044 | case XML_RELAXNG_PARENTREF: |
| 2045 | return ("parentRef"); |
| 2046 | case XML_RELAXNG_OPTIONAL: |
| 2047 | return ("optional"); |
| 2048 | case XML_RELAXNG_ZEROORMORE: |
| 2049 | return ("zeroOrMore"); |
| 2050 | case XML_RELAXNG_ONEORMORE: |
| 2051 | return ("oneOrMore"); |
| 2052 | case XML_RELAXNG_CHOICE: |
| 2053 | return ("choice"); |
| 2054 | case XML_RELAXNG_GROUP: |
| 2055 | return ("group"); |
| 2056 | case XML_RELAXNG_INTERLEAVE: |
| 2057 | return ("interleave"); |
| 2058 | case XML_RELAXNG_START: |
| 2059 | return ("start"); |
| 2060 | case XML_RELAXNG_NOOP: |
| 2061 | return ("noop"); |
| 2062 | case XML_RELAXNG_PARAM: |
| 2063 | return ("param"); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 2064 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2065 | return ("unknown"); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 2066 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 2067 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2068 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2069 | * xmlRelaxNGGetErrorString: |
| 2070 | * @err: the error code |
| 2071 | * @arg1: the first string argument |
| 2072 | * @arg2: the second string argument |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2073 | * |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2074 | * computes a formatted error string for the given error code and args |
| 2075 | * |
| 2076 | * Returns the error string, it must be deallocated by the caller |
| 2077 | */ |
| 2078 | static xmlChar * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2079 | xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar * arg1, |
| 2080 | const xmlChar * arg2) |
| 2081 | { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2082 | char msg[1000]; |
| 2083 | |
| 2084 | if (arg1 == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2085 | arg1 = BAD_CAST ""; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2086 | if (arg2 == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2087 | arg2 = BAD_CAST ""; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2088 | |
| 2089 | msg[0] = 0; |
| 2090 | switch (err) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2091 | case XML_RELAXNG_OK: |
| 2092 | return (NULL); |
| 2093 | case XML_RELAXNG_ERR_MEMORY: |
| 2094 | return (xmlCharStrdup("out of memory\n")); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2095 | case XML_RELAXNG_ERR_TYPE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2096 | snprintf(msg, 1000, "failed to validate type %s\n", arg1); |
| 2097 | break; |
| 2098 | case XML_RELAXNG_ERR_TYPEVAL: |
| 2099 | snprintf(msg, 1000, "Type %s doesn't allow value '%s'\n", arg1, |
| 2100 | arg2); |
| 2101 | break; |
| 2102 | case XML_RELAXNG_ERR_DUPID: |
| 2103 | snprintf(msg, 1000, "ID %s redefined\n", arg1); |
| 2104 | break; |
| 2105 | case XML_RELAXNG_ERR_TYPECMP: |
| 2106 | snprintf(msg, 1000, "failed to compare type %s\n", arg1); |
| 2107 | break; |
| 2108 | case XML_RELAXNG_ERR_NOSTATE: |
| 2109 | return (xmlCharStrdup("Internal error: no state\n")); |
| 2110 | case XML_RELAXNG_ERR_NODEFINE: |
| 2111 | return (xmlCharStrdup("Internal error: no define\n")); |
| 2112 | case XML_RELAXNG_ERR_INTERNAL: |
| 2113 | snprintf(msg, 1000, "Internal error: %s\n", arg1); |
| 2114 | break; |
| 2115 | case XML_RELAXNG_ERR_LISTEXTRA: |
| 2116 | snprintf(msg, 1000, "Extra data in list: %s\n", arg1); |
| 2117 | break; |
| 2118 | case XML_RELAXNG_ERR_INTERNODATA: |
| 2119 | return (xmlCharStrdup |
| 2120 | ("Internal: interleave block has no data\n")); |
| 2121 | case XML_RELAXNG_ERR_INTERSEQ: |
| 2122 | return (xmlCharStrdup("Invalid sequence in interleave\n")); |
| 2123 | case XML_RELAXNG_ERR_INTEREXTRA: |
| 2124 | snprintf(msg, 1000, "Extra element %s in interleave\n", arg1); |
| 2125 | break; |
| 2126 | case XML_RELAXNG_ERR_ELEMNAME: |
| 2127 | snprintf(msg, 1000, "Expecting element %s, got %s\n", arg1, |
| 2128 | arg2); |
| 2129 | break; |
| 2130 | case XML_RELAXNG_ERR_ELEMNONS: |
| 2131 | snprintf(msg, 1000, "Expecting a namespace for element %s\n", |
| 2132 | arg1); |
| 2133 | break; |
| 2134 | case XML_RELAXNG_ERR_ELEMWRONGNS: |
| 2135 | snprintf(msg, 1000, |
| 2136 | "Element %s has wrong namespace: expecting %s\n", arg1, |
| 2137 | arg2); |
| 2138 | break; |
| 2139 | case XML_RELAXNG_ERR_ELEMWRONG: |
| 2140 | snprintf(msg, 1000, "Did not expect element %s there\n", arg1); |
| 2141 | break; |
| 2142 | case XML_RELAXNG_ERR_TEXTWRONG: |
| 2143 | snprintf(msg, 1000, |
| 2144 | "Did not expect text in element %s content\n", arg1); |
| 2145 | break; |
| 2146 | case XML_RELAXNG_ERR_ELEMEXTRANS: |
| 2147 | snprintf(msg, 1000, "Expecting no namespace for element %s\n", |
| 2148 | arg1); |
| 2149 | break; |
| 2150 | case XML_RELAXNG_ERR_ELEMNOTEMPTY: |
| 2151 | snprintf(msg, 1000, "Expecting element %s to be empty\n", arg1); |
| 2152 | break; |
| 2153 | case XML_RELAXNG_ERR_NOELEM: |
| 2154 | snprintf(msg, 1000, "Expecting an element %s, got nothing\n", |
| 2155 | arg1); |
| 2156 | break; |
| 2157 | case XML_RELAXNG_ERR_NOTELEM: |
| 2158 | return (xmlCharStrdup("Expecting an element got text\n")); |
| 2159 | case XML_RELAXNG_ERR_ATTRVALID: |
| 2160 | snprintf(msg, 1000, "Element %s failed to validate attributes\n", |
| 2161 | arg1); |
| 2162 | break; |
| 2163 | case XML_RELAXNG_ERR_CONTENTVALID: |
| 2164 | snprintf(msg, 1000, "Element %s failed to validate content\n", |
| 2165 | arg1); |
| 2166 | break; |
| 2167 | case XML_RELAXNG_ERR_EXTRACONTENT: |
| 2168 | snprintf(msg, 1000, "Element %s has extra content: %s\n", |
| 2169 | arg1, arg2); |
| 2170 | break; |
| 2171 | case XML_RELAXNG_ERR_INVALIDATTR: |
| 2172 | snprintf(msg, 1000, "Invalid attribute %s for element %s\n", |
| 2173 | arg1, arg2); |
| 2174 | break; |
| 2175 | case XML_RELAXNG_ERR_LACKDATA: |
| 2176 | snprintf(msg, 1000, "Datatype element %s contains no data\n", |
| 2177 | arg1); |
| 2178 | break; |
| 2179 | case XML_RELAXNG_ERR_DATAELEM: |
| 2180 | snprintf(msg, 1000, "Datatype element %s has child elements\n", |
| 2181 | arg1); |
| 2182 | break; |
| 2183 | case XML_RELAXNG_ERR_VALELEM: |
| 2184 | snprintf(msg, 1000, "Value element %s has child elements\n", |
| 2185 | arg1); |
| 2186 | break; |
| 2187 | case XML_RELAXNG_ERR_LISTELEM: |
| 2188 | snprintf(msg, 1000, "List element %s has child elements\n", |
| 2189 | arg1); |
| 2190 | break; |
| 2191 | case XML_RELAXNG_ERR_DATATYPE: |
| 2192 | snprintf(msg, 1000, "Error validating datatype %s\n", arg1); |
| 2193 | break; |
| 2194 | case XML_RELAXNG_ERR_VALUE: |
| 2195 | snprintf(msg, 1000, "Error validating value %s\n", arg1); |
| 2196 | break; |
| 2197 | case XML_RELAXNG_ERR_LIST: |
| 2198 | return (xmlCharStrdup("Error validating list\n")); |
| 2199 | case XML_RELAXNG_ERR_NOGRAMMAR: |
| 2200 | return (xmlCharStrdup("No top grammar defined\n")); |
| 2201 | case XML_RELAXNG_ERR_EXTRADATA: |
| 2202 | return (xmlCharStrdup("Extra data in the document\n")); |
| 2203 | default: |
| 2204 | return (xmlCharStrdup("Unknown error !\n")); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2205 | } |
| 2206 | if (msg[0] == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2207 | snprintf(msg, 1000, "Unknown error code %d\n", err); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2208 | } |
Daniel Veillard | adbb0e6 | 2003-05-10 20:02:45 +0000 | [diff] [blame] | 2209 | msg[1000 - 1] = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2210 | return (xmlStrdup((xmlChar *) msg)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2211 | } |
| 2212 | |
| 2213 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2214 | * xmlRelaxNGShowValidError: |
| 2215 | * @ctxt: the validation context |
| 2216 | * @err: the error number |
| 2217 | * @node: the node |
| 2218 | * @child: the node child generating the problem. |
| 2219 | * @arg1: the first argument |
| 2220 | * @arg2: the second argument |
| 2221 | * |
| 2222 | * Show a validation error. |
| 2223 | */ |
| 2224 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2225 | xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt, |
| 2226 | xmlRelaxNGValidErr err, xmlNodePtr node, |
| 2227 | xmlNodePtr child, const xmlChar * arg1, |
| 2228 | const xmlChar * arg2) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2229 | { |
| 2230 | xmlChar *msg; |
| 2231 | |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 2232 | if (ctxt->flags & FLAGS_NOERROR) |
Daniel Veillard | f03a8cd | 2005-09-04 12:01:57 +0000 | [diff] [blame] | 2233 | return; |
| 2234 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2235 | #ifdef DEBUG_ERROR |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2236 | xmlGenericError(xmlGenericErrorContext, "Show error %d\n", err); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2237 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2238 | msg = xmlRelaxNGGetErrorString(err, arg1, arg2); |
| 2239 | if (msg == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2240 | return; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2241 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2242 | if (ctxt->errNo == XML_RELAXNG_OK) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2243 | ctxt->errNo = err; |
| 2244 | xmlRngVErr(ctxt, (child == NULL ? node : child), err, |
| 2245 | (const char *) msg, arg1, arg2); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2246 | xmlFree(msg); |
| 2247 | } |
| 2248 | |
| 2249 | /** |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2250 | * xmlRelaxNGPopErrors: |
| 2251 | * @ctxt: the validation context |
| 2252 | * @level: the error level in the stack |
| 2253 | * |
| 2254 | * pop and discard all errors until the given level is reached |
| 2255 | */ |
| 2256 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2257 | xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level) |
| 2258 | { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2259 | int i; |
| 2260 | xmlRelaxNGValidErrorPtr err; |
| 2261 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2262 | #ifdef DEBUG_ERROR |
| 2263 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2264 | "Pop errors till level %d\n", level); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2265 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2266 | for (i = level; i < ctxt->errNr; i++) { |
| 2267 | err = &ctxt->errTab[i]; |
| 2268 | if (err->flags & ERROR_IS_DUP) { |
| 2269 | if (err->arg1 != NULL) |
| 2270 | xmlFree((xmlChar *) err->arg1); |
| 2271 | err->arg1 = NULL; |
| 2272 | if (err->arg2 != NULL) |
| 2273 | xmlFree((xmlChar *) err->arg2); |
| 2274 | err->arg2 = NULL; |
| 2275 | err->flags = 0; |
| 2276 | } |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2277 | } |
| 2278 | ctxt->errNr = level; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2279 | if (ctxt->errNr <= 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2280 | ctxt->err = NULL; |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2281 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2282 | |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2283 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2284 | * xmlRelaxNGDumpValidError: |
| 2285 | * @ctxt: the validation context |
| 2286 | * |
| 2287 | * Show all validation error over a given index. |
| 2288 | */ |
| 2289 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2290 | xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt) |
| 2291 | { |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 2292 | int i, j, k; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2293 | xmlRelaxNGValidErrorPtr err, dup; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2294 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2295 | #ifdef DEBUG_ERROR |
| 2296 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2297 | "Dumping error stack %d errors\n", ctxt->errNr); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2298 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2299 | for (i = 0, k = 0; i < ctxt->errNr; i++) { |
| 2300 | err = &ctxt->errTab[i]; |
| 2301 | if (k < MAX_ERROR) { |
| 2302 | for (j = 0; j < i; j++) { |
| 2303 | dup = &ctxt->errTab[j]; |
| 2304 | if ((err->err == dup->err) && (err->node == dup->node) && |
| 2305 | (xmlStrEqual(err->arg1, dup->arg1)) && |
| 2306 | (xmlStrEqual(err->arg2, dup->arg2))) { |
| 2307 | goto skip; |
| 2308 | } |
| 2309 | } |
| 2310 | xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq, |
| 2311 | err->arg1, err->arg2); |
| 2312 | k++; |
| 2313 | } |
| 2314 | skip: |
| 2315 | if (err->flags & ERROR_IS_DUP) { |
| 2316 | if (err->arg1 != NULL) |
| 2317 | xmlFree((xmlChar *) err->arg1); |
| 2318 | err->arg1 = NULL; |
| 2319 | if (err->arg2 != NULL) |
| 2320 | xmlFree((xmlChar *) err->arg2); |
| 2321 | err->arg2 = NULL; |
| 2322 | err->flags = 0; |
| 2323 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2324 | } |
| 2325 | ctxt->errNr = 0; |
| 2326 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2327 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2328 | /** |
| 2329 | * xmlRelaxNGAddValidError: |
| 2330 | * @ctxt: the validation context |
| 2331 | * @err: the error number |
| 2332 | * @arg1: the first argument |
| 2333 | * @arg2: the second argument |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2334 | * @dup: need to dup the args |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2335 | * |
| 2336 | * Register a validation error, either generating it if it's sure |
| 2337 | * or stacking it for later handling if unsure. |
| 2338 | */ |
| 2339 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2340 | xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt, |
| 2341 | xmlRelaxNGValidErr err, const xmlChar * arg1, |
| 2342 | const xmlChar * arg2, int dup) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2343 | { |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 2344 | if (ctxt == NULL) |
| 2345 | return; |
| 2346 | if (ctxt->flags & FLAGS_NOERROR) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2347 | return; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2348 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2349 | #ifdef DEBUG_ERROR |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2350 | xmlGenericError(xmlGenericErrorContext, "Adding error %d\n", err); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2351 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2352 | /* |
| 2353 | * generate the error directly |
| 2354 | */ |
William M. Brack | 6092962 | 2004-03-27 17:54:18 +0000 | [diff] [blame] | 2355 | if (((ctxt->flags & FLAGS_IGNORABLE) == 0) || |
| 2356 | (ctxt->flags & FLAGS_NEGATIVE)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2357 | xmlNodePtr node, seq; |
| 2358 | |
| 2359 | /* |
| 2360 | * Flush first any stacked error which might be the |
| 2361 | * real cause of the problem. |
| 2362 | */ |
| 2363 | if (ctxt->errNr != 0) |
| 2364 | xmlRelaxNGDumpValidError(ctxt); |
| 2365 | if (ctxt->state != NULL) { |
| 2366 | node = ctxt->state->node; |
| 2367 | seq = ctxt->state->seq; |
| 2368 | } else { |
| 2369 | node = seq = NULL; |
| 2370 | } |
| 2371 | xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2372 | } |
| 2373 | /* |
| 2374 | * Stack the error for later processing if needed |
| 2375 | */ |
| 2376 | else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2377 | xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2378 | } |
| 2379 | } |
| 2380 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2381 | |
| 2382 | /************************************************************************ |
| 2383 | * * |
| 2384 | * Type library hooks * |
| 2385 | * * |
| 2386 | ************************************************************************/ |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2387 | static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2388 | const xmlChar * str); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2389 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2390 | /** |
| 2391 | * xmlRelaxNGSchemaTypeHave: |
| 2392 | * @data: data needed for the library |
| 2393 | * @type: the type name |
| 2394 | * |
| 2395 | * Check if the given type is provided by |
| 2396 | * the W3C XMLSchema Datatype library. |
| 2397 | * |
| 2398 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2399 | */ |
| 2400 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2401 | xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar * type) |
| 2402 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2403 | xmlSchemaTypePtr typ; |
| 2404 | |
| 2405 | if (type == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2406 | return (-1); |
| 2407 | typ = xmlSchemaGetPredefinedType(type, |
| 2408 | BAD_CAST |
| 2409 | "http://www.w3.org/2001/XMLSchema"); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2410 | if (typ == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2411 | return (0); |
| 2412 | return (1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2413 | } |
| 2414 | |
| 2415 | /** |
| 2416 | * xmlRelaxNGSchemaTypeCheck: |
| 2417 | * @data: data needed for the library |
| 2418 | * @type: the type name |
| 2419 | * @value: the value to check |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2420 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2421 | * |
| 2422 | * Check if the given type and value are validated by |
| 2423 | * the W3C XMLSchema Datatype library. |
| 2424 | * |
| 2425 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2426 | */ |
| 2427 | static int |
| 2428 | xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2429 | const xmlChar * type, |
| 2430 | const xmlChar * value, |
| 2431 | void **result, xmlNodePtr node) |
| 2432 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2433 | xmlSchemaTypePtr typ; |
| 2434 | int ret; |
| 2435 | |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2436 | if ((type == NULL) || (value == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2437 | return (-1); |
| 2438 | typ = xmlSchemaGetPredefinedType(type, |
| 2439 | BAD_CAST |
| 2440 | "http://www.w3.org/2001/XMLSchema"); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2441 | if (typ == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2442 | return (-1); |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2443 | ret = xmlSchemaValPredefTypeNode(typ, value, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2444 | (xmlSchemaValPtr *) result, node); |
| 2445 | if (ret == 2) /* special ID error code */ |
| 2446 | return (2); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2447 | if (ret == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2448 | return (1); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2449 | if (ret > 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2450 | return (0); |
| 2451 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2452 | } |
| 2453 | |
| 2454 | /** |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2455 | * xmlRelaxNGSchemaFacetCheck: |
| 2456 | * @data: data needed for the library |
| 2457 | * @type: the type name |
| 2458 | * @facet: the facet name |
| 2459 | * @val: the facet value |
| 2460 | * @strval: the string value |
| 2461 | * @value: the value to check |
| 2462 | * |
| 2463 | * Function provided by a type library to check a value facet |
| 2464 | * |
| 2465 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2466 | */ |
| 2467 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2468 | xmlRelaxNGSchemaFacetCheck(void *data ATTRIBUTE_UNUSED, |
| 2469 | const xmlChar * type, const xmlChar * facetname, |
| 2470 | const xmlChar * val, const xmlChar * strval, |
| 2471 | void *value) |
| 2472 | { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2473 | xmlSchemaFacetPtr facet; |
| 2474 | xmlSchemaTypePtr typ; |
| 2475 | int ret; |
| 2476 | |
| 2477 | if ((type == NULL) || (strval == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2478 | return (-1); |
| 2479 | typ = xmlSchemaGetPredefinedType(type, |
| 2480 | BAD_CAST |
| 2481 | "http://www.w3.org/2001/XMLSchema"); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2482 | if (typ == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2483 | return (-1); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2484 | |
| 2485 | facet = xmlSchemaNewFacet(); |
| 2486 | if (facet == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2487 | return (-1); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2488 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2489 | if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2490 | facet->type = XML_SCHEMA_FACET_MININCLUSIVE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2491 | } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2492 | facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2493 | } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2494 | facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2495 | } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2496 | facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2497 | } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2498 | facet->type = XML_SCHEMA_FACET_TOTALDIGITS; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2499 | } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2500 | facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2501 | } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2502 | facet->type = XML_SCHEMA_FACET_PATTERN; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2503 | } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2504 | facet->type = XML_SCHEMA_FACET_ENUMERATION; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2505 | } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2506 | facet->type = XML_SCHEMA_FACET_WHITESPACE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2507 | } else if (xmlStrEqual(facetname, BAD_CAST "length")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2508 | facet->type = XML_SCHEMA_FACET_LENGTH; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2509 | } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2510 | facet->type = XML_SCHEMA_FACET_MAXLENGTH; |
| 2511 | } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) { |
| 2512 | facet->type = XML_SCHEMA_FACET_MINLENGTH; |
| 2513 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2514 | xmlSchemaFreeFacet(facet); |
| 2515 | return (-1); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2516 | } |
Daniel Veillard | 6dc9196 | 2004-03-22 19:10:02 +0000 | [diff] [blame] | 2517 | facet->value = val; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2518 | ret = xmlSchemaCheckFacet(facet, typ, NULL, type); |
| 2519 | if (ret != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2520 | xmlSchemaFreeFacet(facet); |
| 2521 | return (-1); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2522 | } |
| 2523 | ret = xmlSchemaValidateFacet(typ, facet, strval, value); |
| 2524 | xmlSchemaFreeFacet(facet); |
| 2525 | if (ret != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2526 | return (-1); |
| 2527 | return (0); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2528 | } |
| 2529 | |
| 2530 | /** |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2531 | * xmlRelaxNGSchemaFreeValue: |
| 2532 | * @data: data needed for the library |
| 2533 | * @value: the value to free |
| 2534 | * |
| 2535 | * Function provided by a type library to free a Schemas value |
| 2536 | * |
| 2537 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2538 | */ |
| 2539 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2540 | xmlRelaxNGSchemaFreeValue(void *data ATTRIBUTE_UNUSED, void *value) |
| 2541 | { |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2542 | xmlSchemaFreeValue(value); |
| 2543 | } |
| 2544 | |
| 2545 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2546 | * xmlRelaxNGSchemaTypeCompare: |
| 2547 | * @data: data needed for the library |
| 2548 | * @type: the type name |
| 2549 | * @value1: the first value |
| 2550 | * @value2: the second value |
| 2551 | * |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2552 | * Compare two values for equality accordingly a type from the W3C XMLSchema |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2553 | * Datatype library. |
| 2554 | * |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2555 | * 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] | 2556 | */ |
| 2557 | static int |
| 2558 | xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2559 | const xmlChar * type, |
| 2560 | const xmlChar * value1, |
| 2561 | xmlNodePtr ctxt1, |
| 2562 | void *comp1, |
| 2563 | const xmlChar * value2, xmlNodePtr ctxt2) |
| 2564 | { |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2565 | int ret; |
| 2566 | xmlSchemaTypePtr typ; |
| 2567 | xmlSchemaValPtr res1 = NULL, res2 = NULL; |
| 2568 | |
| 2569 | if ((type == NULL) || (value1 == NULL) || (value2 == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2570 | return (-1); |
| 2571 | typ = xmlSchemaGetPredefinedType(type, |
| 2572 | BAD_CAST |
| 2573 | "http://www.w3.org/2001/XMLSchema"); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2574 | if (typ == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2575 | return (-1); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2576 | if (comp1 == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2577 | ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1); |
| 2578 | if (ret != 0) |
| 2579 | return (-1); |
| 2580 | if (res1 == NULL) |
| 2581 | return (-1); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2582 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2583 | res1 = (xmlSchemaValPtr) comp1; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2584 | } |
| 2585 | ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2586 | if (ret != 0) { |
Daniel Veillard | f464403 | 2005-06-13 11:41:31 +0000 | [diff] [blame] | 2587 | if ((comp1 == NULL) && (res1 != NULL)) |
| 2588 | xmlSchemaFreeValue(res1); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2589 | return (-1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2590 | } |
| 2591 | if (res1 == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2592 | return (-1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2593 | } |
| 2594 | ret = xmlSchemaCompareValues(res1, res2); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2595 | if (res1 != (xmlSchemaValPtr) comp1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2596 | xmlSchemaFreeValue(res1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2597 | xmlSchemaFreeValue(res2); |
| 2598 | if (ret == -2) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2599 | return (-1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2600 | if (ret == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2601 | return (1); |
| 2602 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2603 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2604 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2605 | /** |
| 2606 | * xmlRelaxNGDefaultTypeHave: |
| 2607 | * @data: data needed for the library |
| 2608 | * @type: the type name |
| 2609 | * |
| 2610 | * Check if the given type is provided by |
| 2611 | * the default datatype library. |
| 2612 | * |
| 2613 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2614 | */ |
| 2615 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2616 | xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED, |
| 2617 | const xmlChar * type) |
| 2618 | { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2619 | if (type == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2620 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2621 | if (xmlStrEqual(type, BAD_CAST "string")) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2622 | return (1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2623 | if (xmlStrEqual(type, BAD_CAST "token")) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2624 | return (1); |
| 2625 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2626 | } |
| 2627 | |
| 2628 | /** |
| 2629 | * xmlRelaxNGDefaultTypeCheck: |
| 2630 | * @data: data needed for the library |
| 2631 | * @type: the type name |
| 2632 | * @value: the value to check |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2633 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2634 | * |
| 2635 | * Check if the given type and value are validated by |
| 2636 | * the default datatype library. |
| 2637 | * |
| 2638 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2639 | */ |
| 2640 | static int |
| 2641 | xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2642 | const xmlChar * type ATTRIBUTE_UNUSED, |
| 2643 | const xmlChar * value ATTRIBUTE_UNUSED, |
| 2644 | void **result ATTRIBUTE_UNUSED, |
| 2645 | xmlNodePtr node ATTRIBUTE_UNUSED) |
| 2646 | { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2647 | if (value == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2648 | return (-1); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2649 | if (xmlStrEqual(type, BAD_CAST "string")) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2650 | return (1); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2651 | if (xmlStrEqual(type, BAD_CAST "token")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2652 | return (1); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2653 | } |
| 2654 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2655 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2656 | } |
| 2657 | |
| 2658 | /** |
| 2659 | * xmlRelaxNGDefaultTypeCompare: |
| 2660 | * @data: data needed for the library |
| 2661 | * @type: the type name |
| 2662 | * @value1: the first value |
| 2663 | * @value2: the second value |
| 2664 | * |
| 2665 | * Compare two values accordingly a type from the default |
| 2666 | * datatype library. |
| 2667 | * |
| 2668 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2669 | */ |
| 2670 | static int |
| 2671 | xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2672 | const xmlChar * type, |
| 2673 | const xmlChar * value1, |
| 2674 | xmlNodePtr ctxt1 ATTRIBUTE_UNUSED, |
| 2675 | void *comp1 ATTRIBUTE_UNUSED, |
| 2676 | const xmlChar * value2, |
| 2677 | xmlNodePtr ctxt2 ATTRIBUTE_UNUSED) |
| 2678 | { |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2679 | int ret = -1; |
| 2680 | |
| 2681 | if (xmlStrEqual(type, BAD_CAST "string")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2682 | ret = xmlStrEqual(value1, value2); |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2683 | } else if (xmlStrEqual(type, BAD_CAST "token")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2684 | if (!xmlStrEqual(value1, value2)) { |
| 2685 | xmlChar *nval, *nvalue; |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2686 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2687 | /* |
| 2688 | * TODO: trivial optimizations are possible by |
| 2689 | * computing at compile-time |
| 2690 | */ |
| 2691 | nval = xmlRelaxNGNormalize(NULL, value1); |
| 2692 | nvalue = xmlRelaxNGNormalize(NULL, value2); |
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 | if ((nval == NULL) || (nvalue == NULL)) |
| 2695 | ret = -1; |
| 2696 | else if (xmlStrEqual(nval, nvalue)) |
| 2697 | ret = 1; |
| 2698 | else |
| 2699 | ret = 0; |
| 2700 | if (nval != NULL) |
| 2701 | xmlFree(nval); |
| 2702 | if (nvalue != NULL) |
| 2703 | xmlFree(nvalue); |
| 2704 | } else |
| 2705 | ret = 1; |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2706 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2707 | return (ret); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2708 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2709 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2710 | static int xmlRelaxNGTypeInitialized = 0; |
| 2711 | static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL; |
| 2712 | |
| 2713 | /** |
| 2714 | * xmlRelaxNGFreeTypeLibrary: |
| 2715 | * @lib: the type library structure |
| 2716 | * @namespace: the URI bound to the library |
| 2717 | * |
| 2718 | * Free the structure associated to the type library |
| 2719 | */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2720 | static void |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2721 | xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2722 | const xmlChar * namespace ATTRIBUTE_UNUSED) |
| 2723 | { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2724 | if (lib == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2725 | return; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2726 | if (lib->namespace != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2727 | xmlFree((xmlChar *) lib->namespace); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2728 | xmlFree(lib); |
| 2729 | } |
| 2730 | |
| 2731 | /** |
| 2732 | * xmlRelaxNGRegisterTypeLibrary: |
| 2733 | * @namespace: the URI bound to the library |
| 2734 | * @data: data associated to the library |
| 2735 | * @have: the provide function |
| 2736 | * @check: the checking function |
| 2737 | * @comp: the comparison function |
| 2738 | * |
| 2739 | * Register a new type library |
| 2740 | * |
| 2741 | * Returns 0 in case of success and -1 in case of error. |
| 2742 | */ |
| 2743 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2744 | xmlRelaxNGRegisterTypeLibrary(const xmlChar * namespace, void *data, |
| 2745 | xmlRelaxNGTypeHave have, |
| 2746 | xmlRelaxNGTypeCheck check, |
| 2747 | xmlRelaxNGTypeCompare comp, |
| 2748 | xmlRelaxNGFacetCheck facet, |
| 2749 | xmlRelaxNGTypeFree freef) |
| 2750 | { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2751 | xmlRelaxNGTypeLibraryPtr lib; |
| 2752 | int ret; |
| 2753 | |
| 2754 | if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2755 | (check == NULL) || (comp == NULL)) |
| 2756 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2757 | if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2758 | xmlGenericError(xmlGenericErrorContext, |
| 2759 | "Relax-NG types library '%s' already registered\n", |
| 2760 | namespace); |
| 2761 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2762 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2763 | lib = |
| 2764 | (xmlRelaxNGTypeLibraryPtr) |
| 2765 | xmlMalloc(sizeof(xmlRelaxNGTypeLibrary)); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2766 | if (lib == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2767 | xmlRngVErrMemory(NULL, "adding types library\n"); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2768 | return (-1); |
| 2769 | } |
| 2770 | memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary)); |
| 2771 | lib->namespace = xmlStrdup(namespace); |
| 2772 | lib->data = data; |
| 2773 | lib->have = have; |
| 2774 | lib->comp = comp; |
| 2775 | lib->check = check; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2776 | lib->facet = facet; |
| 2777 | lib->freef = freef; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2778 | ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib); |
| 2779 | if (ret < 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2780 | xmlGenericError(xmlGenericErrorContext, |
| 2781 | "Relax-NG types library failed to register '%s'\n", |
| 2782 | namespace); |
| 2783 | xmlRelaxNGFreeTypeLibrary(lib, namespace); |
| 2784 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2785 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2786 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2787 | } |
| 2788 | |
| 2789 | /** |
| 2790 | * xmlRelaxNGInitTypes: |
| 2791 | * |
| 2792 | * Initilize the default type libraries. |
| 2793 | * |
| 2794 | * Returns 0 in case of success and -1 in case of error. |
| 2795 | */ |
Daniel Veillard | dd6d300 | 2004-11-03 14:20:29 +0000 | [diff] [blame] | 2796 | int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2797 | xmlRelaxNGInitTypes(void) |
| 2798 | { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2799 | if (xmlRelaxNGTypeInitialized != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2800 | return (0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2801 | xmlRelaxNGRegisteredTypes = xmlHashCreate(10); |
| 2802 | if (xmlRelaxNGRegisteredTypes == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2803 | xmlGenericError(xmlGenericErrorContext, |
| 2804 | "Failed to allocate sh table for Relax-NG types\n"); |
| 2805 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2806 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2807 | xmlRelaxNGRegisterTypeLibrary(BAD_CAST |
| 2808 | "http://www.w3.org/2001/XMLSchema-datatypes", |
| 2809 | NULL, xmlRelaxNGSchemaTypeHave, |
| 2810 | xmlRelaxNGSchemaTypeCheck, |
| 2811 | xmlRelaxNGSchemaTypeCompare, |
| 2812 | xmlRelaxNGSchemaFacetCheck, |
| 2813 | xmlRelaxNGSchemaFreeValue); |
| 2814 | xmlRelaxNGRegisterTypeLibrary(xmlRelaxNGNs, NULL, |
| 2815 | xmlRelaxNGDefaultTypeHave, |
| 2816 | xmlRelaxNGDefaultTypeCheck, |
| 2817 | xmlRelaxNGDefaultTypeCompare, NULL, |
| 2818 | NULL); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2819 | xmlRelaxNGTypeInitialized = 1; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2820 | return (0); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2821 | } |
| 2822 | |
| 2823 | /** |
| 2824 | * xmlRelaxNGCleanupTypes: |
| 2825 | * |
| 2826 | * Cleanup the default Schemas type library associated to RelaxNG |
| 2827 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2828 | void |
| 2829 | xmlRelaxNGCleanupTypes(void) |
| 2830 | { |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 2831 | xmlSchemaCleanupTypes(); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2832 | if (xmlRelaxNGTypeInitialized == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2833 | return; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2834 | xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2835 | xmlRelaxNGFreeTypeLibrary); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2836 | xmlRelaxNGTypeInitialized = 0; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2837 | } |
| 2838 | |
| 2839 | /************************************************************************ |
| 2840 | * * |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2841 | * Compiling element content into regexp * |
| 2842 | * * |
| 2843 | * Sometime the element content can be compiled into a pure regexp, * |
| 2844 | * This allows a faster execution and streamability at that level * |
| 2845 | * * |
| 2846 | ************************************************************************/ |
| 2847 | |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2848 | static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, |
| 2849 | xmlRelaxNGDefinePtr def); |
| 2850 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2851 | /** |
| 2852 | * xmlRelaxNGIsCompileable: |
| 2853 | * @define: the definition to check |
| 2854 | * |
| 2855 | * Check if a definition is nullable. |
| 2856 | * |
| 2857 | * Returns 1 if yes, 0 if no and -1 in case of error |
| 2858 | */ |
| 2859 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2860 | xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def) |
| 2861 | { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2862 | int ret = -1; |
| 2863 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2864 | if (def == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2865 | return (-1); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2866 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2867 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 2868 | (def->dflags & IS_COMPILABLE)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2869 | return (1); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2870 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 2871 | (def->dflags & IS_NOT_COMPILABLE)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2872 | return (0); |
| 2873 | switch (def->type) { |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2874 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2875 | ret = xmlRelaxNGIsCompileable(def->content); |
| 2876 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2877 | case XML_RELAXNG_TEXT: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2878 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2879 | ret = 1; |
| 2880 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2881 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2882 | /* |
| 2883 | * Check if the element content is compileable |
| 2884 | */ |
| 2885 | if (((def->dflags & IS_NOT_COMPILABLE) == 0) && |
| 2886 | ((def->dflags & IS_COMPILABLE) == 0)) { |
| 2887 | xmlRelaxNGDefinePtr list; |
| 2888 | |
| 2889 | list = def->content; |
| 2890 | while (list != NULL) { |
| 2891 | ret = xmlRelaxNGIsCompileable(list); |
| 2892 | if (ret != 1) |
| 2893 | break; |
| 2894 | list = list->next; |
| 2895 | } |
William M. Brack | 6092962 | 2004-03-27 17:54:18 +0000 | [diff] [blame] | 2896 | /* |
| 2897 | * Because the routine is recursive, we must guard against |
| 2898 | * discovering both COMPILABLE and NOT_COMPILABLE |
| 2899 | */ |
| 2900 | if (ret == 0) { |
| 2901 | def->dflags &= ~IS_COMPILABLE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2902 | def->dflags |= IS_NOT_COMPILABLE; |
William M. Brack | 6092962 | 2004-03-27 17:54:18 +0000 | [diff] [blame] | 2903 | } |
| 2904 | if ((ret == 1) && !(def->dflags &= IS_NOT_COMPILABLE)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2905 | def->dflags |= IS_COMPILABLE; |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2906 | #ifdef DEBUG_COMPILE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2907 | if (ret == 1) { |
| 2908 | xmlGenericError(xmlGenericErrorContext, |
| 2909 | "element content for %s is compilable\n", |
| 2910 | def->name); |
| 2911 | } else if (ret == 0) { |
| 2912 | xmlGenericError(xmlGenericErrorContext, |
| 2913 | "element content for %s is not compilable\n", |
| 2914 | def->name); |
| 2915 | } else { |
| 2916 | xmlGenericError(xmlGenericErrorContext, |
| 2917 | "Problem in RelaxNGIsCompileable for element %s\n", |
| 2918 | def->name); |
| 2919 | } |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2920 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2921 | } |
| 2922 | /* |
| 2923 | * All elements return a compileable status unless they |
| 2924 | * are generic like anyName |
| 2925 | */ |
| 2926 | if ((def->nameClass != NULL) || (def->name == NULL)) |
| 2927 | ret = 0; |
| 2928 | else |
| 2929 | ret = 1; |
| 2930 | return (ret); |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2931 | case XML_RELAXNG_REF: |
| 2932 | case XML_RELAXNG_EXTERNALREF: |
| 2933 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2934 | if (def->depth == -20) { |
| 2935 | return (1); |
| 2936 | } else { |
| 2937 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2938 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2939 | def->depth = -20; |
| 2940 | list = def->content; |
| 2941 | while (list != NULL) { |
| 2942 | ret = xmlRelaxNGIsCompileable(list); |
| 2943 | if (ret != 1) |
| 2944 | break; |
| 2945 | list = list->next; |
| 2946 | } |
| 2947 | } |
| 2948 | break; |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2949 | case XML_RELAXNG_START: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2950 | case XML_RELAXNG_OPTIONAL: |
| 2951 | case XML_RELAXNG_ZEROORMORE: |
| 2952 | case XML_RELAXNG_ONEORMORE: |
| 2953 | case XML_RELAXNG_CHOICE: |
| 2954 | case XML_RELAXNG_GROUP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2955 | case XML_RELAXNG_DEF:{ |
| 2956 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2957 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2958 | list = def->content; |
| 2959 | while (list != NULL) { |
| 2960 | ret = xmlRelaxNGIsCompileable(list); |
| 2961 | if (ret != 1) |
| 2962 | break; |
| 2963 | list = list->next; |
| 2964 | } |
| 2965 | break; |
| 2966 | } |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2967 | case XML_RELAXNG_EXCEPT: |
| 2968 | case XML_RELAXNG_ATTRIBUTE: |
| 2969 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2970 | case XML_RELAXNG_DATATYPE: |
| 2971 | case XML_RELAXNG_LIST: |
| 2972 | case XML_RELAXNG_PARAM: |
| 2973 | case XML_RELAXNG_VALUE: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2974 | case XML_RELAXNG_NOT_ALLOWED: |
William M. Brack | 7e29c0a | 2004-04-02 09:07:22 +0000 | [diff] [blame] | 2975 | ret = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2976 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2977 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2978 | if (ret == 0) |
| 2979 | def->dflags |= IS_NOT_COMPILABLE; |
| 2980 | if (ret == 1) |
| 2981 | def->dflags |= IS_COMPILABLE; |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2982 | #ifdef DEBUG_COMPILE |
| 2983 | if (ret == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2984 | xmlGenericError(xmlGenericErrorContext, |
| 2985 | "RelaxNGIsCompileable %s : true\n", |
| 2986 | xmlRelaxNGDefName(def)); |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2987 | } else if (ret == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2988 | xmlGenericError(xmlGenericErrorContext, |
| 2989 | "RelaxNGIsCompileable %s : false\n", |
| 2990 | xmlRelaxNGDefName(def)); |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2991 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2992 | xmlGenericError(xmlGenericErrorContext, |
| 2993 | "Problem in RelaxNGIsCompileable %s\n", |
| 2994 | xmlRelaxNGDefName(def)); |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2995 | } |
| 2996 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 2997 | return (ret); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2998 | } |
| 2999 | |
| 3000 | /** |
| 3001 | * xmlRelaxNGCompile: |
| 3002 | * ctxt: the RelaxNG parser context |
| 3003 | * @define: the definition tree to compile |
| 3004 | * |
| 3005 | * Compile the set of definitions, it works recursively, till the |
| 3006 | * element boundaries, where it tries to compile the content if possible |
| 3007 | * |
| 3008 | * Returns 0 if success and -1 in case of error |
| 3009 | */ |
| 3010 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3011 | xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) |
| 3012 | { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3013 | int ret = 0; |
| 3014 | xmlRelaxNGDefinePtr list; |
| 3015 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3016 | if ((ctxt == NULL) || (def == NULL)) |
| 3017 | return (-1); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3018 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3019 | switch (def->type) { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3020 | case XML_RELAXNG_START: |
| 3021 | if ((xmlRelaxNGIsCompileable(def) == 1) && (def->depth != -25)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3022 | xmlAutomataPtr oldam = ctxt->am; |
| 3023 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3024 | |
| 3025 | def->depth = -25; |
| 3026 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3027 | list = def->content; |
| 3028 | ctxt->am = xmlNewAutomata(); |
| 3029 | if (ctxt->am == NULL) |
| 3030 | return (-1); |
| 3031 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
| 3032 | while (list != NULL) { |
| 3033 | xmlRelaxNGCompile(ctxt, list); |
| 3034 | list = list->next; |
| 3035 | } |
| 3036 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
| 3037 | def->contModel = xmlAutomataCompile(ctxt->am); |
| 3038 | xmlRegexpIsDeterminist(def->contModel); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3039 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3040 | xmlFreeAutomata(ctxt->am); |
| 3041 | ctxt->state = oldstate; |
| 3042 | ctxt->am = oldam; |
| 3043 | } |
| 3044 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3045 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3046 | if ((ctxt->am != NULL) && (def->name != NULL)) { |
| 3047 | ctxt->state = xmlAutomataNewTransition2(ctxt->am, |
| 3048 | ctxt->state, NULL, |
| 3049 | def->name, def->ns, |
| 3050 | def); |
| 3051 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3052 | if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3053 | xmlAutomataPtr oldam = ctxt->am; |
| 3054 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3055 | |
| 3056 | def->depth = -25; |
| 3057 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3058 | list = def->content; |
| 3059 | ctxt->am = xmlNewAutomata(); |
| 3060 | if (ctxt->am == NULL) |
| 3061 | return (-1); |
| 3062 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
| 3063 | while (list != NULL) { |
| 3064 | xmlRelaxNGCompile(ctxt, list); |
| 3065 | list = list->next; |
| 3066 | } |
| 3067 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
| 3068 | def->contModel = xmlAutomataCompile(ctxt->am); |
| 3069 | if (!xmlRegexpIsDeterminist(def->contModel)) { |
| 3070 | /* |
| 3071 | * we can only use the automata if it is determinist |
| 3072 | */ |
| 3073 | xmlRegFreeRegexp(def->contModel); |
| 3074 | def->contModel = NULL; |
| 3075 | } |
| 3076 | xmlFreeAutomata(ctxt->am); |
| 3077 | ctxt->state = oldstate; |
| 3078 | ctxt->am = oldam; |
| 3079 | } else { |
| 3080 | xmlAutomataPtr oldam = ctxt->am; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3081 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3082 | /* |
| 3083 | * we can't build the content model for this element content |
| 3084 | * but it still might be possible to build it for some of its |
| 3085 | * children, recurse. |
| 3086 | */ |
| 3087 | ret = xmlRelaxNGTryCompile(ctxt, def); |
| 3088 | ctxt->am = oldam; |
| 3089 | } |
| 3090 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3091 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3092 | ret = xmlRelaxNGCompile(ctxt, def->content); |
| 3093 | break; |
| 3094 | case XML_RELAXNG_OPTIONAL:{ |
| 3095 | xmlAutomataStatePtr oldstate = ctxt->state; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3096 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3097 | xmlRelaxNGCompile(ctxt, def->content); |
| 3098 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
| 3099 | break; |
| 3100 | } |
| 3101 | case XML_RELAXNG_ZEROORMORE:{ |
| 3102 | xmlAutomataStatePtr oldstate; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3103 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3104 | ctxt->state = |
| 3105 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 3106 | oldstate = ctxt->state; |
| 3107 | list = def->content; |
| 3108 | while (list != NULL) { |
| 3109 | xmlRelaxNGCompile(ctxt, list); |
| 3110 | list = list->next; |
| 3111 | } |
| 3112 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate); |
| 3113 | ctxt->state = |
| 3114 | xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 3115 | break; |
| 3116 | } |
| 3117 | case XML_RELAXNG_ONEORMORE:{ |
| 3118 | xmlAutomataStatePtr oldstate; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3119 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3120 | list = def->content; |
| 3121 | while (list != NULL) { |
| 3122 | xmlRelaxNGCompile(ctxt, list); |
| 3123 | list = list->next; |
| 3124 | } |
| 3125 | oldstate = ctxt->state; |
| 3126 | list = def->content; |
| 3127 | while (list != NULL) { |
| 3128 | xmlRelaxNGCompile(ctxt, list); |
| 3129 | list = list->next; |
| 3130 | } |
| 3131 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate); |
| 3132 | ctxt->state = |
| 3133 | xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 3134 | break; |
| 3135 | } |
| 3136 | case XML_RELAXNG_CHOICE:{ |
| 3137 | xmlAutomataStatePtr target = NULL; |
| 3138 | xmlAutomataStatePtr oldstate = ctxt->state; |
| 3139 | |
| 3140 | list = def->content; |
| 3141 | while (list != NULL) { |
| 3142 | ctxt->state = oldstate; |
| 3143 | ret = xmlRelaxNGCompile(ctxt, list); |
| 3144 | if (ret != 0) |
| 3145 | break; |
| 3146 | if (target == NULL) |
| 3147 | target = ctxt->state; |
| 3148 | else { |
| 3149 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, |
| 3150 | target); |
| 3151 | } |
| 3152 | list = list->next; |
| 3153 | } |
| 3154 | ctxt->state = target; |
| 3155 | |
| 3156 | break; |
| 3157 | } |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3158 | case XML_RELAXNG_REF: |
| 3159 | case XML_RELAXNG_EXTERNALREF: |
| 3160 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3161 | case XML_RELAXNG_GROUP: |
| 3162 | case XML_RELAXNG_DEF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3163 | list = def->content; |
| 3164 | while (list != NULL) { |
| 3165 | ret = xmlRelaxNGCompile(ctxt, list); |
| 3166 | if (ret != 0) |
| 3167 | break; |
| 3168 | list = list->next; |
| 3169 | } |
| 3170 | break; |
| 3171 | case XML_RELAXNG_TEXT:{ |
| 3172 | xmlAutomataStatePtr oldstate; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3173 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3174 | ctxt->state = |
| 3175 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 3176 | oldstate = ctxt->state; |
| 3177 | xmlRelaxNGCompile(ctxt, def->content); |
| 3178 | xmlAutomataNewTransition(ctxt->am, ctxt->state, |
| 3179 | ctxt->state, BAD_CAST "#text", |
| 3180 | NULL); |
| 3181 | ctxt->state = |
| 3182 | xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 3183 | break; |
| 3184 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3185 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3186 | ctxt->state = |
| 3187 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 3188 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3189 | case XML_RELAXNG_EXCEPT: |
| 3190 | case XML_RELAXNG_ATTRIBUTE: |
| 3191 | case XML_RELAXNG_INTERLEAVE: |
| 3192 | case XML_RELAXNG_NOT_ALLOWED: |
| 3193 | case XML_RELAXNG_DATATYPE: |
| 3194 | case XML_RELAXNG_LIST: |
| 3195 | case XML_RELAXNG_PARAM: |
| 3196 | case XML_RELAXNG_VALUE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3197 | /* This should not happen and generate an internal error */ |
| 3198 | fprintf(stderr, "RNG internal error trying to compile %s\n", |
| 3199 | xmlRelaxNGDefName(def)); |
| 3200 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3201 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3202 | return (ret); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3203 | } |
| 3204 | |
| 3205 | /** |
| 3206 | * xmlRelaxNGTryCompile: |
| 3207 | * ctxt: the RelaxNG parser context |
| 3208 | * @define: the definition tree to compile |
| 3209 | * |
| 3210 | * Try to compile the set of definitions, it works recursively, |
| 3211 | * possibly ignoring parts which cannot be compiled. |
| 3212 | * |
| 3213 | * Returns 0 if success and -1 in case of error |
| 3214 | */ |
| 3215 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3216 | xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) |
| 3217 | { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3218 | int ret = 0; |
| 3219 | xmlRelaxNGDefinePtr list; |
| 3220 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3221 | if ((ctxt == NULL) || (def == NULL)) |
| 3222 | return (-1); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3223 | |
| 3224 | if ((def->type == XML_RELAXNG_START) || |
| 3225 | (def->type == XML_RELAXNG_ELEMENT)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3226 | ret = xmlRelaxNGIsCompileable(def); |
| 3227 | if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) { |
| 3228 | ctxt->am = NULL; |
| 3229 | ret = xmlRelaxNGCompile(ctxt, def); |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3230 | #ifdef DEBUG_PROGRESSIVE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3231 | if (ret == 0) { |
| 3232 | if (def->type == XML_RELAXNG_START) |
| 3233 | xmlGenericError(xmlGenericErrorContext, |
| 3234 | "compiled the start\n"); |
| 3235 | else |
| 3236 | xmlGenericError(xmlGenericErrorContext, |
| 3237 | "compiled element %s\n", def->name); |
| 3238 | } else { |
| 3239 | if (def->type == XML_RELAXNG_START) |
| 3240 | xmlGenericError(xmlGenericErrorContext, |
| 3241 | "failed to compile the start\n"); |
| 3242 | else |
| 3243 | xmlGenericError(xmlGenericErrorContext, |
| 3244 | "failed to compile element %s\n", |
| 3245 | def->name); |
| 3246 | } |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3247 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3248 | return (ret); |
| 3249 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3250 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3251 | switch (def->type) { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3252 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3253 | ret = xmlRelaxNGTryCompile(ctxt, def->content); |
| 3254 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3255 | case XML_RELAXNG_TEXT: |
| 3256 | case XML_RELAXNG_DATATYPE: |
| 3257 | case XML_RELAXNG_LIST: |
| 3258 | case XML_RELAXNG_PARAM: |
| 3259 | case XML_RELAXNG_VALUE: |
| 3260 | case XML_RELAXNG_EMPTY: |
| 3261 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3262 | ret = 0; |
| 3263 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3264 | case XML_RELAXNG_OPTIONAL: |
| 3265 | case XML_RELAXNG_ZEROORMORE: |
| 3266 | case XML_RELAXNG_ONEORMORE: |
| 3267 | case XML_RELAXNG_CHOICE: |
| 3268 | case XML_RELAXNG_GROUP: |
| 3269 | case XML_RELAXNG_DEF: |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3270 | case XML_RELAXNG_START: |
| 3271 | case XML_RELAXNG_REF: |
| 3272 | case XML_RELAXNG_EXTERNALREF: |
| 3273 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3274 | list = def->content; |
| 3275 | while (list != NULL) { |
| 3276 | ret = xmlRelaxNGTryCompile(ctxt, list); |
| 3277 | if (ret != 0) |
| 3278 | break; |
| 3279 | list = list->next; |
| 3280 | } |
| 3281 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3282 | case XML_RELAXNG_EXCEPT: |
| 3283 | case XML_RELAXNG_ATTRIBUTE: |
| 3284 | case XML_RELAXNG_INTERLEAVE: |
| 3285 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3286 | ret = 0; |
| 3287 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3288 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3289 | return (ret); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 3290 | } |
| 3291 | |
| 3292 | /************************************************************************ |
| 3293 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3294 | * Parsing functions * |
| 3295 | * * |
| 3296 | ************************************************************************/ |
| 3297 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3298 | static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr |
| 3299 | ctxt, xmlNodePtr node); |
| 3300 | static xmlRelaxNGDefinePtr xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr |
| 3301 | ctxt, xmlNodePtr node); |
| 3302 | static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr |
| 3303 | ctxt, xmlNodePtr nodes, |
| 3304 | int group); |
| 3305 | static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr |
| 3306 | ctxt, xmlNodePtr node); |
| 3307 | static xmlRelaxNGPtr xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, |
| 3308 | xmlNodePtr node); |
| 3309 | static int xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt, |
| 3310 | xmlNodePtr nodes); |
| 3311 | static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr |
| 3312 | ctxt, xmlNodePtr node, |
| 3313 | xmlRelaxNGDefinePtr |
| 3314 | def); |
| 3315 | static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr |
| 3316 | ctxt, xmlNodePtr nodes); |
| 3317 | static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 3318 | xmlRelaxNGDefinePtr define, |
| 3319 | xmlNodePtr elem); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3320 | |
| 3321 | |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 3322 | #define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content)) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3323 | |
| 3324 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3325 | * xmlRelaxNGIsNullable: |
| 3326 | * @define: the definition to verify |
| 3327 | * |
| 3328 | * Check if a definition is nullable. |
| 3329 | * |
| 3330 | * Returns 1 if yes, 0 if no and -1 in case of error |
| 3331 | */ |
| 3332 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3333 | xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define) |
| 3334 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3335 | int ret; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3336 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3337 | if (define == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3338 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3339 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3340 | if (define->dflags & IS_NULLABLE) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3341 | return (1); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3342 | if (define->dflags & IS_NOT_NULLABLE) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3343 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3344 | switch (define->type) { |
| 3345 | case XML_RELAXNG_EMPTY: |
| 3346 | case XML_RELAXNG_TEXT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3347 | ret = 1; |
| 3348 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3349 | case XML_RELAXNG_NOOP: |
| 3350 | case XML_RELAXNG_DEF: |
| 3351 | case XML_RELAXNG_REF: |
| 3352 | case XML_RELAXNG_EXTERNALREF: |
| 3353 | case XML_RELAXNG_PARENTREF: |
| 3354 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3355 | ret = xmlRelaxNGIsNullable(define->content); |
| 3356 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3357 | case XML_RELAXNG_EXCEPT: |
| 3358 | case XML_RELAXNG_NOT_ALLOWED: |
| 3359 | case XML_RELAXNG_ELEMENT: |
| 3360 | case XML_RELAXNG_DATATYPE: |
| 3361 | case XML_RELAXNG_PARAM: |
| 3362 | case XML_RELAXNG_VALUE: |
| 3363 | case XML_RELAXNG_LIST: |
| 3364 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3365 | ret = 0; |
| 3366 | break; |
| 3367 | case XML_RELAXNG_CHOICE:{ |
| 3368 | xmlRelaxNGDefinePtr list = define->content; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3369 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3370 | while (list != NULL) { |
| 3371 | ret = xmlRelaxNGIsNullable(list); |
| 3372 | if (ret != 0) |
| 3373 | goto done; |
| 3374 | list = list->next; |
| 3375 | } |
| 3376 | ret = 0; |
| 3377 | break; |
| 3378 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3379 | case XML_RELAXNG_START: |
| 3380 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3381 | case XML_RELAXNG_GROUP:{ |
| 3382 | xmlRelaxNGDefinePtr list = define->content; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3383 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3384 | while (list != NULL) { |
| 3385 | ret = xmlRelaxNGIsNullable(list); |
| 3386 | if (ret != 1) |
| 3387 | goto done; |
| 3388 | list = list->next; |
| 3389 | } |
| 3390 | return (1); |
| 3391 | } |
| 3392 | default: |
| 3393 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3394 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3395 | done: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3396 | if (ret == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3397 | define->dflags |= IS_NOT_NULLABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3398 | if (ret == 1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3399 | define->dflags |= IS_NULLABLE; |
| 3400 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3401 | } |
| 3402 | |
| 3403 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3404 | * xmlRelaxNGIsBlank: |
| 3405 | * @str: a string |
| 3406 | * |
| 3407 | * Check if a string is ignorable c.f. 4.2. Whitespace |
| 3408 | * |
| 3409 | * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise |
| 3410 | */ |
| 3411 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3412 | xmlRelaxNGIsBlank(xmlChar * str) |
| 3413 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3414 | if (str == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3415 | return (1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3416 | while (*str != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3417 | if (!(IS_BLANK_CH(*str))) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3418 | return (0); |
| 3419 | str++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3420 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3421 | return (1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3422 | } |
| 3423 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3424 | /** |
| 3425 | * xmlRelaxNGGetDataTypeLibrary: |
| 3426 | * @ctxt: a Relax-NG parser context |
| 3427 | * @node: the current data or value element |
| 3428 | * |
| 3429 | * Applies algorithm from 4.3. datatypeLibrary attribute |
| 3430 | * |
| 3431 | * Returns the datatypeLibary value or NULL if not found |
| 3432 | */ |
| 3433 | static xmlChar * |
| 3434 | xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3435 | xmlNodePtr node) |
| 3436 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3437 | xmlChar *ret, *escape; |
| 3438 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3439 | if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3440 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
| 3441 | if (ret != NULL) { |
| 3442 | if (ret[0] == 0) { |
| 3443 | xmlFree(ret); |
| 3444 | return (NULL); |
| 3445 | } |
| 3446 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
| 3447 | if (escape == NULL) { |
| 3448 | return (ret); |
| 3449 | } |
| 3450 | xmlFree(ret); |
| 3451 | return (escape); |
| 3452 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3453 | } |
| 3454 | node = node->parent; |
| 3455 | while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3456 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
| 3457 | if (ret != NULL) { |
| 3458 | if (ret[0] == 0) { |
| 3459 | xmlFree(ret); |
| 3460 | return (NULL); |
| 3461 | } |
| 3462 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
| 3463 | if (escape == NULL) { |
| 3464 | return (ret); |
| 3465 | } |
| 3466 | xmlFree(ret); |
| 3467 | return (escape); |
| 3468 | } |
| 3469 | node = node->parent; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3470 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3471 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3472 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3473 | |
| 3474 | /** |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3475 | * xmlRelaxNGParseValue: |
| 3476 | * @ctxt: a Relax-NG parser context |
| 3477 | * @node: the data node. |
| 3478 | * |
| 3479 | * parse the content of a RelaxNG value node. |
| 3480 | * |
| 3481 | * Returns the definition pointer or NULL in case of error |
| 3482 | */ |
| 3483 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3484 | xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 3485 | { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3486 | xmlRelaxNGDefinePtr def = NULL; |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 3487 | xmlRelaxNGTypeLibraryPtr lib = NULL; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3488 | xmlChar *type; |
| 3489 | xmlChar *library; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3490 | int success = 0; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3491 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3492 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3493 | if (def == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3494 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3495 | def->type = XML_RELAXNG_VALUE; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3496 | |
| 3497 | type = xmlGetProp(node, BAD_CAST "type"); |
| 3498 | if (type != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3499 | xmlRelaxNGNormExtSpace(type); |
| 3500 | if (xmlValidateNCName(type, 0)) { |
| 3501 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE, |
| 3502 | "value type '%s' is not an NCName\n", type, NULL); |
| 3503 | } |
| 3504 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
| 3505 | if (library == NULL) |
| 3506 | library = |
| 3507 | xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3508 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3509 | def->name = type; |
| 3510 | def->ns = library; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3511 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3512 | lib = (xmlRelaxNGTypeLibraryPtr) |
| 3513 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
| 3514 | if (lib == NULL) { |
| 3515 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB, |
| 3516 | "Use of unregistered type library '%s'\n", library, |
| 3517 | NULL); |
| 3518 | def->data = NULL; |
| 3519 | } else { |
| 3520 | def->data = lib; |
| 3521 | if (lib->have == NULL) { |
| 3522 | xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB, |
| 3523 | "Internal error with type library '%s': no 'have'\n", |
| 3524 | library, NULL); |
| 3525 | } else { |
| 3526 | success = lib->have(lib->data, def->name); |
| 3527 | if (success != 1) { |
| 3528 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND, |
| 3529 | "Error type '%s' is not exported by type library '%s'\n", |
| 3530 | def->name, library); |
| 3531 | } |
| 3532 | } |
| 3533 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3534 | } |
| 3535 | if (node->children == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3536 | def->value = xmlStrdup(BAD_CAST ""); |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 3537 | } else if (((node->children->type != XML_TEXT_NODE) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3538 | (node->children->type != XML_CDATA_SECTION_NODE)) || |
| 3539 | (node->children->next != NULL)) { |
| 3540 | xmlRngPErr(ctxt, node, XML_RNGP_TEXT_EXPECTED, |
| 3541 | "Expecting a single text value for <value>content\n", |
| 3542 | NULL, NULL); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3543 | } else if (def != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3544 | def->value = xmlNodeGetContent(node); |
| 3545 | if (def->value == NULL) { |
| 3546 | xmlRngPErr(ctxt, node, XML_RNGP_VALUE_NO_CONTENT, |
| 3547 | "Element <value> has no content\n", NULL, NULL); |
| 3548 | } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) { |
| 3549 | void *val = NULL; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3550 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3551 | success = |
| 3552 | lib->check(lib->data, def->name, def->value, &val, node); |
| 3553 | if (success != 1) { |
| 3554 | xmlRngPErr(ctxt, node, XML_RNGP_INVALID_VALUE, |
| 3555 | "Value '%s' is not acceptable for type '%s'\n", |
| 3556 | def->value, def->name); |
| 3557 | } else { |
| 3558 | if (val != NULL) |
| 3559 | def->attrs = val; |
| 3560 | } |
| 3561 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3562 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3563 | return (def); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3564 | } |
| 3565 | |
| 3566 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3567 | * xmlRelaxNGParseData: |
| 3568 | * @ctxt: a Relax-NG parser context |
| 3569 | * @node: the data node. |
| 3570 | * |
| 3571 | * parse the content of a RelaxNG data node. |
| 3572 | * |
| 3573 | * Returns the definition pointer or NULL in case of error |
| 3574 | */ |
| 3575 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3576 | xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 3577 | { |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3578 | xmlRelaxNGDefinePtr def = NULL, except, last = NULL; |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 3579 | xmlRelaxNGDefinePtr param, lastparam = NULL; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3580 | xmlRelaxNGTypeLibraryPtr lib; |
| 3581 | xmlChar *type; |
| 3582 | xmlChar *library; |
| 3583 | xmlNodePtr content; |
| 3584 | int tmp; |
| 3585 | |
| 3586 | type = xmlGetProp(node, BAD_CAST "type"); |
| 3587 | if (type == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3588 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_MISSING, "data has no type\n", NULL, |
| 3589 | NULL); |
| 3590 | return (NULL); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3591 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3592 | xmlRelaxNGNormExtSpace(type); |
| 3593 | if (xmlValidateNCName(type, 0)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3594 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE, |
| 3595 | "data type '%s' is not an NCName\n", type, NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3596 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3597 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
| 3598 | if (library == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3599 | library = |
| 3600 | xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3601 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3602 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3603 | if (def == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3604 | xmlFree(type); |
| 3605 | return (NULL); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3606 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3607 | def->type = XML_RELAXNG_DATATYPE; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3608 | def->name = type; |
| 3609 | def->ns = library; |
| 3610 | |
| 3611 | lib = (xmlRelaxNGTypeLibraryPtr) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3612 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3613 | if (lib == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3614 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB, |
| 3615 | "Use of unregistered type library '%s'\n", library, |
| 3616 | NULL); |
| 3617 | def->data = NULL; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3618 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3619 | def->data = lib; |
| 3620 | if (lib->have == NULL) { |
| 3621 | xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB, |
| 3622 | "Internal error with type library '%s': no 'have'\n", |
| 3623 | library, NULL); |
| 3624 | } else { |
| 3625 | tmp = lib->have(lib->data, def->name); |
| 3626 | if (tmp != 1) { |
| 3627 | xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND, |
| 3628 | "Error type '%s' is not exported by type library '%s'\n", |
| 3629 | def->name, library); |
| 3630 | } else |
| 3631 | if ((xmlStrEqual |
| 3632 | (library, |
| 3633 | BAD_CAST |
| 3634 | "http://www.w3.org/2001/XMLSchema-datatypes")) |
| 3635 | && ((xmlStrEqual(def->name, BAD_CAST "IDREF")) |
| 3636 | || (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) { |
| 3637 | ctxt->idref = 1; |
| 3638 | } |
| 3639 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3640 | } |
| 3641 | content = node->children; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3642 | |
| 3643 | /* |
| 3644 | * Handle optional params |
| 3645 | */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3646 | while (content != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3647 | if (!xmlStrEqual(content->name, BAD_CAST "param")) |
| 3648 | break; |
| 3649 | if (xmlStrEqual(library, |
| 3650 | BAD_CAST "http://relaxng.org/ns/structure/1.0")) { |
| 3651 | xmlRngPErr(ctxt, node, XML_RNGP_PARAM_FORBIDDEN, |
| 3652 | "Type library '%s' does not allow type parameters\n", |
| 3653 | library, NULL); |
| 3654 | content = content->next; |
| 3655 | while ((content != NULL) && |
| 3656 | (xmlStrEqual(content->name, BAD_CAST "param"))) |
| 3657 | content = content->next; |
| 3658 | } else { |
| 3659 | param = xmlRelaxNGNewDefine(ctxt, node); |
| 3660 | if (param != NULL) { |
| 3661 | param->type = XML_RELAXNG_PARAM; |
| 3662 | param->name = xmlGetProp(content, BAD_CAST "name"); |
| 3663 | if (param->name == NULL) { |
| 3664 | xmlRngPErr(ctxt, node, XML_RNGP_PARAM_NAME_MISSING, |
| 3665 | "param has no name\n", NULL, NULL); |
| 3666 | } |
| 3667 | param->value = xmlNodeGetContent(content); |
| 3668 | if (lastparam == NULL) { |
| 3669 | def->attrs = lastparam = param; |
| 3670 | } else { |
| 3671 | lastparam->next = param; |
| 3672 | lastparam = param; |
| 3673 | } |
| 3674 | if (lib != NULL) { |
| 3675 | } |
| 3676 | } |
| 3677 | content = content->next; |
| 3678 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3679 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3680 | /* |
| 3681 | * Handle optional except |
| 3682 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3683 | if ((content != NULL) |
| 3684 | && (xmlStrEqual(content->name, BAD_CAST "except"))) { |
| 3685 | xmlNodePtr child; |
| 3686 | xmlRelaxNGDefinePtr tmp2, last2 = NULL; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3687 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3688 | except = xmlRelaxNGNewDefine(ctxt, node); |
| 3689 | if (except == NULL) { |
| 3690 | return (def); |
| 3691 | } |
| 3692 | except->type = XML_RELAXNG_EXCEPT; |
| 3693 | child = content->children; |
| 3694 | if (last == NULL) { |
| 3695 | def->content = except; |
| 3696 | } else { |
| 3697 | last->next = except; |
| 3698 | } |
| 3699 | if (child == NULL) { |
| 3700 | xmlRngPErr(ctxt, content, XML_RNGP_EXCEPT_NO_CONTENT, |
| 3701 | "except has no content\n", NULL, NULL); |
| 3702 | } |
| 3703 | while (child != NULL) { |
| 3704 | tmp2 = xmlRelaxNGParsePattern(ctxt, child); |
| 3705 | if (tmp2 != NULL) { |
| 3706 | if (last2 == NULL) { |
| 3707 | except->content = last2 = tmp2; |
| 3708 | } else { |
| 3709 | last2->next = tmp2; |
| 3710 | last2 = tmp2; |
| 3711 | } |
| 3712 | } |
| 3713 | child = child->next; |
| 3714 | } |
| 3715 | content = content->next; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3716 | } |
| 3717 | /* |
| 3718 | * Check there is no unhandled data |
| 3719 | */ |
| 3720 | if (content != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3721 | xmlRngPErr(ctxt, content, XML_RNGP_DATA_CONTENT, |
| 3722 | "Element data has unexpected content %s\n", |
| 3723 | content->name, NULL); |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3724 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3725 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3726 | return (def); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3727 | } |
| 3728 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3729 | static const xmlChar *invalidName = BAD_CAST "\1"; |
| 3730 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3731 | /** |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3732 | * xmlRelaxNGCompareNameClasses: |
| 3733 | * @defs1: the first element/attribute defs |
| 3734 | * @defs2: the second element/attribute defs |
| 3735 | * @name: the restriction on the name |
| 3736 | * @ns: the restriction on the namespace |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3737 | * |
| 3738 | * Compare the 2 lists of element definitions. The comparison is |
| 3739 | * that if both lists do not accept the same QNames, it returns 1 |
| 3740 | * If the 2 lists can accept the same QName the comparison returns 0 |
| 3741 | * |
| 3742 | * Returns 1 disttinct, 0 if equal |
| 3743 | */ |
| 3744 | static int |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3745 | xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3746 | xmlRelaxNGDefinePtr def2) |
| 3747 | { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3748 | int ret = 1; |
| 3749 | xmlNode node; |
| 3750 | xmlNs ns; |
| 3751 | xmlRelaxNGValidCtxt ctxt; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3752 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 3753 | memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt)); |
| 3754 | |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 3755 | ctxt.flags = FLAGS_IGNORABLE | FLAGS_NOERROR; |
| 3756 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3757 | if ((def1->type == XML_RELAXNG_ELEMENT) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3758 | (def1->type == XML_RELAXNG_ATTRIBUTE)) { |
| 3759 | if (def2->type == XML_RELAXNG_TEXT) |
| 3760 | return (1); |
| 3761 | if (def1->name != NULL) { |
| 3762 | node.name = def1->name; |
| 3763 | } else { |
| 3764 | node.name = invalidName; |
| 3765 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3766 | if (def1->ns != NULL) { |
| 3767 | if (def1->ns[0] == 0) { |
| 3768 | node.ns = NULL; |
| 3769 | } else { |
William M. Brack | a74a6ff | 2004-04-02 14:03:22 +0000 | [diff] [blame] | 3770 | node.ns = &ns; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3771 | ns.href = def1->ns; |
| 3772 | } |
| 3773 | } else { |
William M. Brack | a74a6ff | 2004-04-02 14:03:22 +0000 | [diff] [blame] | 3774 | node.ns = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3775 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3776 | if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3777 | if (def1->nameClass != NULL) { |
| 3778 | ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2); |
| 3779 | } else { |
| 3780 | ret = 0; |
| 3781 | } |
| 3782 | } else { |
| 3783 | ret = 1; |
| 3784 | } |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3785 | } else if (def1->type == XML_RELAXNG_TEXT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3786 | if (def2->type == XML_RELAXNG_TEXT) |
| 3787 | return (0); |
| 3788 | return (1); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3789 | } else if (def1->type == XML_RELAXNG_EXCEPT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3790 | TODO ret = 0; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3791 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3792 | TODO ret = 0; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3793 | } |
| 3794 | if (ret == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3795 | return (ret); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3796 | if ((def2->type == XML_RELAXNG_ELEMENT) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3797 | (def2->type == XML_RELAXNG_ATTRIBUTE)) { |
| 3798 | if (def2->name != NULL) { |
| 3799 | node.name = def2->name; |
| 3800 | } else { |
| 3801 | node.name = invalidName; |
| 3802 | } |
| 3803 | node.ns = &ns; |
| 3804 | if (def2->ns != NULL) { |
| 3805 | if (def2->ns[0] == 0) { |
| 3806 | node.ns = NULL; |
| 3807 | } else { |
| 3808 | ns.href = def2->ns; |
| 3809 | } |
| 3810 | } else { |
| 3811 | ns.href = invalidName; |
| 3812 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3813 | if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3814 | if (def2->nameClass != NULL) { |
| 3815 | ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1); |
| 3816 | } else { |
| 3817 | ret = 0; |
| 3818 | } |
| 3819 | } else { |
| 3820 | ret = 1; |
| 3821 | } |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3822 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3823 | TODO ret = 0; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3824 | } |
| 3825 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3826 | return (ret); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3827 | } |
| 3828 | |
| 3829 | /** |
| 3830 | * xmlRelaxNGCompareElemDefLists: |
| 3831 | * @ctxt: a Relax-NG parser context |
| 3832 | * @defs1: the first list of element/attribute defs |
| 3833 | * @defs2: the second list of element/attribute defs |
| 3834 | * |
| 3835 | * Compare the 2 lists of element or attribute definitions. The comparison |
| 3836 | * is that if both lists do not accept the same QNames, it returns 1 |
| 3837 | * If the 2 lists can accept the same QName the comparison returns 0 |
| 3838 | * |
| 3839 | * Returns 1 disttinct, 0 if equal |
| 3840 | */ |
| 3841 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3842 | xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt |
| 3843 | ATTRIBUTE_UNUSED, xmlRelaxNGDefinePtr * def1, |
| 3844 | xmlRelaxNGDefinePtr * def2) |
| 3845 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3846 | xmlRelaxNGDefinePtr *basedef2 = def2; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3847 | |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3848 | if ((def1 == NULL) || (def2 == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3849 | return (1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3850 | if ((*def1 == NULL) || (*def2 == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3851 | return (1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3852 | while (*def1 != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3853 | while ((*def2) != NULL) { |
| 3854 | if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0) |
| 3855 | return (0); |
| 3856 | def2++; |
| 3857 | } |
| 3858 | def2 = basedef2; |
| 3859 | def1++; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3860 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3861 | return (1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3862 | } |
| 3863 | |
| 3864 | /** |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3865 | * xmlRelaxNGGenerateAttributes: |
| 3866 | * @ctxt: a Relax-NG parser context |
| 3867 | * @def: the definition definition |
| 3868 | * |
| 3869 | * Check if the definition can only generate attributes |
| 3870 | * |
| 3871 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 3872 | */ |
| 3873 | static int |
| 3874 | xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3875 | xmlRelaxNGDefinePtr def) |
| 3876 | { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3877 | xmlRelaxNGDefinePtr parent, cur, tmp; |
| 3878 | |
| 3879 | /* |
| 3880 | * Don't run that check in case of error. Infinite recursion |
| 3881 | * becomes possible. |
| 3882 | */ |
| 3883 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3884 | return (-1); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3885 | |
| 3886 | parent = NULL; |
| 3887 | cur = def; |
| 3888 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3889 | if ((cur->type == XML_RELAXNG_ELEMENT) || |
| 3890 | (cur->type == XML_RELAXNG_TEXT) || |
| 3891 | (cur->type == XML_RELAXNG_DATATYPE) || |
| 3892 | (cur->type == XML_RELAXNG_PARAM) || |
| 3893 | (cur->type == XML_RELAXNG_LIST) || |
| 3894 | (cur->type == XML_RELAXNG_VALUE) || |
| 3895 | (cur->type == XML_RELAXNG_EMPTY)) |
| 3896 | return (0); |
| 3897 | if ((cur->type == XML_RELAXNG_CHOICE) || |
| 3898 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
| 3899 | (cur->type == XML_RELAXNG_GROUP) || |
| 3900 | (cur->type == XML_RELAXNG_ONEORMORE) || |
| 3901 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
| 3902 | (cur->type == XML_RELAXNG_OPTIONAL) || |
| 3903 | (cur->type == XML_RELAXNG_PARENTREF) || |
| 3904 | (cur->type == XML_RELAXNG_EXTERNALREF) || |
| 3905 | (cur->type == XML_RELAXNG_REF) || |
| 3906 | (cur->type == XML_RELAXNG_DEF)) { |
| 3907 | if (cur->content != NULL) { |
| 3908 | parent = cur; |
| 3909 | cur = cur->content; |
| 3910 | tmp = cur; |
| 3911 | while (tmp != NULL) { |
| 3912 | tmp->parent = parent; |
| 3913 | tmp = tmp->next; |
| 3914 | } |
| 3915 | continue; |
| 3916 | } |
| 3917 | } |
| 3918 | if (cur == def) |
| 3919 | break; |
| 3920 | if (cur->next != NULL) { |
| 3921 | cur = cur->next; |
| 3922 | continue; |
| 3923 | } |
| 3924 | do { |
| 3925 | cur = cur->parent; |
| 3926 | if (cur == NULL) |
| 3927 | break; |
| 3928 | if (cur == def) |
| 3929 | return (1); |
| 3930 | if (cur->next != NULL) { |
| 3931 | cur = cur->next; |
| 3932 | break; |
| 3933 | } |
| 3934 | } while (cur != NULL); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3935 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3936 | return (1); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3937 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3938 | |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3939 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3940 | * xmlRelaxNGGetElements: |
| 3941 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3942 | * @def: the definition definition |
| 3943 | * @eora: gather elements (0) or attributes (1) |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3944 | * |
| 3945 | * Compute the list of top elements a definition can generate |
| 3946 | * |
| 3947 | * Returns a list of elements or NULL if none was found. |
| 3948 | */ |
| 3949 | static xmlRelaxNGDefinePtr * |
| 3950 | xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3951 | xmlRelaxNGDefinePtr def, int eora) |
| 3952 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3953 | xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3954 | int len = 0; |
| 3955 | int max = 0; |
| 3956 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3957 | /* |
| 3958 | * Don't run that check in case of error. Infinite recursion |
| 3959 | * becomes possible. |
| 3960 | */ |
| 3961 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3962 | return (NULL); |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3963 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3964 | parent = NULL; |
| 3965 | cur = def; |
| 3966 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3967 | if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) || |
| 3968 | (cur->type == XML_RELAXNG_TEXT))) || |
| 3969 | ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) { |
| 3970 | if (ret == NULL) { |
| 3971 | max = 10; |
| 3972 | ret = (xmlRelaxNGDefinePtr *) |
| 3973 | xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
| 3974 | if (ret == NULL) { |
| 3975 | xmlRngPErrMemory(ctxt, "getting element list\n"); |
| 3976 | return (NULL); |
| 3977 | } |
| 3978 | } else if (max <= len) { |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 3979 | xmlRelaxNGDefinePtr *temp; |
| 3980 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3981 | max *= 2; |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 3982 | temp = xmlRealloc(ret, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3983 | (max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 3984 | if (temp == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3985 | xmlRngPErrMemory(ctxt, "getting element list\n"); |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 3986 | xmlFree(ret); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3987 | return (NULL); |
| 3988 | } |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 3989 | ret = temp; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 3990 | } |
| 3991 | ret[len++] = cur; |
| 3992 | ret[len] = NULL; |
| 3993 | } else if ((cur->type == XML_RELAXNG_CHOICE) || |
| 3994 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
| 3995 | (cur->type == XML_RELAXNG_GROUP) || |
| 3996 | (cur->type == XML_RELAXNG_ONEORMORE) || |
| 3997 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
| 3998 | (cur->type == XML_RELAXNG_OPTIONAL) || |
| 3999 | (cur->type == XML_RELAXNG_PARENTREF) || |
| 4000 | (cur->type == XML_RELAXNG_REF) || |
William M. Brack | 236c8c0 | 2004-03-20 11:32:36 +0000 | [diff] [blame] | 4001 | (cur->type == XML_RELAXNG_DEF) || |
| 4002 | (cur->type == XML_RELAXNG_EXTERNALREF)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4003 | /* |
| 4004 | * Don't go within elements or attributes or string values. |
| 4005 | * Just gather the element top list |
| 4006 | */ |
| 4007 | if (cur->content != NULL) { |
| 4008 | parent = cur; |
| 4009 | cur = cur->content; |
| 4010 | tmp = cur; |
| 4011 | while (tmp != NULL) { |
| 4012 | tmp->parent = parent; |
| 4013 | tmp = tmp->next; |
| 4014 | } |
| 4015 | continue; |
| 4016 | } |
| 4017 | } |
| 4018 | if (cur == def) |
| 4019 | break; |
| 4020 | if (cur->next != NULL) { |
| 4021 | cur = cur->next; |
| 4022 | continue; |
| 4023 | } |
| 4024 | do { |
| 4025 | cur = cur->parent; |
| 4026 | if (cur == NULL) |
| 4027 | break; |
| 4028 | if (cur == def) |
| 4029 | return (ret); |
| 4030 | if (cur->next != NULL) { |
| 4031 | cur = cur->next; |
| 4032 | break; |
| 4033 | } |
| 4034 | } while (cur != NULL); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4035 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4036 | return (ret); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4037 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4038 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4039 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4040 | * xmlRelaxNGCheckChoiceDeterminism: |
| 4041 | * @ctxt: a Relax-NG parser context |
| 4042 | * @def: the choice definition |
| 4043 | * |
| 4044 | * Also used to find indeterministic pattern in choice |
| 4045 | */ |
| 4046 | static void |
| 4047 | xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4048 | xmlRelaxNGDefinePtr def) |
| 4049 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4050 | xmlRelaxNGDefinePtr **list; |
| 4051 | xmlRelaxNGDefinePtr cur; |
| 4052 | int nbchild = 0, i, j, ret; |
| 4053 | int is_nullable = 0; |
| 4054 | int is_indeterminist = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4055 | xmlHashTablePtr triage = NULL; |
| 4056 | int is_triable = 1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4057 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4058 | if ((def == NULL) || (def->type != XML_RELAXNG_CHOICE)) |
| 4059 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4060 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4061 | if (def->dflags & IS_PROCESSED) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4062 | return; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4063 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4064 | /* |
| 4065 | * Don't run that check in case of error. Infinite recursion |
| 4066 | * becomes possible. |
| 4067 | */ |
| 4068 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4069 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4070 | |
| 4071 | is_nullable = xmlRelaxNGIsNullable(def); |
| 4072 | |
| 4073 | cur = def->content; |
| 4074 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4075 | nbchild++; |
| 4076 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4077 | } |
| 4078 | |
| 4079 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4080 | sizeof(xmlRelaxNGDefinePtr |
| 4081 | *)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4082 | if (list == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4083 | xmlRngPErrMemory(ctxt, "building choice\n"); |
| 4084 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4085 | } |
| 4086 | i = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4087 | /* |
| 4088 | * a bit strong but safe |
| 4089 | */ |
| 4090 | if (is_nullable == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4091 | triage = xmlHashCreate(10); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4092 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4093 | is_triable = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4094 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4095 | cur = def->content; |
| 4096 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4097 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 0); |
| 4098 | if ((list[i] == NULL) || (list[i][0] == NULL)) { |
| 4099 | is_triable = 0; |
| 4100 | } else if (is_triable == 1) { |
| 4101 | xmlRelaxNGDefinePtr *tmp; |
| 4102 | int res; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4103 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4104 | tmp = list[i]; |
| 4105 | while ((*tmp != NULL) && (is_triable == 1)) { |
| 4106 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
| 4107 | res = xmlHashAddEntry2(triage, |
| 4108 | BAD_CAST "#text", NULL, |
| 4109 | (void *) cur); |
| 4110 | if (res != 0) |
| 4111 | is_triable = -1; |
| 4112 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
| 4113 | ((*tmp)->name != NULL)) { |
| 4114 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4115 | res = xmlHashAddEntry2(triage, |
| 4116 | (*tmp)->name, NULL, |
| 4117 | (void *) cur); |
| 4118 | else |
| 4119 | res = xmlHashAddEntry2(triage, |
| 4120 | (*tmp)->name, (*tmp)->ns, |
| 4121 | (void *) cur); |
| 4122 | if (res != 0) |
| 4123 | is_triable = -1; |
| 4124 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
| 4125 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4126 | res = xmlHashAddEntry2(triage, |
| 4127 | BAD_CAST "#any", NULL, |
| 4128 | (void *) cur); |
| 4129 | else |
| 4130 | res = xmlHashAddEntry2(triage, |
| 4131 | BAD_CAST "#any", (*tmp)->ns, |
| 4132 | (void *) cur); |
| 4133 | if (res != 0) |
| 4134 | is_triable = -1; |
| 4135 | } else { |
| 4136 | is_triable = -1; |
| 4137 | } |
| 4138 | tmp++; |
| 4139 | } |
| 4140 | } |
| 4141 | i++; |
| 4142 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4143 | } |
| 4144 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4145 | for (i = 0; i < nbchild; i++) { |
| 4146 | if (list[i] == NULL) |
| 4147 | continue; |
| 4148 | for (j = 0; j < i; j++) { |
| 4149 | if (list[j] == NULL) |
| 4150 | continue; |
| 4151 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
| 4152 | if (ret == 0) { |
| 4153 | is_indeterminist = 1; |
| 4154 | } |
| 4155 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4156 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4157 | for (i = 0; i < nbchild; i++) { |
| 4158 | if (list[i] != NULL) |
| 4159 | xmlFree(list[i]); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4160 | } |
| 4161 | |
| 4162 | xmlFree(list); |
| 4163 | if (is_indeterminist) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4164 | def->dflags |= IS_INDETERMINIST; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4165 | } |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4166 | if (is_triable == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4167 | def->dflags |= IS_TRIABLE; |
| 4168 | def->data = triage; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4169 | } else if (triage != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4170 | xmlHashFree(triage, NULL); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4171 | } |
| 4172 | def->dflags |= IS_PROCESSED; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4173 | } |
| 4174 | |
| 4175 | /** |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4176 | * xmlRelaxNGCheckGroupAttrs: |
| 4177 | * @ctxt: a Relax-NG parser context |
| 4178 | * @def: the group definition |
| 4179 | * |
| 4180 | * Detects violations of rule 7.3 |
| 4181 | */ |
| 4182 | static void |
| 4183 | xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4184 | xmlRelaxNGDefinePtr def) |
| 4185 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4186 | xmlRelaxNGDefinePtr **list; |
| 4187 | xmlRelaxNGDefinePtr cur; |
| 4188 | int nbchild = 0, i, j, ret; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4189 | |
| 4190 | if ((def == NULL) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4191 | ((def->type != XML_RELAXNG_GROUP) && |
| 4192 | (def->type != XML_RELAXNG_ELEMENT))) |
| 4193 | return; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4194 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4195 | if (def->dflags & IS_PROCESSED) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4196 | return; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4197 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4198 | /* |
| 4199 | * Don't run that check in case of error. Infinite recursion |
| 4200 | * becomes possible. |
| 4201 | */ |
| 4202 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4203 | return; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4204 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4205 | cur = def->attrs; |
| 4206 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4207 | nbchild++; |
| 4208 | cur = cur->next; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4209 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4210 | cur = def->content; |
| 4211 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4212 | nbchild++; |
| 4213 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4214 | } |
| 4215 | |
| 4216 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4217 | sizeof(xmlRelaxNGDefinePtr |
| 4218 | *)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4219 | if (list == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4220 | xmlRngPErrMemory(ctxt, "building group\n"); |
| 4221 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4222 | } |
| 4223 | i = 0; |
| 4224 | cur = def->attrs; |
| 4225 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4226 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4227 | i++; |
| 4228 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4229 | } |
| 4230 | cur = def->content; |
| 4231 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4232 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4233 | i++; |
| 4234 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4235 | } |
| 4236 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4237 | for (i = 0; i < nbchild; i++) { |
| 4238 | if (list[i] == NULL) |
| 4239 | continue; |
| 4240 | for (j = 0; j < i; j++) { |
| 4241 | if (list[j] == NULL) |
| 4242 | continue; |
| 4243 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
| 4244 | if (ret == 0) { |
| 4245 | xmlRngPErr(ctxt, def->node, XML_RNGP_GROUP_ATTR_CONFLICT, |
| 4246 | "Attributes conflicts in group\n", NULL, NULL); |
| 4247 | } |
| 4248 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4249 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4250 | for (i = 0; i < nbchild; i++) { |
| 4251 | if (list[i] != NULL) |
| 4252 | xmlFree(list[i]); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4253 | } |
| 4254 | |
| 4255 | xmlFree(list); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4256 | def->dflags |= IS_PROCESSED; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4257 | } |
| 4258 | |
| 4259 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4260 | * xmlRelaxNGComputeInterleaves: |
| 4261 | * @def: the interleave definition |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4262 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4263 | * @name: the definition name |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4264 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4265 | * A lot of work for preprocessing interleave definitions |
| 4266 | * is potentially needed to get a decent execution speed at runtime |
| 4267 | * - trying to get a total order on the element nodes generated |
| 4268 | * by the interleaves, order the list of interleave definitions |
| 4269 | * following that order. |
| 4270 | * - if <text/> is used to handle mixed content, it is better to |
| 4271 | * flag this in the define and simplify the runtime checking |
| 4272 | * algorithm |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4273 | */ |
| 4274 | static void |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4275 | xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4276 | xmlRelaxNGParserCtxtPtr ctxt, |
| 4277 | xmlChar * name ATTRIBUTE_UNUSED) |
| 4278 | { |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4279 | xmlRelaxNGDefinePtr cur, *tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4280 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4281 | xmlRelaxNGPartitionPtr partitions = NULL; |
| 4282 | xmlRelaxNGInterleaveGroupPtr *groups = NULL; |
| 4283 | xmlRelaxNGInterleaveGroupPtr group; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4284 | int i, j, ret, res; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4285 | int nbgroups = 0; |
| 4286 | int nbchild = 0; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 4287 | int is_mixed = 0; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4288 | int is_determinist = 1; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4289 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4290 | /* |
| 4291 | * Don't run that check in case of error. Infinite recursion |
| 4292 | * becomes possible. |
| 4293 | */ |
| 4294 | if (ctxt->nbErrors != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4295 | return; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4296 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4297 | #ifdef DEBUG_INTERLEAVE |
| 4298 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4299 | "xmlRelaxNGComputeInterleaves(%s)\n", name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4300 | #endif |
| 4301 | cur = def->content; |
| 4302 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4303 | nbchild++; |
| 4304 | cur = cur->next; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4305 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4306 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4307 | #ifdef DEBUG_INTERLEAVE |
| 4308 | xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild); |
| 4309 | #endif |
| 4310 | groups = (xmlRelaxNGInterleaveGroupPtr *) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4311 | xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4312 | if (groups == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4313 | goto error; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4314 | cur = def->content; |
| 4315 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4316 | groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr) |
| 4317 | xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup)); |
| 4318 | if (groups[nbgroups] == NULL) |
| 4319 | goto error; |
| 4320 | if (cur->type == XML_RELAXNG_TEXT) |
| 4321 | is_mixed++; |
| 4322 | groups[nbgroups]->rule = cur; |
| 4323 | groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0); |
| 4324 | groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4325 | nbgroups++; |
| 4326 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4327 | } |
| 4328 | #ifdef DEBUG_INTERLEAVE |
| 4329 | xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups); |
| 4330 | #endif |
| 4331 | |
| 4332 | /* |
| 4333 | * Let's check that all rules makes a partitions according to 7.4 |
| 4334 | */ |
| 4335 | partitions = (xmlRelaxNGPartitionPtr) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4336 | xmlMalloc(sizeof(xmlRelaxNGPartition)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4337 | if (partitions == NULL) |
| 4338 | goto error; |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 4339 | memset(partitions, 0, sizeof(xmlRelaxNGPartition)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4340 | partitions->nbgroups = nbgroups; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4341 | partitions->triage = xmlHashCreate(nbgroups); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4342 | for (i = 0; i < nbgroups; i++) { |
| 4343 | group = groups[i]; |
| 4344 | for (j = i + 1; j < nbgroups; j++) { |
| 4345 | if (groups[j] == NULL) |
| 4346 | continue; |
| 4347 | |
| 4348 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs, |
| 4349 | groups[j]->defs); |
| 4350 | if (ret == 0) { |
| 4351 | xmlRngPErr(ctxt, def->node, XML_RNGP_ELEM_TEXT_CONFLICT, |
| 4352 | "Element or text conflicts in interleave\n", |
| 4353 | NULL, NULL); |
| 4354 | } |
| 4355 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs, |
| 4356 | groups[j]->attrs); |
| 4357 | if (ret == 0) { |
| 4358 | xmlRngPErr(ctxt, def->node, XML_RNGP_ATTR_CONFLICT, |
| 4359 | "Attributes conflicts in interleave\n", NULL, |
| 4360 | NULL); |
| 4361 | } |
| 4362 | } |
| 4363 | tmp = group->defs; |
| 4364 | if ((tmp != NULL) && (*tmp != NULL)) { |
| 4365 | while (*tmp != NULL) { |
| 4366 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
| 4367 | res = xmlHashAddEntry2(partitions->triage, |
| 4368 | BAD_CAST "#text", NULL, |
| 4369 | (void *) (long) (i + 1)); |
| 4370 | if (res != 0) |
| 4371 | is_determinist = -1; |
| 4372 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
| 4373 | ((*tmp)->name != NULL)) { |
| 4374 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4375 | res = xmlHashAddEntry2(partitions->triage, |
| 4376 | (*tmp)->name, NULL, |
| 4377 | (void *) (long) (i + 1)); |
| 4378 | else |
| 4379 | res = xmlHashAddEntry2(partitions->triage, |
| 4380 | (*tmp)->name, (*tmp)->ns, |
| 4381 | (void *) (long) (i + 1)); |
| 4382 | if (res != 0) |
| 4383 | is_determinist = -1; |
| 4384 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
| 4385 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4386 | res = xmlHashAddEntry2(partitions->triage, |
| 4387 | BAD_CAST "#any", NULL, |
| 4388 | (void *) (long) (i + 1)); |
| 4389 | else |
| 4390 | res = xmlHashAddEntry2(partitions->triage, |
| 4391 | BAD_CAST "#any", (*tmp)->ns, |
| 4392 | (void *) (long) (i + 1)); |
| 4393 | if ((*tmp)->nameClass != NULL) |
| 4394 | is_determinist = 2; |
| 4395 | if (res != 0) |
| 4396 | is_determinist = -1; |
| 4397 | } else { |
| 4398 | is_determinist = -1; |
| 4399 | } |
| 4400 | tmp++; |
| 4401 | } |
| 4402 | } else { |
| 4403 | is_determinist = 0; |
| 4404 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4405 | } |
| 4406 | partitions->groups = groups; |
| 4407 | |
| 4408 | /* |
| 4409 | * and save the partition list back in the def |
| 4410 | */ |
| 4411 | def->data = partitions; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 4412 | if (is_mixed != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4413 | def->dflags |= IS_MIXED; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4414 | if (is_determinist == 1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4415 | partitions->flags = IS_DETERMINIST; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4416 | if (is_determinist == 2) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4417 | partitions->flags = IS_DETERMINIST | IS_NEEDCHECK; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4418 | return; |
| 4419 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4420 | error: |
| 4421 | xmlRngPErrMemory(ctxt, "in interleave computation\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4422 | if (groups != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4423 | for (i = 0; i < nbgroups; i++) |
| 4424 | if (groups[i] != NULL) { |
| 4425 | if (groups[i]->defs != NULL) |
| 4426 | xmlFree(groups[i]->defs); |
| 4427 | xmlFree(groups[i]); |
| 4428 | } |
| 4429 | xmlFree(groups); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4430 | } |
| 4431 | xmlRelaxNGFreePartition(partitions); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4432 | } |
| 4433 | |
| 4434 | /** |
| 4435 | * xmlRelaxNGParseInterleave: |
| 4436 | * @ctxt: a Relax-NG parser context |
| 4437 | * @node: the data node. |
| 4438 | * |
| 4439 | * parse the content of a RelaxNG interleave node. |
| 4440 | * |
| 4441 | * Returns the definition pointer or NULL in case of error |
| 4442 | */ |
| 4443 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4444 | xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4445 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4446 | xmlRelaxNGDefinePtr def = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4447 | xmlRelaxNGDefinePtr last = NULL, cur; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4448 | xmlNodePtr child; |
| 4449 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4450 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4451 | if (def == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4452 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4453 | } |
| 4454 | def->type = XML_RELAXNG_INTERLEAVE; |
| 4455 | |
| 4456 | if (ctxt->interleaves == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4457 | ctxt->interleaves = xmlHashCreate(10); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4458 | if (ctxt->interleaves == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4459 | xmlRngPErrMemory(ctxt, "create interleaves\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4460 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4461 | char name[32]; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4462 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4463 | snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++); |
| 4464 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) { |
| 4465 | xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_ADD, |
| 4466 | "Failed to add %s to hash table\n", |
| 4467 | (const xmlChar *) name, NULL); |
| 4468 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4469 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4470 | child = node->children; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4471 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4472 | xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_NO_CONTENT, |
| 4473 | "Element interleave is empty\n", NULL, NULL); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 4474 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4475 | while (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4476 | if (IS_RELAXNG(child, "element")) { |
| 4477 | cur = xmlRelaxNGParseElement(ctxt, child); |
| 4478 | } else { |
| 4479 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 4480 | } |
| 4481 | if (cur != NULL) { |
| 4482 | cur->parent = def; |
| 4483 | if (last == NULL) { |
| 4484 | def->content = last = cur; |
| 4485 | } else { |
| 4486 | last->next = cur; |
| 4487 | last = cur; |
| 4488 | } |
| 4489 | } |
| 4490 | child = child->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4491 | } |
| 4492 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4493 | return (def); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4494 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4495 | |
| 4496 | /** |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4497 | * xmlRelaxNGParseInclude: |
| 4498 | * @ctxt: a Relax-NG parser context |
| 4499 | * @node: the include node |
| 4500 | * |
| 4501 | * Integrate the content of an include node in the current grammar |
| 4502 | * |
| 4503 | * Returns 0 in case of success or -1 in case of error |
| 4504 | */ |
| 4505 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4506 | xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4507 | { |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4508 | xmlRelaxNGIncludePtr incl; |
| 4509 | xmlNodePtr root; |
| 4510 | int ret = 0, tmp; |
| 4511 | |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 4512 | incl = node->psvi; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4513 | if (incl == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4514 | xmlRngPErr(ctxt, node, XML_RNGP_INCLUDE_EMPTY, |
| 4515 | "Include node has no data\n", NULL, NULL); |
| 4516 | return (-1); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4517 | } |
| 4518 | root = xmlDocGetRootElement(incl->doc); |
| 4519 | if (root == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4520 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, "Include document is empty\n", |
| 4521 | NULL, NULL); |
| 4522 | return (-1); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4523 | } |
| 4524 | if (!xmlStrEqual(root->name, BAD_CAST "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4525 | xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING, |
| 4526 | "Include document root is not a grammar\n", NULL, NULL); |
| 4527 | return (-1); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4528 | } |
| 4529 | |
| 4530 | /* |
| 4531 | * Merge the definition from both the include and the internal list |
| 4532 | */ |
| 4533 | if (root->children != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4534 | tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children); |
| 4535 | if (tmp != 0) |
| 4536 | ret = -1; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4537 | } |
| 4538 | if (node->children != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4539 | tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children); |
| 4540 | if (tmp != 0) |
| 4541 | ret = -1; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4542 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4543 | return (ret); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4544 | } |
| 4545 | |
| 4546 | /** |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4547 | * xmlRelaxNGParseDefine: |
| 4548 | * @ctxt: a Relax-NG parser context |
| 4549 | * @node: the define node |
| 4550 | * |
| 4551 | * parse the content of a RelaxNG define element node. |
| 4552 | * |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4553 | * Returns 0 in case of success or -1 in case of error |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4554 | */ |
| 4555 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4556 | xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4557 | { |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4558 | xmlChar *name; |
| 4559 | int ret = 0, tmp; |
| 4560 | xmlRelaxNGDefinePtr def; |
| 4561 | const xmlChar *olddefine; |
| 4562 | |
| 4563 | name = xmlGetProp(node, BAD_CAST "name"); |
| 4564 | if (name == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4565 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_NAME_MISSING, |
| 4566 | "define has no name\n", NULL, NULL); |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4567 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4568 | xmlRelaxNGNormExtSpace(name); |
| 4569 | if (xmlValidateNCName(name, 0)) { |
| 4570 | xmlRngPErr(ctxt, node, XML_RNGP_INVALID_DEFINE_NAME, |
| 4571 | "define name '%s' is not an NCName\n", name, NULL); |
| 4572 | } |
| 4573 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4574 | if (def == NULL) { |
| 4575 | xmlFree(name); |
| 4576 | return (-1); |
| 4577 | } |
| 4578 | def->type = XML_RELAXNG_DEF; |
| 4579 | def->name = name; |
| 4580 | if (node->children == NULL) { |
| 4581 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_EMPTY, |
| 4582 | "define has no children\n", NULL, NULL); |
| 4583 | } else { |
| 4584 | olddefine = ctxt->define; |
| 4585 | ctxt->define = name; |
| 4586 | def->content = |
| 4587 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4588 | ctxt->define = olddefine; |
| 4589 | } |
| 4590 | if (ctxt->grammar->defs == NULL) |
| 4591 | ctxt->grammar->defs = xmlHashCreate(10); |
| 4592 | if (ctxt->grammar->defs == NULL) { |
| 4593 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED, |
| 4594 | "Could not create definition hash\n", NULL, NULL); |
| 4595 | ret = -1; |
| 4596 | } else { |
| 4597 | tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def); |
| 4598 | if (tmp < 0) { |
| 4599 | xmlRelaxNGDefinePtr prev; |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 4600 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4601 | prev = xmlHashLookup(ctxt->grammar->defs, name); |
| 4602 | if (prev == NULL) { |
| 4603 | xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED, |
| 4604 | "Internal error on define aggregation of %s\n", |
| 4605 | name, NULL); |
| 4606 | ret = -1; |
| 4607 | } else { |
| 4608 | while (prev->nextHash != NULL) |
| 4609 | prev = prev->nextHash; |
| 4610 | prev->nextHash = def; |
| 4611 | } |
| 4612 | } |
| 4613 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4614 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4615 | return (ret); |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4616 | } |
| 4617 | |
| 4618 | /** |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4619 | * xmlRelaxNGProcessExternalRef: |
| 4620 | * @ctxt: the parser context |
| 4621 | * @node: the externlRef node |
| 4622 | * |
| 4623 | * Process and compile an externlRef node |
| 4624 | * |
| 4625 | * Returns the xmlRelaxNGDefinePtr or NULL in case of error |
| 4626 | */ |
| 4627 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4628 | xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4629 | { |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4630 | xmlRelaxNGDocumentPtr docu; |
| 4631 | xmlNodePtr root, tmp; |
| 4632 | xmlChar *ns; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4633 | int newNs = 0, oldflags; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4634 | xmlRelaxNGDefinePtr def; |
| 4635 | |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 4636 | docu = node->psvi; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4637 | if (docu != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4638 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4639 | if (def == NULL) |
| 4640 | return (NULL); |
| 4641 | def->type = XML_RELAXNG_EXTERNALREF; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4642 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4643 | if (docu->content == NULL) { |
| 4644 | /* |
| 4645 | * Then do the parsing for good |
| 4646 | */ |
| 4647 | root = xmlDocGetRootElement(docu->doc); |
| 4648 | if (root == NULL) { |
| 4649 | xmlRngPErr(ctxt, node, XML_RNGP_EXTERNALREF_EMTPY, |
| 4650 | "xmlRelaxNGParse: %s is empty\n", ctxt->URL, |
| 4651 | NULL); |
| 4652 | return (NULL); |
| 4653 | } |
| 4654 | /* |
| 4655 | * ns transmission rules |
| 4656 | */ |
| 4657 | ns = xmlGetProp(root, BAD_CAST "ns"); |
| 4658 | if (ns == NULL) { |
| 4659 | tmp = node; |
| 4660 | while ((tmp != NULL) && (tmp->type == XML_ELEMENT_NODE)) { |
| 4661 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 4662 | if (ns != NULL) { |
| 4663 | break; |
| 4664 | } |
| 4665 | tmp = tmp->parent; |
| 4666 | } |
| 4667 | if (ns != NULL) { |
| 4668 | xmlSetProp(root, BAD_CAST "ns", ns); |
| 4669 | newNs = 1; |
| 4670 | xmlFree(ns); |
| 4671 | } |
| 4672 | } else { |
| 4673 | xmlFree(ns); |
| 4674 | } |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4675 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4676 | /* |
| 4677 | * Parsing to get a precompiled schemas. |
| 4678 | */ |
| 4679 | oldflags = ctxt->flags; |
| 4680 | ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF; |
| 4681 | docu->schema = xmlRelaxNGParseDocument(ctxt, root); |
| 4682 | ctxt->flags = oldflags; |
| 4683 | if ((docu->schema != NULL) && |
| 4684 | (docu->schema->topgrammar != NULL)) { |
| 4685 | docu->content = docu->schema->topgrammar->start; |
| 4686 | } |
| 4687 | |
| 4688 | /* |
| 4689 | * the externalRef may be reused in a different ns context |
| 4690 | */ |
| 4691 | if (newNs == 1) { |
| 4692 | xmlUnsetProp(root, BAD_CAST "ns"); |
| 4693 | } |
| 4694 | } |
| 4695 | def->content = docu->content; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4696 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4697 | def = NULL; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4698 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4699 | return (def); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4700 | } |
| 4701 | |
| 4702 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4703 | * xmlRelaxNGParsePattern: |
| 4704 | * @ctxt: a Relax-NG parser context |
| 4705 | * @node: the pattern node. |
| 4706 | * |
| 4707 | * parse the content of a RelaxNG pattern node. |
| 4708 | * |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4709 | * Returns the definition pointer or NULL in case of error or if no |
| 4710 | * pattern is generated. |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4711 | */ |
| 4712 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4713 | xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 4714 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4715 | xmlRelaxNGDefinePtr def = NULL; |
| 4716 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4717 | if (node == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4718 | return (NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4719 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4720 | if (IS_RELAXNG(node, "element")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4721 | def = xmlRelaxNGParseElement(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4722 | } else if (IS_RELAXNG(node, "attribute")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4723 | def = xmlRelaxNGParseAttribute(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4724 | } else if (IS_RELAXNG(node, "empty")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4725 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4726 | if (def == NULL) |
| 4727 | return (NULL); |
| 4728 | def->type = XML_RELAXNG_EMPTY; |
| 4729 | if (node->children != NULL) { |
| 4730 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_NOT_EMPTY, |
| 4731 | "empty: had a child node\n", NULL, NULL); |
| 4732 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4733 | } else if (IS_RELAXNG(node, "text")) { |
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_TEXT; |
| 4738 | if (node->children != NULL) { |
| 4739 | xmlRngPErr(ctxt, node, XML_RNGP_TEXT_HAS_CHILD, |
| 4740 | "text: had a child node\n", NULL, NULL); |
| 4741 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4742 | } else if (IS_RELAXNG(node, "zeroOrMore")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4743 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4744 | if (def == NULL) |
| 4745 | return (NULL); |
| 4746 | def->type = XML_RELAXNG_ZEROORMORE; |
| 4747 | if (node->children == NULL) { |
| 4748 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4749 | "Element %s is empty\n", node->name, NULL); |
| 4750 | } else { |
| 4751 | def->content = |
| 4752 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4753 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4754 | } else if (IS_RELAXNG(node, "oneOrMore")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4755 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4756 | if (def == NULL) |
| 4757 | return (NULL); |
| 4758 | def->type = XML_RELAXNG_ONEORMORE; |
| 4759 | if (node->children == NULL) { |
| 4760 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4761 | "Element %s is empty\n", node->name, NULL); |
| 4762 | } else { |
| 4763 | def->content = |
| 4764 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4765 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4766 | } else if (IS_RELAXNG(node, "optional")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4767 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4768 | if (def == NULL) |
| 4769 | return (NULL); |
| 4770 | def->type = XML_RELAXNG_OPTIONAL; |
| 4771 | if (node->children == NULL) { |
| 4772 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4773 | "Element %s is empty\n", node->name, NULL); |
| 4774 | } else { |
| 4775 | def->content = |
| 4776 | xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4777 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4778 | } else if (IS_RELAXNG(node, "choice")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4779 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4780 | if (def == NULL) |
| 4781 | return (NULL); |
| 4782 | def->type = XML_RELAXNG_CHOICE; |
| 4783 | if (node->children == NULL) { |
| 4784 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4785 | "Element %s is empty\n", node->name, NULL); |
| 4786 | } else { |
| 4787 | def->content = |
| 4788 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4789 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4790 | } else if (IS_RELAXNG(node, "group")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4791 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4792 | if (def == NULL) |
| 4793 | return (NULL); |
| 4794 | def->type = XML_RELAXNG_GROUP; |
| 4795 | if (node->children == NULL) { |
| 4796 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4797 | "Element %s is empty\n", node->name, NULL); |
| 4798 | } else { |
| 4799 | def->content = |
| 4800 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4801 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4802 | } else if (IS_RELAXNG(node, "ref")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4803 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4804 | if (def == NULL) |
| 4805 | return (NULL); |
| 4806 | def->type = XML_RELAXNG_REF; |
| 4807 | def->name = xmlGetProp(node, BAD_CAST "name"); |
| 4808 | if (def->name == NULL) { |
| 4809 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NO_NAME, "ref has no name\n", |
| 4810 | NULL, NULL); |
| 4811 | } else { |
| 4812 | xmlRelaxNGNormExtSpace(def->name); |
| 4813 | if (xmlValidateNCName(def->name, 0)) { |
| 4814 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NAME_INVALID, |
| 4815 | "ref name '%s' is not an NCName\n", def->name, |
| 4816 | NULL); |
| 4817 | } |
| 4818 | } |
| 4819 | if (node->children != NULL) { |
| 4820 | xmlRngPErr(ctxt, node, XML_RNGP_REF_NOT_EMPTY, "ref is not empty\n", |
| 4821 | NULL, NULL); |
| 4822 | } |
| 4823 | if (ctxt->grammar->refs == NULL) |
| 4824 | ctxt->grammar->refs = xmlHashCreate(10); |
| 4825 | if (ctxt->grammar->refs == NULL) { |
| 4826 | xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED, |
| 4827 | "Could not create references hash\n", NULL, NULL); |
| 4828 | def = NULL; |
| 4829 | } else { |
| 4830 | int tmp; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4831 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4832 | tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def); |
| 4833 | if (tmp < 0) { |
| 4834 | xmlRelaxNGDefinePtr prev; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4835 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4836 | prev = (xmlRelaxNGDefinePtr) |
| 4837 | xmlHashLookup(ctxt->grammar->refs, def->name); |
| 4838 | if (prev == NULL) { |
| 4839 | if (def->name != NULL) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 4840 | xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED, |
| 4841 | "Error refs definitions '%s'\n", |
| 4842 | def->name, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4843 | } else { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 4844 | xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED, |
| 4845 | "Error refs definitions\n", |
| 4846 | NULL, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4847 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4848 | def = NULL; |
| 4849 | } else { |
| 4850 | def->nextHash = prev->nextHash; |
| 4851 | prev->nextHash = def; |
| 4852 | } |
| 4853 | } |
| 4854 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 4855 | } else if (IS_RELAXNG(node, "data")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4856 | def = xmlRelaxNGParseData(ctxt, node); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 4857 | } else if (IS_RELAXNG(node, "value")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4858 | def = xmlRelaxNGParseValue(ctxt, node); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 4859 | } else if (IS_RELAXNG(node, "list")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4860 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4861 | if (def == NULL) |
| 4862 | return (NULL); |
| 4863 | def->type = XML_RELAXNG_LIST; |
| 4864 | if (node->children == NULL) { |
| 4865 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, |
| 4866 | "Element %s is empty\n", node->name, NULL); |
| 4867 | } else { |
| 4868 | def->content = |
| 4869 | xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4870 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4871 | } else if (IS_RELAXNG(node, "interleave")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4872 | def = xmlRelaxNGParseInterleave(ctxt, node); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4873 | } else if (IS_RELAXNG(node, "externalRef")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4874 | def = xmlRelaxNGProcessExternalRef(ctxt, node); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4875 | } else if (IS_RELAXNG(node, "notAllowed")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4876 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4877 | if (def == NULL) |
| 4878 | return (NULL); |
| 4879 | def->type = XML_RELAXNG_NOT_ALLOWED; |
| 4880 | if (node->children != NULL) { |
| 4881 | xmlRngPErr(ctxt, node, XML_RNGP_NOTALLOWED_NOT_EMPTY, |
| 4882 | "xmlRelaxNGParse: notAllowed element is not empty\n", |
| 4883 | NULL, NULL); |
| 4884 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4885 | } else if (IS_RELAXNG(node, "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4886 | xmlRelaxNGGrammarPtr grammar, old; |
| 4887 | xmlRelaxNGGrammarPtr oldparent; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4888 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4889 | #ifdef DEBUG_GRAMMAR |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4890 | xmlGenericError(xmlGenericErrorContext, |
| 4891 | "Found <grammar> pattern\n"); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4892 | #endif |
| 4893 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4894 | oldparent = ctxt->parentgrammar; |
| 4895 | old = ctxt->grammar; |
| 4896 | ctxt->parentgrammar = old; |
| 4897 | grammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
| 4898 | if (old != NULL) { |
| 4899 | ctxt->grammar = old; |
| 4900 | ctxt->parentgrammar = oldparent; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4901 | #if 0 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4902 | if (grammar != NULL) { |
| 4903 | grammar->next = old->next; |
| 4904 | old->next = grammar; |
| 4905 | } |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4906 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4907 | } |
| 4908 | if (grammar != NULL) |
| 4909 | def = grammar->start; |
| 4910 | else |
| 4911 | def = NULL; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4912 | } else if (IS_RELAXNG(node, "parentRef")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4913 | if (ctxt->parentgrammar == NULL) { |
| 4914 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_PARENT, |
| 4915 | "Use of parentRef without a parent grammar\n", NULL, |
| 4916 | NULL); |
| 4917 | return (NULL); |
| 4918 | } |
| 4919 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4920 | if (def == NULL) |
| 4921 | return (NULL); |
| 4922 | def->type = XML_RELAXNG_PARENTREF; |
| 4923 | def->name = xmlGetProp(node, BAD_CAST "name"); |
| 4924 | if (def->name == NULL) { |
| 4925 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_NAME, |
| 4926 | "parentRef has no name\n", NULL, NULL); |
| 4927 | } else { |
| 4928 | xmlRelaxNGNormExtSpace(def->name); |
| 4929 | if (xmlValidateNCName(def->name, 0)) { |
| 4930 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NAME_INVALID, |
| 4931 | "parentRef name '%s' is not an NCName\n", |
| 4932 | def->name, NULL); |
| 4933 | } |
| 4934 | } |
| 4935 | if (node->children != NULL) { |
| 4936 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NOT_EMPTY, |
| 4937 | "parentRef is not empty\n", NULL, NULL); |
| 4938 | } |
| 4939 | if (ctxt->parentgrammar->refs == NULL) |
| 4940 | ctxt->parentgrammar->refs = xmlHashCreate(10); |
| 4941 | if (ctxt->parentgrammar->refs == NULL) { |
| 4942 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED, |
| 4943 | "Could not create references hash\n", NULL, NULL); |
| 4944 | def = NULL; |
| 4945 | } else if (def->name != NULL) { |
| 4946 | int tmp; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4947 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4948 | tmp = |
| 4949 | xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def); |
| 4950 | if (tmp < 0) { |
| 4951 | xmlRelaxNGDefinePtr prev; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4952 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4953 | prev = (xmlRelaxNGDefinePtr) |
| 4954 | xmlHashLookup(ctxt->parentgrammar->refs, def->name); |
| 4955 | if (prev == NULL) { |
| 4956 | xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED, |
| 4957 | "Internal error parentRef definitions '%s'\n", |
| 4958 | def->name, NULL); |
| 4959 | def = NULL; |
| 4960 | } else { |
| 4961 | def->nextHash = prev->nextHash; |
| 4962 | prev->nextHash = def; |
| 4963 | } |
| 4964 | } |
| 4965 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4966 | } else if (IS_RELAXNG(node, "mixed")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4967 | if (node->children == NULL) { |
| 4968 | xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, "Mixed is empty\n", |
| 4969 | NULL, NULL); |
| 4970 | def = NULL; |
| 4971 | } else { |
| 4972 | def = xmlRelaxNGParseInterleave(ctxt, node); |
| 4973 | if (def != NULL) { |
| 4974 | xmlRelaxNGDefinePtr tmp; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4975 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4976 | if ((def->content != NULL) && (def->content->next != NULL)) { |
| 4977 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
| 4978 | if (tmp != NULL) { |
| 4979 | tmp->type = XML_RELAXNG_GROUP; |
| 4980 | tmp->content = def->content; |
| 4981 | def->content = tmp; |
| 4982 | } |
| 4983 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4984 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4985 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
| 4986 | if (tmp == NULL) |
| 4987 | return (def); |
| 4988 | tmp->type = XML_RELAXNG_TEXT; |
| 4989 | tmp->next = def->content; |
| 4990 | def->content = tmp; |
| 4991 | } |
| 4992 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4993 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4994 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_CONSTRUCT, |
| 4995 | "Unexpected node %s is not a pattern\n", node->name, |
| 4996 | NULL); |
| 4997 | def = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4998 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 4999 | return (def); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5000 | } |
| 5001 | |
| 5002 | /** |
| 5003 | * xmlRelaxNGParseAttribute: |
| 5004 | * @ctxt: a Relax-NG parser context |
| 5005 | * @node: the element node |
| 5006 | * |
| 5007 | * parse the content of a RelaxNG attribute node. |
| 5008 | * |
| 5009 | * Returns the definition pointer or NULL in case of error. |
| 5010 | */ |
| 5011 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5012 | xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 5013 | { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5014 | xmlRelaxNGDefinePtr ret, cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5015 | xmlNodePtr child; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5016 | int old_flags; |
| 5017 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5018 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5019 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5020 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5021 | ret->type = XML_RELAXNG_ATTRIBUTE; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5022 | ret->parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5023 | child = node->children; |
| 5024 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5025 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_EMPTY, |
| 5026 | "xmlRelaxNGParseattribute: attribute has no children\n", |
| 5027 | NULL, NULL); |
| 5028 | return (ret); |
| 5029 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5030 | old_flags = ctxt->flags; |
| 5031 | ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5032 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5033 | if (cur != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5034 | child = child->next; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5035 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5036 | if (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5037 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 5038 | if (cur != NULL) { |
| 5039 | switch (cur->type) { |
| 5040 | case XML_RELAXNG_EMPTY: |
| 5041 | case XML_RELAXNG_NOT_ALLOWED: |
| 5042 | case XML_RELAXNG_TEXT: |
| 5043 | case XML_RELAXNG_ELEMENT: |
| 5044 | case XML_RELAXNG_DATATYPE: |
| 5045 | case XML_RELAXNG_VALUE: |
| 5046 | case XML_RELAXNG_LIST: |
| 5047 | case XML_RELAXNG_REF: |
| 5048 | case XML_RELAXNG_PARENTREF: |
| 5049 | case XML_RELAXNG_EXTERNALREF: |
| 5050 | case XML_RELAXNG_DEF: |
| 5051 | case XML_RELAXNG_ONEORMORE: |
| 5052 | case XML_RELAXNG_ZEROORMORE: |
| 5053 | case XML_RELAXNG_OPTIONAL: |
| 5054 | case XML_RELAXNG_CHOICE: |
| 5055 | case XML_RELAXNG_GROUP: |
| 5056 | case XML_RELAXNG_INTERLEAVE: |
| 5057 | case XML_RELAXNG_ATTRIBUTE: |
| 5058 | ret->content = cur; |
| 5059 | cur->parent = ret; |
| 5060 | break; |
| 5061 | case XML_RELAXNG_START: |
| 5062 | case XML_RELAXNG_PARAM: |
| 5063 | case XML_RELAXNG_EXCEPT: |
| 5064 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CONTENT, |
| 5065 | "attribute has invalid content\n", NULL, |
| 5066 | NULL); |
| 5067 | break; |
| 5068 | case XML_RELAXNG_NOOP: |
| 5069 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_NOOP, |
| 5070 | "RNG Internal error, noop found in attribute\n", |
| 5071 | NULL, NULL); |
| 5072 | break; |
| 5073 | } |
| 5074 | } |
| 5075 | child = child->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5076 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5077 | if (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5078 | xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CHILDREN, |
| 5079 | "attribute has multiple children\n", NULL, NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5080 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5081 | ctxt->flags = old_flags; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5082 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5083 | } |
| 5084 | |
| 5085 | /** |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5086 | * xmlRelaxNGParseExceptNameClass: |
| 5087 | * @ctxt: a Relax-NG parser context |
| 5088 | * @node: the except node |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5089 | * @attr: 1 if within an attribute, 0 if within an element |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5090 | * |
| 5091 | * parse the content of a RelaxNG nameClass node. |
| 5092 | * |
| 5093 | * Returns the definition pointer or NULL in case of error. |
| 5094 | */ |
| 5095 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5096 | xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5097 | xmlNodePtr node, int attr) |
| 5098 | { |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5099 | xmlRelaxNGDefinePtr ret, cur, last = NULL; |
| 5100 | xmlNodePtr child; |
| 5101 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5102 | if (!IS_RELAXNG(node, "except")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5103 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MISSING, |
| 5104 | "Expecting an except node\n", NULL, NULL); |
| 5105 | return (NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5106 | } |
| 5107 | if (node->next != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5108 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MULTIPLE, |
| 5109 | "exceptNameClass allows only a single except node\n", |
| 5110 | NULL, NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5111 | } |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5112 | if (node->children == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5113 | xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_EMPTY, "except has no content\n", |
| 5114 | NULL, NULL); |
| 5115 | return (NULL); |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5116 | } |
| 5117 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5118 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5119 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5120 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5121 | ret->type = XML_RELAXNG_EXCEPT; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5122 | child = node->children; |
| 5123 | while (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5124 | cur = xmlRelaxNGNewDefine(ctxt, child); |
| 5125 | if (cur == NULL) |
| 5126 | break; |
| 5127 | if (attr) |
| 5128 | cur->type = XML_RELAXNG_ATTRIBUTE; |
| 5129 | else |
| 5130 | cur->type = XML_RELAXNG_ELEMENT; |
| 5131 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 5132 | if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5133 | if (last == NULL) { |
| 5134 | ret->content = cur; |
| 5135 | } else { |
| 5136 | last->next = cur; |
| 5137 | } |
| 5138 | last = cur; |
| 5139 | } |
| 5140 | child = child->next; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5141 | } |
| 5142 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5143 | return (ret); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5144 | } |
| 5145 | |
| 5146 | /** |
| 5147 | * xmlRelaxNGParseNameClass: |
| 5148 | * @ctxt: a Relax-NG parser context |
| 5149 | * @node: the nameClass node |
| 5150 | * @def: the current definition |
| 5151 | * |
| 5152 | * parse the content of a RelaxNG nameClass node. |
| 5153 | * |
| 5154 | * Returns the definition pointer or NULL in case of error. |
| 5155 | */ |
| 5156 | static xmlRelaxNGDefinePtr |
| 5157 | xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5158 | xmlRelaxNGDefinePtr def) |
| 5159 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5160 | xmlRelaxNGDefinePtr ret, tmp; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5161 | xmlChar *val; |
| 5162 | |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5163 | ret = def; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5164 | if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) || |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5165 | (IS_RELAXNG(node, "nsName"))) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5166 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 5167 | (def->type != XML_RELAXNG_ATTRIBUTE)) { |
| 5168 | ret = xmlRelaxNGNewDefine(ctxt, node); |
| 5169 | if (ret == NULL) |
| 5170 | return (NULL); |
| 5171 | ret->parent = def; |
| 5172 | if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) |
| 5173 | ret->type = XML_RELAXNG_ATTRIBUTE; |
| 5174 | else |
| 5175 | ret->type = XML_RELAXNG_ELEMENT; |
| 5176 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5177 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5178 | if (IS_RELAXNG(node, "name")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5179 | val = xmlNodeGetContent(node); |
| 5180 | xmlRelaxNGNormExtSpace(val); |
| 5181 | if (xmlValidateNCName(val, 0)) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5182 | if (node->parent != NULL) |
| 5183 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME, |
| 5184 | "Element %s name '%s' is not an NCName\n", |
| 5185 | node->parent->name, val); |
| 5186 | else |
| 5187 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME, |
| 5188 | "name '%s' is not an NCName\n", |
| 5189 | val, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5190 | } |
| 5191 | ret->name = val; |
| 5192 | val = xmlGetProp(node, BAD_CAST "ns"); |
| 5193 | ret->ns = val; |
| 5194 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5195 | (val != NULL) && |
| 5196 | (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5197 | xmlRngPErr(ctxt, node, XML_RNGP_XML_NS, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5198 | "Attribute with namespace '%s' is not allowed\n", |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5199 | val, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5200 | } |
| 5201 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5202 | (val != NULL) && |
| 5203 | (val[0] == 0) && (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) { |
Daniel Veillard | 6edbfbb | 2003-10-07 12:17:44 +0000 | [diff] [blame] | 5204 | xmlRngPErr(ctxt, node, XML_RNGP_XMLNS_NAME, |
| 5205 | "Attribute with QName 'xmlns' is not allowed\n", |
| 5206 | val, NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5207 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5208 | } else if (IS_RELAXNG(node, "anyName")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5209 | ret->name = NULL; |
| 5210 | ret->ns = NULL; |
| 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, "nsName")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5218 | ret->name = NULL; |
| 5219 | ret->ns = xmlGetProp(node, BAD_CAST "ns"); |
| 5220 | if (ret->ns == NULL) { |
| 5221 | xmlRngPErr(ctxt, node, XML_RNGP_NSNAME_NO_NS, |
| 5222 | "nsName has no ns attribute\n", NULL, NULL); |
| 5223 | } |
| 5224 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5225 | (ret->ns != NULL) && |
| 5226 | (xmlStrEqual |
| 5227 | (ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
| 5228 | xmlRngPErr(ctxt, node, XML_RNGP_XML_NS, |
| 5229 | "Attribute with namespace '%s' is not allowed\n", |
| 5230 | ret->ns, NULL); |
| 5231 | } |
| 5232 | if (node->children != NULL) { |
| 5233 | ret->nameClass = |
| 5234 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
| 5235 | (def->type == |
| 5236 | XML_RELAXNG_ATTRIBUTE)); |
| 5237 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5238 | } else if (IS_RELAXNG(node, "choice")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5239 | xmlNodePtr child; |
| 5240 | xmlRelaxNGDefinePtr last = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5241 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5242 | ret = xmlRelaxNGNewDefine(ctxt, node); |
| 5243 | if (ret == NULL) |
| 5244 | return (NULL); |
| 5245 | ret->parent = def; |
| 5246 | ret->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5247 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5248 | if (node->children == NULL) { |
| 5249 | xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_EMPTY, |
| 5250 | "Element choice is empty\n", NULL, NULL); |
| 5251 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5252 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5253 | child = node->children; |
| 5254 | while (child != NULL) { |
| 5255 | tmp = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5256 | if (tmp != NULL) { |
| 5257 | if (last == NULL) { |
| 5258 | last = ret->nameClass = tmp; |
| 5259 | } else { |
| 5260 | last->next = tmp; |
| 5261 | last = tmp; |
| 5262 | } |
| 5263 | } |
| 5264 | child = child->next; |
| 5265 | } |
| 5266 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5267 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5268 | xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_CONTENT, |
| 5269 | "expecting name, anyName, nsName or choice : got %s\n", |
| 5270 | node->name, NULL); |
| 5271 | return (NULL); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5272 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5273 | if (ret != def) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5274 | if (def->nameClass == NULL) { |
| 5275 | def->nameClass = ret; |
| 5276 | } else { |
| 5277 | tmp = def->nameClass; |
| 5278 | while (tmp->next != NULL) { |
| 5279 | tmp = tmp->next; |
| 5280 | } |
| 5281 | tmp->next = ret; |
| 5282 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5283 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5284 | return (ret); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5285 | } |
| 5286 | |
| 5287 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5288 | * xmlRelaxNGParseElement: |
| 5289 | * @ctxt: a Relax-NG parser context |
| 5290 | * @node: the element node |
| 5291 | * |
| 5292 | * parse the content of a RelaxNG element node. |
| 5293 | * |
| 5294 | * Returns the definition pointer or NULL in case of error. |
| 5295 | */ |
| 5296 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5297 | xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 5298 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5299 | xmlRelaxNGDefinePtr ret, cur, last; |
| 5300 | xmlNodePtr child; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5301 | const xmlChar *olddefine; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5302 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5303 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5304 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5305 | return (NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5306 | ret->type = XML_RELAXNG_ELEMENT; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5307 | ret->parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5308 | child = node->children; |
| 5309 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5310 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_EMPTY, |
| 5311 | "xmlRelaxNGParseElement: element has no children\n", |
| 5312 | NULL, NULL); |
| 5313 | return (ret); |
| 5314 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5315 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5316 | if (cur != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5317 | child = child->next; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5318 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5319 | if (child == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5320 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NO_CONTENT, |
| 5321 | "xmlRelaxNGParseElement: element has no content\n", |
| 5322 | NULL, NULL); |
| 5323 | return (ret); |
| 5324 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5325 | olddefine = ctxt->define; |
| 5326 | ctxt->define = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5327 | last = NULL; |
| 5328 | while (child != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5329 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 5330 | if (cur != NULL) { |
| 5331 | cur->parent = ret; |
| 5332 | switch (cur->type) { |
| 5333 | case XML_RELAXNG_EMPTY: |
| 5334 | case XML_RELAXNG_NOT_ALLOWED: |
| 5335 | case XML_RELAXNG_TEXT: |
| 5336 | case XML_RELAXNG_ELEMENT: |
| 5337 | case XML_RELAXNG_DATATYPE: |
| 5338 | case XML_RELAXNG_VALUE: |
| 5339 | case XML_RELAXNG_LIST: |
| 5340 | case XML_RELAXNG_REF: |
| 5341 | case XML_RELAXNG_PARENTREF: |
| 5342 | case XML_RELAXNG_EXTERNALREF: |
| 5343 | case XML_RELAXNG_DEF: |
| 5344 | case XML_RELAXNG_ZEROORMORE: |
| 5345 | case XML_RELAXNG_ONEORMORE: |
| 5346 | case XML_RELAXNG_OPTIONAL: |
| 5347 | case XML_RELAXNG_CHOICE: |
| 5348 | case XML_RELAXNG_GROUP: |
| 5349 | case XML_RELAXNG_INTERLEAVE: |
| 5350 | if (last == NULL) { |
| 5351 | ret->content = last = cur; |
| 5352 | } else { |
| 5353 | if ((last->type == XML_RELAXNG_ELEMENT) && |
| 5354 | (ret->content == last)) { |
| 5355 | ret->content = xmlRelaxNGNewDefine(ctxt, node); |
| 5356 | if (ret->content != NULL) { |
| 5357 | ret->content->type = XML_RELAXNG_GROUP; |
| 5358 | ret->content->content = last; |
| 5359 | } else { |
| 5360 | ret->content = last; |
| 5361 | } |
| 5362 | } |
| 5363 | last->next = cur; |
| 5364 | last = cur; |
| 5365 | } |
| 5366 | break; |
| 5367 | case XML_RELAXNG_ATTRIBUTE: |
| 5368 | cur->next = ret->attrs; |
| 5369 | ret->attrs = cur; |
| 5370 | break; |
| 5371 | case XML_RELAXNG_START: |
| 5372 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5373 | "RNG Internal error, start found in element\n", |
| 5374 | NULL, NULL); |
| 5375 | break; |
| 5376 | case XML_RELAXNG_PARAM: |
| 5377 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5378 | "RNG Internal error, param found in element\n", |
| 5379 | NULL, NULL); |
| 5380 | break; |
| 5381 | case XML_RELAXNG_EXCEPT: |
| 5382 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5383 | "RNG Internal error, except found in element\n", |
| 5384 | NULL, NULL); |
| 5385 | break; |
| 5386 | case XML_RELAXNG_NOOP: |
| 5387 | xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT, |
| 5388 | "RNG Internal error, noop found in element\n", |
| 5389 | NULL, NULL); |
| 5390 | break; |
| 5391 | } |
| 5392 | } |
| 5393 | child = child->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5394 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5395 | ctxt->define = olddefine; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5396 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5397 | } |
| 5398 | |
| 5399 | /** |
| 5400 | * xmlRelaxNGParsePatterns: |
| 5401 | * @ctxt: a Relax-NG parser context |
| 5402 | * @nodes: list of nodes |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5403 | * @group: use an implicit <group> for elements |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5404 | * |
| 5405 | * parse the content of a RelaxNG start node. |
| 5406 | * |
| 5407 | * Returns the definition pointer or NULL in case of error. |
| 5408 | */ |
| 5409 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5410 | xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5411 | int group) |
| 5412 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5413 | xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5414 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5415 | parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5416 | while (nodes != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5417 | if (IS_RELAXNG(nodes, "element")) { |
| 5418 | cur = xmlRelaxNGParseElement(ctxt, nodes); |
| 5419 | if (def == NULL) { |
| 5420 | def = last = cur; |
| 5421 | } else { |
| 5422 | if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) && |
| 5423 | (def == last)) { |
| 5424 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
| 5425 | def->type = XML_RELAXNG_GROUP; |
| 5426 | def->content = last; |
| 5427 | } |
| 5428 | last->next = cur; |
| 5429 | last = cur; |
| 5430 | } |
| 5431 | cur->parent = parent; |
| 5432 | } else { |
| 5433 | cur = xmlRelaxNGParsePattern(ctxt, nodes); |
| 5434 | if (cur != NULL) { |
| 5435 | if (def == NULL) { |
| 5436 | def = last = cur; |
| 5437 | } else { |
| 5438 | last->next = cur; |
| 5439 | last = cur; |
| 5440 | } |
| 5441 | } |
| 5442 | } |
| 5443 | nodes = nodes->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5444 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5445 | return (def); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5446 | } |
| 5447 | |
| 5448 | /** |
| 5449 | * xmlRelaxNGParseStart: |
| 5450 | * @ctxt: a Relax-NG parser context |
| 5451 | * @nodes: start children nodes |
| 5452 | * |
| 5453 | * parse the content of a RelaxNG start node. |
| 5454 | * |
| 5455 | * Returns 0 in case of success, -1 in case of error |
| 5456 | */ |
| 5457 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5458 | xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) |
| 5459 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5460 | int ret = 0; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5461 | xmlRelaxNGDefinePtr def = NULL, last; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5462 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5463 | if (nodes == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5464 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, "start has no children\n", |
| 5465 | NULL, NULL); |
| 5466 | return (-1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5467 | } |
| 5468 | if (IS_RELAXNG(nodes, "empty")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5469 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
| 5470 | if (def == NULL) |
| 5471 | return (-1); |
| 5472 | def->type = XML_RELAXNG_EMPTY; |
| 5473 | if (nodes->children != NULL) { |
| 5474 | xmlRngPErr(ctxt, nodes, XML_RNGP_EMPTY_CONTENT, |
| 5475 | "element empty is not empty\n", NULL, NULL); |
| 5476 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5477 | } else if (IS_RELAXNG(nodes, "notAllowed")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5478 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
| 5479 | if (def == NULL) |
| 5480 | return (-1); |
| 5481 | def->type = XML_RELAXNG_NOT_ALLOWED; |
| 5482 | if (nodes->children != NULL) { |
| 5483 | xmlRngPErr(ctxt, nodes, XML_RNGP_NOTALLOWED_NOT_EMPTY, |
| 5484 | "element notAllowed is not empty\n", NULL, NULL); |
| 5485 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5486 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5487 | def = xmlRelaxNGParsePatterns(ctxt, nodes, 1); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5488 | } |
| 5489 | if (ctxt->grammar->start != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5490 | last = ctxt->grammar->start; |
| 5491 | while (last->next != NULL) |
| 5492 | last = last->next; |
| 5493 | last->next = def; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5494 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5495 | ctxt->grammar->start = def; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5496 | } |
| 5497 | nodes = nodes->next; |
| 5498 | if (nodes != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5499 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_CONTENT, |
| 5500 | "start more than one children\n", NULL, NULL); |
| 5501 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5502 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5503 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5504 | } |
| 5505 | |
| 5506 | /** |
| 5507 | * xmlRelaxNGParseGrammarContent: |
| 5508 | * @ctxt: a Relax-NG parser context |
| 5509 | * @nodes: grammar children nodes |
| 5510 | * |
| 5511 | * parse the content of a RelaxNG grammar node. |
| 5512 | * |
| 5513 | * Returns 0 in case of success, -1 in case of error |
| 5514 | */ |
| 5515 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5516 | xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt, |
| 5517 | xmlNodePtr nodes) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5518 | { |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5519 | int ret = 0, tmp; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5520 | |
| 5521 | if (nodes == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5522 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_EMPTY, |
| 5523 | "grammar has no children\n", NULL, NULL); |
| 5524 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5525 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5526 | while (nodes != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5527 | if (IS_RELAXNG(nodes, "start")) { |
| 5528 | if (nodes->children == NULL) { |
| 5529 | xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, |
| 5530 | "start has no children\n", NULL, NULL); |
| 5531 | } else { |
| 5532 | tmp = xmlRelaxNGParseStart(ctxt, nodes->children); |
| 5533 | if (tmp != 0) |
| 5534 | ret = -1; |
| 5535 | } |
| 5536 | } else if (IS_RELAXNG(nodes, "define")) { |
| 5537 | tmp = xmlRelaxNGParseDefine(ctxt, nodes); |
| 5538 | if (tmp != 0) |
| 5539 | ret = -1; |
| 5540 | } else if (IS_RELAXNG(nodes, "include")) { |
| 5541 | tmp = xmlRelaxNGParseInclude(ctxt, nodes); |
| 5542 | if (tmp != 0) |
| 5543 | ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5544 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5545 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT, |
| 5546 | "grammar has unexpected child %s\n", nodes->name, |
| 5547 | NULL); |
| 5548 | ret = -1; |
| 5549 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5550 | nodes = nodes->next; |
| 5551 | } |
| 5552 | return (ret); |
| 5553 | } |
| 5554 | |
| 5555 | /** |
| 5556 | * xmlRelaxNGCheckReference: |
| 5557 | * @ref: the ref |
| 5558 | * @ctxt: a Relax-NG parser context |
| 5559 | * @name: the name associated to the defines |
| 5560 | * |
| 5561 | * Applies the 4.17. combine attribute rule for all the define |
| 5562 | * element of a given grammar using the same name. |
| 5563 | */ |
| 5564 | static void |
| 5565 | xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5566 | xmlRelaxNGParserCtxtPtr ctxt, |
| 5567 | const xmlChar * name) |
| 5568 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5569 | xmlRelaxNGGrammarPtr grammar; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5570 | xmlRelaxNGDefinePtr def, cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5571 | |
| 5572 | grammar = ctxt->grammar; |
| 5573 | if (grammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5574 | xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR, |
| 5575 | "Internal error: no grammar in CheckReference %s\n", |
| 5576 | name, NULL); |
| 5577 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5578 | } |
| 5579 | if (ref->content != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5580 | xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR, |
| 5581 | "Internal error: reference has content in CheckReference %s\n", |
| 5582 | name, NULL); |
| 5583 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5584 | } |
| 5585 | if (grammar->defs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5586 | def = xmlHashLookup(grammar->defs, name); |
| 5587 | if (def != NULL) { |
| 5588 | cur = ref; |
| 5589 | while (cur != NULL) { |
| 5590 | cur->content = def; |
| 5591 | cur = cur->nextHash; |
| 5592 | } |
| 5593 | } else { |
| 5594 | xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF, |
| 5595 | "Reference %s has no matching definition\n", name, |
| 5596 | NULL); |
| 5597 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5598 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5599 | xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF, |
| 5600 | "Reference %s has no matching definition\n", name, |
| 5601 | NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5602 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5603 | } |
| 5604 | |
| 5605 | /** |
| 5606 | * xmlRelaxNGCheckCombine: |
| 5607 | * @define: the define(s) list |
| 5608 | * @ctxt: a Relax-NG parser context |
| 5609 | * @name: the name associated to the defines |
| 5610 | * |
| 5611 | * Applies the 4.17. combine attribute rule for all the define |
| 5612 | * element of a given grammar using the same name. |
| 5613 | */ |
| 5614 | static void |
| 5615 | xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5616 | xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * name) |
| 5617 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5618 | xmlChar *combine; |
| 5619 | int choiceOrInterleave = -1; |
| 5620 | int missing = 0; |
| 5621 | xmlRelaxNGDefinePtr cur, last, tmp, tmp2; |
| 5622 | |
| 5623 | if (define->nextHash == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5624 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5625 | cur = define; |
| 5626 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5627 | combine = xmlGetProp(cur->node, BAD_CAST "combine"); |
| 5628 | if (combine != NULL) { |
| 5629 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
| 5630 | if (choiceOrInterleave == -1) |
| 5631 | choiceOrInterleave = 1; |
| 5632 | else if (choiceOrInterleave == 0) { |
| 5633 | xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE, |
| 5634 | "Defines for %s use both 'choice' and 'interleave'\n", |
| 5635 | name, NULL); |
| 5636 | } |
| 5637 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
| 5638 | if (choiceOrInterleave == -1) |
| 5639 | choiceOrInterleave = 0; |
| 5640 | else if (choiceOrInterleave == 1) { |
| 5641 | xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE, |
| 5642 | "Defines for %s use both 'choice' and 'interleave'\n", |
| 5643 | name, NULL); |
| 5644 | } |
| 5645 | } else { |
| 5646 | xmlRngPErr(ctxt, define->node, XML_RNGP_UNKNOWN_COMBINE, |
| 5647 | "Defines for %s use unknown combine value '%s''\n", |
| 5648 | name, combine); |
| 5649 | } |
| 5650 | xmlFree(combine); |
| 5651 | } else { |
| 5652 | if (missing == 0) |
| 5653 | missing = 1; |
| 5654 | else { |
| 5655 | xmlRngPErr(ctxt, define->node, XML_RNGP_NEED_COMBINE, |
| 5656 | "Some defines for %s needs the combine attribute\n", |
| 5657 | name, NULL); |
| 5658 | } |
| 5659 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5660 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5661 | cur = cur->nextHash; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5662 | } |
| 5663 | #ifdef DEBUG |
| 5664 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5665 | "xmlRelaxNGCheckCombine(): merging %s defines: %d\n", |
| 5666 | name, choiceOrInterleave); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5667 | #endif |
| 5668 | if (choiceOrInterleave == -1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5669 | choiceOrInterleave = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5670 | cur = xmlRelaxNGNewDefine(ctxt, define->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5671 | if (cur == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5672 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5673 | if (choiceOrInterleave == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5674 | cur->type = XML_RELAXNG_INTERLEAVE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5675 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5676 | cur->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5677 | tmp = define; |
| 5678 | last = NULL; |
| 5679 | while (tmp != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5680 | if (tmp->content != NULL) { |
| 5681 | if (tmp->content->next != NULL) { |
| 5682 | /* |
| 5683 | * we need first to create a wrapper. |
| 5684 | */ |
| 5685 | tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node); |
| 5686 | if (tmp2 == NULL) |
| 5687 | break; |
| 5688 | tmp2->type = XML_RELAXNG_GROUP; |
| 5689 | tmp2->content = tmp->content; |
| 5690 | } else { |
| 5691 | tmp2 = tmp->content; |
| 5692 | } |
| 5693 | if (last == NULL) { |
| 5694 | cur->content = tmp2; |
| 5695 | } else { |
| 5696 | last->next = tmp2; |
| 5697 | } |
| 5698 | last = tmp2; |
| 5699 | } |
| 5700 | tmp->content = cur; |
| 5701 | tmp = tmp->nextHash; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5702 | } |
| 5703 | define->content = cur; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5704 | if (choiceOrInterleave == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5705 | if (ctxt->interleaves == NULL) |
| 5706 | ctxt->interleaves = xmlHashCreate(10); |
| 5707 | if (ctxt->interleaves == NULL) { |
| 5708 | xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5709 | "Failed to create interleaves hash table\n", NULL, |
| 5710 | NULL); |
| 5711 | } else { |
| 5712 | char tmpname[32]; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5713 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5714 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
| 5715 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < |
| 5716 | 0) { |
| 5717 | xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5718 | "Failed to add %s to hash table\n", |
| 5719 | (const xmlChar *) tmpname, NULL); |
| 5720 | } |
| 5721 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5722 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5723 | } |
| 5724 | |
| 5725 | /** |
| 5726 | * xmlRelaxNGCombineStart: |
| 5727 | * @ctxt: a Relax-NG parser context |
| 5728 | * @grammar: the grammar |
| 5729 | * |
| 5730 | * Applies the 4.17. combine rule for all the start |
| 5731 | * element of a given grammar. |
| 5732 | */ |
| 5733 | static void |
| 5734 | xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5735 | xmlRelaxNGGrammarPtr grammar) |
| 5736 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5737 | xmlRelaxNGDefinePtr starts; |
| 5738 | xmlChar *combine; |
| 5739 | int choiceOrInterleave = -1; |
| 5740 | int missing = 0; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5741 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5742 | |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5743 | starts = grammar->start; |
| 5744 | if ((starts == NULL) || (starts->next == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5745 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5746 | cur = starts; |
| 5747 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5748 | if ((cur->node == NULL) || (cur->node->parent == NULL) || |
| 5749 | (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) { |
| 5750 | combine = NULL; |
| 5751 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_MISSING, |
| 5752 | "Internal error: start element not found\n", NULL, |
| 5753 | NULL); |
| 5754 | } else { |
| 5755 | combine = xmlGetProp(cur->node->parent, BAD_CAST "combine"); |
| 5756 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5757 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5758 | if (combine != NULL) { |
| 5759 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
| 5760 | if (choiceOrInterleave == -1) |
| 5761 | choiceOrInterleave = 1; |
| 5762 | else if (choiceOrInterleave == 0) { |
| 5763 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE, |
| 5764 | "<start> use both 'choice' and 'interleave'\n", |
| 5765 | NULL, NULL); |
| 5766 | } |
| 5767 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
| 5768 | if (choiceOrInterleave == -1) |
| 5769 | choiceOrInterleave = 0; |
| 5770 | else if (choiceOrInterleave == 1) { |
| 5771 | xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE, |
| 5772 | "<start> use both 'choice' and 'interleave'\n", |
| 5773 | NULL, NULL); |
| 5774 | } |
| 5775 | } else { |
| 5776 | xmlRngPErr(ctxt, cur->node, XML_RNGP_UNKNOWN_COMBINE, |
| 5777 | "<start> uses unknown combine value '%s''\n", |
| 5778 | combine, NULL); |
| 5779 | } |
| 5780 | xmlFree(combine); |
| 5781 | } else { |
| 5782 | if (missing == 0) |
| 5783 | missing = 1; |
| 5784 | else { |
| 5785 | xmlRngPErr(ctxt, cur->node, XML_RNGP_NEED_COMBINE, |
| 5786 | "Some <start> element miss the combine attribute\n", |
| 5787 | NULL, NULL); |
| 5788 | } |
| 5789 | } |
| 5790 | |
| 5791 | cur = cur->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5792 | } |
| 5793 | #ifdef DEBUG |
| 5794 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5795 | "xmlRelaxNGCombineStart(): merging <start>: %d\n", |
| 5796 | choiceOrInterleave); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5797 | #endif |
| 5798 | if (choiceOrInterleave == -1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5799 | choiceOrInterleave = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5800 | cur = xmlRelaxNGNewDefine(ctxt, starts->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5801 | if (cur == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5802 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5803 | if (choiceOrInterleave == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5804 | cur->type = XML_RELAXNG_INTERLEAVE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5805 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5806 | cur->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5807 | cur->content = grammar->start; |
| 5808 | grammar->start = cur; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5809 | if (choiceOrInterleave == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5810 | if (ctxt->interleaves == NULL) |
| 5811 | ctxt->interleaves = xmlHashCreate(10); |
| 5812 | if (ctxt->interleaves == NULL) { |
| 5813 | xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5814 | "Failed to create interleaves hash table\n", NULL, |
| 5815 | NULL); |
| 5816 | } else { |
| 5817 | char tmpname[32]; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5818 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5819 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
| 5820 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < |
| 5821 | 0) { |
| 5822 | xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED, |
| 5823 | "Failed to add %s to hash table\n", |
| 5824 | (const xmlChar *) tmpname, NULL); |
| 5825 | } |
| 5826 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5827 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5828 | } |
| 5829 | |
| 5830 | /** |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5831 | * xmlRelaxNGCheckCycles: |
| 5832 | * @ctxt: a Relax-NG parser context |
| 5833 | * @nodes: grammar children nodes |
| 5834 | * @depth: the counter |
| 5835 | * |
| 5836 | * Check for cycles. |
| 5837 | * |
| 5838 | * Returns 0 if check passed, and -1 in case of error |
| 5839 | */ |
| 5840 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5841 | xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt, |
| 5842 | xmlRelaxNGDefinePtr cur, int depth) |
| 5843 | { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5844 | int ret = 0; |
| 5845 | |
| 5846 | while ((ret == 0) && (cur != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5847 | if ((cur->type == XML_RELAXNG_REF) || |
| 5848 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5849 | if (cur->depth == -1) { |
| 5850 | cur->depth = depth; |
| 5851 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
| 5852 | cur->depth = -2; |
| 5853 | } else if (depth == cur->depth) { |
| 5854 | xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_CYCLE, |
| 5855 | "Detected a cycle in %s references\n", |
| 5856 | cur->name, NULL); |
| 5857 | return (-1); |
| 5858 | } |
| 5859 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
| 5860 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1); |
| 5861 | } else { |
| 5862 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
| 5863 | } |
| 5864 | cur = cur->next; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5865 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5866 | return (ret); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5867 | } |
| 5868 | |
| 5869 | /** |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5870 | * xmlRelaxNGTryUnlink: |
| 5871 | * @ctxt: a Relax-NG parser context |
| 5872 | * @cur: the definition to unlink |
| 5873 | * @parent: the parent definition |
| 5874 | * @prev: the previous sibling definition |
| 5875 | * |
| 5876 | * Try to unlink a definition. If not possble make it a NOOP |
| 5877 | * |
| 5878 | * Returns the new prev definition |
| 5879 | */ |
| 5880 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5881 | xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 5882 | xmlRelaxNGDefinePtr cur, |
| 5883 | xmlRelaxNGDefinePtr parent, xmlRelaxNGDefinePtr prev) |
| 5884 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5885 | if (prev != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5886 | prev->next = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5887 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5888 | if (parent != NULL) { |
| 5889 | if (parent->content == cur) |
| 5890 | parent->content = cur->next; |
| 5891 | else if (parent->attrs == cur) |
| 5892 | parent->attrs = cur->next; |
| 5893 | else if (parent->nameClass == cur) |
| 5894 | parent->nameClass = cur->next; |
| 5895 | } else { |
| 5896 | cur->type = XML_RELAXNG_NOOP; |
| 5897 | prev = cur; |
| 5898 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5899 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5900 | return (prev); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5901 | } |
| 5902 | |
| 5903 | /** |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5904 | * xmlRelaxNGSimplify: |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5905 | * @ctxt: a Relax-NG parser context |
| 5906 | * @nodes: grammar children nodes |
| 5907 | * |
| 5908 | * Check for simplification of empty and notAllowed |
| 5909 | */ |
| 5910 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5911 | xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt, |
| 5912 | xmlRelaxNGDefinePtr cur, xmlRelaxNGDefinePtr parent) |
| 5913 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5914 | xmlRelaxNGDefinePtr prev = NULL; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5915 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5916 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5917 | if ((cur->type == XML_RELAXNG_REF) || |
| 5918 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5919 | if (cur->depth != -3) { |
| 5920 | cur->depth = -3; |
| 5921 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
| 5922 | } |
| 5923 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
| 5924 | cur->parent = parent; |
| 5925 | if ((parent != NULL) && |
| 5926 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
| 5927 | (parent->type == XML_RELAXNG_LIST) || |
| 5928 | (parent->type == XML_RELAXNG_GROUP) || |
| 5929 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 5930 | (parent->type == XML_RELAXNG_ONEORMORE) || |
| 5931 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 5932 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
| 5933 | break; |
| 5934 | } |
| 5935 | if ((parent != NULL) && (parent->type == XML_RELAXNG_CHOICE)) { |
| 5936 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5937 | } else |
| 5938 | prev = cur; |
| 5939 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 5940 | cur->parent = parent; |
| 5941 | if ((parent != NULL) && |
| 5942 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
| 5943 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 5944 | parent->type = XML_RELAXNG_EMPTY; |
| 5945 | break; |
| 5946 | } |
| 5947 | if ((parent != NULL) && |
| 5948 | ((parent->type == XML_RELAXNG_GROUP) || |
| 5949 | (parent->type == XML_RELAXNG_INTERLEAVE))) { |
| 5950 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5951 | } else |
| 5952 | prev = cur; |
| 5953 | } else { |
| 5954 | cur->parent = parent; |
| 5955 | if (cur->content != NULL) |
| 5956 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
| 5957 | if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL)) |
| 5958 | xmlRelaxNGSimplify(ctxt, cur->attrs, cur); |
| 5959 | if (cur->nameClass != NULL) |
| 5960 | xmlRelaxNGSimplify(ctxt, cur->nameClass, cur); |
| 5961 | /* |
| 5962 | * On Elements, try to move attribute only generating rules on |
| 5963 | * the attrs rules. |
| 5964 | */ |
| 5965 | if (cur->type == XML_RELAXNG_ELEMENT) { |
| 5966 | int attronly; |
| 5967 | xmlRelaxNGDefinePtr tmp, pre; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 5968 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 5969 | while (cur->content != NULL) { |
| 5970 | attronly = |
| 5971 | xmlRelaxNGGenerateAttributes(ctxt, cur->content); |
| 5972 | if (attronly == 1) { |
| 5973 | /* |
| 5974 | * migrate cur->content to attrs |
| 5975 | */ |
| 5976 | tmp = cur->content; |
| 5977 | cur->content = tmp->next; |
| 5978 | tmp->next = cur->attrs; |
| 5979 | cur->attrs = tmp; |
| 5980 | } else { |
| 5981 | /* |
| 5982 | * cur->content can generate elements or text |
| 5983 | */ |
| 5984 | break; |
| 5985 | } |
| 5986 | } |
| 5987 | pre = cur->content; |
| 5988 | while ((pre != NULL) && (pre->next != NULL)) { |
| 5989 | tmp = pre->next; |
| 5990 | attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp); |
| 5991 | if (attronly == 1) { |
| 5992 | /* |
| 5993 | * migrate tmp to attrs |
| 5994 | */ |
| 5995 | pre->next = tmp->next; |
| 5996 | tmp->next = cur->attrs; |
| 5997 | cur->attrs = tmp; |
| 5998 | } else { |
| 5999 | pre = tmp; |
| 6000 | } |
| 6001 | } |
| 6002 | } |
| 6003 | /* |
| 6004 | * This may result in a simplification |
| 6005 | */ |
| 6006 | if ((cur->type == XML_RELAXNG_GROUP) || |
| 6007 | (cur->type == XML_RELAXNG_INTERLEAVE)) { |
| 6008 | if (cur->content == NULL) |
| 6009 | cur->type = XML_RELAXNG_EMPTY; |
| 6010 | else if (cur->content->next == NULL) { |
| 6011 | if ((parent == NULL) && (prev == NULL)) { |
| 6012 | cur->type = XML_RELAXNG_NOOP; |
| 6013 | } else if (prev == NULL) { |
| 6014 | parent->content = cur->content; |
| 6015 | cur->content->next = cur->next; |
| 6016 | cur = cur->content; |
| 6017 | } else { |
| 6018 | cur->content->next = cur->next; |
| 6019 | prev->next = cur->content; |
| 6020 | cur = cur->content; |
| 6021 | } |
| 6022 | } |
| 6023 | } |
| 6024 | /* |
| 6025 | * the current node may have been transformed back |
| 6026 | */ |
| 6027 | if ((cur->type == XML_RELAXNG_EXCEPT) && |
| 6028 | (cur->content != NULL) && |
| 6029 | (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) { |
| 6030 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 6031 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
| 6032 | if ((parent != NULL) && |
| 6033 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
| 6034 | (parent->type == XML_RELAXNG_LIST) || |
| 6035 | (parent->type == XML_RELAXNG_GROUP) || |
| 6036 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 6037 | (parent->type == XML_RELAXNG_ONEORMORE) || |
| 6038 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 6039 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
| 6040 | break; |
| 6041 | } |
| 6042 | if ((parent != NULL) && |
| 6043 | (parent->type == XML_RELAXNG_CHOICE)) { |
| 6044 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 6045 | } else |
| 6046 | prev = cur; |
| 6047 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 6048 | if ((parent != NULL) && |
| 6049 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
| 6050 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 6051 | parent->type = XML_RELAXNG_EMPTY; |
| 6052 | break; |
| 6053 | } |
| 6054 | if ((parent != NULL) && |
| 6055 | ((parent->type == XML_RELAXNG_GROUP) || |
| 6056 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 6057 | (parent->type == XML_RELAXNG_CHOICE))) { |
| 6058 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 6059 | } else |
| 6060 | prev = cur; |
| 6061 | } else { |
| 6062 | prev = cur; |
| 6063 | } |
| 6064 | } |
| 6065 | cur = cur->next; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6066 | } |
| 6067 | } |
| 6068 | |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6069 | /** |
| 6070 | * xmlRelaxNGGroupContentType: |
| 6071 | * @ct1: the first content type |
| 6072 | * @ct2: the second content type |
| 6073 | * |
| 6074 | * Try to group 2 content types |
| 6075 | * |
| 6076 | * Returns the content type |
| 6077 | */ |
| 6078 | static xmlRelaxNGContentType |
| 6079 | xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6080 | xmlRelaxNGContentType ct2) |
| 6081 | { |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6082 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6083 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
| 6084 | return (XML_RELAXNG_CONTENT_ERROR); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6085 | if (ct1 == XML_RELAXNG_CONTENT_EMPTY) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6086 | return (ct2); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6087 | if (ct2 == XML_RELAXNG_CONTENT_EMPTY) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6088 | return (ct1); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6089 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6090 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
| 6091 | return (XML_RELAXNG_CONTENT_COMPLEX); |
| 6092 | return (XML_RELAXNG_CONTENT_ERROR); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6093 | } |
| 6094 | |
| 6095 | /** |
| 6096 | * xmlRelaxNGMaxContentType: |
| 6097 | * @ct1: the first content type |
| 6098 | * @ct2: the second content type |
| 6099 | * |
| 6100 | * Compute the max content-type |
| 6101 | * |
| 6102 | * Returns the content type |
| 6103 | */ |
| 6104 | static xmlRelaxNGContentType |
| 6105 | xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6106 | xmlRelaxNGContentType ct2) |
| 6107 | { |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6108 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6109 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
| 6110 | return (XML_RELAXNG_CONTENT_ERROR); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6111 | if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6112 | (ct2 == XML_RELAXNG_CONTENT_SIMPLE)) |
| 6113 | return (XML_RELAXNG_CONTENT_SIMPLE); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6114 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6115 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
| 6116 | return (XML_RELAXNG_CONTENT_COMPLEX); |
| 6117 | return (XML_RELAXNG_CONTENT_EMPTY); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6118 | } |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6119 | |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6120 | /** |
| 6121 | * xmlRelaxNGCheckRules: |
| 6122 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6123 | * @cur: the current definition |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6124 | * @flags: some accumulated flags |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6125 | * @ptype: the parent type |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6126 | * |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6127 | * Check for rules in section 7.1 and 7.2 |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6128 | * |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6129 | * Returns the content type of @cur |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6130 | */ |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6131 | static xmlRelaxNGContentType |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6132 | xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt, |
| 6133 | xmlRelaxNGDefinePtr cur, int flags, |
| 6134 | xmlRelaxNGType ptype) |
| 6135 | { |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6136 | int nflags = flags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6137 | xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6138 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6139 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6140 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6141 | if ((cur->type == XML_RELAXNG_REF) || |
| 6142 | (cur->type == XML_RELAXNG_PARENTREF)) { |
Daniel Veillard | 63d68a3 | 2005-03-31 13:50:00 +0000 | [diff] [blame] | 6143 | /* |
| 6144 | * This should actually be caught by list//element(ref) at the |
| 6145 | * element boundaries, c.f. Bug #159968 local refs are dropped |
| 6146 | * in step 4.19. |
| 6147 | */ |
| 6148 | #if 0 |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6149 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6150 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_REF, |
| 6151 | "Found forbidden pattern list//ref\n", NULL, |
| 6152 | NULL); |
| 6153 | } |
Daniel Veillard | 63d68a3 | 2005-03-31 13:50:00 +0000 | [diff] [blame] | 6154 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6155 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6156 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_REF, |
| 6157 | "Found forbidden pattern data/except//ref\n", |
| 6158 | NULL, NULL); |
| 6159 | } |
| 6160 | if (cur->depth > -4) { |
| 6161 | cur->depth = -4; |
| 6162 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, |
| 6163 | flags, cur->type); |
| 6164 | cur->depth = ret - 15; |
| 6165 | } else if (cur->depth == -4) { |
| 6166 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6167 | } else { |
| 6168 | ret = (xmlRelaxNGContentType) (cur->depth + 15); |
| 6169 | } |
| 6170 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
| 6171 | /* |
| 6172 | * The 7.3 Attribute derivation rule for groups is plugged there |
| 6173 | */ |
| 6174 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
| 6175 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6176 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ELEM, |
| 6177 | "Found forbidden pattern data/except//element(ref)\n", |
| 6178 | NULL, NULL); |
| 6179 | } |
| 6180 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6181 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ELEM, |
| 6182 | "Found forbidden pattern list//element(ref)\n", |
| 6183 | NULL, NULL); |
| 6184 | } |
| 6185 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6186 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM, |
| 6187 | "Found forbidden pattern attribute//element(ref)\n", |
| 6188 | NULL, NULL); |
| 6189 | } |
| 6190 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6191 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM, |
| 6192 | "Found forbidden pattern attribute//element(ref)\n", |
| 6193 | NULL, NULL); |
| 6194 | } |
| 6195 | /* |
| 6196 | * reset since in the simple form elements are only child |
| 6197 | * of grammar/define |
| 6198 | */ |
| 6199 | nflags = 0; |
| 6200 | ret = |
| 6201 | xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type); |
| 6202 | if (ret != XML_RELAXNG_CONTENT_EMPTY) { |
| 6203 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_EMPTY, |
| 6204 | "Element %s attributes have a content type error\n", |
| 6205 | cur->name, NULL); |
| 6206 | } |
| 6207 | ret = |
| 6208 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6209 | cur->type); |
| 6210 | if (ret == XML_RELAXNG_CONTENT_ERROR) { |
| 6211 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_ERROR, |
| 6212 | "Element %s has a content type error\n", |
| 6213 | cur->name, NULL); |
| 6214 | } else { |
| 6215 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6216 | } |
| 6217 | } else if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
| 6218 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6219 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ATTR, |
| 6220 | "Found forbidden pattern attribute//attribute\n", |
| 6221 | NULL, NULL); |
| 6222 | } |
| 6223 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6224 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ATTR, |
| 6225 | "Found forbidden pattern list//attribute\n", |
| 6226 | NULL, NULL); |
| 6227 | } |
| 6228 | if (flags & XML_RELAXNG_IN_OOMGROUP) { |
| 6229 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_GROUP_ATTR, |
| 6230 | "Found forbidden pattern oneOrMore//group//attribute\n", |
| 6231 | NULL, NULL); |
| 6232 | } |
| 6233 | if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) { |
| 6234 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_INTERLEAVE_ATTR, |
| 6235 | "Found forbidden pattern oneOrMore//interleave//attribute\n", |
| 6236 | NULL, NULL); |
| 6237 | } |
| 6238 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6239 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ATTR, |
| 6240 | "Found forbidden pattern data/except//attribute\n", |
| 6241 | NULL, NULL); |
| 6242 | } |
| 6243 | if (flags & XML_RELAXNG_IN_START) { |
| 6244 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ATTR, |
| 6245 | "Found forbidden pattern start//attribute\n", |
| 6246 | NULL, NULL); |
| 6247 | } |
| 6248 | if ((!(flags & XML_RELAXNG_IN_ONEORMORE)) |
| 6249 | && (cur->name == NULL)) { |
| 6250 | if (cur->ns == NULL) { |
| 6251 | xmlRngPErr(ctxt, cur->node, XML_RNGP_ANYNAME_ATTR_ANCESTOR, |
| 6252 | "Found anyName attribute without oneOrMore ancestor\n", |
| 6253 | NULL, NULL); |
| 6254 | } else { |
| 6255 | xmlRngPErr(ctxt, cur->node, XML_RNGP_NSNAME_ATTR_ANCESTOR, |
| 6256 | "Found nsName attribute without oneOrMore ancestor\n", |
| 6257 | NULL, NULL); |
| 6258 | } |
| 6259 | } |
| 6260 | nflags = flags | XML_RELAXNG_IN_ATTRIBUTE; |
| 6261 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6262 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6263 | } else if ((cur->type == XML_RELAXNG_ONEORMORE) || |
| 6264 | (cur->type == XML_RELAXNG_ZEROORMORE)) { |
| 6265 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6266 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ONEMORE, |
| 6267 | "Found forbidden pattern data/except//oneOrMore\n", |
| 6268 | NULL, NULL); |
| 6269 | } |
| 6270 | if (flags & XML_RELAXNG_IN_START) { |
| 6271 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ONEMORE, |
| 6272 | "Found forbidden pattern start//oneOrMore\n", |
| 6273 | NULL, NULL); |
| 6274 | } |
| 6275 | nflags = flags | XML_RELAXNG_IN_ONEORMORE; |
| 6276 | ret = |
| 6277 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6278 | cur->type); |
| 6279 | ret = xmlRelaxNGGroupContentType(ret, ret); |
| 6280 | } else if (cur->type == XML_RELAXNG_LIST) { |
| 6281 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6282 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_LIST, |
| 6283 | "Found forbidden pattern list//list\n", NULL, |
| 6284 | NULL); |
| 6285 | } |
| 6286 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6287 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_LIST, |
| 6288 | "Found forbidden pattern data/except//list\n", |
| 6289 | NULL, NULL); |
| 6290 | } |
| 6291 | if (flags & XML_RELAXNG_IN_START) { |
| 6292 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_LIST, |
| 6293 | "Found forbidden pattern start//list\n", NULL, |
| 6294 | NULL); |
| 6295 | } |
| 6296 | nflags = flags | XML_RELAXNG_IN_LIST; |
| 6297 | ret = |
| 6298 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6299 | cur->type); |
| 6300 | } else if (cur->type == XML_RELAXNG_GROUP) { |
| 6301 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6302 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_GROUP, |
| 6303 | "Found forbidden pattern data/except//group\n", |
| 6304 | NULL, NULL); |
| 6305 | } |
| 6306 | if (flags & XML_RELAXNG_IN_START) { |
| 6307 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_GROUP, |
| 6308 | "Found forbidden pattern start//group\n", NULL, |
| 6309 | NULL); |
| 6310 | } |
| 6311 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
| 6312 | nflags = flags | XML_RELAXNG_IN_OOMGROUP; |
| 6313 | else |
| 6314 | nflags = flags; |
| 6315 | ret = |
| 6316 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6317 | cur->type); |
| 6318 | /* |
| 6319 | * The 7.3 Attribute derivation rule for groups is plugged there |
| 6320 | */ |
| 6321 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
| 6322 | } else if (cur->type == XML_RELAXNG_INTERLEAVE) { |
| 6323 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6324 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_INTERLEAVE, |
| 6325 | "Found forbidden pattern list//interleave\n", |
| 6326 | NULL, NULL); |
| 6327 | } |
| 6328 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6329 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE, |
| 6330 | "Found forbidden pattern data/except//interleave\n", |
| 6331 | NULL, NULL); |
| 6332 | } |
| 6333 | if (flags & XML_RELAXNG_IN_START) { |
| 6334 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE, |
| 6335 | "Found forbidden pattern start//interleave\n", |
| 6336 | NULL, NULL); |
| 6337 | } |
| 6338 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
| 6339 | nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE; |
| 6340 | else |
| 6341 | nflags = flags; |
| 6342 | ret = |
| 6343 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6344 | cur->type); |
| 6345 | } else if (cur->type == XML_RELAXNG_EXCEPT) { |
| 6346 | if ((cur->parent != NULL) && |
| 6347 | (cur->parent->type == XML_RELAXNG_DATATYPE)) |
| 6348 | nflags = flags | XML_RELAXNG_IN_DATAEXCEPT; |
| 6349 | else |
| 6350 | nflags = flags; |
| 6351 | ret = |
| 6352 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, |
| 6353 | cur->type); |
| 6354 | } else if (cur->type == XML_RELAXNG_DATATYPE) { |
| 6355 | if (flags & XML_RELAXNG_IN_START) { |
| 6356 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_DATA, |
| 6357 | "Found forbidden pattern start//data\n", NULL, |
| 6358 | NULL); |
| 6359 | } |
| 6360 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6361 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
| 6362 | } else if (cur->type == XML_RELAXNG_VALUE) { |
| 6363 | if (flags & XML_RELAXNG_IN_START) { |
| 6364 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_VALUE, |
| 6365 | "Found forbidden pattern start//value\n", NULL, |
| 6366 | NULL); |
| 6367 | } |
| 6368 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6369 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
| 6370 | } else if (cur->type == XML_RELAXNG_TEXT) { |
| 6371 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6372 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_TEXT, |
| 6373 | "Found forbidden pattern list//text\n", NULL, |
| 6374 | NULL); |
| 6375 | } |
| 6376 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6377 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_TEXT, |
| 6378 | "Found forbidden pattern data/except//text\n", |
| 6379 | NULL, NULL); |
| 6380 | } |
| 6381 | if (flags & XML_RELAXNG_IN_START) { |
| 6382 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_TEXT, |
| 6383 | "Found forbidden pattern start//text\n", NULL, |
| 6384 | NULL); |
| 6385 | } |
| 6386 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6387 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 6388 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6389 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_EMPTY, |
| 6390 | "Found forbidden pattern data/except//empty\n", |
| 6391 | NULL, NULL); |
| 6392 | } |
| 6393 | if (flags & XML_RELAXNG_IN_START) { |
| 6394 | xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_EMPTY, |
| 6395 | "Found forbidden pattern start//empty\n", NULL, |
| 6396 | NULL); |
| 6397 | } |
| 6398 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6399 | } else if (cur->type == XML_RELAXNG_CHOICE) { |
| 6400 | xmlRelaxNGCheckChoiceDeterminism(ctxt, cur); |
| 6401 | ret = |
| 6402 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6403 | } else { |
| 6404 | ret = |
| 6405 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6406 | } |
| 6407 | cur = cur->next; |
| 6408 | if (ptype == XML_RELAXNG_GROUP) { |
| 6409 | val = xmlRelaxNGGroupContentType(val, ret); |
| 6410 | } else if (ptype == XML_RELAXNG_INTERLEAVE) { |
| 6411 | tmp = xmlRelaxNGGroupContentType(val, ret); |
| 6412 | if (tmp != XML_RELAXNG_CONTENT_ERROR) |
| 6413 | tmp = xmlRelaxNGMaxContentType(val, ret); |
| 6414 | } else if (ptype == XML_RELAXNG_CHOICE) { |
| 6415 | val = xmlRelaxNGMaxContentType(val, ret); |
| 6416 | } else if (ptype == XML_RELAXNG_LIST) { |
| 6417 | val = XML_RELAXNG_CONTENT_SIMPLE; |
| 6418 | } else if (ptype == XML_RELAXNG_EXCEPT) { |
| 6419 | if (ret == XML_RELAXNG_CONTENT_ERROR) |
| 6420 | val = XML_RELAXNG_CONTENT_ERROR; |
| 6421 | else |
| 6422 | val = XML_RELAXNG_CONTENT_SIMPLE; |
| 6423 | } else { |
| 6424 | val = xmlRelaxNGGroupContentType(val, ret); |
| 6425 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6426 | |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6427 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6428 | return (val); |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6429 | } |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6430 | |
| 6431 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6432 | * xmlRelaxNGParseGrammar: |
| 6433 | * @ctxt: a Relax-NG parser context |
| 6434 | * @nodes: grammar children nodes |
| 6435 | * |
| 6436 | * parse a Relax-NG <grammar> node |
| 6437 | * |
| 6438 | * Returns the internal xmlRelaxNGGrammarPtr built or |
| 6439 | * NULL in case of error |
| 6440 | */ |
| 6441 | static xmlRelaxNGGrammarPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6442 | xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) |
| 6443 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6444 | xmlRelaxNGGrammarPtr ret, tmp, old; |
| 6445 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6446 | #ifdef DEBUG_GRAMMAR |
| 6447 | xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n"); |
| 6448 | #endif |
| 6449 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6450 | ret = xmlRelaxNGNewGrammar(ctxt); |
| 6451 | if (ret == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6452 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6453 | |
| 6454 | /* |
| 6455 | * Link the new grammar in the tree |
| 6456 | */ |
| 6457 | ret->parent = ctxt->grammar; |
| 6458 | if (ctxt->grammar != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6459 | tmp = ctxt->grammar->children; |
| 6460 | if (tmp == NULL) { |
| 6461 | ctxt->grammar->children = ret; |
| 6462 | } else { |
| 6463 | while (tmp->next != NULL) |
| 6464 | tmp = tmp->next; |
| 6465 | tmp->next = ret; |
| 6466 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6467 | } |
| 6468 | |
| 6469 | old = ctxt->grammar; |
| 6470 | ctxt->grammar = ret; |
| 6471 | xmlRelaxNGParseGrammarContent(ctxt, nodes); |
| 6472 | ctxt->grammar = ret; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6473 | if (ctxt->grammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6474 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT, |
| 6475 | "Failed to parse <grammar> content\n", NULL, NULL); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6476 | } else if (ctxt->grammar->start == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6477 | xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_NO_START, |
| 6478 | "Element <grammar> has no <start>\n", NULL, NULL); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6479 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6480 | |
| 6481 | /* |
| 6482 | * Apply 4.17 mergingd rules to defines and starts |
| 6483 | */ |
| 6484 | xmlRelaxNGCombineStart(ctxt, ret); |
| 6485 | if (ret->defs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6486 | xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine, |
| 6487 | ctxt); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6488 | } |
| 6489 | |
| 6490 | /* |
| 6491 | * link together defines and refs in this grammar |
| 6492 | */ |
| 6493 | if (ret->refs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6494 | xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference, |
| 6495 | ctxt); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6496 | } |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 6497 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6498 | ctxt->grammar = old; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6499 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6500 | } |
| 6501 | |
| 6502 | /** |
| 6503 | * xmlRelaxNGParseDocument: |
| 6504 | * @ctxt: a Relax-NG parser context |
| 6505 | * @node: the root node of the RelaxNG schema |
| 6506 | * |
| 6507 | * parse a Relax-NG definition resource and build an internal |
| 6508 | * xmlRelaxNG struture which can be used to validate instances. |
| 6509 | * |
| 6510 | * Returns the internal XML RelaxNG structure built or |
| 6511 | * NULL in case of error |
| 6512 | */ |
| 6513 | static xmlRelaxNGPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6514 | xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 6515 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6516 | xmlRelaxNGPtr schema = NULL; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6517 | const xmlChar *olddefine; |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 6518 | xmlRelaxNGGrammarPtr old; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6519 | |
| 6520 | if ((ctxt == NULL) || (node == NULL)) |
| 6521 | return (NULL); |
| 6522 | |
| 6523 | schema = xmlRelaxNGNewRelaxNG(ctxt); |
| 6524 | if (schema == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6525 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6526 | |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6527 | olddefine = ctxt->define; |
| 6528 | ctxt->define = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6529 | if (IS_RELAXNG(node, "grammar")) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6530 | schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6531 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6532 | xmlRelaxNGGrammarPtr tmp, ret; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6533 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6534 | schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt); |
| 6535 | if (schema->topgrammar == NULL) { |
| 6536 | return (schema); |
| 6537 | } |
| 6538 | /* |
| 6539 | * Link the new grammar in the tree |
| 6540 | */ |
| 6541 | ret->parent = ctxt->grammar; |
| 6542 | if (ctxt->grammar != NULL) { |
| 6543 | tmp = ctxt->grammar->children; |
| 6544 | if (tmp == NULL) { |
| 6545 | ctxt->grammar->children = ret; |
| 6546 | } else { |
| 6547 | while (tmp->next != NULL) |
| 6548 | tmp = tmp->next; |
| 6549 | tmp->next = ret; |
| 6550 | } |
| 6551 | } |
| 6552 | old = ctxt->grammar; |
| 6553 | ctxt->grammar = ret; |
| 6554 | xmlRelaxNGParseStart(ctxt, node); |
| 6555 | if (old != NULL) |
| 6556 | ctxt->grammar = old; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6557 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6558 | ctxt->define = olddefine; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 6559 | if (schema->topgrammar->start != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6560 | xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0); |
| 6561 | if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) { |
| 6562 | xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL); |
| 6563 | while ((schema->topgrammar->start != NULL) && |
| 6564 | (schema->topgrammar->start->type == XML_RELAXNG_NOOP) && |
| 6565 | (schema->topgrammar->start->next != NULL)) |
| 6566 | schema->topgrammar->start = |
| 6567 | schema->topgrammar->start->content; |
| 6568 | xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start, |
| 6569 | XML_RELAXNG_IN_START, XML_RELAXNG_NOOP); |
| 6570 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 6571 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6572 | #ifdef DEBUG |
| 6573 | if (schema == NULL) |
| 6574 | xmlGenericError(xmlGenericErrorContext, |
| 6575 | "xmlRelaxNGParseDocument() failed\n"); |
| 6576 | #endif |
| 6577 | |
| 6578 | return (schema); |
| 6579 | } |
| 6580 | |
| 6581 | /************************************************************************ |
| 6582 | * * |
| 6583 | * Reading RelaxNGs * |
| 6584 | * * |
| 6585 | ************************************************************************/ |
| 6586 | |
| 6587 | /** |
| 6588 | * xmlRelaxNGNewParserCtxt: |
| 6589 | * @URL: the location of the schema |
| 6590 | * |
| 6591 | * Create an XML RelaxNGs parse context for that file/resource expected |
| 6592 | * to contain an XML RelaxNGs file. |
| 6593 | * |
| 6594 | * Returns the parser context or NULL in case of error |
| 6595 | */ |
| 6596 | xmlRelaxNGParserCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6597 | xmlRelaxNGNewParserCtxt(const char *URL) |
| 6598 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6599 | xmlRelaxNGParserCtxtPtr ret; |
| 6600 | |
| 6601 | if (URL == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6602 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6603 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6604 | ret = |
| 6605 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6606 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6607 | xmlRngPErrMemory(NULL, "building parser\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6608 | return (NULL); |
| 6609 | } |
| 6610 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6611 | ret->URL = xmlStrdup((const xmlChar *) URL); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 6612 | ret->error = xmlGenericError; |
| 6613 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6614 | return (ret); |
| 6615 | } |
| 6616 | |
| 6617 | /** |
| 6618 | * xmlRelaxNGNewMemParserCtxt: |
| 6619 | * @buffer: a pointer to a char array containing the schemas |
| 6620 | * @size: the size of the array |
| 6621 | * |
| 6622 | * Create an XML RelaxNGs parse context for that memory buffer expected |
| 6623 | * to contain an XML RelaxNGs file. |
| 6624 | * |
| 6625 | * Returns the parser context or NULL in case of error |
| 6626 | */ |
| 6627 | xmlRelaxNGParserCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6628 | xmlRelaxNGNewMemParserCtxt(const char *buffer, int size) |
| 6629 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6630 | xmlRelaxNGParserCtxtPtr ret; |
| 6631 | |
| 6632 | if ((buffer == NULL) || (size <= 0)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6633 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6634 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6635 | ret = |
| 6636 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6637 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6638 | xmlRngPErrMemory(NULL, "building parser\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6639 | return (NULL); |
| 6640 | } |
| 6641 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6642 | ret->buffer = buffer; |
| 6643 | ret->size = size; |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 6644 | ret->error = xmlGenericError; |
| 6645 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6646 | return (ret); |
| 6647 | } |
| 6648 | |
| 6649 | /** |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6650 | * xmlRelaxNGNewDocParserCtxt: |
| 6651 | * @doc: a preparsed document tree |
| 6652 | * |
| 6653 | * Create an XML RelaxNGs parser context for that document. |
| 6654 | * Note: since the process of compiling a RelaxNG schemas modifies the |
| 6655 | * document, the @doc parameter is duplicated internally. |
| 6656 | * |
| 6657 | * Returns the parser context or NULL in case of error |
| 6658 | */ |
| 6659 | xmlRelaxNGParserCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6660 | xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc) |
| 6661 | { |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6662 | xmlRelaxNGParserCtxtPtr ret; |
| 6663 | xmlDocPtr copy; |
| 6664 | |
| 6665 | if (doc == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6666 | return (NULL); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6667 | copy = xmlCopyDoc(doc, 1); |
| 6668 | if (copy == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6669 | return (NULL); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6670 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6671 | ret = |
| 6672 | (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6673 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6674 | xmlRngPErrMemory(NULL, "building parser\n"); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6675 | return (NULL); |
| 6676 | } |
| 6677 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6678 | ret->document = copy; |
Daniel Veillard | 4259532 | 2004-11-08 10:52:06 +0000 | [diff] [blame] | 6679 | ret->freedoc = 1; |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6680 | ret->userData = xmlGenericErrorContext; |
| 6681 | return (ret); |
| 6682 | } |
| 6683 | |
| 6684 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6685 | * xmlRelaxNGFreeParserCtxt: |
| 6686 | * @ctxt: the schema parser context |
| 6687 | * |
| 6688 | * Free the resources associated to the schema parser context |
| 6689 | */ |
| 6690 | void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6691 | xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt) |
| 6692 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6693 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6694 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6695 | if (ctxt->URL != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6696 | xmlFree(ctxt->URL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6697 | if (ctxt->doc != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6698 | xmlRelaxNGFreeDocument(ctxt->doc); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 6699 | if (ctxt->interleaves != NULL) |
| 6700 | xmlHashFree(ctxt->interleaves, NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6701 | if (ctxt->documents != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6702 | xmlRelaxNGFreeDocumentList(ctxt->documents); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 6703 | if (ctxt->includes != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6704 | xmlRelaxNGFreeIncludeList(ctxt->includes); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6705 | if (ctxt->docTab != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6706 | xmlFree(ctxt->docTab); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 6707 | if (ctxt->incTab != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6708 | xmlFree(ctxt->incTab); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6709 | if (ctxt->defTab != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6710 | int i; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6711 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6712 | for (i = 0; i < ctxt->defNr; i++) |
| 6713 | xmlRelaxNGFreeDefine(ctxt->defTab[i]); |
| 6714 | xmlFree(ctxt->defTab); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6715 | } |
Daniel Veillard | 4259532 | 2004-11-08 10:52:06 +0000 | [diff] [blame] | 6716 | if ((ctxt->document != NULL) && (ctxt->freedoc)) |
| 6717 | xmlFreeDoc(ctxt->document); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6718 | xmlFree(ctxt); |
| 6719 | } |
| 6720 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6721 | /** |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6722 | * xmlRelaxNGNormExtSpace: |
| 6723 | * @value: a value |
| 6724 | * |
| 6725 | * Removes the leading and ending spaces of the value |
| 6726 | * The string is modified "in situ" |
| 6727 | */ |
| 6728 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6729 | xmlRelaxNGNormExtSpace(xmlChar * value) |
| 6730 | { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6731 | xmlChar *start = value; |
| 6732 | xmlChar *cur = value; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6733 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6734 | if (value == NULL) |
| 6735 | return; |
| 6736 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6737 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6738 | cur++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6739 | if (cur == start) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6740 | do { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6741 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6742 | cur++; |
| 6743 | if (*cur == 0) |
| 6744 | return; |
| 6745 | start = cur; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6746 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6747 | cur++; |
| 6748 | if (*cur == 0) { |
| 6749 | *start = 0; |
| 6750 | return; |
| 6751 | } |
| 6752 | } while (1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6753 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6754 | do { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6755 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6756 | *start++ = *cur++; |
| 6757 | if (*cur == 0) { |
| 6758 | *start = 0; |
| 6759 | return; |
| 6760 | } |
| 6761 | /* don't try to normalize the inner spaces */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6762 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6763 | cur++; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6764 | if (*cur == 0) { |
| 6765 | *start = 0; |
| 6766 | return; |
| 6767 | } |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 6768 | *start++ = *cur++; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6769 | } while (1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6770 | } |
| 6771 | } |
| 6772 | |
| 6773 | /** |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 6774 | * xmlRelaxNGCleanupAttributes: |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6775 | * @ctxt: a Relax-NG parser context |
| 6776 | * @node: a Relax-NG node |
| 6777 | * |
| 6778 | * Check all the attributes on the given node |
| 6779 | */ |
| 6780 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6781 | xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
| 6782 | { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6783 | xmlAttrPtr cur, next; |
| 6784 | |
| 6785 | cur = node->properties; |
| 6786 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6787 | next = cur->next; |
| 6788 | if ((cur->ns == NULL) || |
| 6789 | (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
| 6790 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
| 6791 | if ((!xmlStrEqual(node->name, BAD_CAST "element")) && |
| 6792 | (!xmlStrEqual(node->name, BAD_CAST "attribute")) && |
| 6793 | (!xmlStrEqual(node->name, BAD_CAST "ref")) && |
| 6794 | (!xmlStrEqual(node->name, BAD_CAST "parentRef")) && |
| 6795 | (!xmlStrEqual(node->name, BAD_CAST "param")) && |
| 6796 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
| 6797 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6798 | "Attribute %s is not allowed on %s\n", |
| 6799 | cur->name, node->name); |
| 6800 | } |
| 6801 | } else if (xmlStrEqual(cur->name, BAD_CAST "type")) { |
| 6802 | if ((!xmlStrEqual(node->name, BAD_CAST "value")) && |
| 6803 | (!xmlStrEqual(node->name, BAD_CAST "data"))) { |
| 6804 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6805 | "Attribute %s is not allowed on %s\n", |
| 6806 | cur->name, node->name); |
| 6807 | } |
| 6808 | } else if (xmlStrEqual(cur->name, BAD_CAST "href")) { |
| 6809 | if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) && |
| 6810 | (!xmlStrEqual(node->name, BAD_CAST "include"))) { |
| 6811 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6812 | "Attribute %s is not allowed on %s\n", |
| 6813 | cur->name, node->name); |
| 6814 | } |
| 6815 | } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) { |
| 6816 | if ((!xmlStrEqual(node->name, BAD_CAST "start")) && |
| 6817 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
| 6818 | xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE, |
| 6819 | "Attribute %s is not allowed on %s\n", |
| 6820 | cur->name, node->name); |
| 6821 | } |
| 6822 | } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) { |
| 6823 | xmlChar *val; |
| 6824 | xmlURIPtr uri; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6825 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6826 | val = xmlNodeListGetString(node->doc, cur->children, 1); |
| 6827 | if (val != NULL) { |
| 6828 | if (val[0] != 0) { |
| 6829 | uri = xmlParseURI((const char *) val); |
| 6830 | if (uri == NULL) { |
| 6831 | xmlRngPErr(ctxt, node, XML_RNGP_INVALID_URI, |
| 6832 | "Attribute %s contains invalid URI %s\n", |
| 6833 | cur->name, val); |
| 6834 | } else { |
| 6835 | if (uri->scheme == NULL) { |
| 6836 | xmlRngPErr(ctxt, node, XML_RNGP_URI_NOT_ABSOLUTE, |
| 6837 | "Attribute %s URI %s is not absolute\n", |
| 6838 | cur->name, val); |
| 6839 | } |
| 6840 | if (uri->fragment != NULL) { |
| 6841 | xmlRngPErr(ctxt, node, XML_RNGP_URI_FRAGMENT, |
| 6842 | "Attribute %s URI %s has a fragment ID\n", |
| 6843 | cur->name, val); |
| 6844 | } |
| 6845 | xmlFreeURI(uri); |
| 6846 | } |
| 6847 | } |
| 6848 | xmlFree(val); |
| 6849 | } |
| 6850 | } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) { |
| 6851 | xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_ATTRIBUTE, |
| 6852 | "Unknown attribute %s on %s\n", cur->name, |
| 6853 | node->name); |
| 6854 | } |
| 6855 | } |
| 6856 | cur = next; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6857 | } |
| 6858 | } |
| 6859 | |
| 6860 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6861 | * xmlRelaxNGCleanupTree: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6862 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6863 | * @root: an xmlNodePtr subtree |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6864 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6865 | * Cleanup the subtree from unwanted nodes for parsing, resolve |
| 6866 | * Include and externalRef lookups. |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6867 | */ |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6868 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6869 | xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root) |
| 6870 | { |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6871 | xmlNodePtr cur, delete; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6872 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6873 | delete = NULL; |
| 6874 | cur = root; |
| 6875 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6876 | if (delete != NULL) { |
| 6877 | xmlUnlinkNode(delete); |
| 6878 | xmlFreeNode(delete); |
| 6879 | delete = NULL; |
| 6880 | } |
| 6881 | if (cur->type == XML_ELEMENT_NODE) { |
| 6882 | /* |
| 6883 | * Simplification 4.1. Annotations |
| 6884 | */ |
| 6885 | if ((cur->ns == NULL) || |
| 6886 | (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
| 6887 | if ((cur->parent != NULL) && |
| 6888 | (cur->parent->type == XML_ELEMENT_NODE) && |
| 6889 | ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) || |
| 6890 | (xmlStrEqual(cur->parent->name, BAD_CAST "value")) || |
| 6891 | (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) { |
| 6892 | xmlRngPErr(ctxt, cur, XML_RNGP_FOREIGN_ELEMENT, |
| 6893 | "element %s doesn't allow foreign elements\n", |
| 6894 | cur->parent->name, NULL); |
| 6895 | } |
| 6896 | delete = cur; |
| 6897 | goto skip_children; |
| 6898 | } else { |
| 6899 | xmlRelaxNGCleanupAttributes(ctxt, cur); |
| 6900 | if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) { |
| 6901 | xmlChar *href, *ns, *base, *URL; |
| 6902 | xmlRelaxNGDocumentPtr docu; |
| 6903 | xmlNodePtr tmp; |
Daniel Veillard | 6dc9196 | 2004-03-22 19:10:02 +0000 | [diff] [blame] | 6904 | xmlURIPtr uri; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6905 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6906 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6907 | if (ns == NULL) { |
| 6908 | tmp = cur->parent; |
| 6909 | while ((tmp != NULL) && |
| 6910 | (tmp->type == XML_ELEMENT_NODE)) { |
| 6911 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 6912 | if (ns != NULL) |
| 6913 | break; |
| 6914 | tmp = tmp->parent; |
| 6915 | } |
| 6916 | } |
| 6917 | href = xmlGetProp(cur, BAD_CAST "href"); |
| 6918 | if (href == NULL) { |
| 6919 | xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF, |
| 6920 | "xmlRelaxNGParse: externalRef has no href attribute\n", |
| 6921 | NULL, NULL); |
Daniel Veillard | f2e066a | 2005-06-30 13:04:44 +0000 | [diff] [blame] | 6922 | if (ns != NULL) |
| 6923 | xmlFree(ns); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6924 | delete = cur; |
| 6925 | goto skip_children; |
| 6926 | } |
Daniel Veillard | 6dc9196 | 2004-03-22 19:10:02 +0000 | [diff] [blame] | 6927 | uri = xmlParseURI((const char *) href); |
| 6928 | if (uri == NULL) { |
| 6929 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
| 6930 | "Incorrect URI for externalRef %s\n", |
| 6931 | href, NULL); |
Daniel Veillard | f2e066a | 2005-06-30 13:04:44 +0000 | [diff] [blame] | 6932 | if (ns != NULL) |
| 6933 | xmlFree(ns); |
Daniel Veillard | 6dc9196 | 2004-03-22 19:10:02 +0000 | [diff] [blame] | 6934 | if (href != NULL) |
| 6935 | xmlFree(href); |
| 6936 | delete = cur; |
| 6937 | goto skip_children; |
| 6938 | } |
| 6939 | if (uri->fragment != NULL) { |
| 6940 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
| 6941 | "Fragment forbidden in URI for externalRef %s\n", |
| 6942 | href, NULL); |
Daniel Veillard | f2e066a | 2005-06-30 13:04:44 +0000 | [diff] [blame] | 6943 | if (ns != NULL) |
| 6944 | xmlFree(ns); |
Daniel Veillard | 6dc9196 | 2004-03-22 19:10:02 +0000 | [diff] [blame] | 6945 | xmlFreeURI(uri); |
| 6946 | if (href != NULL) |
| 6947 | xmlFree(href); |
| 6948 | delete = cur; |
| 6949 | goto skip_children; |
| 6950 | } |
| 6951 | xmlFreeURI(uri); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6952 | base = xmlNodeGetBase(cur->doc, cur); |
| 6953 | URL = xmlBuildURI(href, base); |
| 6954 | if (URL == NULL) { |
| 6955 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
| 6956 | "Failed to compute URL for externalRef %s\n", |
| 6957 | href, NULL); |
Daniel Veillard | f2e066a | 2005-06-30 13:04:44 +0000 | [diff] [blame] | 6958 | if (ns != NULL) |
| 6959 | xmlFree(ns); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6960 | if (href != NULL) |
| 6961 | xmlFree(href); |
| 6962 | if (base != NULL) |
| 6963 | xmlFree(base); |
| 6964 | delete = cur; |
| 6965 | goto skip_children; |
| 6966 | } |
| 6967 | if (href != NULL) |
| 6968 | xmlFree(href); |
| 6969 | if (base != NULL) |
| 6970 | xmlFree(base); |
| 6971 | docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns); |
| 6972 | if (docu == NULL) { |
| 6973 | xmlRngPErr(ctxt, cur, XML_RNGP_EXTERNAL_REF_FAILURE, |
| 6974 | "Failed to load externalRef %s\n", URL, |
| 6975 | NULL); |
Daniel Veillard | f2e066a | 2005-06-30 13:04:44 +0000 | [diff] [blame] | 6976 | if (ns != NULL) |
| 6977 | xmlFree(ns); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6978 | xmlFree(URL); |
| 6979 | delete = cur; |
| 6980 | goto skip_children; |
| 6981 | } |
| 6982 | if (ns != NULL) |
| 6983 | xmlFree(ns); |
| 6984 | xmlFree(URL); |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 6985 | cur->psvi = docu; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6986 | } else if (xmlStrEqual(cur->name, BAD_CAST "include")) { |
| 6987 | xmlChar *href, *ns, *base, *URL; |
| 6988 | xmlRelaxNGIncludePtr incl; |
| 6989 | xmlNodePtr tmp; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6990 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 6991 | href = xmlGetProp(cur, BAD_CAST "href"); |
| 6992 | if (href == NULL) { |
| 6993 | xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF, |
| 6994 | "xmlRelaxNGParse: include has no href attribute\n", |
| 6995 | NULL, NULL); |
| 6996 | delete = cur; |
| 6997 | goto skip_children; |
| 6998 | } |
| 6999 | base = xmlNodeGetBase(cur->doc, cur); |
| 7000 | URL = xmlBuildURI(href, base); |
| 7001 | if (URL == NULL) { |
| 7002 | xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR, |
| 7003 | "Failed to compute URL for include %s\n", |
| 7004 | href, NULL); |
| 7005 | if (href != NULL) |
| 7006 | xmlFree(href); |
| 7007 | if (base != NULL) |
| 7008 | xmlFree(base); |
| 7009 | delete = cur; |
| 7010 | goto skip_children; |
| 7011 | } |
| 7012 | if (href != NULL) |
| 7013 | xmlFree(href); |
| 7014 | if (base != NULL) |
| 7015 | xmlFree(base); |
| 7016 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 7017 | if (ns == NULL) { |
| 7018 | tmp = cur->parent; |
| 7019 | while ((tmp != NULL) && |
| 7020 | (tmp->type == XML_ELEMENT_NODE)) { |
| 7021 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 7022 | if (ns != NULL) |
| 7023 | break; |
| 7024 | tmp = tmp->parent; |
| 7025 | } |
| 7026 | } |
| 7027 | incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns); |
| 7028 | if (ns != NULL) |
| 7029 | xmlFree(ns); |
| 7030 | if (incl == NULL) { |
| 7031 | xmlRngPErr(ctxt, cur, XML_RNGP_INCLUDE_FAILURE, |
| 7032 | "Failed to load include %s\n", URL, |
| 7033 | NULL); |
| 7034 | xmlFree(URL); |
| 7035 | delete = cur; |
| 7036 | goto skip_children; |
| 7037 | } |
| 7038 | xmlFree(URL); |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 7039 | cur->psvi = incl; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7040 | } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) || |
| 7041 | (xmlStrEqual(cur->name, BAD_CAST "attribute"))) |
| 7042 | { |
| 7043 | xmlChar *name, *ns; |
| 7044 | xmlNodePtr text = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7045 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7046 | /* |
| 7047 | * Simplification 4.8. name attribute of element |
| 7048 | * and attribute elements |
| 7049 | */ |
| 7050 | name = xmlGetProp(cur, BAD_CAST "name"); |
| 7051 | if (name != NULL) { |
| 7052 | if (cur->children == NULL) { |
| 7053 | text = |
| 7054 | xmlNewChild(cur, cur->ns, BAD_CAST "name", |
| 7055 | name); |
| 7056 | } else { |
| 7057 | xmlNodePtr node; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7058 | |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 7059 | node = xmlNewDocNode(cur->doc, cur->ns, |
| 7060 | BAD_CAST "name", NULL); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7061 | if (node != NULL) { |
| 7062 | xmlAddPrevSibling(cur->children, node); |
| 7063 | text = xmlNewText(name); |
| 7064 | xmlAddChild(node, text); |
| 7065 | text = node; |
| 7066 | } |
| 7067 | } |
| 7068 | if (text == NULL) { |
| 7069 | xmlRngPErr(ctxt, cur, XML_RNGP_CREATE_FAILURE, |
| 7070 | "Failed to create a name %s element\n", |
| 7071 | name, NULL); |
| 7072 | } |
| 7073 | xmlUnsetProp(cur, BAD_CAST "name"); |
| 7074 | xmlFree(name); |
| 7075 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 7076 | if (ns != NULL) { |
| 7077 | if (text != NULL) { |
| 7078 | xmlSetProp(text, BAD_CAST "ns", ns); |
| 7079 | /* xmlUnsetProp(cur, BAD_CAST "ns"); */ |
| 7080 | } |
| 7081 | xmlFree(ns); |
| 7082 | } else if (xmlStrEqual(cur->name, |
| 7083 | BAD_CAST "attribute")) { |
| 7084 | xmlSetProp(text, BAD_CAST "ns", BAD_CAST ""); |
| 7085 | } |
| 7086 | } |
| 7087 | } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) || |
| 7088 | (xmlStrEqual(cur->name, BAD_CAST "nsName")) || |
| 7089 | (xmlStrEqual(cur->name, BAD_CAST "value"))) { |
| 7090 | /* |
| 7091 | * Simplification 4.8. name attribute of element |
| 7092 | * and attribute elements |
| 7093 | */ |
| 7094 | if (xmlHasProp(cur, BAD_CAST "ns") == NULL) { |
| 7095 | xmlNodePtr node; |
| 7096 | xmlChar *ns = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7097 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7098 | node = cur->parent; |
| 7099 | while ((node != NULL) && |
| 7100 | (node->type == XML_ELEMENT_NODE)) { |
| 7101 | ns = xmlGetProp(node, BAD_CAST "ns"); |
| 7102 | if (ns != NULL) { |
| 7103 | break; |
| 7104 | } |
| 7105 | node = node->parent; |
| 7106 | } |
| 7107 | if (ns == NULL) { |
| 7108 | xmlSetProp(cur, BAD_CAST "ns", BAD_CAST ""); |
| 7109 | } else { |
| 7110 | xmlSetProp(cur, BAD_CAST "ns", ns); |
| 7111 | xmlFree(ns); |
| 7112 | } |
| 7113 | } |
| 7114 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
| 7115 | xmlChar *name, *local, *prefix; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7116 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7117 | /* |
| 7118 | * Simplification: 4.10. QNames |
| 7119 | */ |
| 7120 | name = xmlNodeGetContent(cur); |
| 7121 | if (name != NULL) { |
| 7122 | local = xmlSplitQName2(name, &prefix); |
| 7123 | if (local != NULL) { |
| 7124 | xmlNsPtr ns; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7125 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7126 | ns = xmlSearchNs(cur->doc, cur, prefix); |
| 7127 | if (ns == NULL) { |
| 7128 | xmlRngPErr(ctxt, cur, |
| 7129 | XML_RNGP_PREFIX_UNDEFINED, |
| 7130 | "xmlRelaxNGParse: no namespace for prefix %s\n", |
| 7131 | prefix, NULL); |
| 7132 | } else { |
| 7133 | xmlSetProp(cur, BAD_CAST "ns", |
| 7134 | ns->href); |
| 7135 | xmlNodeSetContent(cur, local); |
| 7136 | } |
| 7137 | xmlFree(local); |
| 7138 | xmlFree(prefix); |
| 7139 | } |
| 7140 | xmlFree(name); |
| 7141 | } |
| 7142 | } |
| 7143 | /* |
| 7144 | * 4.16 |
| 7145 | */ |
| 7146 | if (xmlStrEqual(cur->name, BAD_CAST "nsName")) { |
| 7147 | if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
| 7148 | xmlRngPErr(ctxt, cur, |
| 7149 | XML_RNGP_PAT_NSNAME_EXCEPT_NSNAME, |
| 7150 | "Found nsName/except//nsName forbidden construct\n", |
| 7151 | NULL, NULL); |
| 7152 | } |
| 7153 | } |
| 7154 | } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) && |
| 7155 | (cur != root)) { |
| 7156 | int oldflags = ctxt->flags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7157 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7158 | /* |
| 7159 | * 4.16 |
| 7160 | */ |
| 7161 | if ((cur->parent != NULL) && |
| 7162 | (xmlStrEqual |
| 7163 | (cur->parent->name, BAD_CAST "anyName"))) { |
| 7164 | ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT; |
| 7165 | xmlRelaxNGCleanupTree(ctxt, cur); |
| 7166 | ctxt->flags = oldflags; |
| 7167 | goto skip_children; |
| 7168 | } else if ((cur->parent != NULL) && |
| 7169 | (xmlStrEqual |
| 7170 | (cur->parent->name, BAD_CAST "nsName"))) { |
| 7171 | ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT; |
| 7172 | xmlRelaxNGCleanupTree(ctxt, cur); |
| 7173 | ctxt->flags = oldflags; |
| 7174 | goto skip_children; |
| 7175 | } |
| 7176 | } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) { |
| 7177 | /* |
| 7178 | * 4.16 |
| 7179 | */ |
| 7180 | if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) { |
| 7181 | xmlRngPErr(ctxt, cur, |
| 7182 | XML_RNGP_PAT_ANYNAME_EXCEPT_ANYNAME, |
| 7183 | "Found anyName/except//anyName forbidden construct\n", |
| 7184 | NULL, NULL); |
| 7185 | } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
| 7186 | xmlRngPErr(ctxt, cur, |
| 7187 | XML_RNGP_PAT_NSNAME_EXCEPT_ANYNAME, |
| 7188 | "Found nsName/except//anyName forbidden construct\n", |
| 7189 | NULL, NULL); |
| 7190 | } |
| 7191 | } |
| 7192 | /* |
| 7193 | * Thisd is not an else since "include" is transformed |
| 7194 | * into a div |
| 7195 | */ |
| 7196 | if (xmlStrEqual(cur->name, BAD_CAST "div")) { |
| 7197 | xmlChar *ns; |
| 7198 | xmlNodePtr child, ins, tmp; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7199 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7200 | /* |
| 7201 | * implements rule 4.11 |
| 7202 | */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7203 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7204 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 7205 | |
| 7206 | child = cur->children; |
| 7207 | ins = cur; |
| 7208 | while (child != NULL) { |
| 7209 | if (ns != NULL) { |
| 7210 | if (!xmlHasProp(child, BAD_CAST "ns")) { |
| 7211 | xmlSetProp(child, BAD_CAST "ns", ns); |
| 7212 | } |
| 7213 | } |
| 7214 | tmp = child->next; |
| 7215 | xmlUnlinkNode(child); |
| 7216 | ins = xmlAddNextSibling(ins, child); |
| 7217 | child = tmp; |
| 7218 | } |
| 7219 | if (ns != NULL) |
| 7220 | xmlFree(ns); |
William M. Brack | 8eabb05 | 2004-06-07 14:15:54 +0000 | [diff] [blame] | 7221 | /* |
| 7222 | * Since we are about to delete cur, if it's nsDef is non-NULL we |
| 7223 | * need to preserve it (it contains the ns definitions for the |
| 7224 | * children we just moved). We'll just stick it on to the end |
| 7225 | * of cur->parent's list, since it's never going to be re-serialized |
| 7226 | * (bug 143738). |
| 7227 | */ |
| 7228 | if (cur->nsDef != NULL) { |
| 7229 | xmlNsPtr parDef = (xmlNsPtr)&cur->parent->nsDef; |
| 7230 | while (parDef->next != NULL) |
| 7231 | parDef = parDef->next; |
| 7232 | parDef->next = cur->nsDef; |
| 7233 | cur->nsDef = NULL; |
| 7234 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7235 | delete = cur; |
| 7236 | goto skip_children; |
| 7237 | } |
| 7238 | } |
| 7239 | } |
| 7240 | /* |
| 7241 | * Simplification 4.2 whitespaces |
| 7242 | */ |
| 7243 | else if ((cur->type == XML_TEXT_NODE) || |
| 7244 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 7245 | if (IS_BLANK_NODE(cur)) { |
| 7246 | if (cur->parent->type == XML_ELEMENT_NODE) { |
| 7247 | if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value")) |
| 7248 | && |
| 7249 | (!xmlStrEqual |
| 7250 | (cur->parent->name, BAD_CAST "param"))) |
| 7251 | delete = cur; |
| 7252 | } else { |
| 7253 | delete = cur; |
| 7254 | goto skip_children; |
| 7255 | } |
| 7256 | } |
| 7257 | } else { |
| 7258 | delete = cur; |
| 7259 | goto skip_children; |
| 7260 | } |
| 7261 | |
| 7262 | /* |
| 7263 | * Skip to next node |
| 7264 | */ |
| 7265 | if (cur->children != NULL) { |
| 7266 | if ((cur->children->type != XML_ENTITY_DECL) && |
| 7267 | (cur->children->type != XML_ENTITY_REF_NODE) && |
| 7268 | (cur->children->type != XML_ENTITY_NODE)) { |
| 7269 | cur = cur->children; |
| 7270 | continue; |
| 7271 | } |
| 7272 | } |
| 7273 | skip_children: |
| 7274 | if (cur->next != NULL) { |
| 7275 | cur = cur->next; |
| 7276 | continue; |
| 7277 | } |
| 7278 | |
| 7279 | do { |
| 7280 | cur = cur->parent; |
| 7281 | if (cur == NULL) |
| 7282 | break; |
| 7283 | if (cur == root) { |
| 7284 | cur = NULL; |
| 7285 | break; |
| 7286 | } |
| 7287 | if (cur->next != NULL) { |
| 7288 | cur = cur->next; |
| 7289 | break; |
| 7290 | } |
| 7291 | } while (cur != NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7292 | } |
| 7293 | if (delete != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7294 | xmlUnlinkNode(delete); |
| 7295 | xmlFreeNode(delete); |
| 7296 | delete = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7297 | } |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7298 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7299 | |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7300 | /** |
| 7301 | * xmlRelaxNGCleanupDoc: |
| 7302 | * @ctxt: a Relax-NG parser context |
| 7303 | * @doc: an xmldocPtr document pointer |
| 7304 | * |
| 7305 | * Cleanup the document from unwanted nodes for parsing, resolve |
| 7306 | * Include and externalRef lookups. |
| 7307 | * |
| 7308 | * Returns the cleaned up document or NULL in case of error |
| 7309 | */ |
| 7310 | static xmlDocPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7311 | xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc) |
| 7312 | { |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7313 | xmlNodePtr root; |
| 7314 | |
| 7315 | /* |
| 7316 | * Extract the root |
| 7317 | */ |
| 7318 | root = xmlDocGetRootElement(doc); |
| 7319 | if (root == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7320 | xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n", |
| 7321 | ctxt->URL, NULL); |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7322 | return (NULL); |
| 7323 | } |
| 7324 | xmlRelaxNGCleanupTree(ctxt, root); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7325 | return (doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7326 | } |
| 7327 | |
| 7328 | /** |
| 7329 | * xmlRelaxNGParse: |
| 7330 | * @ctxt: a Relax-NG parser context |
| 7331 | * |
| 7332 | * parse a schema definition resource and build an internal |
| 7333 | * XML Shema struture which can be used to validate instances. |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7334 | * |
| 7335 | * Returns the internal XML RelaxNG structure built from the resource or |
| 7336 | * NULL in case of error |
| 7337 | */ |
| 7338 | xmlRelaxNGPtr |
| 7339 | xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt) |
| 7340 | { |
| 7341 | xmlRelaxNGPtr ret = NULL; |
| 7342 | xmlDocPtr doc; |
| 7343 | xmlNodePtr root; |
| 7344 | |
| 7345 | xmlRelaxNGInitTypes(); |
| 7346 | |
| 7347 | if (ctxt == NULL) |
| 7348 | return (NULL); |
| 7349 | |
| 7350 | /* |
| 7351 | * First step is to parse the input document into an DOM/Infoset |
| 7352 | */ |
| 7353 | if (ctxt->URL != NULL) { |
Daniel Veillard | 87247e8 | 2004-01-13 20:42:02 +0000 | [diff] [blame] | 7354 | doc = xmlReadFile((const char *) ctxt->URL,NULL,0); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7355 | if (doc == NULL) { |
| 7356 | xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR, |
| 7357 | "xmlRelaxNGParse: could not load %s\n", ctxt->URL, |
| 7358 | NULL); |
| 7359 | return (NULL); |
| 7360 | } |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7361 | } else if (ctxt->buffer != NULL) { |
Daniel Veillard | 87247e8 | 2004-01-13 20:42:02 +0000 | [diff] [blame] | 7362 | doc = xmlReadMemory(ctxt->buffer, ctxt->size,NULL,NULL,0); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7363 | if (doc == NULL) { |
| 7364 | xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR, |
| 7365 | "xmlRelaxNGParse: could not parse schemas\n", NULL, |
| 7366 | NULL); |
| 7367 | return (NULL); |
| 7368 | } |
| 7369 | doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
| 7370 | ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 7371 | } else if (ctxt->document != NULL) { |
| 7372 | doc = ctxt->document; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7373 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7374 | xmlRngPErr(ctxt, NULL, XML_RNGP_EMPTY, |
| 7375 | "xmlRelaxNGParse: nothing to parse\n", NULL, NULL); |
| 7376 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7377 | } |
| 7378 | ctxt->document = doc; |
| 7379 | |
| 7380 | /* |
| 7381 | * Some preprocessing of the document content |
| 7382 | */ |
| 7383 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 7384 | if (doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7385 | xmlFreeDoc(ctxt->document); |
| 7386 | ctxt->document = NULL; |
| 7387 | return (NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7388 | } |
| 7389 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7390 | /* |
| 7391 | * Then do the parsing for good |
| 7392 | */ |
| 7393 | root = xmlDocGetRootElement(doc); |
| 7394 | if (root == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7395 | xmlRngPErr(ctxt, (xmlNodePtr) doc, |
| 7396 | XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n", |
| 7397 | ctxt->URL, NULL); |
| 7398 | xmlFreeDoc(doc); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7399 | return (NULL); |
| 7400 | } |
| 7401 | ret = xmlRelaxNGParseDocument(ctxt, root); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7402 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7403 | xmlFreeDoc(doc); |
| 7404 | return (NULL); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7405 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7406 | |
| 7407 | /* |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7408 | * Check the ref/defines links |
| 7409 | */ |
| 7410 | /* |
| 7411 | * try to preprocess interleaves |
| 7412 | */ |
| 7413 | if (ctxt->interleaves != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7414 | xmlHashScan(ctxt->interleaves, |
| 7415 | (xmlHashScanner) xmlRelaxNGComputeInterleaves, ctxt); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7416 | } |
| 7417 | |
| 7418 | /* |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7419 | * if there was a parsing error return NULL |
| 7420 | */ |
| 7421 | if (ctxt->nbErrors > 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7422 | xmlRelaxNGFree(ret); |
| 7423 | ctxt->document = NULL; |
| 7424 | xmlFreeDoc(doc); |
| 7425 | return (NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7426 | } |
| 7427 | |
| 7428 | /* |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 7429 | * try to compile (parts of) the schemas |
| 7430 | */ |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7431 | if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) { |
| 7432 | if (ret->topgrammar->start->type != XML_RELAXNG_START) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7433 | xmlRelaxNGDefinePtr def; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7434 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7435 | def = xmlRelaxNGNewDefine(ctxt, NULL); |
| 7436 | if (def != NULL) { |
| 7437 | def->type = XML_RELAXNG_START; |
| 7438 | def->content = ret->topgrammar->start; |
| 7439 | ret->topgrammar->start = def; |
| 7440 | } |
| 7441 | } |
| 7442 | xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7443 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 7444 | |
| 7445 | /* |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7446 | * Transfer the pointer for cleanup at the schema level. |
| 7447 | */ |
| 7448 | ret->doc = doc; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7449 | ctxt->document = NULL; |
| 7450 | ret->documents = ctxt->documents; |
| 7451 | ctxt->documents = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7452 | |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 7453 | ret->includes = ctxt->includes; |
| 7454 | ctxt->includes = NULL; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7455 | ret->defNr = ctxt->defNr; |
| 7456 | ret->defTab = ctxt->defTab; |
| 7457 | ctxt->defTab = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7458 | if (ctxt->idref == 1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7459 | ret->idref = 1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7460 | |
| 7461 | return (ret); |
| 7462 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7463 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7464 | /** |
| 7465 | * xmlRelaxNGSetParserErrors: |
| 7466 | * @ctxt: a Relax-NG validation context |
| 7467 | * @err: the error callback |
| 7468 | * @warn: the warning callback |
| 7469 | * @ctx: contextual data for the callbacks |
| 7470 | * |
| 7471 | * Set the callback functions used to handle errors for a validation context |
| 7472 | */ |
| 7473 | void |
| 7474 | xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7475 | xmlRelaxNGValidityErrorFunc err, |
| 7476 | xmlRelaxNGValidityWarningFunc warn, void *ctx) |
| 7477 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7478 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7479 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7480 | ctxt->error = err; |
| 7481 | ctxt->warning = warn; |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 7482 | ctxt->serror = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7483 | ctxt->userData = ctx; |
| 7484 | } |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 7485 | |
| 7486 | /** |
| 7487 | * xmlRelaxNGGetParserErrors: |
| 7488 | * @ctxt: a Relax-NG validation context |
| 7489 | * @err: the error callback result |
| 7490 | * @warn: the warning callback result |
| 7491 | * @ctx: contextual data for the callbacks result |
| 7492 | * |
| 7493 | * Get the callback information used to handle errors for a validation context |
| 7494 | * |
| 7495 | * Returns -1 in case of failure, 0 otherwise. |
| 7496 | */ |
| 7497 | int |
| 7498 | xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7499 | xmlRelaxNGValidityErrorFunc * err, |
| 7500 | xmlRelaxNGValidityWarningFunc * warn, void **ctx) |
| 7501 | { |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 7502 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7503 | return (-1); |
| 7504 | if (err != NULL) |
| 7505 | *err = ctxt->error; |
| 7506 | if (warn != NULL) |
| 7507 | *warn = ctxt->warning; |
| 7508 | if (ctx != NULL) |
| 7509 | *ctx = ctxt->userData; |
| 7510 | return (0); |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 7511 | } |
| 7512 | |
Kasimier T. Buchcik | a930fbe | 2006-01-09 16:28:20 +0000 | [diff] [blame] | 7513 | void |
| 7514 | xmlRelaxNGSetParserStructuredErrors( |
| 7515 | xmlRelaxNGParserCtxtPtr ctxt, |
| 7516 | xmlStructuredErrorFunc serror, |
| 7517 | void *ctx) |
| 7518 | { |
| 7519 | if (ctxt == NULL) |
| 7520 | return; |
| 7521 | ctxt->serror = serror; |
| 7522 | ctxt->error = NULL; |
| 7523 | ctxt->warning = NULL; |
| 7524 | ctxt->userData = ctx; |
| 7525 | } |
| 7526 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 7527 | #ifdef LIBXML_OUTPUT_ENABLED |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7528 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7529 | /************************************************************************ |
| 7530 | * * |
| 7531 | * Dump back a compiled form * |
| 7532 | * * |
| 7533 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7534 | static void xmlRelaxNGDumpDefine(FILE * output, |
| 7535 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7536 | |
| 7537 | /** |
| 7538 | * xmlRelaxNGDumpDefines: |
| 7539 | * @output: the file output |
| 7540 | * @defines: a list of define structures |
| 7541 | * |
| 7542 | * Dump a RelaxNG structure back |
| 7543 | */ |
| 7544 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7545 | xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines) |
| 7546 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7547 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7548 | xmlRelaxNGDumpDefine(output, defines); |
| 7549 | defines = defines->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7550 | } |
| 7551 | } |
| 7552 | |
| 7553 | /** |
| 7554 | * xmlRelaxNGDumpDefine: |
| 7555 | * @output: the file output |
| 7556 | * @define: a define structure |
| 7557 | * |
| 7558 | * Dump a RelaxNG structure back |
| 7559 | */ |
| 7560 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7561 | xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define) |
| 7562 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7563 | if (define == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7564 | return; |
| 7565 | switch (define->type) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7566 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7567 | fprintf(output, "<empty/>\n"); |
| 7568 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7569 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7570 | fprintf(output, "<notAllowed/>\n"); |
| 7571 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7572 | case XML_RELAXNG_TEXT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7573 | fprintf(output, "<text/>\n"); |
| 7574 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7575 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7576 | fprintf(output, "<element>\n"); |
| 7577 | if (define->name != NULL) { |
| 7578 | fprintf(output, "<name"); |
| 7579 | if (define->ns != NULL) |
| 7580 | fprintf(output, " ns=\"%s\"", define->ns); |
| 7581 | fprintf(output, ">%s</name>\n", define->name); |
| 7582 | } |
| 7583 | xmlRelaxNGDumpDefines(output, define->attrs); |
| 7584 | xmlRelaxNGDumpDefines(output, define->content); |
| 7585 | fprintf(output, "</element>\n"); |
| 7586 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7587 | case XML_RELAXNG_LIST: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7588 | fprintf(output, "<list>\n"); |
| 7589 | xmlRelaxNGDumpDefines(output, define->content); |
| 7590 | fprintf(output, "</list>\n"); |
| 7591 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7592 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7593 | fprintf(output, "<oneOrMore>\n"); |
| 7594 | xmlRelaxNGDumpDefines(output, define->content); |
| 7595 | fprintf(output, "</oneOrMore>\n"); |
| 7596 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7597 | case XML_RELAXNG_ZEROORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7598 | fprintf(output, "<zeroOrMore>\n"); |
| 7599 | xmlRelaxNGDumpDefines(output, define->content); |
| 7600 | fprintf(output, "</zeroOrMore>\n"); |
| 7601 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7602 | case XML_RELAXNG_CHOICE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7603 | fprintf(output, "<choice>\n"); |
| 7604 | xmlRelaxNGDumpDefines(output, define->content); |
| 7605 | fprintf(output, "</choice>\n"); |
| 7606 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7607 | case XML_RELAXNG_GROUP: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7608 | fprintf(output, "<group>\n"); |
| 7609 | xmlRelaxNGDumpDefines(output, define->content); |
| 7610 | fprintf(output, "</group>\n"); |
| 7611 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7612 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7613 | fprintf(output, "<interleave>\n"); |
| 7614 | xmlRelaxNGDumpDefines(output, define->content); |
| 7615 | fprintf(output, "</interleave>\n"); |
| 7616 | break; |
| 7617 | case XML_RELAXNG_OPTIONAL: |
| 7618 | fprintf(output, "<optional>\n"); |
| 7619 | xmlRelaxNGDumpDefines(output, define->content); |
| 7620 | fprintf(output, "</optional>\n"); |
| 7621 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7622 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7623 | fprintf(output, "<attribute>\n"); |
| 7624 | xmlRelaxNGDumpDefines(output, define->content); |
| 7625 | fprintf(output, "</attribute>\n"); |
| 7626 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7627 | case XML_RELAXNG_DEF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7628 | fprintf(output, "<define"); |
| 7629 | if (define->name != NULL) |
| 7630 | fprintf(output, " name=\"%s\"", define->name); |
| 7631 | fprintf(output, ">\n"); |
| 7632 | xmlRelaxNGDumpDefines(output, define->content); |
| 7633 | fprintf(output, "</define>\n"); |
| 7634 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7635 | case XML_RELAXNG_REF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7636 | fprintf(output, "<ref"); |
| 7637 | if (define->name != NULL) |
| 7638 | fprintf(output, " name=\"%s\"", define->name); |
| 7639 | fprintf(output, ">\n"); |
| 7640 | xmlRelaxNGDumpDefines(output, define->content); |
| 7641 | fprintf(output, "</ref>\n"); |
| 7642 | break; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7643 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7644 | fprintf(output, "<parentRef"); |
| 7645 | if (define->name != NULL) |
| 7646 | fprintf(output, " name=\"%s\"", define->name); |
| 7647 | fprintf(output, ">\n"); |
| 7648 | xmlRelaxNGDumpDefines(output, define->content); |
| 7649 | fprintf(output, "</parentRef>\n"); |
| 7650 | break; |
| 7651 | case XML_RELAXNG_EXTERNALREF: |
| 7652 | fprintf(output, "<externalRef>"); |
| 7653 | xmlRelaxNGDumpDefines(output, define->content); |
| 7654 | fprintf(output, "</externalRef>\n"); |
| 7655 | break; |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 7656 | case XML_RELAXNG_DATATYPE: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7657 | case XML_RELAXNG_VALUE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7658 | TODO break; |
| 7659 | case XML_RELAXNG_START: |
| 7660 | case XML_RELAXNG_EXCEPT: |
| 7661 | case XML_RELAXNG_PARAM: |
| 7662 | TODO break; |
| 7663 | case XML_RELAXNG_NOOP: |
| 7664 | xmlRelaxNGDumpDefines(output, define->content); |
| 7665 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7666 | } |
| 7667 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7668 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7669 | /** |
| 7670 | * xmlRelaxNGDumpGrammar: |
| 7671 | * @output: the file output |
| 7672 | * @grammar: a grammar structure |
| 7673 | * @top: is this a top grammar |
| 7674 | * |
| 7675 | * Dump a RelaxNG structure back |
| 7676 | */ |
| 7677 | static void |
| 7678 | xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top) |
| 7679 | { |
| 7680 | if (grammar == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7681 | return; |
| 7682 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7683 | fprintf(output, "<grammar"); |
| 7684 | if (top) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7685 | fprintf(output, " xmlns=\"http://relaxng.org/ns/structure/1.0\""); |
| 7686 | switch (grammar->combine) { |
| 7687 | case XML_RELAXNG_COMBINE_UNDEFINED: |
| 7688 | break; |
| 7689 | case XML_RELAXNG_COMBINE_CHOICE: |
| 7690 | fprintf(output, " combine=\"choice\""); |
| 7691 | break; |
| 7692 | case XML_RELAXNG_COMBINE_INTERLEAVE: |
| 7693 | fprintf(output, " combine=\"interleave\""); |
| 7694 | break; |
| 7695 | default: |
| 7696 | fprintf(output, " <!-- invalid combine value -->"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7697 | } |
| 7698 | fprintf(output, ">\n"); |
| 7699 | if (grammar->start == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7700 | fprintf(output, " <!-- grammar had no start -->"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7701 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7702 | fprintf(output, "<start>\n"); |
| 7703 | xmlRelaxNGDumpDefine(output, grammar->start); |
| 7704 | fprintf(output, "</start>\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7705 | } |
| 7706 | /* TODO ? Dump the defines ? */ |
| 7707 | fprintf(output, "</grammar>\n"); |
| 7708 | } |
| 7709 | |
| 7710 | /** |
| 7711 | * xmlRelaxNGDump: |
| 7712 | * @output: the file output |
| 7713 | * @schema: a schema structure |
| 7714 | * |
| 7715 | * Dump a RelaxNG structure back |
| 7716 | */ |
| 7717 | void |
| 7718 | xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema) |
| 7719 | { |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 7720 | if (output == NULL) |
| 7721 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7722 | if (schema == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7723 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
| 7724 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7725 | } |
| 7726 | fprintf(output, "RelaxNG: "); |
| 7727 | if (schema->doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7728 | fprintf(output, "no document\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7729 | } else if (schema->doc->URL != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7730 | fprintf(output, "%s\n", schema->doc->URL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7731 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7732 | fprintf(output, "\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7733 | } |
| 7734 | if (schema->topgrammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7735 | fprintf(output, "RelaxNG has no top grammar\n"); |
| 7736 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7737 | } |
| 7738 | xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1); |
| 7739 | } |
| 7740 | |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7741 | /** |
| 7742 | * xmlRelaxNGDumpTree: |
| 7743 | * @output: the file output |
| 7744 | * @schema: a schema structure |
| 7745 | * |
| 7746 | * Dump the transformed RelaxNG tree. |
| 7747 | */ |
| 7748 | void |
| 7749 | xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema) |
| 7750 | { |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 7751 | if (output == NULL) |
| 7752 | return; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7753 | if (schema == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7754 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
| 7755 | return; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7756 | } |
| 7757 | if (schema->doc == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7758 | fprintf(output, "no document\n"); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7759 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7760 | xmlDocDump(output, schema->doc); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7761 | } |
| 7762 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 7763 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7764 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7765 | /************************************************************************ |
| 7766 | * * |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7767 | * Validation of compiled content * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7768 | * * |
| 7769 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7770 | static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
| 7771 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7772 | |
| 7773 | /** |
| 7774 | * xmlRelaxNGValidateCompiledCallback: |
| 7775 | * @exec: the regular expression instance |
| 7776 | * @token: the token which matched |
| 7777 | * @transdata: callback data, the define for the subelement if available |
| 7778 | @ @inputdata: callback data, the Relax NG validation context |
| 7779 | * |
| 7780 | * Handle the callback and if needed validate the element children. |
| 7781 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7782 | static void |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7783 | xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7784 | const xmlChar * token, |
| 7785 | void *transdata, void *inputdata) |
| 7786 | { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7787 | xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata; |
| 7788 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata; |
| 7789 | int ret; |
| 7790 | |
| 7791 | #ifdef DEBUG_COMPILE |
| 7792 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7793 | "Compiled callback for: '%s'\n", token); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7794 | #endif |
| 7795 | if (ctxt == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7796 | fprintf(stderr, "callback on %s missing context\n", token); |
| 7797 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7798 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7799 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7800 | } |
| 7801 | if (define == NULL) { |
| 7802 | if (token[0] == '#') |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7803 | return; |
| 7804 | fprintf(stderr, "callback on %s missing define\n", token); |
| 7805 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7806 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7807 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7808 | } |
| 7809 | if ((ctxt == NULL) || (define == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7810 | fprintf(stderr, "callback on %s missing info\n", token); |
| 7811 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7812 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7813 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7814 | } else if (define->type != XML_RELAXNG_ELEMENT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7815 | fprintf(stderr, "callback on %s define is not element\n", token); |
| 7816 | if (ctxt->errNo == XML_RELAXNG_OK) |
| 7817 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7818 | return; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7819 | } |
| 7820 | ret = xmlRelaxNGValidateDefinition(ctxt, define); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7821 | if (ret != 0) |
| 7822 | ctxt->perr = ret; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7823 | } |
| 7824 | |
| 7825 | /** |
| 7826 | * xmlRelaxNGValidateCompiledContent: |
| 7827 | * @ctxt: the RelaxNG validation context |
| 7828 | * @regexp: the regular expression as compiled |
| 7829 | * @content: list of children to test against the regexp |
| 7830 | * |
| 7831 | * Validate the content model of an element or start using the regexp |
| 7832 | * |
| 7833 | * Returns 0 in case of success, -1 in case of error. |
| 7834 | */ |
| 7835 | static int |
| 7836 | xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7837 | xmlRegexpPtr regexp, xmlNodePtr content) |
| 7838 | { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7839 | xmlRegExecCtxtPtr exec; |
| 7840 | xmlNodePtr cur; |
Daniel Veillard | 6216360 | 2003-04-17 09:36:38 +0000 | [diff] [blame] | 7841 | int ret = 0; |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7842 | int oldperr = ctxt->perr; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7843 | |
| 7844 | if ((ctxt == NULL) || (regexp == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7845 | return (-1); |
| 7846 | exec = xmlRegNewExecCtxt(regexp, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7847 | xmlRelaxNGValidateCompiledCallback, ctxt); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7848 | ctxt->perr = 0; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7849 | cur = content; |
| 7850 | while (cur != NULL) { |
| 7851 | ctxt->state->seq = cur; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7852 | switch (cur->type) { |
| 7853 | case XML_TEXT_NODE: |
| 7854 | case XML_CDATA_SECTION_NODE: |
| 7855 | if (xmlIsBlankNode(cur)) |
| 7856 | break; |
| 7857 | ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt); |
| 7858 | if (ret < 0) { |
| 7859 | VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, |
| 7860 | cur->parent->name); |
| 7861 | } |
| 7862 | break; |
| 7863 | case XML_ELEMENT_NODE: |
| 7864 | if (cur->ns != NULL) { |
| 7865 | ret = xmlRegExecPushString2(exec, cur->name, |
| 7866 | cur->ns->href, ctxt); |
| 7867 | } else { |
| 7868 | ret = xmlRegExecPushString(exec, cur->name, ctxt); |
| 7869 | } |
| 7870 | if (ret < 0) { |
| 7871 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name); |
| 7872 | } |
| 7873 | break; |
| 7874 | default: |
| 7875 | break; |
| 7876 | } |
| 7877 | if (ret < 0) |
| 7878 | break; |
| 7879 | /* |
| 7880 | * Switch to next element |
| 7881 | */ |
| 7882 | cur = cur->next; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7883 | } |
| 7884 | ret = xmlRegExecPushString(exec, NULL, NULL); |
| 7885 | if (ret == 1) { |
| 7886 | ret = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7887 | ctxt->state->seq = NULL; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7888 | } else if (ret == 0) { |
| 7889 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7890 | * TODO: get some of the names needed to exit the current state of exec |
| 7891 | */ |
| 7892 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST ""); |
| 7893 | ret = -1; |
| 7894 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 7895 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7896 | } else { |
| 7897 | ret = -1; |
| 7898 | } |
| 7899 | xmlRegFreeExecCtxt(exec); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7900 | /* |
| 7901 | * There might be content model errors outside of the pure |
| 7902 | * regexp validation, e.g. for attribute values. |
| 7903 | */ |
| 7904 | if ((ret == 0) && (ctxt->perr != 0)) { |
| 7905 | ret = ctxt->perr; |
| 7906 | } |
| 7907 | ctxt->perr = oldperr; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7908 | return (ret); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7909 | } |
| 7910 | |
| 7911 | /************************************************************************ |
| 7912 | * * |
| 7913 | * Progressive validation of when possible * |
| 7914 | * * |
| 7915 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7916 | static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
| 7917 | xmlRelaxNGDefinePtr defines); |
| 7918 | static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 7919 | int dolog); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 7920 | static void xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7921 | |
| 7922 | /** |
| 7923 | * xmlRelaxNGElemPush: |
| 7924 | * @ctxt: the validation context |
| 7925 | * @exec: the regexp runtime for the new content model |
| 7926 | * |
| 7927 | * Push a new regexp for the current node content model on the stack |
| 7928 | * |
| 7929 | * Returns 0 in case of success and -1 in case of error. |
| 7930 | */ |
| 7931 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7932 | xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec) |
| 7933 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7934 | if (ctxt->elemTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7935 | ctxt->elemMax = 10; |
| 7936 | ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax * |
| 7937 | sizeof |
| 7938 | (xmlRegExecCtxtPtr)); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7939 | if (ctxt->elemTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7940 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 7941 | return (-1); |
| 7942 | } |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7943 | } |
| 7944 | if (ctxt->elemNr >= ctxt->elemMax) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7945 | ctxt->elemMax *= 2; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7946 | ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7947 | ctxt->elemMax * |
| 7948 | sizeof |
| 7949 | (xmlRegExecCtxtPtr)); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7950 | if (ctxt->elemTab == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7951 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 7952 | return (-1); |
| 7953 | } |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7954 | } |
| 7955 | ctxt->elemTab[ctxt->elemNr++] = exec; |
| 7956 | ctxt->elem = exec; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7957 | return (0); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7958 | } |
| 7959 | |
| 7960 | /** |
| 7961 | * xmlRelaxNGElemPop: |
| 7962 | * @ctxt: the validation context |
| 7963 | * |
| 7964 | * Pop the regexp of the current node content model from the stack |
| 7965 | * |
| 7966 | * Returns the exec or NULL if empty |
| 7967 | */ |
| 7968 | static xmlRegExecCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7969 | xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt) |
| 7970 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7971 | xmlRegExecCtxtPtr ret; |
| 7972 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7973 | if (ctxt->elemNr <= 0) |
| 7974 | return (NULL); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7975 | ctxt->elemNr--; |
| 7976 | ret = ctxt->elemTab[ctxt->elemNr]; |
| 7977 | ctxt->elemTab[ctxt->elemNr] = NULL; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7978 | if (ctxt->elemNr > 0) |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7979 | ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1]; |
| 7980 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7981 | ctxt->elem = NULL; |
| 7982 | return (ret); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7983 | } |
| 7984 | |
| 7985 | /** |
| 7986 | * xmlRelaxNGValidateProgressiveCallback: |
| 7987 | * @exec: the regular expression instance |
| 7988 | * @token: the token which matched |
| 7989 | * @transdata: callback data, the define for the subelement if available |
| 7990 | @ @inputdata: callback data, the Relax NG validation context |
| 7991 | * |
| 7992 | * Handle the callback and if needed validate the element children. |
| 7993 | * some of the in/out informations are passed via the context in @inputdata. |
| 7994 | */ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 7995 | static void |
| 7996 | xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec |
| 7997 | ATTRIBUTE_UNUSED, |
| 7998 | const xmlChar * token, |
| 7999 | void *transdata, void *inputdata) |
| 8000 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8001 | xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata; |
| 8002 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8003 | xmlRelaxNGValidStatePtr state, oldstate; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8004 | xmlNodePtr node = ctxt->pnode; |
Daniel Veillard | c3ca5ba | 2003-05-09 22:26:28 +0000 | [diff] [blame] | 8005 | int ret = 0, oldflags; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8006 | |
| 8007 | #ifdef DEBUG_PROGRESSIVE |
| 8008 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8009 | "Progressive callback for: '%s'\n", token); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8010 | #endif |
| 8011 | if (ctxt == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8012 | fprintf(stderr, "callback on %s missing context\n", token); |
| 8013 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8014 | } |
| 8015 | ctxt->pstate = 1; |
| 8016 | if (define == NULL) { |
| 8017 | if (token[0] == '#') |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8018 | return; |
| 8019 | fprintf(stderr, "callback on %s missing define\n", token); |
| 8020 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 8021 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 8022 | ctxt->pstate = -1; |
| 8023 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8024 | } |
| 8025 | if ((ctxt == NULL) || (define == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8026 | fprintf(stderr, "callback on %s missing info\n", token); |
| 8027 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 8028 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 8029 | ctxt->pstate = -1; |
| 8030 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8031 | } else if (define->type != XML_RELAXNG_ELEMENT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8032 | fprintf(stderr, "callback on %s define is not element\n", token); |
| 8033 | if (ctxt->errNo == XML_RELAXNG_OK) |
| 8034 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 8035 | ctxt->pstate = -1; |
| 8036 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8037 | } |
| 8038 | if (node->type != XML_ELEMENT_NODE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8039 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
| 8040 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8041 | xmlRelaxNGDumpValidError(ctxt); |
| 8042 | ctxt->pstate = -1; |
| 8043 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8044 | } |
| 8045 | if (define->contModel == NULL) { |
| 8046 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8047 | * this node cannot be validated in a streamable fashion |
| 8048 | */ |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8049 | #ifdef DEBUG_PROGRESSIVE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8050 | xmlGenericError(xmlGenericErrorContext, |
| 8051 | "Element '%s' validation is not streamable\n", |
| 8052 | token); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8053 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8054 | ctxt->pstate = 0; |
| 8055 | ctxt->pdef = define; |
| 8056 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8057 | } |
| 8058 | exec = xmlRegNewExecCtxt(define->contModel, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8059 | xmlRelaxNGValidateProgressiveCallback, ctxt); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8060 | if (exec == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8061 | ctxt->pstate = -1; |
| 8062 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8063 | } |
| 8064 | xmlRelaxNGElemPush(ctxt, exec); |
| 8065 | |
| 8066 | /* |
| 8067 | * Validate the attributes part of the content. |
| 8068 | */ |
| 8069 | state = xmlRelaxNGNewValidState(ctxt, node); |
| 8070 | if (state == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8071 | ctxt->pstate = -1; |
| 8072 | return; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8073 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8074 | oldstate = ctxt->state; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8075 | ctxt->state = state; |
| 8076 | if (define->attrs != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8077 | ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
| 8078 | if (ret != 0) { |
| 8079 | ctxt->pstate = -1; |
| 8080 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
| 8081 | } |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8082 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8083 | if (ctxt->state != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8084 | ctxt->state->seq = NULL; |
| 8085 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
| 8086 | if (ret != 0) { |
| 8087 | ctxt->pstate = -1; |
| 8088 | } |
| 8089 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8090 | } else if (ctxt->states != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8091 | int tmp = -1, i; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8092 | |
| 8093 | oldflags = ctxt->flags; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8094 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8095 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 8096 | state = ctxt->states->tabState[i]; |
| 8097 | ctxt->state = state; |
| 8098 | ctxt->state->seq = NULL; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8099 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8100 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
| 8101 | tmp = 0; |
| 8102 | break; |
| 8103 | } |
| 8104 | } |
| 8105 | if (tmp != 0) { |
| 8106 | /* |
| 8107 | * validation error, log the message for the "best" one |
| 8108 | */ |
| 8109 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8110 | xmlRelaxNGLogBestError(ctxt); |
| 8111 | } |
| 8112 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 8113 | xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[i]); |
| 8114 | } |
| 8115 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 8116 | ctxt->states = NULL; |
| 8117 | if ((ret == 0) && (tmp == -1)) |
| 8118 | ctxt->pstate = -1; |
| 8119 | ctxt->flags = oldflags; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8120 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8121 | if (ctxt->pstate == -1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8122 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 8123 | xmlRelaxNGDumpValidError(ctxt); |
| 8124 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8125 | } |
| 8126 | ctxt->state = oldstate; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8127 | } |
| 8128 | |
| 8129 | /** |
| 8130 | * xmlRelaxNGValidatePushElement: |
| 8131 | * @ctxt: the validation context |
| 8132 | * @doc: a document instance |
| 8133 | * @elem: an element instance |
| 8134 | * |
| 8135 | * Push a new element start on the RelaxNG validation stack. |
| 8136 | * |
| 8137 | * returns 1 if no validation problem was found or 0 if validating the |
| 8138 | * element requires a full node, and -1 in case of error. |
| 8139 | */ |
| 8140 | int |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8141 | xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8142 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8143 | xmlNodePtr elem) |
| 8144 | { |
| 8145 | int ret = 1; |
| 8146 | |
| 8147 | if ((ctxt == NULL) || (elem == NULL)) |
| 8148 | return (-1); |
| 8149 | |
| 8150 | #ifdef DEBUG_PROGRESSIVE |
| 8151 | xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name); |
| 8152 | #endif |
| 8153 | if (ctxt->elem == 0) { |
| 8154 | xmlRelaxNGPtr schema; |
| 8155 | xmlRelaxNGGrammarPtr grammar; |
| 8156 | xmlRegExecCtxtPtr exec; |
| 8157 | xmlRelaxNGDefinePtr define; |
| 8158 | |
| 8159 | schema = ctxt->schema; |
| 8160 | if (schema == NULL) { |
| 8161 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 8162 | return (-1); |
| 8163 | } |
| 8164 | grammar = schema->topgrammar; |
| 8165 | if ((grammar == NULL) || (grammar->start == NULL)) { |
| 8166 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 8167 | return (-1); |
| 8168 | } |
| 8169 | define = grammar->start; |
| 8170 | if (define->contModel == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8171 | ctxt->pdef = define; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8172 | return (0); |
| 8173 | } |
| 8174 | exec = xmlRegNewExecCtxt(define->contModel, |
| 8175 | xmlRelaxNGValidateProgressiveCallback, |
| 8176 | ctxt); |
| 8177 | if (exec == NULL) { |
| 8178 | return (-1); |
| 8179 | } |
| 8180 | xmlRelaxNGElemPush(ctxt, exec); |
| 8181 | } |
| 8182 | ctxt->pnode = elem; |
| 8183 | ctxt->pstate = 0; |
| 8184 | if (elem->ns != NULL) { |
| 8185 | ret = |
| 8186 | xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href, |
| 8187 | ctxt); |
| 8188 | } else { |
| 8189 | ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt); |
| 8190 | } |
| 8191 | if (ret < 0) { |
| 8192 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name); |
| 8193 | } else { |
| 8194 | if (ctxt->pstate == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8195 | ret = 0; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8196 | else if (ctxt->pstate < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8197 | ret = -1; |
| 8198 | else |
| 8199 | ret = 1; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8200 | } |
| 8201 | #ifdef DEBUG_PROGRESSIVE |
| 8202 | if (ret < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8203 | xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n", |
| 8204 | elem->name); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8205 | #endif |
| 8206 | return (ret); |
| 8207 | } |
| 8208 | |
| 8209 | /** |
| 8210 | * xmlRelaxNGValidatePushCData: |
| 8211 | * @ctxt: the RelaxNG validation context |
| 8212 | * @data: some character data read |
| 8213 | * @len: the lenght of the data |
| 8214 | * |
| 8215 | * check the CData parsed for validation in the current stack |
| 8216 | * |
| 8217 | * returns 1 if no validation problem was found or -1 otherwise |
| 8218 | */ |
| 8219 | int |
| 8220 | xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8221 | const xmlChar * data, int len ATTRIBUTE_UNUSED) |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8222 | { |
| 8223 | int ret = 1; |
| 8224 | |
| 8225 | if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL)) |
| 8226 | return (-1); |
| 8227 | |
| 8228 | #ifdef DEBUG_PROGRESSIVE |
| 8229 | xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len); |
| 8230 | #endif |
| 8231 | |
| 8232 | while (*data != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8233 | if (!IS_BLANK_CH(*data)) |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8234 | break; |
| 8235 | data++; |
| 8236 | } |
| 8237 | if (*data == 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8238 | return (1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8239 | |
| 8240 | ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt); |
| 8241 | if (ret < 0) { |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8242 | VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO "); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8243 | #ifdef DEBUG_PROGRESSIVE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8244 | xmlGenericError(xmlGenericErrorContext, "CDATA failed\n"); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8245 | #endif |
| 8246 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8247 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8248 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8249 | return (1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8250 | } |
| 8251 | |
| 8252 | /** |
| 8253 | * xmlRelaxNGValidatePopElement: |
| 8254 | * @ctxt: the RelaxNG validation context |
| 8255 | * @doc: a document instance |
| 8256 | * @elem: an element instance |
| 8257 | * |
| 8258 | * Pop the element end from the RelaxNG validation stack. |
| 8259 | * |
| 8260 | * returns 1 if no validation problem was found or 0 otherwise |
| 8261 | */ |
| 8262 | int |
| 8263 | xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8264 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8265 | xmlNodePtr elem) |
| 8266 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8267 | int ret; |
| 8268 | xmlRegExecCtxtPtr exec; |
| 8269 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8270 | if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL)) |
| 8271 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8272 | #ifdef DEBUG_PROGRESSIVE |
| 8273 | xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name); |
| 8274 | #endif |
| 8275 | /* |
| 8276 | * verify that we reached a terminal state of the content model. |
| 8277 | */ |
| 8278 | exec = xmlRelaxNGElemPop(ctxt); |
| 8279 | ret = xmlRegExecPushString(exec, NULL, NULL); |
| 8280 | if (ret == 0) { |
| 8281 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8282 | * TODO: get some of the names needed to exit the current state of exec |
| 8283 | */ |
| 8284 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST ""); |
| 8285 | ret = -1; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8286 | } else if (ret < 0) { |
| 8287 | ret = -1; |
| 8288 | } else { |
| 8289 | ret = 1; |
| 8290 | } |
| 8291 | xmlRegFreeExecCtxt(exec); |
| 8292 | #ifdef DEBUG_PROGRESSIVE |
| 8293 | if (ret < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8294 | xmlGenericError(xmlGenericErrorContext, "PopElem %s failed\n", |
| 8295 | elem->name); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8296 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8297 | return (ret); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8298 | } |
| 8299 | |
| 8300 | /** |
| 8301 | * xmlRelaxNGValidateFullElement: |
| 8302 | * @ctxt: the validation context |
| 8303 | * @doc: a document instance |
| 8304 | * @elem: an element instance |
| 8305 | * |
| 8306 | * Validate a full subtree when xmlRelaxNGValidatePushElement() returned |
| 8307 | * 0 and the content of the node has been expanded. |
| 8308 | * |
| 8309 | * returns 1 if no validation problem was found or -1 in case of error. |
| 8310 | */ |
| 8311 | int |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8312 | xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8313 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8314 | xmlNodePtr elem) |
| 8315 | { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8316 | int ret; |
| 8317 | xmlRelaxNGValidStatePtr state; |
| 8318 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8319 | if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL)) |
| 8320 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8321 | #ifdef DEBUG_PROGRESSIVE |
| 8322 | xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name); |
| 8323 | #endif |
| 8324 | state = xmlRelaxNGNewValidState(ctxt, elem->parent); |
| 8325 | if (state == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8326 | return (-1); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8327 | } |
| 8328 | state->seq = elem; |
| 8329 | ctxt->state = state; |
| 8330 | ctxt->errNo = XML_RELAXNG_OK; |
| 8331 | ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8332 | if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK)) |
| 8333 | ret = -1; |
| 8334 | else |
| 8335 | ret = 1; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8336 | xmlRelaxNGFreeValidState(ctxt, state); |
| 8337 | ctxt->state = NULL; |
| 8338 | #ifdef DEBUG_PROGRESSIVE |
| 8339 | if (ret < 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8340 | xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n", |
| 8341 | elem->name); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8342 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8343 | return (ret); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8344 | } |
| 8345 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 8346 | /************************************************************************ |
| 8347 | * * |
| 8348 | * Generic interpreted validation implementation * |
| 8349 | * * |
| 8350 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8351 | static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
| 8352 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8353 | |
| 8354 | /** |
| 8355 | * xmlRelaxNGSkipIgnored: |
| 8356 | * @ctxt: a schema validation context |
| 8357 | * @node: the top node. |
| 8358 | * |
| 8359 | * Skip ignorable nodes in that context |
| 8360 | * |
| 8361 | * Returns the new sibling or NULL in case of error. |
| 8362 | */ |
| 8363 | static xmlNodePtr |
| 8364 | xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8365 | xmlNodePtr node) |
| 8366 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8367 | /* |
| 8368 | * TODO complete and handle entities |
| 8369 | */ |
| 8370 | while ((node != NULL) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8371 | ((node->type == XML_COMMENT_NODE) || |
| 8372 | (node->type == XML_PI_NODE) || |
William M. Brack | 7217c86 | 2004-03-15 02:43:56 +0000 | [diff] [blame] | 8373 | (node->type == XML_XINCLUDE_START) || |
| 8374 | (node->type == XML_XINCLUDE_END) || |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8375 | (((node->type == XML_TEXT_NODE) || |
| 8376 | (node->type == XML_CDATA_SECTION_NODE)) && |
| 8377 | ((ctxt->flags & FLAGS_MIXED_CONTENT) || |
| 8378 | (IS_BLANK_NODE(node)))))) { |
| 8379 | node = node->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8380 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8381 | return (node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8382 | } |
| 8383 | |
| 8384 | /** |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8385 | * xmlRelaxNGNormalize: |
| 8386 | * @ctxt: a schema validation context |
| 8387 | * @str: the string to normalize |
| 8388 | * |
| 8389 | * Implements the normalizeWhiteSpace( s ) function from |
| 8390 | * section 6.2.9 of the spec |
| 8391 | * |
| 8392 | * Returns the new string or NULL in case of error. |
| 8393 | */ |
| 8394 | static xmlChar * |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8395 | xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar * str) |
| 8396 | { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8397 | xmlChar *ret, *p; |
| 8398 | const xmlChar *tmp; |
| 8399 | int len; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8400 | |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8401 | if (str == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8402 | return (NULL); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8403 | tmp = str; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8404 | while (*tmp != 0) |
| 8405 | tmp++; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8406 | len = tmp - str; |
| 8407 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 8408 | ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar)); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8409 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8410 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 8411 | return (NULL); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8412 | } |
| 8413 | p = ret; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8414 | while (IS_BLANK_CH(*str)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8415 | str++; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8416 | while (*str != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8417 | if (IS_BLANK_CH(*str)) { |
| 8418 | while (IS_BLANK_CH(*str)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8419 | str++; |
| 8420 | if (*str == 0) |
| 8421 | break; |
| 8422 | *p++ = ' '; |
| 8423 | } else |
| 8424 | *p++ = *str++; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8425 | } |
| 8426 | *p = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8427 | return (ret); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8428 | } |
| 8429 | |
| 8430 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8431 | * xmlRelaxNGValidateDatatype: |
| 8432 | * @ctxt: a Relax-NG validation context |
| 8433 | * @value: the string value |
| 8434 | * @type: the datatype definition |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8435 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8436 | * |
| 8437 | * Validate the given value against the dataype |
| 8438 | * |
| 8439 | * Returns 0 if the validation succeeded or an error code. |
| 8440 | */ |
| 8441 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8442 | xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt, |
| 8443 | const xmlChar * value, |
| 8444 | xmlRelaxNGDefinePtr define, xmlNodePtr node) |
| 8445 | { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8446 | int ret, tmp; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8447 | xmlRelaxNGTypeLibraryPtr lib; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8448 | void *result = NULL; |
| 8449 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8450 | |
| 8451 | if ((define == NULL) || (define->data == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8452 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8453 | } |
| 8454 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8455 | if (lib->check != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8456 | if ((define->attrs != NULL) && |
| 8457 | (define->attrs->type == XML_RELAXNG_PARAM)) { |
| 8458 | ret = |
| 8459 | lib->check(lib->data, define->name, value, &result, node); |
| 8460 | } else { |
| 8461 | ret = lib->check(lib->data, define->name, value, NULL, node); |
| 8462 | } |
| 8463 | } else |
| 8464 | ret = -1; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8465 | if (ret < 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8466 | VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name); |
| 8467 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
| 8468 | lib->freef(lib->data, result); |
| 8469 | return (-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8470 | } else if (ret == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8471 | ret = 0; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8472 | } else if (ret == 2) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8473 | VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8474 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8475 | VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value); |
| 8476 | ret = -1; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8477 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8478 | cur = define->attrs; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8479 | while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8480 | if (lib->facet != NULL) { |
| 8481 | tmp = lib->facet(lib->data, define->name, cur->name, |
| 8482 | cur->value, value, result); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8483 | if (tmp != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8484 | ret = -1; |
| 8485 | } |
| 8486 | cur = cur->next; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8487 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8488 | if ((ret == 0) && (define->content != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8489 | const xmlChar *oldvalue, *oldendvalue; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8490 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8491 | oldvalue = ctxt->state->value; |
| 8492 | oldendvalue = ctxt->state->endvalue; |
| 8493 | ctxt->state->value = (xmlChar *) value; |
| 8494 | ctxt->state->endvalue = NULL; |
| 8495 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 8496 | ctxt->state->value = (xmlChar *) oldvalue; |
| 8497 | ctxt->state->endvalue = (xmlChar *) oldendvalue; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8498 | } |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8499 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8500 | lib->freef(lib->data, result); |
| 8501 | return (ret); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8502 | } |
| 8503 | |
| 8504 | /** |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8505 | * xmlRelaxNGNextValue: |
| 8506 | * @ctxt: a Relax-NG validation context |
| 8507 | * |
| 8508 | * Skip to the next value when validating within a list |
| 8509 | * |
| 8510 | * Returns 0 if the operation succeeded or an error code. |
| 8511 | */ |
| 8512 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8513 | xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt) |
| 8514 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8515 | xmlChar *cur; |
| 8516 | |
| 8517 | cur = ctxt->state->value; |
| 8518 | if ((cur == NULL) || (ctxt->state->endvalue == NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8519 | ctxt->state->value = NULL; |
| 8520 | ctxt->state->endvalue = NULL; |
| 8521 | return (0); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8522 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8523 | while (*cur != 0) |
| 8524 | cur++; |
| 8525 | while ((cur != ctxt->state->endvalue) && (*cur == 0)) |
| 8526 | cur++; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8527 | if (cur == ctxt->state->endvalue) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8528 | ctxt->state->value = NULL; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8529 | else |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8530 | ctxt->state->value = cur; |
| 8531 | return (0); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8532 | } |
| 8533 | |
| 8534 | /** |
| 8535 | * xmlRelaxNGValidateValueList: |
| 8536 | * @ctxt: a Relax-NG validation context |
| 8537 | * @defines: the list of definitions to verify |
| 8538 | * |
| 8539 | * Validate the given set of definitions for the current value |
| 8540 | * |
| 8541 | * Returns 0 if the validation succeeded or an error code. |
| 8542 | */ |
| 8543 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8544 | xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt, |
| 8545 | xmlRelaxNGDefinePtr defines) |
| 8546 | { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8547 | int ret = 0; |
| 8548 | |
| 8549 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8550 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
| 8551 | if (ret != 0) |
| 8552 | break; |
| 8553 | defines = defines->next; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8554 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8555 | return (ret); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8556 | } |
| 8557 | |
| 8558 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8559 | * xmlRelaxNGValidateValue: |
| 8560 | * @ctxt: a Relax-NG validation context |
| 8561 | * @define: the definition to verify |
| 8562 | * |
| 8563 | * Validate the given definition for the current value |
| 8564 | * |
| 8565 | * Returns 0 if the validation succeeded or an error code. |
| 8566 | */ |
| 8567 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8568 | xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
| 8569 | xmlRelaxNGDefinePtr define) |
| 8570 | { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8571 | int ret = 0, oldflags; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8572 | xmlChar *value; |
| 8573 | |
| 8574 | value = ctxt->state->value; |
| 8575 | switch (define->type) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8576 | case XML_RELAXNG_EMPTY:{ |
| 8577 | if ((value != NULL) && (value[0] != 0)) { |
| 8578 | int idx = 0; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 8579 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8580 | while (IS_BLANK_CH(value[idx])) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8581 | idx++; |
| 8582 | if (value[idx] != 0) |
| 8583 | ret = -1; |
| 8584 | } |
| 8585 | break; |
| 8586 | } |
| 8587 | case XML_RELAXNG_TEXT: |
| 8588 | break; |
| 8589 | case XML_RELAXNG_VALUE:{ |
| 8590 | if (!xmlStrEqual(value, define->value)) { |
| 8591 | if (define->name != NULL) { |
| 8592 | xmlRelaxNGTypeLibraryPtr lib; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8593 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8594 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
| 8595 | if ((lib != NULL) && (lib->comp != NULL)) { |
| 8596 | ret = lib->comp(lib->data, define->name, |
| 8597 | define->value, define->node, |
| 8598 | (void *) define->attrs, |
| 8599 | value, ctxt->state->node); |
| 8600 | } else |
| 8601 | ret = -1; |
| 8602 | if (ret < 0) { |
| 8603 | VALID_ERR2(XML_RELAXNG_ERR_TYPECMP, |
| 8604 | define->name); |
| 8605 | return (-1); |
| 8606 | } else if (ret == 1) { |
| 8607 | ret = 0; |
| 8608 | } else { |
| 8609 | ret = -1; |
| 8610 | } |
| 8611 | } else { |
| 8612 | xmlChar *nval, *nvalue; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8613 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8614 | /* |
| 8615 | * TODO: trivial optimizations are possible by |
| 8616 | * computing at compile-time |
| 8617 | */ |
| 8618 | nval = xmlRelaxNGNormalize(ctxt, define->value); |
| 8619 | nvalue = xmlRelaxNGNormalize(ctxt, value); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8620 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8621 | if ((nval == NULL) || (nvalue == NULL) || |
| 8622 | (!xmlStrEqual(nval, nvalue))) |
| 8623 | ret = -1; |
| 8624 | if (nval != NULL) |
| 8625 | xmlFree(nval); |
| 8626 | if (nvalue != NULL) |
| 8627 | xmlFree(nvalue); |
| 8628 | } |
| 8629 | } |
| 8630 | if (ret == 0) |
| 8631 | xmlRelaxNGNextValue(ctxt); |
| 8632 | break; |
| 8633 | } |
| 8634 | case XML_RELAXNG_DATATYPE:{ |
| 8635 | ret = xmlRelaxNGValidateDatatype(ctxt, value, define, |
| 8636 | ctxt->state->seq); |
| 8637 | if (ret == 0) |
| 8638 | xmlRelaxNGNextValue(ctxt); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8639 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8640 | break; |
| 8641 | } |
| 8642 | case XML_RELAXNG_CHOICE:{ |
| 8643 | xmlRelaxNGDefinePtr list = define->content; |
| 8644 | xmlChar *oldvalue; |
| 8645 | |
| 8646 | oldflags = ctxt->flags; |
| 8647 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8648 | |
| 8649 | oldvalue = ctxt->state->value; |
| 8650 | while (list != NULL) { |
| 8651 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8652 | if (ret == 0) { |
| 8653 | break; |
| 8654 | } |
| 8655 | ctxt->state->value = oldvalue; |
| 8656 | list = list->next; |
| 8657 | } |
| 8658 | ctxt->flags = oldflags; |
| 8659 | if (ret != 0) { |
| 8660 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8661 | xmlRelaxNGDumpValidError(ctxt); |
| 8662 | } else { |
| 8663 | if (ctxt->errNr > 0) |
| 8664 | xmlRelaxNGPopErrors(ctxt, 0); |
| 8665 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8666 | break; |
| 8667 | } |
| 8668 | case XML_RELAXNG_LIST:{ |
| 8669 | xmlRelaxNGDefinePtr list = define->content; |
| 8670 | xmlChar *oldvalue, *oldend, *val, *cur; |
| 8671 | |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8672 | #ifdef DEBUG_LIST |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8673 | int nb_values = 0; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8674 | #endif |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8675 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8676 | oldvalue = ctxt->state->value; |
| 8677 | oldend = ctxt->state->endvalue; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8678 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8679 | val = xmlStrdup(oldvalue); |
| 8680 | if (val == NULL) { |
| 8681 | val = xmlStrdup(BAD_CAST ""); |
| 8682 | } |
| 8683 | if (val == NULL) { |
| 8684 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 8685 | return (-1); |
| 8686 | } |
| 8687 | cur = val; |
| 8688 | while (*cur != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8689 | if (IS_BLANK_CH(*cur)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8690 | *cur = 0; |
| 8691 | cur++; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8692 | #ifdef DEBUG_LIST |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8693 | nb_values++; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8694 | #endif |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8695 | while (IS_BLANK_CH(*cur)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8696 | *cur++ = 0; |
| 8697 | } else |
| 8698 | cur++; |
| 8699 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8700 | #ifdef DEBUG_LIST |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8701 | xmlGenericError(xmlGenericErrorContext, |
| 8702 | "list value: '%s' found %d items\n", |
| 8703 | oldvalue, nb_values); |
| 8704 | nb_values = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8705 | #endif |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8706 | ctxt->state->endvalue = cur; |
| 8707 | cur = val; |
| 8708 | while ((*cur == 0) && (cur != ctxt->state->endvalue)) |
| 8709 | cur++; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8710 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8711 | ctxt->state->value = cur; |
| 8712 | |
| 8713 | while (list != NULL) { |
| 8714 | if (ctxt->state->value == ctxt->state->endvalue) |
| 8715 | ctxt->state->value = NULL; |
| 8716 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8717 | if (ret != 0) { |
| 8718 | #ifdef DEBUG_LIST |
| 8719 | xmlGenericError(xmlGenericErrorContext, |
| 8720 | "Failed to validate value: '%s' with %d rule\n", |
| 8721 | ctxt->state->value, nb_values); |
| 8722 | #endif |
| 8723 | break; |
| 8724 | } |
| 8725 | #ifdef DEBUG_LIST |
| 8726 | nb_values++; |
| 8727 | #endif |
| 8728 | list = list->next; |
| 8729 | } |
| 8730 | |
| 8731 | if ((ret == 0) && (ctxt->state->value != NULL) && |
| 8732 | (ctxt->state->value != ctxt->state->endvalue)) { |
| 8733 | VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA, |
| 8734 | ctxt->state->value); |
| 8735 | ret = -1; |
| 8736 | } |
| 8737 | xmlFree(val); |
| 8738 | ctxt->state->value = oldvalue; |
| 8739 | ctxt->state->endvalue = oldend; |
| 8740 | break; |
| 8741 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8742 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8743 | ret = xmlRelaxNGValidateValueList(ctxt, define->content); |
| 8744 | if (ret != 0) { |
| 8745 | break; |
| 8746 | } |
| 8747 | /* no break on purpose */ |
| 8748 | case XML_RELAXNG_ZEROORMORE:{ |
| 8749 | xmlChar *cur, *temp; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8750 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8751 | oldflags = ctxt->flags; |
| 8752 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8753 | cur = ctxt->state->value; |
| 8754 | temp = NULL; |
| 8755 | while ((cur != NULL) && (cur != ctxt->state->endvalue) && |
| 8756 | (temp != cur)) { |
| 8757 | temp = cur; |
| 8758 | ret = |
| 8759 | xmlRelaxNGValidateValueList(ctxt, define->content); |
| 8760 | if (ret != 0) { |
| 8761 | ctxt->state->value = temp; |
| 8762 | ret = 0; |
| 8763 | break; |
| 8764 | } |
| 8765 | cur = ctxt->state->value; |
| 8766 | } |
| 8767 | ctxt->flags = oldflags; |
| 8768 | if (ret != 0) { |
| 8769 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8770 | xmlRelaxNGDumpValidError(ctxt); |
| 8771 | } else { |
| 8772 | if (ctxt->errNr > 0) |
| 8773 | xmlRelaxNGPopErrors(ctxt, 0); |
| 8774 | } |
| 8775 | break; |
| 8776 | } |
| 8777 | case XML_RELAXNG_EXCEPT:{ |
| 8778 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8779 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8780 | list = define->content; |
| 8781 | while (list != NULL) { |
| 8782 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8783 | if (ret == 0) { |
| 8784 | ret = -1; |
| 8785 | break; |
| 8786 | } else |
| 8787 | ret = 0; |
| 8788 | list = list->next; |
| 8789 | } |
| 8790 | break; |
| 8791 | } |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 8792 | case XML_RELAXNG_DEF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8793 | case XML_RELAXNG_GROUP:{ |
| 8794 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8795 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8796 | list = define->content; |
| 8797 | while (list != NULL) { |
| 8798 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8799 | if (ret != 0) { |
| 8800 | ret = -1; |
| 8801 | break; |
| 8802 | } else |
| 8803 | ret = 0; |
| 8804 | list = list->next; |
| 8805 | } |
| 8806 | break; |
| 8807 | } |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 8808 | case XML_RELAXNG_REF: |
| 8809 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8810 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 8811 | break; |
| 8812 | default: |
| 8813 | TODO ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8814 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8815 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8816 | } |
| 8817 | |
| 8818 | /** |
| 8819 | * xmlRelaxNGValidateValueContent: |
| 8820 | * @ctxt: a Relax-NG validation context |
| 8821 | * @defines: the list of definitions to verify |
| 8822 | * |
| 8823 | * Validate the given definitions for the current value |
| 8824 | * |
| 8825 | * Returns 0 if the validation succeeded or an error code. |
| 8826 | */ |
| 8827 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8828 | xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt, |
| 8829 | xmlRelaxNGDefinePtr defines) |
| 8830 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8831 | int ret = 0; |
| 8832 | |
| 8833 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8834 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
| 8835 | if (ret != 0) |
| 8836 | break; |
| 8837 | defines = defines->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8838 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8839 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8840 | } |
| 8841 | |
| 8842 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8843 | * xmlRelaxNGAttributeMatch: |
| 8844 | * @ctxt: a Relax-NG validation context |
| 8845 | * @define: the definition to check |
| 8846 | * @prop: the attribute |
| 8847 | * |
| 8848 | * Check if the attribute matches the definition nameClass |
| 8849 | * |
| 8850 | * Returns 1 if the attribute matches, 0 if no, or -1 in case of error |
| 8851 | */ |
| 8852 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8853 | xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 8854 | xmlRelaxNGDefinePtr define, xmlAttrPtr prop) |
| 8855 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8856 | int ret; |
| 8857 | |
| 8858 | if (define->name != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8859 | if (!xmlStrEqual(define->name, prop->name)) |
| 8860 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8861 | } |
| 8862 | if (define->ns != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8863 | if (define->ns[0] == 0) { |
| 8864 | if (prop->ns != NULL) |
| 8865 | return (0); |
| 8866 | } else { |
| 8867 | if ((prop->ns == NULL) || |
| 8868 | (!xmlStrEqual(define->ns, prop->ns->href))) |
| 8869 | return (0); |
| 8870 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8871 | } |
| 8872 | if (define->nameClass == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8873 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8874 | define = define->nameClass; |
| 8875 | if (define->type == XML_RELAXNG_EXCEPT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8876 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8877 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8878 | list = define->content; |
| 8879 | while (list != NULL) { |
| 8880 | ret = xmlRelaxNGAttributeMatch(ctxt, list, prop); |
| 8881 | if (ret == 1) |
| 8882 | return (0); |
| 8883 | if (ret < 0) |
| 8884 | return (ret); |
| 8885 | list = list->next; |
| 8886 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8887 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8888 | TODO} |
| 8889 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8890 | } |
| 8891 | |
| 8892 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8893 | * xmlRelaxNGValidateAttribute: |
| 8894 | * @ctxt: a Relax-NG validation context |
| 8895 | * @define: the definition to verify |
| 8896 | * |
| 8897 | * Validate the given attribute definition for that node |
| 8898 | * |
| 8899 | * Returns 0 if the validation succeeded or an error code. |
| 8900 | */ |
| 8901 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8902 | xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt, |
| 8903 | xmlRelaxNGDefinePtr define) |
| 8904 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8905 | int ret = 0, i; |
| 8906 | xmlChar *value, *oldvalue; |
| 8907 | xmlAttrPtr prop = NULL, tmp; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8908 | xmlNodePtr oldseq; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8909 | |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 8910 | if (ctxt->state->nbAttrLeft <= 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8911 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8912 | if (define->name != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8913 | for (i = 0; i < ctxt->state->nbAttrs; i++) { |
| 8914 | tmp = ctxt->state->attrs[i]; |
| 8915 | if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) { |
| 8916 | if ((((define->ns == NULL) || (define->ns[0] == 0)) && |
| 8917 | (tmp->ns == NULL)) || |
| 8918 | ((tmp->ns != NULL) && |
| 8919 | (xmlStrEqual(define->ns, tmp->ns->href)))) { |
| 8920 | prop = tmp; |
| 8921 | break; |
| 8922 | } |
| 8923 | } |
| 8924 | } |
| 8925 | if (prop != NULL) { |
| 8926 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 8927 | oldvalue = ctxt->state->value; |
| 8928 | oldseq = ctxt->state->seq; |
| 8929 | ctxt->state->seq = (xmlNodePtr) prop; |
| 8930 | ctxt->state->value = value; |
| 8931 | ctxt->state->endvalue = NULL; |
| 8932 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
| 8933 | if (ctxt->state->value != NULL) |
| 8934 | value = ctxt->state->value; |
| 8935 | if (value != NULL) |
| 8936 | xmlFree(value); |
| 8937 | ctxt->state->value = oldvalue; |
| 8938 | ctxt->state->seq = oldseq; |
| 8939 | if (ret == 0) { |
| 8940 | /* |
| 8941 | * flag the attribute as processed |
| 8942 | */ |
| 8943 | ctxt->state->attrs[i] = NULL; |
| 8944 | ctxt->state->nbAttrLeft--; |
| 8945 | } |
| 8946 | } else { |
| 8947 | ret = -1; |
| 8948 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8949 | #ifdef DEBUG |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8950 | xmlGenericError(xmlGenericErrorContext, |
| 8951 | "xmlRelaxNGValidateAttribute(%s): %d\n", |
| 8952 | define->name, ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8953 | #endif |
| 8954 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8955 | for (i = 0; i < ctxt->state->nbAttrs; i++) { |
| 8956 | tmp = ctxt->state->attrs[i]; |
| 8957 | if ((tmp != NULL) && |
| 8958 | (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) { |
| 8959 | prop = tmp; |
| 8960 | break; |
| 8961 | } |
| 8962 | } |
| 8963 | if (prop != NULL) { |
| 8964 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 8965 | oldvalue = ctxt->state->value; |
| 8966 | oldseq = ctxt->state->seq; |
| 8967 | ctxt->state->seq = (xmlNodePtr) prop; |
| 8968 | ctxt->state->value = value; |
| 8969 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
| 8970 | if (ctxt->state->value != NULL) |
| 8971 | value = ctxt->state->value; |
| 8972 | if (value != NULL) |
| 8973 | xmlFree(value); |
| 8974 | ctxt->state->value = oldvalue; |
| 8975 | ctxt->state->seq = oldseq; |
| 8976 | if (ret == 0) { |
| 8977 | /* |
| 8978 | * flag the attribute as processed |
| 8979 | */ |
| 8980 | ctxt->state->attrs[i] = NULL; |
| 8981 | ctxt->state->nbAttrLeft--; |
| 8982 | } |
| 8983 | } else { |
| 8984 | ret = -1; |
| 8985 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8986 | #ifdef DEBUG |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8987 | if (define->ns != NULL) { |
| 8988 | xmlGenericError(xmlGenericErrorContext, |
| 8989 | "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n", |
| 8990 | define->ns, ret); |
| 8991 | } else { |
| 8992 | xmlGenericError(xmlGenericErrorContext, |
| 8993 | "xmlRelaxNGValidateAttribute(anyName): %d\n", |
| 8994 | ret); |
| 8995 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8996 | #endif |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8997 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 8998 | |
| 8999 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9000 | } |
| 9001 | |
| 9002 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9003 | * xmlRelaxNGValidateAttributeList: |
| 9004 | * @ctxt: a Relax-NG validation context |
| 9005 | * @define: the list of definition to verify |
| 9006 | * |
| 9007 | * Validate the given node against the list of attribute definitions |
| 9008 | * |
| 9009 | * Returns 0 if the validation succeeded or an error code. |
| 9010 | */ |
| 9011 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9012 | xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
| 9013 | xmlRelaxNGDefinePtr defines) |
| 9014 | { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9015 | int ret = 0, res; |
| 9016 | int needmore = 0; |
| 9017 | xmlRelaxNGDefinePtr cur; |
| 9018 | |
| 9019 | cur = defines; |
| 9020 | while (cur != NULL) { |
| 9021 | if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9022 | if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0) |
| 9023 | ret = -1; |
| 9024 | } else |
| 9025 | needmore = 1; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9026 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9027 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9028 | if (!needmore) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9029 | return (ret); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9030 | cur = defines; |
| 9031 | while (cur != NULL) { |
| 9032 | if (cur->type != XML_RELAXNG_ATTRIBUTE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9033 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
| 9034 | res = xmlRelaxNGValidateDefinition(ctxt, cur); |
| 9035 | if (res < 0) |
| 9036 | ret = -1; |
| 9037 | } else { |
| 9038 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 9039 | return (-1); |
| 9040 | } |
| 9041 | if (res == -1) /* continues on -2 */ |
| 9042 | break; |
| 9043 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9044 | cur = cur->next; |
| 9045 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9046 | |
| 9047 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9048 | } |
| 9049 | |
| 9050 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 9051 | * xmlRelaxNGNodeMatchesList: |
| 9052 | * @node: the node |
| 9053 | * @list: a NULL terminated array of definitions |
| 9054 | * |
| 9055 | * Check if a node can be matched by one of the definitions |
| 9056 | * |
| 9057 | * Returns 1 if matches 0 otherwise |
| 9058 | */ |
| 9059 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9060 | xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr * list) |
| 9061 | { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 9062 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9063 | int i = 0, tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 9064 | |
| 9065 | if ((node == NULL) || (list == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9066 | return (0); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 9067 | |
| 9068 | cur = list[i++]; |
| 9069 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9070 | if ((node->type == XML_ELEMENT_NODE) && |
| 9071 | (cur->type == XML_RELAXNG_ELEMENT)) { |
| 9072 | tmp = xmlRelaxNGElementMatch(NULL, cur, node); |
| 9073 | if (tmp == 1) |
| 9074 | return (1); |
| 9075 | } else if (((node->type == XML_TEXT_NODE) || |
| 9076 | (node->type == XML_CDATA_SECTION_NODE)) && |
| 9077 | (cur->type == XML_RELAXNG_TEXT)) { |
| 9078 | return (1); |
| 9079 | } |
| 9080 | cur = list[i++]; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 9081 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9082 | return (0); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 9083 | } |
| 9084 | |
| 9085 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9086 | * xmlRelaxNGValidateInterleave: |
| 9087 | * @ctxt: a Relax-NG validation context |
| 9088 | * @define: the definition to verify |
| 9089 | * |
| 9090 | * Validate an interleave definition for a node. |
| 9091 | * |
| 9092 | * Returns 0 if the validation succeeded or an error code. |
| 9093 | */ |
| 9094 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9095 | xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt, |
| 9096 | xmlRelaxNGDefinePtr define) |
| 9097 | { |
William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 9098 | int ret = 0, i, nbgroups; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9099 | int errNr = ctxt->errNr; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9100 | int oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9101 | |
| 9102 | xmlRelaxNGValidStatePtr oldstate; |
| 9103 | xmlRelaxNGPartitionPtr partitions; |
| 9104 | xmlRelaxNGInterleaveGroupPtr group = NULL; |
| 9105 | xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem; |
| 9106 | xmlNodePtr *list = NULL, *lasts = NULL; |
| 9107 | |
| 9108 | if (define->data != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9109 | partitions = (xmlRelaxNGPartitionPtr) define->data; |
| 9110 | nbgroups = partitions->nbgroups; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9111 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9112 | VALID_ERR(XML_RELAXNG_ERR_INTERNODATA); |
| 9113 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9114 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9115 | /* |
| 9116 | * Optimizations for MIXED |
| 9117 | */ |
| 9118 | oldflags = ctxt->flags; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 9119 | if (define->dflags & IS_MIXED) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9120 | ctxt->flags |= FLAGS_MIXED_CONTENT; |
| 9121 | if (nbgroups == 2) { |
| 9122 | /* |
| 9123 | * this is a pure <mixed> case |
| 9124 | */ |
| 9125 | if (ctxt->state != NULL) |
| 9126 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
| 9127 | ctxt->state->seq); |
| 9128 | if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT) |
| 9129 | ret = xmlRelaxNGValidateDefinition(ctxt, |
| 9130 | partitions->groups[1]-> |
| 9131 | rule); |
| 9132 | else |
| 9133 | ret = xmlRelaxNGValidateDefinition(ctxt, |
| 9134 | partitions->groups[0]-> |
| 9135 | rule); |
| 9136 | if (ret == 0) { |
| 9137 | if (ctxt->state != NULL) |
| 9138 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
| 9139 | ctxt->state-> |
| 9140 | seq); |
| 9141 | } |
| 9142 | ctxt->flags = oldflags; |
| 9143 | return (ret); |
| 9144 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9145 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9146 | |
| 9147 | /* |
| 9148 | * Build arrays to store the first and last node of the chain |
| 9149 | * pertaining to each group |
| 9150 | */ |
| 9151 | list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
| 9152 | if (list == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9153 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 9154 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9155 | } |
| 9156 | memset(list, 0, nbgroups * sizeof(xmlNodePtr)); |
| 9157 | lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
| 9158 | if (lasts == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9159 | xmlRngVErrMemory(ctxt, "validating\n"); |
| 9160 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9161 | } |
| 9162 | memset(lasts, 0, nbgroups * sizeof(xmlNodePtr)); |
| 9163 | |
| 9164 | /* |
| 9165 | * Walk the sequence of children finding the right group and |
| 9166 | * sorting them in sequences. |
| 9167 | */ |
| 9168 | cur = ctxt->state->seq; |
| 9169 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 9170 | start = cur; |
| 9171 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9172 | ctxt->state->seq = cur; |
| 9173 | if ((partitions->triage != NULL) && |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9174 | (partitions->flags & IS_DETERMINIST)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9175 | void *tmp = NULL; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9176 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9177 | if ((cur->type == XML_TEXT_NODE) || |
| 9178 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 9179 | tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text", |
| 9180 | NULL); |
| 9181 | } else if (cur->type == XML_ELEMENT_NODE) { |
| 9182 | if (cur->ns != NULL) { |
| 9183 | tmp = xmlHashLookup2(partitions->triage, cur->name, |
| 9184 | cur->ns->href); |
| 9185 | if (tmp == NULL) |
| 9186 | tmp = xmlHashLookup2(partitions->triage, |
| 9187 | BAD_CAST "#any", |
| 9188 | cur->ns->href); |
| 9189 | } else |
| 9190 | tmp = |
| 9191 | xmlHashLookup2(partitions->triage, cur->name, |
| 9192 | NULL); |
| 9193 | if (tmp == NULL) |
| 9194 | tmp = |
| 9195 | xmlHashLookup2(partitions->triage, BAD_CAST "#any", |
| 9196 | NULL); |
| 9197 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9198 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9199 | if (tmp == NULL) { |
| 9200 | i = nbgroups; |
| 9201 | } else { |
| 9202 | i = ((long) tmp) - 1; |
| 9203 | if (partitions->flags & IS_NEEDCHECK) { |
| 9204 | group = partitions->groups[i]; |
| 9205 | if (!xmlRelaxNGNodeMatchesList(cur, group->defs)) |
| 9206 | i = nbgroups; |
| 9207 | } |
| 9208 | } |
| 9209 | } else { |
| 9210 | for (i = 0; i < nbgroups; i++) { |
| 9211 | group = partitions->groups[i]; |
| 9212 | if (group == NULL) |
| 9213 | continue; |
| 9214 | if (xmlRelaxNGNodeMatchesList(cur, group->defs)) |
| 9215 | break; |
| 9216 | } |
| 9217 | } |
| 9218 | /* |
| 9219 | * We break as soon as an element not matched is found |
| 9220 | */ |
| 9221 | if (i >= nbgroups) { |
| 9222 | break; |
| 9223 | } |
| 9224 | if (lasts[i] != NULL) { |
| 9225 | lasts[i]->next = cur; |
| 9226 | lasts[i] = cur; |
| 9227 | } else { |
| 9228 | list[i] = cur; |
| 9229 | lasts[i] = cur; |
| 9230 | } |
| 9231 | if (cur->next != NULL) |
| 9232 | lastchg = cur->next; |
| 9233 | else |
| 9234 | lastchg = cur; |
| 9235 | cur = xmlRelaxNGSkipIgnored(ctxt, cur->next); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9236 | } |
| 9237 | if (ret != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9238 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
| 9239 | ret = -1; |
| 9240 | goto done; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9241 | } |
| 9242 | lastelem = cur; |
| 9243 | oldstate = ctxt->state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9244 | for (i = 0; i < nbgroups; i++) { |
| 9245 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate); |
| 9246 | group = partitions->groups[i]; |
| 9247 | if (lasts[i] != NULL) { |
| 9248 | last = lasts[i]->next; |
| 9249 | lasts[i]->next = NULL; |
| 9250 | } |
| 9251 | ctxt->state->seq = list[i]; |
| 9252 | ret = xmlRelaxNGValidateDefinition(ctxt, group->rule); |
| 9253 | if (ret != 0) |
| 9254 | break; |
| 9255 | if (ctxt->state != NULL) { |
| 9256 | cur = ctxt->state->seq; |
| 9257 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 9258 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 9259 | oldstate = ctxt->state; |
| 9260 | ctxt->state = NULL; |
| 9261 | if (cur != NULL) { |
| 9262 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
| 9263 | ret = -1; |
| 9264 | ctxt->state = oldstate; |
| 9265 | goto done; |
| 9266 | } |
| 9267 | } else if (ctxt->states != NULL) { |
| 9268 | int j; |
| 9269 | int found = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9270 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9271 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 9272 | cur = ctxt->states->tabState[j]->seq; |
| 9273 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 9274 | if (cur == NULL) { |
| 9275 | found = 1; |
| 9276 | break; |
| 9277 | } |
| 9278 | } |
| 9279 | if (ctxt->states->nbState > 0) { |
| 9280 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 9281 | oldstate = |
| 9282 | ctxt->states->tabState[ctxt->states->nbState - 1]; |
| 9283 | } |
| 9284 | for (j = 0; j < ctxt->states->nbState - 1; j++) { |
| 9285 | xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[j]); |
| 9286 | } |
| 9287 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9288 | ctxt->states = NULL; |
| 9289 | if (found == 0) { |
| 9290 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
| 9291 | ret = -1; |
| 9292 | ctxt->state = oldstate; |
| 9293 | goto done; |
| 9294 | } |
| 9295 | } else { |
| 9296 | ret = -1; |
| 9297 | break; |
| 9298 | } |
| 9299 | if (lasts[i] != NULL) { |
| 9300 | lasts[i]->next = last; |
| 9301 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9302 | } |
| 9303 | if (ctxt->state != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9304 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9305 | ctxt->state = oldstate; |
| 9306 | ctxt->state->seq = lastelem; |
| 9307 | if (ret != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9308 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
| 9309 | ret = -1; |
| 9310 | goto done; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9311 | } |
| 9312 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9313 | done: |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9314 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9315 | /* |
| 9316 | * builds the next links chain from the prev one |
| 9317 | */ |
| 9318 | cur = lastchg; |
| 9319 | while (cur != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9320 | if ((cur == start) || (cur->prev == NULL)) |
| 9321 | break; |
| 9322 | cur->prev->next = cur; |
| 9323 | cur = cur->prev; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9324 | } |
| 9325 | if (ret == 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9326 | if (ctxt->errNr > errNr) |
| 9327 | xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9328 | } |
| 9329 | |
| 9330 | xmlFree(list); |
| 9331 | xmlFree(lasts); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9332 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9333 | } |
| 9334 | |
| 9335 | /** |
| 9336 | * xmlRelaxNGValidateDefinitionList: |
| 9337 | * @ctxt: a Relax-NG validation context |
| 9338 | * @define: the list of definition to verify |
| 9339 | * |
| 9340 | * Validate the given node content against the (list) of definitions |
| 9341 | * |
| 9342 | * Returns 0 if the validation succeeded or an error code. |
| 9343 | */ |
| 9344 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9345 | xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt, |
| 9346 | xmlRelaxNGDefinePtr defines) |
| 9347 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9348 | int ret = 0, res; |
| 9349 | |
| 9350 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 9351 | if (defines == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9352 | VALID_ERR2(XML_RELAXNG_ERR_INTERNAL, |
| 9353 | BAD_CAST "NULL definition list"); |
| 9354 | return (-1); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 9355 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9356 | while (defines != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9357 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
| 9358 | res = xmlRelaxNGValidateDefinition(ctxt, defines); |
| 9359 | if (res < 0) |
| 9360 | ret = -1; |
| 9361 | } else { |
| 9362 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 9363 | return (-1); |
| 9364 | } |
| 9365 | if (res == -1) /* continues on -2 */ |
| 9366 | break; |
| 9367 | defines = defines->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9368 | } |
| 9369 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9370 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9371 | } |
| 9372 | |
| 9373 | /** |
| 9374 | * xmlRelaxNGElementMatch: |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9375 | * @ctxt: a Relax-NG validation context |
| 9376 | * @define: the definition to check |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9377 | * @elem: the element |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9378 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9379 | * Check if the element matches the definition nameClass |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9380 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9381 | * 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] | 9382 | */ |
| 9383 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9384 | xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 9385 | xmlRelaxNGDefinePtr define, xmlNodePtr elem) |
| 9386 | { |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 9387 | int ret = 0, oldflags = 0; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9388 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9389 | if (define->name != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9390 | if (!xmlStrEqual(elem->name, define->name)) { |
| 9391 | VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name); |
| 9392 | return (0); |
| 9393 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9394 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9395 | if ((define->ns != NULL) && (define->ns[0] != 0)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9396 | if (elem->ns == NULL) { |
| 9397 | VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, elem->name); |
| 9398 | return (0); |
| 9399 | } else if (!xmlStrEqual(elem->ns->href, define->ns)) { |
| 9400 | VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS, |
| 9401 | elem->name, define->ns); |
| 9402 | return (0); |
| 9403 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9404 | } else if ((elem->ns != NULL) && (define->ns != NULL) && |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9405 | (define->name == NULL)) { |
| 9406 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, elem->name); |
| 9407 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9408 | } else if ((elem->ns != NULL) && (define->name != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9409 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, define->name); |
| 9410 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9411 | } |
| 9412 | |
| 9413 | if (define->nameClass == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9414 | return (1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9415 | |
| 9416 | define = define->nameClass; |
| 9417 | if (define->type == XML_RELAXNG_EXCEPT) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9418 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9419 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9420 | if (ctxt != NULL) { |
| 9421 | oldflags = ctxt->flags; |
| 9422 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9423 | } |
| 9424 | |
| 9425 | list = define->content; |
| 9426 | while (list != NULL) { |
| 9427 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
| 9428 | if (ret == 1) { |
| 9429 | if (ctxt != NULL) |
| 9430 | ctxt->flags = oldflags; |
| 9431 | return (0); |
| 9432 | } |
| 9433 | if (ret < 0) { |
| 9434 | if (ctxt != NULL) |
| 9435 | ctxt->flags = oldflags; |
| 9436 | return (ret); |
| 9437 | } |
| 9438 | list = list->next; |
| 9439 | } |
| 9440 | ret = 1; |
| 9441 | if (ctxt != NULL) { |
| 9442 | ctxt->flags = oldflags; |
| 9443 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9444 | } else if (define->type == XML_RELAXNG_CHOICE) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9445 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9446 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9447 | if (ctxt != NULL) { |
| 9448 | oldflags = ctxt->flags; |
| 9449 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9450 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9451 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9452 | list = define->nameClass; |
| 9453 | while (list != NULL) { |
| 9454 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
| 9455 | if (ret == 1) { |
| 9456 | if (ctxt != NULL) |
| 9457 | ctxt->flags = oldflags; |
| 9458 | return (1); |
| 9459 | } |
| 9460 | if (ret < 0) { |
| 9461 | if (ctxt != NULL) |
| 9462 | ctxt->flags = oldflags; |
| 9463 | return (ret); |
| 9464 | } |
| 9465 | list = list->next; |
| 9466 | } |
| 9467 | if (ctxt != NULL) { |
| 9468 | if (ret != 0) { |
| 9469 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9470 | xmlRelaxNGDumpValidError(ctxt); |
| 9471 | } else { |
| 9472 | if (ctxt->errNr > 0) |
| 9473 | xmlRelaxNGPopErrors(ctxt, 0); |
| 9474 | } |
| 9475 | } |
| 9476 | ret = 0; |
| 9477 | if (ctxt != NULL) { |
| 9478 | ctxt->flags = oldflags; |
| 9479 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9480 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9481 | TODO ret = -1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9482 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9483 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9484 | } |
| 9485 | |
| 9486 | /** |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9487 | * xmlRelaxNGBestState: |
| 9488 | * @ctxt: a Relax-NG validation context |
| 9489 | * |
| 9490 | * Find the "best" state in the ctxt->states list of states to report |
| 9491 | * errors about. I.e. a state with no element left in the child list |
| 9492 | * or the one with the less attributes left. |
| 9493 | * This is called only if a falidation error was detected |
| 9494 | * |
| 9495 | * Returns the index of the "best" state or -1 in case of error |
| 9496 | */ |
| 9497 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9498 | xmlRelaxNGBestState(xmlRelaxNGValidCtxtPtr ctxt) |
| 9499 | { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9500 | xmlRelaxNGValidStatePtr state; |
| 9501 | int i, tmp; |
| 9502 | int best = -1; |
| 9503 | int value = 1000000; |
| 9504 | |
| 9505 | if ((ctxt == NULL) || (ctxt->states == NULL) || |
| 9506 | (ctxt->states->nbState <= 0)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9507 | return (-1); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9508 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9509 | for (i = 0; i < ctxt->states->nbState; i++) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9510 | state = ctxt->states->tabState[i]; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9511 | if (state == NULL) |
| 9512 | continue; |
| 9513 | if (state->seq != NULL) { |
| 9514 | if ((best == -1) || (value > 100000)) { |
| 9515 | value = 100000; |
| 9516 | best = i; |
| 9517 | } |
| 9518 | } else { |
| 9519 | tmp = state->nbAttrLeft; |
| 9520 | if ((best == -1) || (value > tmp)) { |
| 9521 | value = tmp; |
| 9522 | best = i; |
| 9523 | } |
| 9524 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9525 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9526 | return (best); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9527 | } |
| 9528 | |
| 9529 | /** |
| 9530 | * xmlRelaxNGLogBestError: |
| 9531 | * @ctxt: a Relax-NG validation context |
| 9532 | * |
| 9533 | * Find the "best" state in the ctxt->states list of states to report |
| 9534 | * errors about and log it. |
| 9535 | */ |
| 9536 | static void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9537 | xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt) |
| 9538 | { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9539 | int best; |
| 9540 | |
| 9541 | if ((ctxt == NULL) || (ctxt->states == NULL) || |
| 9542 | (ctxt->states->nbState <= 0)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9543 | return; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9544 | |
| 9545 | best = xmlRelaxNGBestState(ctxt); |
| 9546 | if ((best >= 0) && (best < ctxt->states->nbState)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9547 | ctxt->state = ctxt->states->tabState[best]; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9548 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9549 | xmlRelaxNGValidateElementEnd(ctxt, 1); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9550 | } |
| 9551 | } |
| 9552 | |
| 9553 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9554 | * xmlRelaxNGValidateElementEnd: |
| 9555 | * @ctxt: a Relax-NG validation context |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 9556 | * @dolog: indicate that error logging should be done |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9557 | * |
| 9558 | * Validate the end of the element, implements check that |
| 9559 | * there is nothing left not consumed in the element content |
| 9560 | * or in the attribute list. |
| 9561 | * |
| 9562 | * Returns 0 if the validation succeeded or an error code. |
| 9563 | */ |
| 9564 | static int |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 9565 | xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int dolog) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9566 | { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9567 | int i; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9568 | xmlRelaxNGValidStatePtr state; |
| 9569 | |
| 9570 | state = ctxt->state; |
| 9571 | if (state->seq != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9572 | state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq); |
| 9573 | if (state->seq != NULL) { |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 9574 | if (dolog) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9575 | VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT, |
| 9576 | state->node->name, state->seq->name); |
| 9577 | } |
| 9578 | return (-1); |
| 9579 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9580 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9581 | for (i = 0; i < state->nbAttrs; i++) { |
| 9582 | if (state->attrs[i] != NULL) { |
William M. Brack | 272693c | 2003-11-14 16:20:34 +0000 | [diff] [blame] | 9583 | if (dolog) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9584 | VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR, |
| 9585 | state->attrs[i]->name, state->node->name); |
| 9586 | } |
| 9587 | return (-1 - i); |
| 9588 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9589 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9590 | return (0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9591 | } |
| 9592 | |
| 9593 | /** |
| 9594 | * xmlRelaxNGValidateState: |
| 9595 | * @ctxt: a Relax-NG validation context |
| 9596 | * @define: the definition to verify |
| 9597 | * |
| 9598 | * Validate the current state against the definition |
| 9599 | * |
| 9600 | * Returns 0 if the validation succeeded or an error code. |
| 9601 | */ |
| 9602 | static int |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9603 | xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt, |
| 9604 | xmlRelaxNGDefinePtr define) |
| 9605 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9606 | xmlNodePtr node; |
| 9607 | int ret = 0, i, tmp, oldflags, errNr; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9608 | xmlRelaxNGValidStatePtr oldstate = NULL, state; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9609 | |
| 9610 | if (define == NULL) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9611 | VALID_ERR(XML_RELAXNG_ERR_NODEFINE); |
| 9612 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9613 | } |
| 9614 | |
| 9615 | if (ctxt->state != NULL) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9616 | node = ctxt->state->seq; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9617 | } else { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9618 | node = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9619 | } |
| 9620 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9621 | for (i = 0; i < ctxt->depth; i++) |
| 9622 | xmlGenericError(xmlGenericErrorContext, " "); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9623 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9624 | "Start validating %s ", xmlRelaxNGDefName(define)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9625 | if (define->name != NULL) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9626 | xmlGenericError(xmlGenericErrorContext, "%s ", define->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9627 | if ((node != NULL) && (node->name != NULL)) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9628 | xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9629 | else |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9630 | xmlGenericError(xmlGenericErrorContext, "\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9631 | #endif |
| 9632 | ctxt->depth++; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9633 | switch (define->type) { |
| 9634 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9635 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 9636 | ret = 0; |
| 9637 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9638 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9639 | ret = -1; |
| 9640 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9641 | case XML_RELAXNG_TEXT: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9642 | while ((node != NULL) && |
| 9643 | ((node->type == XML_TEXT_NODE) || |
| 9644 | (node->type == XML_COMMENT_NODE) || |
| 9645 | (node->type == XML_PI_NODE) || |
| 9646 | (node->type == XML_CDATA_SECTION_NODE))) |
| 9647 | node = node->next; |
| 9648 | ctxt->state->seq = node; |
| 9649 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9650 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9651 | errNr = ctxt->errNr; |
| 9652 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 9653 | if (node == NULL) { |
| 9654 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name); |
| 9655 | ret = -1; |
| 9656 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9657 | xmlRelaxNGDumpValidError(ctxt); |
| 9658 | break; |
| 9659 | } |
| 9660 | if (node->type != XML_ELEMENT_NODE) { |
| 9661 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
| 9662 | ret = -1; |
| 9663 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9664 | xmlRelaxNGDumpValidError(ctxt); |
| 9665 | break; |
| 9666 | } |
| 9667 | /* |
| 9668 | * This node was already validated successfully against |
| 9669 | * this definition. |
| 9670 | */ |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 9671 | if (node->psvi == define) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9672 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
| 9673 | if (ctxt->errNr > errNr) |
| 9674 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9675 | if (ctxt->errNr != 0) { |
| 9676 | while ((ctxt->err != NULL) && |
| 9677 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) |
| 9678 | && (xmlStrEqual(ctxt->err->arg2, node->name))) |
| 9679 | || |
| 9680 | ((ctxt->err->err == |
| 9681 | XML_RELAXNG_ERR_ELEMEXTRANS) |
| 9682 | && (xmlStrEqual(ctxt->err->arg1, node->name))) |
| 9683 | || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) |
| 9684 | || (ctxt->err->err == |
| 9685 | XML_RELAXNG_ERR_NOTELEM))) |
| 9686 | xmlRelaxNGValidErrorPop(ctxt); |
| 9687 | } |
| 9688 | break; |
| 9689 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9690 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9691 | ret = xmlRelaxNGElementMatch(ctxt, define, node); |
| 9692 | if (ret <= 0) { |
| 9693 | ret = -1; |
| 9694 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9695 | xmlRelaxNGDumpValidError(ctxt); |
| 9696 | break; |
| 9697 | } |
| 9698 | ret = 0; |
| 9699 | if (ctxt->errNr != 0) { |
| 9700 | if (ctxt->errNr > errNr) |
| 9701 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9702 | while ((ctxt->err != NULL) && |
| 9703 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) && |
| 9704 | (xmlStrEqual(ctxt->err->arg2, node->name))) || |
| 9705 | ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) && |
| 9706 | (xmlStrEqual(ctxt->err->arg1, node->name))) || |
| 9707 | (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) || |
| 9708 | (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM))) |
| 9709 | xmlRelaxNGValidErrorPop(ctxt); |
| 9710 | } |
| 9711 | errNr = ctxt->errNr; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9712 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9713 | oldflags = ctxt->flags; |
| 9714 | if (ctxt->flags & FLAGS_MIXED_CONTENT) { |
| 9715 | ctxt->flags -= FLAGS_MIXED_CONTENT; |
| 9716 | } |
| 9717 | state = xmlRelaxNGNewValidState(ctxt, node); |
| 9718 | if (state == NULL) { |
| 9719 | ret = -1; |
| 9720 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9721 | xmlRelaxNGDumpValidError(ctxt); |
| 9722 | break; |
| 9723 | } |
Daniel Veillard | 7fe1f3a | 2003-03-31 22:13:33 +0000 | [diff] [blame] | 9724 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9725 | oldstate = ctxt->state; |
| 9726 | ctxt->state = state; |
| 9727 | if (define->attrs != NULL) { |
| 9728 | tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
| 9729 | if (tmp != 0) { |
| 9730 | ret = -1; |
| 9731 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
| 9732 | } |
| 9733 | } |
| 9734 | if (define->contModel != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9735 | xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state; |
| 9736 | xmlRelaxNGStatesPtr tmpstates = ctxt->states; |
| 9737 | xmlNodePtr nseq; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9738 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9739 | nstate = xmlRelaxNGNewValidState(ctxt, node); |
| 9740 | ctxt->state = nstate; |
| 9741 | ctxt->states = NULL; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9742 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9743 | tmp = xmlRelaxNGValidateCompiledContent(ctxt, |
| 9744 | define->contModel, |
| 9745 | ctxt->state->seq); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9746 | nseq = ctxt->state->seq; |
| 9747 | ctxt->state = tmpstate; |
| 9748 | ctxt->states = tmpstates; |
| 9749 | xmlRelaxNGFreeValidState(ctxt, nstate); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9750 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9751 | #ifdef DEBUG_COMPILE |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9752 | xmlGenericError(xmlGenericErrorContext, |
| 9753 | "Validating content of '%s' : %d\n", |
| 9754 | define->name, tmp); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9755 | #endif |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9756 | if (tmp != 0) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9757 | ret = -1; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9758 | |
| 9759 | if (ctxt->states != NULL) { |
| 9760 | tmp = -1; |
| 9761 | |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9762 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9763 | state = ctxt->states->tabState[i]; |
| 9764 | ctxt->state = state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9765 | ctxt->state->seq = nseq; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9766 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9767 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9768 | tmp = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9769 | break; |
| 9770 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9771 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9772 | if (tmp != 0) { |
| 9773 | /* |
| 9774 | * validation error, log the message for the "best" one |
| 9775 | */ |
| 9776 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9777 | xmlRelaxNGLogBestError(ctxt); |
| 9778 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9779 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9780 | xmlRelaxNGFreeValidState(ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9781 | ctxt->states-> |
| 9782 | tabState[i]); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9783 | } |
| 9784 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9785 | ctxt->flags = oldflags; |
| 9786 | ctxt->states = NULL; |
| 9787 | if ((ret == 0) && (tmp == -1)) |
| 9788 | ret = -1; |
| 9789 | } else { |
| 9790 | state = ctxt->state; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9791 | ctxt->state->seq = nseq; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9792 | if (ret == 0) |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9793 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9794 | xmlRelaxNGFreeValidState(ctxt, state); |
| 9795 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9796 | } else { |
| 9797 | if (define->content != NULL) { |
| 9798 | tmp = xmlRelaxNGValidateDefinitionList(ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9799 | define-> |
| 9800 | content); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9801 | if (tmp != 0) { |
| 9802 | ret = -1; |
| 9803 | if (ctxt->state == NULL) { |
| 9804 | ctxt->state = oldstate; |
| 9805 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, |
| 9806 | node->name); |
| 9807 | ctxt->state = NULL; |
| 9808 | } else { |
| 9809 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, |
| 9810 | node->name); |
| 9811 | } |
| 9812 | |
| 9813 | } |
| 9814 | } |
| 9815 | if (ctxt->states != NULL) { |
| 9816 | tmp = -1; |
| 9817 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9818 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9819 | state = ctxt->states->tabState[i]; |
| 9820 | ctxt->state = state; |
| 9821 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9822 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9823 | tmp = 0; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9824 | break; |
| 9825 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9826 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9827 | if (tmp != 0) { |
| 9828 | /* |
| 9829 | * validation error, log the message for the "best" one |
| 9830 | */ |
| 9831 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9832 | xmlRelaxNGLogBestError(ctxt); |
| 9833 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9834 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9835 | xmlRelaxNGFreeValidState(ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 9836 | ctxt->states-> |
| 9837 | tabState[i]); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9838 | } |
| 9839 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9840 | ctxt->flags = oldflags; |
| 9841 | ctxt->states = NULL; |
| 9842 | if ((ret == 0) && (tmp == -1)) |
| 9843 | ret = -1; |
| 9844 | } else { |
| 9845 | state = ctxt->state; |
| 9846 | if (ret == 0) |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9847 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9848 | xmlRelaxNGFreeValidState(ctxt, state); |
| 9849 | } |
| 9850 | } |
| 9851 | if (ret == 0) { |
Daniel Veillard | 807daf8 | 2004-02-22 22:13:27 +0000 | [diff] [blame] | 9852 | node->psvi = define; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9853 | } |
| 9854 | ctxt->flags = oldflags; |
| 9855 | ctxt->state = oldstate; |
| 9856 | if (oldstate != NULL) |
| 9857 | oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
| 9858 | if (ret != 0) { |
| 9859 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 9860 | xmlRelaxNGDumpValidError(ctxt); |
| 9861 | ret = 0; |
| 9862 | } else { |
| 9863 | ret = -2; |
| 9864 | } |
| 9865 | } else { |
| 9866 | if (ctxt->errNr > errNr) |
| 9867 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9868 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9869 | |
| 9870 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9871 | xmlGenericError(xmlGenericErrorContext, |
| 9872 | "xmlRelaxNGValidateDefinition(): validated %s : %d", |
| 9873 | node->name, ret); |
| 9874 | if (oldstate == NULL) |
| 9875 | xmlGenericError(xmlGenericErrorContext, ": no state\n"); |
| 9876 | else if (oldstate->seq == NULL) |
| 9877 | xmlGenericError(xmlGenericErrorContext, ": done\n"); |
| 9878 | else if (oldstate->seq->type == XML_ELEMENT_NODE) |
| 9879 | xmlGenericError(xmlGenericErrorContext, ": next elem %s\n", |
| 9880 | oldstate->seq->name); |
| 9881 | else |
| 9882 | xmlGenericError(xmlGenericErrorContext, ": next %s %d\n", |
| 9883 | oldstate->seq->name, oldstate->seq->type); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9884 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9885 | break; |
| 9886 | case XML_RELAXNG_OPTIONAL:{ |
| 9887 | errNr = ctxt->errNr; |
| 9888 | oldflags = ctxt->flags; |
| 9889 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9890 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
| 9891 | ret = |
| 9892 | xmlRelaxNGValidateDefinitionList(ctxt, |
| 9893 | define->content); |
| 9894 | if (ret != 0) { |
| 9895 | if (ctxt->state != NULL) |
| 9896 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 9897 | ctxt->state = oldstate; |
| 9898 | ctxt->flags = oldflags; |
| 9899 | ret = 0; |
| 9900 | if (ctxt->errNr > errNr) |
| 9901 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9902 | break; |
| 9903 | } |
| 9904 | if (ctxt->states != NULL) { |
| 9905 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
| 9906 | } else { |
| 9907 | ctxt->states = xmlRelaxNGNewStates(ctxt, 1); |
| 9908 | if (ctxt->states == NULL) { |
| 9909 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 9910 | ctxt->flags = oldflags; |
| 9911 | ret = -1; |
| 9912 | if (ctxt->errNr > errNr) |
| 9913 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9914 | break; |
| 9915 | } |
| 9916 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
| 9917 | xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state); |
| 9918 | ctxt->state = NULL; |
| 9919 | } |
| 9920 | ctxt->flags = oldflags; |
| 9921 | ret = 0; |
| 9922 | if (ctxt->errNr > errNr) |
| 9923 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9924 | break; |
| 9925 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9926 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9927 | errNr = ctxt->errNr; |
| 9928 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 9929 | if (ret != 0) { |
| 9930 | break; |
| 9931 | } |
| 9932 | if (ctxt->errNr > errNr) |
| 9933 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9934 | /* no break on purpose */ |
| 9935 | case XML_RELAXNG_ZEROORMORE:{ |
| 9936 | int progress; |
| 9937 | xmlRelaxNGStatesPtr states = NULL, res = NULL; |
| 9938 | int base, j; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9939 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9940 | errNr = ctxt->errNr; |
| 9941 | res = xmlRelaxNGNewStates(ctxt, 1); |
| 9942 | if (res == NULL) { |
| 9943 | ret = -1; |
| 9944 | break; |
| 9945 | } |
| 9946 | /* |
| 9947 | * All the input states are also exit states |
| 9948 | */ |
| 9949 | if (ctxt->state != NULL) { |
| 9950 | xmlRelaxNGAddStates(ctxt, res, |
| 9951 | xmlRelaxNGCopyValidState(ctxt, |
| 9952 | ctxt-> |
| 9953 | state)); |
| 9954 | } else { |
| 9955 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 9956 | xmlRelaxNGAddStates(ctxt, res, |
| 9957 | xmlRelaxNGCopyValidState(ctxt, |
| 9958 | ctxt-> |
| 9959 | states-> |
| 9960 | tabState |
| 9961 | [j])); |
| 9962 | } |
| 9963 | } |
| 9964 | oldflags = ctxt->flags; |
| 9965 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9966 | do { |
| 9967 | progress = 0; |
| 9968 | base = res->nbState; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9969 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9970 | if (ctxt->states != NULL) { |
| 9971 | states = ctxt->states; |
| 9972 | for (i = 0; i < states->nbState; i++) { |
| 9973 | ctxt->state = states->tabState[i]; |
| 9974 | ctxt->states = NULL; |
| 9975 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
| 9976 | define-> |
| 9977 | content); |
| 9978 | if (ret == 0) { |
| 9979 | if (ctxt->state != NULL) { |
| 9980 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 9981 | ctxt->state); |
| 9982 | ctxt->state = NULL; |
| 9983 | if (tmp == 1) |
| 9984 | progress = 1; |
| 9985 | } else if (ctxt->states != NULL) { |
| 9986 | for (j = 0; j < ctxt->states->nbState; |
| 9987 | j++) { |
| 9988 | tmp = |
| 9989 | xmlRelaxNGAddStates(ctxt, res, |
| 9990 | ctxt-> |
| 9991 | states-> |
| 9992 | tabState |
| 9993 | [j]); |
| 9994 | if (tmp == 1) |
| 9995 | progress = 1; |
| 9996 | } |
| 9997 | xmlRelaxNGFreeStates(ctxt, |
| 9998 | ctxt->states); |
| 9999 | ctxt->states = NULL; |
| 10000 | } |
| 10001 | } else { |
| 10002 | if (ctxt->state != NULL) { |
| 10003 | xmlRelaxNGFreeValidState(ctxt, |
| 10004 | ctxt->state); |
| 10005 | ctxt->state = NULL; |
| 10006 | } |
| 10007 | } |
| 10008 | } |
| 10009 | } else { |
| 10010 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
| 10011 | define-> |
| 10012 | content); |
| 10013 | if (ret != 0) { |
| 10014 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10015 | ctxt->state = NULL; |
| 10016 | } else { |
| 10017 | base = res->nbState; |
| 10018 | if (ctxt->state != NULL) { |
| 10019 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 10020 | ctxt->state); |
| 10021 | ctxt->state = NULL; |
| 10022 | if (tmp == 1) |
| 10023 | progress = 1; |
| 10024 | } else if (ctxt->states != NULL) { |
| 10025 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 10026 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 10027 | ctxt-> |
| 10028 | states-> |
| 10029 | tabState[j]); |
| 10030 | if (tmp == 1) |
| 10031 | progress = 1; |
| 10032 | } |
| 10033 | if (states == NULL) { |
| 10034 | states = ctxt->states; |
| 10035 | } else { |
| 10036 | xmlRelaxNGFreeStates(ctxt, |
| 10037 | ctxt->states); |
| 10038 | } |
| 10039 | ctxt->states = NULL; |
| 10040 | } |
| 10041 | } |
| 10042 | } |
| 10043 | if (progress) { |
| 10044 | /* |
| 10045 | * Collect all the new nodes added at that step |
| 10046 | * and make them the new node set |
| 10047 | */ |
| 10048 | if (res->nbState - base == 1) { |
| 10049 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, |
| 10050 | res-> |
| 10051 | tabState |
| 10052 | [base]); |
| 10053 | } else { |
| 10054 | if (states == NULL) { |
| 10055 | xmlRelaxNGNewStates(ctxt, |
| 10056 | res->nbState - base); |
| 10057 | } |
| 10058 | states->nbState = 0; |
| 10059 | for (i = base; i < res->nbState; i++) |
| 10060 | xmlRelaxNGAddStates(ctxt, states, |
| 10061 | xmlRelaxNGCopyValidState |
| 10062 | (ctxt, |
| 10063 | res->tabState[i])); |
| 10064 | ctxt->states = states; |
| 10065 | } |
| 10066 | } |
| 10067 | } while (progress == 1); |
| 10068 | if (states != NULL) { |
| 10069 | xmlRelaxNGFreeStates(ctxt, states); |
| 10070 | } |
| 10071 | ctxt->states = res; |
| 10072 | ctxt->flags = oldflags; |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 10073 | #if 0 |
| 10074 | /* |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10075 | * errors may have to be propagated back... |
| 10076 | */ |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10077 | if (ctxt->errNr > errNr) |
| 10078 | xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 10079 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10080 | ret = 0; |
| 10081 | break; |
| 10082 | } |
| 10083 | case XML_RELAXNG_CHOICE:{ |
| 10084 | xmlRelaxNGDefinePtr list = NULL; |
| 10085 | xmlRelaxNGStatesPtr states = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10086 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10087 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10088 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10089 | errNr = ctxt->errNr; |
Daniel Veillard | 9186a1f | 2005-01-15 12:38:10 +0000 | [diff] [blame] | 10090 | if ((define->dflags & IS_TRIABLE) && (define->data != NULL) && |
| 10091 | (node != NULL)) { |
| 10092 | /* |
| 10093 | * node == NULL can't be optimized since IS_TRIABLE |
| 10094 | * doesn't account for choice which may lead to |
| 10095 | * only attributes. |
| 10096 | */ |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10097 | xmlHashTablePtr triage = |
| 10098 | (xmlHashTablePtr) define->data; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 10099 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10100 | /* |
| 10101 | * Something we can optimize cleanly there is only one |
| 10102 | * possble branch out ! |
| 10103 | */ |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10104 | if ((node->type == XML_TEXT_NODE) || |
| 10105 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 10106 | list = |
| 10107 | xmlHashLookup2(triage, BAD_CAST "#text", NULL); |
| 10108 | } else if (node->type == XML_ELEMENT_NODE) { |
| 10109 | if (node->ns != NULL) { |
| 10110 | list = xmlHashLookup2(triage, node->name, |
| 10111 | node->ns->href); |
| 10112 | if (list == NULL) |
| 10113 | list = |
| 10114 | xmlHashLookup2(triage, BAD_CAST "#any", |
| 10115 | node->ns->href); |
| 10116 | } else |
| 10117 | list = |
| 10118 | xmlHashLookup2(triage, node->name, NULL); |
| 10119 | if (list == NULL) |
| 10120 | list = |
| 10121 | xmlHashLookup2(triage, BAD_CAST "#any", |
| 10122 | NULL); |
| 10123 | } |
| 10124 | if (list == NULL) { |
| 10125 | ret = -1; |
William M. Brack | 2f07606 | 2004-03-21 11:21:14 +0000 | [diff] [blame] | 10126 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, node->name); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10127 | break; |
| 10128 | } |
| 10129 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
| 10130 | if (ret == 0) { |
| 10131 | } |
| 10132 | break; |
| 10133 | } |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 10134 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10135 | list = define->content; |
| 10136 | oldflags = ctxt->flags; |
| 10137 | ctxt->flags |= FLAGS_IGNORABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10138 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10139 | while (list != NULL) { |
| 10140 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
| 10141 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
| 10142 | if (ret == 0) { |
| 10143 | if (states == NULL) { |
| 10144 | states = xmlRelaxNGNewStates(ctxt, 1); |
| 10145 | } |
| 10146 | if (ctxt->state != NULL) { |
| 10147 | xmlRelaxNGAddStates(ctxt, states, ctxt->state); |
| 10148 | } else if (ctxt->states != NULL) { |
| 10149 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 10150 | xmlRelaxNGAddStates(ctxt, states, |
| 10151 | ctxt->states-> |
| 10152 | tabState[i]); |
| 10153 | } |
| 10154 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10155 | ctxt->states = NULL; |
| 10156 | } |
| 10157 | } else { |
| 10158 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10159 | } |
| 10160 | ctxt->state = oldstate; |
| 10161 | list = list->next; |
| 10162 | } |
| 10163 | if (states != NULL) { |
| 10164 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 10165 | ctxt->states = states; |
| 10166 | ctxt->state = NULL; |
| 10167 | ret = 0; |
| 10168 | } else { |
| 10169 | ctxt->states = NULL; |
| 10170 | } |
| 10171 | ctxt->flags = oldflags; |
| 10172 | if (ret != 0) { |
| 10173 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 10174 | xmlRelaxNGDumpValidError(ctxt); |
| 10175 | } |
| 10176 | } else { |
| 10177 | if (ctxt->errNr > errNr) |
| 10178 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 10179 | } |
| 10180 | break; |
| 10181 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10182 | case XML_RELAXNG_DEF: |
| 10183 | case XML_RELAXNG_GROUP: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10184 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 10185 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10186 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10187 | ret = xmlRelaxNGValidateInterleave(ctxt, define); |
| 10188 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10189 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10190 | ret = xmlRelaxNGValidateAttribute(ctxt, define); |
| 10191 | break; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 10192 | case XML_RELAXNG_START: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10193 | case XML_RELAXNG_NOOP: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10194 | case XML_RELAXNG_REF: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10195 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 10196 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10197 | ret = xmlRelaxNGValidateDefinition(ctxt, define->content); |
| 10198 | break; |
| 10199 | case XML_RELAXNG_DATATYPE:{ |
| 10200 | xmlNodePtr child; |
| 10201 | xmlChar *content = NULL; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10202 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10203 | child = node; |
| 10204 | while (child != NULL) { |
| 10205 | if (child->type == XML_ELEMENT_NODE) { |
| 10206 | VALID_ERR2(XML_RELAXNG_ERR_DATAELEM, |
| 10207 | node->parent->name); |
| 10208 | ret = -1; |
| 10209 | break; |
| 10210 | } else if ((child->type == XML_TEXT_NODE) || |
| 10211 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 10212 | content = xmlStrcat(content, child->content); |
| 10213 | } |
| 10214 | /* TODO: handle entities ... */ |
| 10215 | child = child->next; |
| 10216 | } |
| 10217 | if (ret == -1) { |
| 10218 | if (content != NULL) |
| 10219 | xmlFree(content); |
| 10220 | break; |
| 10221 | } |
| 10222 | if (content == NULL) { |
| 10223 | content = xmlStrdup(BAD_CAST ""); |
| 10224 | if (content == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10225 | xmlRngVErrMemory(ctxt, "validating\n"); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10226 | ret = -1; |
| 10227 | break; |
| 10228 | } |
| 10229 | } |
| 10230 | ret = xmlRelaxNGValidateDatatype(ctxt, content, define, |
| 10231 | ctxt->state->seq); |
| 10232 | if (ret == -1) { |
| 10233 | VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name); |
| 10234 | } else if (ret == 0) { |
| 10235 | ctxt->state->seq = NULL; |
| 10236 | } |
| 10237 | if (content != NULL) |
| 10238 | xmlFree(content); |
| 10239 | break; |
| 10240 | } |
| 10241 | case XML_RELAXNG_VALUE:{ |
| 10242 | xmlChar *content = NULL; |
| 10243 | xmlChar *oldvalue; |
| 10244 | xmlNodePtr child; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10245 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10246 | child = node; |
| 10247 | while (child != NULL) { |
| 10248 | if (child->type == XML_ELEMENT_NODE) { |
| 10249 | VALID_ERR2(XML_RELAXNG_ERR_VALELEM, |
| 10250 | node->parent->name); |
| 10251 | ret = -1; |
| 10252 | break; |
| 10253 | } else if ((child->type == XML_TEXT_NODE) || |
| 10254 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 10255 | content = xmlStrcat(content, child->content); |
| 10256 | } |
| 10257 | /* TODO: handle entities ... */ |
| 10258 | child = child->next; |
| 10259 | } |
| 10260 | if (ret == -1) { |
| 10261 | if (content != NULL) |
| 10262 | xmlFree(content); |
| 10263 | break; |
| 10264 | } |
| 10265 | if (content == NULL) { |
| 10266 | content = xmlStrdup(BAD_CAST ""); |
| 10267 | if (content == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10268 | xmlRngVErrMemory(ctxt, "validating\n"); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10269 | ret = -1; |
| 10270 | break; |
| 10271 | } |
| 10272 | } |
| 10273 | oldvalue = ctxt->state->value; |
| 10274 | ctxt->state->value = content; |
| 10275 | ret = xmlRelaxNGValidateValue(ctxt, define); |
| 10276 | ctxt->state->value = oldvalue; |
| 10277 | if (ret == -1) { |
| 10278 | VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name); |
| 10279 | } else if (ret == 0) { |
| 10280 | ctxt->state->seq = NULL; |
| 10281 | } |
| 10282 | if (content != NULL) |
| 10283 | xmlFree(content); |
| 10284 | break; |
| 10285 | } |
| 10286 | case XML_RELAXNG_LIST:{ |
| 10287 | xmlChar *content; |
| 10288 | xmlNodePtr child; |
| 10289 | xmlChar *oldvalue, *oldendvalue; |
| 10290 | int len; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10291 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10292 | /* |
| 10293 | * Make sure it's only text nodes |
| 10294 | */ |
| 10295 | |
| 10296 | content = NULL; |
| 10297 | child = node; |
| 10298 | while (child != NULL) { |
| 10299 | if (child->type == XML_ELEMENT_NODE) { |
| 10300 | VALID_ERR2(XML_RELAXNG_ERR_LISTELEM, |
| 10301 | node->parent->name); |
| 10302 | ret = -1; |
| 10303 | break; |
| 10304 | } else if ((child->type == XML_TEXT_NODE) || |
| 10305 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 10306 | content = xmlStrcat(content, child->content); |
| 10307 | } |
| 10308 | /* TODO: handle entities ... */ |
| 10309 | child = child->next; |
| 10310 | } |
| 10311 | if (ret == -1) { |
| 10312 | if (content != NULL) |
| 10313 | xmlFree(content); |
| 10314 | break; |
| 10315 | } |
| 10316 | if (content == NULL) { |
| 10317 | content = xmlStrdup(BAD_CAST ""); |
| 10318 | if (content == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10319 | xmlRngVErrMemory(ctxt, "validating\n"); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10320 | ret = -1; |
| 10321 | break; |
| 10322 | } |
| 10323 | } |
| 10324 | len = xmlStrlen(content); |
| 10325 | oldvalue = ctxt->state->value; |
| 10326 | oldendvalue = ctxt->state->endvalue; |
| 10327 | ctxt->state->value = content; |
| 10328 | ctxt->state->endvalue = content + len; |
| 10329 | ret = xmlRelaxNGValidateValue(ctxt, define); |
| 10330 | ctxt->state->value = oldvalue; |
| 10331 | ctxt->state->endvalue = oldendvalue; |
| 10332 | if (ret == -1) { |
| 10333 | VALID_ERR(XML_RELAXNG_ERR_LIST); |
| 10334 | } else if ((ret == 0) && (node != NULL)) { |
| 10335 | ctxt->state->seq = node->next; |
| 10336 | } |
| 10337 | if (content != NULL) |
| 10338 | xmlFree(content); |
| 10339 | break; |
| 10340 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10341 | case XML_RELAXNG_EXCEPT: |
| 10342 | case XML_RELAXNG_PARAM: |
| 10343 | TODO ret = -1; |
| 10344 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10345 | } |
| 10346 | ctxt->depth--; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10347 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10348 | for (i = 0; i < ctxt->depth; i++) |
| 10349 | xmlGenericError(xmlGenericErrorContext, " "); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10350 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10351 | "Validating %s ", xmlRelaxNGDefName(define)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10352 | if (define->name != NULL) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10353 | xmlGenericError(xmlGenericErrorContext, "%s ", define->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10354 | if (ret == 0) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10355 | xmlGenericError(xmlGenericErrorContext, "suceeded\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10356 | else |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10357 | xmlGenericError(xmlGenericErrorContext, "failed\n"); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10358 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10359 | return (ret); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10360 | } |
| 10361 | |
| 10362 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10363 | * xmlRelaxNGValidateDefinition: |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10364 | * @ctxt: a Relax-NG validation context |
| 10365 | * @define: the definition to verify |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10366 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10367 | * Validate the current node lists against the definition |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10368 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10369 | * Returns 0 if the validation succeeded or an error code. |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10370 | */ |
| 10371 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10372 | xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
| 10373 | xmlRelaxNGDefinePtr define) |
| 10374 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10375 | xmlRelaxNGStatesPtr states, res; |
| 10376 | int i, j, k, ret, oldflags; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10377 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10378 | /* |
| 10379 | * We should NOT have both ctxt->state and ctxt->states |
| 10380 | */ |
| 10381 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10382 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10383 | ctxt->state = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10384 | } |
| 10385 | |
| 10386 | if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10387 | if (ctxt->states != NULL) { |
| 10388 | ctxt->state = ctxt->states->tabState[0]; |
| 10389 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10390 | ctxt->states = NULL; |
| 10391 | } |
| 10392 | ret = xmlRelaxNGValidateState(ctxt, define); |
| 10393 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10394 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10395 | ctxt->state = NULL; |
| 10396 | } |
| 10397 | if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) { |
| 10398 | ctxt->state = ctxt->states->tabState[0]; |
| 10399 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10400 | ctxt->states = NULL; |
| 10401 | } |
| 10402 | return (ret); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10403 | } |
| 10404 | |
| 10405 | states = ctxt->states; |
| 10406 | ctxt->states = NULL; |
| 10407 | res = NULL; |
| 10408 | j = 0; |
| 10409 | oldflags = ctxt->flags; |
| 10410 | ctxt->flags |= FLAGS_IGNORABLE; |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10411 | for (i = 0; i < states->nbState; i++) { |
| 10412 | ctxt->state = states->tabState[i]; |
| 10413 | ctxt->states = NULL; |
| 10414 | ret = xmlRelaxNGValidateState(ctxt, define); |
| 10415 | /* |
| 10416 | * We should NOT have both ctxt->state and ctxt->states |
| 10417 | */ |
| 10418 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10419 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10420 | ctxt->state = NULL; |
| 10421 | } |
| 10422 | if (ret == 0) { |
| 10423 | if (ctxt->states == NULL) { |
| 10424 | if (res != NULL) { |
| 10425 | /* add the state to the container */ |
| 10426 | xmlRelaxNGAddStates(ctxt, res, ctxt->state); |
| 10427 | ctxt->state = NULL; |
| 10428 | } else { |
| 10429 | /* add the state directly in states */ |
| 10430 | states->tabState[j++] = ctxt->state; |
| 10431 | ctxt->state = NULL; |
| 10432 | } |
| 10433 | } else { |
| 10434 | if (res == NULL) { |
| 10435 | /* make it the new container and copy other results */ |
| 10436 | res = ctxt->states; |
| 10437 | ctxt->states = NULL; |
| 10438 | for (k = 0; k < j; k++) |
| 10439 | xmlRelaxNGAddStates(ctxt, res, |
| 10440 | states->tabState[k]); |
| 10441 | } else { |
| 10442 | /* add all the new results to res and reff the container */ |
| 10443 | for (k = 0; k < ctxt->states->nbState; k++) |
| 10444 | xmlRelaxNGAddStates(ctxt, res, |
| 10445 | ctxt->states->tabState[k]); |
| 10446 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10447 | ctxt->states = NULL; |
| 10448 | } |
| 10449 | } |
| 10450 | } else { |
| 10451 | if (ctxt->state != NULL) { |
| 10452 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10453 | ctxt->state = NULL; |
| 10454 | } else if (ctxt->states != NULL) { |
| 10455 | for (k = 0; k < ctxt->states->nbState; k++) |
| 10456 | xmlRelaxNGFreeValidState(ctxt, |
| 10457 | ctxt->states->tabState[k]); |
| 10458 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10459 | ctxt->states = NULL; |
| 10460 | } |
| 10461 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10462 | } |
| 10463 | ctxt->flags = oldflags; |
| 10464 | if (res != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10465 | xmlRelaxNGFreeStates(ctxt, states); |
| 10466 | ctxt->states = res; |
| 10467 | ret = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10468 | } else if (j > 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10469 | states->nbState = j; |
| 10470 | ctxt->states = states; |
| 10471 | ret = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10472 | } else if (j == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10473 | ctxt->state = states->tabState[0]; |
| 10474 | xmlRelaxNGFreeStates(ctxt, states); |
| 10475 | ret = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10476 | } else { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10477 | ret = -1; |
| 10478 | xmlRelaxNGFreeStates(ctxt, states); |
| 10479 | if (ctxt->states != NULL) { |
| 10480 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10481 | ctxt->states = NULL; |
| 10482 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10483 | } |
| 10484 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10485 | TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10486 | ctxt->state = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10487 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10488 | return (ret); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10489 | } |
| 10490 | |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10491 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10492 | * xmlRelaxNGValidateDocument: |
| 10493 | * @ctxt: a Relax-NG validation context |
| 10494 | * @doc: the document |
| 10495 | * |
| 10496 | * Validate the given document |
| 10497 | * |
| 10498 | * Returns 0 if the validation succeeded or an error code. |
| 10499 | */ |
| 10500 | static int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10501 | xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) |
| 10502 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10503 | int ret; |
| 10504 | xmlRelaxNGPtr schema; |
| 10505 | xmlRelaxNGGrammarPtr grammar; |
| 10506 | xmlRelaxNGValidStatePtr state; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10507 | xmlNodePtr node; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10508 | |
| 10509 | if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10510 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10511 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10512 | ctxt->errNo = XML_RELAXNG_OK; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10513 | schema = ctxt->schema; |
| 10514 | grammar = schema->topgrammar; |
| 10515 | if (grammar == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10516 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 10517 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10518 | } |
| 10519 | state = xmlRelaxNGNewValidState(ctxt, NULL); |
| 10520 | ctxt->state = state; |
| 10521 | ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10522 | if ((ctxt->state != NULL) && (state->seq != NULL)) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10523 | state = ctxt->state; |
| 10524 | node = state->seq; |
| 10525 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 10526 | if (node != NULL) { |
| 10527 | if (ret != -1) { |
| 10528 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
| 10529 | ret = -1; |
| 10530 | } |
| 10531 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10532 | } else if (ctxt->states != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10533 | int i; |
| 10534 | int tmp = -1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10535 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10536 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 10537 | state = ctxt->states->tabState[i]; |
| 10538 | node = state->seq; |
| 10539 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 10540 | if (node == NULL) |
| 10541 | tmp = 0; |
| 10542 | xmlRelaxNGFreeValidState(ctxt, state); |
| 10543 | } |
| 10544 | if (tmp == -1) { |
| 10545 | if (ret != -1) { |
| 10546 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
| 10547 | ret = -1; |
| 10548 | } |
| 10549 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10550 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 10551 | if (ctxt->state != NULL) { |
| 10552 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10553 | ctxt->state = NULL; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 10554 | } |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10555 | if (ret != 0) |
| 10556 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 10557 | #ifdef DEBUG |
| 10558 | else if (ctxt->errNr != 0) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10559 | ctxt->error(ctxt->userData, |
| 10560 | "%d Extra error messages left on stack !\n", |
| 10561 | ctxt->errNr); |
| 10562 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 10563 | } |
| 10564 | #endif |
Daniel Veillard | f54cd53 | 2004-02-25 11:52:31 +0000 | [diff] [blame] | 10565 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10566 | if (ctxt->idref == 1) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10567 | xmlValidCtxt vctxt; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10568 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10569 | memset(&vctxt, 0, sizeof(xmlValidCtxt)); |
| 10570 | vctxt.valid = 1; |
| 10571 | vctxt.error = ctxt->error; |
| 10572 | vctxt.warning = ctxt->warning; |
| 10573 | vctxt.userData = ctxt->userData; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10574 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10575 | if (xmlValidateDocumentFinal(&vctxt, doc) != 1) |
| 10576 | ret = -1; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10577 | } |
Daniel Veillard | f54cd53 | 2004-02-25 11:52:31 +0000 | [diff] [blame] | 10578 | #endif /* LIBXML_VALID_ENABLED */ |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10579 | if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10580 | ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10581 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10582 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10583 | } |
| 10584 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10585 | /************************************************************************ |
| 10586 | * * |
| 10587 | * Validation interfaces * |
| 10588 | * * |
| 10589 | ************************************************************************/ |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10590 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10591 | /** |
| 10592 | * xmlRelaxNGNewValidCtxt: |
| 10593 | * @schema: a precompiled XML RelaxNGs |
| 10594 | * |
| 10595 | * Create an XML RelaxNGs validation context based on the given schema |
| 10596 | * |
| 10597 | * Returns the validation context or NULL in case of error |
| 10598 | */ |
| 10599 | xmlRelaxNGValidCtxtPtr |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10600 | xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema) |
| 10601 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10602 | xmlRelaxNGValidCtxtPtr ret; |
| 10603 | |
| 10604 | ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt)); |
| 10605 | if (ret == NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10606 | xmlRngVErrMemory(NULL, "building context\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10607 | return (NULL); |
| 10608 | } |
| 10609 | memset(ret, 0, sizeof(xmlRelaxNGValidCtxt)); |
| 10610 | ret->schema = schema; |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 10611 | ret->error = xmlGenericError; |
| 10612 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 10613 | ret->errNr = 0; |
| 10614 | ret->errMax = 0; |
| 10615 | ret->err = NULL; |
| 10616 | ret->errTab = NULL; |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 10617 | if (schema != NULL) |
| 10618 | ret->idref = schema->idref; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10619 | ret->states = NULL; |
| 10620 | ret->freeState = NULL; |
| 10621 | ret->freeStates = NULL; |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10622 | ret->errNo = XML_RELAXNG_OK; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10623 | return (ret); |
| 10624 | } |
| 10625 | |
| 10626 | /** |
| 10627 | * xmlRelaxNGFreeValidCtxt: |
| 10628 | * @ctxt: the schema validation context |
| 10629 | * |
| 10630 | * Free the resources associated to the schema validation context |
| 10631 | */ |
| 10632 | void |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10633 | xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt) |
| 10634 | { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10635 | int k; |
| 10636 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10637 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10638 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10639 | if (ctxt->states != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10640 | xmlRelaxNGFreeStates(NULL, ctxt->states); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10641 | if (ctxt->freeState != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10642 | for (k = 0; k < ctxt->freeState->nbState; k++) { |
| 10643 | xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]); |
| 10644 | } |
| 10645 | xmlRelaxNGFreeStates(NULL, ctxt->freeState); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10646 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10647 | if (ctxt->freeStates != NULL) { |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10648 | for (k = 0; k < ctxt->freeStatesNr; k++) { |
| 10649 | xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]); |
| 10650 | } |
| 10651 | xmlFree(ctxt->freeStates); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10652 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 10653 | if (ctxt->errTab != NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10654 | xmlFree(ctxt->errTab); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 10655 | if (ctxt->elemTab != NULL) { |
| 10656 | xmlRegExecCtxtPtr exec; |
| 10657 | |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10658 | exec = xmlRelaxNGElemPop(ctxt); |
| 10659 | while (exec != NULL) { |
| 10660 | xmlRegFreeExecCtxt(exec); |
| 10661 | exec = xmlRelaxNGElemPop(ctxt); |
| 10662 | } |
| 10663 | xmlFree(ctxt->elemTab); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 10664 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10665 | xmlFree(ctxt); |
| 10666 | } |
| 10667 | |
| 10668 | /** |
| 10669 | * xmlRelaxNGSetValidErrors: |
| 10670 | * @ctxt: a Relax-NG validation context |
| 10671 | * @err: the error function |
| 10672 | * @warn: the warning function |
| 10673 | * @ctx: the functions context |
| 10674 | * |
| 10675 | * Set the error and warning callback informations |
| 10676 | */ |
| 10677 | void |
| 10678 | xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10679 | xmlRelaxNGValidityErrorFunc err, |
| 10680 | xmlRelaxNGValidityWarningFunc warn, void *ctx) |
| 10681 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10682 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10683 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10684 | ctxt->error = err; |
| 10685 | ctxt->warning = warn; |
| 10686 | ctxt->userData = ctx; |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 10687 | ctxt->serror = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10688 | } |
| 10689 | |
| 10690 | /** |
Daniel Veillard | da0aa4c | 2005-07-13 23:07:49 +0000 | [diff] [blame] | 10691 | * xmlRelaxNGSetValidStructuredErrors: |
| 10692 | * @ctxt: a Relax-NG validation context |
| 10693 | * @serror: the structured error function |
| 10694 | * @ctx: the functions context |
| 10695 | * |
| 10696 | * Set the structured error callback |
| 10697 | */ |
| 10698 | void |
| 10699 | xmlRelaxNGSetValidStructuredErrors(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 10700 | xmlStructuredErrorFunc serror, void *ctx) |
Daniel Veillard | da0aa4c | 2005-07-13 23:07:49 +0000 | [diff] [blame] | 10701 | { |
| 10702 | if (ctxt == NULL) |
| 10703 | return; |
Daniel Veillard | b30ca31 | 2005-09-04 13:50:03 +0000 | [diff] [blame] | 10704 | ctxt->serror = serror; |
Daniel Veillard | da0aa4c | 2005-07-13 23:07:49 +0000 | [diff] [blame] | 10705 | ctxt->error = NULL; |
| 10706 | ctxt->warning = NULL; |
| 10707 | ctxt->userData = ctx; |
| 10708 | } |
| 10709 | |
| 10710 | /** |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 10711 | * xmlRelaxNGGetValidErrors: |
| 10712 | * @ctxt: a Relax-NG validation context |
| 10713 | * @err: the error function result |
| 10714 | * @warn: the warning function result |
| 10715 | * @ctx: the functions context result |
| 10716 | * |
| 10717 | * Get the error and warning callback informations |
| 10718 | * |
| 10719 | * Returns -1 in case of error and 0 otherwise |
| 10720 | */ |
| 10721 | int |
| 10722 | xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10723 | xmlRelaxNGValidityErrorFunc * err, |
| 10724 | xmlRelaxNGValidityWarningFunc * warn, void **ctx) |
| 10725 | { |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 10726 | if (ctxt == NULL) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10727 | return (-1); |
| 10728 | if (err != NULL) |
| 10729 | *err = ctxt->error; |
| 10730 | if (warn != NULL) |
| 10731 | *warn = ctxt->warning; |
| 10732 | if (ctx != NULL) |
| 10733 | *ctx = ctxt->userData; |
| 10734 | return (0); |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 10735 | } |
| 10736 | |
| 10737 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10738 | * xmlRelaxNGValidateDoc: |
| 10739 | * @ctxt: a Relax-NG validation context |
| 10740 | * @doc: a parsed document tree |
| 10741 | * |
| 10742 | * Validate a document tree in memory. |
| 10743 | * |
| 10744 | * Returns 0 if the document is valid, a positive error code |
| 10745 | * number otherwise and -1 in case of internal or API error. |
| 10746 | */ |
| 10747 | int |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10748 | xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) |
| 10749 | { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10750 | int ret; |
| 10751 | |
| 10752 | if ((ctxt == NULL) || (doc == NULL)) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10753 | return (-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10754 | |
| 10755 | ctxt->doc = doc; |
| 10756 | |
| 10757 | ret = xmlRelaxNGValidateDocument(ctxt, doc); |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 10758 | /* |
| 10759 | * TODO: build error codes |
| 10760 | */ |
| 10761 | if (ret == -1) |
Daniel Veillard | 4c00414 | 2003-10-07 11:33:24 +0000 | [diff] [blame] | 10762 | return (1); |
| 10763 | return (ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10764 | } |
| 10765 | |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 10766 | #define bottom_relaxng |
| 10767 | #include "elfgcchack.h" |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10768 | #endif /* LIBXML_SCHEMAS_ENABLED */ |