blob: 370e314ceac2c742c0cd7f323a5a73f816d9b309 [file] [log] [blame]
Daniel Veillard6eadf632003-01-23 18:29:16 +00001/*
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 Veillardd41f4f42003-01-29 21:07:52 +00009/**
10 * TODO:
Daniel Veillardf4b4f982003-02-13 11:02:08 +000011 * - add support for DTD compatibility spec
12 * http://www.oasis-open.org/committees/relax-ng/compatibility-20011203.html
Daniel Veillardac297932003-04-17 12:55:35 +000013 * - report better mem allocations pbms at runtime and abort immediately.
Daniel Veillardd41f4f42003-01-29 21:07:52 +000014 */
15
Daniel Veillard6eadf632003-01-23 18:29:16 +000016#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 Veillardc6e997c2003-01-27 12:35:42 +000034#include <libxml/xmlschemastypes.h>
Daniel Veillard6eadf632003-01-23 18:29:16 +000035
36/*
37 * The Relax-NG namespace
38 */
39static const xmlChar *xmlRelaxNGNs = (const xmlChar *)
40 "http://relaxng.org/ns/structure/1.0";
41
Daniel Veillard264cee62012-08-13 12:40:53 +080042#define IS_RELAXNG(node, typ) \
Daniel Veillard6eadf632003-01-23 18:29:16 +000043 ((node != NULL) && (node->ns != NULL) && \
Daniel Veillard264cee62012-08-13 12:40:53 +080044 (node->type == XML_ELEMENT_NODE) && \
45 (xmlStrEqual(node->name, (const xmlChar *) typ)) && \
Daniel Veillard6eadf632003-01-23 18:29:16 +000046 (xmlStrEqual(node->ns->href, xmlRelaxNGNs)))
47
48
Daniel Veillard23a47d62008-06-25 04:11:24 +000049#if 0
Daniel Veillard87254c82006-02-19 15:27:17 +000050#define DEBUG 1
Daniel Veillard4c004142003-10-07 11:33:24 +000051
Daniel Veillard87254c82006-02-19 15:27:17 +000052#define DEBUG_GRAMMAR 1
Daniel Veillard4c004142003-10-07 11:33:24 +000053
Daniel Veillard87254c82006-02-19 15:27:17 +000054#define DEBUG_CONTENT 1
Daniel Veillard4c004142003-10-07 11:33:24 +000055
Daniel Veillard87254c82006-02-19 15:27:17 +000056#define DEBUG_TYPE 1
Daniel Veillard4c004142003-10-07 11:33:24 +000057
Daniel Veillard87254c82006-02-19 15:27:17 +000058#define DEBUG_VALID 1
Daniel Veillard4c004142003-10-07 11:33:24 +000059
Daniel Veillard87254c82006-02-19 15:27:17 +000060#define DEBUG_INTERLEAVE 1
Daniel Veillard4c004142003-10-07 11:33:24 +000061
Daniel Veillard87254c82006-02-19 15:27:17 +000062#define DEBUG_LIST 1
Daniel Veillard4c004142003-10-07 11:33:24 +000063
Daniel Veillardf8e3db02012-09-11 13:26:36 +080064#define DEBUG_INCLUDE 1
Daniel Veillard4c004142003-10-07 11:33:24 +000065
Daniel Veillard87254c82006-02-19 15:27:17 +000066#define DEBUG_ERROR 1
Daniel Veillard4c004142003-10-07 11:33:24 +000067
Daniel Veillard87254c82006-02-19 15:27:17 +000068#define DEBUG_COMPILE 1
Daniel Veillard4c004142003-10-07 11:33:24 +000069
Daniel Veillard87254c82006-02-19 15:27:17 +000070#define DEBUG_PROGRESSIVE 1
71#endif
Daniel Veillard6eadf632003-01-23 18:29:16 +000072
Daniel Veillard5f1946a2003-03-31 16:38:16 +000073#define MAX_ERROR 5
74
Daniel Veillardf8e3db02012-09-11 13:26:36 +080075#define TODO \
Daniel Veillard6eadf632003-01-23 18:29:16 +000076 xmlGenericError(xmlGenericErrorContext, \
77 "Unimplemented block at %s:%d\n", \
78 __FILE__, __LINE__);
79
80typedef struct _xmlRelaxNGSchema xmlRelaxNGSchema;
81typedef xmlRelaxNGSchema *xmlRelaxNGSchemaPtr;
82
83typedef struct _xmlRelaxNGDefine xmlRelaxNGDefine;
84typedef xmlRelaxNGDefine *xmlRelaxNGDefinePtr;
85
Daniel Veillardd41f4f42003-01-29 21:07:52 +000086typedef struct _xmlRelaxNGDocument xmlRelaxNGDocument;
87typedef xmlRelaxNGDocument *xmlRelaxNGDocumentPtr;
88
Daniel Veillarda9d912d2003-02-01 17:43:10 +000089typedef struct _xmlRelaxNGInclude xmlRelaxNGInclude;
90typedef xmlRelaxNGInclude *xmlRelaxNGIncludePtr;
91
Daniel Veillard6eadf632003-01-23 18:29:16 +000092typedef enum {
Daniel Veillard4c004142003-10-07 11:33:24 +000093 XML_RELAXNG_COMBINE_UNDEFINED = 0, /* undefined */
94 XML_RELAXNG_COMBINE_CHOICE, /* choice */
95 XML_RELAXNG_COMBINE_INTERLEAVE /* interleave */
Daniel Veillard6eadf632003-01-23 18:29:16 +000096} xmlRelaxNGCombine;
97
Daniel Veillard4c5cf702003-02-21 15:40:34 +000098typedef enum {
99 XML_RELAXNG_CONTENT_ERROR = -1,
100 XML_RELAXNG_CONTENT_EMPTY = 0,
101 XML_RELAXNG_CONTENT_SIMPLE,
102 XML_RELAXNG_CONTENT_COMPLEX
103} xmlRelaxNGContentType;
104
Daniel Veillard6eadf632003-01-23 18:29:16 +0000105typedef struct _xmlRelaxNGGrammar xmlRelaxNGGrammar;
106typedef xmlRelaxNGGrammar *xmlRelaxNGGrammarPtr;
107
108struct _xmlRelaxNGGrammar {
Daniel Veillard4c004142003-10-07 11:33:24 +0000109 xmlRelaxNGGrammarPtr parent; /* the parent grammar if any */
110 xmlRelaxNGGrammarPtr children; /* the children grammar if any */
111 xmlRelaxNGGrammarPtr next; /* the next grammar if any */
112 xmlRelaxNGDefinePtr start; /* <start> content */
113 xmlRelaxNGCombine combine; /* the default combine value */
114 xmlRelaxNGDefinePtr startList; /* list of <start> definitions */
115 xmlHashTablePtr defs; /* define* */
116 xmlHashTablePtr refs; /* references */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000117};
118
119
Daniel Veillard6eadf632003-01-23 18:29:16 +0000120typedef enum {
Daniel Veillard4c004142003-10-07 11:33:24 +0000121 XML_RELAXNG_NOOP = -1, /* a no operation from simplification */
122 XML_RELAXNG_EMPTY = 0, /* an empty pattern */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000123 XML_RELAXNG_NOT_ALLOWED, /* not allowed top */
Daniel Veillard4c004142003-10-07 11:33:24 +0000124 XML_RELAXNG_EXCEPT, /* except present in nameclass defs */
125 XML_RELAXNG_TEXT, /* textual content */
126 XML_RELAXNG_ELEMENT, /* an element */
127 XML_RELAXNG_DATATYPE, /* extenal data type definition */
128 XML_RELAXNG_PARAM, /* extenal data type parameter */
129 XML_RELAXNG_VALUE, /* value from an extenal data type definition */
130 XML_RELAXNG_LIST, /* a list of patterns */
131 XML_RELAXNG_ATTRIBUTE, /* an attrbute following a pattern */
132 XML_RELAXNG_DEF, /* a definition */
133 XML_RELAXNG_REF, /* reference to a definition */
134 XML_RELAXNG_EXTERNALREF, /* reference to an external def */
135 XML_RELAXNG_PARENTREF, /* reference to a def in the parent grammar */
136 XML_RELAXNG_OPTIONAL, /* optional patterns */
137 XML_RELAXNG_ZEROORMORE, /* zero or more non empty patterns */
138 XML_RELAXNG_ONEORMORE, /* one or more non empty patterns */
139 XML_RELAXNG_CHOICE, /* a choice between non empty patterns */
140 XML_RELAXNG_GROUP, /* a pair/group of non empty patterns */
141 XML_RELAXNG_INTERLEAVE, /* interleaving choice of non-empty patterns */
142 XML_RELAXNG_START /* Used to keep track of starts on grammars */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000143} xmlRelaxNGType;
144
Daniel Veillard52b48c72003-04-13 19:53:42 +0000145#define IS_NULLABLE (1 << 0)
146#define IS_NOT_NULLABLE (1 << 1)
147#define IS_INDETERMINIST (1 << 2)
148#define IS_MIXED (1 << 3)
149#define IS_TRIABLE (1 << 4)
150#define IS_PROCESSED (1 << 5)
151#define IS_COMPILABLE (1 << 6)
152#define IS_NOT_COMPILABLE (1 << 7)
Daniel Veillardaa422d92009-09-24 11:31:48 +0200153#define IS_EXTERNAL_REF (1 << 8)
Daniel Veillard1564e6e2003-03-15 21:30:25 +0000154
Daniel Veillard6eadf632003-01-23 18:29:16 +0000155struct _xmlRelaxNGDefine {
Daniel Veillard4c004142003-10-07 11:33:24 +0000156 xmlRelaxNGType type; /* the type of definition */
157 xmlNodePtr node; /* the node in the source */
158 xmlChar *name; /* the element local name if present */
159 xmlChar *ns; /* the namespace local name if present */
160 xmlChar *value; /* value when available */
161 void *data; /* data lib or specific pointer */
162 xmlRelaxNGDefinePtr content; /* the expected content */
163 xmlRelaxNGDefinePtr parent; /* the parent definition, if any */
164 xmlRelaxNGDefinePtr next; /* list within grouping sequences */
165 xmlRelaxNGDefinePtr attrs; /* list of attributes for elements */
166 xmlRelaxNGDefinePtr nameClass; /* the nameClass definition if any */
167 xmlRelaxNGDefinePtr nextHash; /* next define in defs/refs hash tables */
168 short depth; /* used for the cycle detection */
169 short dflags; /* define related flags */
170 xmlRegexpPtr contModel; /* a compiled content model if available */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000171};
172
173/**
174 * _xmlRelaxNG:
175 *
176 * A RelaxNGs definition
177 */
178struct _xmlRelaxNG {
Daniel Veillard4c004142003-10-07 11:33:24 +0000179 void *_private; /* unused by the library for users or bindings */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000180 xmlRelaxNGGrammarPtr topgrammar;
181 xmlDocPtr doc;
182
Daniel Veillard4c004142003-10-07 11:33:24 +0000183 int idref; /* requires idref checking */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000184
Daniel Veillard4c004142003-10-07 11:33:24 +0000185 xmlHashTablePtr defs; /* define */
186 xmlHashTablePtr refs; /* references */
187 xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
188 xmlRelaxNGIncludePtr includes; /* all the includes loaded */
189 int defNr; /* number of defines used */
190 xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000191
Daniel Veillard6eadf632003-01-23 18:29:16 +0000192};
193
Daniel Veillard77648bb2003-02-20 15:03:22 +0000194#define XML_RELAXNG_IN_ATTRIBUTE (1 << 0)
195#define XML_RELAXNG_IN_ONEORMORE (1 << 1)
196#define XML_RELAXNG_IN_LIST (1 << 2)
197#define XML_RELAXNG_IN_DATAEXCEPT (1 << 3)
198#define XML_RELAXNG_IN_START (1 << 4)
199#define XML_RELAXNG_IN_OOMGROUP (1 << 5)
200#define XML_RELAXNG_IN_OOMINTERLEAVE (1 << 6)
201#define XML_RELAXNG_IN_EXTERNALREF (1 << 7)
Daniel Veillardc5312d72003-02-21 17:14:10 +0000202#define XML_RELAXNG_IN_ANYEXCEPT (1 << 8)
203#define XML_RELAXNG_IN_NSEXCEPT (1 << 9)
Daniel Veillard6eadf632003-01-23 18:29:16 +0000204
205struct _xmlRelaxNGParserCtxt {
Daniel Veillard4c004142003-10-07 11:33:24 +0000206 void *userData; /* user specific data block */
207 xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
208 xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */
Daniel Veillard659e71e2003-10-10 14:10:40 +0000209 xmlStructuredErrorFunc serror;
Daniel Veillard42f12e92003-03-07 18:32:59 +0000210 xmlRelaxNGValidErr err;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000211
Daniel Veillard4c004142003-10-07 11:33:24 +0000212 xmlRelaxNGPtr schema; /* The schema in use */
213 xmlRelaxNGGrammarPtr grammar; /* the current grammar */
214 xmlRelaxNGGrammarPtr parentgrammar; /* the parent grammar */
215 int flags; /* parser flags */
216 int nbErrors; /* number of errors at parse time */
217 int nbWarnings; /* number of warnings at parse time */
218 const xmlChar *define; /* the current define scope */
219 xmlRelaxNGDefinePtr def; /* the current define */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000220
Daniel Veillard4c004142003-10-07 11:33:24 +0000221 int nbInterleaves;
222 xmlHashTablePtr interleaves; /* keep track of all the interleaves */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000223
Daniel Veillard4c004142003-10-07 11:33:24 +0000224 xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
225 xmlRelaxNGIncludePtr includes; /* all the includes loaded */
226 xmlChar *URL;
227 xmlDocPtr document;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000228
Daniel Veillard4c004142003-10-07 11:33:24 +0000229 int defNr; /* number of defines used */
230 int defMax; /* number of defines aloocated */
231 xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */
Daniel Veillard419a7682003-02-03 23:22:49 +0000232
Daniel Veillard4c004142003-10-07 11:33:24 +0000233 const char *buffer;
234 int size;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000235
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000236 /* the document stack */
Daniel Veillard4c004142003-10-07 11:33:24 +0000237 xmlRelaxNGDocumentPtr doc; /* Current parsed external ref */
238 int docNr; /* Depth of the parsing stack */
239 int docMax; /* Max depth of the parsing stack */
240 xmlRelaxNGDocumentPtr *docTab; /* array of docs */
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000241
242 /* the include stack */
Daniel Veillard4c004142003-10-07 11:33:24 +0000243 xmlRelaxNGIncludePtr inc; /* Current parsed include */
244 int incNr; /* Depth of the include parsing stack */
245 int incMax; /* Max depth of the parsing stack */
246 xmlRelaxNGIncludePtr *incTab; /* array of incs */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000247
Daniel Veillard4c004142003-10-07 11:33:24 +0000248 int idref; /* requires idref checking */
Daniel Veillard52b48c72003-04-13 19:53:42 +0000249
250 /* used to compile content models */
Daniel Veillard4c004142003-10-07 11:33:24 +0000251 xmlAutomataPtr am; /* the automata */
252 xmlAutomataStatePtr state; /* used to build the automata */
Daniel Veillard03c2f0a2004-01-25 19:54:59 +0000253
254 int crng; /* compact syntax and other flags */
Daniel Veillard42595322004-11-08 10:52:06 +0000255 int freedoc; /* need to free the document */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000256};
257
258#define FLAGS_IGNORABLE 1
259#define FLAGS_NEGATIVE 2
Daniel Veillard249d7bb2003-03-19 21:02:29 +0000260#define FLAGS_MIXED_CONTENT 4
Daniel Veillardb30ca312005-09-04 13:50:03 +0000261#define FLAGS_NOERROR 8
Daniel Veillard6eadf632003-01-23 18:29:16 +0000262
263/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000264 * xmlRelaxNGInterleaveGroup:
265 *
266 * A RelaxNGs partition set associated to lists of definitions
267 */
268typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup;
269typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr;
270struct _xmlRelaxNGInterleaveGroup {
Daniel Veillard4c004142003-10-07 11:33:24 +0000271 xmlRelaxNGDefinePtr rule; /* the rule to satisfy */
272 xmlRelaxNGDefinePtr *defs; /* the array of element definitions */
273 xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000274};
275
Daniel Veillardbbb78b52003-03-21 01:24:45 +0000276#define IS_DETERMINIST 1
277#define IS_NEEDCHECK 2
Daniel Veillard4c004142003-10-07 11:33:24 +0000278
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000279/**
280 * xmlRelaxNGPartitions:
281 *
282 * A RelaxNGs partition associated to an interleave group
283 */
284typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition;
285typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr;
286struct _xmlRelaxNGPartition {
Daniel Veillard4c004142003-10-07 11:33:24 +0000287 int nbgroups; /* number of groups in the partitions */
288 xmlHashTablePtr triage; /* hash table used to direct nodes to the
289 * right group when possible */
290 int flags; /* determinist ? */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000291 xmlRelaxNGInterleaveGroupPtr *groups;
292};
293
294/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000295 * xmlRelaxNGValidState:
296 *
297 * A RelaxNGs validation state
298 */
299#define MAX_ATTR 20
300typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState;
301typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr;
302struct _xmlRelaxNGValidState {
Daniel Veillard4c004142003-10-07 11:33:24 +0000303 xmlNodePtr node; /* the current node */
304 xmlNodePtr seq; /* the sequence of children left to validate */
305 int nbAttrs; /* the number of attributes */
306 int maxAttrs; /* the size of attrs */
307 int nbAttrLeft; /* the number of attributes left to validate */
308 xmlChar *value; /* the value when operating on string */
309 xmlChar *endvalue; /* the end value when operating on string */
310 xmlAttrPtr *attrs; /* the array of attributes */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000311};
312
313/**
Daniel Veillardfd573f12003-03-16 17:52:32 +0000314 * xmlRelaxNGStates:
315 *
316 * A RelaxNGs container for validation state
317 */
318typedef struct _xmlRelaxNGStates xmlRelaxNGStates;
319typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr;
320struct _xmlRelaxNGStates {
Daniel Veillard4c004142003-10-07 11:33:24 +0000321 int nbState; /* the number of states */
322 int maxState; /* the size of the array */
Daniel Veillardfd573f12003-03-16 17:52:32 +0000323 xmlRelaxNGValidStatePtr *tabState;
324};
325
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000326#define ERROR_IS_DUP 1
Daniel Veillard4c004142003-10-07 11:33:24 +0000327
Daniel Veillardfd573f12003-03-16 17:52:32 +0000328/**
Daniel Veillard42f12e92003-03-07 18:32:59 +0000329 * xmlRelaxNGValidError:
330 *
331 * A RelaxNGs validation error
332 */
333typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError;
334typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr;
335struct _xmlRelaxNGValidError {
Daniel Veillard4c004142003-10-07 11:33:24 +0000336 xmlRelaxNGValidErr err; /* the error number */
337 int flags; /* flags */
338 xmlNodePtr node; /* the current node */
339 xmlNodePtr seq; /* the current child */
340 const xmlChar *arg1; /* first arg */
341 const xmlChar *arg2; /* second arg */
Daniel Veillard42f12e92003-03-07 18:32:59 +0000342};
343
344/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000345 * xmlRelaxNGValidCtxt:
346 *
347 * A RelaxNGs validation context
348 */
349
350struct _xmlRelaxNGValidCtxt {
Daniel Veillard4c004142003-10-07 11:33:24 +0000351 void *userData; /* user specific data block */
352 xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
353 xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */
Daniel Veillard659e71e2003-10-10 14:10:40 +0000354 xmlStructuredErrorFunc serror;
Daniel Veillard4c004142003-10-07 11:33:24 +0000355 int nbErrors; /* number of errors in validation */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000356
Daniel Veillard4c004142003-10-07 11:33:24 +0000357 xmlRelaxNGPtr schema; /* The schema in use */
358 xmlDocPtr doc; /* the document being validated */
359 int flags; /* validation flags */
360 int depth; /* validation depth */
361 int idref; /* requires idref checking */
362 int errNo; /* the first error found */
Daniel Veillard42f12e92003-03-07 18:32:59 +0000363
364 /*
365 * Errors accumulated in branches may have to be stacked to be
366 * provided back when it's sure they affect validation.
367 */
368 xmlRelaxNGValidErrorPtr err; /* Last error */
Daniel Veillard4c004142003-10-07 11:33:24 +0000369 int errNr; /* Depth of the error stack */
370 int errMax; /* Max depth of the error stack */
371 xmlRelaxNGValidErrorPtr errTab; /* stack of errors */
Daniel Veillard1564e6e2003-03-15 21:30:25 +0000372
Daniel Veillard4c004142003-10-07 11:33:24 +0000373 xmlRelaxNGValidStatePtr state; /* the current validation state */
374 xmlRelaxNGStatesPtr states; /* the accumulated state list */
Daniel Veillard798024a2003-03-19 10:36:09 +0000375
Daniel Veillard4c004142003-10-07 11:33:24 +0000376 xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */
377 int freeStatesNr;
378 int freeStatesMax;
379 xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */
Daniel Veillardf4e55762003-04-15 23:32:22 +0000380
381 /*
382 * This is used for "progressive" validation
383 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000384 xmlRegExecCtxtPtr elem; /* the current element regexp */
385 int elemNr; /* the number of element validated */
386 int elemMax; /* the max depth of elements */
387 xmlRegExecCtxtPtr *elemTab; /* the stack of regexp runtime */
388 int pstate; /* progressive state */
389 xmlNodePtr pnode; /* the current node */
390 xmlRelaxNGDefinePtr pdef; /* the non-streamable definition */
391 int perr; /* signal error in content model
392 * outside the regexp */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000393};
394
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000395/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000396 * xmlRelaxNGInclude:
397 *
398 * Structure associated to a RelaxNGs document element
399 */
400struct _xmlRelaxNGInclude {
Daniel Veillard4c004142003-10-07 11:33:24 +0000401 xmlRelaxNGIncludePtr next; /* keep a chain of includes */
402 xmlChar *href; /* the normalized href value */
403 xmlDocPtr doc; /* the associated XML document */
404 xmlRelaxNGDefinePtr content; /* the definitions */
405 xmlRelaxNGPtr schema; /* the schema */
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000406};
407
408/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000409 * xmlRelaxNGDocument:
410 *
411 * Structure associated to a RelaxNGs document element
412 */
413struct _xmlRelaxNGDocument {
Daniel Veillardc482e262003-02-26 14:48:48 +0000414 xmlRelaxNGDocumentPtr next; /* keep a chain of documents */
Daniel Veillard4c004142003-10-07 11:33:24 +0000415 xmlChar *href; /* the normalized href value */
416 xmlDocPtr doc; /* the associated XML document */
417 xmlRelaxNGDefinePtr content; /* the definitions */
418 xmlRelaxNGPtr schema; /* the schema */
Daniel Veillard81c51e12009-08-14 18:52:10 +0200419 int externalRef; /* 1 if an external ref */
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000420};
421
Daniel Veillard3ebc7d42003-02-24 17:17:58 +0000422
Daniel Veillard6eadf632003-01-23 18:29:16 +0000423/************************************************************************
Daniel Veillard4c004142003-10-07 11:33:24 +0000424 * *
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800425 * Some factorized error routines *
Daniel Veillard4c004142003-10-07 11:33:24 +0000426 * *
427 ************************************************************************/
428
429/**
430 * xmlRngPErrMemory:
431 * @ctxt: an Relax-NG parser context
432 * @extra: extra informations
433 *
434 * Handle a redefinition of attribute error
435 */
436static void
437xmlRngPErrMemory(xmlRelaxNGParserCtxtPtr ctxt, const char *extra)
438{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000439 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +0000440 xmlGenericErrorFunc channel = NULL;
441 void *data = NULL;
442
443 if (ctxt != NULL) {
Daniel Veillardb30ca312005-09-04 13:50:03 +0000444 if (ctxt->serror != NULL)
445 schannel = ctxt->serror;
446 else
447 channel = ctxt->error;
Daniel Veillard4c004142003-10-07 11:33:24 +0000448 data = ctxt->userData;
449 ctxt->nbErrors++;
450 }
451 if (extra)
Daniel Veillard659e71e2003-10-10 14:10:40 +0000452 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000453 NULL, NULL, XML_FROM_RELAXNGP,
454 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
455 NULL, NULL, 0, 0,
456 "Memory allocation failed : %s\n", extra);
457 else
Daniel Veillard659e71e2003-10-10 14:10:40 +0000458 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000459 NULL, NULL, XML_FROM_RELAXNGP,
460 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
461 NULL, NULL, 0, 0, "Memory allocation failed\n");
462}
463
464/**
465 * xmlRngVErrMemory:
466 * @ctxt: a Relax-NG validation context
467 * @extra: extra informations
468 *
469 * Handle a redefinition of attribute error
470 */
471static void
472xmlRngVErrMemory(xmlRelaxNGValidCtxtPtr ctxt, const char *extra)
473{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000474 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +0000475 xmlGenericErrorFunc channel = NULL;
476 void *data = NULL;
477
478 if (ctxt != NULL) {
Daniel Veillardb30ca312005-09-04 13:50:03 +0000479 if (ctxt->serror != NULL)
480 schannel = ctxt->serror;
481 else
482 channel = ctxt->error;
Daniel Veillard4c004142003-10-07 11:33:24 +0000483 data = ctxt->userData;
484 ctxt->nbErrors++;
485 }
486 if (extra)
Daniel Veillard659e71e2003-10-10 14:10:40 +0000487 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000488 NULL, NULL, XML_FROM_RELAXNGV,
489 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
490 NULL, NULL, 0, 0,
491 "Memory allocation failed : %s\n", extra);
492 else
Daniel Veillard659e71e2003-10-10 14:10:40 +0000493 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000494 NULL, NULL, XML_FROM_RELAXNGV,
495 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
496 NULL, NULL, 0, 0, "Memory allocation failed\n");
497}
498
499/**
500 * xmlRngPErr:
501 * @ctxt: a Relax-NG parser context
502 * @node: the node raising the error
503 * @error: the error code
504 * @msg: message
505 * @str1: extra info
506 * @str2: extra info
507 *
508 * Handle a Relax NG Parsing error
509 */
510static void
511xmlRngPErr(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, int error,
512 const char *msg, const xmlChar * str1, const xmlChar * str2)
513{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000514 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +0000515 xmlGenericErrorFunc channel = NULL;
516 void *data = NULL;
517
518 if (ctxt != NULL) {
Daniel Veillardb30ca312005-09-04 13:50:03 +0000519 if (ctxt->serror != NULL)
520 schannel = ctxt->serror;
521 else
522 channel = ctxt->error;
Daniel Veillard4c004142003-10-07 11:33:24 +0000523 data = ctxt->userData;
524 ctxt->nbErrors++;
525 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000526 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000527 NULL, node, XML_FROM_RELAXNGP,
528 error, XML_ERR_ERROR, NULL, 0,
529 (const char *) str1, (const char *) str2, NULL, 0, 0,
530 msg, str1, str2);
531}
532
533/**
534 * xmlRngVErr:
535 * @ctxt: a Relax-NG validation context
536 * @node: the node raising the error
537 * @error: the error code
538 * @msg: message
539 * @str1: extra info
540 * @str2: extra info
541 *
542 * Handle a Relax NG Validation error
543 */
544static void
545xmlRngVErr(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node, int error,
546 const char *msg, const xmlChar * str1, const xmlChar * str2)
547{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000548 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +0000549 xmlGenericErrorFunc channel = NULL;
550 void *data = NULL;
551
552 if (ctxt != NULL) {
Daniel Veillardb30ca312005-09-04 13:50:03 +0000553 if (ctxt->serror != NULL)
554 schannel = ctxt->serror;
555 else
556 channel = ctxt->error;
Daniel Veillard4c004142003-10-07 11:33:24 +0000557 data = ctxt->userData;
558 ctxt->nbErrors++;
559 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000560 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000561 NULL, node, XML_FROM_RELAXNGV,
562 error, XML_ERR_ERROR, NULL, 0,
563 (const char *) str1, (const char *) str2, NULL, 0, 0,
564 msg, str1, str2);
565}
566
567/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800568 * *
569 * Preliminary type checking interfaces *
570 * *
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000571 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +0000572
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000573/**
574 * xmlRelaxNGTypeHave:
575 * @data: data needed for the library
576 * @type: the type name
577 * @value: the value to check
578 *
579 * Function provided by a type library to check if a type is exported
580 *
581 * Returns 1 if yes, 0 if no and -1 in case of error.
582 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000583typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar * type);
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000584
585/**
586 * xmlRelaxNGTypeCheck:
587 * @data: data needed for the library
588 * @type: the type name
589 * @value: the value to check
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000590 * @result: place to store the result if needed
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000591 *
592 * Function provided by a type library to check if a value match a type
593 *
594 * Returns 1 if yes, 0 if no and -1 in case of error.
595 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000596typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar * type,
597 const xmlChar * value, void **result,
598 xmlNodePtr node);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000599
600/**
601 * xmlRelaxNGFacetCheck:
602 * @data: data needed for the library
603 * @type: the type name
604 * @facet: the facet name
605 * @val: the facet value
606 * @strval: the string value
607 * @value: the value to check
608 *
609 * Function provided by a type library to check a value facet
610 *
611 * Returns 1 if yes, 0 if no and -1 in case of error.
612 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000613typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar * type,
614 const xmlChar * facet,
615 const xmlChar * val,
616 const xmlChar * strval, void *value);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000617
618/**
619 * xmlRelaxNGTypeFree:
620 * @data: data needed for the library
621 * @result: the value to free
622 *
623 * Function provided by a type library to free a returned result
624 */
625typedef void (*xmlRelaxNGTypeFree) (void *data, void *result);
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000626
627/**
628 * xmlRelaxNGTypeCompare:
629 * @data: data needed for the library
630 * @type: the type name
631 * @value1: the first value
632 * @value2: the second value
633 *
634 * Function provided by a type library to compare two values accordingly
635 * to a type.
636 *
637 * Returns 1 if yes, 0 if no and -1 in case of error.
638 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000639typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar * type,
640 const xmlChar * value1,
641 xmlNodePtr ctxt1,
642 void *comp1,
643 const xmlChar * value2,
644 xmlNodePtr ctxt2);
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000645typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary;
646typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr;
647struct _xmlRelaxNGTypeLibrary {
Daniel Veillard4c004142003-10-07 11:33:24 +0000648 const xmlChar *namespace; /* the datatypeLibrary value */
649 void *data; /* data needed for the library */
650 xmlRelaxNGTypeHave have; /* the export function */
651 xmlRelaxNGTypeCheck check; /* the checking function */
652 xmlRelaxNGTypeCompare comp; /* the compare function */
653 xmlRelaxNGFacetCheck facet; /* the facet check function */
654 xmlRelaxNGTypeFree freef; /* the freeing function */
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000655};
656
657/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800658 * *
659 * Allocation functions *
660 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +0000661 ************************************************************************/
Daniel Veillard6eadf632003-01-23 18:29:16 +0000662static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar);
663static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define);
Daniel Veillard4c004142003-10-07 11:33:24 +0000664static void xmlRelaxNGNormExtSpace(xmlChar * value);
Daniel Veillardc482e262003-02-26 14:48:48 +0000665static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema);
Daniel Veillard4c004142003-10-07 11:33:24 +0000666static int xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt
667 ATTRIBUTE_UNUSED,
668 xmlRelaxNGValidStatePtr state1,
669 xmlRelaxNGValidStatePtr state2);
Daniel Veillard798024a2003-03-19 10:36:09 +0000670static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +0000671 xmlRelaxNGValidStatePtr state);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000672
673/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000674 * xmlRelaxNGFreeDocument:
675 * @docu: a document structure
676 *
677 * Deallocate a RelaxNG document structure.
678 */
679static void
680xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu)
681{
682 if (docu == NULL)
683 return;
684
685 if (docu->href != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000686 xmlFree(docu->href);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000687 if (docu->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000688 xmlFreeDoc(docu->doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000689 if (docu->schema != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000690 xmlRelaxNGFreeInnerSchema(docu->schema);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000691 xmlFree(docu);
692}
693
694/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000695 * xmlRelaxNGFreeDocumentList:
696 * @docu: a list of document structure
697 *
698 * Deallocate a RelaxNG document structures.
699 */
700static void
701xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu)
702{
703 xmlRelaxNGDocumentPtr next;
Daniel Veillard4c004142003-10-07 11:33:24 +0000704
Daniel Veillardc482e262003-02-26 14:48:48 +0000705 while (docu != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000706 next = docu->next;
707 xmlRelaxNGFreeDocument(docu);
708 docu = next;
Daniel Veillardc482e262003-02-26 14:48:48 +0000709 }
710}
711
712/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000713 * xmlRelaxNGFreeInclude:
714 * @incl: a include structure
715 *
716 * Deallocate a RelaxNG include structure.
717 */
718static void
719xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl)
720{
721 if (incl == NULL)
722 return;
723
724 if (incl->href != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000725 xmlFree(incl->href);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000726 if (incl->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000727 xmlFreeDoc(incl->doc);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000728 if (incl->schema != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000729 xmlRelaxNGFree(incl->schema);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000730 xmlFree(incl);
731}
732
733/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000734 * xmlRelaxNGFreeIncludeList:
735 * @incl: a include structure list
736 *
737 * Deallocate a RelaxNG include structure.
738 */
739static void
740xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl)
741{
742 xmlRelaxNGIncludePtr next;
Daniel Veillard4c004142003-10-07 11:33:24 +0000743
Daniel Veillardc482e262003-02-26 14:48:48 +0000744 while (incl != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000745 next = incl->next;
746 xmlRelaxNGFreeInclude(incl);
747 incl = next;
Daniel Veillardc482e262003-02-26 14:48:48 +0000748 }
749}
750
751/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000752 * xmlRelaxNGNewRelaxNG:
753 * @ctxt: a Relax-NG validation context (optional)
754 *
755 * Allocate a new RelaxNG structure.
756 *
757 * Returns the newly allocated structure or NULL in case or error
758 */
759static xmlRelaxNGPtr
760xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt)
761{
762 xmlRelaxNGPtr ret;
763
764 ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG));
765 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000766 xmlRngPErrMemory(ctxt, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000767 return (NULL);
768 }
769 memset(ret, 0, sizeof(xmlRelaxNG));
770
771 return (ret);
772}
773
774/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000775 * xmlRelaxNGFreeInnerSchema:
776 * @schema: a schema structure
777 *
778 * Deallocate a RelaxNG schema structure.
779 */
780static void
781xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema)
782{
783 if (schema == NULL)
784 return;
785
786 if (schema->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000787 xmlFreeDoc(schema->doc);
Daniel Veillardc482e262003-02-26 14:48:48 +0000788 if (schema->defTab != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000789 int i;
Daniel Veillardc482e262003-02-26 14:48:48 +0000790
Daniel Veillard4c004142003-10-07 11:33:24 +0000791 for (i = 0; i < schema->defNr; i++)
792 xmlRelaxNGFreeDefine(schema->defTab[i]);
793 xmlFree(schema->defTab);
Daniel Veillardc482e262003-02-26 14:48:48 +0000794 }
795
796 xmlFree(schema);
797}
798
799/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000800 * xmlRelaxNGFree:
801 * @schema: a schema structure
802 *
803 * Deallocate a RelaxNG structure.
804 */
805void
806xmlRelaxNGFree(xmlRelaxNGPtr schema)
807{
808 if (schema == NULL)
809 return;
810
Daniel Veillard6eadf632003-01-23 18:29:16 +0000811 if (schema->topgrammar != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000812 xmlRelaxNGFreeGrammar(schema->topgrammar);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000813 if (schema->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000814 xmlFreeDoc(schema->doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000815 if (schema->documents != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000816 xmlRelaxNGFreeDocumentList(schema->documents);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000817 if (schema->includes != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000818 xmlRelaxNGFreeIncludeList(schema->includes);
Daniel Veillard419a7682003-02-03 23:22:49 +0000819 if (schema->defTab != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000820 int i;
Daniel Veillard419a7682003-02-03 23:22:49 +0000821
Daniel Veillard4c004142003-10-07 11:33:24 +0000822 for (i = 0; i < schema->defNr; i++)
823 xmlRelaxNGFreeDefine(schema->defTab[i]);
824 xmlFree(schema->defTab);
Daniel Veillard419a7682003-02-03 23:22:49 +0000825 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000826
827 xmlFree(schema);
828}
829
830/**
831 * xmlRelaxNGNewGrammar:
832 * @ctxt: a Relax-NG validation context (optional)
833 *
834 * Allocate a new RelaxNG grammar.
835 *
836 * Returns the newly allocated structure or NULL in case or error
837 */
838static xmlRelaxNGGrammarPtr
839xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt)
840{
841 xmlRelaxNGGrammarPtr ret;
842
843 ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar));
844 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000845 xmlRngPErrMemory(ctxt, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000846 return (NULL);
847 }
848 memset(ret, 0, sizeof(xmlRelaxNGGrammar));
849
850 return (ret);
851}
852
853/**
854 * xmlRelaxNGFreeGrammar:
855 * @grammar: a grammar structure
856 *
857 * Deallocate a RelaxNG grammar structure.
858 */
859static void
860xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar)
861{
862 if (grammar == NULL)
863 return;
864
Daniel Veillardc482e262003-02-26 14:48:48 +0000865 if (grammar->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000866 xmlRelaxNGFreeGrammar(grammar->children);
Daniel Veillardc482e262003-02-26 14:48:48 +0000867 }
Daniel Veillard419a7682003-02-03 23:22:49 +0000868 if (grammar->next != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000869 xmlRelaxNGFreeGrammar(grammar->next);
Daniel Veillard419a7682003-02-03 23:22:49 +0000870 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000871 if (grammar->refs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000872 xmlHashFree(grammar->refs, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000873 }
874 if (grammar->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000875 xmlHashFree(grammar->defs, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000876 }
877
878 xmlFree(grammar);
879}
880
881/**
882 * xmlRelaxNGNewDefine:
883 * @ctxt: a Relax-NG validation context
884 * @node: the node in the input document.
885 *
886 * Allocate a new RelaxNG define.
887 *
888 * Returns the newly allocated structure or NULL in case or error
889 */
890static xmlRelaxNGDefinePtr
Daniel Veillardfd573f12003-03-16 17:52:32 +0000891xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
Daniel Veillard6eadf632003-01-23 18:29:16 +0000892{
893 xmlRelaxNGDefinePtr ret;
894
Daniel Veillard419a7682003-02-03 23:22:49 +0000895 if (ctxt->defMax == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000896 ctxt->defMax = 16;
897 ctxt->defNr = 0;
898 ctxt->defTab = (xmlRelaxNGDefinePtr *)
899 xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr));
900 if (ctxt->defTab == NULL) {
901 xmlRngPErrMemory(ctxt, "allocating define\n");
902 return (NULL);
903 }
Daniel Veillard419a7682003-02-03 23:22:49 +0000904 } else if (ctxt->defMax <= ctxt->defNr) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000905 xmlRelaxNGDefinePtr *tmp;
906
907 ctxt->defMax *= 2;
908 tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab,
909 ctxt->defMax *
910 sizeof
911 (xmlRelaxNGDefinePtr));
912 if (tmp == NULL) {
913 xmlRngPErrMemory(ctxt, "allocating define\n");
914 return (NULL);
915 }
916 ctxt->defTab = tmp;
Daniel Veillard419a7682003-02-03 23:22:49 +0000917 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000918 ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine));
919 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000920 xmlRngPErrMemory(ctxt, "allocating define\n");
921 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000922 }
923 memset(ret, 0, sizeof(xmlRelaxNGDefine));
Daniel Veillard419a7682003-02-03 23:22:49 +0000924 ctxt->defTab[ctxt->defNr++] = ret;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000925 ret->node = node;
Daniel Veillardd4310742003-02-18 21:12:46 +0000926 ret->depth = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000927 return (ret);
928}
929
930/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000931 * xmlRelaxNGFreePartition:
932 * @partitions: a partition set structure
933 *
934 * Deallocate RelaxNG partition set structures.
935 */
936static void
Daniel Veillard4c004142003-10-07 11:33:24 +0000937xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions)
938{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000939 xmlRelaxNGInterleaveGroupPtr group;
940 int j;
941
942 if (partitions != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000943 if (partitions->groups != NULL) {
944 for (j = 0; j < partitions->nbgroups; j++) {
945 group = partitions->groups[j];
946 if (group != NULL) {
947 if (group->defs != NULL)
948 xmlFree(group->defs);
949 if (group->attrs != NULL)
950 xmlFree(group->attrs);
951 xmlFree(group);
952 }
953 }
954 xmlFree(partitions->groups);
955 }
956 if (partitions->triage != NULL) {
957 xmlHashFree(partitions->triage, NULL);
958 }
959 xmlFree(partitions);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000960 }
961}
Daniel Veillard4c004142003-10-07 11:33:24 +0000962
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000963/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000964 * xmlRelaxNGFreeDefine:
965 * @define: a define structure
966 *
967 * Deallocate a RelaxNG define structure.
968 */
969static void
970xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define)
971{
972 if (define == NULL)
973 return;
974
Daniel Veillard4c004142003-10-07 11:33:24 +0000975 if ((define->type == XML_RELAXNG_VALUE) && (define->attrs != NULL)) {
976 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000977
Daniel Veillard4c004142003-10-07 11:33:24 +0000978 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
979 if ((lib != NULL) && (lib->freef != NULL))
980 lib->freef(lib->data, (void *) define->attrs);
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000981 }
Daniel Veillard4c004142003-10-07 11:33:24 +0000982 if ((define->data != NULL) && (define->type == XML_RELAXNG_INTERLEAVE))
983 xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data);
984 if ((define->data != NULL) && (define->type == XML_RELAXNG_CHOICE))
985 xmlHashFree((xmlHashTablePtr) define->data, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000986 if (define->name != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000987 xmlFree(define->name);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000988 if (define->ns != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000989 xmlFree(define->ns);
Daniel Veillardedc91922003-01-26 00:52:04 +0000990 if (define->value != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000991 xmlFree(define->value);
Daniel Veillard52b48c72003-04-13 19:53:42 +0000992 if (define->contModel != NULL)
993 xmlRegFreeRegexp(define->contModel);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000994 xmlFree(define);
995}
996
997/**
Daniel Veillardfd573f12003-03-16 17:52:32 +0000998 * xmlRelaxNGNewStates:
999 * @ctxt: a Relax-NG validation context
1000 * @size: the default size for the container
1001 *
1002 * Allocate a new RelaxNG validation state container
Daniel Veillardfd573f12003-03-16 17:52:32 +00001003 *
1004 * Returns the newly allocated structure or NULL in case or error
1005 */
1006static xmlRelaxNGStatesPtr
1007xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size)
1008{
1009 xmlRelaxNGStatesPtr ret;
1010
Daniel Veillard798024a2003-03-19 10:36:09 +00001011 if ((ctxt != NULL) &&
Daniel Veillard9fcd4622009-08-14 16:16:31 +02001012 (ctxt->freeStates != NULL) && (ctxt->freeStatesNr > 0)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001013 ctxt->freeStatesNr--;
1014 ret = ctxt->freeStates[ctxt->freeStatesNr];
1015 ret->nbState = 0;
1016 return (ret);
Daniel Veillard798024a2003-03-19 10:36:09 +00001017 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001018 if (size < 16)
1019 size = 16;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001020
1021 ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) +
Daniel Veillard4c004142003-10-07 11:33:24 +00001022 (size -
1023 1) *
1024 sizeof(xmlRelaxNGValidStatePtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001025 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001026 xmlRngVErrMemory(ctxt, "allocating states\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00001027 return (NULL);
1028 }
1029 ret->nbState = 0;
1030 ret->maxState = size;
Daniel Veillard4c004142003-10-07 11:33:24 +00001031 ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc((size) *
1032 sizeof
1033 (xmlRelaxNGValidStatePtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001034 if (ret->tabState == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001035 xmlRngVErrMemory(ctxt, "allocating states\n");
1036 xmlFree(ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001037 return (NULL);
1038 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001039 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001040}
1041
1042/**
Daniel Veillard798024a2003-03-19 10:36:09 +00001043 * xmlRelaxNGAddStateUniq:
1044 * @ctxt: a Relax-NG validation context
1045 * @states: the states container
1046 * @state: the validation state
1047 *
1048 * Add a RelaxNG validation state to the container without checking
1049 * for unicity.
1050 *
1051 * Return 1 in case of success and 0 if this is a duplicate and -1 on error
1052 */
1053static int
1054xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001055 xmlRelaxNGStatesPtr states,
1056 xmlRelaxNGValidStatePtr state)
Daniel Veillard798024a2003-03-19 10:36:09 +00001057{
1058 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001059 return (-1);
Daniel Veillard798024a2003-03-19 10:36:09 +00001060 }
1061 if (states->nbState >= states->maxState) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001062 xmlRelaxNGValidStatePtr *tmp;
1063 int size;
Daniel Veillard798024a2003-03-19 10:36:09 +00001064
Daniel Veillard4c004142003-10-07 11:33:24 +00001065 size = states->maxState * 2;
1066 tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
1067 (size) *
1068 sizeof
1069 (xmlRelaxNGValidStatePtr));
Daniel Veillard798024a2003-03-19 10:36:09 +00001070 if (tmp == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001071 xmlRngVErrMemory(ctxt, "adding states\n");
1072 return (-1);
1073 }
1074 states->tabState = tmp;
1075 states->maxState = size;
Daniel Veillard798024a2003-03-19 10:36:09 +00001076 }
1077 states->tabState[states->nbState++] = state;
Daniel Veillard4c004142003-10-07 11:33:24 +00001078 return (1);
Daniel Veillard798024a2003-03-19 10:36:09 +00001079}
1080
1081/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00001082 * xmlRelaxNGAddState:
1083 * @ctxt: a Relax-NG validation context
1084 * @states: the states container
1085 * @state: the validation state
1086 *
1087 * Add a RelaxNG validation state to the container
1088 *
1089 * Return 1 in case of success and 0 if this is a duplicate and -1 on error
1090 */
1091static int
Daniel Veillard4c004142003-10-07 11:33:24 +00001092xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt,
1093 xmlRelaxNGStatesPtr states,
1094 xmlRelaxNGValidStatePtr state)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001095{
1096 int i;
1097
1098 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001099 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001100 }
1101 if (states->nbState >= states->maxState) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001102 xmlRelaxNGValidStatePtr *tmp;
1103 int size;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001104
Daniel Veillard4c004142003-10-07 11:33:24 +00001105 size = states->maxState * 2;
1106 tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
1107 (size) *
1108 sizeof
1109 (xmlRelaxNGValidStatePtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001110 if (tmp == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001111 xmlRngVErrMemory(ctxt, "adding states\n");
1112 return (-1);
1113 }
1114 states->tabState = tmp;
1115 states->maxState = size;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001116 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001117 for (i = 0; i < states->nbState; i++) {
1118 if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) {
1119 xmlRelaxNGFreeValidState(ctxt, state);
1120 return (0);
1121 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00001122 }
1123 states->tabState[states->nbState++] = state;
Daniel Veillard4c004142003-10-07 11:33:24 +00001124 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001125}
1126
1127/**
1128 * xmlRelaxNGFreeStates:
1129 * @ctxt: a Relax-NG validation context
1130 * @states: teh container
1131 *
1132 * Free a RelaxNG validation state container
Daniel Veillardfd573f12003-03-16 17:52:32 +00001133 */
1134static void
Daniel Veillard798024a2003-03-19 10:36:09 +00001135xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001136 xmlRelaxNGStatesPtr states)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001137{
Daniel Veillard798024a2003-03-19 10:36:09 +00001138 if (states == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00001139 return;
Daniel Veillard798024a2003-03-19 10:36:09 +00001140 if ((ctxt != NULL) && (ctxt->freeStates == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001141 ctxt->freeStatesMax = 40;
1142 ctxt->freeStatesNr = 0;
1143 ctxt->freeStates = (xmlRelaxNGStatesPtr *)
1144 xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr));
1145 if (ctxt->freeStates == NULL) {
1146 xmlRngVErrMemory(ctxt, "storing states\n");
1147 }
1148 } else if ((ctxt != NULL)
1149 && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) {
1150 xmlRelaxNGStatesPtr *tmp;
Daniel Veillard798024a2003-03-19 10:36:09 +00001151
Daniel Veillard4c004142003-10-07 11:33:24 +00001152 tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates,
1153 2 * ctxt->freeStatesMax *
1154 sizeof
1155 (xmlRelaxNGStatesPtr));
1156 if (tmp == NULL) {
1157 xmlRngVErrMemory(ctxt, "storing states\n");
1158 xmlFree(states->tabState);
1159 xmlFree(states);
1160 return;
1161 }
1162 ctxt->freeStates = tmp;
1163 ctxt->freeStatesMax *= 2;
Daniel Veillard798024a2003-03-19 10:36:09 +00001164 }
Daniel Veillard14b56432006-03-09 18:41:40 +00001165 if ((ctxt == NULL) || (ctxt->freeStates == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001166 xmlFree(states->tabState);
1167 xmlFree(states);
Daniel Veillard798024a2003-03-19 10:36:09 +00001168 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001169 ctxt->freeStates[ctxt->freeStatesNr++] = states;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001170 }
1171}
1172
1173/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00001174 * xmlRelaxNGNewValidState:
1175 * @ctxt: a Relax-NG validation context
1176 * @node: the current node or NULL for the document
1177 *
1178 * Allocate a new RelaxNG validation state
1179 *
1180 * Returns the newly allocated structure or NULL in case or error
1181 */
1182static xmlRelaxNGValidStatePtr
1183xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node)
1184{
1185 xmlRelaxNGValidStatePtr ret;
1186 xmlAttrPtr attr;
1187 xmlAttrPtr attrs[MAX_ATTR];
1188 int nbAttrs = 0;
1189 xmlNodePtr root = NULL;
1190
1191 if (node == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001192 root = xmlDocGetRootElement(ctxt->doc);
1193 if (root == NULL)
1194 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00001195 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001196 attr = node->properties;
1197 while (attr != NULL) {
1198 if (nbAttrs < MAX_ATTR)
1199 attrs[nbAttrs++] = attr;
1200 else
1201 nbAttrs++;
1202 attr = attr->next;
1203 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00001204 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001205 if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) {
1206 ctxt->freeState->nbState--;
1207 ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
Daniel Veillard798024a2003-03-19 10:36:09 +00001208 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001209 ret =
1210 (xmlRelaxNGValidStatePtr)
1211 xmlMalloc(sizeof(xmlRelaxNGValidState));
1212 if (ret == NULL) {
1213 xmlRngVErrMemory(ctxt, "allocating states\n");
1214 return (NULL);
1215 }
1216 memset(ret, 0, sizeof(xmlRelaxNGValidState));
Daniel Veillard6eadf632003-01-23 18:29:16 +00001217 }
Daniel Veillarde5b110b2003-02-04 14:43:39 +00001218 ret->value = NULL;
1219 ret->endvalue = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001220 if (node == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001221 ret->node = (xmlNodePtr) ctxt->doc;
1222 ret->seq = root;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001223 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001224 ret->node = node;
1225 ret->seq = node->children;
Daniel Veillard798024a2003-03-19 10:36:09 +00001226 }
1227 ret->nbAttrs = 0;
1228 if (nbAttrs > 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001229 if (ret->attrs == NULL) {
1230 if (nbAttrs < 4)
1231 ret->maxAttrs = 4;
1232 else
1233 ret->maxAttrs = nbAttrs;
1234 ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
1235 sizeof(xmlAttrPtr));
1236 if (ret->attrs == NULL) {
1237 xmlRngVErrMemory(ctxt, "allocating states\n");
1238 return (ret);
1239 }
1240 } else if (ret->maxAttrs < nbAttrs) {
1241 xmlAttrPtr *tmp;
Daniel Veillard798024a2003-03-19 10:36:09 +00001242
Daniel Veillard4c004142003-10-07 11:33:24 +00001243 tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs *
1244 sizeof(xmlAttrPtr));
1245 if (tmp == NULL) {
1246 xmlRngVErrMemory(ctxt, "allocating states\n");
1247 return (ret);
1248 }
1249 ret->attrs = tmp;
1250 ret->maxAttrs = nbAttrs;
1251 }
1252 ret->nbAttrs = nbAttrs;
1253 if (nbAttrs < MAX_ATTR) {
1254 memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs);
1255 } else {
1256 attr = node->properties;
1257 nbAttrs = 0;
1258 while (attr != NULL) {
1259 ret->attrs[nbAttrs++] = attr;
1260 attr = attr->next;
1261 }
1262 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00001263 }
Daniel Veillard1ed7f362003-02-03 10:57:45 +00001264 ret->nbAttrLeft = ret->nbAttrs;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001265 return (ret);
1266}
1267
1268/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00001269 * xmlRelaxNGCopyValidState:
1270 * @ctxt: a Relax-NG validation context
1271 * @state: a validation state
1272 *
1273 * Copy the validation state
1274 *
1275 * Returns the newly allocated structure or NULL in case or error
1276 */
1277static xmlRelaxNGValidStatePtr
1278xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001279 xmlRelaxNGValidStatePtr state)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001280{
1281 xmlRelaxNGValidStatePtr ret;
Daniel Veillard798024a2003-03-19 10:36:09 +00001282 unsigned int maxAttrs;
1283 xmlAttrPtr *attrs;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001284
1285 if (state == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00001286 return (NULL);
1287 if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) {
1288 ctxt->freeState->nbState--;
1289 ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
Daniel Veillard798024a2003-03-19 10:36:09 +00001290 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001291 ret =
1292 (xmlRelaxNGValidStatePtr)
1293 xmlMalloc(sizeof(xmlRelaxNGValidState));
1294 if (ret == NULL) {
1295 xmlRngVErrMemory(ctxt, "allocating states\n");
1296 return (NULL);
1297 }
1298 memset(ret, 0, sizeof(xmlRelaxNGValidState));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001299 }
Daniel Veillard798024a2003-03-19 10:36:09 +00001300 attrs = ret->attrs;
1301 maxAttrs = ret->maxAttrs;
1302 memcpy(ret, state, sizeof(xmlRelaxNGValidState));
1303 ret->attrs = attrs;
1304 ret->maxAttrs = maxAttrs;
1305 if (state->nbAttrs > 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001306 if (ret->attrs == NULL) {
1307 ret->maxAttrs = state->maxAttrs;
1308 ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
1309 sizeof(xmlAttrPtr));
1310 if (ret->attrs == NULL) {
1311 xmlRngVErrMemory(ctxt, "allocating states\n");
1312 ret->nbAttrs = 0;
1313 return (ret);
1314 }
1315 } else if (ret->maxAttrs < state->nbAttrs) {
1316 xmlAttrPtr *tmp;
Daniel Veillard798024a2003-03-19 10:36:09 +00001317
Daniel Veillard4c004142003-10-07 11:33:24 +00001318 tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs *
1319 sizeof(xmlAttrPtr));
1320 if (tmp == NULL) {
1321 xmlRngVErrMemory(ctxt, "allocating states\n");
1322 ret->nbAttrs = 0;
1323 return (ret);
1324 }
1325 ret->maxAttrs = state->maxAttrs;
1326 ret->attrs = tmp;
1327 }
1328 memcpy(ret->attrs, state->attrs,
1329 state->nbAttrs * sizeof(xmlAttrPtr));
Daniel Veillard798024a2003-03-19 10:36:09 +00001330 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001331 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001332}
1333
1334/**
1335 * xmlRelaxNGEqualValidState:
1336 * @ctxt: a Relax-NG validation context
1337 * @state1: a validation state
1338 * @state2: a validation state
1339 *
1340 * Compare the validation states for equality
1341 *
1342 * Returns 1 if equald, 0 otherwise
1343 */
1344static int
1345xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00001346 xmlRelaxNGValidStatePtr state1,
1347 xmlRelaxNGValidStatePtr state2)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001348{
1349 int i;
1350
1351 if ((state1 == NULL) || (state2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00001352 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001353 if (state1 == state2)
Daniel Veillard4c004142003-10-07 11:33:24 +00001354 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001355 if (state1->node != state2->node)
Daniel Veillard4c004142003-10-07 11:33:24 +00001356 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001357 if (state1->seq != state2->seq)
Daniel Veillard4c004142003-10-07 11:33:24 +00001358 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001359 if (state1->nbAttrLeft != state2->nbAttrLeft)
Daniel Veillard4c004142003-10-07 11:33:24 +00001360 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001361 if (state1->nbAttrs != state2->nbAttrs)
Daniel Veillard4c004142003-10-07 11:33:24 +00001362 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001363 if (state1->endvalue != state2->endvalue)
Daniel Veillard4c004142003-10-07 11:33:24 +00001364 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001365 if ((state1->value != state2->value) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00001366 (!xmlStrEqual(state1->value, state2->value)))
1367 return (0);
1368 for (i = 0; i < state1->nbAttrs; i++) {
1369 if (state1->attrs[i] != state2->attrs[i])
1370 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001371 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001372 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001373}
1374
1375/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00001376 * xmlRelaxNGFreeValidState:
1377 * @state: a validation state structure
1378 *
1379 * Deallocate a RelaxNG validation state structure.
1380 */
1381static void
Daniel Veillard798024a2003-03-19 10:36:09 +00001382xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001383 xmlRelaxNGValidStatePtr state)
Daniel Veillard6eadf632003-01-23 18:29:16 +00001384{
1385 if (state == NULL)
1386 return;
1387
Daniel Veillard798024a2003-03-19 10:36:09 +00001388 if ((ctxt != NULL) && (ctxt->freeState == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001389 ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40);
Daniel Veillard798024a2003-03-19 10:36:09 +00001390 }
1391 if ((ctxt == NULL) || (ctxt->freeState == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001392 if (state->attrs != NULL)
1393 xmlFree(state->attrs);
1394 xmlFree(state);
Daniel Veillard798024a2003-03-19 10:36:09 +00001395 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001396 xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state);
Daniel Veillard798024a2003-03-19 10:36:09 +00001397 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00001398}
1399
1400/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001401 * *
1402 * Semi internal functions *
1403 * *
Daniel Veillard03c2f0a2004-01-25 19:54:59 +00001404 ************************************************************************/
1405
1406/**
1407 * xmlRelaxParserSetFlag:
1408 * @ctxt: a RelaxNG parser context
1409 * @flags: a set of flags values
1410 *
1411 * Semi private function used to pass informations to a parser context
1412 * which are a combination of xmlRelaxNGParserFlag .
1413 *
1414 * Returns 0 if success and -1 in case of error
1415 */
1416int
1417xmlRelaxParserSetFlag(xmlRelaxNGParserCtxtPtr ctxt, int flags)
1418{
1419 if (ctxt == NULL) return(-1);
1420 if (flags & XML_RELAXNGP_FREE_DOC) {
1421 ctxt->crng |= XML_RELAXNGP_FREE_DOC;
1422 flags -= XML_RELAXNGP_FREE_DOC;
1423 }
1424 if (flags & XML_RELAXNGP_CRNG) {
1425 ctxt->crng |= XML_RELAXNGP_CRNG;
1426 flags -= XML_RELAXNGP_CRNG;
1427 }
1428 if (flags != 0) return(-1);
1429 return(0);
1430}
1431
1432/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001433 * *
1434 * Document functions *
1435 * *
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001436 ************************************************************************/
1437static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001438 xmlDocPtr doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001439
1440/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001441 * xmlRelaxNGIncludePush:
1442 * @ctxt: the parser context
1443 * @value: the element doc
1444 *
1445 * Pushes a new include on top of the include stack
1446 *
1447 * Returns 0 in case of error, the index in the stack otherwise
1448 */
1449static int
1450xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001451 xmlRelaxNGIncludePtr value)
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001452{
1453 if (ctxt->incTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001454 ctxt->incMax = 4;
1455 ctxt->incNr = 0;
1456 ctxt->incTab =
1457 (xmlRelaxNGIncludePtr *) xmlMalloc(ctxt->incMax *
1458 sizeof(ctxt->incTab[0]));
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001459 if (ctxt->incTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001460 xmlRngPErrMemory(ctxt, "allocating include\n");
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001461 return (0);
1462 }
1463 }
1464 if (ctxt->incNr >= ctxt->incMax) {
1465 ctxt->incMax *= 2;
1466 ctxt->incTab =
1467 (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00001468 ctxt->incMax *
1469 sizeof(ctxt->incTab[0]));
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001470 if (ctxt->incTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001471 xmlRngPErrMemory(ctxt, "allocating include\n");
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001472 return (0);
1473 }
1474 }
1475 ctxt->incTab[ctxt->incNr] = value;
1476 ctxt->inc = value;
1477 return (ctxt->incNr++);
1478}
1479
1480/**
1481 * xmlRelaxNGIncludePop:
1482 * @ctxt: the parser context
1483 *
1484 * Pops the top include from the include stack
1485 *
1486 * Returns the include just removed
1487 */
1488static xmlRelaxNGIncludePtr
1489xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt)
1490{
1491 xmlRelaxNGIncludePtr ret;
1492
1493 if (ctxt->incNr <= 0)
Daniel Veillard24505b02005-07-28 23:49:35 +00001494 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001495 ctxt->incNr--;
1496 if (ctxt->incNr > 0)
1497 ctxt->inc = ctxt->incTab[ctxt->incNr - 1];
1498 else
1499 ctxt->inc = NULL;
1500 ret = ctxt->incTab[ctxt->incNr];
Daniel Veillard24505b02005-07-28 23:49:35 +00001501 ctxt->incTab[ctxt->incNr] = NULL;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001502 return (ret);
1503}
1504
1505/**
Daniel Veillard5add8682003-03-10 13:13:58 +00001506 * xmlRelaxNGRemoveRedefine:
1507 * @ctxt: the parser context
1508 * @URL: the normalized URL
1509 * @target: the included target
1510 * @name: the define name to eliminate
1511 *
1512 * Applies the elimination algorithm of 4.7
1513 *
1514 * Returns 0 in case of error, 1 in case of success.
1515 */
1516static int
1517xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001518 const xmlChar * URL ATTRIBUTE_UNUSED,
1519 xmlNodePtr target, const xmlChar * name)
1520{
Daniel Veillard5add8682003-03-10 13:13:58 +00001521 int found = 0;
1522 xmlNodePtr tmp, tmp2;
1523 xmlChar *name2;
1524
1525#ifdef DEBUG_INCLUDE
Daniel Veillard952379b2003-03-17 15:37:12 +00001526 if (name == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00001527 xmlGenericError(xmlGenericErrorContext,
1528 "Elimination of <include> start from %s\n", URL);
Daniel Veillard952379b2003-03-17 15:37:12 +00001529 else
Daniel Veillard4c004142003-10-07 11:33:24 +00001530 xmlGenericError(xmlGenericErrorContext,
1531 "Elimination of <include> define %s from %s\n",
1532 name, URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001533#endif
1534 tmp = target;
1535 while (tmp != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001536 tmp2 = tmp->next;
1537 if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) {
1538 found = 1;
1539 xmlUnlinkNode(tmp);
1540 xmlFreeNode(tmp);
1541 } else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) {
1542 name2 = xmlGetProp(tmp, BAD_CAST "name");
1543 xmlRelaxNGNormExtSpace(name2);
1544 if (name2 != NULL) {
1545 if (xmlStrEqual(name, name2)) {
1546 found = 1;
1547 xmlUnlinkNode(tmp);
1548 xmlFreeNode(tmp);
1549 }
1550 xmlFree(name2);
1551 }
1552 } else if (IS_RELAXNG(tmp, "include")) {
1553 xmlChar *href = NULL;
Daniel Veillard807daf82004-02-22 22:13:27 +00001554 xmlRelaxNGDocumentPtr inc = tmp->psvi;
Daniel Veillard5add8682003-03-10 13:13:58 +00001555
Daniel Veillard4c004142003-10-07 11:33:24 +00001556 if ((inc != NULL) && (inc->doc != NULL) &&
1557 (inc->doc->children != NULL)) {
Daniel Veillard5add8682003-03-10 13:13:58 +00001558
Daniel Veillard4c004142003-10-07 11:33:24 +00001559 if (xmlStrEqual
1560 (inc->doc->children->name, BAD_CAST "grammar")) {
Daniel Veillard5add8682003-03-10 13:13:58 +00001561#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001562 href = xmlGetProp(tmp, BAD_CAST "href");
Daniel Veillard5add8682003-03-10 13:13:58 +00001563#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00001564 if (xmlRelaxNGRemoveRedefine(ctxt, href,
Shaun McCanced7eb9b52011-08-04 10:28:59 -04001565 xmlDocGetRootElement(inc->doc)->children,
1566 name) == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001567 found = 1;
1568 }
Daniel Veillard14b56432006-03-09 18:41:40 +00001569#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001570 if (href != NULL)
1571 xmlFree(href);
Daniel Veillard14b56432006-03-09 18:41:40 +00001572#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00001573 }
1574 }
1575 }
1576 tmp = tmp2;
Daniel Veillard5add8682003-03-10 13:13:58 +00001577 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001578 return (found);
Daniel Veillard5add8682003-03-10 13:13:58 +00001579}
1580
1581/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001582 * xmlRelaxNGLoadInclude:
1583 * @ctxt: the parser context
1584 * @URL: the normalized URL
1585 * @node: the include node.
Daniel Veillard416589a2003-02-17 17:25:42 +00001586 * @ns: the namespace passed from the context.
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001587 *
1588 * First lookup if the document is already loaded into the parser context,
1589 * check against recursion. If not found the resource is loaded and
1590 * the content is preprocessed before being returned back to the caller.
1591 *
1592 * Returns the xmlRelaxNGIncludePtr or NULL in case of error
1593 */
1594static xmlRelaxNGIncludePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00001595xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * URL,
1596 xmlNodePtr node, const xmlChar * ns)
1597{
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001598 xmlRelaxNGIncludePtr ret = NULL;
1599 xmlDocPtr doc;
1600 int i;
Daniel Veillard5add8682003-03-10 13:13:58 +00001601 xmlNodePtr root, cur;
1602
1603#ifdef DEBUG_INCLUDE
1604 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00001605 "xmlRelaxNGLoadInclude(%s)\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001606#endif
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001607
1608 /*
1609 * check against recursion in the stack
1610 */
Daniel Veillard4c004142003-10-07 11:33:24 +00001611 for (i = 0; i < ctxt->incNr; i++) {
1612 if (xmlStrEqual(ctxt->incTab[i]->href, URL)) {
1613 xmlRngPErr(ctxt, NULL, XML_RNGP_INCLUDE_RECURSE,
1614 "Detected an Include recursion for %s\n", URL,
1615 NULL);
1616 return (NULL);
1617 }
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001618 }
1619
1620 /*
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001621 * load the document
1622 */
Daniel Veillard87247e82004-01-13 20:42:02 +00001623 doc = xmlReadFile((const char *) URL,NULL,0);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001624 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001625 xmlRngPErr(ctxt, node, XML_RNGP_PARSE_ERROR,
1626 "xmlRelaxNG: could not load %s\n", URL, NULL);
1627 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001628 }
Daniel Veillard5add8682003-03-10 13:13:58 +00001629#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001630 xmlGenericError(xmlGenericErrorContext, "Parsed %s Okay\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001631#endif
1632
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001633 /*
1634 * Allocate the document structures and register it first.
1635 */
1636 ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude));
1637 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001638 xmlRngPErrMemory(ctxt, "allocating include\n");
1639 xmlFreeDoc(doc);
1640 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001641 }
1642 memset(ret, 0, sizeof(xmlRelaxNGInclude));
1643 ret->doc = doc;
1644 ret->href = xmlStrdup(URL);
Daniel Veillardc482e262003-02-26 14:48:48 +00001645 ret->next = ctxt->includes;
1646 ctxt->includes = ret;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001647
1648 /*
Daniel Veillard416589a2003-02-17 17:25:42 +00001649 * transmit the ns if needed
1650 */
1651 if (ns != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001652 root = xmlDocGetRootElement(doc);
1653 if (root != NULL) {
1654 if (xmlHasProp(root, BAD_CAST "ns") == NULL) {
1655 xmlSetProp(root, BAD_CAST "ns", ns);
1656 }
1657 }
Daniel Veillard416589a2003-02-17 17:25:42 +00001658 }
1659
1660 /*
Daniel Veillardc482e262003-02-26 14:48:48 +00001661 * push it on the stack
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001662 */
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001663 xmlRelaxNGIncludePush(ctxt, ret);
1664
1665 /*
1666 * Some preprocessing of the document content, this include recursing
1667 * in the include stack.
1668 */
Daniel Veillard5add8682003-03-10 13:13:58 +00001669#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001670 xmlGenericError(xmlGenericErrorContext, "cleanup of %s\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001671#endif
1672
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001673 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
1674 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001675 ctxt->inc = NULL;
1676 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001677 }
1678
1679 /*
1680 * Pop up the include from the stack
1681 */
1682 xmlRelaxNGIncludePop(ctxt);
1683
Daniel Veillard5add8682003-03-10 13:13:58 +00001684#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001685 xmlGenericError(xmlGenericErrorContext, "Checking of %s\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001686#endif
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001687 /*
1688 * Check that the top element is a grammar
1689 */
1690 root = xmlDocGetRootElement(doc);
1691 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001692 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY,
1693 "xmlRelaxNG: included document is empty %s\n", URL,
1694 NULL);
1695 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001696 }
1697 if (!IS_RELAXNG(root, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001698 xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING,
1699 "xmlRelaxNG: included document %s root is not a grammar\n",
1700 URL, NULL);
1701 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001702 }
1703
1704 /*
1705 * Elimination of redefined rules in the include.
1706 */
1707 cur = node->children;
1708 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001709 if (IS_RELAXNG(cur, "start")) {
1710 int found = 0;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001711
Daniel Veillard4c004142003-10-07 11:33:24 +00001712 found =
1713 xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL);
1714 if (!found) {
1715 xmlRngPErr(ctxt, node, XML_RNGP_START_MISSING,
1716 "xmlRelaxNG: include %s has a start but not the included grammar\n",
1717 URL, NULL);
1718 }
1719 } else if (IS_RELAXNG(cur, "define")) {
1720 xmlChar *name;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001721
Daniel Veillard4c004142003-10-07 11:33:24 +00001722 name = xmlGetProp(cur, BAD_CAST "name");
1723 if (name == NULL) {
1724 xmlRngPErr(ctxt, node, XML_RNGP_NAME_MISSING,
1725 "xmlRelaxNG: include %s has define without name\n",
1726 URL, NULL);
1727 } else {
1728 int found;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001729
Daniel Veillard4c004142003-10-07 11:33:24 +00001730 xmlRelaxNGNormExtSpace(name);
1731 found = xmlRelaxNGRemoveRedefine(ctxt, URL,
1732 root->children, name);
1733 if (!found) {
1734 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_MISSING,
1735 "xmlRelaxNG: include %s has a define %s but not the included grammar\n",
1736 URL, name);
1737 }
1738 xmlFree(name);
1739 }
1740 }
1741 cur = cur->next;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001742 }
1743
1744
Daniel Veillard4c004142003-10-07 11:33:24 +00001745 return (ret);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001746}
1747
1748/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00001749 * xmlRelaxNGValidErrorPush:
1750 * @ctxt: the validation context
1751 * @err: the error code
1752 * @arg1: the first string argument
1753 * @arg2: the second string argument
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001754 * @dup: arg need to be duplicated
Daniel Veillard42f12e92003-03-07 18:32:59 +00001755 *
1756 * Pushes a new error on top of the error stack
1757 *
1758 * Returns 0 in case of error, the index in the stack otherwise
1759 */
1760static int
Daniel Veillard4c004142003-10-07 11:33:24 +00001761xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt,
1762 xmlRelaxNGValidErr err, const xmlChar * arg1,
1763 const xmlChar * arg2, int dup)
Daniel Veillard42f12e92003-03-07 18:32:59 +00001764{
1765 xmlRelaxNGValidErrorPtr cur;
Daniel Veillard4c004142003-10-07 11:33:24 +00001766
Daniel Veillarda507fbf2003-03-31 16:09:37 +00001767#ifdef DEBUG_ERROR
1768 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00001769 "Pushing error %d at %d on stack\n", err, ctxt->errNr);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00001770#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00001771 if (ctxt->errTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001772 ctxt->errMax = 8;
1773 ctxt->errNr = 0;
1774 ctxt->errTab =
1775 (xmlRelaxNGValidErrorPtr) xmlMalloc(ctxt->errMax *
1776 sizeof
1777 (xmlRelaxNGValidError));
Daniel Veillard42f12e92003-03-07 18:32:59 +00001778 if (ctxt->errTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001779 xmlRngVErrMemory(ctxt, "pushing error\n");
Daniel Veillard42f12e92003-03-07 18:32:59 +00001780 return (0);
1781 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001782 ctxt->err = NULL;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001783 }
1784 if (ctxt->errNr >= ctxt->errMax) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001785 ctxt->errMax *= 2;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001786 ctxt->errTab =
1787 (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00001788 ctxt->errMax *
1789 sizeof
1790 (xmlRelaxNGValidError));
Daniel Veillard42f12e92003-03-07 18:32:59 +00001791 if (ctxt->errTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001792 xmlRngVErrMemory(ctxt, "pushing error\n");
Daniel Veillard42f12e92003-03-07 18:32:59 +00001793 return (0);
1794 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001795 ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
Daniel Veillard42f12e92003-03-07 18:32:59 +00001796 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00001797 if ((ctxt->err != NULL) && (ctxt->state != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00001798 (ctxt->err->node == ctxt->state->node) && (ctxt->err->err == err))
1799 return (ctxt->errNr);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001800 cur = &ctxt->errTab[ctxt->errNr];
1801 cur->err = err;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001802 if (dup) {
1803 cur->arg1 = xmlStrdup(arg1);
1804 cur->arg2 = xmlStrdup(arg2);
Daniel Veillard4c004142003-10-07 11:33:24 +00001805 cur->flags = ERROR_IS_DUP;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001806 } else {
1807 cur->arg1 = arg1;
1808 cur->arg2 = arg2;
Daniel Veillard4c004142003-10-07 11:33:24 +00001809 cur->flags = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001810 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001811 if (ctxt->state != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001812 cur->node = ctxt->state->node;
1813 cur->seq = ctxt->state->seq;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001814 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001815 cur->node = NULL;
1816 cur->seq = NULL;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001817 }
1818 ctxt->err = cur;
1819 return (ctxt->errNr++);
1820}
1821
1822/**
1823 * xmlRelaxNGValidErrorPop:
1824 * @ctxt: the validation context
1825 *
1826 * Pops the top error from the error stack
Daniel Veillard42f12e92003-03-07 18:32:59 +00001827 */
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001828static void
Daniel Veillard42f12e92003-03-07 18:32:59 +00001829xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt)
1830{
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001831 xmlRelaxNGValidErrorPtr cur;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001832
Daniel Veillard580ced82003-03-21 21:22:48 +00001833 if (ctxt->errNr <= 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001834 ctxt->err = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001835 return;
Daniel Veillard580ced82003-03-21 21:22:48 +00001836 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001837 ctxt->errNr--;
1838 if (ctxt->errNr > 0)
1839 ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
1840 else
1841 ctxt->err = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001842 cur = &ctxt->errTab[ctxt->errNr];
1843 if (cur->flags & ERROR_IS_DUP) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001844 if (cur->arg1 != NULL)
1845 xmlFree((xmlChar *) cur->arg1);
1846 cur->arg1 = NULL;
1847 if (cur->arg2 != NULL)
1848 xmlFree((xmlChar *) cur->arg2);
1849 cur->arg2 = NULL;
1850 cur->flags = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001851 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001852}
1853
Daniel Veillard42f12e92003-03-07 18:32:59 +00001854/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001855 * xmlRelaxNGDocumentPush:
1856 * @ctxt: the parser context
1857 * @value: the element doc
1858 *
1859 * Pushes a new doc on top of the doc stack
1860 *
1861 * Returns 0 in case of error, the index in the stack otherwise
1862 */
1863static int
1864xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001865 xmlRelaxNGDocumentPtr value)
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001866{
1867 if (ctxt->docTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001868 ctxt->docMax = 4;
1869 ctxt->docNr = 0;
1870 ctxt->docTab =
1871 (xmlRelaxNGDocumentPtr *) xmlMalloc(ctxt->docMax *
1872 sizeof(ctxt->docTab[0]));
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001873 if (ctxt->docTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001874 xmlRngPErrMemory(ctxt, "adding document\n");
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001875 return (0);
1876 }
1877 }
1878 if (ctxt->docNr >= ctxt->docMax) {
1879 ctxt->docMax *= 2;
1880 ctxt->docTab =
1881 (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00001882 ctxt->docMax *
1883 sizeof(ctxt->docTab[0]));
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001884 if (ctxt->docTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001885 xmlRngPErrMemory(ctxt, "adding document\n");
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001886 return (0);
1887 }
1888 }
1889 ctxt->docTab[ctxt->docNr] = value;
1890 ctxt->doc = value;
1891 return (ctxt->docNr++);
1892}
1893
1894/**
1895 * xmlRelaxNGDocumentPop:
1896 * @ctxt: the parser context
1897 *
1898 * Pops the top doc from the doc stack
1899 *
1900 * Returns the doc just removed
1901 */
1902static xmlRelaxNGDocumentPtr
1903xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt)
1904{
1905 xmlRelaxNGDocumentPtr ret;
1906
1907 if (ctxt->docNr <= 0)
Daniel Veillard24505b02005-07-28 23:49:35 +00001908 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001909 ctxt->docNr--;
1910 if (ctxt->docNr > 0)
1911 ctxt->doc = ctxt->docTab[ctxt->docNr - 1];
1912 else
1913 ctxt->doc = NULL;
1914 ret = ctxt->docTab[ctxt->docNr];
Daniel Veillard24505b02005-07-28 23:49:35 +00001915 ctxt->docTab[ctxt->docNr] = NULL;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001916 return (ret);
1917}
1918
1919/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001920 * xmlRelaxNGLoadExternalRef:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001921 * @ctxt: the parser context
1922 * @URL: the normalized URL
1923 * @ns: the inherited ns if any
1924 *
1925 * First lookup if the document is already loaded into the parser context,
1926 * check against recursion. If not found the resource is loaded and
1927 * the content is preprocessed before being returned back to the caller.
1928 *
1929 * Returns the xmlRelaxNGDocumentPtr or NULL in case of error
1930 */
1931static xmlRelaxNGDocumentPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00001932xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt,
1933 const xmlChar * URL, const xmlChar * ns)
1934{
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001935 xmlRelaxNGDocumentPtr ret = NULL;
1936 xmlDocPtr doc;
1937 xmlNodePtr root;
1938 int i;
1939
1940 /*
1941 * check against recursion in the stack
1942 */
Daniel Veillard4c004142003-10-07 11:33:24 +00001943 for (i = 0; i < ctxt->docNr; i++) {
1944 if (xmlStrEqual(ctxt->docTab[i]->href, URL)) {
1945 xmlRngPErr(ctxt, NULL, XML_RNGP_EXTERNALREF_RECURSE,
1946 "Detected an externalRef recursion for %s\n", URL,
1947 NULL);
1948 return (NULL);
1949 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001950 }
1951
1952 /*
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001953 * load the document
1954 */
Daniel Veillard87247e82004-01-13 20:42:02 +00001955 doc = xmlReadFile((const char *) URL,NULL,0);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001956 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001957 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
1958 "xmlRelaxNG: could not load %s\n", URL, NULL);
1959 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001960 }
1961
1962 /*
1963 * Allocate the document structures and register it first.
1964 */
1965 ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument));
1966 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001967 xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_ERR_NO_MEMORY,
1968 "xmlRelaxNG: allocate memory for doc %s\n", URL, NULL);
1969 xmlFreeDoc(doc);
1970 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001971 }
1972 memset(ret, 0, sizeof(xmlRelaxNGDocument));
1973 ret->doc = doc;
1974 ret->href = xmlStrdup(URL);
Daniel Veillardc482e262003-02-26 14:48:48 +00001975 ret->next = ctxt->documents;
Daniel Veillard81c51e12009-08-14 18:52:10 +02001976 ret->externalRef = 1;
Daniel Veillardc482e262003-02-26 14:48:48 +00001977 ctxt->documents = ret;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001978
1979 /*
1980 * transmit the ns if needed
1981 */
1982 if (ns != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001983 root = xmlDocGetRootElement(doc);
1984 if (root != NULL) {
1985 if (xmlHasProp(root, BAD_CAST "ns") == NULL) {
1986 xmlSetProp(root, BAD_CAST "ns", ns);
1987 }
1988 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001989 }
1990
1991 /*
1992 * push it on the stack and register it in the hash table
1993 */
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001994 xmlRelaxNGDocumentPush(ctxt, ret);
1995
1996 /*
1997 * Some preprocessing of the document content
1998 */
1999 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
2000 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002001 ctxt->doc = NULL;
2002 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00002003 }
2004
2005 xmlRelaxNGDocumentPop(ctxt);
2006
Daniel Veillard4c004142003-10-07 11:33:24 +00002007 return (ret);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00002008}
2009
2010/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002011 * *
2012 * Error functions *
2013 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00002014 ************************************************************************/
2015
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002016#define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0);
2017#define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0);
2018#define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0);
2019#define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1);
2020#define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002021
Daniel Veillard231d7912003-02-09 14:22:17 +00002022static const char *
Daniel Veillard4c004142003-10-07 11:33:24 +00002023xmlRelaxNGDefName(xmlRelaxNGDefinePtr def)
2024{
Daniel Veillard231d7912003-02-09 14:22:17 +00002025 if (def == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002026 return ("none");
2027 switch (def->type) {
2028 case XML_RELAXNG_EMPTY:
2029 return ("empty");
2030 case XML_RELAXNG_NOT_ALLOWED:
2031 return ("notAllowed");
2032 case XML_RELAXNG_EXCEPT:
2033 return ("except");
2034 case XML_RELAXNG_TEXT:
2035 return ("text");
2036 case XML_RELAXNG_ELEMENT:
2037 return ("element");
2038 case XML_RELAXNG_DATATYPE:
2039 return ("datatype");
2040 case XML_RELAXNG_VALUE:
2041 return ("value");
2042 case XML_RELAXNG_LIST:
2043 return ("list");
2044 case XML_RELAXNG_ATTRIBUTE:
2045 return ("attribute");
2046 case XML_RELAXNG_DEF:
2047 return ("def");
2048 case XML_RELAXNG_REF:
2049 return ("ref");
2050 case XML_RELAXNG_EXTERNALREF:
2051 return ("externalRef");
2052 case XML_RELAXNG_PARENTREF:
2053 return ("parentRef");
2054 case XML_RELAXNG_OPTIONAL:
2055 return ("optional");
2056 case XML_RELAXNG_ZEROORMORE:
2057 return ("zeroOrMore");
2058 case XML_RELAXNG_ONEORMORE:
2059 return ("oneOrMore");
2060 case XML_RELAXNG_CHOICE:
2061 return ("choice");
2062 case XML_RELAXNG_GROUP:
2063 return ("group");
2064 case XML_RELAXNG_INTERLEAVE:
2065 return ("interleave");
2066 case XML_RELAXNG_START:
2067 return ("start");
2068 case XML_RELAXNG_NOOP:
2069 return ("noop");
2070 case XML_RELAXNG_PARAM:
2071 return ("param");
Daniel Veillard231d7912003-02-09 14:22:17 +00002072 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002073 return ("unknown");
Daniel Veillard231d7912003-02-09 14:22:17 +00002074}
Daniel Veillardd2298792003-02-14 16:54:11 +00002075
Daniel Veillard6eadf632003-01-23 18:29:16 +00002076/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00002077 * xmlRelaxNGGetErrorString:
2078 * @err: the error code
2079 * @arg1: the first string argument
2080 * @arg2: the second string argument
Daniel Veillard6eadf632003-01-23 18:29:16 +00002081 *
Daniel Veillard42f12e92003-03-07 18:32:59 +00002082 * computes a formatted error string for the given error code and args
2083 *
2084 * Returns the error string, it must be deallocated by the caller
2085 */
2086static xmlChar *
Daniel Veillard4c004142003-10-07 11:33:24 +00002087xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar * arg1,
2088 const xmlChar * arg2)
2089{
Daniel Veillard42f12e92003-03-07 18:32:59 +00002090 char msg[1000];
2091
2092 if (arg1 == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002093 arg1 = BAD_CAST "";
Daniel Veillard42f12e92003-03-07 18:32:59 +00002094 if (arg2 == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002095 arg2 = BAD_CAST "";
Daniel Veillard42f12e92003-03-07 18:32:59 +00002096
2097 msg[0] = 0;
2098 switch (err) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002099 case XML_RELAXNG_OK:
2100 return (NULL);
2101 case XML_RELAXNG_ERR_MEMORY:
2102 return (xmlCharStrdup("out of memory\n"));
Daniel Veillard42f12e92003-03-07 18:32:59 +00002103 case XML_RELAXNG_ERR_TYPE:
Daniel Veillard4c004142003-10-07 11:33:24 +00002104 snprintf(msg, 1000, "failed to validate type %s\n", arg1);
2105 break;
2106 case XML_RELAXNG_ERR_TYPEVAL:
2107 snprintf(msg, 1000, "Type %s doesn't allow value '%s'\n", arg1,
2108 arg2);
2109 break;
2110 case XML_RELAXNG_ERR_DUPID:
2111 snprintf(msg, 1000, "ID %s redefined\n", arg1);
2112 break;
2113 case XML_RELAXNG_ERR_TYPECMP:
2114 snprintf(msg, 1000, "failed to compare type %s\n", arg1);
2115 break;
2116 case XML_RELAXNG_ERR_NOSTATE:
2117 return (xmlCharStrdup("Internal error: no state\n"));
2118 case XML_RELAXNG_ERR_NODEFINE:
2119 return (xmlCharStrdup("Internal error: no define\n"));
2120 case XML_RELAXNG_ERR_INTERNAL:
2121 snprintf(msg, 1000, "Internal error: %s\n", arg1);
2122 break;
2123 case XML_RELAXNG_ERR_LISTEXTRA:
2124 snprintf(msg, 1000, "Extra data in list: %s\n", arg1);
2125 break;
2126 case XML_RELAXNG_ERR_INTERNODATA:
2127 return (xmlCharStrdup
2128 ("Internal: interleave block has no data\n"));
2129 case XML_RELAXNG_ERR_INTERSEQ:
2130 return (xmlCharStrdup("Invalid sequence in interleave\n"));
2131 case XML_RELAXNG_ERR_INTEREXTRA:
2132 snprintf(msg, 1000, "Extra element %s in interleave\n", arg1);
2133 break;
2134 case XML_RELAXNG_ERR_ELEMNAME:
2135 snprintf(msg, 1000, "Expecting element %s, got %s\n", arg1,
2136 arg2);
2137 break;
2138 case XML_RELAXNG_ERR_ELEMNONS:
2139 snprintf(msg, 1000, "Expecting a namespace for element %s\n",
2140 arg1);
2141 break;
2142 case XML_RELAXNG_ERR_ELEMWRONGNS:
2143 snprintf(msg, 1000,
2144 "Element %s has wrong namespace: expecting %s\n", arg1,
2145 arg2);
2146 break;
2147 case XML_RELAXNG_ERR_ELEMWRONG:
2148 snprintf(msg, 1000, "Did not expect element %s there\n", arg1);
2149 break;
2150 case XML_RELAXNG_ERR_TEXTWRONG:
2151 snprintf(msg, 1000,
2152 "Did not expect text in element %s content\n", arg1);
2153 break;
2154 case XML_RELAXNG_ERR_ELEMEXTRANS:
2155 snprintf(msg, 1000, "Expecting no namespace for element %s\n",
2156 arg1);
2157 break;
2158 case XML_RELAXNG_ERR_ELEMNOTEMPTY:
2159 snprintf(msg, 1000, "Expecting element %s to be empty\n", arg1);
2160 break;
2161 case XML_RELAXNG_ERR_NOELEM:
2162 snprintf(msg, 1000, "Expecting an element %s, got nothing\n",
2163 arg1);
2164 break;
2165 case XML_RELAXNG_ERR_NOTELEM:
2166 return (xmlCharStrdup("Expecting an element got text\n"));
2167 case XML_RELAXNG_ERR_ATTRVALID:
2168 snprintf(msg, 1000, "Element %s failed to validate attributes\n",
2169 arg1);
2170 break;
2171 case XML_RELAXNG_ERR_CONTENTVALID:
2172 snprintf(msg, 1000, "Element %s failed to validate content\n",
2173 arg1);
2174 break;
2175 case XML_RELAXNG_ERR_EXTRACONTENT:
2176 snprintf(msg, 1000, "Element %s has extra content: %s\n",
2177 arg1, arg2);
2178 break;
2179 case XML_RELAXNG_ERR_INVALIDATTR:
2180 snprintf(msg, 1000, "Invalid attribute %s for element %s\n",
2181 arg1, arg2);
2182 break;
2183 case XML_RELAXNG_ERR_LACKDATA:
2184 snprintf(msg, 1000, "Datatype element %s contains no data\n",
2185 arg1);
2186 break;
2187 case XML_RELAXNG_ERR_DATAELEM:
2188 snprintf(msg, 1000, "Datatype element %s has child elements\n",
2189 arg1);
2190 break;
2191 case XML_RELAXNG_ERR_VALELEM:
2192 snprintf(msg, 1000, "Value element %s has child elements\n",
2193 arg1);
2194 break;
2195 case XML_RELAXNG_ERR_LISTELEM:
2196 snprintf(msg, 1000, "List element %s has child elements\n",
2197 arg1);
2198 break;
2199 case XML_RELAXNG_ERR_DATATYPE:
2200 snprintf(msg, 1000, "Error validating datatype %s\n", arg1);
2201 break;
2202 case XML_RELAXNG_ERR_VALUE:
2203 snprintf(msg, 1000, "Error validating value %s\n", arg1);
2204 break;
2205 case XML_RELAXNG_ERR_LIST:
2206 return (xmlCharStrdup("Error validating list\n"));
2207 case XML_RELAXNG_ERR_NOGRAMMAR:
2208 return (xmlCharStrdup("No top grammar defined\n"));
2209 case XML_RELAXNG_ERR_EXTRADATA:
2210 return (xmlCharStrdup("Extra data in the document\n"));
2211 default:
2212 return (xmlCharStrdup("Unknown error !\n"));
Daniel Veillard42f12e92003-03-07 18:32:59 +00002213 }
2214 if (msg[0] == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002215 snprintf(msg, 1000, "Unknown error code %d\n", err);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002216 }
Daniel Veillardadbb0e62003-05-10 20:02:45 +00002217 msg[1000 - 1] = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00002218 return (xmlStrdup((xmlChar *) msg));
Daniel Veillard42f12e92003-03-07 18:32:59 +00002219}
2220
2221/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00002222 * xmlRelaxNGShowValidError:
2223 * @ctxt: the validation context
2224 * @err: the error number
2225 * @node: the node
2226 * @child: the node child generating the problem.
2227 * @arg1: the first argument
2228 * @arg2: the second argument
2229 *
2230 * Show a validation error.
2231 */
2232static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002233xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt,
2234 xmlRelaxNGValidErr err, xmlNodePtr node,
2235 xmlNodePtr child, const xmlChar * arg1,
2236 const xmlChar * arg2)
Daniel Veillard42f12e92003-03-07 18:32:59 +00002237{
2238 xmlChar *msg;
2239
Daniel Veillardb30ca312005-09-04 13:50:03 +00002240 if (ctxt->flags & FLAGS_NOERROR)
Daniel Veillardf03a8cd2005-09-04 12:01:57 +00002241 return;
2242
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002243#ifdef DEBUG_ERROR
Daniel Veillard4c004142003-10-07 11:33:24 +00002244 xmlGenericError(xmlGenericErrorContext, "Show error %d\n", err);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002245#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00002246 msg = xmlRelaxNGGetErrorString(err, arg1, arg2);
2247 if (msg == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002248 return;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002249
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002250 if (ctxt->errNo == XML_RELAXNG_OK)
Daniel Veillard4c004142003-10-07 11:33:24 +00002251 ctxt->errNo = err;
2252 xmlRngVErr(ctxt, (child == NULL ? node : child), err,
2253 (const char *) msg, arg1, arg2);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002254 xmlFree(msg);
2255}
2256
2257/**
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002258 * xmlRelaxNGPopErrors:
2259 * @ctxt: the validation context
2260 * @level: the error level in the stack
2261 *
2262 * pop and discard all errors until the given level is reached
2263 */
2264static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002265xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level)
2266{
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002267 int i;
2268 xmlRelaxNGValidErrorPtr err;
2269
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002270#ifdef DEBUG_ERROR
2271 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00002272 "Pop errors till level %d\n", level);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002273#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002274 for (i = level; i < ctxt->errNr; i++) {
2275 err = &ctxt->errTab[i];
2276 if (err->flags & ERROR_IS_DUP) {
2277 if (err->arg1 != NULL)
2278 xmlFree((xmlChar *) err->arg1);
2279 err->arg1 = NULL;
2280 if (err->arg2 != NULL)
2281 xmlFree((xmlChar *) err->arg2);
2282 err->arg2 = NULL;
2283 err->flags = 0;
2284 }
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002285 }
2286 ctxt->errNr = level;
Daniel Veillard580ced82003-03-21 21:22:48 +00002287 if (ctxt->errNr <= 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002288 ctxt->err = NULL;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002289}
Daniel Veillard4c004142003-10-07 11:33:24 +00002290
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002291/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00002292 * xmlRelaxNGDumpValidError:
2293 * @ctxt: the validation context
2294 *
2295 * Show all validation error over a given index.
2296 */
2297static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002298xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt)
2299{
Daniel Veillard5f1946a2003-03-31 16:38:16 +00002300 int i, j, k;
Daniel Veillard580ced82003-03-21 21:22:48 +00002301 xmlRelaxNGValidErrorPtr err, dup;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002302
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002303#ifdef DEBUG_ERROR
2304 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00002305 "Dumping error stack %d errors\n", ctxt->errNr);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002306#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002307 for (i = 0, k = 0; i < ctxt->errNr; i++) {
2308 err = &ctxt->errTab[i];
2309 if (k < MAX_ERROR) {
2310 for (j = 0; j < i; j++) {
2311 dup = &ctxt->errTab[j];
2312 if ((err->err == dup->err) && (err->node == dup->node) &&
2313 (xmlStrEqual(err->arg1, dup->arg1)) &&
2314 (xmlStrEqual(err->arg2, dup->arg2))) {
2315 goto skip;
2316 }
2317 }
2318 xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq,
2319 err->arg1, err->arg2);
2320 k++;
2321 }
2322 skip:
2323 if (err->flags & ERROR_IS_DUP) {
2324 if (err->arg1 != NULL)
2325 xmlFree((xmlChar *) err->arg1);
2326 err->arg1 = NULL;
2327 if (err->arg2 != NULL)
2328 xmlFree((xmlChar *) err->arg2);
2329 err->arg2 = NULL;
2330 err->flags = 0;
2331 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00002332 }
2333 ctxt->errNr = 0;
2334}
Daniel Veillard4c004142003-10-07 11:33:24 +00002335
Daniel Veillard42f12e92003-03-07 18:32:59 +00002336/**
2337 * xmlRelaxNGAddValidError:
2338 * @ctxt: the validation context
2339 * @err: the error number
2340 * @arg1: the first argument
2341 * @arg2: the second argument
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002342 * @dup: need to dup the args
Daniel Veillard42f12e92003-03-07 18:32:59 +00002343 *
2344 * Register a validation error, either generating it if it's sure
2345 * or stacking it for later handling if unsure.
2346 */
2347static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002348xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt,
2349 xmlRelaxNGValidErr err, const xmlChar * arg1,
2350 const xmlChar * arg2, int dup)
Daniel Veillard42f12e92003-03-07 18:32:59 +00002351{
Daniel Veillardb30ca312005-09-04 13:50:03 +00002352 if (ctxt == NULL)
2353 return;
2354 if (ctxt->flags & FLAGS_NOERROR)
Daniel Veillard4c004142003-10-07 11:33:24 +00002355 return;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002356
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002357#ifdef DEBUG_ERROR
Daniel Veillard4c004142003-10-07 11:33:24 +00002358 xmlGenericError(xmlGenericErrorContext, "Adding error %d\n", err);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002359#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00002360 /*
2361 * generate the error directly
2362 */
William M. Brack60929622004-03-27 17:54:18 +00002363 if (((ctxt->flags & FLAGS_IGNORABLE) == 0) ||
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002364 (ctxt->flags & FLAGS_NEGATIVE)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002365 xmlNodePtr node, seq;
2366
2367 /*
2368 * Flush first any stacked error which might be the
2369 * real cause of the problem.
2370 */
2371 if (ctxt->errNr != 0)
2372 xmlRelaxNGDumpValidError(ctxt);
2373 if (ctxt->state != NULL) {
2374 node = ctxt->state->node;
2375 seq = ctxt->state->seq;
2376 } else {
2377 node = seq = NULL;
2378 }
Daniel Veillardec18c962009-08-26 18:37:43 +02002379 if ((node == NULL) && (seq == NULL)) {
2380 node = ctxt->pnode;
2381 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002382 xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002383 }
2384 /*
2385 * Stack the error for later processing if needed
2386 */
2387 else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002388 xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002389 }
2390}
2391
Daniel Veillard6eadf632003-01-23 18:29:16 +00002392
2393/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002394 * *
2395 * Type library hooks *
2396 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00002397 ************************************************************************/
Daniel Veillardea3f3982003-01-26 19:45:18 +00002398static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00002399 const xmlChar * str);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002400
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002401/**
2402 * xmlRelaxNGSchemaTypeHave:
2403 * @data: data needed for the library
2404 * @type: the type name
2405 *
2406 * Check if the given type is provided by
2407 * the W3C XMLSchema Datatype library.
2408 *
2409 * Returns 1 if yes, 0 if no and -1 in case of error.
2410 */
2411static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002412xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar * type)
2413{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002414 xmlSchemaTypePtr typ;
2415
2416 if (type == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002417 return (-1);
2418 typ = xmlSchemaGetPredefinedType(type,
2419 BAD_CAST
2420 "http://www.w3.org/2001/XMLSchema");
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002421 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002422 return (0);
2423 return (1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002424}
2425
2426/**
2427 * xmlRelaxNGSchemaTypeCheck:
2428 * @data: data needed for the library
2429 * @type: the type name
2430 * @value: the value to check
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002431 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002432 *
2433 * Check if the given type and value are validated by
2434 * the W3C XMLSchema Datatype library.
2435 *
2436 * Returns 1 if yes, 0 if no and -1 in case of error.
2437 */
2438static int
2439xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002440 const xmlChar * type,
2441 const xmlChar * value,
2442 void **result, xmlNodePtr node)
2443{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002444 xmlSchemaTypePtr typ;
2445 int ret;
2446
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002447 if ((type == NULL) || (value == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002448 return (-1);
2449 typ = xmlSchemaGetPredefinedType(type,
2450 BAD_CAST
2451 "http://www.w3.org/2001/XMLSchema");
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002452 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002453 return (-1);
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002454 ret = xmlSchemaValPredefTypeNode(typ, value,
Daniel Veillard4c004142003-10-07 11:33:24 +00002455 (xmlSchemaValPtr *) result, node);
2456 if (ret == 2) /* special ID error code */
2457 return (2);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002458 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002459 return (1);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002460 if (ret > 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002461 return (0);
2462 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002463}
2464
2465/**
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002466 * xmlRelaxNGSchemaFacetCheck:
2467 * @data: data needed for the library
2468 * @type: the type name
2469 * @facet: the facet name
2470 * @val: the facet value
2471 * @strval: the string value
2472 * @value: the value to check
2473 *
2474 * Function provided by a type library to check a value facet
2475 *
2476 * Returns 1 if yes, 0 if no and -1 in case of error.
2477 */
2478static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002479xmlRelaxNGSchemaFacetCheck(void *data ATTRIBUTE_UNUSED,
2480 const xmlChar * type, const xmlChar * facetname,
2481 const xmlChar * val, const xmlChar * strval,
2482 void *value)
2483{
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002484 xmlSchemaFacetPtr facet;
2485 xmlSchemaTypePtr typ;
2486 int ret;
2487
2488 if ((type == NULL) || (strval == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002489 return (-1);
2490 typ = xmlSchemaGetPredefinedType(type,
2491 BAD_CAST
2492 "http://www.w3.org/2001/XMLSchema");
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002493 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002494 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002495
2496 facet = xmlSchemaNewFacet();
2497 if (facet == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002498 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002499
Daniel Veillard4c004142003-10-07 11:33:24 +00002500 if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002501 facet->type = XML_SCHEMA_FACET_MININCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002502 } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002503 facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002504 } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002505 facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002506 } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002507 facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002508 } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002509 facet->type = XML_SCHEMA_FACET_TOTALDIGITS;
Daniel Veillard4c004142003-10-07 11:33:24 +00002510 } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002511 facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS;
Daniel Veillard4c004142003-10-07 11:33:24 +00002512 } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002513 facet->type = XML_SCHEMA_FACET_PATTERN;
Daniel Veillard4c004142003-10-07 11:33:24 +00002514 } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002515 facet->type = XML_SCHEMA_FACET_ENUMERATION;
Daniel Veillard4c004142003-10-07 11:33:24 +00002516 } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002517 facet->type = XML_SCHEMA_FACET_WHITESPACE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002518 } else if (xmlStrEqual(facetname, BAD_CAST "length")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002519 facet->type = XML_SCHEMA_FACET_LENGTH;
Daniel Veillard4c004142003-10-07 11:33:24 +00002520 } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002521 facet->type = XML_SCHEMA_FACET_MAXLENGTH;
2522 } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) {
2523 facet->type = XML_SCHEMA_FACET_MINLENGTH;
2524 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002525 xmlSchemaFreeFacet(facet);
2526 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002527 }
Daniel Veillard6dc91962004-03-22 19:10:02 +00002528 facet->value = val;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002529 ret = xmlSchemaCheckFacet(facet, typ, NULL, type);
2530 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002531 xmlSchemaFreeFacet(facet);
2532 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002533 }
2534 ret = xmlSchemaValidateFacet(typ, facet, strval, value);
2535 xmlSchemaFreeFacet(facet);
2536 if (ret != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002537 return (-1);
2538 return (0);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002539}
2540
2541/**
Daniel Veillard80b19092003-03-28 13:29:53 +00002542 * xmlRelaxNGSchemaFreeValue:
2543 * @data: data needed for the library
2544 * @value: the value to free
2545 *
2546 * Function provided by a type library to free a Schemas value
2547 *
2548 * Returns 1 if yes, 0 if no and -1 in case of error.
2549 */
2550static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002551xmlRelaxNGSchemaFreeValue(void *data ATTRIBUTE_UNUSED, void *value)
2552{
Daniel Veillard80b19092003-03-28 13:29:53 +00002553 xmlSchemaFreeValue(value);
2554}
2555
2556/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002557 * xmlRelaxNGSchemaTypeCompare:
2558 * @data: data needed for the library
2559 * @type: the type name
2560 * @value1: the first value
2561 * @value2: the second value
2562 *
Daniel Veillard80b19092003-03-28 13:29:53 +00002563 * Compare two values for equality accordingly a type from the W3C XMLSchema
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002564 * Datatype library.
2565 *
Daniel Veillard80b19092003-03-28 13:29:53 +00002566 * Returns 1 if equal, 0 if no and -1 in case of error.
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002567 */
2568static int
2569xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002570 const xmlChar * type,
2571 const xmlChar * value1,
2572 xmlNodePtr ctxt1,
2573 void *comp1,
2574 const xmlChar * value2, xmlNodePtr ctxt2)
2575{
Daniel Veillard80b19092003-03-28 13:29:53 +00002576 int ret;
2577 xmlSchemaTypePtr typ;
2578 xmlSchemaValPtr res1 = NULL, res2 = NULL;
2579
2580 if ((type == NULL) || (value1 == NULL) || (value2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002581 return (-1);
2582 typ = xmlSchemaGetPredefinedType(type,
2583 BAD_CAST
2584 "http://www.w3.org/2001/XMLSchema");
Daniel Veillard80b19092003-03-28 13:29:53 +00002585 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002586 return (-1);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002587 if (comp1 == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002588 ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1);
2589 if (ret != 0)
2590 return (-1);
2591 if (res1 == NULL)
2592 return (-1);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002593 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002594 res1 = (xmlSchemaValPtr) comp1;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002595 }
2596 ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2);
Daniel Veillard80b19092003-03-28 13:29:53 +00002597 if (ret != 0) {
Daniel Veillardf4644032005-06-13 11:41:31 +00002598 if ((comp1 == NULL) && (res1 != NULL))
2599 xmlSchemaFreeValue(res1);
Daniel Veillard4c004142003-10-07 11:33:24 +00002600 return (-1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002601 }
2602 if (res1 == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002603 return (-1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002604 }
2605 ret = xmlSchemaCompareValues(res1, res2);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002606 if (res1 != (xmlSchemaValPtr) comp1)
Daniel Veillard4c004142003-10-07 11:33:24 +00002607 xmlSchemaFreeValue(res1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002608 xmlSchemaFreeValue(res2);
2609 if (ret == -2)
Daniel Veillard4c004142003-10-07 11:33:24 +00002610 return (-1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002611 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002612 return (1);
2613 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002614}
Daniel Veillard4c004142003-10-07 11:33:24 +00002615
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002616/**
2617 * xmlRelaxNGDefaultTypeHave:
2618 * @data: data needed for the library
2619 * @type: the type name
2620 *
2621 * Check if the given type is provided by
2622 * the default datatype library.
2623 *
2624 * Returns 1 if yes, 0 if no and -1 in case of error.
2625 */
2626static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002627xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED,
2628 const xmlChar * type)
2629{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002630 if (type == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002631 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002632 if (xmlStrEqual(type, BAD_CAST "string"))
Daniel Veillard4c004142003-10-07 11:33:24 +00002633 return (1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002634 if (xmlStrEqual(type, BAD_CAST "token"))
Daniel Veillard4c004142003-10-07 11:33:24 +00002635 return (1);
2636 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002637}
2638
2639/**
2640 * xmlRelaxNGDefaultTypeCheck:
2641 * @data: data needed for the library
2642 * @type: the type name
2643 * @value: the value to check
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002644 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002645 *
2646 * Check if the given type and value are validated by
2647 * the default datatype library.
2648 *
2649 * Returns 1 if yes, 0 if no and -1 in case of error.
2650 */
2651static int
2652xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002653 const xmlChar * type ATTRIBUTE_UNUSED,
2654 const xmlChar * value ATTRIBUTE_UNUSED,
2655 void **result ATTRIBUTE_UNUSED,
2656 xmlNodePtr node ATTRIBUTE_UNUSED)
2657{
Daniel Veillardd4310742003-02-18 21:12:46 +00002658 if (value == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002659 return (-1);
Daniel Veillardd4310742003-02-18 21:12:46 +00002660 if (xmlStrEqual(type, BAD_CAST "string"))
Daniel Veillard4c004142003-10-07 11:33:24 +00002661 return (1);
Daniel Veillardd4310742003-02-18 21:12:46 +00002662 if (xmlStrEqual(type, BAD_CAST "token")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002663 return (1);
Daniel Veillardd4310742003-02-18 21:12:46 +00002664 }
2665
Daniel Veillard4c004142003-10-07 11:33:24 +00002666 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002667}
2668
2669/**
2670 * xmlRelaxNGDefaultTypeCompare:
2671 * @data: data needed for the library
2672 * @type: the type name
2673 * @value1: the first value
2674 * @value2: the second value
2675 *
2676 * Compare two values accordingly a type from the default
2677 * datatype library.
2678 *
2679 * Returns 1 if yes, 0 if no and -1 in case of error.
2680 */
2681static int
2682xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002683 const xmlChar * type,
2684 const xmlChar * value1,
2685 xmlNodePtr ctxt1 ATTRIBUTE_UNUSED,
2686 void *comp1 ATTRIBUTE_UNUSED,
2687 const xmlChar * value2,
2688 xmlNodePtr ctxt2 ATTRIBUTE_UNUSED)
2689{
Daniel Veillardea3f3982003-01-26 19:45:18 +00002690 int ret = -1;
2691
2692 if (xmlStrEqual(type, BAD_CAST "string")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002693 ret = xmlStrEqual(value1, value2);
Daniel Veillardea3f3982003-01-26 19:45:18 +00002694 } else if (xmlStrEqual(type, BAD_CAST "token")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002695 if (!xmlStrEqual(value1, value2)) {
2696 xmlChar *nval, *nvalue;
Daniel Veillardea3f3982003-01-26 19:45:18 +00002697
Daniel Veillard4c004142003-10-07 11:33:24 +00002698 /*
2699 * TODO: trivial optimizations are possible by
2700 * computing at compile-time
2701 */
2702 nval = xmlRelaxNGNormalize(NULL, value1);
2703 nvalue = xmlRelaxNGNormalize(NULL, value2);
Daniel Veillardea3f3982003-01-26 19:45:18 +00002704
Daniel Veillard4c004142003-10-07 11:33:24 +00002705 if ((nval == NULL) || (nvalue == NULL))
2706 ret = -1;
2707 else if (xmlStrEqual(nval, nvalue))
2708 ret = 1;
2709 else
2710 ret = 0;
2711 if (nval != NULL)
2712 xmlFree(nval);
2713 if (nvalue != NULL)
2714 xmlFree(nvalue);
2715 } else
2716 ret = 1;
Daniel Veillardea3f3982003-01-26 19:45:18 +00002717 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002718 return (ret);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002719}
Daniel Veillard4c004142003-10-07 11:33:24 +00002720
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002721static int xmlRelaxNGTypeInitialized = 0;
2722static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL;
2723
2724/**
2725 * xmlRelaxNGFreeTypeLibrary:
2726 * @lib: the type library structure
2727 * @namespace: the URI bound to the library
2728 *
2729 * Free the structure associated to the type library
2730 */
Daniel Veillard6eadf632003-01-23 18:29:16 +00002731static void
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002732xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib,
Daniel Veillard4c004142003-10-07 11:33:24 +00002733 const xmlChar * namespace ATTRIBUTE_UNUSED)
2734{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002735 if (lib == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002736 return;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002737 if (lib->namespace != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002738 xmlFree((xmlChar *) lib->namespace);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002739 xmlFree(lib);
2740}
2741
2742/**
2743 * xmlRelaxNGRegisterTypeLibrary:
2744 * @namespace: the URI bound to the library
2745 * @data: data associated to the library
2746 * @have: the provide function
2747 * @check: the checking function
2748 * @comp: the comparison function
2749 *
2750 * Register a new type library
2751 *
2752 * Returns 0 in case of success and -1 in case of error.
2753 */
2754static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002755xmlRelaxNGRegisterTypeLibrary(const xmlChar * namespace, void *data,
2756 xmlRelaxNGTypeHave have,
2757 xmlRelaxNGTypeCheck check,
2758 xmlRelaxNGTypeCompare comp,
2759 xmlRelaxNGFacetCheck facet,
2760 xmlRelaxNGTypeFree freef)
2761{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002762 xmlRelaxNGTypeLibraryPtr lib;
2763 int ret;
2764
2765 if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00002766 (check == NULL) || (comp == NULL))
2767 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002768 if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002769 xmlGenericError(xmlGenericErrorContext,
2770 "Relax-NG types library '%s' already registered\n",
2771 namespace);
2772 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002773 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002774 lib =
2775 (xmlRelaxNGTypeLibraryPtr)
2776 xmlMalloc(sizeof(xmlRelaxNGTypeLibrary));
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002777 if (lib == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002778 xmlRngVErrMemory(NULL, "adding types library\n");
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002779 return (-1);
2780 }
2781 memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary));
2782 lib->namespace = xmlStrdup(namespace);
2783 lib->data = data;
2784 lib->have = have;
2785 lib->comp = comp;
2786 lib->check = check;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002787 lib->facet = facet;
2788 lib->freef = freef;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002789 ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib);
2790 if (ret < 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002791 xmlGenericError(xmlGenericErrorContext,
2792 "Relax-NG types library failed to register '%s'\n",
2793 namespace);
2794 xmlRelaxNGFreeTypeLibrary(lib, namespace);
2795 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002796 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002797 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002798}
2799
2800/**
2801 * xmlRelaxNGInitTypes:
2802 *
2803 * Initilize the default type libraries.
2804 *
2805 * Returns 0 in case of success and -1 in case of error.
2806 */
Daniel Veillarddd6d3002004-11-03 14:20:29 +00002807int
Daniel Veillard4c004142003-10-07 11:33:24 +00002808xmlRelaxNGInitTypes(void)
2809{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002810 if (xmlRelaxNGTypeInitialized != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002811 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002812 xmlRelaxNGRegisteredTypes = xmlHashCreate(10);
2813 if (xmlRelaxNGRegisteredTypes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002814 xmlGenericError(xmlGenericErrorContext,
2815 "Failed to allocate sh table for Relax-NG types\n");
2816 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002817 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002818 xmlRelaxNGRegisterTypeLibrary(BAD_CAST
2819 "http://www.w3.org/2001/XMLSchema-datatypes",
2820 NULL, xmlRelaxNGSchemaTypeHave,
2821 xmlRelaxNGSchemaTypeCheck,
2822 xmlRelaxNGSchemaTypeCompare,
2823 xmlRelaxNGSchemaFacetCheck,
2824 xmlRelaxNGSchemaFreeValue);
2825 xmlRelaxNGRegisterTypeLibrary(xmlRelaxNGNs, NULL,
2826 xmlRelaxNGDefaultTypeHave,
2827 xmlRelaxNGDefaultTypeCheck,
2828 xmlRelaxNGDefaultTypeCompare, NULL,
2829 NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002830 xmlRelaxNGTypeInitialized = 1;
Daniel Veillard4c004142003-10-07 11:33:24 +00002831 return (0);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002832}
2833
2834/**
2835 * xmlRelaxNGCleanupTypes:
2836 *
2837 * Cleanup the default Schemas type library associated to RelaxNG
2838 */
Daniel Veillard4c004142003-10-07 11:33:24 +00002839void
2840xmlRelaxNGCleanupTypes(void)
2841{
Daniel Veillarda84c0b32003-06-02 16:58:46 +00002842 xmlSchemaCleanupTypes();
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002843 if (xmlRelaxNGTypeInitialized == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002844 return;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002845 xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator)
Daniel Veillard4c004142003-10-07 11:33:24 +00002846 xmlRelaxNGFreeTypeLibrary);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002847 xmlRelaxNGTypeInitialized = 0;
Daniel Veillard6eadf632003-01-23 18:29:16 +00002848}
2849
2850/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002851 * *
2852 * Compiling element content into regexp *
2853 * *
Daniel Veillard952379b2003-03-17 15:37:12 +00002854 * Sometime the element content can be compiled into a pure regexp, *
2855 * This allows a faster execution and streamability at that level *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002856 * *
Daniel Veillard952379b2003-03-17 15:37:12 +00002857 ************************************************************************/
2858
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002859/* from automata.c but not exported */
2860void xmlAutomataSetFlags(xmlAutomataPtr am, int flags);
2861
2862
Daniel Veillard52b48c72003-04-13 19:53:42 +00002863static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt,
2864 xmlRelaxNGDefinePtr def);
2865
Daniel Veillard952379b2003-03-17 15:37:12 +00002866/**
2867 * xmlRelaxNGIsCompileable:
2868 * @define: the definition to check
2869 *
2870 * Check if a definition is nullable.
2871 *
2872 * Returns 1 if yes, 0 if no and -1 in case of error
2873 */
2874static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002875xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def)
2876{
Daniel Veillard52b48c72003-04-13 19:53:42 +00002877 int ret = -1;
2878
Daniel Veillard952379b2003-03-17 15:37:12 +00002879 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002880 return (-1);
Daniel Veillard952379b2003-03-17 15:37:12 +00002881 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00002882 if ((def->type != XML_RELAXNG_ELEMENT) &&
2883 (def->dflags & IS_COMPILABLE))
Daniel Veillard4c004142003-10-07 11:33:24 +00002884 return (1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00002885 if ((def->type != XML_RELAXNG_ELEMENT) &&
2886 (def->dflags & IS_NOT_COMPILABLE))
Daniel Veillard4c004142003-10-07 11:33:24 +00002887 return (0);
2888 switch (def->type) {
Daniel Veillard952379b2003-03-17 15:37:12 +00002889 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00002890 ret = xmlRelaxNGIsCompileable(def->content);
2891 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002892 case XML_RELAXNG_TEXT:
Daniel Veillard952379b2003-03-17 15:37:12 +00002893 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00002894 ret = 1;
2895 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002896 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00002897 /*
2898 * Check if the element content is compileable
2899 */
2900 if (((def->dflags & IS_NOT_COMPILABLE) == 0) &&
2901 ((def->dflags & IS_COMPILABLE) == 0)) {
2902 xmlRelaxNGDefinePtr list;
2903
2904 list = def->content;
2905 while (list != NULL) {
2906 ret = xmlRelaxNGIsCompileable(list);
2907 if (ret != 1)
2908 break;
2909 list = list->next;
2910 }
William M. Brack60929622004-03-27 17:54:18 +00002911 /*
2912 * Because the routine is recursive, we must guard against
2913 * discovering both COMPILABLE and NOT_COMPILABLE
2914 */
2915 if (ret == 0) {
2916 def->dflags &= ~IS_COMPILABLE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002917 def->dflags |= IS_NOT_COMPILABLE;
William M. Brack60929622004-03-27 17:54:18 +00002918 }
2919 if ((ret == 1) && !(def->dflags &= IS_NOT_COMPILABLE))
Daniel Veillard4c004142003-10-07 11:33:24 +00002920 def->dflags |= IS_COMPILABLE;
Daniel Veillardd94849b2003-07-28 13:02:24 +00002921#ifdef DEBUG_COMPILE
Daniel Veillard4c004142003-10-07 11:33:24 +00002922 if (ret == 1) {
2923 xmlGenericError(xmlGenericErrorContext,
2924 "element content for %s is compilable\n",
2925 def->name);
2926 } else if (ret == 0) {
2927 xmlGenericError(xmlGenericErrorContext,
2928 "element content for %s is not compilable\n",
2929 def->name);
2930 } else {
2931 xmlGenericError(xmlGenericErrorContext,
2932 "Problem in RelaxNGIsCompileable for element %s\n",
2933 def->name);
2934 }
Daniel Veillardd94849b2003-07-28 13:02:24 +00002935#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002936 }
2937 /*
2938 * All elements return a compileable status unless they
2939 * are generic like anyName
2940 */
2941 if ((def->nameClass != NULL) || (def->name == NULL))
2942 ret = 0;
2943 else
2944 ret = 1;
2945 return (ret);
Daniel Veillard2134ab12003-07-23 19:56:29 +00002946 case XML_RELAXNG_REF:
2947 case XML_RELAXNG_EXTERNALREF:
2948 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00002949 if (def->depth == -20) {
2950 return (1);
2951 } else {
2952 xmlRelaxNGDefinePtr list;
Daniel Veillard2134ab12003-07-23 19:56:29 +00002953
Daniel Veillard4c004142003-10-07 11:33:24 +00002954 def->depth = -20;
2955 list = def->content;
2956 while (list != NULL) {
2957 ret = xmlRelaxNGIsCompileable(list);
2958 if (ret != 1)
2959 break;
2960 list = list->next;
2961 }
2962 }
2963 break;
Daniel Veillard2134ab12003-07-23 19:56:29 +00002964 case XML_RELAXNG_START:
Daniel Veillard952379b2003-03-17 15:37:12 +00002965 case XML_RELAXNG_OPTIONAL:
2966 case XML_RELAXNG_ZEROORMORE:
2967 case XML_RELAXNG_ONEORMORE:
2968 case XML_RELAXNG_CHOICE:
2969 case XML_RELAXNG_GROUP:
Daniel Veillard4c004142003-10-07 11:33:24 +00002970 case XML_RELAXNG_DEF:{
2971 xmlRelaxNGDefinePtr list;
Daniel Veillard952379b2003-03-17 15:37:12 +00002972
Daniel Veillard4c004142003-10-07 11:33:24 +00002973 list = def->content;
2974 while (list != NULL) {
2975 ret = xmlRelaxNGIsCompileable(list);
2976 if (ret != 1)
2977 break;
2978 list = list->next;
2979 }
2980 break;
2981 }
Daniel Veillard952379b2003-03-17 15:37:12 +00002982 case XML_RELAXNG_EXCEPT:
2983 case XML_RELAXNG_ATTRIBUTE:
2984 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard52b48c72003-04-13 19:53:42 +00002985 case XML_RELAXNG_DATATYPE:
2986 case XML_RELAXNG_LIST:
2987 case XML_RELAXNG_PARAM:
2988 case XML_RELAXNG_VALUE:
Daniel Veillard952379b2003-03-17 15:37:12 +00002989 case XML_RELAXNG_NOT_ALLOWED:
William M. Brack7e29c0a2004-04-02 09:07:22 +00002990 ret = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00002991 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002992 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002993 if (ret == 0)
2994 def->dflags |= IS_NOT_COMPILABLE;
2995 if (ret == 1)
2996 def->dflags |= IS_COMPILABLE;
Daniel Veillardd94849b2003-07-28 13:02:24 +00002997#ifdef DEBUG_COMPILE
2998 if (ret == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002999 xmlGenericError(xmlGenericErrorContext,
3000 "RelaxNGIsCompileable %s : true\n",
3001 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00003002 } else if (ret == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003003 xmlGenericError(xmlGenericErrorContext,
3004 "RelaxNGIsCompileable %s : false\n",
3005 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00003006 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003007 xmlGenericError(xmlGenericErrorContext,
3008 "Problem in RelaxNGIsCompileable %s\n",
3009 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00003010 }
3011#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00003012 return (ret);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003013}
3014
3015/**
3016 * xmlRelaxNGCompile:
3017 * ctxt: the RelaxNG parser context
3018 * @define: the definition tree to compile
3019 *
3020 * Compile the set of definitions, it works recursively, till the
3021 * element boundaries, where it tries to compile the content if possible
3022 *
3023 * Returns 0 if success and -1 in case of error
3024 */
3025static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003026xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
3027{
Daniel Veillard52b48c72003-04-13 19:53:42 +00003028 int ret = 0;
3029 xmlRelaxNGDefinePtr list;
3030
Daniel Veillard4c004142003-10-07 11:33:24 +00003031 if ((ctxt == NULL) || (def == NULL))
3032 return (-1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003033
Daniel Veillard4c004142003-10-07 11:33:24 +00003034 switch (def->type) {
Daniel Veillard52b48c72003-04-13 19:53:42 +00003035 case XML_RELAXNG_START:
3036 if ((xmlRelaxNGIsCompileable(def) == 1) && (def->depth != -25)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003037 xmlAutomataPtr oldam = ctxt->am;
3038 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003039
3040 def->depth = -25;
3041
Daniel Veillard4c004142003-10-07 11:33:24 +00003042 list = def->content;
3043 ctxt->am = xmlNewAutomata();
3044 if (ctxt->am == NULL)
3045 return (-1);
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02003046
3047 /*
3048 * assume identical strings but not same pointer are different
3049 * atoms, needed for non-determinism detection
3050 * That way if 2 elements with the same name are in a choice
3051 * branch the automata is found non-deterministic and
3052 * we fallback to the normal validation which does the right
3053 * thing of exploring both choices.
3054 */
3055 xmlAutomataSetFlags(ctxt->am, 1);
3056
Daniel Veillard4c004142003-10-07 11:33:24 +00003057 ctxt->state = xmlAutomataGetInitState(ctxt->am);
3058 while (list != NULL) {
3059 xmlRelaxNGCompile(ctxt, list);
3060 list = list->next;
3061 }
3062 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
Noam9313ae82012-05-15 11:03:46 +08003063 if (xmlAutomataIsDeterminist(ctxt->am))
3064 def->contModel = xmlAutomataCompile(ctxt->am);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003065
Daniel Veillard4c004142003-10-07 11:33:24 +00003066 xmlFreeAutomata(ctxt->am);
3067 ctxt->state = oldstate;
3068 ctxt->am = oldam;
3069 }
3070 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003071 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003072 if ((ctxt->am != NULL) && (def->name != NULL)) {
3073 ctxt->state = xmlAutomataNewTransition2(ctxt->am,
3074 ctxt->state, NULL,
3075 def->name, def->ns,
3076 def);
3077 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003078 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003079 xmlAutomataPtr oldam = ctxt->am;
3080 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003081
3082 def->depth = -25;
3083
Daniel Veillard4c004142003-10-07 11:33:24 +00003084 list = def->content;
3085 ctxt->am = xmlNewAutomata();
3086 if (ctxt->am == NULL)
3087 return (-1);
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02003088 xmlAutomataSetFlags(ctxt->am, 1);
Daniel Veillard4c004142003-10-07 11:33:24 +00003089 ctxt->state = xmlAutomataGetInitState(ctxt->am);
3090 while (list != NULL) {
3091 xmlRelaxNGCompile(ctxt, list);
3092 list = list->next;
3093 }
3094 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
3095 def->contModel = xmlAutomataCompile(ctxt->am);
3096 if (!xmlRegexpIsDeterminist(def->contModel)) {
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02003097#ifdef DEBUG_COMPILE
3098 xmlGenericError(xmlGenericErrorContext,
3099 "Content model not determinist %s\n",
3100 def->name);
3101#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00003102 /*
3103 * we can only use the automata if it is determinist
3104 */
3105 xmlRegFreeRegexp(def->contModel);
3106 def->contModel = NULL;
3107 }
3108 xmlFreeAutomata(ctxt->am);
3109 ctxt->state = oldstate;
3110 ctxt->am = oldam;
3111 } else {
3112 xmlAutomataPtr oldam = ctxt->am;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003113
Daniel Veillard4c004142003-10-07 11:33:24 +00003114 /*
3115 * we can't build the content model for this element content
3116 * but it still might be possible to build it for some of its
3117 * children, recurse.
3118 */
3119 ret = xmlRelaxNGTryCompile(ctxt, def);
3120 ctxt->am = oldam;
3121 }
3122 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003123 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00003124 ret = xmlRelaxNGCompile(ctxt, def->content);
3125 break;
3126 case XML_RELAXNG_OPTIONAL:{
3127 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003128
Daniel Veillardfd780772009-08-26 18:35:29 +02003129 list = def->content;
3130 while (list != NULL) {
3131 xmlRelaxNGCompile(ctxt, list);
3132 list = list->next;
3133 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003134 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
3135 break;
3136 }
3137 case XML_RELAXNG_ZEROORMORE:{
3138 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003139
Daniel Veillard4c004142003-10-07 11:33:24 +00003140 ctxt->state =
3141 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3142 oldstate = ctxt->state;
3143 list = def->content;
3144 while (list != NULL) {
3145 xmlRelaxNGCompile(ctxt, list);
3146 list = list->next;
3147 }
3148 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
3149 ctxt->state =
3150 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3151 break;
3152 }
3153 case XML_RELAXNG_ONEORMORE:{
3154 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003155
Daniel Veillard4c004142003-10-07 11:33:24 +00003156 list = def->content;
3157 while (list != NULL) {
3158 xmlRelaxNGCompile(ctxt, list);
3159 list = list->next;
3160 }
3161 oldstate = ctxt->state;
3162 list = def->content;
3163 while (list != NULL) {
3164 xmlRelaxNGCompile(ctxt, list);
3165 list = list->next;
3166 }
3167 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
3168 ctxt->state =
3169 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3170 break;
3171 }
3172 case XML_RELAXNG_CHOICE:{
3173 xmlAutomataStatePtr target = NULL;
3174 xmlAutomataStatePtr oldstate = ctxt->state;
3175
3176 list = def->content;
3177 while (list != NULL) {
3178 ctxt->state = oldstate;
3179 ret = xmlRelaxNGCompile(ctxt, list);
3180 if (ret != 0)
3181 break;
3182 if (target == NULL)
3183 target = ctxt->state;
3184 else {
3185 xmlAutomataNewEpsilon(ctxt->am, ctxt->state,
3186 target);
3187 }
3188 list = list->next;
3189 }
3190 ctxt->state = target;
3191
3192 break;
3193 }
Daniel Veillard2134ab12003-07-23 19:56:29 +00003194 case XML_RELAXNG_REF:
3195 case XML_RELAXNG_EXTERNALREF:
3196 case XML_RELAXNG_PARENTREF:
Daniel Veillard52b48c72003-04-13 19:53:42 +00003197 case XML_RELAXNG_GROUP:
3198 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00003199 list = def->content;
3200 while (list != NULL) {
3201 ret = xmlRelaxNGCompile(ctxt, list);
3202 if (ret != 0)
3203 break;
3204 list = list->next;
3205 }
3206 break;
3207 case XML_RELAXNG_TEXT:{
3208 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003209
Daniel Veillard4c004142003-10-07 11:33:24 +00003210 ctxt->state =
3211 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3212 oldstate = ctxt->state;
3213 xmlRelaxNGCompile(ctxt, def->content);
3214 xmlAutomataNewTransition(ctxt->am, ctxt->state,
3215 ctxt->state, BAD_CAST "#text",
3216 NULL);
3217 ctxt->state =
3218 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3219 break;
3220 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003221 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00003222 ctxt->state =
3223 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3224 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003225 case XML_RELAXNG_EXCEPT:
3226 case XML_RELAXNG_ATTRIBUTE:
3227 case XML_RELAXNG_INTERLEAVE:
3228 case XML_RELAXNG_NOT_ALLOWED:
3229 case XML_RELAXNG_DATATYPE:
3230 case XML_RELAXNG_LIST:
3231 case XML_RELAXNG_PARAM:
3232 case XML_RELAXNG_VALUE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003233 /* This should not happen and generate an internal error */
3234 fprintf(stderr, "RNG internal error trying to compile %s\n",
3235 xmlRelaxNGDefName(def));
3236 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003237 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003238 return (ret);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003239}
3240
3241/**
3242 * xmlRelaxNGTryCompile:
3243 * ctxt: the RelaxNG parser context
3244 * @define: the definition tree to compile
3245 *
3246 * Try to compile the set of definitions, it works recursively,
3247 * possibly ignoring parts which cannot be compiled.
3248 *
3249 * Returns 0 if success and -1 in case of error
3250 */
3251static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003252xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
3253{
Daniel Veillard52b48c72003-04-13 19:53:42 +00003254 int ret = 0;
3255 xmlRelaxNGDefinePtr list;
3256
Daniel Veillard4c004142003-10-07 11:33:24 +00003257 if ((ctxt == NULL) || (def == NULL))
3258 return (-1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003259
3260 if ((def->type == XML_RELAXNG_START) ||
3261 (def->type == XML_RELAXNG_ELEMENT)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003262 ret = xmlRelaxNGIsCompileable(def);
3263 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
3264 ctxt->am = NULL;
3265 ret = xmlRelaxNGCompile(ctxt, def);
Daniel Veillard2134ab12003-07-23 19:56:29 +00003266#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00003267 if (ret == 0) {
3268 if (def->type == XML_RELAXNG_START)
3269 xmlGenericError(xmlGenericErrorContext,
3270 "compiled the start\n");
3271 else
3272 xmlGenericError(xmlGenericErrorContext,
3273 "compiled element %s\n", def->name);
3274 } else {
3275 if (def->type == XML_RELAXNG_START)
3276 xmlGenericError(xmlGenericErrorContext,
3277 "failed to compile the start\n");
3278 else
3279 xmlGenericError(xmlGenericErrorContext,
3280 "failed to compile element %s\n",
3281 def->name);
3282 }
Daniel Veillard2134ab12003-07-23 19:56:29 +00003283#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00003284 return (ret);
3285 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003286 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003287 switch (def->type) {
Daniel Veillard52b48c72003-04-13 19:53:42 +00003288 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00003289 ret = xmlRelaxNGTryCompile(ctxt, def->content);
3290 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003291 case XML_RELAXNG_TEXT:
3292 case XML_RELAXNG_DATATYPE:
3293 case XML_RELAXNG_LIST:
3294 case XML_RELAXNG_PARAM:
3295 case XML_RELAXNG_VALUE:
3296 case XML_RELAXNG_EMPTY:
3297 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003298 ret = 0;
3299 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003300 case XML_RELAXNG_OPTIONAL:
3301 case XML_RELAXNG_ZEROORMORE:
3302 case XML_RELAXNG_ONEORMORE:
3303 case XML_RELAXNG_CHOICE:
3304 case XML_RELAXNG_GROUP:
3305 case XML_RELAXNG_DEF:
Daniel Veillard2134ab12003-07-23 19:56:29 +00003306 case XML_RELAXNG_START:
3307 case XML_RELAXNG_REF:
3308 case XML_RELAXNG_EXTERNALREF:
3309 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00003310 list = def->content;
3311 while (list != NULL) {
3312 ret = xmlRelaxNGTryCompile(ctxt, list);
3313 if (ret != 0)
3314 break;
3315 list = list->next;
3316 }
3317 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003318 case XML_RELAXNG_EXCEPT:
3319 case XML_RELAXNG_ATTRIBUTE:
3320 case XML_RELAXNG_INTERLEAVE:
3321 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillard4c004142003-10-07 11:33:24 +00003322 ret = 0;
3323 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003324 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003325 return (ret);
Daniel Veillard952379b2003-03-17 15:37:12 +00003326}
3327
3328/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003329 * *
3330 * Parsing functions *
3331 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00003332 ************************************************************************/
3333
Daniel Veillard4c004142003-10-07 11:33:24 +00003334static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr
3335 ctxt, xmlNodePtr node);
3336static xmlRelaxNGDefinePtr xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr
3337 ctxt, xmlNodePtr node);
3338static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr
3339 ctxt, xmlNodePtr nodes,
3340 int group);
3341static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr
3342 ctxt, xmlNodePtr node);
3343static xmlRelaxNGPtr xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt,
3344 xmlNodePtr node);
3345static int xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
3346 xmlNodePtr nodes);
3347static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr
3348 ctxt, xmlNodePtr node,
3349 xmlRelaxNGDefinePtr
3350 def);
3351static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr
3352 ctxt, xmlNodePtr nodes);
3353static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
3354 xmlRelaxNGDefinePtr define,
3355 xmlNodePtr elem);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003356
3357
Daniel Veillard249d7bb2003-03-19 21:02:29 +00003358#define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content))
Daniel Veillard6eadf632003-01-23 18:29:16 +00003359
3360/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00003361 * xmlRelaxNGIsNullable:
3362 * @define: the definition to verify
3363 *
3364 * Check if a definition is nullable.
3365 *
3366 * Returns 1 if yes, 0 if no and -1 in case of error
3367 */
3368static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003369xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define)
3370{
Daniel Veillardfd573f12003-03-16 17:52:32 +00003371 int ret;
Daniel Veillard4c004142003-10-07 11:33:24 +00003372
Daniel Veillardfd573f12003-03-16 17:52:32 +00003373 if (define == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003374 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003375
Daniel Veillarde063f482003-03-21 16:53:17 +00003376 if (define->dflags & IS_NULLABLE)
Daniel Veillard4c004142003-10-07 11:33:24 +00003377 return (1);
Daniel Veillarde063f482003-03-21 16:53:17 +00003378 if (define->dflags & IS_NOT_NULLABLE)
Daniel Veillard4c004142003-10-07 11:33:24 +00003379 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003380 switch (define->type) {
3381 case XML_RELAXNG_EMPTY:
3382 case XML_RELAXNG_TEXT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003383 ret = 1;
3384 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003385 case XML_RELAXNG_NOOP:
3386 case XML_RELAXNG_DEF:
3387 case XML_RELAXNG_REF:
3388 case XML_RELAXNG_EXTERNALREF:
3389 case XML_RELAXNG_PARENTREF:
3390 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003391 ret = xmlRelaxNGIsNullable(define->content);
3392 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003393 case XML_RELAXNG_EXCEPT:
3394 case XML_RELAXNG_NOT_ALLOWED:
3395 case XML_RELAXNG_ELEMENT:
3396 case XML_RELAXNG_DATATYPE:
3397 case XML_RELAXNG_PARAM:
3398 case XML_RELAXNG_VALUE:
3399 case XML_RELAXNG_LIST:
3400 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003401 ret = 0;
3402 break;
3403 case XML_RELAXNG_CHOICE:{
3404 xmlRelaxNGDefinePtr list = define->content;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003405
Daniel Veillard4c004142003-10-07 11:33:24 +00003406 while (list != NULL) {
3407 ret = xmlRelaxNGIsNullable(list);
3408 if (ret != 0)
3409 goto done;
3410 list = list->next;
3411 }
3412 ret = 0;
3413 break;
3414 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003415 case XML_RELAXNG_START:
3416 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003417 case XML_RELAXNG_GROUP:{
3418 xmlRelaxNGDefinePtr list = define->content;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003419
Daniel Veillard4c004142003-10-07 11:33:24 +00003420 while (list != NULL) {
3421 ret = xmlRelaxNGIsNullable(list);
3422 if (ret != 1)
3423 goto done;
3424 list = list->next;
3425 }
3426 return (1);
3427 }
3428 default:
3429 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003430 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003431 done:
Daniel Veillardfd573f12003-03-16 17:52:32 +00003432 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003433 define->dflags |= IS_NOT_NULLABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003434 if (ret == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00003435 define->dflags |= IS_NULLABLE;
3436 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003437}
3438
3439/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00003440 * xmlRelaxNGIsBlank:
3441 * @str: a string
3442 *
3443 * Check if a string is ignorable c.f. 4.2. Whitespace
3444 *
3445 * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
3446 */
3447static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003448xmlRelaxNGIsBlank(xmlChar * str)
3449{
Daniel Veillard6eadf632003-01-23 18:29:16 +00003450 if (str == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003451 return (1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003452 while (*str != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00003453 if (!(IS_BLANK_CH(*str)))
Daniel Veillard4c004142003-10-07 11:33:24 +00003454 return (0);
3455 str++;
Daniel Veillard6eadf632003-01-23 18:29:16 +00003456 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003457 return (1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003458}
3459
Daniel Veillard6eadf632003-01-23 18:29:16 +00003460/**
3461 * xmlRelaxNGGetDataTypeLibrary:
3462 * @ctxt: a Relax-NG parser context
3463 * @node: the current data or value element
3464 *
3465 * Applies algorithm from 4.3. datatypeLibrary attribute
3466 *
3467 * Returns the datatypeLibary value or NULL if not found
3468 */
3469static xmlChar *
3470xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00003471 xmlNodePtr node)
3472{
Daniel Veillard6eadf632003-01-23 18:29:16 +00003473 xmlChar *ret, *escape;
3474
Daniel Veillardd44b9362009-09-07 12:15:08 +02003475 if (node == NULL)
3476 return(NULL);
3477
Daniel Veillard6eadf632003-01-23 18:29:16 +00003478 if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003479 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3480 if (ret != NULL) {
3481 if (ret[0] == 0) {
3482 xmlFree(ret);
3483 return (NULL);
3484 }
3485 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3486 if (escape == NULL) {
3487 return (ret);
3488 }
3489 xmlFree(ret);
3490 return (escape);
3491 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00003492 }
3493 node = node->parent;
3494 while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003495 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3496 if (ret != NULL) {
3497 if (ret[0] == 0) {
3498 xmlFree(ret);
3499 return (NULL);
3500 }
3501 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3502 if (escape == NULL) {
3503 return (ret);
3504 }
3505 xmlFree(ret);
3506 return (escape);
3507 }
3508 node = node->parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00003509 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003510 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003511}
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003512
3513/**
Daniel Veillardedc91922003-01-26 00:52:04 +00003514 * xmlRelaxNGParseValue:
3515 * @ctxt: a Relax-NG parser context
3516 * @node: the data node.
3517 *
3518 * parse the content of a RelaxNG value node.
3519 *
3520 * Returns the definition pointer or NULL in case of error
3521 */
3522static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00003523xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
3524{
Daniel Veillardedc91922003-01-26 00:52:04 +00003525 xmlRelaxNGDefinePtr def = NULL;
Daniel Veillard5f1946a2003-03-31 16:38:16 +00003526 xmlRelaxNGTypeLibraryPtr lib = NULL;
Daniel Veillardedc91922003-01-26 00:52:04 +00003527 xmlChar *type;
3528 xmlChar *library;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003529 int success = 0;
Daniel Veillardedc91922003-01-26 00:52:04 +00003530
Daniel Veillardfd573f12003-03-16 17:52:32 +00003531 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillardedc91922003-01-26 00:52:04 +00003532 if (def == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003533 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003534 def->type = XML_RELAXNG_VALUE;
Daniel Veillardedc91922003-01-26 00:52:04 +00003535
3536 type = xmlGetProp(node, BAD_CAST "type");
3537 if (type != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003538 xmlRelaxNGNormExtSpace(type);
3539 if (xmlValidateNCName(type, 0)) {
3540 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE,
3541 "value type '%s' is not an NCName\n", type, NULL);
3542 }
3543 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3544 if (library == NULL)
3545 library =
3546 xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
Daniel Veillardedc91922003-01-26 00:52:04 +00003547
Daniel Veillard4c004142003-10-07 11:33:24 +00003548 def->name = type;
3549 def->ns = library;
Daniel Veillardedc91922003-01-26 00:52:04 +00003550
Daniel Veillard4c004142003-10-07 11:33:24 +00003551 lib = (xmlRelaxNGTypeLibraryPtr)
3552 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
3553 if (lib == NULL) {
3554 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
3555 "Use of unregistered type library '%s'\n", library,
3556 NULL);
3557 def->data = NULL;
3558 } else {
3559 def->data = lib;
3560 if (lib->have == NULL) {
3561 xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
3562 "Internal error with type library '%s': no 'have'\n",
3563 library, NULL);
3564 } else {
3565 success = lib->have(lib->data, def->name);
3566 if (success != 1) {
3567 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND,
3568 "Error type '%s' is not exported by type library '%s'\n",
3569 def->name, library);
3570 }
3571 }
3572 }
Daniel Veillardedc91922003-01-26 00:52:04 +00003573 }
3574 if (node->children == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003575 def->value = xmlStrdup(BAD_CAST "");
Daniel Veillard39eb88b2003-03-11 11:21:28 +00003576 } else if (((node->children->type != XML_TEXT_NODE) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00003577 (node->children->type != XML_CDATA_SECTION_NODE)) ||
3578 (node->children->next != NULL)) {
3579 xmlRngPErr(ctxt, node, XML_RNGP_TEXT_EXPECTED,
3580 "Expecting a single text value for <value>content\n",
3581 NULL, NULL);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003582 } else if (def != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003583 def->value = xmlNodeGetContent(node);
3584 if (def->value == NULL) {
3585 xmlRngPErr(ctxt, node, XML_RNGP_VALUE_NO_CONTENT,
3586 "Element <value> has no content\n", NULL, NULL);
3587 } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) {
3588 void *val = NULL;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003589
Daniel Veillard4c004142003-10-07 11:33:24 +00003590 success =
3591 lib->check(lib->data, def->name, def->value, &val, node);
3592 if (success != 1) {
3593 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_VALUE,
3594 "Value '%s' is not acceptable for type '%s'\n",
3595 def->value, def->name);
3596 } else {
3597 if (val != NULL)
3598 def->attrs = val;
3599 }
3600 }
Daniel Veillardedc91922003-01-26 00:52:04 +00003601 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003602 return (def);
Daniel Veillardedc91922003-01-26 00:52:04 +00003603}
3604
3605/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003606 * xmlRelaxNGParseData:
3607 * @ctxt: a Relax-NG parser context
3608 * @node: the data node.
3609 *
3610 * parse the content of a RelaxNG data node.
3611 *
3612 * Returns the definition pointer or NULL in case of error
3613 */
3614static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00003615xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
3616{
Daniel Veillard14b56432006-03-09 18:41:40 +00003617 xmlRelaxNGDefinePtr def = NULL, except;
Daniel Veillard8fe98712003-02-19 00:19:14 +00003618 xmlRelaxNGDefinePtr param, lastparam = NULL;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003619 xmlRelaxNGTypeLibraryPtr lib;
3620 xmlChar *type;
3621 xmlChar *library;
3622 xmlNodePtr content;
3623 int tmp;
3624
3625 type = xmlGetProp(node, BAD_CAST "type");
3626 if (type == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003627 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_MISSING, "data has no type\n", NULL,
3628 NULL);
3629 return (NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003630 }
Daniel Veillardd2298792003-02-14 16:54:11 +00003631 xmlRelaxNGNormExtSpace(type);
3632 if (xmlValidateNCName(type, 0)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003633 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE,
3634 "data type '%s' is not an NCName\n", type, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00003635 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003636 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3637 if (library == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003638 library =
3639 xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003640
Daniel Veillardfd573f12003-03-16 17:52:32 +00003641 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003642 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003643 xmlFree(type);
3644 return (NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003645 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003646 def->type = XML_RELAXNG_DATATYPE;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003647 def->name = type;
3648 def->ns = library;
3649
3650 lib = (xmlRelaxNGTypeLibraryPtr)
Daniel Veillard4c004142003-10-07 11:33:24 +00003651 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003652 if (lib == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003653 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
3654 "Use of unregistered type library '%s'\n", library,
3655 NULL);
3656 def->data = NULL;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003657 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003658 def->data = lib;
3659 if (lib->have == NULL) {
3660 xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
3661 "Internal error with type library '%s': no 'have'\n",
3662 library, NULL);
3663 } else {
3664 tmp = lib->have(lib->data, def->name);
3665 if (tmp != 1) {
3666 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND,
3667 "Error type '%s' is not exported by type library '%s'\n",
3668 def->name, library);
3669 } else
3670 if ((xmlStrEqual
3671 (library,
3672 BAD_CAST
3673 "http://www.w3.org/2001/XMLSchema-datatypes"))
3674 && ((xmlStrEqual(def->name, BAD_CAST "IDREF"))
3675 || (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) {
3676 ctxt->idref = 1;
3677 }
3678 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003679 }
3680 content = node->children;
Daniel Veillard416589a2003-02-17 17:25:42 +00003681
3682 /*
3683 * Handle optional params
3684 */
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003685 while (content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003686 if (!xmlStrEqual(content->name, BAD_CAST "param"))
3687 break;
3688 if (xmlStrEqual(library,
3689 BAD_CAST "http://relaxng.org/ns/structure/1.0")) {
3690 xmlRngPErr(ctxt, node, XML_RNGP_PARAM_FORBIDDEN,
3691 "Type library '%s' does not allow type parameters\n",
3692 library, NULL);
3693 content = content->next;
3694 while ((content != NULL) &&
3695 (xmlStrEqual(content->name, BAD_CAST "param")))
3696 content = content->next;
3697 } else {
3698 param = xmlRelaxNGNewDefine(ctxt, node);
3699 if (param != NULL) {
3700 param->type = XML_RELAXNG_PARAM;
3701 param->name = xmlGetProp(content, BAD_CAST "name");
3702 if (param->name == NULL) {
3703 xmlRngPErr(ctxt, node, XML_RNGP_PARAM_NAME_MISSING,
3704 "param has no name\n", NULL, NULL);
3705 }
3706 param->value = xmlNodeGetContent(content);
3707 if (lastparam == NULL) {
3708 def->attrs = lastparam = param;
3709 } else {
3710 lastparam->next = param;
3711 lastparam = param;
3712 }
3713 if (lib != NULL) {
3714 }
3715 }
3716 content = content->next;
3717 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003718 }
Daniel Veillard416589a2003-02-17 17:25:42 +00003719 /*
3720 * Handle optional except
3721 */
Daniel Veillard4c004142003-10-07 11:33:24 +00003722 if ((content != NULL)
3723 && (xmlStrEqual(content->name, BAD_CAST "except"))) {
3724 xmlNodePtr child;
Daniel Veillard14b56432006-03-09 18:41:40 +00003725 xmlRelaxNGDefinePtr tmp2, last = NULL;
Daniel Veillard416589a2003-02-17 17:25:42 +00003726
Daniel Veillard4c004142003-10-07 11:33:24 +00003727 except = xmlRelaxNGNewDefine(ctxt, node);
3728 if (except == NULL) {
3729 return (def);
3730 }
3731 except->type = XML_RELAXNG_EXCEPT;
3732 child = content->children;
Daniel Veillard14b56432006-03-09 18:41:40 +00003733 def->content = except;
Daniel Veillard4c004142003-10-07 11:33:24 +00003734 if (child == NULL) {
3735 xmlRngPErr(ctxt, content, XML_RNGP_EXCEPT_NO_CONTENT,
3736 "except has no content\n", NULL, NULL);
3737 }
3738 while (child != NULL) {
3739 tmp2 = xmlRelaxNGParsePattern(ctxt, child);
3740 if (tmp2 != NULL) {
Daniel Veillard14b56432006-03-09 18:41:40 +00003741 if (last == NULL) {
3742 except->content = last = tmp2;
Daniel Veillard4c004142003-10-07 11:33:24 +00003743 } else {
Daniel Veillard14b56432006-03-09 18:41:40 +00003744 last->next = tmp2;
3745 last = tmp2;
Daniel Veillard4c004142003-10-07 11:33:24 +00003746 }
3747 }
3748 child = child->next;
3749 }
3750 content = content->next;
Daniel Veillard416589a2003-02-17 17:25:42 +00003751 }
3752 /*
3753 * Check there is no unhandled data
3754 */
3755 if (content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003756 xmlRngPErr(ctxt, content, XML_RNGP_DATA_CONTENT,
3757 "Element data has unexpected content %s\n",
3758 content->name, NULL);
Daniel Veillard416589a2003-02-17 17:25:42 +00003759 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003760
Daniel Veillard4c004142003-10-07 11:33:24 +00003761 return (def);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003762}
3763
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003764static const xmlChar *invalidName = BAD_CAST "\1";
3765
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003766/**
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003767 * xmlRelaxNGCompareNameClasses:
3768 * @defs1: the first element/attribute defs
3769 * @defs2: the second element/attribute defs
3770 * @name: the restriction on the name
3771 * @ns: the restriction on the namespace
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003772 *
3773 * Compare the 2 lists of element definitions. The comparison is
3774 * that if both lists do not accept the same QNames, it returns 1
3775 * If the 2 lists can accept the same QName the comparison returns 0
3776 *
3777 * Returns 1 disttinct, 0 if equal
3778 */
3779static int
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003780xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1,
Daniel Veillard4c004142003-10-07 11:33:24 +00003781 xmlRelaxNGDefinePtr def2)
3782{
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003783 int ret = 1;
3784 xmlNode node;
3785 xmlNs ns;
3786 xmlRelaxNGValidCtxt ctxt;
Daniel Veillard4c004142003-10-07 11:33:24 +00003787
Daniel Veillard42f12e92003-03-07 18:32:59 +00003788 memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt));
3789
Daniel Veillardb30ca312005-09-04 13:50:03 +00003790 ctxt.flags = FLAGS_IGNORABLE | FLAGS_NOERROR;
3791
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003792 if ((def1->type == XML_RELAXNG_ELEMENT) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00003793 (def1->type == XML_RELAXNG_ATTRIBUTE)) {
3794 if (def2->type == XML_RELAXNG_TEXT)
3795 return (1);
3796 if (def1->name != NULL) {
3797 node.name = def1->name;
3798 } else {
3799 node.name = invalidName;
3800 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003801 if (def1->ns != NULL) {
3802 if (def1->ns[0] == 0) {
3803 node.ns = NULL;
3804 } else {
William M. Bracka74a6ff2004-04-02 14:03:22 +00003805 node.ns = &ns;
Daniel Veillard4c004142003-10-07 11:33:24 +00003806 ns.href = def1->ns;
3807 }
3808 } else {
William M. Bracka74a6ff2004-04-02 14:03:22 +00003809 node.ns = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00003810 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003811 if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003812 if (def1->nameClass != NULL) {
3813 ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2);
3814 } else {
3815 ret = 0;
3816 }
3817 } else {
3818 ret = 1;
3819 }
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003820 } else if (def1->type == XML_RELAXNG_TEXT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003821 if (def2->type == XML_RELAXNG_TEXT)
3822 return (0);
3823 return (1);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003824 } else if (def1->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003825 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003826 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003827 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003828 }
3829 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003830 return (ret);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003831 if ((def2->type == XML_RELAXNG_ELEMENT) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00003832 (def2->type == XML_RELAXNG_ATTRIBUTE)) {
3833 if (def2->name != NULL) {
3834 node.name = def2->name;
3835 } else {
3836 node.name = invalidName;
3837 }
3838 node.ns = &ns;
3839 if (def2->ns != NULL) {
3840 if (def2->ns[0] == 0) {
3841 node.ns = NULL;
3842 } else {
3843 ns.href = def2->ns;
3844 }
3845 } else {
3846 ns.href = invalidName;
3847 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003848 if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003849 if (def2->nameClass != NULL) {
3850 ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1);
3851 } else {
3852 ret = 0;
3853 }
3854 } else {
3855 ret = 1;
3856 }
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003857 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003858 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003859 }
3860
Daniel Veillard4c004142003-10-07 11:33:24 +00003861 return (ret);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003862}
3863
3864/**
3865 * xmlRelaxNGCompareElemDefLists:
3866 * @ctxt: a Relax-NG parser context
3867 * @defs1: the first list of element/attribute defs
3868 * @defs2: the second list of element/attribute defs
3869 *
3870 * Compare the 2 lists of element or attribute definitions. The comparison
3871 * is that if both lists do not accept the same QNames, it returns 1
3872 * If the 2 lists can accept the same QName the comparison returns 0
3873 *
3874 * Returns 1 disttinct, 0 if equal
3875 */
3876static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003877xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt
3878 ATTRIBUTE_UNUSED, xmlRelaxNGDefinePtr * def1,
3879 xmlRelaxNGDefinePtr * def2)
3880{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003881 xmlRelaxNGDefinePtr *basedef2 = def2;
Daniel Veillard4c004142003-10-07 11:33:24 +00003882
Daniel Veillard154877e2003-01-30 12:17:05 +00003883 if ((def1 == NULL) || (def2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00003884 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003885 if ((*def1 == NULL) || (*def2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00003886 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003887 while (*def1 != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003888 while ((*def2) != NULL) {
3889 if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0)
3890 return (0);
3891 def2++;
3892 }
3893 def2 = basedef2;
3894 def1++;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003895 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003896 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003897}
3898
3899/**
Daniel Veillardce192eb2003-04-16 15:58:05 +00003900 * xmlRelaxNGGenerateAttributes:
3901 * @ctxt: a Relax-NG parser context
3902 * @def: the definition definition
3903 *
3904 * Check if the definition can only generate attributes
3905 *
3906 * Returns 1 if yes, 0 if no and -1 in case of error.
3907 */
3908static int
3909xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00003910 xmlRelaxNGDefinePtr def)
3911{
Daniel Veillardce192eb2003-04-16 15:58:05 +00003912 xmlRelaxNGDefinePtr parent, cur, tmp;
3913
3914 /*
3915 * Don't run that check in case of error. Infinite recursion
3916 * becomes possible.
3917 */
3918 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003919 return (-1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003920
3921 parent = NULL;
3922 cur = def;
3923 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003924 if ((cur->type == XML_RELAXNG_ELEMENT) ||
3925 (cur->type == XML_RELAXNG_TEXT) ||
3926 (cur->type == XML_RELAXNG_DATATYPE) ||
3927 (cur->type == XML_RELAXNG_PARAM) ||
3928 (cur->type == XML_RELAXNG_LIST) ||
3929 (cur->type == XML_RELAXNG_VALUE) ||
3930 (cur->type == XML_RELAXNG_EMPTY))
3931 return (0);
3932 if ((cur->type == XML_RELAXNG_CHOICE) ||
3933 (cur->type == XML_RELAXNG_INTERLEAVE) ||
3934 (cur->type == XML_RELAXNG_GROUP) ||
3935 (cur->type == XML_RELAXNG_ONEORMORE) ||
3936 (cur->type == XML_RELAXNG_ZEROORMORE) ||
3937 (cur->type == XML_RELAXNG_OPTIONAL) ||
3938 (cur->type == XML_RELAXNG_PARENTREF) ||
3939 (cur->type == XML_RELAXNG_EXTERNALREF) ||
3940 (cur->type == XML_RELAXNG_REF) ||
3941 (cur->type == XML_RELAXNG_DEF)) {
3942 if (cur->content != NULL) {
3943 parent = cur;
3944 cur = cur->content;
3945 tmp = cur;
3946 while (tmp != NULL) {
3947 tmp->parent = parent;
3948 tmp = tmp->next;
3949 }
3950 continue;
3951 }
3952 }
3953 if (cur == def)
3954 break;
3955 if (cur->next != NULL) {
3956 cur = cur->next;
3957 continue;
3958 }
3959 do {
3960 cur = cur->parent;
3961 if (cur == NULL)
3962 break;
3963 if (cur == def)
3964 return (1);
3965 if (cur->next != NULL) {
3966 cur = cur->next;
3967 break;
3968 }
3969 } while (cur != NULL);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003970 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003971 return (1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003972}
Daniel Veillard4c004142003-10-07 11:33:24 +00003973
Daniel Veillardce192eb2003-04-16 15:58:05 +00003974/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003975 * xmlRelaxNGGetElements:
3976 * @ctxt: a Relax-NG parser context
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003977 * @def: the definition definition
3978 * @eora: gather elements (0) or attributes (1)
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003979 *
3980 * Compute the list of top elements a definition can generate
3981 *
3982 * Returns a list of elements or NULL if none was found.
3983 */
3984static xmlRelaxNGDefinePtr *
3985xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00003986 xmlRelaxNGDefinePtr def, int eora)
3987{
Daniel Veillardfd573f12003-03-16 17:52:32 +00003988 xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003989 int len = 0;
3990 int max = 0;
3991
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003992 /*
3993 * Don't run that check in case of error. Infinite recursion
3994 * becomes possible.
3995 */
3996 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003997 return (NULL);
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003998
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003999 parent = NULL;
4000 cur = def;
4001 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004002 if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) ||
4003 (cur->type == XML_RELAXNG_TEXT))) ||
4004 ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) {
4005 if (ret == NULL) {
4006 max = 10;
4007 ret = (xmlRelaxNGDefinePtr *)
4008 xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr));
4009 if (ret == NULL) {
4010 xmlRngPErrMemory(ctxt, "getting element list\n");
4011 return (NULL);
4012 }
4013 } else if (max <= len) {
Daniel Veillard079f6a72004-09-23 13:15:03 +00004014 xmlRelaxNGDefinePtr *temp;
4015
Daniel Veillard4c004142003-10-07 11:33:24 +00004016 max *= 2;
Daniel Veillard079f6a72004-09-23 13:15:03 +00004017 temp = xmlRealloc(ret,
Daniel Veillard4c004142003-10-07 11:33:24 +00004018 (max + 1) * sizeof(xmlRelaxNGDefinePtr));
Daniel Veillard079f6a72004-09-23 13:15:03 +00004019 if (temp == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004020 xmlRngPErrMemory(ctxt, "getting element list\n");
Daniel Veillard079f6a72004-09-23 13:15:03 +00004021 xmlFree(ret);
Daniel Veillard4c004142003-10-07 11:33:24 +00004022 return (NULL);
4023 }
Daniel Veillard079f6a72004-09-23 13:15:03 +00004024 ret = temp;
Daniel Veillard4c004142003-10-07 11:33:24 +00004025 }
4026 ret[len++] = cur;
4027 ret[len] = NULL;
4028 } else if ((cur->type == XML_RELAXNG_CHOICE) ||
4029 (cur->type == XML_RELAXNG_INTERLEAVE) ||
4030 (cur->type == XML_RELAXNG_GROUP) ||
4031 (cur->type == XML_RELAXNG_ONEORMORE) ||
4032 (cur->type == XML_RELAXNG_ZEROORMORE) ||
4033 (cur->type == XML_RELAXNG_OPTIONAL) ||
4034 (cur->type == XML_RELAXNG_PARENTREF) ||
4035 (cur->type == XML_RELAXNG_REF) ||
William M. Brack236c8c02004-03-20 11:32:36 +00004036 (cur->type == XML_RELAXNG_DEF) ||
4037 (cur->type == XML_RELAXNG_EXTERNALREF)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004038 /*
4039 * Don't go within elements or attributes or string values.
4040 * Just gather the element top list
4041 */
4042 if (cur->content != NULL) {
4043 parent = cur;
4044 cur = cur->content;
4045 tmp = cur;
4046 while (tmp != NULL) {
4047 tmp->parent = parent;
4048 tmp = tmp->next;
4049 }
4050 continue;
4051 }
4052 }
4053 if (cur == def)
4054 break;
4055 if (cur->next != NULL) {
4056 cur = cur->next;
4057 continue;
4058 }
4059 do {
4060 cur = cur->parent;
4061 if (cur == NULL)
4062 break;
4063 if (cur == def)
4064 return (ret);
4065 if (cur->next != NULL) {
4066 cur = cur->next;
4067 break;
4068 }
4069 } while (cur != NULL);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004070 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004071 return (ret);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004072}
Daniel Veillard4c004142003-10-07 11:33:24 +00004073
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004074/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00004075 * xmlRelaxNGCheckChoiceDeterminism:
4076 * @ctxt: a Relax-NG parser context
4077 * @def: the choice definition
4078 *
4079 * Also used to find indeterministic pattern in choice
4080 */
4081static void
4082xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00004083 xmlRelaxNGDefinePtr def)
4084{
Daniel Veillardfd573f12003-03-16 17:52:32 +00004085 xmlRelaxNGDefinePtr **list;
4086 xmlRelaxNGDefinePtr cur;
4087 int nbchild = 0, i, j, ret;
4088 int is_nullable = 0;
4089 int is_indeterminist = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004090 xmlHashTablePtr triage = NULL;
4091 int is_triable = 1;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004092
Daniel Veillard4c004142003-10-07 11:33:24 +00004093 if ((def == NULL) || (def->type != XML_RELAXNG_CHOICE))
4094 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004095
Daniel Veillarde063f482003-03-21 16:53:17 +00004096 if (def->dflags & IS_PROCESSED)
Daniel Veillard4c004142003-10-07 11:33:24 +00004097 return;
Daniel Veillarde063f482003-03-21 16:53:17 +00004098
Daniel Veillardfd573f12003-03-16 17:52:32 +00004099 /*
4100 * Don't run that check in case of error. Infinite recursion
4101 * becomes possible.
4102 */
4103 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004104 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004105
4106 is_nullable = xmlRelaxNGIsNullable(def);
4107
4108 cur = def->content;
4109 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004110 nbchild++;
4111 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004112 }
4113
4114 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
Daniel Veillard4c004142003-10-07 11:33:24 +00004115 sizeof(xmlRelaxNGDefinePtr
4116 *));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004117 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004118 xmlRngPErrMemory(ctxt, "building choice\n");
4119 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004120 }
4121 i = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004122 /*
4123 * a bit strong but safe
4124 */
4125 if (is_nullable == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004126 triage = xmlHashCreate(10);
Daniel Veillarde063f482003-03-21 16:53:17 +00004127 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004128 is_triable = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004129 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004130 cur = def->content;
4131 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004132 list[i] = xmlRelaxNGGetElements(ctxt, cur, 0);
4133 if ((list[i] == NULL) || (list[i][0] == NULL)) {
4134 is_triable = 0;
4135 } else if (is_triable == 1) {
4136 xmlRelaxNGDefinePtr *tmp;
4137 int res;
Daniel Veillarde063f482003-03-21 16:53:17 +00004138
Daniel Veillard4c004142003-10-07 11:33:24 +00004139 tmp = list[i];
4140 while ((*tmp != NULL) && (is_triable == 1)) {
4141 if ((*tmp)->type == XML_RELAXNG_TEXT) {
4142 res = xmlHashAddEntry2(triage,
4143 BAD_CAST "#text", NULL,
4144 (void *) cur);
4145 if (res != 0)
4146 is_triable = -1;
4147 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
4148 ((*tmp)->name != NULL)) {
4149 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4150 res = xmlHashAddEntry2(triage,
4151 (*tmp)->name, NULL,
4152 (void *) cur);
4153 else
4154 res = xmlHashAddEntry2(triage,
4155 (*tmp)->name, (*tmp)->ns,
4156 (void *) cur);
4157 if (res != 0)
4158 is_triable = -1;
4159 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
4160 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4161 res = xmlHashAddEntry2(triage,
4162 BAD_CAST "#any", NULL,
4163 (void *) cur);
4164 else
4165 res = xmlHashAddEntry2(triage,
4166 BAD_CAST "#any", (*tmp)->ns,
4167 (void *) cur);
4168 if (res != 0)
4169 is_triable = -1;
4170 } else {
4171 is_triable = -1;
4172 }
4173 tmp++;
4174 }
4175 }
4176 i++;
4177 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004178 }
4179
Daniel Veillard4c004142003-10-07 11:33:24 +00004180 for (i = 0; i < nbchild; i++) {
4181 if (list[i] == NULL)
4182 continue;
4183 for (j = 0; j < i; j++) {
4184 if (list[j] == NULL)
4185 continue;
4186 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4187 if (ret == 0) {
4188 is_indeterminist = 1;
4189 }
4190 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004191 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004192 for (i = 0; i < nbchild; i++) {
4193 if (list[i] != NULL)
4194 xmlFree(list[i]);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004195 }
4196
4197 xmlFree(list);
4198 if (is_indeterminist) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004199 def->dflags |= IS_INDETERMINIST;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004200 }
Daniel Veillarde063f482003-03-21 16:53:17 +00004201 if (is_triable == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004202 def->dflags |= IS_TRIABLE;
4203 def->data = triage;
Daniel Veillarde063f482003-03-21 16:53:17 +00004204 } else if (triage != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004205 xmlHashFree(triage, NULL);
Daniel Veillarde063f482003-03-21 16:53:17 +00004206 }
4207 def->dflags |= IS_PROCESSED;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004208}
4209
4210/**
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004211 * xmlRelaxNGCheckGroupAttrs:
4212 * @ctxt: a Relax-NG parser context
4213 * @def: the group definition
4214 *
4215 * Detects violations of rule 7.3
4216 */
4217static void
4218xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00004219 xmlRelaxNGDefinePtr def)
4220{
Daniel Veillardfd573f12003-03-16 17:52:32 +00004221 xmlRelaxNGDefinePtr **list;
4222 xmlRelaxNGDefinePtr cur;
4223 int nbchild = 0, i, j, ret;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004224
4225 if ((def == NULL) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00004226 ((def->type != XML_RELAXNG_GROUP) &&
4227 (def->type != XML_RELAXNG_ELEMENT)))
4228 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004229
Daniel Veillarde063f482003-03-21 16:53:17 +00004230 if (def->dflags & IS_PROCESSED)
Daniel Veillard4c004142003-10-07 11:33:24 +00004231 return;
Daniel Veillarde063f482003-03-21 16:53:17 +00004232
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004233 /*
4234 * Don't run that check in case of error. Infinite recursion
4235 * becomes possible.
4236 */
4237 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004238 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004239
Daniel Veillardfd573f12003-03-16 17:52:32 +00004240 cur = def->attrs;
4241 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004242 nbchild++;
4243 cur = cur->next;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004244 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004245 cur = def->content;
4246 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004247 nbchild++;
4248 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004249 }
4250
4251 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
Daniel Veillard4c004142003-10-07 11:33:24 +00004252 sizeof(xmlRelaxNGDefinePtr
4253 *));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004254 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004255 xmlRngPErrMemory(ctxt, "building group\n");
4256 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004257 }
4258 i = 0;
4259 cur = def->attrs;
4260 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004261 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4262 i++;
4263 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004264 }
4265 cur = def->content;
4266 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004267 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4268 i++;
4269 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004270 }
4271
Daniel Veillard4c004142003-10-07 11:33:24 +00004272 for (i = 0; i < nbchild; i++) {
4273 if (list[i] == NULL)
4274 continue;
4275 for (j = 0; j < i; j++) {
4276 if (list[j] == NULL)
4277 continue;
4278 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4279 if (ret == 0) {
4280 xmlRngPErr(ctxt, def->node, XML_RNGP_GROUP_ATTR_CONFLICT,
4281 "Attributes conflicts in group\n", NULL, NULL);
4282 }
4283 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004284 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004285 for (i = 0; i < nbchild; i++) {
4286 if (list[i] != NULL)
4287 xmlFree(list[i]);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004288 }
4289
4290 xmlFree(list);
Daniel Veillarde063f482003-03-21 16:53:17 +00004291 def->dflags |= IS_PROCESSED;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004292}
4293
4294/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00004295 * xmlRelaxNGComputeInterleaves:
4296 * @def: the interleave definition
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004297 * @ctxt: a Relax-NG parser context
Daniel Veillardfd573f12003-03-16 17:52:32 +00004298 * @name: the definition name
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004299 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00004300 * A lot of work for preprocessing interleave definitions
4301 * is potentially needed to get a decent execution speed at runtime
4302 * - trying to get a total order on the element nodes generated
4303 * by the interleaves, order the list of interleave definitions
4304 * following that order.
4305 * - if <text/> is used to handle mixed content, it is better to
4306 * flag this in the define and simplify the runtime checking
4307 * algorithm
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004308 */
4309static void
Daniel Veillardfd573f12003-03-16 17:52:32 +00004310xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def,
Daniel Veillard4c004142003-10-07 11:33:24 +00004311 xmlRelaxNGParserCtxtPtr ctxt,
4312 xmlChar * name ATTRIBUTE_UNUSED)
4313{
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004314 xmlRelaxNGDefinePtr cur, *tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004315
Daniel Veillardfd573f12003-03-16 17:52:32 +00004316 xmlRelaxNGPartitionPtr partitions = NULL;
4317 xmlRelaxNGInterleaveGroupPtr *groups = NULL;
4318 xmlRelaxNGInterleaveGroupPtr group;
Daniel Veillard4c004142003-10-07 11:33:24 +00004319 int i, j, ret, res;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004320 int nbgroups = 0;
4321 int nbchild = 0;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00004322 int is_mixed = 0;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004323 int is_determinist = 1;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004324
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004325 /*
4326 * Don't run that check in case of error. Infinite recursion
4327 * becomes possible.
4328 */
4329 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004330 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004331
Daniel Veillardfd573f12003-03-16 17:52:32 +00004332#ifdef DEBUG_INTERLEAVE
4333 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00004334 "xmlRelaxNGComputeInterleaves(%s)\n", name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004335#endif
4336 cur = def->content;
4337 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004338 nbchild++;
4339 cur = cur->next;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004340 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004341
Daniel Veillardfd573f12003-03-16 17:52:32 +00004342#ifdef DEBUG_INTERLEAVE
4343 xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild);
4344#endif
4345 groups = (xmlRelaxNGInterleaveGroupPtr *)
Daniel Veillard4c004142003-10-07 11:33:24 +00004346 xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004347 if (groups == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00004348 goto error;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004349 cur = def->content;
4350 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004351 groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr)
4352 xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup));
4353 if (groups[nbgroups] == NULL)
4354 goto error;
4355 if (cur->type == XML_RELAXNG_TEXT)
4356 is_mixed++;
4357 groups[nbgroups]->rule = cur;
4358 groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0);
4359 groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1);
4360 nbgroups++;
4361 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004362 }
4363#ifdef DEBUG_INTERLEAVE
4364 xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups);
4365#endif
4366
4367 /*
4368 * Let's check that all rules makes a partitions according to 7.4
4369 */
4370 partitions = (xmlRelaxNGPartitionPtr)
Daniel Veillard4c004142003-10-07 11:33:24 +00004371 xmlMalloc(sizeof(xmlRelaxNGPartition));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004372 if (partitions == NULL)
4373 goto error;
Daniel Veillard20863822003-03-22 17:51:47 +00004374 memset(partitions, 0, sizeof(xmlRelaxNGPartition));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004375 partitions->nbgroups = nbgroups;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004376 partitions->triage = xmlHashCreate(nbgroups);
Daniel Veillard4c004142003-10-07 11:33:24 +00004377 for (i = 0; i < nbgroups; i++) {
4378 group = groups[i];
4379 for (j = i + 1; j < nbgroups; j++) {
4380 if (groups[j] == NULL)
4381 continue;
4382
4383 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs,
4384 groups[j]->defs);
4385 if (ret == 0) {
4386 xmlRngPErr(ctxt, def->node, XML_RNGP_ELEM_TEXT_CONFLICT,
4387 "Element or text conflicts in interleave\n",
4388 NULL, NULL);
4389 }
4390 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs,
4391 groups[j]->attrs);
4392 if (ret == 0) {
4393 xmlRngPErr(ctxt, def->node, XML_RNGP_ATTR_CONFLICT,
4394 "Attributes conflicts in interleave\n", NULL,
4395 NULL);
4396 }
4397 }
4398 tmp = group->defs;
4399 if ((tmp != NULL) && (*tmp != NULL)) {
4400 while (*tmp != NULL) {
4401 if ((*tmp)->type == XML_RELAXNG_TEXT) {
4402 res = xmlHashAddEntry2(partitions->triage,
4403 BAD_CAST "#text", NULL,
4404 (void *) (long) (i + 1));
4405 if (res != 0)
4406 is_determinist = -1;
4407 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
4408 ((*tmp)->name != NULL)) {
4409 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4410 res = xmlHashAddEntry2(partitions->triage,
4411 (*tmp)->name, NULL,
4412 (void *) (long) (i + 1));
4413 else
4414 res = xmlHashAddEntry2(partitions->triage,
4415 (*tmp)->name, (*tmp)->ns,
4416 (void *) (long) (i + 1));
4417 if (res != 0)
4418 is_determinist = -1;
4419 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
4420 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4421 res = xmlHashAddEntry2(partitions->triage,
4422 BAD_CAST "#any", NULL,
4423 (void *) (long) (i + 1));
4424 else
4425 res = xmlHashAddEntry2(partitions->triage,
4426 BAD_CAST "#any", (*tmp)->ns,
4427 (void *) (long) (i + 1));
4428 if ((*tmp)->nameClass != NULL)
4429 is_determinist = 2;
4430 if (res != 0)
4431 is_determinist = -1;
4432 } else {
4433 is_determinist = -1;
4434 }
4435 tmp++;
4436 }
4437 } else {
4438 is_determinist = 0;
4439 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004440 }
4441 partitions->groups = groups;
4442
4443 /*
4444 * and save the partition list back in the def
4445 */
4446 def->data = partitions;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00004447 if (is_mixed != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004448 def->dflags |= IS_MIXED;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004449 if (is_determinist == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00004450 partitions->flags = IS_DETERMINIST;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004451 if (is_determinist == 2)
Daniel Veillard4c004142003-10-07 11:33:24 +00004452 partitions->flags = IS_DETERMINIST | IS_NEEDCHECK;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004453 return;
4454
Daniel Veillard4c004142003-10-07 11:33:24 +00004455 error:
4456 xmlRngPErrMemory(ctxt, "in interleave computation\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00004457 if (groups != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004458 for (i = 0; i < nbgroups; i++)
4459 if (groups[i] != NULL) {
4460 if (groups[i]->defs != NULL)
4461 xmlFree(groups[i]->defs);
4462 xmlFree(groups[i]);
4463 }
4464 xmlFree(groups);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004465 }
4466 xmlRelaxNGFreePartition(partitions);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004467}
4468
4469/**
4470 * xmlRelaxNGParseInterleave:
4471 * @ctxt: a Relax-NG parser context
4472 * @node: the data node.
4473 *
4474 * parse the content of a RelaxNG interleave node.
4475 *
4476 * Returns the definition pointer or NULL in case of error
4477 */
4478static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004479xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4480{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004481 xmlRelaxNGDefinePtr def = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004482 xmlRelaxNGDefinePtr last = NULL, cur;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004483 xmlNodePtr child;
4484
Daniel Veillardfd573f12003-03-16 17:52:32 +00004485 def = xmlRelaxNGNewDefine(ctxt, node);
4486 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004487 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004488 }
4489 def->type = XML_RELAXNG_INTERLEAVE;
4490
4491 if (ctxt->interleaves == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00004492 ctxt->interleaves = xmlHashCreate(10);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004493 if (ctxt->interleaves == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004494 xmlRngPErrMemory(ctxt, "create interleaves\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00004495 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004496 char name[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00004497
Daniel Veillard4c004142003-10-07 11:33:24 +00004498 snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++);
4499 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) {
4500 xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_ADD,
4501 "Failed to add %s to hash table\n",
4502 (const xmlChar *) name, NULL);
4503 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004504 }
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004505 child = node->children;
Daniel Veillardd2298792003-02-14 16:54:11 +00004506 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004507 xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_NO_CONTENT,
4508 "Element interleave is empty\n", NULL, NULL);
Daniel Veillard1564e6e2003-03-15 21:30:25 +00004509 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004510 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004511 if (IS_RELAXNG(child, "element")) {
4512 cur = xmlRelaxNGParseElement(ctxt, child);
4513 } else {
4514 cur = xmlRelaxNGParsePattern(ctxt, child);
4515 }
4516 if (cur != NULL) {
4517 cur->parent = def;
4518 if (last == NULL) {
4519 def->content = last = cur;
4520 } else {
4521 last->next = cur;
4522 last = cur;
4523 }
4524 }
4525 child = child->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004526 }
4527
Daniel Veillard4c004142003-10-07 11:33:24 +00004528 return (def);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004529}
Daniel Veillard6eadf632003-01-23 18:29:16 +00004530
4531/**
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004532 * xmlRelaxNGParseInclude:
4533 * @ctxt: a Relax-NG parser context
4534 * @node: the include node
4535 *
4536 * Integrate the content of an include node in the current grammar
4537 *
4538 * Returns 0 in case of success or -1 in case of error
4539 */
4540static int
Daniel Veillard4c004142003-10-07 11:33:24 +00004541xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4542{
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004543 xmlRelaxNGIncludePtr incl;
4544 xmlNodePtr root;
4545 int ret = 0, tmp;
4546
Daniel Veillard807daf82004-02-22 22:13:27 +00004547 incl = node->psvi;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004548 if (incl == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004549 xmlRngPErr(ctxt, node, XML_RNGP_INCLUDE_EMPTY,
4550 "Include node has no data\n", NULL, NULL);
4551 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004552 }
4553 root = xmlDocGetRootElement(incl->doc);
4554 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004555 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, "Include document is empty\n",
4556 NULL, NULL);
4557 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004558 }
4559 if (!xmlStrEqual(root->name, BAD_CAST "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004560 xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING,
4561 "Include document root is not a grammar\n", NULL, NULL);
4562 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004563 }
4564
4565 /*
4566 * Merge the definition from both the include and the internal list
4567 */
4568 if (root->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004569 tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children);
4570 if (tmp != 0)
4571 ret = -1;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004572 }
4573 if (node->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004574 tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children);
4575 if (tmp != 0)
4576 ret = -1;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004577 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004578 return (ret);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004579}
4580
4581/**
Daniel Veillard276be4a2003-01-24 01:03:34 +00004582 * xmlRelaxNGParseDefine:
4583 * @ctxt: a Relax-NG parser context
4584 * @node: the define node
4585 *
4586 * parse the content of a RelaxNG define element node.
4587 *
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004588 * Returns 0 in case of success or -1 in case of error
Daniel Veillard276be4a2003-01-24 01:03:34 +00004589 */
4590static int
Daniel Veillard4c004142003-10-07 11:33:24 +00004591xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4592{
Daniel Veillard276be4a2003-01-24 01:03:34 +00004593 xmlChar *name;
4594 int ret = 0, tmp;
4595 xmlRelaxNGDefinePtr def;
4596 const xmlChar *olddefine;
4597
4598 name = xmlGetProp(node, BAD_CAST "name");
4599 if (name == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004600 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_NAME_MISSING,
4601 "define has no name\n", NULL, NULL);
Daniel Veillard276be4a2003-01-24 01:03:34 +00004602 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004603 xmlRelaxNGNormExtSpace(name);
4604 if (xmlValidateNCName(name, 0)) {
4605 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_DEFINE_NAME,
4606 "define name '%s' is not an NCName\n", name, NULL);
4607 }
4608 def = xmlRelaxNGNewDefine(ctxt, node);
4609 if (def == NULL) {
4610 xmlFree(name);
4611 return (-1);
4612 }
4613 def->type = XML_RELAXNG_DEF;
4614 def->name = name;
4615 if (node->children == NULL) {
4616 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_EMPTY,
4617 "define has no children\n", NULL, NULL);
4618 } else {
4619 olddefine = ctxt->define;
4620 ctxt->define = name;
4621 def->content =
4622 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4623 ctxt->define = olddefine;
4624 }
4625 if (ctxt->grammar->defs == NULL)
4626 ctxt->grammar->defs = xmlHashCreate(10);
4627 if (ctxt->grammar->defs == NULL) {
4628 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
4629 "Could not create definition hash\n", NULL, NULL);
4630 ret = -1;
4631 } else {
4632 tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def);
4633 if (tmp < 0) {
4634 xmlRelaxNGDefinePtr prev;
Daniel Veillard154877e2003-01-30 12:17:05 +00004635
Daniel Veillard4c004142003-10-07 11:33:24 +00004636 prev = xmlHashLookup(ctxt->grammar->defs, name);
4637 if (prev == NULL) {
4638 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
4639 "Internal error on define aggregation of %s\n",
4640 name, NULL);
4641 ret = -1;
4642 } else {
4643 while (prev->nextHash != NULL)
4644 prev = prev->nextHash;
4645 prev->nextHash = def;
4646 }
4647 }
4648 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00004649 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004650 return (ret);
Daniel Veillard276be4a2003-01-24 01:03:34 +00004651}
4652
4653/**
Daniel Veillard81c51e12009-08-14 18:52:10 +02004654 * xmlRelaxNGParseImportRef:
4655 * @payload: the parser context
4656 * @data: the current grammar
4657 * @name: the reference name
4658 *
4659 * Import import one references into the current grammar
4660 */
4661static void
4662xmlRelaxNGParseImportRef(void *payload, void *data, xmlChar *name) {
4663 xmlRelaxNGParserCtxtPtr ctxt = (xmlRelaxNGParserCtxtPtr) data;
4664 xmlRelaxNGDefinePtr def = (xmlRelaxNGDefinePtr) payload;
4665 int tmp;
4666
Daniel Veillardaa422d92009-09-24 11:31:48 +02004667 def->dflags |= IS_EXTERNAL_REF;
4668
Daniel Veillard81c51e12009-08-14 18:52:10 +02004669 tmp = xmlHashAddEntry(ctxt->grammar->refs, name, def);
4670 if (tmp < 0) {
4671 xmlRelaxNGDefinePtr prev;
4672
4673 prev = (xmlRelaxNGDefinePtr)
4674 xmlHashLookup(ctxt->grammar->refs, def->name);
4675 if (prev == NULL) {
4676 if (def->name != NULL) {
4677 xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED,
4678 "Error refs definitions '%s'\n",
4679 def->name, NULL);
4680 } else {
4681 xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED,
4682 "Error refs definitions\n",
4683 NULL, NULL);
4684 }
4685 } else {
4686 def->nextHash = prev->nextHash;
4687 prev->nextHash = def;
4688 }
4689 }
4690}
4691
4692/**
4693 * xmlRelaxNGParseImportRefs:
4694 * @ctxt: the parser context
4695 * @grammar: the sub grammar
4696 *
4697 * Import references from the subgrammar into the current grammar
4698 *
4699 * Returns 0 in case of success, -1 in case of failure
4700 */
4701static int
4702xmlRelaxNGParseImportRefs(xmlRelaxNGParserCtxtPtr ctxt,
4703 xmlRelaxNGGrammarPtr grammar) {
4704 if ((ctxt == NULL) || (grammar == NULL) || (ctxt->grammar == NULL))
4705 return(-1);
4706 if (grammar->refs == NULL)
4707 return(0);
4708 if (ctxt->grammar->refs == NULL)
4709 ctxt->grammar->refs = xmlHashCreate(10);
4710 if (ctxt->grammar->refs == NULL) {
4711 xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED,
4712 "Could not create references hash\n", NULL, NULL);
4713 return(-1);
4714 }
4715 xmlHashScan(grammar->refs, xmlRelaxNGParseImportRef, ctxt);
Daniel Veillardec18c962009-08-26 18:37:43 +02004716 return(0);
Daniel Veillard81c51e12009-08-14 18:52:10 +02004717}
4718
4719/**
Daniel Veillardfebcca42003-02-16 15:44:18 +00004720 * xmlRelaxNGProcessExternalRef:
4721 * @ctxt: the parser context
4722 * @node: the externlRef node
4723 *
4724 * Process and compile an externlRef node
4725 *
4726 * Returns the xmlRelaxNGDefinePtr or NULL in case of error
4727 */
4728static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004729xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4730{
Daniel Veillardfebcca42003-02-16 15:44:18 +00004731 xmlRelaxNGDocumentPtr docu;
4732 xmlNodePtr root, tmp;
4733 xmlChar *ns;
Daniel Veillard77648bb2003-02-20 15:03:22 +00004734 int newNs = 0, oldflags;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004735 xmlRelaxNGDefinePtr def;
4736
Daniel Veillard807daf82004-02-22 22:13:27 +00004737 docu = node->psvi;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004738 if (docu != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004739 def = xmlRelaxNGNewDefine(ctxt, node);
4740 if (def == NULL)
4741 return (NULL);
4742 def->type = XML_RELAXNG_EXTERNALREF;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004743
Daniel Veillard4c004142003-10-07 11:33:24 +00004744 if (docu->content == NULL) {
4745 /*
4746 * Then do the parsing for good
4747 */
4748 root = xmlDocGetRootElement(docu->doc);
4749 if (root == NULL) {
4750 xmlRngPErr(ctxt, node, XML_RNGP_EXTERNALREF_EMTPY,
4751 "xmlRelaxNGParse: %s is empty\n", ctxt->URL,
4752 NULL);
4753 return (NULL);
4754 }
4755 /*
4756 * ns transmission rules
4757 */
4758 ns = xmlGetProp(root, BAD_CAST "ns");
4759 if (ns == NULL) {
4760 tmp = node;
4761 while ((tmp != NULL) && (tmp->type == XML_ELEMENT_NODE)) {
4762 ns = xmlGetProp(tmp, BAD_CAST "ns");
4763 if (ns != NULL) {
4764 break;
4765 }
4766 tmp = tmp->parent;
4767 }
4768 if (ns != NULL) {
4769 xmlSetProp(root, BAD_CAST "ns", ns);
4770 newNs = 1;
4771 xmlFree(ns);
4772 }
4773 } else {
4774 xmlFree(ns);
4775 }
Daniel Veillardfebcca42003-02-16 15:44:18 +00004776
Daniel Veillard4c004142003-10-07 11:33:24 +00004777 /*
4778 * Parsing to get a precompiled schemas.
4779 */
4780 oldflags = ctxt->flags;
4781 ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF;
4782 docu->schema = xmlRelaxNGParseDocument(ctxt, root);
4783 ctxt->flags = oldflags;
4784 if ((docu->schema != NULL) &&
4785 (docu->schema->topgrammar != NULL)) {
4786 docu->content = docu->schema->topgrammar->start;
Daniel Veillard81c51e12009-08-14 18:52:10 +02004787 if (docu->schema->topgrammar->refs)
4788 xmlRelaxNGParseImportRefs(ctxt, docu->schema->topgrammar);
Daniel Veillard4c004142003-10-07 11:33:24 +00004789 }
4790
4791 /*
4792 * the externalRef may be reused in a different ns context
4793 */
4794 if (newNs == 1) {
4795 xmlUnsetProp(root, BAD_CAST "ns");
4796 }
4797 }
4798 def->content = docu->content;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004799 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004800 def = NULL;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004801 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004802 return (def);
Daniel Veillardfebcca42003-02-16 15:44:18 +00004803}
4804
4805/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00004806 * xmlRelaxNGParsePattern:
4807 * @ctxt: a Relax-NG parser context
4808 * @node: the pattern node.
4809 *
4810 * parse the content of a RelaxNG pattern node.
4811 *
Daniel Veillard276be4a2003-01-24 01:03:34 +00004812 * Returns the definition pointer or NULL in case of error or if no
4813 * pattern is generated.
Daniel Veillard6eadf632003-01-23 18:29:16 +00004814 */
4815static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004816xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4817{
Daniel Veillard6eadf632003-01-23 18:29:16 +00004818 xmlRelaxNGDefinePtr def = NULL;
4819
Daniel Veillardd2298792003-02-14 16:54:11 +00004820 if (node == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004821 return (NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00004822 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004823 if (IS_RELAXNG(node, "element")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004824 def = xmlRelaxNGParseElement(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004825 } else if (IS_RELAXNG(node, "attribute")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004826 def = xmlRelaxNGParseAttribute(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004827 } else if (IS_RELAXNG(node, "empty")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004828 def = xmlRelaxNGNewDefine(ctxt, node);
4829 if (def == NULL)
4830 return (NULL);
4831 def->type = XML_RELAXNG_EMPTY;
4832 if (node->children != NULL) {
4833 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_NOT_EMPTY,
4834 "empty: had a child node\n", NULL, NULL);
4835 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004836 } else if (IS_RELAXNG(node, "text")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004837 def = xmlRelaxNGNewDefine(ctxt, node);
4838 if (def == NULL)
4839 return (NULL);
4840 def->type = XML_RELAXNG_TEXT;
4841 if (node->children != NULL) {
4842 xmlRngPErr(ctxt, node, XML_RNGP_TEXT_HAS_CHILD,
4843 "text: had a child node\n", NULL, NULL);
4844 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004845 } else if (IS_RELAXNG(node, "zeroOrMore")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004846 def = xmlRelaxNGNewDefine(ctxt, node);
4847 if (def == NULL)
4848 return (NULL);
4849 def->type = XML_RELAXNG_ZEROORMORE;
4850 if (node->children == NULL) {
4851 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4852 "Element %s is empty\n", node->name, NULL);
4853 } else {
4854 def->content =
4855 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4856 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004857 } else if (IS_RELAXNG(node, "oneOrMore")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004858 def = xmlRelaxNGNewDefine(ctxt, node);
4859 if (def == NULL)
4860 return (NULL);
4861 def->type = XML_RELAXNG_ONEORMORE;
4862 if (node->children == NULL) {
4863 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4864 "Element %s is empty\n", node->name, NULL);
4865 } else {
4866 def->content =
4867 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4868 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004869 } else if (IS_RELAXNG(node, "optional")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004870 def = xmlRelaxNGNewDefine(ctxt, node);
4871 if (def == NULL)
4872 return (NULL);
4873 def->type = XML_RELAXNG_OPTIONAL;
4874 if (node->children == NULL) {
4875 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4876 "Element %s is empty\n", node->name, NULL);
4877 } else {
4878 def->content =
4879 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4880 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004881 } else if (IS_RELAXNG(node, "choice")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004882 def = xmlRelaxNGNewDefine(ctxt, node);
4883 if (def == NULL)
4884 return (NULL);
4885 def->type = XML_RELAXNG_CHOICE;
4886 if (node->children == NULL) {
4887 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4888 "Element %s is empty\n", node->name, NULL);
4889 } else {
4890 def->content =
4891 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4892 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004893 } else if (IS_RELAXNG(node, "group")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004894 def = xmlRelaxNGNewDefine(ctxt, node);
4895 if (def == NULL)
4896 return (NULL);
4897 def->type = XML_RELAXNG_GROUP;
4898 if (node->children == NULL) {
4899 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4900 "Element %s is empty\n", node->name, NULL);
4901 } else {
4902 def->content =
4903 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4904 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004905 } else if (IS_RELAXNG(node, "ref")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004906 def = xmlRelaxNGNewDefine(ctxt, node);
4907 if (def == NULL)
4908 return (NULL);
4909 def->type = XML_RELAXNG_REF;
4910 def->name = xmlGetProp(node, BAD_CAST "name");
4911 if (def->name == NULL) {
4912 xmlRngPErr(ctxt, node, XML_RNGP_REF_NO_NAME, "ref has no name\n",
4913 NULL, NULL);
4914 } else {
4915 xmlRelaxNGNormExtSpace(def->name);
4916 if (xmlValidateNCName(def->name, 0)) {
4917 xmlRngPErr(ctxt, node, XML_RNGP_REF_NAME_INVALID,
4918 "ref name '%s' is not an NCName\n", def->name,
4919 NULL);
4920 }
4921 }
4922 if (node->children != NULL) {
4923 xmlRngPErr(ctxt, node, XML_RNGP_REF_NOT_EMPTY, "ref is not empty\n",
4924 NULL, NULL);
4925 }
4926 if (ctxt->grammar->refs == NULL)
4927 ctxt->grammar->refs = xmlHashCreate(10);
4928 if (ctxt->grammar->refs == NULL) {
4929 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4930 "Could not create references hash\n", NULL, NULL);
4931 def = NULL;
4932 } else {
4933 int tmp;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004934
Daniel Veillard4c004142003-10-07 11:33:24 +00004935 tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def);
4936 if (tmp < 0) {
4937 xmlRelaxNGDefinePtr prev;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004938
Daniel Veillard4c004142003-10-07 11:33:24 +00004939 prev = (xmlRelaxNGDefinePtr)
4940 xmlHashLookup(ctxt->grammar->refs, def->name);
4941 if (prev == NULL) {
4942 if (def->name != NULL) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00004943 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4944 "Error refs definitions '%s'\n",
4945 def->name, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00004946 } else {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00004947 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4948 "Error refs definitions\n",
4949 NULL, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00004950 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004951 def = NULL;
4952 } else {
4953 def->nextHash = prev->nextHash;
4954 prev->nextHash = def;
4955 }
4956 }
4957 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00004958 } else if (IS_RELAXNG(node, "data")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004959 def = xmlRelaxNGParseData(ctxt, node);
Daniel Veillardedc91922003-01-26 00:52:04 +00004960 } else if (IS_RELAXNG(node, "value")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004961 def = xmlRelaxNGParseValue(ctxt, node);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00004962 } else if (IS_RELAXNG(node, "list")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004963 def = xmlRelaxNGNewDefine(ctxt, node);
4964 if (def == NULL)
4965 return (NULL);
4966 def->type = XML_RELAXNG_LIST;
4967 if (node->children == NULL) {
4968 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4969 "Element %s is empty\n", node->name, NULL);
4970 } else {
4971 def->content =
4972 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4973 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004974 } else if (IS_RELAXNG(node, "interleave")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004975 def = xmlRelaxNGParseInterleave(ctxt, node);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00004976 } else if (IS_RELAXNG(node, "externalRef")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004977 def = xmlRelaxNGProcessExternalRef(ctxt, node);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004978 } else if (IS_RELAXNG(node, "notAllowed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004979 def = xmlRelaxNGNewDefine(ctxt, node);
4980 if (def == NULL)
4981 return (NULL);
4982 def->type = XML_RELAXNG_NOT_ALLOWED;
4983 if (node->children != NULL) {
4984 xmlRngPErr(ctxt, node, XML_RNGP_NOTALLOWED_NOT_EMPTY,
4985 "xmlRelaxNGParse: notAllowed element is not empty\n",
4986 NULL, NULL);
4987 }
Daniel Veillard419a7682003-02-03 23:22:49 +00004988 } else if (IS_RELAXNG(node, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004989 xmlRelaxNGGrammarPtr grammar, old;
4990 xmlRelaxNGGrammarPtr oldparent;
Daniel Veillard419a7682003-02-03 23:22:49 +00004991
Daniel Veillardc482e262003-02-26 14:48:48 +00004992#ifdef DEBUG_GRAMMAR
Daniel Veillard4c004142003-10-07 11:33:24 +00004993 xmlGenericError(xmlGenericErrorContext,
4994 "Found <grammar> pattern\n");
Daniel Veillardc482e262003-02-26 14:48:48 +00004995#endif
4996
Daniel Veillard4c004142003-10-07 11:33:24 +00004997 oldparent = ctxt->parentgrammar;
4998 old = ctxt->grammar;
4999 ctxt->parentgrammar = old;
5000 grammar = xmlRelaxNGParseGrammar(ctxt, node->children);
5001 if (old != NULL) {
5002 ctxt->grammar = old;
5003 ctxt->parentgrammar = oldparent;
Daniel Veillardc482e262003-02-26 14:48:48 +00005004#if 0
Daniel Veillard4c004142003-10-07 11:33:24 +00005005 if (grammar != NULL) {
5006 grammar->next = old->next;
5007 old->next = grammar;
5008 }
Daniel Veillardc482e262003-02-26 14:48:48 +00005009#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00005010 }
5011 if (grammar != NULL)
5012 def = grammar->start;
5013 else
5014 def = NULL;
Daniel Veillard419a7682003-02-03 23:22:49 +00005015 } else if (IS_RELAXNG(node, "parentRef")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005016 if (ctxt->parentgrammar == NULL) {
5017 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_PARENT,
5018 "Use of parentRef without a parent grammar\n", NULL,
5019 NULL);
5020 return (NULL);
5021 }
5022 def = xmlRelaxNGNewDefine(ctxt, node);
5023 if (def == NULL)
5024 return (NULL);
5025 def->type = XML_RELAXNG_PARENTREF;
5026 def->name = xmlGetProp(node, BAD_CAST "name");
5027 if (def->name == NULL) {
5028 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_NAME,
5029 "parentRef has no name\n", NULL, NULL);
5030 } else {
5031 xmlRelaxNGNormExtSpace(def->name);
5032 if (xmlValidateNCName(def->name, 0)) {
5033 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NAME_INVALID,
5034 "parentRef name '%s' is not an NCName\n",
5035 def->name, NULL);
5036 }
5037 }
5038 if (node->children != NULL) {
5039 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NOT_EMPTY,
5040 "parentRef is not empty\n", NULL, NULL);
5041 }
5042 if (ctxt->parentgrammar->refs == NULL)
5043 ctxt->parentgrammar->refs = xmlHashCreate(10);
5044 if (ctxt->parentgrammar->refs == NULL) {
5045 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
5046 "Could not create references hash\n", NULL, NULL);
5047 def = NULL;
5048 } else if (def->name != NULL) {
5049 int tmp;
Daniel Veillard419a7682003-02-03 23:22:49 +00005050
Daniel Veillard4c004142003-10-07 11:33:24 +00005051 tmp =
5052 xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def);
5053 if (tmp < 0) {
5054 xmlRelaxNGDefinePtr prev;
Daniel Veillard419a7682003-02-03 23:22:49 +00005055
Daniel Veillard4c004142003-10-07 11:33:24 +00005056 prev = (xmlRelaxNGDefinePtr)
5057 xmlHashLookup(ctxt->parentgrammar->refs, def->name);
5058 if (prev == NULL) {
5059 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
5060 "Internal error parentRef definitions '%s'\n",
5061 def->name, NULL);
5062 def = NULL;
5063 } else {
5064 def->nextHash = prev->nextHash;
5065 prev->nextHash = def;
5066 }
5067 }
5068 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005069 } else if (IS_RELAXNG(node, "mixed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005070 if (node->children == NULL) {
5071 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, "Mixed is empty\n",
5072 NULL, NULL);
5073 def = NULL;
5074 } else {
5075 def = xmlRelaxNGParseInterleave(ctxt, node);
5076 if (def != NULL) {
5077 xmlRelaxNGDefinePtr tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005078
Daniel Veillard4c004142003-10-07 11:33:24 +00005079 if ((def->content != NULL) && (def->content->next != NULL)) {
5080 tmp = xmlRelaxNGNewDefine(ctxt, node);
5081 if (tmp != NULL) {
5082 tmp->type = XML_RELAXNG_GROUP;
5083 tmp->content = def->content;
5084 def->content = tmp;
5085 }
5086 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005087
Daniel Veillard4c004142003-10-07 11:33:24 +00005088 tmp = xmlRelaxNGNewDefine(ctxt, node);
5089 if (tmp == NULL)
5090 return (def);
5091 tmp->type = XML_RELAXNG_TEXT;
5092 tmp->next = def->content;
5093 def->content = tmp;
5094 }
5095 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005096 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005097 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_CONSTRUCT,
5098 "Unexpected node %s is not a pattern\n", node->name,
5099 NULL);
5100 def = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005101 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005102 return (def);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005103}
5104
5105/**
5106 * xmlRelaxNGParseAttribute:
5107 * @ctxt: a Relax-NG parser context
5108 * @node: the element node
5109 *
5110 * parse the content of a RelaxNG attribute node.
5111 *
5112 * Returns the definition pointer or NULL in case of error.
5113 */
5114static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00005115xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
5116{
Daniel Veillardd2298792003-02-14 16:54:11 +00005117 xmlRelaxNGDefinePtr ret, cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005118 xmlNodePtr child;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005119 int old_flags;
5120
Daniel Veillardfd573f12003-03-16 17:52:32 +00005121 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005122 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005123 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005124 ret->type = XML_RELAXNG_ATTRIBUTE;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005125 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005126 child = node->children;
5127 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005128 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_EMPTY,
5129 "xmlRelaxNGParseattribute: attribute has no children\n",
5130 NULL, NULL);
5131 return (ret);
5132 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005133 old_flags = ctxt->flags;
5134 ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005135 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
5136 if (cur != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005137 child = child->next;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005138
Daniel Veillardd2298792003-02-14 16:54:11 +00005139 if (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005140 cur = xmlRelaxNGParsePattern(ctxt, child);
5141 if (cur != NULL) {
5142 switch (cur->type) {
5143 case XML_RELAXNG_EMPTY:
5144 case XML_RELAXNG_NOT_ALLOWED:
5145 case XML_RELAXNG_TEXT:
5146 case XML_RELAXNG_ELEMENT:
5147 case XML_RELAXNG_DATATYPE:
5148 case XML_RELAXNG_VALUE:
5149 case XML_RELAXNG_LIST:
5150 case XML_RELAXNG_REF:
5151 case XML_RELAXNG_PARENTREF:
5152 case XML_RELAXNG_EXTERNALREF:
5153 case XML_RELAXNG_DEF:
5154 case XML_RELAXNG_ONEORMORE:
5155 case XML_RELAXNG_ZEROORMORE:
5156 case XML_RELAXNG_OPTIONAL:
5157 case XML_RELAXNG_CHOICE:
5158 case XML_RELAXNG_GROUP:
5159 case XML_RELAXNG_INTERLEAVE:
5160 case XML_RELAXNG_ATTRIBUTE:
5161 ret->content = cur;
5162 cur->parent = ret;
5163 break;
5164 case XML_RELAXNG_START:
5165 case XML_RELAXNG_PARAM:
5166 case XML_RELAXNG_EXCEPT:
5167 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CONTENT,
5168 "attribute has invalid content\n", NULL,
5169 NULL);
5170 break;
5171 case XML_RELAXNG_NOOP:
5172 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_NOOP,
5173 "RNG Internal error, noop found in attribute\n",
5174 NULL, NULL);
5175 break;
5176 }
5177 }
5178 child = child->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005179 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005180 if (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005181 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CHILDREN,
5182 "attribute has multiple children\n", NULL, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005183 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005184 ctxt->flags = old_flags;
Daniel Veillard4c004142003-10-07 11:33:24 +00005185 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005186}
5187
5188/**
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005189 * xmlRelaxNGParseExceptNameClass:
5190 * @ctxt: a Relax-NG parser context
5191 * @node: the except node
Daniel Veillard144fae12003-02-03 13:17:57 +00005192 * @attr: 1 if within an attribute, 0 if within an element
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005193 *
5194 * parse the content of a RelaxNG nameClass node.
5195 *
5196 * Returns the definition pointer or NULL in case of error.
5197 */
5198static xmlRelaxNGDefinePtr
Daniel Veillard144fae12003-02-03 13:17:57 +00005199xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00005200 xmlNodePtr node, int attr)
5201{
Daniel Veillard144fae12003-02-03 13:17:57 +00005202 xmlRelaxNGDefinePtr ret, cur, last = NULL;
5203 xmlNodePtr child;
5204
Daniel Veillardd2298792003-02-14 16:54:11 +00005205 if (!IS_RELAXNG(node, "except")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005206 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MISSING,
5207 "Expecting an except node\n", NULL, NULL);
5208 return (NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005209 }
5210 if (node->next != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005211 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MULTIPLE,
5212 "exceptNameClass allows only a single except node\n",
5213 NULL, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005214 }
Daniel Veillard144fae12003-02-03 13:17:57 +00005215 if (node->children == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005216 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_EMPTY, "except has no content\n",
5217 NULL, NULL);
5218 return (NULL);
Daniel Veillard144fae12003-02-03 13:17:57 +00005219 }
5220
Daniel Veillardfd573f12003-03-16 17:52:32 +00005221 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard144fae12003-02-03 13:17:57 +00005222 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005223 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005224 ret->type = XML_RELAXNG_EXCEPT;
Daniel Veillard144fae12003-02-03 13:17:57 +00005225 child = node->children;
5226 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005227 cur = xmlRelaxNGNewDefine(ctxt, child);
5228 if (cur == NULL)
5229 break;
5230 if (attr)
5231 cur->type = XML_RELAXNG_ATTRIBUTE;
5232 else
5233 cur->type = XML_RELAXNG_ELEMENT;
5234
Daniel Veillard419a7682003-02-03 23:22:49 +00005235 if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005236 if (last == NULL) {
5237 ret->content = cur;
5238 } else {
5239 last->next = cur;
5240 }
5241 last = cur;
5242 }
5243 child = child->next;
Daniel Veillard144fae12003-02-03 13:17:57 +00005244 }
5245
Daniel Veillard4c004142003-10-07 11:33:24 +00005246 return (ret);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005247}
5248
5249/**
5250 * xmlRelaxNGParseNameClass:
5251 * @ctxt: a Relax-NG parser context
5252 * @node: the nameClass node
5253 * @def: the current definition
5254 *
5255 * parse the content of a RelaxNG nameClass node.
5256 *
5257 * Returns the definition pointer or NULL in case of error.
5258 */
5259static xmlRelaxNGDefinePtr
5260xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node,
Daniel Veillard4c004142003-10-07 11:33:24 +00005261 xmlRelaxNGDefinePtr def)
5262{
Daniel Veillardfd573f12003-03-16 17:52:32 +00005263 xmlRelaxNGDefinePtr ret, tmp;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005264 xmlChar *val;
5265
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005266 ret = def;
Daniel Veillard4c004142003-10-07 11:33:24 +00005267 if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) ||
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005268 (IS_RELAXNG(node, "nsName"))) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005269 if ((def->type != XML_RELAXNG_ELEMENT) &&
5270 (def->type != XML_RELAXNG_ATTRIBUTE)) {
5271 ret = xmlRelaxNGNewDefine(ctxt, node);
5272 if (ret == NULL)
5273 return (NULL);
5274 ret->parent = def;
5275 if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE)
5276 ret->type = XML_RELAXNG_ATTRIBUTE;
5277 else
5278 ret->type = XML_RELAXNG_ELEMENT;
5279 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005280 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005281 if (IS_RELAXNG(node, "name")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005282 val = xmlNodeGetContent(node);
5283 xmlRelaxNGNormExtSpace(val);
5284 if (xmlValidateNCName(val, 0)) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005285 if (node->parent != NULL)
5286 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
5287 "Element %s name '%s' is not an NCName\n",
5288 node->parent->name, val);
5289 else
5290 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
5291 "name '%s' is not an NCName\n",
5292 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005293 }
5294 ret->name = val;
5295 val = xmlGetProp(node, BAD_CAST "ns");
5296 ret->ns = val;
5297 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5298 (val != NULL) &&
5299 (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005300 xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
Daniel Veillard4c004142003-10-07 11:33:24 +00005301 "Attribute with namespace '%s' is not allowed\n",
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005302 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005303 }
5304 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5305 (val != NULL) &&
5306 (val[0] == 0) && (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005307 xmlRngPErr(ctxt, node, XML_RNGP_XMLNS_NAME,
5308 "Attribute with QName 'xmlns' is not allowed\n",
5309 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005310 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005311 } else if (IS_RELAXNG(node, "anyName")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005312 ret->name = NULL;
5313 ret->ns = NULL;
5314 if (node->children != NULL) {
5315 ret->nameClass =
5316 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5317 (def->type ==
5318 XML_RELAXNG_ATTRIBUTE));
5319 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005320 } else if (IS_RELAXNG(node, "nsName")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005321 ret->name = NULL;
5322 ret->ns = xmlGetProp(node, BAD_CAST "ns");
5323 if (ret->ns == NULL) {
5324 xmlRngPErr(ctxt, node, XML_RNGP_NSNAME_NO_NS,
5325 "nsName has no ns attribute\n", NULL, NULL);
5326 }
5327 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5328 (ret->ns != NULL) &&
5329 (xmlStrEqual
5330 (ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
5331 xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
5332 "Attribute with namespace '%s' is not allowed\n",
5333 ret->ns, NULL);
5334 }
5335 if (node->children != NULL) {
5336 ret->nameClass =
5337 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5338 (def->type ==
5339 XML_RELAXNG_ATTRIBUTE));
5340 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005341 } else if (IS_RELAXNG(node, "choice")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005342 xmlNodePtr child;
5343 xmlRelaxNGDefinePtr last = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005344
Daniel Veillard4c004142003-10-07 11:33:24 +00005345 ret = xmlRelaxNGNewDefine(ctxt, node);
5346 if (ret == NULL)
5347 return (NULL);
5348 ret->parent = def;
5349 ret->type = XML_RELAXNG_CHOICE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005350
Daniel Veillard4c004142003-10-07 11:33:24 +00005351 if (node->children == NULL) {
5352 xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_EMPTY,
5353 "Element choice is empty\n", NULL, NULL);
5354 } else {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005355
Daniel Veillard4c004142003-10-07 11:33:24 +00005356 child = node->children;
5357 while (child != NULL) {
5358 tmp = xmlRelaxNGParseNameClass(ctxt, child, ret);
5359 if (tmp != NULL) {
5360 if (last == NULL) {
5361 last = ret->nameClass = tmp;
5362 } else {
5363 last->next = tmp;
5364 last = tmp;
5365 }
5366 }
5367 child = child->next;
5368 }
5369 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005370 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005371 xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_CONTENT,
5372 "expecting name, anyName, nsName or choice : got %s\n",
Ben Waltona7a6a4b2010-03-15 10:06:36 +01005373 (node == NULL ? (const xmlChar *) "nothing" : node->name),
5374 NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005375 return (NULL);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005376 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005377 if (ret != def) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005378 if (def->nameClass == NULL) {
5379 def->nameClass = ret;
5380 } else {
5381 tmp = def->nameClass;
5382 while (tmp->next != NULL) {
5383 tmp = tmp->next;
5384 }
5385 tmp->next = ret;
5386 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005387 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005388 return (ret);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005389}
5390
5391/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00005392 * xmlRelaxNGParseElement:
5393 * @ctxt: a Relax-NG parser context
5394 * @node: the element node
5395 *
5396 * parse the content of a RelaxNG element node.
5397 *
5398 * Returns the definition pointer or NULL in case of error.
5399 */
5400static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00005401xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
5402{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005403 xmlRelaxNGDefinePtr ret, cur, last;
5404 xmlNodePtr child;
Daniel Veillard276be4a2003-01-24 01:03:34 +00005405 const xmlChar *olddefine;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005406
Daniel Veillardfd573f12003-03-16 17:52:32 +00005407 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005408 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005409 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005410 ret->type = XML_RELAXNG_ELEMENT;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005411 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005412 child = node->children;
5413 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005414 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_EMPTY,
5415 "xmlRelaxNGParseElement: element has no children\n",
5416 NULL, NULL);
5417 return (ret);
5418 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005419 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
5420 if (cur != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005421 child = child->next;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005422
Daniel Veillard6eadf632003-01-23 18:29:16 +00005423 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005424 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NO_CONTENT,
5425 "xmlRelaxNGParseElement: element has no content\n",
5426 NULL, NULL);
5427 return (ret);
5428 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00005429 olddefine = ctxt->define;
5430 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005431 last = NULL;
5432 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005433 cur = xmlRelaxNGParsePattern(ctxt, child);
5434 if (cur != NULL) {
5435 cur->parent = ret;
5436 switch (cur->type) {
5437 case XML_RELAXNG_EMPTY:
5438 case XML_RELAXNG_NOT_ALLOWED:
5439 case XML_RELAXNG_TEXT:
5440 case XML_RELAXNG_ELEMENT:
5441 case XML_RELAXNG_DATATYPE:
5442 case XML_RELAXNG_VALUE:
5443 case XML_RELAXNG_LIST:
5444 case XML_RELAXNG_REF:
5445 case XML_RELAXNG_PARENTREF:
5446 case XML_RELAXNG_EXTERNALREF:
5447 case XML_RELAXNG_DEF:
5448 case XML_RELAXNG_ZEROORMORE:
5449 case XML_RELAXNG_ONEORMORE:
5450 case XML_RELAXNG_OPTIONAL:
5451 case XML_RELAXNG_CHOICE:
5452 case XML_RELAXNG_GROUP:
5453 case XML_RELAXNG_INTERLEAVE:
5454 if (last == NULL) {
5455 ret->content = last = cur;
5456 } else {
5457 if ((last->type == XML_RELAXNG_ELEMENT) &&
5458 (ret->content == last)) {
5459 ret->content = xmlRelaxNGNewDefine(ctxt, node);
5460 if (ret->content != NULL) {
5461 ret->content->type = XML_RELAXNG_GROUP;
5462 ret->content->content = last;
5463 } else {
5464 ret->content = last;
5465 }
5466 }
5467 last->next = cur;
5468 last = cur;
5469 }
5470 break;
5471 case XML_RELAXNG_ATTRIBUTE:
5472 cur->next = ret->attrs;
5473 ret->attrs = cur;
5474 break;
5475 case XML_RELAXNG_START:
5476 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5477 "RNG Internal error, start found in element\n",
5478 NULL, NULL);
5479 break;
5480 case XML_RELAXNG_PARAM:
5481 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5482 "RNG Internal error, param found in element\n",
5483 NULL, NULL);
5484 break;
5485 case XML_RELAXNG_EXCEPT:
5486 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5487 "RNG Internal error, except found in element\n",
5488 NULL, NULL);
5489 break;
5490 case XML_RELAXNG_NOOP:
5491 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5492 "RNG Internal error, noop found in element\n",
5493 NULL, NULL);
5494 break;
5495 }
5496 }
5497 child = child->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005498 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00005499 ctxt->define = olddefine;
Daniel Veillard4c004142003-10-07 11:33:24 +00005500 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005501}
5502
5503/**
5504 * xmlRelaxNGParsePatterns:
5505 * @ctxt: a Relax-NG parser context
5506 * @nodes: list of nodes
Daniel Veillard154877e2003-01-30 12:17:05 +00005507 * @group: use an implicit <group> for elements
Daniel Veillard6eadf632003-01-23 18:29:16 +00005508 *
5509 * parse the content of a RelaxNG start node.
5510 *
5511 * Returns the definition pointer or NULL in case of error.
5512 */
5513static xmlRelaxNGDefinePtr
Daniel Veillard154877e2003-01-30 12:17:05 +00005514xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes,
Daniel Veillard4c004142003-10-07 11:33:24 +00005515 int group)
5516{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005517 xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005518
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005519 parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005520 while (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005521 if (IS_RELAXNG(nodes, "element")) {
5522 cur = xmlRelaxNGParseElement(ctxt, nodes);
5523 if (def == NULL) {
5524 def = last = cur;
5525 } else {
5526 if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) &&
5527 (def == last)) {
5528 def = xmlRelaxNGNewDefine(ctxt, nodes);
5529 def->type = XML_RELAXNG_GROUP;
5530 def->content = last;
5531 }
5532 last->next = cur;
5533 last = cur;
5534 }
5535 cur->parent = parent;
5536 } else {
5537 cur = xmlRelaxNGParsePattern(ctxt, nodes);
5538 if (cur != NULL) {
5539 if (def == NULL) {
5540 def = last = cur;
5541 } else {
5542 last->next = cur;
5543 last = cur;
5544 }
5545 }
5546 }
5547 nodes = nodes->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005548 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005549 return (def);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005550}
5551
5552/**
5553 * xmlRelaxNGParseStart:
5554 * @ctxt: a Relax-NG parser context
5555 * @nodes: start children nodes
5556 *
5557 * parse the content of a RelaxNG start node.
5558 *
5559 * Returns 0 in case of success, -1 in case of error
5560 */
5561static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005562xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
5563{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005564 int ret = 0;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005565 xmlRelaxNGDefinePtr def = NULL, last;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005566
Daniel Veillardd2298792003-02-14 16:54:11 +00005567 if (nodes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005568 xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, "start has no children\n",
5569 NULL, NULL);
5570 return (-1);
Daniel Veillardd2298792003-02-14 16:54:11 +00005571 }
5572 if (IS_RELAXNG(nodes, "empty")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005573 def = xmlRelaxNGNewDefine(ctxt, nodes);
5574 if (def == NULL)
5575 return (-1);
5576 def->type = XML_RELAXNG_EMPTY;
5577 if (nodes->children != NULL) {
5578 xmlRngPErr(ctxt, nodes, XML_RNGP_EMPTY_CONTENT,
5579 "element empty is not empty\n", NULL, NULL);
5580 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005581 } else if (IS_RELAXNG(nodes, "notAllowed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005582 def = xmlRelaxNGNewDefine(ctxt, nodes);
5583 if (def == NULL)
5584 return (-1);
5585 def->type = XML_RELAXNG_NOT_ALLOWED;
5586 if (nodes->children != NULL) {
5587 xmlRngPErr(ctxt, nodes, XML_RNGP_NOTALLOWED_NOT_EMPTY,
5588 "element notAllowed is not empty\n", NULL, NULL);
5589 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005590 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005591 def = xmlRelaxNGParsePatterns(ctxt, nodes, 1);
Daniel Veillard2df2de22003-02-17 23:34:33 +00005592 }
5593 if (ctxt->grammar->start != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005594 last = ctxt->grammar->start;
5595 while (last->next != NULL)
5596 last = last->next;
5597 last->next = def;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005598 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005599 ctxt->grammar->start = def;
Daniel Veillardd2298792003-02-14 16:54:11 +00005600 }
5601 nodes = nodes->next;
5602 if (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005603 xmlRngPErr(ctxt, nodes, XML_RNGP_START_CONTENT,
5604 "start more than one children\n", NULL, NULL);
5605 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005606 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005607 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005608}
5609
5610/**
5611 * xmlRelaxNGParseGrammarContent:
5612 * @ctxt: a Relax-NG parser context
5613 * @nodes: grammar children nodes
5614 *
5615 * parse the content of a RelaxNG grammar node.
5616 *
5617 * Returns 0 in case of success, -1 in case of error
5618 */
5619static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005620xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
5621 xmlNodePtr nodes)
Daniel Veillard6eadf632003-01-23 18:29:16 +00005622{
Daniel Veillarde2a5a082003-02-02 14:35:17 +00005623 int ret = 0, tmp;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005624
5625 if (nodes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005626 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_EMPTY,
5627 "grammar has no children\n", NULL, NULL);
5628 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005629 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005630 while (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005631 if (IS_RELAXNG(nodes, "start")) {
5632 if (nodes->children == NULL) {
5633 xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY,
5634 "start has no children\n", NULL, NULL);
5635 } else {
5636 tmp = xmlRelaxNGParseStart(ctxt, nodes->children);
5637 if (tmp != 0)
5638 ret = -1;
5639 }
5640 } else if (IS_RELAXNG(nodes, "define")) {
5641 tmp = xmlRelaxNGParseDefine(ctxt, nodes);
5642 if (tmp != 0)
5643 ret = -1;
5644 } else if (IS_RELAXNG(nodes, "include")) {
5645 tmp = xmlRelaxNGParseInclude(ctxt, nodes);
5646 if (tmp != 0)
5647 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005648 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005649 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
5650 "grammar has unexpected child %s\n", nodes->name,
5651 NULL);
5652 ret = -1;
5653 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005654 nodes = nodes->next;
5655 }
5656 return (ret);
5657}
5658
5659/**
5660 * xmlRelaxNGCheckReference:
5661 * @ref: the ref
5662 * @ctxt: a Relax-NG parser context
5663 * @name: the name associated to the defines
5664 *
5665 * Applies the 4.17. combine attribute rule for all the define
5666 * element of a given grammar using the same name.
5667 */
5668static void
5669xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref,
Daniel Veillard4c004142003-10-07 11:33:24 +00005670 xmlRelaxNGParserCtxtPtr ctxt,
5671 const xmlChar * name)
5672{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005673 xmlRelaxNGGrammarPtr grammar;
Daniel Veillard276be4a2003-01-24 01:03:34 +00005674 xmlRelaxNGDefinePtr def, cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005675
Daniel Veillardaa422d92009-09-24 11:31:48 +02005676 /*
5677 * Those rules don't apply to imported ref from xmlRelaxNGParseImportRef
5678 */
5679 if (ref->dflags & IS_EXTERNAL_REF)
5680 return;
5681
Daniel Veillard6eadf632003-01-23 18:29:16 +00005682 grammar = ctxt->grammar;
5683 if (grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005684 xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
5685 "Internal error: no grammar in CheckReference %s\n",
5686 name, NULL);
5687 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005688 }
5689 if (ref->content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005690 xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
5691 "Internal error: reference has content in CheckReference %s\n",
5692 name, NULL);
5693 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005694 }
5695 if (grammar->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005696 def = xmlHashLookup(grammar->defs, name);
5697 if (def != NULL) {
5698 cur = ref;
5699 while (cur != NULL) {
5700 cur->content = def;
5701 cur = cur->nextHash;
5702 }
5703 } else {
5704 xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
5705 "Reference %s has no matching definition\n", name,
5706 NULL);
5707 }
Daniel Veillardd4310742003-02-18 21:12:46 +00005708 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005709 xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
5710 "Reference %s has no matching definition\n", name,
5711 NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005712 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005713}
5714
5715/**
5716 * xmlRelaxNGCheckCombine:
5717 * @define: the define(s) list
5718 * @ctxt: a Relax-NG parser context
5719 * @name: the name associated to the defines
5720 *
5721 * Applies the 4.17. combine attribute rule for all the define
5722 * element of a given grammar using the same name.
5723 */
5724static void
5725xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define,
Daniel Veillard4c004142003-10-07 11:33:24 +00005726 xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * name)
5727{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005728 xmlChar *combine;
5729 int choiceOrInterleave = -1;
5730 int missing = 0;
5731 xmlRelaxNGDefinePtr cur, last, tmp, tmp2;
5732
5733 if (define->nextHash == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005734 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005735 cur = define;
5736 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005737 combine = xmlGetProp(cur->node, BAD_CAST "combine");
5738 if (combine != NULL) {
5739 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5740 if (choiceOrInterleave == -1)
5741 choiceOrInterleave = 1;
5742 else if (choiceOrInterleave == 0) {
5743 xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
5744 "Defines for %s use both 'choice' and 'interleave'\n",
5745 name, NULL);
5746 }
5747 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
5748 if (choiceOrInterleave == -1)
5749 choiceOrInterleave = 0;
5750 else if (choiceOrInterleave == 1) {
5751 xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
5752 "Defines for %s use both 'choice' and 'interleave'\n",
5753 name, NULL);
5754 }
5755 } else {
5756 xmlRngPErr(ctxt, define->node, XML_RNGP_UNKNOWN_COMBINE,
5757 "Defines for %s use unknown combine value '%s''\n",
5758 name, combine);
5759 }
5760 xmlFree(combine);
5761 } else {
5762 if (missing == 0)
5763 missing = 1;
5764 else {
5765 xmlRngPErr(ctxt, define->node, XML_RNGP_NEED_COMBINE,
5766 "Some defines for %s needs the combine attribute\n",
5767 name, NULL);
5768 }
5769 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005770
Daniel Veillard4c004142003-10-07 11:33:24 +00005771 cur = cur->nextHash;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005772 }
5773#ifdef DEBUG
5774 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00005775 "xmlRelaxNGCheckCombine(): merging %s defines: %d\n",
5776 name, choiceOrInterleave);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005777#endif
5778 if (choiceOrInterleave == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +00005779 choiceOrInterleave = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005780 cur = xmlRelaxNGNewDefine(ctxt, define->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005781 if (cur == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005782 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005783 if (choiceOrInterleave == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00005784 cur->type = XML_RELAXNG_INTERLEAVE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005785 else
Daniel Veillard4c004142003-10-07 11:33:24 +00005786 cur->type = XML_RELAXNG_CHOICE;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005787 tmp = define;
5788 last = NULL;
5789 while (tmp != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005790 if (tmp->content != NULL) {
5791 if (tmp->content->next != NULL) {
5792 /*
5793 * we need first to create a wrapper.
5794 */
5795 tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node);
5796 if (tmp2 == NULL)
5797 break;
5798 tmp2->type = XML_RELAXNG_GROUP;
5799 tmp2->content = tmp->content;
5800 } else {
5801 tmp2 = tmp->content;
5802 }
5803 if (last == NULL) {
5804 cur->content = tmp2;
5805 } else {
5806 last->next = tmp2;
5807 }
5808 last = tmp2;
5809 }
5810 tmp->content = cur;
5811 tmp = tmp->nextHash;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005812 }
5813 define->content = cur;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005814 if (choiceOrInterleave == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005815 if (ctxt->interleaves == NULL)
5816 ctxt->interleaves = xmlHashCreate(10);
5817 if (ctxt->interleaves == NULL) {
5818 xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5819 "Failed to create interleaves hash table\n", NULL,
5820 NULL);
5821 } else {
5822 char tmpname[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00005823
Daniel Veillard4c004142003-10-07 11:33:24 +00005824 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5825 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
5826 0) {
5827 xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5828 "Failed to add %s to hash table\n",
5829 (const xmlChar *) tmpname, NULL);
5830 }
5831 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005832 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005833}
5834
5835/**
5836 * xmlRelaxNGCombineStart:
5837 * @ctxt: a Relax-NG parser context
5838 * @grammar: the grammar
5839 *
5840 * Applies the 4.17. combine rule for all the start
5841 * element of a given grammar.
5842 */
5843static void
5844xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00005845 xmlRelaxNGGrammarPtr grammar)
5846{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005847 xmlRelaxNGDefinePtr starts;
5848 xmlChar *combine;
5849 int choiceOrInterleave = -1;
5850 int missing = 0;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005851 xmlRelaxNGDefinePtr cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005852
Daniel Veillard2df2de22003-02-17 23:34:33 +00005853 starts = grammar->start;
5854 if ((starts == NULL) || (starts->next == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00005855 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005856 cur = starts;
5857 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005858 if ((cur->node == NULL) || (cur->node->parent == NULL) ||
5859 (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) {
5860 combine = NULL;
5861 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_MISSING,
5862 "Internal error: start element not found\n", NULL,
5863 NULL);
5864 } else {
5865 combine = xmlGetProp(cur->node->parent, BAD_CAST "combine");
5866 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005867
Daniel Veillard4c004142003-10-07 11:33:24 +00005868 if (combine != NULL) {
5869 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5870 if (choiceOrInterleave == -1)
5871 choiceOrInterleave = 1;
5872 else if (choiceOrInterleave == 0) {
5873 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
5874 "<start> use both 'choice' and 'interleave'\n",
5875 NULL, NULL);
5876 }
5877 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
5878 if (choiceOrInterleave == -1)
5879 choiceOrInterleave = 0;
5880 else if (choiceOrInterleave == 1) {
5881 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
5882 "<start> use both 'choice' and 'interleave'\n",
5883 NULL, NULL);
5884 }
5885 } else {
5886 xmlRngPErr(ctxt, cur->node, XML_RNGP_UNKNOWN_COMBINE,
5887 "<start> uses unknown combine value '%s''\n",
5888 combine, NULL);
5889 }
5890 xmlFree(combine);
5891 } else {
5892 if (missing == 0)
5893 missing = 1;
5894 else {
5895 xmlRngPErr(ctxt, cur->node, XML_RNGP_NEED_COMBINE,
5896 "Some <start> element miss the combine attribute\n",
5897 NULL, NULL);
5898 }
5899 }
5900
5901 cur = cur->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005902 }
5903#ifdef DEBUG
5904 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00005905 "xmlRelaxNGCombineStart(): merging <start>: %d\n",
5906 choiceOrInterleave);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005907#endif
5908 if (choiceOrInterleave == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +00005909 choiceOrInterleave = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005910 cur = xmlRelaxNGNewDefine(ctxt, starts->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005911 if (cur == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005912 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005913 if (choiceOrInterleave == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00005914 cur->type = XML_RELAXNG_INTERLEAVE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005915 else
Daniel Veillard4c004142003-10-07 11:33:24 +00005916 cur->type = XML_RELAXNG_CHOICE;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005917 cur->content = grammar->start;
5918 grammar->start = cur;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005919 if (choiceOrInterleave == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005920 if (ctxt->interleaves == NULL)
5921 ctxt->interleaves = xmlHashCreate(10);
5922 if (ctxt->interleaves == NULL) {
5923 xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5924 "Failed to create interleaves hash table\n", NULL,
5925 NULL);
5926 } else {
5927 char tmpname[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00005928
Daniel Veillard4c004142003-10-07 11:33:24 +00005929 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5930 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
5931 0) {
5932 xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5933 "Failed to add %s to hash table\n",
5934 (const xmlChar *) tmpname, NULL);
5935 }
5936 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005937 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005938}
5939
5940/**
Daniel Veillardd4310742003-02-18 21:12:46 +00005941 * xmlRelaxNGCheckCycles:
5942 * @ctxt: a Relax-NG parser context
5943 * @nodes: grammar children nodes
5944 * @depth: the counter
5945 *
5946 * Check for cycles.
5947 *
5948 * Returns 0 if check passed, and -1 in case of error
5949 */
5950static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005951xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt,
5952 xmlRelaxNGDefinePtr cur, int depth)
5953{
Daniel Veillardd4310742003-02-18 21:12:46 +00005954 int ret = 0;
5955
5956 while ((ret == 0) && (cur != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005957 if ((cur->type == XML_RELAXNG_REF) ||
5958 (cur->type == XML_RELAXNG_PARENTREF)) {
5959 if (cur->depth == -1) {
5960 cur->depth = depth;
5961 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5962 cur->depth = -2;
5963 } else if (depth == cur->depth) {
5964 xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_CYCLE,
5965 "Detected a cycle in %s references\n",
5966 cur->name, NULL);
5967 return (-1);
5968 }
5969 } else if (cur->type == XML_RELAXNG_ELEMENT) {
5970 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1);
5971 } else {
5972 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5973 }
5974 cur = cur->next;
Daniel Veillardd4310742003-02-18 21:12:46 +00005975 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005976 return (ret);
Daniel Veillardd4310742003-02-18 21:12:46 +00005977}
5978
5979/**
Daniel Veillard77648bb2003-02-20 15:03:22 +00005980 * xmlRelaxNGTryUnlink:
5981 * @ctxt: a Relax-NG parser context
5982 * @cur: the definition to unlink
5983 * @parent: the parent definition
5984 * @prev: the previous sibling definition
5985 *
5986 * Try to unlink a definition. If not possble make it a NOOP
5987 *
5988 * Returns the new prev definition
5989 */
5990static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00005991xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
5992 xmlRelaxNGDefinePtr cur,
5993 xmlRelaxNGDefinePtr parent, xmlRelaxNGDefinePtr prev)
5994{
Daniel Veillardfd573f12003-03-16 17:52:32 +00005995 if (prev != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005996 prev->next = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005997 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005998 if (parent != NULL) {
5999 if (parent->content == cur)
6000 parent->content = cur->next;
6001 else if (parent->attrs == cur)
6002 parent->attrs = cur->next;
6003 else if (parent->nameClass == cur)
6004 parent->nameClass = cur->next;
6005 } else {
6006 cur->type = XML_RELAXNG_NOOP;
6007 prev = cur;
6008 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00006009 }
Daniel Veillard4c004142003-10-07 11:33:24 +00006010 return (prev);
Daniel Veillard77648bb2003-02-20 15:03:22 +00006011}
6012
6013/**
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006014 * xmlRelaxNGSimplify:
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006015 * @ctxt: a Relax-NG parser context
6016 * @nodes: grammar children nodes
6017 *
6018 * Check for simplification of empty and notAllowed
6019 */
6020static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006021xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt,
6022 xmlRelaxNGDefinePtr cur, xmlRelaxNGDefinePtr parent)
6023{
Daniel Veillardfd573f12003-03-16 17:52:32 +00006024 xmlRelaxNGDefinePtr prev = NULL;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006025
Daniel Veillardfd573f12003-03-16 17:52:32 +00006026 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006027 if ((cur->type == XML_RELAXNG_REF) ||
6028 (cur->type == XML_RELAXNG_PARENTREF)) {
6029 if (cur->depth != -3) {
6030 cur->depth = -3;
6031 xmlRelaxNGSimplify(ctxt, cur->content, cur);
6032 }
6033 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
6034 cur->parent = parent;
6035 if ((parent != NULL) &&
6036 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
6037 (parent->type == XML_RELAXNG_LIST) ||
6038 (parent->type == XML_RELAXNG_GROUP) ||
6039 (parent->type == XML_RELAXNG_INTERLEAVE) ||
6040 (parent->type == XML_RELAXNG_ONEORMORE) ||
6041 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6042 parent->type = XML_RELAXNG_NOT_ALLOWED;
6043 break;
6044 }
6045 if ((parent != NULL) && (parent->type == XML_RELAXNG_CHOICE)) {
6046 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6047 } else
6048 prev = cur;
6049 } else if (cur->type == XML_RELAXNG_EMPTY) {
6050 cur->parent = parent;
6051 if ((parent != NULL) &&
6052 ((parent->type == XML_RELAXNG_ONEORMORE) ||
6053 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6054 parent->type = XML_RELAXNG_EMPTY;
6055 break;
6056 }
6057 if ((parent != NULL) &&
6058 ((parent->type == XML_RELAXNG_GROUP) ||
6059 (parent->type == XML_RELAXNG_INTERLEAVE))) {
6060 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6061 } else
6062 prev = cur;
6063 } else {
6064 cur->parent = parent;
6065 if (cur->content != NULL)
6066 xmlRelaxNGSimplify(ctxt, cur->content, cur);
6067 if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL))
6068 xmlRelaxNGSimplify(ctxt, cur->attrs, cur);
6069 if (cur->nameClass != NULL)
6070 xmlRelaxNGSimplify(ctxt, cur->nameClass, cur);
6071 /*
6072 * On Elements, try to move attribute only generating rules on
6073 * the attrs rules.
6074 */
6075 if (cur->type == XML_RELAXNG_ELEMENT) {
6076 int attronly;
6077 xmlRelaxNGDefinePtr tmp, pre;
Daniel Veillardce192eb2003-04-16 15:58:05 +00006078
Daniel Veillard4c004142003-10-07 11:33:24 +00006079 while (cur->content != NULL) {
6080 attronly =
6081 xmlRelaxNGGenerateAttributes(ctxt, cur->content);
6082 if (attronly == 1) {
6083 /*
6084 * migrate cur->content to attrs
6085 */
6086 tmp = cur->content;
6087 cur->content = tmp->next;
6088 tmp->next = cur->attrs;
6089 cur->attrs = tmp;
6090 } else {
6091 /*
6092 * cur->content can generate elements or text
6093 */
6094 break;
6095 }
6096 }
6097 pre = cur->content;
6098 while ((pre != NULL) && (pre->next != NULL)) {
6099 tmp = pre->next;
6100 attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp);
6101 if (attronly == 1) {
6102 /*
6103 * migrate tmp to attrs
6104 */
6105 pre->next = tmp->next;
6106 tmp->next = cur->attrs;
6107 cur->attrs = tmp;
6108 } else {
6109 pre = tmp;
6110 }
6111 }
6112 }
6113 /*
6114 * This may result in a simplification
6115 */
6116 if ((cur->type == XML_RELAXNG_GROUP) ||
6117 (cur->type == XML_RELAXNG_INTERLEAVE)) {
6118 if (cur->content == NULL)
6119 cur->type = XML_RELAXNG_EMPTY;
6120 else if (cur->content->next == NULL) {
6121 if ((parent == NULL) && (prev == NULL)) {
6122 cur->type = XML_RELAXNG_NOOP;
6123 } else if (prev == NULL) {
6124 parent->content = cur->content;
6125 cur->content->next = cur->next;
6126 cur = cur->content;
6127 } else {
6128 cur->content->next = cur->next;
6129 prev->next = cur->content;
6130 cur = cur->content;
6131 }
6132 }
6133 }
6134 /*
6135 * the current node may have been transformed back
6136 */
6137 if ((cur->type == XML_RELAXNG_EXCEPT) &&
6138 (cur->content != NULL) &&
6139 (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) {
6140 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6141 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
6142 if ((parent != NULL) &&
6143 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
6144 (parent->type == XML_RELAXNG_LIST) ||
6145 (parent->type == XML_RELAXNG_GROUP) ||
6146 (parent->type == XML_RELAXNG_INTERLEAVE) ||
6147 (parent->type == XML_RELAXNG_ONEORMORE) ||
6148 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6149 parent->type = XML_RELAXNG_NOT_ALLOWED;
6150 break;
6151 }
6152 if ((parent != NULL) &&
6153 (parent->type == XML_RELAXNG_CHOICE)) {
6154 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6155 } else
6156 prev = cur;
6157 } else if (cur->type == XML_RELAXNG_EMPTY) {
6158 if ((parent != NULL) &&
6159 ((parent->type == XML_RELAXNG_ONEORMORE) ||
6160 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6161 parent->type = XML_RELAXNG_EMPTY;
6162 break;
6163 }
6164 if ((parent != NULL) &&
6165 ((parent->type == XML_RELAXNG_GROUP) ||
6166 (parent->type == XML_RELAXNG_INTERLEAVE) ||
6167 (parent->type == XML_RELAXNG_CHOICE))) {
6168 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6169 } else
6170 prev = cur;
6171 } else {
6172 prev = cur;
6173 }
6174 }
6175 cur = cur->next;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006176 }
6177}
6178
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006179/**
6180 * xmlRelaxNGGroupContentType:
6181 * @ct1: the first content type
6182 * @ct2: the second content type
6183 *
6184 * Try to group 2 content types
6185 *
6186 * Returns the content type
6187 */
6188static xmlRelaxNGContentType
6189xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1,
Daniel Veillard4c004142003-10-07 11:33:24 +00006190 xmlRelaxNGContentType ct2)
6191{
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006192 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006193 (ct2 == XML_RELAXNG_CONTENT_ERROR))
6194 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006195 if (ct1 == XML_RELAXNG_CONTENT_EMPTY)
Daniel Veillard4c004142003-10-07 11:33:24 +00006196 return (ct2);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006197 if (ct2 == XML_RELAXNG_CONTENT_EMPTY)
Daniel Veillard4c004142003-10-07 11:33:24 +00006198 return (ct1);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006199 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00006200 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6201 return (XML_RELAXNG_CONTENT_COMPLEX);
6202 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006203}
6204
6205/**
6206 * xmlRelaxNGMaxContentType:
6207 * @ct1: the first content type
6208 * @ct2: the second content type
6209 *
6210 * Compute the max content-type
6211 *
6212 * Returns the content type
6213 */
6214static xmlRelaxNGContentType
6215xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1,
Daniel Veillard4c004142003-10-07 11:33:24 +00006216 xmlRelaxNGContentType ct2)
6217{
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006218 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006219 (ct2 == XML_RELAXNG_CONTENT_ERROR))
6220 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006221 if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006222 (ct2 == XML_RELAXNG_CONTENT_SIMPLE))
6223 return (XML_RELAXNG_CONTENT_SIMPLE);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006224 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006225 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6226 return (XML_RELAXNG_CONTENT_COMPLEX);
6227 return (XML_RELAXNG_CONTENT_EMPTY);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006228}
Daniel Veillard77648bb2003-02-20 15:03:22 +00006229
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006230/**
6231 * xmlRelaxNGCheckRules:
6232 * @ctxt: a Relax-NG parser context
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006233 * @cur: the current definition
Daniel Veillard77648bb2003-02-20 15:03:22 +00006234 * @flags: some accumulated flags
Daniel Veillardfd573f12003-03-16 17:52:32 +00006235 * @ptype: the parent type
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006236 *
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006237 * Check for rules in section 7.1 and 7.2
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006238 *
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006239 * Returns the content type of @cur
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006240 */
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006241static xmlRelaxNGContentType
Daniel Veillard4c004142003-10-07 11:33:24 +00006242xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt,
6243 xmlRelaxNGDefinePtr cur, int flags,
6244 xmlRelaxNGType ptype)
6245{
Daniel Veillardd44b9362009-09-07 12:15:08 +02006246 int nflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006247 xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006248
Daniel Veillardfd573f12003-03-16 17:52:32 +00006249 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006250 ret = XML_RELAXNG_CONTENT_EMPTY;
6251 if ((cur->type == XML_RELAXNG_REF) ||
6252 (cur->type == XML_RELAXNG_PARENTREF)) {
Daniel Veillard63d68a32005-03-31 13:50:00 +00006253 /*
6254 * This should actually be caught by list//element(ref) at the
6255 * element boundaries, c.f. Bug #159968 local refs are dropped
6256 * in step 4.19.
6257 */
6258#if 0
Daniel Veillard4c004142003-10-07 11:33:24 +00006259 if (flags & XML_RELAXNG_IN_LIST) {
6260 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_REF,
6261 "Found forbidden pattern list//ref\n", NULL,
6262 NULL);
6263 }
Daniel Veillard63d68a32005-03-31 13:50:00 +00006264#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00006265 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6266 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_REF,
6267 "Found forbidden pattern data/except//ref\n",
6268 NULL, NULL);
6269 }
Daniel Veillard81c51e12009-08-14 18:52:10 +02006270 if (cur->content == NULL) {
6271 if (cur->type == XML_RELAXNG_PARENTREF)
6272 xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_NO_DEF,
6273 "Internal found no define for parent refs\n",
6274 NULL, NULL);
6275 else
6276 xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_NO_DEF,
6277 "Internal found no define for ref %s\n",
Daniel Veillarda4f27cb2009-08-21 17:34:17 +02006278 (cur->name ? cur->name: BAD_CAST "null"), NULL);
Daniel Veillard81c51e12009-08-14 18:52:10 +02006279 }
Daniel Veillard4c004142003-10-07 11:33:24 +00006280 if (cur->depth > -4) {
6281 cur->depth = -4;
6282 ret = xmlRelaxNGCheckRules(ctxt, cur->content,
6283 flags, cur->type);
6284 cur->depth = ret - 15;
6285 } else if (cur->depth == -4) {
6286 ret = XML_RELAXNG_CONTENT_COMPLEX;
6287 } else {
6288 ret = (xmlRelaxNGContentType) (cur->depth + 15);
6289 }
6290 } else if (cur->type == XML_RELAXNG_ELEMENT) {
6291 /*
6292 * The 7.3 Attribute derivation rule for groups is plugged there
6293 */
6294 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6295 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6296 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ELEM,
6297 "Found forbidden pattern data/except//element(ref)\n",
6298 NULL, NULL);
6299 }
6300 if (flags & XML_RELAXNG_IN_LIST) {
6301 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ELEM,
6302 "Found forbidden pattern list//element(ref)\n",
6303 NULL, NULL);
6304 }
6305 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6306 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
6307 "Found forbidden pattern attribute//element(ref)\n",
6308 NULL, NULL);
6309 }
6310 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6311 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
6312 "Found forbidden pattern attribute//element(ref)\n",
6313 NULL, NULL);
6314 }
6315 /*
6316 * reset since in the simple form elements are only child
6317 * of grammar/define
6318 */
6319 nflags = 0;
6320 ret =
6321 xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type);
6322 if (ret != XML_RELAXNG_CONTENT_EMPTY) {
6323 xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_EMPTY,
6324 "Element %s attributes have a content type error\n",
6325 cur->name, NULL);
6326 }
6327 ret =
6328 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6329 cur->type);
6330 if (ret == XML_RELAXNG_CONTENT_ERROR) {
6331 xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_ERROR,
6332 "Element %s has a content type error\n",
6333 cur->name, NULL);
6334 } else {
6335 ret = XML_RELAXNG_CONTENT_COMPLEX;
6336 }
6337 } else if (cur->type == XML_RELAXNG_ATTRIBUTE) {
6338 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6339 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ATTR,
6340 "Found forbidden pattern attribute//attribute\n",
6341 NULL, NULL);
6342 }
6343 if (flags & XML_RELAXNG_IN_LIST) {
6344 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ATTR,
6345 "Found forbidden pattern list//attribute\n",
6346 NULL, NULL);
6347 }
6348 if (flags & XML_RELAXNG_IN_OOMGROUP) {
6349 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_GROUP_ATTR,
6350 "Found forbidden pattern oneOrMore//group//attribute\n",
6351 NULL, NULL);
6352 }
6353 if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) {
6354 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_INTERLEAVE_ATTR,
6355 "Found forbidden pattern oneOrMore//interleave//attribute\n",
6356 NULL, NULL);
6357 }
6358 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6359 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ATTR,
6360 "Found forbidden pattern data/except//attribute\n",
6361 NULL, NULL);
6362 }
6363 if (flags & XML_RELAXNG_IN_START) {
6364 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ATTR,
6365 "Found forbidden pattern start//attribute\n",
6366 NULL, NULL);
6367 }
6368 if ((!(flags & XML_RELAXNG_IN_ONEORMORE))
6369 && (cur->name == NULL)) {
6370 if (cur->ns == NULL) {
6371 xmlRngPErr(ctxt, cur->node, XML_RNGP_ANYNAME_ATTR_ANCESTOR,
6372 "Found anyName attribute without oneOrMore ancestor\n",
6373 NULL, NULL);
6374 } else {
6375 xmlRngPErr(ctxt, cur->node, XML_RNGP_NSNAME_ATTR_ANCESTOR,
6376 "Found nsName attribute without oneOrMore ancestor\n",
6377 NULL, NULL);
6378 }
6379 }
6380 nflags = flags | XML_RELAXNG_IN_ATTRIBUTE;
6381 xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
6382 ret = XML_RELAXNG_CONTENT_EMPTY;
6383 } else if ((cur->type == XML_RELAXNG_ONEORMORE) ||
6384 (cur->type == XML_RELAXNG_ZEROORMORE)) {
6385 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6386 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ONEMORE,
6387 "Found forbidden pattern data/except//oneOrMore\n",
6388 NULL, NULL);
6389 }
6390 if (flags & XML_RELAXNG_IN_START) {
6391 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ONEMORE,
6392 "Found forbidden pattern start//oneOrMore\n",
6393 NULL, NULL);
6394 }
6395 nflags = flags | XML_RELAXNG_IN_ONEORMORE;
6396 ret =
6397 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6398 cur->type);
6399 ret = xmlRelaxNGGroupContentType(ret, ret);
6400 } else if (cur->type == XML_RELAXNG_LIST) {
6401 if (flags & XML_RELAXNG_IN_LIST) {
6402 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_LIST,
6403 "Found forbidden pattern list//list\n", NULL,
6404 NULL);
6405 }
6406 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6407 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_LIST,
6408 "Found forbidden pattern data/except//list\n",
6409 NULL, NULL);
6410 }
6411 if (flags & XML_RELAXNG_IN_START) {
6412 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_LIST,
6413 "Found forbidden pattern start//list\n", NULL,
6414 NULL);
6415 }
6416 nflags = flags | XML_RELAXNG_IN_LIST;
6417 ret =
6418 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6419 cur->type);
6420 } else if (cur->type == XML_RELAXNG_GROUP) {
6421 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6422 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_GROUP,
6423 "Found forbidden pattern data/except//group\n",
6424 NULL, NULL);
6425 }
6426 if (flags & XML_RELAXNG_IN_START) {
6427 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_GROUP,
6428 "Found forbidden pattern start//group\n", NULL,
6429 NULL);
6430 }
6431 if (flags & XML_RELAXNG_IN_ONEORMORE)
6432 nflags = flags | XML_RELAXNG_IN_OOMGROUP;
6433 else
6434 nflags = flags;
6435 ret =
6436 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6437 cur->type);
6438 /*
6439 * The 7.3 Attribute derivation rule for groups is plugged there
6440 */
6441 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6442 } else if (cur->type == XML_RELAXNG_INTERLEAVE) {
6443 if (flags & XML_RELAXNG_IN_LIST) {
6444 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_INTERLEAVE,
6445 "Found forbidden pattern list//interleave\n",
6446 NULL, NULL);
6447 }
6448 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6449 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
6450 "Found forbidden pattern data/except//interleave\n",
6451 NULL, NULL);
6452 }
6453 if (flags & XML_RELAXNG_IN_START) {
6454 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
6455 "Found forbidden pattern start//interleave\n",
6456 NULL, NULL);
6457 }
6458 if (flags & XML_RELAXNG_IN_ONEORMORE)
6459 nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE;
6460 else
6461 nflags = flags;
6462 ret =
6463 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6464 cur->type);
6465 } else if (cur->type == XML_RELAXNG_EXCEPT) {
6466 if ((cur->parent != NULL) &&
6467 (cur->parent->type == XML_RELAXNG_DATATYPE))
6468 nflags = flags | XML_RELAXNG_IN_DATAEXCEPT;
6469 else
6470 nflags = flags;
6471 ret =
6472 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6473 cur->type);
6474 } else if (cur->type == XML_RELAXNG_DATATYPE) {
6475 if (flags & XML_RELAXNG_IN_START) {
6476 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_DATA,
6477 "Found forbidden pattern start//data\n", NULL,
6478 NULL);
6479 }
6480 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6481 ret = XML_RELAXNG_CONTENT_SIMPLE;
6482 } else if (cur->type == XML_RELAXNG_VALUE) {
6483 if (flags & XML_RELAXNG_IN_START) {
6484 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_VALUE,
6485 "Found forbidden pattern start//value\n", NULL,
6486 NULL);
6487 }
6488 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6489 ret = XML_RELAXNG_CONTENT_SIMPLE;
6490 } else if (cur->type == XML_RELAXNG_TEXT) {
6491 if (flags & XML_RELAXNG_IN_LIST) {
6492 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_TEXT,
6493 "Found forbidden pattern list//text\n", NULL,
6494 NULL);
6495 }
6496 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6497 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_TEXT,
6498 "Found forbidden pattern data/except//text\n",
6499 NULL, NULL);
6500 }
6501 if (flags & XML_RELAXNG_IN_START) {
6502 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_TEXT,
6503 "Found forbidden pattern start//text\n", NULL,
6504 NULL);
6505 }
6506 ret = XML_RELAXNG_CONTENT_COMPLEX;
6507 } else if (cur->type == XML_RELAXNG_EMPTY) {
6508 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6509 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_EMPTY,
6510 "Found forbidden pattern data/except//empty\n",
6511 NULL, NULL);
6512 }
6513 if (flags & XML_RELAXNG_IN_START) {
6514 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_EMPTY,
6515 "Found forbidden pattern start//empty\n", NULL,
6516 NULL);
6517 }
6518 ret = XML_RELAXNG_CONTENT_EMPTY;
6519 } else if (cur->type == XML_RELAXNG_CHOICE) {
6520 xmlRelaxNGCheckChoiceDeterminism(ctxt, cur);
6521 ret =
6522 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6523 } else {
6524 ret =
6525 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6526 }
6527 cur = cur->next;
6528 if (ptype == XML_RELAXNG_GROUP) {
6529 val = xmlRelaxNGGroupContentType(val, ret);
6530 } else if (ptype == XML_RELAXNG_INTERLEAVE) {
Daniel Veillard594e5df2009-09-07 14:58:47 +02006531 /*
6532 * TODO: scan complain that tmp is never used, seems on purpose
6533 * need double-checking
6534 */
Daniel Veillard4c004142003-10-07 11:33:24 +00006535 tmp = xmlRelaxNGGroupContentType(val, ret);
6536 if (tmp != XML_RELAXNG_CONTENT_ERROR)
6537 tmp = xmlRelaxNGMaxContentType(val, ret);
6538 } else if (ptype == XML_RELAXNG_CHOICE) {
6539 val = xmlRelaxNGMaxContentType(val, ret);
6540 } else if (ptype == XML_RELAXNG_LIST) {
6541 val = XML_RELAXNG_CONTENT_SIMPLE;
6542 } else if (ptype == XML_RELAXNG_EXCEPT) {
6543 if (ret == XML_RELAXNG_CONTENT_ERROR)
6544 val = XML_RELAXNG_CONTENT_ERROR;
6545 else
6546 val = XML_RELAXNG_CONTENT_SIMPLE;
6547 } else {
6548 val = xmlRelaxNGGroupContentType(val, ret);
6549 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00006550
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006551 }
Daniel Veillard4c004142003-10-07 11:33:24 +00006552 return (val);
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006553}
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006554
6555/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00006556 * xmlRelaxNGParseGrammar:
6557 * @ctxt: a Relax-NG parser context
6558 * @nodes: grammar children nodes
6559 *
6560 * parse a Relax-NG <grammar> node
6561 *
6562 * Returns the internal xmlRelaxNGGrammarPtr built or
6563 * NULL in case of error
6564 */
6565static xmlRelaxNGGrammarPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006566xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
6567{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006568 xmlRelaxNGGrammarPtr ret, tmp, old;
6569
Daniel Veillardc482e262003-02-26 14:48:48 +00006570#ifdef DEBUG_GRAMMAR
6571 xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n");
6572#endif
6573
Daniel Veillard6eadf632003-01-23 18:29:16 +00006574 ret = xmlRelaxNGNewGrammar(ctxt);
6575 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006576 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006577
6578 /*
6579 * Link the new grammar in the tree
6580 */
6581 ret->parent = ctxt->grammar;
6582 if (ctxt->grammar != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006583 tmp = ctxt->grammar->children;
6584 if (tmp == NULL) {
6585 ctxt->grammar->children = ret;
6586 } else {
6587 while (tmp->next != NULL)
6588 tmp = tmp->next;
6589 tmp->next = ret;
6590 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006591 }
6592
6593 old = ctxt->grammar;
6594 ctxt->grammar = ret;
6595 xmlRelaxNGParseGrammarContent(ctxt, nodes);
6596 ctxt->grammar = ret;
Daniel Veillard2df2de22003-02-17 23:34:33 +00006597 if (ctxt->grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006598 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
6599 "Failed to parse <grammar> content\n", NULL, NULL);
Daniel Veillard2df2de22003-02-17 23:34:33 +00006600 } else if (ctxt->grammar->start == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006601 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_NO_START,
6602 "Element <grammar> has no <start>\n", NULL, NULL);
Daniel Veillard2df2de22003-02-17 23:34:33 +00006603 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006604
6605 /*
6606 * Apply 4.17 mergingd rules to defines and starts
6607 */
6608 xmlRelaxNGCombineStart(ctxt, ret);
6609 if (ret->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006610 xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine,
6611 ctxt);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006612 }
6613
6614 /*
6615 * link together defines and refs in this grammar
6616 */
6617 if (ret->refs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006618 xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference,
6619 ctxt);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006620 }
Daniel Veillard952379b2003-03-17 15:37:12 +00006621
Daniel Veillard81c51e12009-08-14 18:52:10 +02006622
Daniel Veillard25a1ce92008-06-02 16:04:12 +00006623 /* @@@@ */
6624
Daniel Veillard6eadf632003-01-23 18:29:16 +00006625 ctxt->grammar = old;
Daniel Veillard4c004142003-10-07 11:33:24 +00006626 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006627}
6628
6629/**
6630 * xmlRelaxNGParseDocument:
6631 * @ctxt: a Relax-NG parser context
6632 * @node: the root node of the RelaxNG schema
6633 *
6634 * parse a Relax-NG definition resource and build an internal
6635 * xmlRelaxNG struture which can be used to validate instances.
6636 *
6637 * Returns the internal XML RelaxNG structure built or
6638 * NULL in case of error
6639 */
6640static xmlRelaxNGPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006641xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
6642{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006643 xmlRelaxNGPtr schema = NULL;
Daniel Veillard276be4a2003-01-24 01:03:34 +00006644 const xmlChar *olddefine;
Daniel Veillarde431a272003-01-29 23:02:33 +00006645 xmlRelaxNGGrammarPtr old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006646
6647 if ((ctxt == NULL) || (node == NULL))
6648 return (NULL);
6649
6650 schema = xmlRelaxNGNewRelaxNG(ctxt);
6651 if (schema == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006652 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006653
Daniel Veillard276be4a2003-01-24 01:03:34 +00006654 olddefine = ctxt->define;
6655 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006656 if (IS_RELAXNG(node, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006657 schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006658 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00006659 xmlRelaxNGGrammarPtr tmp, ret;
Daniel Veillardc482e262003-02-26 14:48:48 +00006660
Daniel Veillard4c004142003-10-07 11:33:24 +00006661 schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt);
6662 if (schema->topgrammar == NULL) {
6663 return (schema);
6664 }
6665 /*
6666 * Link the new grammar in the tree
6667 */
6668 ret->parent = ctxt->grammar;
6669 if (ctxt->grammar != NULL) {
6670 tmp = ctxt->grammar->children;
6671 if (tmp == NULL) {
6672 ctxt->grammar->children = ret;
6673 } else {
6674 while (tmp->next != NULL)
6675 tmp = tmp->next;
6676 tmp->next = ret;
6677 }
6678 }
6679 old = ctxt->grammar;
6680 ctxt->grammar = ret;
6681 xmlRelaxNGParseStart(ctxt, node);
6682 if (old != NULL)
6683 ctxt->grammar = old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006684 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00006685 ctxt->define = olddefine;
Daniel Veillardd4310742003-02-18 21:12:46 +00006686 if (schema->topgrammar->start != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006687 xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0);
6688 if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) {
6689 xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL);
6690 while ((schema->topgrammar->start != NULL) &&
6691 (schema->topgrammar->start->type == XML_RELAXNG_NOOP) &&
6692 (schema->topgrammar->start->next != NULL))
6693 schema->topgrammar->start =
6694 schema->topgrammar->start->content;
6695 xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start,
6696 XML_RELAXNG_IN_START, XML_RELAXNG_NOOP);
6697 }
Daniel Veillardd4310742003-02-18 21:12:46 +00006698 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006699#ifdef DEBUG
6700 if (schema == NULL)
6701 xmlGenericError(xmlGenericErrorContext,
6702 "xmlRelaxNGParseDocument() failed\n");
6703#endif
6704
6705 return (schema);
6706}
6707
6708/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006709 * *
6710 * Reading RelaxNGs *
6711 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00006712 ************************************************************************/
6713
6714/**
6715 * xmlRelaxNGNewParserCtxt:
6716 * @URL: the location of the schema
6717 *
6718 * Create an XML RelaxNGs parse context for that file/resource expected
6719 * to contain an XML RelaxNGs file.
6720 *
6721 * Returns the parser context or NULL in case of error
6722 */
6723xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006724xmlRelaxNGNewParserCtxt(const char *URL)
6725{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006726 xmlRelaxNGParserCtxtPtr ret;
6727
6728 if (URL == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006729 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006730
Daniel Veillard4c004142003-10-07 11:33:24 +00006731 ret =
6732 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard6eadf632003-01-23 18:29:16 +00006733 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006734 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00006735 return (NULL);
6736 }
6737 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard4c004142003-10-07 11:33:24 +00006738 ret->URL = xmlStrdup((const xmlChar *) URL);
Daniel Veillard1703c5f2003-02-10 14:28:44 +00006739 ret->error = xmlGenericError;
6740 ret->userData = xmlGenericErrorContext;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006741 return (ret);
6742}
6743
6744/**
6745 * xmlRelaxNGNewMemParserCtxt:
6746 * @buffer: a pointer to a char array containing the schemas
6747 * @size: the size of the array
6748 *
6749 * Create an XML RelaxNGs parse context for that memory buffer expected
6750 * to contain an XML RelaxNGs file.
6751 *
6752 * Returns the parser context or NULL in case of error
6753 */
6754xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006755xmlRelaxNGNewMemParserCtxt(const char *buffer, int size)
6756{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006757 xmlRelaxNGParserCtxtPtr ret;
6758
6759 if ((buffer == NULL) || (size <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00006760 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006761
Daniel Veillard4c004142003-10-07 11:33:24 +00006762 ret =
6763 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard6eadf632003-01-23 18:29:16 +00006764 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006765 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00006766 return (NULL);
6767 }
6768 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6769 ret->buffer = buffer;
6770 ret->size = size;
Daniel Veillard1703c5f2003-02-10 14:28:44 +00006771 ret->error = xmlGenericError;
6772 ret->userData = xmlGenericErrorContext;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006773 return (ret);
6774}
6775
6776/**
Daniel Veillard33300b42003-04-17 09:09:19 +00006777 * xmlRelaxNGNewDocParserCtxt:
6778 * @doc: a preparsed document tree
6779 *
6780 * Create an XML RelaxNGs parser context for that document.
6781 * Note: since the process of compiling a RelaxNG schemas modifies the
6782 * document, the @doc parameter is duplicated internally.
6783 *
6784 * Returns the parser context or NULL in case of error
6785 */
6786xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006787xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc)
6788{
Daniel Veillard33300b42003-04-17 09:09:19 +00006789 xmlRelaxNGParserCtxtPtr ret;
6790 xmlDocPtr copy;
6791
6792 if (doc == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006793 return (NULL);
Daniel Veillard33300b42003-04-17 09:09:19 +00006794 copy = xmlCopyDoc(doc, 1);
6795 if (copy == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006796 return (NULL);
Daniel Veillard33300b42003-04-17 09:09:19 +00006797
Daniel Veillard4c004142003-10-07 11:33:24 +00006798 ret =
6799 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard33300b42003-04-17 09:09:19 +00006800 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006801 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard33300b42003-04-17 09:09:19 +00006802 return (NULL);
6803 }
6804 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6805 ret->document = copy;
Daniel Veillard42595322004-11-08 10:52:06 +00006806 ret->freedoc = 1;
Daniel Veillard33300b42003-04-17 09:09:19 +00006807 ret->userData = xmlGenericErrorContext;
6808 return (ret);
6809}
6810
6811/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00006812 * xmlRelaxNGFreeParserCtxt:
6813 * @ctxt: the schema parser context
6814 *
6815 * Free the resources associated to the schema parser context
6816 */
6817void
Daniel Veillard4c004142003-10-07 11:33:24 +00006818xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt)
6819{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006820 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006821 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006822 if (ctxt->URL != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006823 xmlFree(ctxt->URL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006824 if (ctxt->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006825 xmlRelaxNGFreeDocument(ctxt->doc);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00006826 if (ctxt->interleaves != NULL)
6827 xmlHashFree(ctxt->interleaves, NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006828 if (ctxt->documents != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006829 xmlRelaxNGFreeDocumentList(ctxt->documents);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006830 if (ctxt->includes != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006831 xmlRelaxNGFreeIncludeList(ctxt->includes);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006832 if (ctxt->docTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006833 xmlFree(ctxt->docTab);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00006834 if (ctxt->incTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006835 xmlFree(ctxt->incTab);
Daniel Veillard419a7682003-02-03 23:22:49 +00006836 if (ctxt->defTab != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006837 int i;
Daniel Veillard419a7682003-02-03 23:22:49 +00006838
Daniel Veillard4c004142003-10-07 11:33:24 +00006839 for (i = 0; i < ctxt->defNr; i++)
6840 xmlRelaxNGFreeDefine(ctxt->defTab[i]);
6841 xmlFree(ctxt->defTab);
Daniel Veillard419a7682003-02-03 23:22:49 +00006842 }
Daniel Veillard42595322004-11-08 10:52:06 +00006843 if ((ctxt->document != NULL) && (ctxt->freedoc))
6844 xmlFreeDoc(ctxt->document);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006845 xmlFree(ctxt);
6846}
6847
Daniel Veillard6eadf632003-01-23 18:29:16 +00006848/**
Daniel Veillardd2298792003-02-14 16:54:11 +00006849 * xmlRelaxNGNormExtSpace:
6850 * @value: a value
6851 *
6852 * Removes the leading and ending spaces of the value
6853 * The string is modified "in situ"
6854 */
6855static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006856xmlRelaxNGNormExtSpace(xmlChar * value)
6857{
Daniel Veillardd2298792003-02-14 16:54:11 +00006858 xmlChar *start = value;
6859 xmlChar *cur = value;
Daniel Veillardd2298792003-02-14 16:54:11 +00006860
Daniel Veillard4c004142003-10-07 11:33:24 +00006861 if (value == NULL)
6862 return;
6863
William M. Brack76e95df2003-10-18 16:20:14 +00006864 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006865 cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +00006866 if (cur == start) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006867 do {
William M. Brack76e95df2003-10-18 16:20:14 +00006868 while ((*cur != 0) && (!IS_BLANK_CH(*cur)))
Daniel Veillard4c004142003-10-07 11:33:24 +00006869 cur++;
6870 if (*cur == 0)
6871 return;
6872 start = cur;
William M. Brack76e95df2003-10-18 16:20:14 +00006873 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006874 cur++;
6875 if (*cur == 0) {
6876 *start = 0;
6877 return;
6878 }
6879 } while (1);
Daniel Veillardd2298792003-02-14 16:54:11 +00006880 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00006881 do {
William M. Brack76e95df2003-10-18 16:20:14 +00006882 while ((*cur != 0) && (!IS_BLANK_CH(*cur)))
Daniel Veillard4c004142003-10-07 11:33:24 +00006883 *start++ = *cur++;
6884 if (*cur == 0) {
6885 *start = 0;
6886 return;
6887 }
6888 /* don't try to normalize the inner spaces */
William M. Brack76e95df2003-10-18 16:20:14 +00006889 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006890 cur++;
Daniel Veillard4c004142003-10-07 11:33:24 +00006891 if (*cur == 0) {
6892 *start = 0;
6893 return;
6894 }
Daniel Veillard4aede2e2003-10-17 12:43:59 +00006895 *start++ = *cur++;
Daniel Veillard4c004142003-10-07 11:33:24 +00006896 } while (1);
Daniel Veillardd2298792003-02-14 16:54:11 +00006897 }
6898}
6899
6900/**
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00006901 * xmlRelaxNGCleanupAttributes:
Daniel Veillardd2298792003-02-14 16:54:11 +00006902 * @ctxt: a Relax-NG parser context
6903 * @node: a Relax-NG node
6904 *
6905 * Check all the attributes on the given node
6906 */
6907static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006908xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
6909{
Daniel Veillardd2298792003-02-14 16:54:11 +00006910 xmlAttrPtr cur, next;
6911
6912 cur = node->properties;
6913 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006914 next = cur->next;
6915 if ((cur->ns == NULL) ||
6916 (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
6917 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
6918 if ((!xmlStrEqual(node->name, BAD_CAST "element")) &&
6919 (!xmlStrEqual(node->name, BAD_CAST "attribute")) &&
6920 (!xmlStrEqual(node->name, BAD_CAST "ref")) &&
6921 (!xmlStrEqual(node->name, BAD_CAST "parentRef")) &&
6922 (!xmlStrEqual(node->name, BAD_CAST "param")) &&
6923 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6924 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6925 "Attribute %s is not allowed on %s\n",
6926 cur->name, node->name);
6927 }
6928 } else if (xmlStrEqual(cur->name, BAD_CAST "type")) {
6929 if ((!xmlStrEqual(node->name, BAD_CAST "value")) &&
6930 (!xmlStrEqual(node->name, BAD_CAST "data"))) {
6931 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6932 "Attribute %s is not allowed on %s\n",
6933 cur->name, node->name);
6934 }
6935 } else if (xmlStrEqual(cur->name, BAD_CAST "href")) {
6936 if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) &&
6937 (!xmlStrEqual(node->name, BAD_CAST "include"))) {
6938 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6939 "Attribute %s is not allowed on %s\n",
6940 cur->name, node->name);
6941 }
6942 } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) {
6943 if ((!xmlStrEqual(node->name, BAD_CAST "start")) &&
6944 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6945 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6946 "Attribute %s is not allowed on %s\n",
6947 cur->name, node->name);
6948 }
6949 } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) {
6950 xmlChar *val;
6951 xmlURIPtr uri;
Daniel Veillardd2298792003-02-14 16:54:11 +00006952
Daniel Veillard4c004142003-10-07 11:33:24 +00006953 val = xmlNodeListGetString(node->doc, cur->children, 1);
6954 if (val != NULL) {
6955 if (val[0] != 0) {
6956 uri = xmlParseURI((const char *) val);
6957 if (uri == NULL) {
6958 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_URI,
6959 "Attribute %s contains invalid URI %s\n",
6960 cur->name, val);
6961 } else {
6962 if (uri->scheme == NULL) {
6963 xmlRngPErr(ctxt, node, XML_RNGP_URI_NOT_ABSOLUTE,
6964 "Attribute %s URI %s is not absolute\n",
6965 cur->name, val);
6966 }
6967 if (uri->fragment != NULL) {
6968 xmlRngPErr(ctxt, node, XML_RNGP_URI_FRAGMENT,
6969 "Attribute %s URI %s has a fragment ID\n",
6970 cur->name, val);
6971 }
6972 xmlFreeURI(uri);
6973 }
6974 }
6975 xmlFree(val);
6976 }
6977 } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) {
6978 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_ATTRIBUTE,
6979 "Unknown attribute %s on %s\n", cur->name,
6980 node->name);
6981 }
6982 }
6983 cur = next;
Daniel Veillardd2298792003-02-14 16:54:11 +00006984 }
6985}
6986
6987/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00006988 * xmlRelaxNGCleanupTree:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006989 * @ctxt: a Relax-NG parser context
Daniel Veillardc5312d72003-02-21 17:14:10 +00006990 * @root: an xmlNodePtr subtree
Daniel Veillard6eadf632003-01-23 18:29:16 +00006991 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00006992 * Cleanup the subtree from unwanted nodes for parsing, resolve
6993 * Include and externalRef lookups.
Daniel Veillard6eadf632003-01-23 18:29:16 +00006994 */
Daniel Veillardc5312d72003-02-21 17:14:10 +00006995static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006996xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root)
6997{
Daniel Veillardc5312d72003-02-21 17:14:10 +00006998 xmlNodePtr cur, delete;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006999
Daniel Veillard6eadf632003-01-23 18:29:16 +00007000 delete = NULL;
7001 cur = root;
7002 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007003 if (delete != NULL) {
7004 xmlUnlinkNode(delete);
7005 xmlFreeNode(delete);
7006 delete = NULL;
7007 }
7008 if (cur->type == XML_ELEMENT_NODE) {
7009 /*
7010 * Simplification 4.1. Annotations
7011 */
7012 if ((cur->ns == NULL) ||
7013 (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
7014 if ((cur->parent != NULL) &&
7015 (cur->parent->type == XML_ELEMENT_NODE) &&
7016 ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) ||
7017 (xmlStrEqual(cur->parent->name, BAD_CAST "value")) ||
7018 (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) {
7019 xmlRngPErr(ctxt, cur, XML_RNGP_FOREIGN_ELEMENT,
7020 "element %s doesn't allow foreign elements\n",
7021 cur->parent->name, NULL);
7022 }
7023 delete = cur;
7024 goto skip_children;
7025 } else {
7026 xmlRelaxNGCleanupAttributes(ctxt, cur);
7027 if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) {
7028 xmlChar *href, *ns, *base, *URL;
7029 xmlRelaxNGDocumentPtr docu;
7030 xmlNodePtr tmp;
Daniel Veillard6dc91962004-03-22 19:10:02 +00007031 xmlURIPtr uri;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007032
Daniel Veillard4c004142003-10-07 11:33:24 +00007033 ns = xmlGetProp(cur, BAD_CAST "ns");
7034 if (ns == NULL) {
7035 tmp = cur->parent;
7036 while ((tmp != NULL) &&
7037 (tmp->type == XML_ELEMENT_NODE)) {
7038 ns = xmlGetProp(tmp, BAD_CAST "ns");
7039 if (ns != NULL)
7040 break;
7041 tmp = tmp->parent;
7042 }
7043 }
7044 href = xmlGetProp(cur, BAD_CAST "href");
7045 if (href == NULL) {
7046 xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
7047 "xmlRelaxNGParse: externalRef has no href attribute\n",
7048 NULL, NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007049 if (ns != NULL)
7050 xmlFree(ns);
Daniel Veillard4c004142003-10-07 11:33:24 +00007051 delete = cur;
7052 goto skip_children;
7053 }
Daniel Veillard6dc91962004-03-22 19:10:02 +00007054 uri = xmlParseURI((const char *) href);
7055 if (uri == NULL) {
7056 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7057 "Incorrect URI for externalRef %s\n",
7058 href, NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007059 if (ns != NULL)
7060 xmlFree(ns);
Daniel Veillard6dc91962004-03-22 19:10:02 +00007061 if (href != NULL)
7062 xmlFree(href);
7063 delete = cur;
7064 goto skip_children;
7065 }
7066 if (uri->fragment != NULL) {
7067 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7068 "Fragment forbidden in URI for externalRef %s\n",
7069 href, NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007070 if (ns != NULL)
7071 xmlFree(ns);
Daniel Veillard6dc91962004-03-22 19:10:02 +00007072 xmlFreeURI(uri);
7073 if (href != NULL)
7074 xmlFree(href);
7075 delete = cur;
7076 goto skip_children;
7077 }
7078 xmlFreeURI(uri);
Daniel Veillard4c004142003-10-07 11:33:24 +00007079 base = xmlNodeGetBase(cur->doc, cur);
7080 URL = xmlBuildURI(href, base);
7081 if (URL == NULL) {
7082 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7083 "Failed to compute URL for externalRef %s\n",
7084 href, NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007085 if (ns != NULL)
7086 xmlFree(ns);
Daniel Veillard4c004142003-10-07 11:33:24 +00007087 if (href != NULL)
7088 xmlFree(href);
7089 if (base != NULL)
7090 xmlFree(base);
7091 delete = cur;
7092 goto skip_children;
7093 }
7094 if (href != NULL)
7095 xmlFree(href);
7096 if (base != NULL)
7097 xmlFree(base);
7098 docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns);
7099 if (docu == NULL) {
7100 xmlRngPErr(ctxt, cur, XML_RNGP_EXTERNAL_REF_FAILURE,
7101 "Failed to load externalRef %s\n", URL,
7102 NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007103 if (ns != NULL)
7104 xmlFree(ns);
Daniel Veillard4c004142003-10-07 11:33:24 +00007105 xmlFree(URL);
7106 delete = cur;
7107 goto skip_children;
7108 }
7109 if (ns != NULL)
7110 xmlFree(ns);
7111 xmlFree(URL);
Daniel Veillard807daf82004-02-22 22:13:27 +00007112 cur->psvi = docu;
Daniel Veillard4c004142003-10-07 11:33:24 +00007113 } else if (xmlStrEqual(cur->name, BAD_CAST "include")) {
7114 xmlChar *href, *ns, *base, *URL;
7115 xmlRelaxNGIncludePtr incl;
7116 xmlNodePtr tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007117
Daniel Veillard4c004142003-10-07 11:33:24 +00007118 href = xmlGetProp(cur, BAD_CAST "href");
7119 if (href == NULL) {
7120 xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
7121 "xmlRelaxNGParse: include has no href attribute\n",
7122 NULL, NULL);
7123 delete = cur;
7124 goto skip_children;
7125 }
7126 base = xmlNodeGetBase(cur->doc, cur);
7127 URL = xmlBuildURI(href, base);
7128 if (URL == NULL) {
7129 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7130 "Failed to compute URL for include %s\n",
7131 href, NULL);
7132 if (href != NULL)
7133 xmlFree(href);
7134 if (base != NULL)
7135 xmlFree(base);
7136 delete = cur;
7137 goto skip_children;
7138 }
7139 if (href != NULL)
7140 xmlFree(href);
7141 if (base != NULL)
7142 xmlFree(base);
7143 ns = xmlGetProp(cur, BAD_CAST "ns");
7144 if (ns == NULL) {
7145 tmp = cur->parent;
7146 while ((tmp != NULL) &&
7147 (tmp->type == XML_ELEMENT_NODE)) {
7148 ns = xmlGetProp(tmp, BAD_CAST "ns");
7149 if (ns != NULL)
7150 break;
7151 tmp = tmp->parent;
7152 }
7153 }
7154 incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns);
7155 if (ns != NULL)
7156 xmlFree(ns);
7157 if (incl == NULL) {
7158 xmlRngPErr(ctxt, cur, XML_RNGP_INCLUDE_FAILURE,
7159 "Failed to load include %s\n", URL,
7160 NULL);
7161 xmlFree(URL);
7162 delete = cur;
7163 goto skip_children;
7164 }
7165 xmlFree(URL);
Daniel Veillard807daf82004-02-22 22:13:27 +00007166 cur->psvi = incl;
Daniel Veillard4c004142003-10-07 11:33:24 +00007167 } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) ||
7168 (xmlStrEqual(cur->name, BAD_CAST "attribute")))
7169 {
7170 xmlChar *name, *ns;
7171 xmlNodePtr text = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007172
Daniel Veillard4c004142003-10-07 11:33:24 +00007173 /*
7174 * Simplification 4.8. name attribute of element
7175 * and attribute elements
7176 */
7177 name = xmlGetProp(cur, BAD_CAST "name");
7178 if (name != NULL) {
7179 if (cur->children == NULL) {
7180 text =
7181 xmlNewChild(cur, cur->ns, BAD_CAST "name",
7182 name);
7183 } else {
7184 xmlNodePtr node;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007185
Daniel Veillard03a53c32004-10-26 16:06:51 +00007186 node = xmlNewDocNode(cur->doc, cur->ns,
7187 BAD_CAST "name", NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00007188 if (node != NULL) {
7189 xmlAddPrevSibling(cur->children, node);
7190 text = xmlNewText(name);
7191 xmlAddChild(node, text);
7192 text = node;
7193 }
7194 }
7195 if (text == NULL) {
7196 xmlRngPErr(ctxt, cur, XML_RNGP_CREATE_FAILURE,
7197 "Failed to create a name %s element\n",
7198 name, NULL);
7199 }
7200 xmlUnsetProp(cur, BAD_CAST "name");
7201 xmlFree(name);
7202 ns = xmlGetProp(cur, BAD_CAST "ns");
7203 if (ns != NULL) {
7204 if (text != NULL) {
7205 xmlSetProp(text, BAD_CAST "ns", ns);
7206 /* xmlUnsetProp(cur, BAD_CAST "ns"); */
7207 }
7208 xmlFree(ns);
7209 } else if (xmlStrEqual(cur->name,
7210 BAD_CAST "attribute")) {
7211 xmlSetProp(text, BAD_CAST "ns", BAD_CAST "");
7212 }
7213 }
7214 } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) ||
7215 (xmlStrEqual(cur->name, BAD_CAST "nsName")) ||
7216 (xmlStrEqual(cur->name, BAD_CAST "value"))) {
7217 /*
7218 * Simplification 4.8. name attribute of element
7219 * and attribute elements
7220 */
7221 if (xmlHasProp(cur, BAD_CAST "ns") == NULL) {
7222 xmlNodePtr node;
7223 xmlChar *ns = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007224
Daniel Veillard4c004142003-10-07 11:33:24 +00007225 node = cur->parent;
7226 while ((node != NULL) &&
7227 (node->type == XML_ELEMENT_NODE)) {
7228 ns = xmlGetProp(node, BAD_CAST "ns");
7229 if (ns != NULL) {
7230 break;
7231 }
7232 node = node->parent;
7233 }
7234 if (ns == NULL) {
7235 xmlSetProp(cur, BAD_CAST "ns", BAD_CAST "");
7236 } else {
7237 xmlSetProp(cur, BAD_CAST "ns", ns);
7238 xmlFree(ns);
7239 }
7240 }
7241 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
7242 xmlChar *name, *local, *prefix;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007243
Daniel Veillard4c004142003-10-07 11:33:24 +00007244 /*
7245 * Simplification: 4.10. QNames
7246 */
7247 name = xmlNodeGetContent(cur);
7248 if (name != NULL) {
7249 local = xmlSplitQName2(name, &prefix);
7250 if (local != NULL) {
7251 xmlNsPtr ns;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007252
Daniel Veillard4c004142003-10-07 11:33:24 +00007253 ns = xmlSearchNs(cur->doc, cur, prefix);
7254 if (ns == NULL) {
7255 xmlRngPErr(ctxt, cur,
7256 XML_RNGP_PREFIX_UNDEFINED,
7257 "xmlRelaxNGParse: no namespace for prefix %s\n",
7258 prefix, NULL);
7259 } else {
7260 xmlSetProp(cur, BAD_CAST "ns",
7261 ns->href);
7262 xmlNodeSetContent(cur, local);
7263 }
7264 xmlFree(local);
7265 xmlFree(prefix);
7266 }
7267 xmlFree(name);
7268 }
7269 }
7270 /*
7271 * 4.16
7272 */
7273 if (xmlStrEqual(cur->name, BAD_CAST "nsName")) {
7274 if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7275 xmlRngPErr(ctxt, cur,
7276 XML_RNGP_PAT_NSNAME_EXCEPT_NSNAME,
7277 "Found nsName/except//nsName forbidden construct\n",
7278 NULL, NULL);
7279 }
7280 }
7281 } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) &&
7282 (cur != root)) {
7283 int oldflags = ctxt->flags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007284
Daniel Veillard4c004142003-10-07 11:33:24 +00007285 /*
7286 * 4.16
7287 */
7288 if ((cur->parent != NULL) &&
7289 (xmlStrEqual
7290 (cur->parent->name, BAD_CAST "anyName"))) {
7291 ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT;
7292 xmlRelaxNGCleanupTree(ctxt, cur);
7293 ctxt->flags = oldflags;
7294 goto skip_children;
7295 } else if ((cur->parent != NULL) &&
7296 (xmlStrEqual
7297 (cur->parent->name, BAD_CAST "nsName"))) {
7298 ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT;
7299 xmlRelaxNGCleanupTree(ctxt, cur);
7300 ctxt->flags = oldflags;
7301 goto skip_children;
7302 }
7303 } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) {
7304 /*
7305 * 4.16
7306 */
7307 if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) {
7308 xmlRngPErr(ctxt, cur,
7309 XML_RNGP_PAT_ANYNAME_EXCEPT_ANYNAME,
7310 "Found anyName/except//anyName forbidden construct\n",
7311 NULL, NULL);
7312 } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7313 xmlRngPErr(ctxt, cur,
7314 XML_RNGP_PAT_NSNAME_EXCEPT_ANYNAME,
7315 "Found nsName/except//anyName forbidden construct\n",
7316 NULL, NULL);
7317 }
7318 }
7319 /*
7320 * Thisd is not an else since "include" is transformed
7321 * into a div
7322 */
7323 if (xmlStrEqual(cur->name, BAD_CAST "div")) {
7324 xmlChar *ns;
7325 xmlNodePtr child, ins, tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007326
Daniel Veillard4c004142003-10-07 11:33:24 +00007327 /*
7328 * implements rule 4.11
7329 */
Daniel Veillard6eadf632003-01-23 18:29:16 +00007330
Daniel Veillard4c004142003-10-07 11:33:24 +00007331 ns = xmlGetProp(cur, BAD_CAST "ns");
7332
7333 child = cur->children;
7334 ins = cur;
7335 while (child != NULL) {
7336 if (ns != NULL) {
7337 if (!xmlHasProp(child, BAD_CAST "ns")) {
7338 xmlSetProp(child, BAD_CAST "ns", ns);
7339 }
7340 }
7341 tmp = child->next;
7342 xmlUnlinkNode(child);
7343 ins = xmlAddNextSibling(ins, child);
7344 child = tmp;
7345 }
7346 if (ns != NULL)
7347 xmlFree(ns);
William M. Brack8eabb052004-06-07 14:15:54 +00007348 /*
7349 * Since we are about to delete cur, if it's nsDef is non-NULL we
7350 * need to preserve it (it contains the ns definitions for the
7351 * children we just moved). We'll just stick it on to the end
7352 * of cur->parent's list, since it's never going to be re-serialized
7353 * (bug 143738).
7354 */
7355 if (cur->nsDef != NULL) {
7356 xmlNsPtr parDef = (xmlNsPtr)&cur->parent->nsDef;
7357 while (parDef->next != NULL)
7358 parDef = parDef->next;
7359 parDef->next = cur->nsDef;
7360 cur->nsDef = NULL;
7361 }
Daniel Veillard4c004142003-10-07 11:33:24 +00007362 delete = cur;
7363 goto skip_children;
7364 }
7365 }
7366 }
7367 /*
7368 * Simplification 4.2 whitespaces
7369 */
7370 else if ((cur->type == XML_TEXT_NODE) ||
7371 (cur->type == XML_CDATA_SECTION_NODE)) {
7372 if (IS_BLANK_NODE(cur)) {
7373 if (cur->parent->type == XML_ELEMENT_NODE) {
7374 if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value"))
7375 &&
7376 (!xmlStrEqual
7377 (cur->parent->name, BAD_CAST "param")))
7378 delete = cur;
7379 } else {
7380 delete = cur;
7381 goto skip_children;
7382 }
7383 }
7384 } else {
7385 delete = cur;
7386 goto skip_children;
7387 }
7388
7389 /*
7390 * Skip to next node
7391 */
7392 if (cur->children != NULL) {
7393 if ((cur->children->type != XML_ENTITY_DECL) &&
7394 (cur->children->type != XML_ENTITY_REF_NODE) &&
7395 (cur->children->type != XML_ENTITY_NODE)) {
7396 cur = cur->children;
7397 continue;
7398 }
7399 }
7400 skip_children:
7401 if (cur->next != NULL) {
7402 cur = cur->next;
7403 continue;
7404 }
7405
7406 do {
7407 cur = cur->parent;
7408 if (cur == NULL)
7409 break;
7410 if (cur == root) {
7411 cur = NULL;
7412 break;
7413 }
7414 if (cur->next != NULL) {
7415 cur = cur->next;
7416 break;
7417 }
7418 } while (cur != NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007419 }
7420 if (delete != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007421 xmlUnlinkNode(delete);
7422 xmlFreeNode(delete);
7423 delete = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007424 }
Daniel Veillardc5312d72003-02-21 17:14:10 +00007425}
Daniel Veillard6eadf632003-01-23 18:29:16 +00007426
Daniel Veillardc5312d72003-02-21 17:14:10 +00007427/**
7428 * xmlRelaxNGCleanupDoc:
7429 * @ctxt: a Relax-NG parser context
7430 * @doc: an xmldocPtr document pointer
7431 *
7432 * Cleanup the document from unwanted nodes for parsing, resolve
7433 * Include and externalRef lookups.
7434 *
7435 * Returns the cleaned up document or NULL in case of error
7436 */
7437static xmlDocPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00007438xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc)
7439{
Daniel Veillardc5312d72003-02-21 17:14:10 +00007440 xmlNodePtr root;
7441
7442 /*
7443 * Extract the root
7444 */
7445 root = xmlDocGetRootElement(doc);
7446 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007447 xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
7448 ctxt->URL, NULL);
Daniel Veillardc5312d72003-02-21 17:14:10 +00007449 return (NULL);
7450 }
7451 xmlRelaxNGCleanupTree(ctxt, root);
Daniel Veillard4c004142003-10-07 11:33:24 +00007452 return (doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007453}
7454
7455/**
7456 * xmlRelaxNGParse:
7457 * @ctxt: a Relax-NG parser context
7458 *
7459 * parse a schema definition resource and build an internal
7460 * XML Shema struture which can be used to validate instances.
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007461 *
7462 * Returns the internal XML RelaxNG structure built from the resource or
7463 * NULL in case of error
7464 */
7465xmlRelaxNGPtr
7466xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt)
7467{
7468 xmlRelaxNGPtr ret = NULL;
7469 xmlDocPtr doc;
7470 xmlNodePtr root;
7471
7472 xmlRelaxNGInitTypes();
7473
7474 if (ctxt == NULL)
7475 return (NULL);
7476
7477 /*
7478 * First step is to parse the input document into an DOM/Infoset
7479 */
7480 if (ctxt->URL != NULL) {
Daniel Veillard87247e82004-01-13 20:42:02 +00007481 doc = xmlReadFile((const char *) ctxt->URL,NULL,0);
Daniel Veillard4c004142003-10-07 11:33:24 +00007482 if (doc == NULL) {
7483 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7484 "xmlRelaxNGParse: could not load %s\n", ctxt->URL,
7485 NULL);
7486 return (NULL);
7487 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007488 } else if (ctxt->buffer != NULL) {
Daniel Veillard87247e82004-01-13 20:42:02 +00007489 doc = xmlReadMemory(ctxt->buffer, ctxt->size,NULL,NULL,0);
Daniel Veillard4c004142003-10-07 11:33:24 +00007490 if (doc == NULL) {
7491 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7492 "xmlRelaxNGParse: could not parse schemas\n", NULL,
7493 NULL);
7494 return (NULL);
7495 }
7496 doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
7497 ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
Daniel Veillard33300b42003-04-17 09:09:19 +00007498 } else if (ctxt->document != NULL) {
7499 doc = ctxt->document;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007500 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007501 xmlRngPErr(ctxt, NULL, XML_RNGP_EMPTY,
7502 "xmlRelaxNGParse: nothing to parse\n", NULL, NULL);
7503 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007504 }
7505 ctxt->document = doc;
7506
7507 /*
7508 * Some preprocessing of the document content
7509 */
7510 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
7511 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007512 xmlFreeDoc(ctxt->document);
7513 ctxt->document = NULL;
7514 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007515 }
7516
Daniel Veillard6eadf632003-01-23 18:29:16 +00007517 /*
7518 * Then do the parsing for good
7519 */
7520 root = xmlDocGetRootElement(doc);
7521 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007522 xmlRngPErr(ctxt, (xmlNodePtr) doc,
7523 XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
William M. Brack700f9872006-05-06 03:16:22 +00007524 (ctxt->URL ? ctxt->URL : BAD_CAST "schemas"), NULL);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08007525
Daniel Veillard3f845a92006-04-13 07:33:44 +00007526 xmlFreeDoc(ctxt->document);
7527 ctxt->document = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007528 return (NULL);
7529 }
7530 ret = xmlRelaxNGParseDocument(ctxt, root);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007531 if (ret == NULL) {
Daniel Veillard3f845a92006-04-13 07:33:44 +00007532 xmlFreeDoc(ctxt->document);
7533 ctxt->document = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00007534 return (NULL);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007535 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00007536
7537 /*
Daniel Veillardfd573f12003-03-16 17:52:32 +00007538 * Check the ref/defines links
7539 */
7540 /*
7541 * try to preprocess interleaves
7542 */
7543 if (ctxt->interleaves != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007544 xmlHashScan(ctxt->interleaves,
7545 (xmlHashScanner) xmlRelaxNGComputeInterleaves, ctxt);
Daniel Veillardfd573f12003-03-16 17:52:32 +00007546 }
7547
7548 /*
Daniel Veillard6eadf632003-01-23 18:29:16 +00007549 * if there was a parsing error return NULL
7550 */
7551 if (ctxt->nbErrors > 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007552 xmlRelaxNGFree(ret);
7553 ctxt->document = NULL;
7554 xmlFreeDoc(doc);
7555 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007556 }
7557
7558 /*
Daniel Veillard52b48c72003-04-13 19:53:42 +00007559 * try to compile (parts of) the schemas
7560 */
Daniel Veillardce192eb2003-04-16 15:58:05 +00007561 if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) {
7562 if (ret->topgrammar->start->type != XML_RELAXNG_START) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007563 xmlRelaxNGDefinePtr def;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007564
Daniel Veillard4c004142003-10-07 11:33:24 +00007565 def = xmlRelaxNGNewDefine(ctxt, NULL);
7566 if (def != NULL) {
7567 def->type = XML_RELAXNG_START;
7568 def->content = ret->topgrammar->start;
7569 ret->topgrammar->start = def;
7570 }
7571 }
7572 xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007573 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00007574
7575 /*
Daniel Veillard6eadf632003-01-23 18:29:16 +00007576 * Transfer the pointer for cleanup at the schema level.
7577 */
7578 ret->doc = doc;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007579 ctxt->document = NULL;
7580 ret->documents = ctxt->documents;
7581 ctxt->documents = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00007582
Daniel Veillarde2a5a082003-02-02 14:35:17 +00007583 ret->includes = ctxt->includes;
7584 ctxt->includes = NULL;
Daniel Veillard419a7682003-02-03 23:22:49 +00007585 ret->defNr = ctxt->defNr;
7586 ret->defTab = ctxt->defTab;
7587 ctxt->defTab = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007588 if (ctxt->idref == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00007589 ret->idref = 1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007590
7591 return (ret);
7592}
Daniel Veillard4c004142003-10-07 11:33:24 +00007593
Daniel Veillard6eadf632003-01-23 18:29:16 +00007594/**
7595 * xmlRelaxNGSetParserErrors:
7596 * @ctxt: a Relax-NG validation context
7597 * @err: the error callback
7598 * @warn: the warning callback
7599 * @ctx: contextual data for the callbacks
7600 *
7601 * Set the callback functions used to handle errors for a validation context
7602 */
7603void
7604xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007605 xmlRelaxNGValidityErrorFunc err,
7606 xmlRelaxNGValidityWarningFunc warn, void *ctx)
7607{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007608 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007609 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007610 ctxt->error = err;
7611 ctxt->warning = warn;
Daniel Veillardb30ca312005-09-04 13:50:03 +00007612 ctxt->serror = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007613 ctxt->userData = ctx;
7614}
Daniel Veillard409a8142003-07-18 15:16:57 +00007615
7616/**
7617 * xmlRelaxNGGetParserErrors:
7618 * @ctxt: a Relax-NG validation context
7619 * @err: the error callback result
7620 * @warn: the warning callback result
7621 * @ctx: contextual data for the callbacks result
7622 *
7623 * Get the callback information used to handle errors for a validation context
7624 *
7625 * Returns -1 in case of failure, 0 otherwise.
7626 */
7627int
7628xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007629 xmlRelaxNGValidityErrorFunc * err,
7630 xmlRelaxNGValidityWarningFunc * warn, void **ctx)
7631{
Daniel Veillard409a8142003-07-18 15:16:57 +00007632 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007633 return (-1);
7634 if (err != NULL)
7635 *err = ctxt->error;
7636 if (warn != NULL)
7637 *warn = ctxt->warning;
7638 if (ctx != NULL)
7639 *ctx = ctxt->userData;
7640 return (0);
Daniel Veillard409a8142003-07-18 15:16:57 +00007641}
7642
Daniel Veillardb2f8f1d2006-04-28 16:30:48 +00007643/**
7644 * xmlRelaxNGSetParserStructuredErrors:
7645 * @ctxt: a Relax-NG parser context
7646 * @serror: the error callback
7647 * @ctx: contextual data for the callbacks
7648 *
7649 * Set the callback functions used to handle errors for a parsing context
7650 */
Kasimier T. Buchcika930fbe2006-01-09 16:28:20 +00007651void
Daniel Veillardb2f8f1d2006-04-28 16:30:48 +00007652xmlRelaxNGSetParserStructuredErrors(xmlRelaxNGParserCtxtPtr ctxt,
Kasimier T. Buchcika930fbe2006-01-09 16:28:20 +00007653 xmlStructuredErrorFunc serror,
7654 void *ctx)
7655{
7656 if (ctxt == NULL)
7657 return;
7658 ctxt->serror = serror;
7659 ctxt->error = NULL;
7660 ctxt->warning = NULL;
7661 ctxt->userData = ctx;
7662}
7663
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007664#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard4c004142003-10-07 11:33:24 +00007665
Daniel Veillard6eadf632003-01-23 18:29:16 +00007666/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08007667 * *
7668 * Dump back a compiled form *
7669 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00007670 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007671static void xmlRelaxNGDumpDefine(FILE * output,
7672 xmlRelaxNGDefinePtr define);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007673
7674/**
7675 * xmlRelaxNGDumpDefines:
7676 * @output: the file output
7677 * @defines: a list of define structures
7678 *
7679 * Dump a RelaxNG structure back
7680 */
7681static void
Daniel Veillard4c004142003-10-07 11:33:24 +00007682xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines)
7683{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007684 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007685 xmlRelaxNGDumpDefine(output, defines);
7686 defines = defines->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007687 }
7688}
7689
7690/**
7691 * xmlRelaxNGDumpDefine:
7692 * @output: the file output
7693 * @define: a define structure
7694 *
7695 * Dump a RelaxNG structure back
7696 */
7697static void
Daniel Veillard4c004142003-10-07 11:33:24 +00007698xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define)
7699{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007700 if (define == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007701 return;
7702 switch (define->type) {
Daniel Veillard6eadf632003-01-23 18:29:16 +00007703 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00007704 fprintf(output, "<empty/>\n");
7705 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007706 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillard4c004142003-10-07 11:33:24 +00007707 fprintf(output, "<notAllowed/>\n");
7708 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007709 case XML_RELAXNG_TEXT:
Daniel Veillard4c004142003-10-07 11:33:24 +00007710 fprintf(output, "<text/>\n");
7711 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007712 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00007713 fprintf(output, "<element>\n");
7714 if (define->name != NULL) {
7715 fprintf(output, "<name");
7716 if (define->ns != NULL)
7717 fprintf(output, " ns=\"%s\"", define->ns);
7718 fprintf(output, ">%s</name>\n", define->name);
7719 }
7720 xmlRelaxNGDumpDefines(output, define->attrs);
7721 xmlRelaxNGDumpDefines(output, define->content);
7722 fprintf(output, "</element>\n");
7723 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007724 case XML_RELAXNG_LIST:
Daniel Veillard4c004142003-10-07 11:33:24 +00007725 fprintf(output, "<list>\n");
7726 xmlRelaxNGDumpDefines(output, define->content);
7727 fprintf(output, "</list>\n");
7728 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007729 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007730 fprintf(output, "<oneOrMore>\n");
7731 xmlRelaxNGDumpDefines(output, define->content);
7732 fprintf(output, "</oneOrMore>\n");
7733 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007734 case XML_RELAXNG_ZEROORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007735 fprintf(output, "<zeroOrMore>\n");
7736 xmlRelaxNGDumpDefines(output, define->content);
7737 fprintf(output, "</zeroOrMore>\n");
7738 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007739 case XML_RELAXNG_CHOICE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007740 fprintf(output, "<choice>\n");
7741 xmlRelaxNGDumpDefines(output, define->content);
7742 fprintf(output, "</choice>\n");
7743 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007744 case XML_RELAXNG_GROUP:
Daniel Veillard4c004142003-10-07 11:33:24 +00007745 fprintf(output, "<group>\n");
7746 xmlRelaxNGDumpDefines(output, define->content);
7747 fprintf(output, "</group>\n");
7748 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007749 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007750 fprintf(output, "<interleave>\n");
7751 xmlRelaxNGDumpDefines(output, define->content);
7752 fprintf(output, "</interleave>\n");
7753 break;
7754 case XML_RELAXNG_OPTIONAL:
7755 fprintf(output, "<optional>\n");
7756 xmlRelaxNGDumpDefines(output, define->content);
7757 fprintf(output, "</optional>\n");
7758 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007759 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007760 fprintf(output, "<attribute>\n");
7761 xmlRelaxNGDumpDefines(output, define->content);
7762 fprintf(output, "</attribute>\n");
7763 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007764 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007765 fprintf(output, "<define");
7766 if (define->name != NULL)
7767 fprintf(output, " name=\"%s\"", define->name);
7768 fprintf(output, ">\n");
7769 xmlRelaxNGDumpDefines(output, define->content);
7770 fprintf(output, "</define>\n");
7771 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007772 case XML_RELAXNG_REF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007773 fprintf(output, "<ref");
7774 if (define->name != NULL)
7775 fprintf(output, " name=\"%s\"", define->name);
7776 fprintf(output, ">\n");
7777 xmlRelaxNGDumpDefines(output, define->content);
7778 fprintf(output, "</ref>\n");
7779 break;
Daniel Veillard419a7682003-02-03 23:22:49 +00007780 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007781 fprintf(output, "<parentRef");
7782 if (define->name != NULL)
7783 fprintf(output, " name=\"%s\"", define->name);
7784 fprintf(output, ">\n");
7785 xmlRelaxNGDumpDefines(output, define->content);
7786 fprintf(output, "</parentRef>\n");
7787 break;
7788 case XML_RELAXNG_EXTERNALREF:
7789 fprintf(output, "<externalRef>");
7790 xmlRelaxNGDumpDefines(output, define->content);
7791 fprintf(output, "</externalRef>\n");
7792 break;
Daniel Veillarde431a272003-01-29 23:02:33 +00007793 case XML_RELAXNG_DATATYPE:
Daniel Veillard6eadf632003-01-23 18:29:16 +00007794 case XML_RELAXNG_VALUE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007795 TODO break;
7796 case XML_RELAXNG_START:
7797 case XML_RELAXNG_EXCEPT:
7798 case XML_RELAXNG_PARAM:
7799 TODO break;
7800 case XML_RELAXNG_NOOP:
7801 xmlRelaxNGDumpDefines(output, define->content);
7802 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007803 }
7804}
Daniel Veillard4c004142003-10-07 11:33:24 +00007805
Daniel Veillard6eadf632003-01-23 18:29:16 +00007806/**
7807 * xmlRelaxNGDumpGrammar:
7808 * @output: the file output
7809 * @grammar: a grammar structure
Daniel Veillardf8e3db02012-09-11 13:26:36 +08007810 * @top: is this a top grammar
Daniel Veillard6eadf632003-01-23 18:29:16 +00007811 *
7812 * Dump a RelaxNG structure back
7813 */
7814static void
7815xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top)
7816{
7817 if (grammar == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007818 return;
7819
Daniel Veillard6eadf632003-01-23 18:29:16 +00007820 fprintf(output, "<grammar");
7821 if (top)
Daniel Veillard4c004142003-10-07 11:33:24 +00007822 fprintf(output, " xmlns=\"http://relaxng.org/ns/structure/1.0\"");
7823 switch (grammar->combine) {
7824 case XML_RELAXNG_COMBINE_UNDEFINED:
7825 break;
7826 case XML_RELAXNG_COMBINE_CHOICE:
7827 fprintf(output, " combine=\"choice\"");
7828 break;
7829 case XML_RELAXNG_COMBINE_INTERLEAVE:
7830 fprintf(output, " combine=\"interleave\"");
7831 break;
7832 default:
7833 fprintf(output, " <!-- invalid combine value -->");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007834 }
7835 fprintf(output, ">\n");
7836 if (grammar->start == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007837 fprintf(output, " <!-- grammar had no start -->");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007838 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007839 fprintf(output, "<start>\n");
7840 xmlRelaxNGDumpDefine(output, grammar->start);
7841 fprintf(output, "</start>\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007842 }
7843 /* TODO ? Dump the defines ? */
7844 fprintf(output, "</grammar>\n");
7845}
7846
7847/**
7848 * xmlRelaxNGDump:
7849 * @output: the file output
7850 * @schema: a schema structure
7851 *
7852 * Dump a RelaxNG structure back
7853 */
7854void
7855xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema)
7856{
Daniel Veillardce682bc2004-11-05 17:22:25 +00007857 if (output == NULL)
7858 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007859 if (schema == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007860 fprintf(output, "RelaxNG empty or failed to compile\n");
7861 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007862 }
7863 fprintf(output, "RelaxNG: ");
7864 if (schema->doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007865 fprintf(output, "no document\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007866 } else if (schema->doc->URL != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007867 fprintf(output, "%s\n", schema->doc->URL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007868 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007869 fprintf(output, "\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007870 }
7871 if (schema->topgrammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007872 fprintf(output, "RelaxNG has no top grammar\n");
7873 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007874 }
7875 xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1);
7876}
7877
Daniel Veillardfebcca42003-02-16 15:44:18 +00007878/**
7879 * xmlRelaxNGDumpTree:
7880 * @output: the file output
7881 * @schema: a schema structure
7882 *
7883 * Dump the transformed RelaxNG tree.
7884 */
7885void
7886xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema)
7887{
Daniel Veillardce682bc2004-11-05 17:22:25 +00007888 if (output == NULL)
7889 return;
Daniel Veillardfebcca42003-02-16 15:44:18 +00007890 if (schema == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007891 fprintf(output, "RelaxNG empty or failed to compile\n");
7892 return;
Daniel Veillardfebcca42003-02-16 15:44:18 +00007893 }
7894 if (schema->doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007895 fprintf(output, "no document\n");
Daniel Veillardfebcca42003-02-16 15:44:18 +00007896 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007897 xmlDocDump(output, schema->doc);
Daniel Veillardfebcca42003-02-16 15:44:18 +00007898 }
7899}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007900#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardfebcca42003-02-16 15:44:18 +00007901
Daniel Veillard6eadf632003-01-23 18:29:16 +00007902/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08007903 * *
7904 * Validation of compiled content *
7905 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00007906 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007907static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
7908 xmlRelaxNGDefinePtr define);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007909
7910/**
7911 * xmlRelaxNGValidateCompiledCallback:
7912 * @exec: the regular expression instance
7913 * @token: the token which matched
7914 * @transdata: callback data, the define for the subelement if available
7915 @ @inputdata: callback data, the Relax NG validation context
7916 *
7917 * Handle the callback and if needed validate the element children.
7918 */
Daniel Veillard4c004142003-10-07 11:33:24 +00007919static void
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007920xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00007921 const xmlChar * token,
7922 void *transdata, void *inputdata)
7923{
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007924 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
7925 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
7926 int ret;
7927
7928#ifdef DEBUG_COMPILE
7929 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00007930 "Compiled callback for: '%s'\n", token);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007931#endif
7932 if (ctxt == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007933 fprintf(stderr, "callback on %s missing context\n", token);
Daniel Veillard4c004142003-10-07 11:33:24 +00007934 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007935 }
7936 if (define == NULL) {
7937 if (token[0] == '#')
Daniel Veillard4c004142003-10-07 11:33:24 +00007938 return;
7939 fprintf(stderr, "callback on %s missing define\n", token);
7940 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7941 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7942 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007943 }
7944 if ((ctxt == NULL) || (define == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007945 fprintf(stderr, "callback on %s missing info\n", token);
7946 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7947 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7948 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007949 } else if (define->type != XML_RELAXNG_ELEMENT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007950 fprintf(stderr, "callback on %s define is not element\n", token);
7951 if (ctxt->errNo == XML_RELAXNG_OK)
7952 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7953 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007954 }
7955 ret = xmlRelaxNGValidateDefinition(ctxt, define);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007956 if (ret != 0)
7957 ctxt->perr = ret;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007958}
7959
7960/**
7961 * xmlRelaxNGValidateCompiledContent:
7962 * @ctxt: the RelaxNG validation context
7963 * @regexp: the regular expression as compiled
7964 * @content: list of children to test against the regexp
7965 *
7966 * Validate the content model of an element or start using the regexp
7967 *
7968 * Returns 0 in case of success, -1 in case of error.
7969 */
7970static int
7971xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007972 xmlRegexpPtr regexp, xmlNodePtr content)
7973{
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007974 xmlRegExecCtxtPtr exec;
7975 xmlNodePtr cur;
Daniel Veillard62163602003-04-17 09:36:38 +00007976 int ret = 0;
Daniel Veillard14b56432006-03-09 18:41:40 +00007977 int oldperr;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007978
7979 if ((ctxt == NULL) || (regexp == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00007980 return (-1);
Daniel Veillard14b56432006-03-09 18:41:40 +00007981 oldperr = ctxt->perr;
Daniel Veillard4c004142003-10-07 11:33:24 +00007982 exec = xmlRegNewExecCtxt(regexp,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007983 xmlRelaxNGValidateCompiledCallback, ctxt);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007984 ctxt->perr = 0;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007985 cur = content;
7986 while (cur != NULL) {
7987 ctxt->state->seq = cur;
Daniel Veillard4c004142003-10-07 11:33:24 +00007988 switch (cur->type) {
7989 case XML_TEXT_NODE:
7990 case XML_CDATA_SECTION_NODE:
7991 if (xmlIsBlankNode(cur))
7992 break;
7993 ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt);
7994 if (ret < 0) {
7995 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG,
7996 cur->parent->name);
7997 }
7998 break;
7999 case XML_ELEMENT_NODE:
8000 if (cur->ns != NULL) {
8001 ret = xmlRegExecPushString2(exec, cur->name,
8002 cur->ns->href, ctxt);
8003 } else {
8004 ret = xmlRegExecPushString(exec, cur->name, ctxt);
8005 }
8006 if (ret < 0) {
8007 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name);
8008 }
8009 break;
8010 default:
8011 break;
8012 }
8013 if (ret < 0)
8014 break;
8015 /*
8016 * Switch to next element
8017 */
8018 cur = cur->next;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008019 }
8020 ret = xmlRegExecPushString(exec, NULL, NULL);
8021 if (ret == 1) {
8022 ret = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00008023 ctxt->state->seq = NULL;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008024 } else if (ret == 0) {
8025 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00008026 * TODO: get some of the names needed to exit the current state of exec
8027 */
8028 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
8029 ret = -1;
8030 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8031 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008032 } else {
8033 ret = -1;
8034 }
8035 xmlRegFreeExecCtxt(exec);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00008036 /*
8037 * There might be content model errors outside of the pure
8038 * regexp validation, e.g. for attribute values.
8039 */
8040 if ((ret == 0) && (ctxt->perr != 0)) {
8041 ret = ctxt->perr;
8042 }
8043 ctxt->perr = oldperr;
Daniel Veillard4c004142003-10-07 11:33:24 +00008044 return (ret);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008045}
8046
8047/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08008048 * *
8049 * Progressive validation of when possible *
8050 * *
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008051 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00008052static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
8053 xmlRelaxNGDefinePtr defines);
8054static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt,
William M. Brack272693c2003-11-14 16:20:34 +00008055 int dolog);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00008056static void xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008057
8058/**
8059 * xmlRelaxNGElemPush:
8060 * @ctxt: the validation context
8061 * @exec: the regexp runtime for the new content model
8062 *
8063 * Push a new regexp for the current node content model on the stack
8064 *
8065 * Returns 0 in case of success and -1 in case of error.
8066 */
8067static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008068xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec)
8069{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008070 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008071 ctxt->elemMax = 10;
8072 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax *
8073 sizeof
8074 (xmlRegExecCtxtPtr));
Daniel Veillardf4e55762003-04-15 23:32:22 +00008075 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008076 xmlRngVErrMemory(ctxt, "validating\n");
8077 return (-1);
8078 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00008079 }
8080 if (ctxt->elemNr >= ctxt->elemMax) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008081 ctxt->elemMax *= 2;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008082 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00008083 ctxt->elemMax *
8084 sizeof
8085 (xmlRegExecCtxtPtr));
Daniel Veillardf4e55762003-04-15 23:32:22 +00008086 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008087 xmlRngVErrMemory(ctxt, "validating\n");
8088 return (-1);
8089 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00008090 }
8091 ctxt->elemTab[ctxt->elemNr++] = exec;
8092 ctxt->elem = exec;
Daniel Veillard4c004142003-10-07 11:33:24 +00008093 return (0);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008094}
8095
8096/**
8097 * xmlRelaxNGElemPop:
8098 * @ctxt: the validation context
8099 *
8100 * Pop the regexp of the current node content model from the stack
8101 *
8102 * Returns the exec or NULL if empty
8103 */
8104static xmlRegExecCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00008105xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt)
8106{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008107 xmlRegExecCtxtPtr ret;
8108
Daniel Veillard4c004142003-10-07 11:33:24 +00008109 if (ctxt->elemNr <= 0)
8110 return (NULL);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008111 ctxt->elemNr--;
8112 ret = ctxt->elemTab[ctxt->elemNr];
8113 ctxt->elemTab[ctxt->elemNr] = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00008114 if (ctxt->elemNr > 0)
Daniel Veillardf4e55762003-04-15 23:32:22 +00008115 ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1];
8116 else
Daniel Veillard4c004142003-10-07 11:33:24 +00008117 ctxt->elem = NULL;
8118 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008119}
8120
8121/**
8122 * xmlRelaxNGValidateProgressiveCallback:
8123 * @exec: the regular expression instance
8124 * @token: the token which matched
8125 * @transdata: callback data, the define for the subelement if available
8126 @ @inputdata: callback data, the Relax NG validation context
8127 *
8128 * Handle the callback and if needed validate the element children.
8129 * some of the in/out informations are passed via the context in @inputdata.
8130 */
Daniel Veillard4c004142003-10-07 11:33:24 +00008131static void
8132xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec
8133 ATTRIBUTE_UNUSED,
8134 const xmlChar * token,
8135 void *transdata, void *inputdata)
8136{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008137 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
8138 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008139 xmlRelaxNGValidStatePtr state, oldstate;
Daniel Veillard14b56432006-03-09 18:41:40 +00008140 xmlNodePtr node;
Daniel Veillardc3ca5ba2003-05-09 22:26:28 +00008141 int ret = 0, oldflags;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008142
8143#ifdef DEBUG_PROGRESSIVE
8144 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00008145 "Progressive callback for: '%s'\n", token);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008146#endif
8147 if (ctxt == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008148 fprintf(stderr, "callback on %s missing context\n", token);
8149 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008150 }
Daniel Veillard14b56432006-03-09 18:41:40 +00008151 node = ctxt->pnode;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008152 ctxt->pstate = 1;
8153 if (define == NULL) {
8154 if (token[0] == '#')
Daniel Veillard4c004142003-10-07 11:33:24 +00008155 return;
8156 fprintf(stderr, "callback on %s missing define\n", token);
8157 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
8158 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
8159 ctxt->pstate = -1;
8160 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008161 }
8162 if ((ctxt == NULL) || (define == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008163 fprintf(stderr, "callback on %s missing info\n", token);
8164 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
8165 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
8166 ctxt->pstate = -1;
8167 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008168 } else if (define->type != XML_RELAXNG_ELEMENT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008169 fprintf(stderr, "callback on %s define is not element\n", token);
8170 if (ctxt->errNo == XML_RELAXNG_OK)
8171 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
8172 ctxt->pstate = -1;
8173 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008174 }
8175 if (node->type != XML_ELEMENT_NODE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008176 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
8177 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8178 xmlRelaxNGDumpValidError(ctxt);
8179 ctxt->pstate = -1;
8180 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008181 }
8182 if (define->contModel == NULL) {
8183 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00008184 * this node cannot be validated in a streamable fashion
8185 */
Daniel Veillardf4e55762003-04-15 23:32:22 +00008186#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00008187 xmlGenericError(xmlGenericErrorContext,
8188 "Element '%s' validation is not streamable\n",
8189 token);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008190#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008191 ctxt->pstate = 0;
8192 ctxt->pdef = define;
8193 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008194 }
8195 exec = xmlRegNewExecCtxt(define->contModel,
Daniel Veillard4c004142003-10-07 11:33:24 +00008196 xmlRelaxNGValidateProgressiveCallback, ctxt);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008197 if (exec == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008198 ctxt->pstate = -1;
8199 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008200 }
8201 xmlRelaxNGElemPush(ctxt, exec);
8202
8203 /*
8204 * Validate the attributes part of the content.
8205 */
8206 state = xmlRelaxNGNewValidState(ctxt, node);
8207 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008208 ctxt->pstate = -1;
8209 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008210 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008211 oldstate = ctxt->state;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008212 ctxt->state = state;
8213 if (define->attrs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008214 ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
8215 if (ret != 0) {
8216 ctxt->pstate = -1;
8217 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
8218 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00008219 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008220 if (ctxt->state != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008221 ctxt->state->seq = NULL;
8222 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
8223 if (ret != 0) {
8224 ctxt->pstate = -1;
8225 }
8226 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardce192eb2003-04-16 15:58:05 +00008227 } else if (ctxt->states != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008228 int tmp = -1, i;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008229
8230 oldflags = ctxt->flags;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008231
Daniel Veillard4c004142003-10-07 11:33:24 +00008232 for (i = 0; i < ctxt->states->nbState; i++) {
8233 state = ctxt->states->tabState[i];
8234 ctxt->state = state;
8235 ctxt->state->seq = NULL;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008236
Daniel Veillard4c004142003-10-07 11:33:24 +00008237 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
8238 tmp = 0;
8239 break;
8240 }
8241 }
8242 if (tmp != 0) {
8243 /*
8244 * validation error, log the message for the "best" one
8245 */
8246 ctxt->flags |= FLAGS_IGNORABLE;
8247 xmlRelaxNGLogBestError(ctxt);
8248 }
8249 for (i = 0; i < ctxt->states->nbState; i++) {
8250 xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[i]);
8251 }
8252 xmlRelaxNGFreeStates(ctxt, ctxt->states);
8253 ctxt->states = NULL;
8254 if ((ret == 0) && (tmp == -1))
8255 ctxt->pstate = -1;
8256 ctxt->flags = oldflags;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008257 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008258 if (ctxt->pstate == -1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008259 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
8260 xmlRelaxNGDumpValidError(ctxt);
8261 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008262 }
8263 ctxt->state = oldstate;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008264}
8265
8266/**
8267 * xmlRelaxNGValidatePushElement:
8268 * @ctxt: the validation context
8269 * @doc: a document instance
8270 * @elem: an element instance
8271 *
8272 * Push a new element start on the RelaxNG validation stack.
8273 *
8274 * returns 1 if no validation problem was found or 0 if validating the
8275 * element requires a full node, and -1 in case of error.
8276 */
8277int
Daniel Veillard33300b42003-04-17 09:09:19 +00008278xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt,
8279 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillardf4e55762003-04-15 23:32:22 +00008280 xmlNodePtr elem)
8281{
8282 int ret = 1;
8283
8284 if ((ctxt == NULL) || (elem == NULL))
8285 return (-1);
8286
8287#ifdef DEBUG_PROGRESSIVE
8288 xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name);
8289#endif
8290 if (ctxt->elem == 0) {
8291 xmlRelaxNGPtr schema;
8292 xmlRelaxNGGrammarPtr grammar;
8293 xmlRegExecCtxtPtr exec;
8294 xmlRelaxNGDefinePtr define;
8295
8296 schema = ctxt->schema;
8297 if (schema == NULL) {
8298 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8299 return (-1);
8300 }
8301 grammar = schema->topgrammar;
8302 if ((grammar == NULL) || (grammar->start == NULL)) {
8303 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8304 return (-1);
8305 }
8306 define = grammar->start;
8307 if (define->contModel == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008308 ctxt->pdef = define;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008309 return (0);
8310 }
8311 exec = xmlRegNewExecCtxt(define->contModel,
8312 xmlRelaxNGValidateProgressiveCallback,
8313 ctxt);
8314 if (exec == NULL) {
8315 return (-1);
8316 }
8317 xmlRelaxNGElemPush(ctxt, exec);
8318 }
8319 ctxt->pnode = elem;
8320 ctxt->pstate = 0;
8321 if (elem->ns != NULL) {
8322 ret =
8323 xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href,
8324 ctxt);
8325 } else {
8326 ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt);
8327 }
8328 if (ret < 0) {
8329 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name);
8330 } else {
8331 if (ctxt->pstate == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008332 ret = 0;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008333 else if (ctxt->pstate < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008334 ret = -1;
8335 else
8336 ret = 1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008337 }
8338#ifdef DEBUG_PROGRESSIVE
8339 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008340 xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n",
8341 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008342#endif
8343 return (ret);
8344}
8345
8346/**
8347 * xmlRelaxNGValidatePushCData:
8348 * @ctxt: the RelaxNG validation context
8349 * @data: some character data read
Michael Woodfb27e2c2012-09-28 08:59:33 +02008350 * @len: the length of the data
Daniel Veillardf4e55762003-04-15 23:32:22 +00008351 *
8352 * check the CData parsed for validation in the current stack
8353 *
8354 * returns 1 if no validation problem was found or -1 otherwise
8355 */
8356int
8357xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00008358 const xmlChar * data, int len ATTRIBUTE_UNUSED)
Daniel Veillardf4e55762003-04-15 23:32:22 +00008359{
8360 int ret = 1;
8361
8362 if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL))
8363 return (-1);
8364
8365#ifdef DEBUG_PROGRESSIVE
8366 xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len);
8367#endif
8368
8369 while (*data != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008370 if (!IS_BLANK_CH(*data))
Daniel Veillardf4e55762003-04-15 23:32:22 +00008371 break;
8372 data++;
8373 }
8374 if (*data == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008375 return (1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008376
8377 ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt);
8378 if (ret < 0) {
Daniel Veillard33300b42003-04-17 09:09:19 +00008379 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO ");
Daniel Veillardf4e55762003-04-15 23:32:22 +00008380#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00008381 xmlGenericError(xmlGenericErrorContext, "CDATA failed\n");
Daniel Veillardf4e55762003-04-15 23:32:22 +00008382#endif
8383
Daniel Veillard4c004142003-10-07 11:33:24 +00008384 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008385 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008386 return (1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008387}
8388
8389/**
8390 * xmlRelaxNGValidatePopElement:
8391 * @ctxt: the RelaxNG validation context
8392 * @doc: a document instance
8393 * @elem: an element instance
8394 *
8395 * Pop the element end from the RelaxNG validation stack.
8396 *
8397 * returns 1 if no validation problem was found or 0 otherwise
8398 */
8399int
8400xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt,
8401 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008402 xmlNodePtr elem)
8403{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008404 int ret;
8405 xmlRegExecCtxtPtr exec;
8406
Daniel Veillard4c004142003-10-07 11:33:24 +00008407 if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL))
8408 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008409#ifdef DEBUG_PROGRESSIVE
8410 xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name);
8411#endif
8412 /*
8413 * verify that we reached a terminal state of the content model.
8414 */
8415 exec = xmlRelaxNGElemPop(ctxt);
8416 ret = xmlRegExecPushString(exec, NULL, NULL);
8417 if (ret == 0) {
8418 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00008419 * TODO: get some of the names needed to exit the current state of exec
8420 */
8421 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
8422 ret = -1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008423 } else if (ret < 0) {
8424 ret = -1;
8425 } else {
8426 ret = 1;
8427 }
8428 xmlRegFreeExecCtxt(exec);
8429#ifdef DEBUG_PROGRESSIVE
8430 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008431 xmlGenericError(xmlGenericErrorContext, "PopElem %s failed\n",
8432 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008433#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008434 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008435}
8436
8437/**
8438 * xmlRelaxNGValidateFullElement:
8439 * @ctxt: the validation context
8440 * @doc: a document instance
8441 * @elem: an element instance
8442 *
8443 * Validate a full subtree when xmlRelaxNGValidatePushElement() returned
8444 * 0 and the content of the node has been expanded.
8445 *
8446 * returns 1 if no validation problem was found or -1 in case of error.
8447 */
8448int
Daniel Veillard33300b42003-04-17 09:09:19 +00008449xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt,
8450 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008451 xmlNodePtr elem)
8452{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008453 int ret;
8454 xmlRelaxNGValidStatePtr state;
8455
Daniel Veillard4c004142003-10-07 11:33:24 +00008456 if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL))
8457 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008458#ifdef DEBUG_PROGRESSIVE
8459 xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name);
8460#endif
8461 state = xmlRelaxNGNewValidState(ctxt, elem->parent);
8462 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008463 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008464 }
8465 state->seq = elem;
8466 ctxt->state = state;
8467 ctxt->errNo = XML_RELAXNG_OK;
8468 ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef);
Daniel Veillard4c004142003-10-07 11:33:24 +00008469 if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK))
8470 ret = -1;
8471 else
8472 ret = 1;
Daniel Veillard9fcd4622009-08-14 16:16:31 +02008473 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008474 ctxt->state = NULL;
8475#ifdef DEBUG_PROGRESSIVE
8476 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008477 xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n",
8478 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008479#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008480 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008481}
8482
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008483/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08008484 * *
8485 * Generic interpreted validation implementation *
8486 * *
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008487 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00008488static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8489 xmlRelaxNGDefinePtr define);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008490
8491/**
8492 * xmlRelaxNGSkipIgnored:
8493 * @ctxt: a schema validation context
8494 * @node: the top node.
8495 *
8496 * Skip ignorable nodes in that context
8497 *
8498 * Returns the new sibling or NULL in case of error.
8499 */
8500static xmlNodePtr
8501xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008502 xmlNodePtr node)
8503{
Daniel Veillard6eadf632003-01-23 18:29:16 +00008504 /*
8505 * TODO complete and handle entities
8506 */
8507 while ((node != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00008508 ((node->type == XML_COMMENT_NODE) ||
8509 (node->type == XML_PI_NODE) ||
William M. Brack7217c862004-03-15 02:43:56 +00008510 (node->type == XML_XINCLUDE_START) ||
8511 (node->type == XML_XINCLUDE_END) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00008512 (((node->type == XML_TEXT_NODE) ||
8513 (node->type == XML_CDATA_SECTION_NODE)) &&
8514 ((ctxt->flags & FLAGS_MIXED_CONTENT) ||
8515 (IS_BLANK_NODE(node)))))) {
8516 node = node->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008517 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008518 return (node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008519}
8520
8521/**
Daniel Veillardedc91922003-01-26 00:52:04 +00008522 * xmlRelaxNGNormalize:
8523 * @ctxt: a schema validation context
8524 * @str: the string to normalize
8525 *
8526 * Implements the normalizeWhiteSpace( s ) function from
8527 * section 6.2.9 of the spec
8528 *
8529 * Returns the new string or NULL in case of error.
8530 */
8531static xmlChar *
Daniel Veillard4c004142003-10-07 11:33:24 +00008532xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar * str)
8533{
Daniel Veillardedc91922003-01-26 00:52:04 +00008534 xmlChar *ret, *p;
8535 const xmlChar *tmp;
8536 int len;
Daniel Veillard4c004142003-10-07 11:33:24 +00008537
Daniel Veillardedc91922003-01-26 00:52:04 +00008538 if (str == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00008539 return (NULL);
Daniel Veillardedc91922003-01-26 00:52:04 +00008540 tmp = str;
Daniel Veillard4c004142003-10-07 11:33:24 +00008541 while (*tmp != 0)
8542 tmp++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008543 len = tmp - str;
8544
Daniel Veillard3c908dc2003-04-19 00:07:51 +00008545 ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar));
Daniel Veillardedc91922003-01-26 00:52:04 +00008546 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008547 xmlRngVErrMemory(ctxt, "validating\n");
8548 return (NULL);
Daniel Veillardedc91922003-01-26 00:52:04 +00008549 }
8550 p = ret;
William M. Brack76e95df2003-10-18 16:20:14 +00008551 while (IS_BLANK_CH(*str))
Daniel Veillard4c004142003-10-07 11:33:24 +00008552 str++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008553 while (*str != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008554 if (IS_BLANK_CH(*str)) {
8555 while (IS_BLANK_CH(*str))
Daniel Veillard4c004142003-10-07 11:33:24 +00008556 str++;
8557 if (*str == 0)
8558 break;
8559 *p++ = ' ';
8560 } else
8561 *p++ = *str++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008562 }
8563 *p = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00008564 return (ret);
Daniel Veillardedc91922003-01-26 00:52:04 +00008565}
8566
8567/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008568 * xmlRelaxNGValidateDatatype:
8569 * @ctxt: a Relax-NG validation context
8570 * @value: the string value
8571 * @type: the datatype definition
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008572 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008573 *
8574 * Validate the given value against the dataype
8575 *
8576 * Returns 0 if the validation succeeded or an error code.
8577 */
8578static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008579xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt,
8580 const xmlChar * value,
8581 xmlRelaxNGDefinePtr define, xmlNodePtr node)
8582{
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008583 int ret, tmp;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008584 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008585 void *result = NULL;
8586 xmlRelaxNGDefinePtr cur;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008587
8588 if ((define == NULL) || (define->data == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008589 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008590 }
8591 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008592 if (lib->check != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008593 if ((define->attrs != NULL) &&
8594 (define->attrs->type == XML_RELAXNG_PARAM)) {
8595 ret =
8596 lib->check(lib->data, define->name, value, &result, node);
8597 } else {
8598 ret = lib->check(lib->data, define->name, value, NULL, node);
8599 }
8600 } else
8601 ret = -1;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008602 if (ret < 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008603 VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name);
8604 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
8605 lib->freef(lib->data, result);
8606 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008607 } else if (ret == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008608 ret = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008609 } else if (ret == 2) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008610 VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008611 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00008612 VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value);
8613 ret = -1;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008614 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008615 cur = define->attrs;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008616 while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008617 if (lib->facet != NULL) {
8618 tmp = lib->facet(lib->data, define->name, cur->name,
8619 cur->value, value, result);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008620 if (tmp != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008621 ret = -1;
8622 }
8623 cur = cur->next;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008624 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008625 if ((ret == 0) && (define->content != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008626 const xmlChar *oldvalue, *oldendvalue;
Daniel Veillard416589a2003-02-17 17:25:42 +00008627
Daniel Veillard4c004142003-10-07 11:33:24 +00008628 oldvalue = ctxt->state->value;
8629 oldendvalue = ctxt->state->endvalue;
8630 ctxt->state->value = (xmlChar *) value;
8631 ctxt->state->endvalue = NULL;
8632 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8633 ctxt->state->value = (xmlChar *) oldvalue;
8634 ctxt->state->endvalue = (xmlChar *) oldendvalue;
Daniel Veillard416589a2003-02-17 17:25:42 +00008635 }
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008636 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00008637 lib->freef(lib->data, result);
8638 return (ret);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008639}
8640
8641/**
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008642 * xmlRelaxNGNextValue:
8643 * @ctxt: a Relax-NG validation context
8644 *
8645 * Skip to the next value when validating within a list
8646 *
8647 * Returns 0 if the operation succeeded or an error code.
8648 */
8649static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008650xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt)
8651{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008652 xmlChar *cur;
8653
8654 cur = ctxt->state->value;
8655 if ((cur == NULL) || (ctxt->state->endvalue == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008656 ctxt->state->value = NULL;
8657 ctxt->state->endvalue = NULL;
8658 return (0);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008659 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008660 while (*cur != 0)
8661 cur++;
8662 while ((cur != ctxt->state->endvalue) && (*cur == 0))
8663 cur++;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008664 if (cur == ctxt->state->endvalue)
Daniel Veillard4c004142003-10-07 11:33:24 +00008665 ctxt->state->value = NULL;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008666 else
Daniel Veillard4c004142003-10-07 11:33:24 +00008667 ctxt->state->value = cur;
8668 return (0);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008669}
8670
8671/**
8672 * xmlRelaxNGValidateValueList:
8673 * @ctxt: a Relax-NG validation context
8674 * @defines: the list of definitions to verify
8675 *
8676 * Validate the given set of definitions for the current value
8677 *
8678 * Returns 0 if the validation succeeded or an error code.
8679 */
8680static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008681xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt,
8682 xmlRelaxNGDefinePtr defines)
8683{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008684 int ret = 0;
8685
8686 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008687 ret = xmlRelaxNGValidateValue(ctxt, defines);
8688 if (ret != 0)
8689 break;
8690 defines = defines->next;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008691 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008692 return (ret);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008693}
8694
8695/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00008696 * xmlRelaxNGValidateValue:
8697 * @ctxt: a Relax-NG validation context
8698 * @define: the definition to verify
8699 *
8700 * Validate the given definition for the current value
8701 *
8702 * Returns 0 if the validation succeeded or an error code.
8703 */
8704static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008705xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8706 xmlRelaxNGDefinePtr define)
8707{
Daniel Veillardedc91922003-01-26 00:52:04 +00008708 int ret = 0, oldflags;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008709 xmlChar *value;
8710
8711 value = ctxt->state->value;
8712 switch (define->type) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008713 case XML_RELAXNG_EMPTY:{
8714 if ((value != NULL) && (value[0] != 0)) {
8715 int idx = 0;
Daniel Veillardd4310742003-02-18 21:12:46 +00008716
William M. Brack76e95df2003-10-18 16:20:14 +00008717 while (IS_BLANK_CH(value[idx]))
Daniel Veillard4c004142003-10-07 11:33:24 +00008718 idx++;
8719 if (value[idx] != 0)
8720 ret = -1;
8721 }
8722 break;
8723 }
8724 case XML_RELAXNG_TEXT:
8725 break;
8726 case XML_RELAXNG_VALUE:{
8727 if (!xmlStrEqual(value, define->value)) {
8728 if (define->name != NULL) {
8729 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillardedc91922003-01-26 00:52:04 +00008730
Daniel Veillard4c004142003-10-07 11:33:24 +00008731 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
8732 if ((lib != NULL) && (lib->comp != NULL)) {
8733 ret = lib->comp(lib->data, define->name,
8734 define->value, define->node,
8735 (void *) define->attrs,
8736 value, ctxt->state->node);
8737 } else
8738 ret = -1;
8739 if (ret < 0) {
8740 VALID_ERR2(XML_RELAXNG_ERR_TYPECMP,
8741 define->name);
8742 return (-1);
8743 } else if (ret == 1) {
8744 ret = 0;
8745 } else {
8746 ret = -1;
8747 }
8748 } else {
8749 xmlChar *nval, *nvalue;
Daniel Veillardedc91922003-01-26 00:52:04 +00008750
Daniel Veillard4c004142003-10-07 11:33:24 +00008751 /*
8752 * TODO: trivial optimizations are possible by
8753 * computing at compile-time
8754 */
8755 nval = xmlRelaxNGNormalize(ctxt, define->value);
8756 nvalue = xmlRelaxNGNormalize(ctxt, value);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008757
Daniel Veillard4c004142003-10-07 11:33:24 +00008758 if ((nval == NULL) || (nvalue == NULL) ||
8759 (!xmlStrEqual(nval, nvalue)))
8760 ret = -1;
8761 if (nval != NULL)
8762 xmlFree(nval);
8763 if (nvalue != NULL)
8764 xmlFree(nvalue);
8765 }
8766 }
8767 if (ret == 0)
8768 xmlRelaxNGNextValue(ctxt);
8769 break;
8770 }
8771 case XML_RELAXNG_DATATYPE:{
8772 ret = xmlRelaxNGValidateDatatype(ctxt, value, define,
8773 ctxt->state->seq);
8774 if (ret == 0)
8775 xmlRelaxNGNextValue(ctxt);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008776
Daniel Veillard4c004142003-10-07 11:33:24 +00008777 break;
8778 }
8779 case XML_RELAXNG_CHOICE:{
8780 xmlRelaxNGDefinePtr list = define->content;
8781 xmlChar *oldvalue;
8782
8783 oldflags = ctxt->flags;
8784 ctxt->flags |= FLAGS_IGNORABLE;
8785
8786 oldvalue = ctxt->state->value;
8787 while (list != NULL) {
8788 ret = xmlRelaxNGValidateValue(ctxt, list);
8789 if (ret == 0) {
8790 break;
8791 }
8792 ctxt->state->value = oldvalue;
8793 list = list->next;
8794 }
8795 ctxt->flags = oldflags;
8796 if (ret != 0) {
8797 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8798 xmlRelaxNGDumpValidError(ctxt);
8799 } else {
8800 if (ctxt->errNr > 0)
8801 xmlRelaxNGPopErrors(ctxt, 0);
8802 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008803 break;
8804 }
8805 case XML_RELAXNG_LIST:{
8806 xmlRelaxNGDefinePtr list = define->content;
8807 xmlChar *oldvalue, *oldend, *val, *cur;
8808
Daniel Veillard416589a2003-02-17 17:25:42 +00008809#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008810 int nb_values = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00008811#endif
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008812
Daniel Veillard4c004142003-10-07 11:33:24 +00008813 oldvalue = ctxt->state->value;
8814 oldend = ctxt->state->endvalue;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008815
Daniel Veillard4c004142003-10-07 11:33:24 +00008816 val = xmlStrdup(oldvalue);
8817 if (val == NULL) {
8818 val = xmlStrdup(BAD_CAST "");
8819 }
8820 if (val == NULL) {
8821 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
8822 return (-1);
8823 }
8824 cur = val;
8825 while (*cur != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008826 if (IS_BLANK_CH(*cur)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008827 *cur = 0;
8828 cur++;
Daniel Veillard416589a2003-02-17 17:25:42 +00008829#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008830 nb_values++;
Daniel Veillard416589a2003-02-17 17:25:42 +00008831#endif
William M. Brack76e95df2003-10-18 16:20:14 +00008832 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00008833 *cur++ = 0;
8834 } else
8835 cur++;
8836 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008837#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008838 xmlGenericError(xmlGenericErrorContext,
8839 "list value: '%s' found %d items\n",
8840 oldvalue, nb_values);
8841 nb_values = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008842#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008843 ctxt->state->endvalue = cur;
8844 cur = val;
8845 while ((*cur == 0) && (cur != ctxt->state->endvalue))
8846 cur++;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008847
Daniel Veillard4c004142003-10-07 11:33:24 +00008848 ctxt->state->value = cur;
8849
8850 while (list != NULL) {
8851 if (ctxt->state->value == ctxt->state->endvalue)
8852 ctxt->state->value = NULL;
8853 ret = xmlRelaxNGValidateValue(ctxt, list);
8854 if (ret != 0) {
8855#ifdef DEBUG_LIST
8856 xmlGenericError(xmlGenericErrorContext,
8857 "Failed to validate value: '%s' with %d rule\n",
8858 ctxt->state->value, nb_values);
8859#endif
8860 break;
8861 }
8862#ifdef DEBUG_LIST
8863 nb_values++;
8864#endif
8865 list = list->next;
8866 }
8867
8868 if ((ret == 0) && (ctxt->state->value != NULL) &&
8869 (ctxt->state->value != ctxt->state->endvalue)) {
8870 VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA,
8871 ctxt->state->value);
8872 ret = -1;
8873 }
8874 xmlFree(val);
8875 ctxt->state->value = oldvalue;
8876 ctxt->state->endvalue = oldend;
8877 break;
8878 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008879 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00008880 ret = xmlRelaxNGValidateValueList(ctxt, define->content);
8881 if (ret != 0) {
8882 break;
8883 }
8884 /* no break on purpose */
8885 case XML_RELAXNG_ZEROORMORE:{
8886 xmlChar *cur, *temp;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008887
Daniel Veillard7dd0d912011-11-10 18:08:33 +08008888 if ((ctxt->state->value == NULL) ||
8889 (*ctxt->state->value == 0)) {
8890 ret = 0;
8891 break;
8892 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008893 oldflags = ctxt->flags;
8894 ctxt->flags |= FLAGS_IGNORABLE;
8895 cur = ctxt->state->value;
8896 temp = NULL;
8897 while ((cur != NULL) && (cur != ctxt->state->endvalue) &&
8898 (temp != cur)) {
8899 temp = cur;
8900 ret =
8901 xmlRelaxNGValidateValueList(ctxt, define->content);
8902 if (ret != 0) {
8903 ctxt->state->value = temp;
8904 ret = 0;
8905 break;
8906 }
8907 cur = ctxt->state->value;
8908 }
8909 ctxt->flags = oldflags;
Daniel Veillard14b56432006-03-09 18:41:40 +00008910 if (ctxt->errNr > 0)
8911 xmlRelaxNGPopErrors(ctxt, 0);
Daniel Veillard4c004142003-10-07 11:33:24 +00008912 break;
8913 }
Daniel Veillard7dd0d912011-11-10 18:08:33 +08008914 case XML_RELAXNG_OPTIONAL:{
8915 xmlChar *temp;
8916
8917 if ((ctxt->state->value == NULL) ||
8918 (*ctxt->state->value == 0)) {
8919 ret = 0;
8920 break;
8921 }
8922 oldflags = ctxt->flags;
8923 ctxt->flags |= FLAGS_IGNORABLE;
8924 temp = ctxt->state->value;
8925 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8926 ctxt->flags = oldflags;
8927 if (ret != 0) {
8928 ctxt->state->value = temp;
8929 if (ctxt->errNr > 0)
8930 xmlRelaxNGPopErrors(ctxt, 0);
8931 ret = 0;
8932 break;
8933 }
8934 if (ctxt->errNr > 0)
8935 xmlRelaxNGPopErrors(ctxt, 0);
8936 break;
8937 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008938 case XML_RELAXNG_EXCEPT:{
8939 xmlRelaxNGDefinePtr list;
Daniel Veillard416589a2003-02-17 17:25:42 +00008940
Daniel Veillard4c004142003-10-07 11:33:24 +00008941 list = define->content;
8942 while (list != NULL) {
8943 ret = xmlRelaxNGValidateValue(ctxt, list);
8944 if (ret == 0) {
8945 ret = -1;
8946 break;
8947 } else
8948 ret = 0;
8949 list = list->next;
8950 }
8951 break;
8952 }
Daniel Veillard463a5472003-02-27 21:30:32 +00008953 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00008954 case XML_RELAXNG_GROUP:{
8955 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008956
Daniel Veillard4c004142003-10-07 11:33:24 +00008957 list = define->content;
8958 while (list != NULL) {
8959 ret = xmlRelaxNGValidateValue(ctxt, list);
8960 if (ret != 0) {
8961 ret = -1;
8962 break;
8963 } else
8964 ret = 0;
8965 list = list->next;
8966 }
8967 break;
8968 }
Daniel Veillard463a5472003-02-27 21:30:32 +00008969 case XML_RELAXNG_REF:
8970 case XML_RELAXNG_PARENTREF:
Daniel Veillard25a1ce92008-06-02 16:04:12 +00008971 if (define->content == NULL) {
Daniel Veillard81c51e12009-08-14 18:52:10 +02008972 VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
8973 ret = -1;
8974 } else {
8975 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8976 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008977 break;
8978 default:
8979 TODO ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008980 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008981 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008982}
8983
8984/**
8985 * xmlRelaxNGValidateValueContent:
8986 * @ctxt: a Relax-NG validation context
8987 * @defines: the list of definitions to verify
8988 *
8989 * Validate the given definitions for the current value
8990 *
8991 * Returns 0 if the validation succeeded or an error code.
8992 */
8993static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008994xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt,
8995 xmlRelaxNGDefinePtr defines)
8996{
Daniel Veillard6eadf632003-01-23 18:29:16 +00008997 int ret = 0;
8998
8999 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009000 ret = xmlRelaxNGValidateValue(ctxt, defines);
9001 if (ret != 0)
9002 break;
9003 defines = defines->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00009004 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009005 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00009006}
9007
9008/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009009 * xmlRelaxNGAttributeMatch:
9010 * @ctxt: a Relax-NG validation context
9011 * @define: the definition to check
9012 * @prop: the attribute
9013 *
9014 * Check if the attribute matches the definition nameClass
9015 *
9016 * Returns 1 if the attribute matches, 0 if no, or -1 in case of error
9017 */
9018static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009019xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt,
9020 xmlRelaxNGDefinePtr define, xmlAttrPtr prop)
9021{
Daniel Veillardfd573f12003-03-16 17:52:32 +00009022 int ret;
9023
9024 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009025 if (!xmlStrEqual(define->name, prop->name))
9026 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009027 }
9028 if (define->ns != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009029 if (define->ns[0] == 0) {
9030 if (prop->ns != NULL)
9031 return (0);
9032 } else {
9033 if ((prop->ns == NULL) ||
9034 (!xmlStrEqual(define->ns, prop->ns->href)))
9035 return (0);
9036 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009037 }
9038 if (define->nameClass == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00009039 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009040 define = define->nameClass;
9041 if (define->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009042 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009043
Daniel Veillard4c004142003-10-07 11:33:24 +00009044 list = define->content;
9045 while (list != NULL) {
9046 ret = xmlRelaxNGAttributeMatch(ctxt, list, prop);
9047 if (ret == 1)
9048 return (0);
9049 if (ret < 0)
9050 return (ret);
9051 list = list->next;
9052 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009053 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009054 TODO}
9055 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009056}
9057
9058/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00009059 * xmlRelaxNGValidateAttribute:
9060 * @ctxt: a Relax-NG validation context
9061 * @define: the definition to verify
9062 *
9063 * Validate the given attribute definition for that node
9064 *
9065 * Returns 0 if the validation succeeded or an error code.
9066 */
9067static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009068xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt,
9069 xmlRelaxNGDefinePtr define)
9070{
Daniel Veillard6eadf632003-01-23 18:29:16 +00009071 int ret = 0, i;
9072 xmlChar *value, *oldvalue;
9073 xmlAttrPtr prop = NULL, tmp;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00009074 xmlNodePtr oldseq;
Daniel Veillard6eadf632003-01-23 18:29:16 +00009075
Daniel Veillard1ed7f362003-02-03 10:57:45 +00009076 if (ctxt->state->nbAttrLeft <= 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00009077 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00009078 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009079 for (i = 0; i < ctxt->state->nbAttrs; i++) {
9080 tmp = ctxt->state->attrs[i];
9081 if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) {
9082 if ((((define->ns == NULL) || (define->ns[0] == 0)) &&
9083 (tmp->ns == NULL)) ||
9084 ((tmp->ns != NULL) &&
9085 (xmlStrEqual(define->ns, tmp->ns->href)))) {
9086 prop = tmp;
9087 break;
9088 }
9089 }
9090 }
9091 if (prop != NULL) {
9092 value = xmlNodeListGetString(prop->doc, prop->children, 1);
9093 oldvalue = ctxt->state->value;
9094 oldseq = ctxt->state->seq;
9095 ctxt->state->seq = (xmlNodePtr) prop;
9096 ctxt->state->value = value;
9097 ctxt->state->endvalue = NULL;
9098 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
9099 if (ctxt->state->value != NULL)
9100 value = ctxt->state->value;
9101 if (value != NULL)
9102 xmlFree(value);
9103 ctxt->state->value = oldvalue;
9104 ctxt->state->seq = oldseq;
9105 if (ret == 0) {
9106 /*
9107 * flag the attribute as processed
9108 */
9109 ctxt->state->attrs[i] = NULL;
9110 ctxt->state->nbAttrLeft--;
9111 }
9112 } else {
9113 ret = -1;
9114 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00009115#ifdef DEBUG
Daniel Veillard4c004142003-10-07 11:33:24 +00009116 xmlGenericError(xmlGenericErrorContext,
9117 "xmlRelaxNGValidateAttribute(%s): %d\n",
9118 define->name, ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00009119#endif
9120 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009121 for (i = 0; i < ctxt->state->nbAttrs; i++) {
9122 tmp = ctxt->state->attrs[i];
9123 if ((tmp != NULL) &&
9124 (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) {
9125 prop = tmp;
9126 break;
9127 }
9128 }
9129 if (prop != NULL) {
9130 value = xmlNodeListGetString(prop->doc, prop->children, 1);
9131 oldvalue = ctxt->state->value;
9132 oldseq = ctxt->state->seq;
9133 ctxt->state->seq = (xmlNodePtr) prop;
9134 ctxt->state->value = value;
9135 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
9136 if (ctxt->state->value != NULL)
9137 value = ctxt->state->value;
9138 if (value != NULL)
9139 xmlFree(value);
9140 ctxt->state->value = oldvalue;
9141 ctxt->state->seq = oldseq;
9142 if (ret == 0) {
9143 /*
9144 * flag the attribute as processed
9145 */
9146 ctxt->state->attrs[i] = NULL;
9147 ctxt->state->nbAttrLeft--;
9148 }
9149 } else {
9150 ret = -1;
9151 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00009152#ifdef DEBUG
Daniel Veillard4c004142003-10-07 11:33:24 +00009153 if (define->ns != NULL) {
9154 xmlGenericError(xmlGenericErrorContext,
9155 "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n",
9156 define->ns, ret);
9157 } else {
9158 xmlGenericError(xmlGenericErrorContext,
9159 "xmlRelaxNGValidateAttribute(anyName): %d\n",
9160 ret);
9161 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00009162#endif
Daniel Veillard6eadf632003-01-23 18:29:16 +00009163 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009164
9165 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00009166}
9167
9168/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009169 * xmlRelaxNGValidateAttributeList:
9170 * @ctxt: a Relax-NG validation context
9171 * @define: the list of definition to verify
9172 *
9173 * Validate the given node against the list of attribute definitions
9174 *
9175 * Returns 0 if the validation succeeded or an error code.
9176 */
9177static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009178xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
9179 xmlRelaxNGDefinePtr defines)
9180{
Daniel Veillardce192eb2003-04-16 15:58:05 +00009181 int ret = 0, res;
9182 int needmore = 0;
9183 xmlRelaxNGDefinePtr cur;
9184
9185 cur = defines;
9186 while (cur != NULL) {
9187 if (cur->type == XML_RELAXNG_ATTRIBUTE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009188 if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0)
9189 ret = -1;
9190 } else
9191 needmore = 1;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009192 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009193 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00009194 if (!needmore)
Daniel Veillard4c004142003-10-07 11:33:24 +00009195 return (ret);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009196 cur = defines;
9197 while (cur != NULL) {
9198 if (cur->type != XML_RELAXNG_ATTRIBUTE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009199 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
9200 res = xmlRelaxNGValidateDefinition(ctxt, cur);
9201 if (res < 0)
9202 ret = -1;
9203 } else {
9204 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
9205 return (-1);
9206 }
9207 if (res == -1) /* continues on -2 */
9208 break;
9209 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00009210 cur = cur->next;
9211 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009212
9213 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009214}
9215
9216/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009217 * xmlRelaxNGNodeMatchesList:
9218 * @node: the node
9219 * @list: a NULL terminated array of definitions
9220 *
9221 * Check if a node can be matched by one of the definitions
9222 *
9223 * Returns 1 if matches 0 otherwise
9224 */
9225static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009226xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr * list)
9227{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009228 xmlRelaxNGDefinePtr cur;
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00009229 int i = 0, tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009230
9231 if ((node == NULL) || (list == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00009232 return (0);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009233
9234 cur = list[i++];
9235 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009236 if ((node->type == XML_ELEMENT_NODE) &&
9237 (cur->type == XML_RELAXNG_ELEMENT)) {
9238 tmp = xmlRelaxNGElementMatch(NULL, cur, node);
9239 if (tmp == 1)
9240 return (1);
9241 } else if (((node->type == XML_TEXT_NODE) ||
9242 (node->type == XML_CDATA_SECTION_NODE)) &&
9243 (cur->type == XML_RELAXNG_TEXT)) {
9244 return (1);
9245 }
9246 cur = list[i++];
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009247 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009248 return (0);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009249}
9250
9251/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009252 * xmlRelaxNGValidateInterleave:
9253 * @ctxt: a Relax-NG validation context
9254 * @define: the definition to verify
9255 *
9256 * Validate an interleave definition for a node.
9257 *
9258 * Returns 0 if the validation succeeded or an error code.
9259 */
9260static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009261xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt,
9262 xmlRelaxNGDefinePtr define)
9263{
William M. Brack779af002003-08-01 15:55:39 +00009264 int ret = 0, i, nbgroups;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009265 int errNr = ctxt->errNr;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009266 int oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009267
9268 xmlRelaxNGValidStatePtr oldstate;
9269 xmlRelaxNGPartitionPtr partitions;
9270 xmlRelaxNGInterleaveGroupPtr group = NULL;
9271 xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem;
9272 xmlNodePtr *list = NULL, *lasts = NULL;
9273
9274 if (define->data != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009275 partitions = (xmlRelaxNGPartitionPtr) define->data;
9276 nbgroups = partitions->nbgroups;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009277 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009278 VALID_ERR(XML_RELAXNG_ERR_INTERNODATA);
9279 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009280 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009281 /*
9282 * Optimizations for MIXED
9283 */
9284 oldflags = ctxt->flags;
Daniel Veillarde063f482003-03-21 16:53:17 +00009285 if (define->dflags & IS_MIXED) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009286 ctxt->flags |= FLAGS_MIXED_CONTENT;
9287 if (nbgroups == 2) {
9288 /*
9289 * this is a pure <mixed> case
9290 */
9291 if (ctxt->state != NULL)
9292 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
9293 ctxt->state->seq);
9294 if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT)
9295 ret = xmlRelaxNGValidateDefinition(ctxt,
9296 partitions->groups[1]->
9297 rule);
9298 else
9299 ret = xmlRelaxNGValidateDefinition(ctxt,
9300 partitions->groups[0]->
9301 rule);
9302 if (ret == 0) {
9303 if (ctxt->state != NULL)
9304 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
9305 ctxt->state->
9306 seq);
9307 }
9308 ctxt->flags = oldflags;
9309 return (ret);
9310 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009311 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009312
9313 /*
9314 * Build arrays to store the first and last node of the chain
9315 * pertaining to each group
9316 */
9317 list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9318 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009319 xmlRngVErrMemory(ctxt, "validating\n");
9320 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009321 }
9322 memset(list, 0, nbgroups * sizeof(xmlNodePtr));
9323 lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9324 if (lasts == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009325 xmlRngVErrMemory(ctxt, "validating\n");
9326 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009327 }
9328 memset(lasts, 0, nbgroups * sizeof(xmlNodePtr));
9329
9330 /*
9331 * Walk the sequence of children finding the right group and
9332 * sorting them in sequences.
9333 */
9334 cur = ctxt->state->seq;
9335 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9336 start = cur;
9337 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009338 ctxt->state->seq = cur;
9339 if ((partitions->triage != NULL) &&
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009340 (partitions->flags & IS_DETERMINIST)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009341 void *tmp = NULL;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009342
Daniel Veillard4c004142003-10-07 11:33:24 +00009343 if ((cur->type == XML_TEXT_NODE) ||
9344 (cur->type == XML_CDATA_SECTION_NODE)) {
9345 tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text",
9346 NULL);
9347 } else if (cur->type == XML_ELEMENT_NODE) {
9348 if (cur->ns != NULL) {
9349 tmp = xmlHashLookup2(partitions->triage, cur->name,
9350 cur->ns->href);
9351 if (tmp == NULL)
9352 tmp = xmlHashLookup2(partitions->triage,
9353 BAD_CAST "#any",
9354 cur->ns->href);
9355 } else
9356 tmp =
9357 xmlHashLookup2(partitions->triage, cur->name,
9358 NULL);
9359 if (tmp == NULL)
9360 tmp =
9361 xmlHashLookup2(partitions->triage, BAD_CAST "#any",
9362 NULL);
9363 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009364
Daniel Veillard4c004142003-10-07 11:33:24 +00009365 if (tmp == NULL) {
9366 i = nbgroups;
9367 } else {
9368 i = ((long) tmp) - 1;
9369 if (partitions->flags & IS_NEEDCHECK) {
9370 group = partitions->groups[i];
9371 if (!xmlRelaxNGNodeMatchesList(cur, group->defs))
9372 i = nbgroups;
9373 }
9374 }
9375 } else {
9376 for (i = 0; i < nbgroups; i++) {
9377 group = partitions->groups[i];
9378 if (group == NULL)
9379 continue;
9380 if (xmlRelaxNGNodeMatchesList(cur, group->defs))
9381 break;
9382 }
9383 }
9384 /*
9385 * We break as soon as an element not matched is found
9386 */
9387 if (i >= nbgroups) {
9388 break;
9389 }
9390 if (lasts[i] != NULL) {
9391 lasts[i]->next = cur;
9392 lasts[i] = cur;
9393 } else {
9394 list[i] = cur;
9395 lasts[i] = cur;
9396 }
9397 if (cur->next != NULL)
9398 lastchg = cur->next;
9399 else
9400 lastchg = cur;
9401 cur = xmlRelaxNGSkipIgnored(ctxt, cur->next);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009402 }
9403 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009404 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9405 ret = -1;
9406 goto done;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009407 }
9408 lastelem = cur;
9409 oldstate = ctxt->state;
Daniel Veillard4c004142003-10-07 11:33:24 +00009410 for (i = 0; i < nbgroups; i++) {
9411 ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate);
9412 group = partitions->groups[i];
9413 if (lasts[i] != NULL) {
9414 last = lasts[i]->next;
9415 lasts[i]->next = NULL;
9416 }
9417 ctxt->state->seq = list[i];
9418 ret = xmlRelaxNGValidateDefinition(ctxt, group->rule);
9419 if (ret != 0)
9420 break;
9421 if (ctxt->state != NULL) {
9422 cur = ctxt->state->seq;
9423 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9424 xmlRelaxNGFreeValidState(ctxt, oldstate);
9425 oldstate = ctxt->state;
9426 ctxt->state = NULL;
9427 if (cur != NULL) {
9428 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9429 ret = -1;
9430 ctxt->state = oldstate;
9431 goto done;
9432 }
9433 } else if (ctxt->states != NULL) {
9434 int j;
9435 int found = 0;
Daniel Veillard87254c82006-02-19 15:27:17 +00009436 int best = -1;
9437 int lowattr = -1;
9438
9439 /*
9440 * PBM: what happen if there is attributes checks in the interleaves
9441 */
Daniel Veillardfd573f12003-03-16 17:52:32 +00009442
Daniel Veillard4c004142003-10-07 11:33:24 +00009443 for (j = 0; j < ctxt->states->nbState; j++) {
9444 cur = ctxt->states->tabState[j]->seq;
9445 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9446 if (cur == NULL) {
Daniel Veillard87254c82006-02-19 15:27:17 +00009447 if (found == 0) {
9448 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9449 best = j;
9450 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009451 found = 1;
Daniel Veillard87254c82006-02-19 15:27:17 +00009452 if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr) {
9453 /* try to keep the latest one to mach old heuristic */
9454 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9455 best = j;
9456 }
9457 if (lowattr == 0)
9458 break;
9459 } else if (found == 0) {
9460 if (lowattr == -1) {
9461 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9462 best = j;
9463 } else
9464 if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr) {
9465 /* try to keep the latest one to mach old heuristic */
9466 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9467 best = j;
9468 }
9469 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009470 }
Daniel Veillard87254c82006-02-19 15:27:17 +00009471 /*
9472 * BIG PBM: here we pick only one restarting point :-(
9473 */
Daniel Veillard4c004142003-10-07 11:33:24 +00009474 if (ctxt->states->nbState > 0) {
9475 xmlRelaxNGFreeValidState(ctxt, oldstate);
Daniel Veillard87254c82006-02-19 15:27:17 +00009476 if (best != -1) {
9477 oldstate = ctxt->states->tabState[best];
9478 ctxt->states->tabState[best] = NULL;
9479 } else {
9480 oldstate =
9481 ctxt->states->tabState[ctxt->states->nbState - 1];
9482 ctxt->states->tabState[ctxt->states->nbState - 1] = NULL;
Daniel Veillard9fcd4622009-08-14 16:16:31 +02009483 ctxt->states->nbState--;
Daniel Veillard87254c82006-02-19 15:27:17 +00009484 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009485 }
Daniel Veillard87254c82006-02-19 15:27:17 +00009486 for (j = 0; j < ctxt->states->nbState ; j++) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009487 xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[j]);
9488 }
9489 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9490 ctxt->states = NULL;
9491 if (found == 0) {
Daniel Veillard76d36452009-09-07 11:19:33 +02009492 if (cur == NULL) {
Ben Waltona7a6a4b2010-03-15 10:06:36 +01009493 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA,
9494 (const xmlChar *) "noname");
Daniel Veillard76d36452009-09-07 11:19:33 +02009495 } else {
9496 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9497 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009498 ret = -1;
9499 ctxt->state = oldstate;
9500 goto done;
9501 }
9502 } else {
9503 ret = -1;
9504 break;
9505 }
9506 if (lasts[i] != NULL) {
9507 lasts[i]->next = last;
9508 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009509 }
9510 if (ctxt->state != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00009511 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009512 ctxt->state = oldstate;
9513 ctxt->state->seq = lastelem;
9514 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009515 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9516 ret = -1;
9517 goto done;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009518 }
9519
Daniel Veillard4c004142003-10-07 11:33:24 +00009520 done:
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009521 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009522 /*
9523 * builds the next links chain from the prev one
9524 */
9525 cur = lastchg;
9526 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009527 if ((cur == start) || (cur->prev == NULL))
9528 break;
9529 cur->prev->next = cur;
9530 cur = cur->prev;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009531 }
9532 if (ret == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009533 if (ctxt->errNr > errNr)
9534 xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009535 }
9536
9537 xmlFree(list);
9538 xmlFree(lasts);
Daniel Veillard4c004142003-10-07 11:33:24 +00009539 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009540}
9541
9542/**
9543 * xmlRelaxNGValidateDefinitionList:
9544 * @ctxt: a Relax-NG validation context
9545 * @define: the list of definition to verify
9546 *
9547 * Validate the given node content against the (list) of definitions
9548 *
9549 * Returns 0 if the validation succeeded or an error code.
9550 */
9551static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009552xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt,
9553 xmlRelaxNGDefinePtr defines)
9554{
Daniel Veillardfd573f12003-03-16 17:52:32 +00009555 int ret = 0, res;
9556
9557
Daniel Veillard952379b2003-03-17 15:37:12 +00009558 if (defines == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009559 VALID_ERR2(XML_RELAXNG_ERR_INTERNAL,
9560 BAD_CAST "NULL definition list");
9561 return (-1);
Daniel Veillard952379b2003-03-17 15:37:12 +00009562 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009563 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009564 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
9565 res = xmlRelaxNGValidateDefinition(ctxt, defines);
9566 if (res < 0)
9567 ret = -1;
9568 } else {
9569 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
9570 return (-1);
9571 }
9572 if (res == -1) /* continues on -2 */
9573 break;
9574 defines = defines->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009575 }
9576
Daniel Veillard4c004142003-10-07 11:33:24 +00009577 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009578}
9579
9580/**
9581 * xmlRelaxNGElementMatch:
Daniel Veillard416589a2003-02-17 17:25:42 +00009582 * @ctxt: a Relax-NG validation context
9583 * @define: the definition to check
Daniel Veillardfd573f12003-03-16 17:52:32 +00009584 * @elem: the element
Daniel Veillard416589a2003-02-17 17:25:42 +00009585 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00009586 * Check if the element matches the definition nameClass
Daniel Veillard416589a2003-02-17 17:25:42 +00009587 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00009588 * Returns 1 if the element matches, 0 if no, or -1 in case of error
Daniel Veillard416589a2003-02-17 17:25:42 +00009589 */
9590static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009591xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
9592 xmlRelaxNGDefinePtr define, xmlNodePtr elem)
9593{
Daniel Veillard580ced82003-03-21 21:22:48 +00009594 int ret = 0, oldflags = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00009595
Daniel Veillardfd573f12003-03-16 17:52:32 +00009596 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009597 if (!xmlStrEqual(elem->name, define->name)) {
9598 VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name);
9599 return (0);
9600 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00009601 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009602 if ((define->ns != NULL) && (define->ns[0] != 0)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009603 if (elem->ns == NULL) {
9604 VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, elem->name);
9605 return (0);
9606 } else if (!xmlStrEqual(elem->ns->href, define->ns)) {
9607 VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS,
9608 elem->name, define->ns);
9609 return (0);
9610 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009611 } else if ((elem->ns != NULL) && (define->ns != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00009612 (define->name == NULL)) {
9613 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, elem->name);
9614 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009615 } else if ((elem->ns != NULL) && (define->name != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009616 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, define->name);
9617 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009618 }
9619
9620 if (define->nameClass == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00009621 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009622
9623 define = define->nameClass;
9624 if (define->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009625 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009626
Daniel Veillard4c004142003-10-07 11:33:24 +00009627 if (ctxt != NULL) {
9628 oldflags = ctxt->flags;
9629 ctxt->flags |= FLAGS_IGNORABLE;
9630 }
9631
9632 list = define->content;
9633 while (list != NULL) {
9634 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9635 if (ret == 1) {
9636 if (ctxt != NULL)
9637 ctxt->flags = oldflags;
9638 return (0);
9639 }
9640 if (ret < 0) {
9641 if (ctxt != NULL)
9642 ctxt->flags = oldflags;
9643 return (ret);
9644 }
9645 list = list->next;
9646 }
9647 ret = 1;
9648 if (ctxt != NULL) {
9649 ctxt->flags = oldflags;
9650 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009651 } else if (define->type == XML_RELAXNG_CHOICE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009652 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009653
Daniel Veillard4c004142003-10-07 11:33:24 +00009654 if (ctxt != NULL) {
9655 oldflags = ctxt->flags;
9656 ctxt->flags |= FLAGS_IGNORABLE;
9657 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009658
Daniel Veillard4c004142003-10-07 11:33:24 +00009659 list = define->nameClass;
9660 while (list != NULL) {
9661 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9662 if (ret == 1) {
9663 if (ctxt != NULL)
9664 ctxt->flags = oldflags;
9665 return (1);
9666 }
9667 if (ret < 0) {
9668 if (ctxt != NULL)
9669 ctxt->flags = oldflags;
9670 return (ret);
9671 }
9672 list = list->next;
9673 }
9674 if (ctxt != NULL) {
9675 if (ret != 0) {
9676 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9677 xmlRelaxNGDumpValidError(ctxt);
9678 } else {
9679 if (ctxt->errNr > 0)
9680 xmlRelaxNGPopErrors(ctxt, 0);
9681 }
9682 }
9683 ret = 0;
9684 if (ctxt != NULL) {
9685 ctxt->flags = oldflags;
9686 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009687 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009688 TODO ret = -1;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009689 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009690 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009691}
9692
9693/**
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009694 * xmlRelaxNGBestState:
9695 * @ctxt: a Relax-NG validation context
9696 *
9697 * Find the "best" state in the ctxt->states list of states to report
9698 * errors about. I.e. a state with no element left in the child list
9699 * or the one with the less attributes left.
9700 * This is called only if a falidation error was detected
9701 *
9702 * Returns the index of the "best" state or -1 in case of error
9703 */
9704static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009705xmlRelaxNGBestState(xmlRelaxNGValidCtxtPtr ctxt)
9706{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009707 xmlRelaxNGValidStatePtr state;
9708 int i, tmp;
9709 int best = -1;
9710 int value = 1000000;
9711
9712 if ((ctxt == NULL) || (ctxt->states == NULL) ||
9713 (ctxt->states->nbState <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00009714 return (-1);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009715
Daniel Veillard4c004142003-10-07 11:33:24 +00009716 for (i = 0; i < ctxt->states->nbState; i++) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009717 state = ctxt->states->tabState[i];
Daniel Veillard4c004142003-10-07 11:33:24 +00009718 if (state == NULL)
9719 continue;
9720 if (state->seq != NULL) {
9721 if ((best == -1) || (value > 100000)) {
9722 value = 100000;
9723 best = i;
9724 }
9725 } else {
9726 tmp = state->nbAttrLeft;
9727 if ((best == -1) || (value > tmp)) {
9728 value = tmp;
9729 best = i;
9730 }
9731 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009732 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009733 return (best);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009734}
9735
9736/**
9737 * xmlRelaxNGLogBestError:
9738 * @ctxt: a Relax-NG validation context
9739 *
9740 * Find the "best" state in the ctxt->states list of states to report
9741 * errors about and log it.
9742 */
9743static void
Daniel Veillard4c004142003-10-07 11:33:24 +00009744xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt)
9745{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009746 int best;
9747
9748 if ((ctxt == NULL) || (ctxt->states == NULL) ||
9749 (ctxt->states->nbState <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00009750 return;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009751
9752 best = xmlRelaxNGBestState(ctxt);
9753 if ((best >= 0) && (best < ctxt->states->nbState)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009754 ctxt->state = ctxt->states->tabState[best];
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009755
Daniel Veillard4c004142003-10-07 11:33:24 +00009756 xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009757 }
9758}
9759
9760/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009761 * xmlRelaxNGValidateElementEnd:
9762 * @ctxt: a Relax-NG validation context
William M. Brack272693c2003-11-14 16:20:34 +00009763 * @dolog: indicate that error logging should be done
Daniel Veillardfd573f12003-03-16 17:52:32 +00009764 *
9765 * Validate the end of the element, implements check that
9766 * there is nothing left not consumed in the element content
9767 * or in the attribute list.
9768 *
9769 * Returns 0 if the validation succeeded or an error code.
9770 */
9771static int
William M. Brack272693c2003-11-14 16:20:34 +00009772xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int dolog)
Daniel Veillard4c004142003-10-07 11:33:24 +00009773{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009774 int i;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009775 xmlRelaxNGValidStatePtr state;
9776
9777 state = ctxt->state;
9778 if (state->seq != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009779 state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq);
9780 if (state->seq != NULL) {
William M. Brack272693c2003-11-14 16:20:34 +00009781 if (dolog) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009782 VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT,
9783 state->node->name, state->seq->name);
9784 }
9785 return (-1);
9786 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009787 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009788 for (i = 0; i < state->nbAttrs; i++) {
9789 if (state->attrs[i] != NULL) {
William M. Brack272693c2003-11-14 16:20:34 +00009790 if (dolog) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009791 VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR,
9792 state->attrs[i]->name, state->node->name);
9793 }
9794 return (-1 - i);
9795 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009796 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009797 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009798}
9799
9800/**
9801 * xmlRelaxNGValidateState:
9802 * @ctxt: a Relax-NG validation context
9803 * @define: the definition to verify
9804 *
9805 * Validate the current state against the definition
9806 *
9807 * Returns 0 if the validation succeeded or an error code.
9808 */
9809static int
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009810xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt,
9811 xmlRelaxNGDefinePtr define)
9812{
Daniel Veillardfd573f12003-03-16 17:52:32 +00009813 xmlNodePtr node;
9814 int ret = 0, i, tmp, oldflags, errNr;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009815 xmlRelaxNGValidStatePtr oldstate = NULL, state;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009816
9817 if (define == NULL) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009818 VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
9819 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009820 }
9821
9822 if (ctxt->state != NULL) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009823 node = ctxt->state->seq;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009824 } else {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009825 node = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009826 }
9827#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009828 for (i = 0; i < ctxt->depth; i++)
9829 xmlGenericError(xmlGenericErrorContext, " ");
Daniel Veillardfd573f12003-03-16 17:52:32 +00009830 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009831 "Start validating %s ", xmlRelaxNGDefName(define));
Daniel Veillardfd573f12003-03-16 17:52:32 +00009832 if (define->name != NULL)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009833 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009834 if ((node != NULL) && (node->name != NULL))
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009835 xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009836 else
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009837 xmlGenericError(xmlGenericErrorContext, "\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00009838#endif
9839 ctxt->depth++;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009840 switch (define->type) {
9841 case XML_RELAXNG_EMPTY:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009842 node = xmlRelaxNGSkipIgnored(ctxt, node);
9843 ret = 0;
9844 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009845 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009846 ret = -1;
9847 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009848 case XML_RELAXNG_TEXT:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009849 while ((node != NULL) &&
9850 ((node->type == XML_TEXT_NODE) ||
9851 (node->type == XML_COMMENT_NODE) ||
9852 (node->type == XML_PI_NODE) ||
9853 (node->type == XML_CDATA_SECTION_NODE)))
9854 node = node->next;
9855 ctxt->state->seq = node;
9856 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009857 case XML_RELAXNG_ELEMENT:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009858 errNr = ctxt->errNr;
9859 node = xmlRelaxNGSkipIgnored(ctxt, node);
9860 if (node == NULL) {
9861 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name);
9862 ret = -1;
9863 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9864 xmlRelaxNGDumpValidError(ctxt);
9865 break;
9866 }
9867 if (node->type != XML_ELEMENT_NODE) {
9868 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
9869 ret = -1;
9870 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9871 xmlRelaxNGDumpValidError(ctxt);
9872 break;
9873 }
9874 /*
9875 * This node was already validated successfully against
9876 * this definition.
9877 */
Daniel Veillard807daf82004-02-22 22:13:27 +00009878 if (node->psvi == define) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009879 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
9880 if (ctxt->errNr > errNr)
9881 xmlRelaxNGPopErrors(ctxt, errNr);
9882 if (ctxt->errNr != 0) {
9883 while ((ctxt->err != NULL) &&
9884 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME)
9885 && (xmlStrEqual(ctxt->err->arg2, node->name)))
9886 ||
9887 ((ctxt->err->err ==
9888 XML_RELAXNG_ERR_ELEMEXTRANS)
9889 && (xmlStrEqual(ctxt->err->arg1, node->name)))
9890 || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM)
9891 || (ctxt->err->err ==
9892 XML_RELAXNG_ERR_NOTELEM)))
9893 xmlRelaxNGValidErrorPop(ctxt);
9894 }
9895 break;
9896 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009897
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009898 ret = xmlRelaxNGElementMatch(ctxt, define, node);
9899 if (ret <= 0) {
9900 ret = -1;
9901 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9902 xmlRelaxNGDumpValidError(ctxt);
9903 break;
9904 }
9905 ret = 0;
9906 if (ctxt->errNr != 0) {
9907 if (ctxt->errNr > errNr)
9908 xmlRelaxNGPopErrors(ctxt, errNr);
9909 while ((ctxt->err != NULL) &&
9910 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) &&
9911 (xmlStrEqual(ctxt->err->arg2, node->name))) ||
9912 ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) &&
9913 (xmlStrEqual(ctxt->err->arg1, node->name))) ||
9914 (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) ||
9915 (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM)))
9916 xmlRelaxNGValidErrorPop(ctxt);
9917 }
9918 errNr = ctxt->errNr;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009919
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009920 oldflags = ctxt->flags;
9921 if (ctxt->flags & FLAGS_MIXED_CONTENT) {
9922 ctxt->flags -= FLAGS_MIXED_CONTENT;
9923 }
9924 state = xmlRelaxNGNewValidState(ctxt, node);
9925 if (state == NULL) {
9926 ret = -1;
9927 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9928 xmlRelaxNGDumpValidError(ctxt);
9929 break;
9930 }
Daniel Veillard7fe1f3a2003-03-31 22:13:33 +00009931
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009932 oldstate = ctxt->state;
9933 ctxt->state = state;
9934 if (define->attrs != NULL) {
9935 tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
9936 if (tmp != 0) {
9937 ret = -1;
9938 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
9939 }
9940 }
9941 if (define->contModel != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009942 xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state;
9943 xmlRelaxNGStatesPtr tmpstates = ctxt->states;
9944 xmlNodePtr nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009945
Daniel Veillard4c004142003-10-07 11:33:24 +00009946 nstate = xmlRelaxNGNewValidState(ctxt, node);
9947 ctxt->state = nstate;
9948 ctxt->states = NULL;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009949
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009950 tmp = xmlRelaxNGValidateCompiledContent(ctxt,
9951 define->contModel,
9952 ctxt->state->seq);
Daniel Veillard4c004142003-10-07 11:33:24 +00009953 nseq = ctxt->state->seq;
9954 ctxt->state = tmpstate;
9955 ctxt->states = tmpstates;
9956 xmlRelaxNGFreeValidState(ctxt, nstate);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009957
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009958#ifdef DEBUG_COMPILE
Daniel Veillard4c004142003-10-07 11:33:24 +00009959 xmlGenericError(xmlGenericErrorContext,
9960 "Validating content of '%s' : %d\n",
9961 define->name, tmp);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009962#endif
Daniel Veillardce192eb2003-04-16 15:58:05 +00009963 if (tmp != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00009964 ret = -1;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009965
9966 if (ctxt->states != NULL) {
9967 tmp = -1;
9968
Daniel Veillardce192eb2003-04-16 15:58:05 +00009969 for (i = 0; i < ctxt->states->nbState; i++) {
9970 state = ctxt->states->tabState[i];
9971 ctxt->state = state;
Daniel Veillard4c004142003-10-07 11:33:24 +00009972 ctxt->state->seq = nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009973
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009974 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
Daniel Veillardce192eb2003-04-16 15:58:05 +00009975 tmp = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00009976 break;
9977 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009978 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009979 if (tmp != 0) {
9980 /*
9981 * validation error, log the message for the "best" one
9982 */
9983 ctxt->flags |= FLAGS_IGNORABLE;
9984 xmlRelaxNGLogBestError(ctxt);
9985 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009986 for (i = 0; i < ctxt->states->nbState; i++) {
9987 xmlRelaxNGFreeValidState(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00009988 ctxt->states->
9989 tabState[i]);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009990 }
9991 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9992 ctxt->flags = oldflags;
9993 ctxt->states = NULL;
9994 if ((ret == 0) && (tmp == -1))
9995 ret = -1;
9996 } else {
9997 state = ctxt->state;
Daniel Veillardd8ed1052007-06-12 09:24:46 +00009998 if (ctxt->state != NULL)
9999 ctxt->state->seq = nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +000010000 if (ret == 0)
Daniel Veillard1ac24d32003-08-27 14:15:15 +000010001 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillardce192eb2003-04-16 15:58:05 +000010002 xmlRelaxNGFreeValidState(ctxt, state);
10003 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010004 } else {
10005 if (define->content != NULL) {
10006 tmp = xmlRelaxNGValidateDefinitionList(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +000010007 define->
10008 content);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010009 if (tmp != 0) {
10010 ret = -1;
10011 if (ctxt->state == NULL) {
10012 ctxt->state = oldstate;
10013 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
10014 node->name);
10015 ctxt->state = NULL;
10016 } else {
10017 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
10018 node->name);
10019 }
10020
10021 }
10022 }
10023 if (ctxt->states != NULL) {
10024 tmp = -1;
10025
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010026 for (i = 0; i < ctxt->states->nbState; i++) {
10027 state = ctxt->states->tabState[i];
10028 ctxt->state = state;
10029
Daniel Veillard1ac24d32003-08-27 14:15:15 +000010030 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010031 tmp = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +000010032 break;
10033 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +000010034 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010035 if (tmp != 0) {
10036 /*
10037 * validation error, log the message for the "best" one
10038 */
10039 ctxt->flags |= FLAGS_IGNORABLE;
10040 xmlRelaxNGLogBestError(ctxt);
10041 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +000010042 for (i = 0; i < ctxt->states->nbState; i++) {
10043 xmlRelaxNGFreeValidState(ctxt,
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010044 ctxt->states->tabState[i]);
10045 ctxt->states->tabState[i] = NULL;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010046 }
10047 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10048 ctxt->flags = oldflags;
10049 ctxt->states = NULL;
10050 if ((ret == 0) && (tmp == -1))
10051 ret = -1;
10052 } else {
10053 state = ctxt->state;
10054 if (ret == 0)
Daniel Veillard1ac24d32003-08-27 14:15:15 +000010055 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010056 xmlRelaxNGFreeValidState(ctxt, state);
10057 }
10058 }
10059 if (ret == 0) {
Daniel Veillard807daf82004-02-22 22:13:27 +000010060 node->psvi = define;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010061 }
10062 ctxt->flags = oldflags;
10063 ctxt->state = oldstate;
10064 if (oldstate != NULL)
10065 oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
10066 if (ret != 0) {
10067 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
10068 xmlRelaxNGDumpValidError(ctxt);
10069 ret = 0;
Daniel Veillardfa0d0942006-10-13 16:30:56 +000010070#if 0
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010071 } else {
10072 ret = -2;
Daniel Veillardfa0d0942006-10-13 16:30:56 +000010073#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010074 }
10075 } else {
10076 if (ctxt->errNr > errNr)
10077 xmlRelaxNGPopErrors(ctxt, errNr);
10078 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010079
10080#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010081 xmlGenericError(xmlGenericErrorContext,
10082 "xmlRelaxNGValidateDefinition(): validated %s : %d",
10083 node->name, ret);
10084 if (oldstate == NULL)
10085 xmlGenericError(xmlGenericErrorContext, ": no state\n");
10086 else if (oldstate->seq == NULL)
10087 xmlGenericError(xmlGenericErrorContext, ": done\n");
10088 else if (oldstate->seq->type == XML_ELEMENT_NODE)
10089 xmlGenericError(xmlGenericErrorContext, ": next elem %s\n",
10090 oldstate->seq->name);
10091 else
10092 xmlGenericError(xmlGenericErrorContext, ": next %s %d\n",
10093 oldstate->seq->name, oldstate->seq->type);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010094#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010095 break;
10096 case XML_RELAXNG_OPTIONAL:{
10097 errNr = ctxt->errNr;
10098 oldflags = ctxt->flags;
10099 ctxt->flags |= FLAGS_IGNORABLE;
10100 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
10101 ret =
10102 xmlRelaxNGValidateDefinitionList(ctxt,
10103 define->content);
10104 if (ret != 0) {
10105 if (ctxt->state != NULL)
10106 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10107 ctxt->state = oldstate;
10108 ctxt->flags = oldflags;
10109 ret = 0;
10110 if (ctxt->errNr > errNr)
10111 xmlRelaxNGPopErrors(ctxt, errNr);
10112 break;
10113 }
10114 if (ctxt->states != NULL) {
10115 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
10116 } else {
10117 ctxt->states = xmlRelaxNGNewStates(ctxt, 1);
10118 if (ctxt->states == NULL) {
10119 xmlRelaxNGFreeValidState(ctxt, oldstate);
10120 ctxt->flags = oldflags;
10121 ret = -1;
10122 if (ctxt->errNr > errNr)
10123 xmlRelaxNGPopErrors(ctxt, errNr);
10124 break;
10125 }
10126 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
10127 xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state);
10128 ctxt->state = NULL;
10129 }
10130 ctxt->flags = oldflags;
10131 ret = 0;
10132 if (ctxt->errNr > errNr)
10133 xmlRelaxNGPopErrors(ctxt, errNr);
10134 break;
10135 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010136 case XML_RELAXNG_ONEORMORE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010137 errNr = ctxt->errNr;
10138 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
10139 if (ret != 0) {
10140 break;
10141 }
10142 if (ctxt->errNr > errNr)
10143 xmlRelaxNGPopErrors(ctxt, errNr);
10144 /* no break on purpose */
10145 case XML_RELAXNG_ZEROORMORE:{
10146 int progress;
10147 xmlRelaxNGStatesPtr states = NULL, res = NULL;
10148 int base, j;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010149
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010150 errNr = ctxt->errNr;
10151 res = xmlRelaxNGNewStates(ctxt, 1);
10152 if (res == NULL) {
10153 ret = -1;
10154 break;
10155 }
10156 /*
10157 * All the input states are also exit states
10158 */
10159 if (ctxt->state != NULL) {
10160 xmlRelaxNGAddStates(ctxt, res,
10161 xmlRelaxNGCopyValidState(ctxt,
10162 ctxt->
10163 state));
10164 } else {
10165 for (j = 0; j < ctxt->states->nbState; j++) {
10166 xmlRelaxNGAddStates(ctxt, res,
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010167 xmlRelaxNGCopyValidState(ctxt,
10168 ctxt->states->tabState[j]));
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010169 }
10170 }
10171 oldflags = ctxt->flags;
10172 ctxt->flags |= FLAGS_IGNORABLE;
10173 do {
10174 progress = 0;
10175 base = res->nbState;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010176
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010177 if (ctxt->states != NULL) {
10178 states = ctxt->states;
10179 for (i = 0; i < states->nbState; i++) {
10180 ctxt->state = states->tabState[i];
10181 ctxt->states = NULL;
10182 ret = xmlRelaxNGValidateDefinitionList(ctxt,
10183 define->
10184 content);
10185 if (ret == 0) {
10186 if (ctxt->state != NULL) {
10187 tmp = xmlRelaxNGAddStates(ctxt, res,
10188 ctxt->state);
10189 ctxt->state = NULL;
10190 if (tmp == 1)
10191 progress = 1;
10192 } else if (ctxt->states != NULL) {
10193 for (j = 0; j < ctxt->states->nbState;
10194 j++) {
10195 tmp =
10196 xmlRelaxNGAddStates(ctxt, res,
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010197 ctxt->states->tabState[j]);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010198 if (tmp == 1)
10199 progress = 1;
10200 }
10201 xmlRelaxNGFreeStates(ctxt,
10202 ctxt->states);
10203 ctxt->states = NULL;
10204 }
10205 } else {
10206 if (ctxt->state != NULL) {
10207 xmlRelaxNGFreeValidState(ctxt,
10208 ctxt->state);
10209 ctxt->state = NULL;
10210 }
10211 }
10212 }
10213 } else {
10214 ret = xmlRelaxNGValidateDefinitionList(ctxt,
10215 define->
10216 content);
10217 if (ret != 0) {
10218 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10219 ctxt->state = NULL;
10220 } else {
10221 base = res->nbState;
10222 if (ctxt->state != NULL) {
10223 tmp = xmlRelaxNGAddStates(ctxt, res,
10224 ctxt->state);
10225 ctxt->state = NULL;
10226 if (tmp == 1)
10227 progress = 1;
10228 } else if (ctxt->states != NULL) {
10229 for (j = 0; j < ctxt->states->nbState; j++) {
10230 tmp = xmlRelaxNGAddStates(ctxt, res,
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010231 ctxt->states->tabState[j]);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010232 if (tmp == 1)
10233 progress = 1;
10234 }
10235 if (states == NULL) {
10236 states = ctxt->states;
10237 } else {
10238 xmlRelaxNGFreeStates(ctxt,
10239 ctxt->states);
10240 }
10241 ctxt->states = NULL;
10242 }
10243 }
10244 }
10245 if (progress) {
10246 /*
10247 * Collect all the new nodes added at that step
10248 * and make them the new node set
10249 */
10250 if (res->nbState - base == 1) {
10251 ctxt->state = xmlRelaxNGCopyValidState(ctxt,
10252 res->
10253 tabState
10254 [base]);
10255 } else {
10256 if (states == NULL) {
10257 xmlRelaxNGNewStates(ctxt,
10258 res->nbState - base);
Daniel Veillard14b56432006-03-09 18:41:40 +000010259 states = ctxt->states;
10260 if (states == NULL) {
10261 progress = 0;
10262 break;
10263 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010264 }
10265 states->nbState = 0;
10266 for (i = base; i < res->nbState; i++)
10267 xmlRelaxNGAddStates(ctxt, states,
10268 xmlRelaxNGCopyValidState
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010269 (ctxt, res->tabState[i]));
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010270 ctxt->states = states;
10271 }
10272 }
10273 } while (progress == 1);
10274 if (states != NULL) {
10275 xmlRelaxNGFreeStates(ctxt, states);
10276 }
10277 ctxt->states = res;
10278 ctxt->flags = oldflags;
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +000010279#if 0
10280 /*
Daniel Veillard4c004142003-10-07 11:33:24 +000010281 * errors may have to be propagated back...
10282 */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010283 if (ctxt->errNr > errNr)
10284 xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +000010285#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010286 ret = 0;
10287 break;
10288 }
10289 case XML_RELAXNG_CHOICE:{
10290 xmlRelaxNGDefinePtr list = NULL;
10291 xmlRelaxNGStatesPtr states = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010292
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010293 node = xmlRelaxNGSkipIgnored(ctxt, node);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010294
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010295 errNr = ctxt->errNr;
Daniel Veillard9186a1f2005-01-15 12:38:10 +000010296 if ((define->dflags & IS_TRIABLE) && (define->data != NULL) &&
10297 (node != NULL)) {
10298 /*
10299 * node == NULL can't be optimized since IS_TRIABLE
10300 * doesn't account for choice which may lead to
10301 * only attributes.
10302 */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010303 xmlHashTablePtr triage =
10304 (xmlHashTablePtr) define->data;
Daniel Veillarde063f482003-03-21 16:53:17 +000010305
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010306 /*
10307 * Something we can optimize cleanly there is only one
10308 * possble branch out !
10309 */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010310 if ((node->type == XML_TEXT_NODE) ||
10311 (node->type == XML_CDATA_SECTION_NODE)) {
10312 list =
10313 xmlHashLookup2(triage, BAD_CAST "#text", NULL);
10314 } else if (node->type == XML_ELEMENT_NODE) {
10315 if (node->ns != NULL) {
10316 list = xmlHashLookup2(triage, node->name,
10317 node->ns->href);
10318 if (list == NULL)
10319 list =
10320 xmlHashLookup2(triage, BAD_CAST "#any",
10321 node->ns->href);
10322 } else
10323 list =
10324 xmlHashLookup2(triage, node->name, NULL);
10325 if (list == NULL)
10326 list =
10327 xmlHashLookup2(triage, BAD_CAST "#any",
10328 NULL);
10329 }
10330 if (list == NULL) {
10331 ret = -1;
William M. Brack2f076062004-03-21 11:21:14 +000010332 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, node->name);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010333 break;
10334 }
10335 ret = xmlRelaxNGValidateDefinition(ctxt, list);
10336 if (ret == 0) {
10337 }
10338 break;
10339 }
Daniel Veillarde063f482003-03-21 16:53:17 +000010340
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010341 list = define->content;
10342 oldflags = ctxt->flags;
10343 ctxt->flags |= FLAGS_IGNORABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010344
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010345 while (list != NULL) {
10346 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
10347 ret = xmlRelaxNGValidateDefinition(ctxt, list);
10348 if (ret == 0) {
10349 if (states == NULL) {
10350 states = xmlRelaxNGNewStates(ctxt, 1);
10351 }
10352 if (ctxt->state != NULL) {
10353 xmlRelaxNGAddStates(ctxt, states, ctxt->state);
10354 } else if (ctxt->states != NULL) {
10355 for (i = 0; i < ctxt->states->nbState; i++) {
10356 xmlRelaxNGAddStates(ctxt, states,
10357 ctxt->states->
10358 tabState[i]);
10359 }
10360 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10361 ctxt->states = NULL;
10362 }
10363 } else {
10364 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10365 }
10366 ctxt->state = oldstate;
10367 list = list->next;
10368 }
10369 if (states != NULL) {
10370 xmlRelaxNGFreeValidState(ctxt, oldstate);
10371 ctxt->states = states;
10372 ctxt->state = NULL;
10373 ret = 0;
10374 } else {
10375 ctxt->states = NULL;
10376 }
10377 ctxt->flags = oldflags;
10378 if (ret != 0) {
10379 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
10380 xmlRelaxNGDumpValidError(ctxt);
10381 }
10382 } else {
10383 if (ctxt->errNr > errNr)
10384 xmlRelaxNGPopErrors(ctxt, errNr);
10385 }
10386 break;
10387 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010388 case XML_RELAXNG_DEF:
10389 case XML_RELAXNG_GROUP:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010390 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
10391 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010392 case XML_RELAXNG_INTERLEAVE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010393 ret = xmlRelaxNGValidateInterleave(ctxt, define);
10394 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010395 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010396 ret = xmlRelaxNGValidateAttribute(ctxt, define);
10397 break;
Daniel Veillardf4e55762003-04-15 23:32:22 +000010398 case XML_RELAXNG_START:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010399 case XML_RELAXNG_NOOP:
Daniel Veillardfd573f12003-03-16 17:52:32 +000010400 case XML_RELAXNG_REF:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010401 case XML_RELAXNG_EXTERNALREF:
Daniel Veillard952379b2003-03-17 15:37:12 +000010402 case XML_RELAXNG_PARENTREF:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010403 ret = xmlRelaxNGValidateDefinition(ctxt, define->content);
10404 break;
10405 case XML_RELAXNG_DATATYPE:{
10406 xmlNodePtr child;
10407 xmlChar *content = NULL;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010408
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010409 child = node;
10410 while (child != NULL) {
10411 if (child->type == XML_ELEMENT_NODE) {
10412 VALID_ERR2(XML_RELAXNG_ERR_DATAELEM,
10413 node->parent->name);
10414 ret = -1;
10415 break;
10416 } else if ((child->type == XML_TEXT_NODE) ||
10417 (child->type == XML_CDATA_SECTION_NODE)) {
10418 content = xmlStrcat(content, child->content);
10419 }
10420 /* TODO: handle entities ... */
10421 child = child->next;
10422 }
10423 if (ret == -1) {
10424 if (content != NULL)
10425 xmlFree(content);
10426 break;
10427 }
10428 if (content == NULL) {
10429 content = xmlStrdup(BAD_CAST "");
10430 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010431 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010432 ret = -1;
10433 break;
10434 }
10435 }
10436 ret = xmlRelaxNGValidateDatatype(ctxt, content, define,
10437 ctxt->state->seq);
10438 if (ret == -1) {
10439 VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name);
10440 } else if (ret == 0) {
10441 ctxt->state->seq = NULL;
10442 }
10443 if (content != NULL)
10444 xmlFree(content);
10445 break;
10446 }
10447 case XML_RELAXNG_VALUE:{
10448 xmlChar *content = NULL;
10449 xmlChar *oldvalue;
10450 xmlNodePtr child;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010451
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010452 child = node;
10453 while (child != NULL) {
10454 if (child->type == XML_ELEMENT_NODE) {
10455 VALID_ERR2(XML_RELAXNG_ERR_VALELEM,
10456 node->parent->name);
10457 ret = -1;
10458 break;
10459 } else if ((child->type == XML_TEXT_NODE) ||
10460 (child->type == XML_CDATA_SECTION_NODE)) {
10461 content = xmlStrcat(content, child->content);
10462 }
10463 /* TODO: handle entities ... */
10464 child = child->next;
10465 }
10466 if (ret == -1) {
10467 if (content != NULL)
10468 xmlFree(content);
10469 break;
10470 }
10471 if (content == NULL) {
10472 content = xmlStrdup(BAD_CAST "");
10473 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010474 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010475 ret = -1;
10476 break;
10477 }
10478 }
10479 oldvalue = ctxt->state->value;
10480 ctxt->state->value = content;
10481 ret = xmlRelaxNGValidateValue(ctxt, define);
10482 ctxt->state->value = oldvalue;
10483 if (ret == -1) {
10484 VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name);
10485 } else if (ret == 0) {
10486 ctxt->state->seq = NULL;
10487 }
10488 if (content != NULL)
10489 xmlFree(content);
10490 break;
10491 }
10492 case XML_RELAXNG_LIST:{
10493 xmlChar *content;
10494 xmlNodePtr child;
10495 xmlChar *oldvalue, *oldendvalue;
10496 int len;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010497
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010498 /*
10499 * Make sure it's only text nodes
10500 */
10501
10502 content = NULL;
10503 child = node;
10504 while (child != NULL) {
10505 if (child->type == XML_ELEMENT_NODE) {
10506 VALID_ERR2(XML_RELAXNG_ERR_LISTELEM,
10507 node->parent->name);
10508 ret = -1;
10509 break;
10510 } else if ((child->type == XML_TEXT_NODE) ||
10511 (child->type == XML_CDATA_SECTION_NODE)) {
10512 content = xmlStrcat(content, child->content);
10513 }
10514 /* TODO: handle entities ... */
10515 child = child->next;
10516 }
10517 if (ret == -1) {
10518 if (content != NULL)
10519 xmlFree(content);
10520 break;
10521 }
10522 if (content == NULL) {
10523 content = xmlStrdup(BAD_CAST "");
10524 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010525 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010526 ret = -1;
10527 break;
10528 }
10529 }
10530 len = xmlStrlen(content);
10531 oldvalue = ctxt->state->value;
10532 oldendvalue = ctxt->state->endvalue;
10533 ctxt->state->value = content;
10534 ctxt->state->endvalue = content + len;
10535 ret = xmlRelaxNGValidateValue(ctxt, define);
10536 ctxt->state->value = oldvalue;
10537 ctxt->state->endvalue = oldendvalue;
10538 if (ret == -1) {
10539 VALID_ERR(XML_RELAXNG_ERR_LIST);
10540 } else if ((ret == 0) && (node != NULL)) {
10541 ctxt->state->seq = node->next;
10542 }
10543 if (content != NULL)
10544 xmlFree(content);
10545 break;
10546 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010547 case XML_RELAXNG_EXCEPT:
10548 case XML_RELAXNG_PARAM:
10549 TODO ret = -1;
10550 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010551 }
10552 ctxt->depth--;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010553#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010554 for (i = 0; i < ctxt->depth; i++)
10555 xmlGenericError(xmlGenericErrorContext, " ");
Daniel Veillardfd573f12003-03-16 17:52:32 +000010556 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010557 "Validating %s ", xmlRelaxNGDefName(define));
Daniel Veillardfd573f12003-03-16 17:52:32 +000010558 if (define->name != NULL)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010559 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010560 if (ret == 0)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010561 xmlGenericError(xmlGenericErrorContext, "suceeded\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +000010562 else
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010563 xmlGenericError(xmlGenericErrorContext, "failed\n");
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010564#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010565 return (ret);
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010566}
10567
10568/**
Daniel Veillardfd573f12003-03-16 17:52:32 +000010569 * xmlRelaxNGValidateDefinition:
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010570 * @ctxt: a Relax-NG validation context
10571 * @define: the definition to verify
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010572 *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010573 * Validate the current node lists against the definition
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010574 *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010575 * Returns 0 if the validation succeeded or an error code.
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010576 */
10577static int
Daniel Veillard4c004142003-10-07 11:33:24 +000010578xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
10579 xmlRelaxNGDefinePtr define)
10580{
Daniel Veillardfd573f12003-03-16 17:52:32 +000010581 xmlRelaxNGStatesPtr states, res;
10582 int i, j, k, ret, oldflags;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010583
Daniel Veillardfd573f12003-03-16 17:52:32 +000010584 /*
10585 * We should NOT have both ctxt->state and ctxt->states
10586 */
10587 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010588 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10589 ctxt->state = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010590 }
10591
10592 if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010593 if (ctxt->states != NULL) {
10594 ctxt->state = ctxt->states->tabState[0];
10595 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10596 ctxt->states = NULL;
10597 }
10598 ret = xmlRelaxNGValidateState(ctxt, define);
10599 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10600 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10601 ctxt->state = NULL;
10602 }
10603 if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) {
10604 ctxt->state = ctxt->states->tabState[0];
10605 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10606 ctxt->states = NULL;
10607 }
10608 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010609 }
10610
10611 states = ctxt->states;
10612 ctxt->states = NULL;
10613 res = NULL;
10614 j = 0;
10615 oldflags = ctxt->flags;
10616 ctxt->flags |= FLAGS_IGNORABLE;
Daniel Veillard4c004142003-10-07 11:33:24 +000010617 for (i = 0; i < states->nbState; i++) {
10618 ctxt->state = states->tabState[i];
10619 ctxt->states = NULL;
10620 ret = xmlRelaxNGValidateState(ctxt, define);
10621 /*
10622 * We should NOT have both ctxt->state and ctxt->states
10623 */
10624 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10625 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10626 ctxt->state = NULL;
10627 }
10628 if (ret == 0) {
10629 if (ctxt->states == NULL) {
10630 if (res != NULL) {
10631 /* add the state to the container */
10632 xmlRelaxNGAddStates(ctxt, res, ctxt->state);
10633 ctxt->state = NULL;
10634 } else {
10635 /* add the state directly in states */
10636 states->tabState[j++] = ctxt->state;
10637 ctxt->state = NULL;
10638 }
10639 } else {
10640 if (res == NULL) {
10641 /* make it the new container and copy other results */
10642 res = ctxt->states;
10643 ctxt->states = NULL;
10644 for (k = 0; k < j; k++)
10645 xmlRelaxNGAddStates(ctxt, res,
10646 states->tabState[k]);
10647 } else {
10648 /* add all the new results to res and reff the container */
10649 for (k = 0; k < ctxt->states->nbState; k++)
10650 xmlRelaxNGAddStates(ctxt, res,
10651 ctxt->states->tabState[k]);
10652 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10653 ctxt->states = NULL;
10654 }
10655 }
10656 } else {
10657 if (ctxt->state != NULL) {
10658 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10659 ctxt->state = NULL;
10660 } else if (ctxt->states != NULL) {
10661 for (k = 0; k < ctxt->states->nbState; k++)
10662 xmlRelaxNGFreeValidState(ctxt,
10663 ctxt->states->tabState[k]);
10664 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10665 ctxt->states = NULL;
10666 }
10667 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010668 }
10669 ctxt->flags = oldflags;
10670 if (res != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010671 xmlRelaxNGFreeStates(ctxt, states);
10672 ctxt->states = res;
10673 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010674 } else if (j > 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010675 states->nbState = j;
10676 ctxt->states = states;
10677 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010678 } else if (j == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010679 ctxt->state = states->tabState[0];
10680 xmlRelaxNGFreeStates(ctxt, states);
10681 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010682 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +000010683 ret = -1;
10684 xmlRelaxNGFreeStates(ctxt, states);
10685 if (ctxt->states != NULL) {
10686 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10687 ctxt->states = NULL;
10688 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010689 }
10690 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010691 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10692 ctxt->state = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010693 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010694 return (ret);
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010695}
10696
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010697/**
Daniel Veillard6eadf632003-01-23 18:29:16 +000010698 * xmlRelaxNGValidateDocument:
10699 * @ctxt: a Relax-NG validation context
10700 * @doc: the document
10701 *
10702 * Validate the given document
10703 *
10704 * Returns 0 if the validation succeeded or an error code.
10705 */
10706static int
Daniel Veillard4c004142003-10-07 11:33:24 +000010707xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
10708{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010709 int ret;
10710 xmlRelaxNGPtr schema;
10711 xmlRelaxNGGrammarPtr grammar;
10712 xmlRelaxNGValidStatePtr state;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010713 xmlNodePtr node;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010714
10715 if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +000010716 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010717
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010718 ctxt->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010719 schema = ctxt->schema;
10720 grammar = schema->topgrammar;
10721 if (grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010722 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
10723 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010724 }
10725 state = xmlRelaxNGNewValidState(ctxt, NULL);
10726 ctxt->state = state;
10727 ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010728 if ((ctxt->state != NULL) && (state->seq != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010729 state = ctxt->state;
10730 node = state->seq;
10731 node = xmlRelaxNGSkipIgnored(ctxt, node);
10732 if (node != NULL) {
10733 if (ret != -1) {
10734 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10735 ret = -1;
10736 }
10737 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010738 } else if (ctxt->states != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010739 int i;
10740 int tmp = -1;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010741
Daniel Veillard4c004142003-10-07 11:33:24 +000010742 for (i = 0; i < ctxt->states->nbState; i++) {
10743 state = ctxt->states->tabState[i];
10744 node = state->seq;
10745 node = xmlRelaxNGSkipIgnored(ctxt, node);
10746 if (node == NULL)
10747 tmp = 0;
10748 xmlRelaxNGFreeValidState(ctxt, state);
10749 }
10750 if (tmp == -1) {
10751 if (ret != -1) {
10752 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10753 ret = -1;
10754 }
10755 }
Daniel Veillard6eadf632003-01-23 18:29:16 +000010756 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +000010757 if (ctxt->state != NULL) {
10758 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillard4c004142003-10-07 11:33:24 +000010759 ctxt->state = NULL;
Daniel Veillardbbb78b52003-03-21 01:24:45 +000010760 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010761 if (ret != 0)
10762 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillard580ced82003-03-21 21:22:48 +000010763#ifdef DEBUG
10764 else if (ctxt->errNr != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010765 ctxt->error(ctxt->userData,
10766 "%d Extra error messages left on stack !\n",
10767 ctxt->errNr);
10768 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillard580ced82003-03-21 21:22:48 +000010769 }
10770#endif
Daniel Veillardf54cd532004-02-25 11:52:31 +000010771#ifdef LIBXML_VALID_ENABLED
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010772 if (ctxt->idref == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010773 xmlValidCtxt vctxt;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010774
Daniel Veillard4c004142003-10-07 11:33:24 +000010775 memset(&vctxt, 0, sizeof(xmlValidCtxt));
10776 vctxt.valid = 1;
10777 vctxt.error = ctxt->error;
10778 vctxt.warning = ctxt->warning;
10779 vctxt.userData = ctxt->userData;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010780
Daniel Veillard4c004142003-10-07 11:33:24 +000010781 if (xmlValidateDocumentFinal(&vctxt, doc) != 1)
10782 ret = -1;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010783 }
Daniel Veillardf54cd532004-02-25 11:52:31 +000010784#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010785 if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK))
Daniel Veillard4c004142003-10-07 11:33:24 +000010786 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010787
Daniel Veillard4c004142003-10-07 11:33:24 +000010788 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010789}
10790
Daniel Veillarda4f27cb2009-08-21 17:34:17 +020010791/**
10792 * xmlRelaxNGCleanPSVI:
10793 * @node: an input element or document
10794 *
10795 * Call this routine to speed up XPath computation on static documents.
10796 * This stamps all the element nodes with the document order
10797 * Like for line information, the order is kept in the element->content
10798 * field, the value stored is actually - the node number (starting at -1)
10799 * to be able to differentiate from line numbers.
10800 *
10801 * Returns the number of elements found in the document or -1 in case
10802 * of error.
10803 */
10804static void
10805xmlRelaxNGCleanPSVI(xmlNodePtr node) {
10806 xmlNodePtr cur;
10807
10808 if ((node == NULL) ||
10809 ((node->type != XML_ELEMENT_NODE) &&
10810 (node->type != XML_DOCUMENT_NODE) &&
10811 (node->type != XML_HTML_DOCUMENT_NODE)))
10812 return;
10813 if (node->type == XML_ELEMENT_NODE)
10814 node->psvi = NULL;
10815
10816 cur = node->children;
10817 while (cur != NULL) {
10818 if (cur->type == XML_ELEMENT_NODE) {
10819 cur->psvi = NULL;
10820 if (cur->children != NULL) {
10821 cur = cur->children;
10822 continue;
10823 }
10824 }
10825 if (cur->next != NULL) {
10826 cur = cur->next;
10827 continue;
10828 }
10829 do {
10830 cur = cur->parent;
10831 if (cur == NULL)
10832 break;
10833 if (cur == node) {
10834 cur = NULL;
10835 break;
10836 }
10837 if (cur->next != NULL) {
10838 cur = cur->next;
10839 break;
10840 }
10841 } while (cur != NULL);
10842 }
10843 return;
10844}
Daniel Veillardfd573f12003-03-16 17:52:32 +000010845/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +080010846 * *
10847 * Validation interfaces *
10848 * *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010849 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +000010850
Daniel Veillard6eadf632003-01-23 18:29:16 +000010851/**
10852 * xmlRelaxNGNewValidCtxt:
10853 * @schema: a precompiled XML RelaxNGs
10854 *
10855 * Create an XML RelaxNGs validation context based on the given schema
10856 *
10857 * Returns the validation context or NULL in case of error
10858 */
10859xmlRelaxNGValidCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +000010860xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema)
10861{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010862 xmlRelaxNGValidCtxtPtr ret;
10863
10864 ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt));
10865 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010866 xmlRngVErrMemory(NULL, "building context\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +000010867 return (NULL);
10868 }
10869 memset(ret, 0, sizeof(xmlRelaxNGValidCtxt));
10870 ret->schema = schema;
Daniel Veillard1703c5f2003-02-10 14:28:44 +000010871 ret->error = xmlGenericError;
10872 ret->userData = xmlGenericErrorContext;
Daniel Veillard42f12e92003-03-07 18:32:59 +000010873 ret->errNr = 0;
10874 ret->errMax = 0;
10875 ret->err = NULL;
10876 ret->errTab = NULL;
Daniel Veillardb30ca312005-09-04 13:50:03 +000010877 if (schema != NULL)
10878 ret->idref = schema->idref;
Daniel Veillard798024a2003-03-19 10:36:09 +000010879 ret->states = NULL;
10880 ret->freeState = NULL;
10881 ret->freeStates = NULL;
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010882 ret->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010883 return (ret);
10884}
10885
10886/**
10887 * xmlRelaxNGFreeValidCtxt:
10888 * @ctxt: the schema validation context
10889 *
10890 * Free the resources associated to the schema validation context
10891 */
10892void
Daniel Veillard4c004142003-10-07 11:33:24 +000010893xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt)
10894{
Daniel Veillard798024a2003-03-19 10:36:09 +000010895 int k;
10896
Daniel Veillard6eadf632003-01-23 18:29:16 +000010897 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010898 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010899 if (ctxt->states != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010900 xmlRelaxNGFreeStates(NULL, ctxt->states);
Daniel Veillard798024a2003-03-19 10:36:09 +000010901 if (ctxt->freeState != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010902 for (k = 0; k < ctxt->freeState->nbState; k++) {
10903 xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]);
10904 }
10905 xmlRelaxNGFreeStates(NULL, ctxt->freeState);
Daniel Veillard798024a2003-03-19 10:36:09 +000010906 }
Daniel Veillard798024a2003-03-19 10:36:09 +000010907 if (ctxt->freeStates != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010908 for (k = 0; k < ctxt->freeStatesNr; k++) {
10909 xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]);
10910 }
10911 xmlFree(ctxt->freeStates);
Daniel Veillard798024a2003-03-19 10:36:09 +000010912 }
Daniel Veillard42f12e92003-03-07 18:32:59 +000010913 if (ctxt->errTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010914 xmlFree(ctxt->errTab);
Daniel Veillardf4e55762003-04-15 23:32:22 +000010915 if (ctxt->elemTab != NULL) {
10916 xmlRegExecCtxtPtr exec;
10917
Daniel Veillard4c004142003-10-07 11:33:24 +000010918 exec = xmlRelaxNGElemPop(ctxt);
10919 while (exec != NULL) {
10920 xmlRegFreeExecCtxt(exec);
10921 exec = xmlRelaxNGElemPop(ctxt);
10922 }
10923 xmlFree(ctxt->elemTab);
Daniel Veillardf4e55762003-04-15 23:32:22 +000010924 }
Daniel Veillard6eadf632003-01-23 18:29:16 +000010925 xmlFree(ctxt);
10926}
10927
10928/**
10929 * xmlRelaxNGSetValidErrors:
10930 * @ctxt: a Relax-NG validation context
10931 * @err: the error function
10932 * @warn: the warning function
10933 * @ctx: the functions context
10934 *
10935 * Set the error and warning callback informations
10936 */
10937void
10938xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +000010939 xmlRelaxNGValidityErrorFunc err,
10940 xmlRelaxNGValidityWarningFunc warn, void *ctx)
10941{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010942 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010943 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010944 ctxt->error = err;
10945 ctxt->warning = warn;
10946 ctxt->userData = ctx;
Daniel Veillardb30ca312005-09-04 13:50:03 +000010947 ctxt->serror = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010948}
10949
10950/**
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000010951 * xmlRelaxNGSetValidStructuredErrors:
10952 * @ctxt: a Relax-NG validation context
10953 * @serror: the structured error function
10954 * @ctx: the functions context
10955 *
10956 * Set the structured error callback
10957 */
10958void
10959xmlRelaxNGSetValidStructuredErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillardb30ca312005-09-04 13:50:03 +000010960 xmlStructuredErrorFunc serror, void *ctx)
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000010961{
10962 if (ctxt == NULL)
10963 return;
Daniel Veillardb30ca312005-09-04 13:50:03 +000010964 ctxt->serror = serror;
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000010965 ctxt->error = NULL;
10966 ctxt->warning = NULL;
10967 ctxt->userData = ctx;
10968}
10969
10970/**
Daniel Veillard409a8142003-07-18 15:16:57 +000010971 * xmlRelaxNGGetValidErrors:
10972 * @ctxt: a Relax-NG validation context
10973 * @err: the error function result
10974 * @warn: the warning function result
10975 * @ctx: the functions context result
10976 *
10977 * Get the error and warning callback informations
10978 *
10979 * Returns -1 in case of error and 0 otherwise
10980 */
10981int
10982xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +000010983 xmlRelaxNGValidityErrorFunc * err,
10984 xmlRelaxNGValidityWarningFunc * warn, void **ctx)
10985{
Daniel Veillard409a8142003-07-18 15:16:57 +000010986 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010987 return (-1);
10988 if (err != NULL)
10989 *err = ctxt->error;
10990 if (warn != NULL)
10991 *warn = ctxt->warning;
10992 if (ctx != NULL)
10993 *ctx = ctxt->userData;
10994 return (0);
Daniel Veillard409a8142003-07-18 15:16:57 +000010995}
10996
10997/**
Daniel Veillard6eadf632003-01-23 18:29:16 +000010998 * xmlRelaxNGValidateDoc:
10999 * @ctxt: a Relax-NG validation context
11000 * @doc: a parsed document tree
11001 *
11002 * Validate a document tree in memory.
11003 *
11004 * Returns 0 if the document is valid, a positive error code
11005 * number otherwise and -1 in case of internal or API error.
11006 */
11007int
Daniel Veillard4c004142003-10-07 11:33:24 +000011008xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
11009{
Daniel Veillard6eadf632003-01-23 18:29:16 +000011010 int ret;
11011
11012 if ((ctxt == NULL) || (doc == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +000011013 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000011014
11015 ctxt->doc = doc;
11016
11017 ret = xmlRelaxNGValidateDocument(ctxt, doc);
Daniel Veillard71531f32003-02-05 13:19:53 +000011018 /*
Daniel Veillarda4f27cb2009-08-21 17:34:17 +020011019 * Remove all left PSVI
11020 */
11021 xmlRelaxNGCleanPSVI((xmlNodePtr) doc);
11022
11023 /*
Daniel Veillard71531f32003-02-05 13:19:53 +000011024 * TODO: build error codes
11025 */
11026 if (ret == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +000011027 return (1);
11028 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +000011029}
11030
Daniel Veillard5d4644e2005-04-01 13:11:58 +000011031#define bottom_relaxng
11032#include "elfgcchack.h"
Daniel Veillard6eadf632003-01-23 18:29:16 +000011033#endif /* LIBXML_SCHEMAS_ENABLED */