blob: cbc9073fb928b15f03aecc5c3ff0f5b287c36e7b [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
42#define IS_RELAXNG(node, type) \
43 ((node != NULL) && (node->ns != NULL) && \
44 (xmlStrEqual(node->name, (const xmlChar *) type)) && \
45 (xmlStrEqual(node->ns->href, xmlRelaxNGNs)))
46
47
Daniel Veillard952379b2003-03-17 15:37:12 +000048/* #define DEBUG 1 */
Daniel Veillard4c004142003-10-07 11:33:24 +000049
Daniel Veillardc482e262003-02-26 14:48:48 +000050/* #define DEBUG_GRAMMAR 1 */
Daniel Veillard4c004142003-10-07 11:33:24 +000051
Daniel Veillard71531f32003-02-05 13:19:53 +000052/* #define DEBUG_CONTENT 1 */
Daniel Veillard4c004142003-10-07 11:33:24 +000053
Daniel Veillard71531f32003-02-05 13:19:53 +000054/* #define DEBUG_TYPE 1 */
Daniel Veillard4c004142003-10-07 11:33:24 +000055
Daniel Veillard71531f32003-02-05 13:19:53 +000056/* #define DEBUG_VALID 1 */
Daniel Veillard4c004142003-10-07 11:33:24 +000057
Daniel Veillarde5b110b2003-02-04 14:43:39 +000058/* #define DEBUG_INTERLEAVE 1 */
Daniel Veillard4c004142003-10-07 11:33:24 +000059
Daniel Veillard416589a2003-02-17 17:25:42 +000060/* #define DEBUG_LIST 1 */
Daniel Veillard4c004142003-10-07 11:33:24 +000061
Daniel Veillard5add8682003-03-10 13:13:58 +000062/* #define DEBUG_INCLUDE */
Daniel Veillard4c004142003-10-07 11:33:24 +000063
Daniel Veillarda507fbf2003-03-31 16:09:37 +000064/* #define DEBUG_ERROR 1 */
Daniel Veillard4c004142003-10-07 11:33:24 +000065
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000066/* #define DEBUG_COMPILE 1 */
Daniel Veillard4c004142003-10-07 11:33:24 +000067
Daniel Veillardf4e55762003-04-15 23:32:22 +000068/* #define DEBUG_PROGRESSIVE 1 */
Daniel Veillard6eadf632003-01-23 18:29:16 +000069
Daniel Veillard5f1946a2003-03-31 16:38:16 +000070#define MAX_ERROR 5
71
Daniel Veillard6eadf632003-01-23 18:29:16 +000072#define TODO \
73 xmlGenericError(xmlGenericErrorContext, \
74 "Unimplemented block at %s:%d\n", \
75 __FILE__, __LINE__);
76
77typedef struct _xmlRelaxNGSchema xmlRelaxNGSchema;
78typedef xmlRelaxNGSchema *xmlRelaxNGSchemaPtr;
79
80typedef struct _xmlRelaxNGDefine xmlRelaxNGDefine;
81typedef xmlRelaxNGDefine *xmlRelaxNGDefinePtr;
82
Daniel Veillardd41f4f42003-01-29 21:07:52 +000083typedef struct _xmlRelaxNGDocument xmlRelaxNGDocument;
84typedef xmlRelaxNGDocument *xmlRelaxNGDocumentPtr;
85
Daniel Veillarda9d912d2003-02-01 17:43:10 +000086typedef struct _xmlRelaxNGInclude xmlRelaxNGInclude;
87typedef xmlRelaxNGInclude *xmlRelaxNGIncludePtr;
88
Daniel Veillard6eadf632003-01-23 18:29:16 +000089typedef enum {
Daniel Veillard4c004142003-10-07 11:33:24 +000090 XML_RELAXNG_COMBINE_UNDEFINED = 0, /* undefined */
91 XML_RELAXNG_COMBINE_CHOICE, /* choice */
92 XML_RELAXNG_COMBINE_INTERLEAVE /* interleave */
Daniel Veillard6eadf632003-01-23 18:29:16 +000093} xmlRelaxNGCombine;
94
Daniel Veillard4c5cf702003-02-21 15:40:34 +000095typedef enum {
96 XML_RELAXNG_CONTENT_ERROR = -1,
97 XML_RELAXNG_CONTENT_EMPTY = 0,
98 XML_RELAXNG_CONTENT_SIMPLE,
99 XML_RELAXNG_CONTENT_COMPLEX
100} xmlRelaxNGContentType;
101
Daniel Veillard6eadf632003-01-23 18:29:16 +0000102typedef struct _xmlRelaxNGGrammar xmlRelaxNGGrammar;
103typedef xmlRelaxNGGrammar *xmlRelaxNGGrammarPtr;
104
105struct _xmlRelaxNGGrammar {
Daniel Veillard4c004142003-10-07 11:33:24 +0000106 xmlRelaxNGGrammarPtr parent; /* the parent grammar if any */
107 xmlRelaxNGGrammarPtr children; /* the children grammar if any */
108 xmlRelaxNGGrammarPtr next; /* the next grammar if any */
109 xmlRelaxNGDefinePtr start; /* <start> content */
110 xmlRelaxNGCombine combine; /* the default combine value */
111 xmlRelaxNGDefinePtr startList; /* list of <start> definitions */
112 xmlHashTablePtr defs; /* define* */
113 xmlHashTablePtr refs; /* references */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000114};
115
116
Daniel Veillard6eadf632003-01-23 18:29:16 +0000117typedef enum {
Daniel Veillard4c004142003-10-07 11:33:24 +0000118 XML_RELAXNG_NOOP = -1, /* a no operation from simplification */
119 XML_RELAXNG_EMPTY = 0, /* an empty pattern */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000120 XML_RELAXNG_NOT_ALLOWED, /* not allowed top */
Daniel Veillard4c004142003-10-07 11:33:24 +0000121 XML_RELAXNG_EXCEPT, /* except present in nameclass defs */
122 XML_RELAXNG_TEXT, /* textual content */
123 XML_RELAXNG_ELEMENT, /* an element */
124 XML_RELAXNG_DATATYPE, /* extenal data type definition */
125 XML_RELAXNG_PARAM, /* extenal data type parameter */
126 XML_RELAXNG_VALUE, /* value from an extenal data type definition */
127 XML_RELAXNG_LIST, /* a list of patterns */
128 XML_RELAXNG_ATTRIBUTE, /* an attrbute following a pattern */
129 XML_RELAXNG_DEF, /* a definition */
130 XML_RELAXNG_REF, /* reference to a definition */
131 XML_RELAXNG_EXTERNALREF, /* reference to an external def */
132 XML_RELAXNG_PARENTREF, /* reference to a def in the parent grammar */
133 XML_RELAXNG_OPTIONAL, /* optional patterns */
134 XML_RELAXNG_ZEROORMORE, /* zero or more non empty patterns */
135 XML_RELAXNG_ONEORMORE, /* one or more non empty patterns */
136 XML_RELAXNG_CHOICE, /* a choice between non empty patterns */
137 XML_RELAXNG_GROUP, /* a pair/group of non empty patterns */
138 XML_RELAXNG_INTERLEAVE, /* interleaving choice of non-empty patterns */
139 XML_RELAXNG_START /* Used to keep track of starts on grammars */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000140} xmlRelaxNGType;
141
Daniel Veillard52b48c72003-04-13 19:53:42 +0000142#define IS_NULLABLE (1 << 0)
143#define IS_NOT_NULLABLE (1 << 1)
144#define IS_INDETERMINIST (1 << 2)
145#define IS_MIXED (1 << 3)
146#define IS_TRIABLE (1 << 4)
147#define IS_PROCESSED (1 << 5)
148#define IS_COMPILABLE (1 << 6)
149#define IS_NOT_COMPILABLE (1 << 7)
Daniel Veillard1564e6e2003-03-15 21:30:25 +0000150
Daniel Veillard6eadf632003-01-23 18:29:16 +0000151struct _xmlRelaxNGDefine {
Daniel Veillard4c004142003-10-07 11:33:24 +0000152 xmlRelaxNGType type; /* the type of definition */
153 xmlNodePtr node; /* the node in the source */
154 xmlChar *name; /* the element local name if present */
155 xmlChar *ns; /* the namespace local name if present */
156 xmlChar *value; /* value when available */
157 void *data; /* data lib or specific pointer */
158 xmlRelaxNGDefinePtr content; /* the expected content */
159 xmlRelaxNGDefinePtr parent; /* the parent definition, if any */
160 xmlRelaxNGDefinePtr next; /* list within grouping sequences */
161 xmlRelaxNGDefinePtr attrs; /* list of attributes for elements */
162 xmlRelaxNGDefinePtr nameClass; /* the nameClass definition if any */
163 xmlRelaxNGDefinePtr nextHash; /* next define in defs/refs hash tables */
164 short depth; /* used for the cycle detection */
165 short dflags; /* define related flags */
166 xmlRegexpPtr contModel; /* a compiled content model if available */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000167};
168
169/**
170 * _xmlRelaxNG:
171 *
172 * A RelaxNGs definition
173 */
174struct _xmlRelaxNG {
Daniel Veillard4c004142003-10-07 11:33:24 +0000175 void *_private; /* unused by the library for users or bindings */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000176 xmlRelaxNGGrammarPtr topgrammar;
177 xmlDocPtr doc;
178
Daniel Veillard4c004142003-10-07 11:33:24 +0000179 int idref; /* requires idref checking */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000180
Daniel Veillard4c004142003-10-07 11:33:24 +0000181 xmlHashTablePtr defs; /* define */
182 xmlHashTablePtr refs; /* references */
183 xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
184 xmlRelaxNGIncludePtr includes; /* all the includes loaded */
185 int defNr; /* number of defines used */
186 xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000187
Daniel Veillard6eadf632003-01-23 18:29:16 +0000188};
189
Daniel Veillard77648bb2003-02-20 15:03:22 +0000190#define XML_RELAXNG_IN_ATTRIBUTE (1 << 0)
191#define XML_RELAXNG_IN_ONEORMORE (1 << 1)
192#define XML_RELAXNG_IN_LIST (1 << 2)
193#define XML_RELAXNG_IN_DATAEXCEPT (1 << 3)
194#define XML_RELAXNG_IN_START (1 << 4)
195#define XML_RELAXNG_IN_OOMGROUP (1 << 5)
196#define XML_RELAXNG_IN_OOMINTERLEAVE (1 << 6)
197#define XML_RELAXNG_IN_EXTERNALREF (1 << 7)
Daniel Veillardc5312d72003-02-21 17:14:10 +0000198#define XML_RELAXNG_IN_ANYEXCEPT (1 << 8)
199#define XML_RELAXNG_IN_NSEXCEPT (1 << 9)
Daniel Veillard6eadf632003-01-23 18:29:16 +0000200
201struct _xmlRelaxNGParserCtxt {
Daniel Veillard4c004142003-10-07 11:33:24 +0000202 void *userData; /* user specific data block */
203 xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
204 xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */
Daniel Veillard659e71e2003-10-10 14:10:40 +0000205 xmlStructuredErrorFunc serror;
Daniel Veillard42f12e92003-03-07 18:32:59 +0000206 xmlRelaxNGValidErr err;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000207
Daniel Veillard4c004142003-10-07 11:33:24 +0000208 xmlRelaxNGPtr schema; /* The schema in use */
209 xmlRelaxNGGrammarPtr grammar; /* the current grammar */
210 xmlRelaxNGGrammarPtr parentgrammar; /* the parent grammar */
211 int flags; /* parser flags */
212 int nbErrors; /* number of errors at parse time */
213 int nbWarnings; /* number of warnings at parse time */
214 const xmlChar *define; /* the current define scope */
215 xmlRelaxNGDefinePtr def; /* the current define */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000216
Daniel Veillard4c004142003-10-07 11:33:24 +0000217 int nbInterleaves;
218 xmlHashTablePtr interleaves; /* keep track of all the interleaves */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000219
Daniel Veillard4c004142003-10-07 11:33:24 +0000220 xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
221 xmlRelaxNGIncludePtr includes; /* all the includes loaded */
222 xmlChar *URL;
223 xmlDocPtr document;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000224
Daniel Veillard4c004142003-10-07 11:33:24 +0000225 int defNr; /* number of defines used */
226 int defMax; /* number of defines aloocated */
227 xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */
Daniel Veillard419a7682003-02-03 23:22:49 +0000228
Daniel Veillard4c004142003-10-07 11:33:24 +0000229 const char *buffer;
230 int size;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000231
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000232 /* the document stack */
Daniel Veillard4c004142003-10-07 11:33:24 +0000233 xmlRelaxNGDocumentPtr doc; /* Current parsed external ref */
234 int docNr; /* Depth of the parsing stack */
235 int docMax; /* Max depth of the parsing stack */
236 xmlRelaxNGDocumentPtr *docTab; /* array of docs */
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000237
238 /* the include stack */
Daniel Veillard4c004142003-10-07 11:33:24 +0000239 xmlRelaxNGIncludePtr inc; /* Current parsed include */
240 int incNr; /* Depth of the include parsing stack */
241 int incMax; /* Max depth of the parsing stack */
242 xmlRelaxNGIncludePtr *incTab; /* array of incs */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000243
Daniel Veillard4c004142003-10-07 11:33:24 +0000244 int idref; /* requires idref checking */
Daniel Veillard52b48c72003-04-13 19:53:42 +0000245
246 /* used to compile content models */
Daniel Veillard4c004142003-10-07 11:33:24 +0000247 xmlAutomataPtr am; /* the automata */
248 xmlAutomataStatePtr state; /* used to build the automata */
Daniel Veillard03c2f0a2004-01-25 19:54:59 +0000249
250 int crng; /* compact syntax and other flags */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000251};
252
253#define FLAGS_IGNORABLE 1
254#define FLAGS_NEGATIVE 2
Daniel Veillard249d7bb2003-03-19 21:02:29 +0000255#define FLAGS_MIXED_CONTENT 4
Daniel Veillard6eadf632003-01-23 18:29:16 +0000256
257/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000258 * xmlRelaxNGInterleaveGroup:
259 *
260 * A RelaxNGs partition set associated to lists of definitions
261 */
262typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup;
263typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr;
264struct _xmlRelaxNGInterleaveGroup {
Daniel Veillard4c004142003-10-07 11:33:24 +0000265 xmlRelaxNGDefinePtr rule; /* the rule to satisfy */
266 xmlRelaxNGDefinePtr *defs; /* the array of element definitions */
267 xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000268};
269
Daniel Veillardbbb78b52003-03-21 01:24:45 +0000270#define IS_DETERMINIST 1
271#define IS_NEEDCHECK 2
Daniel Veillard4c004142003-10-07 11:33:24 +0000272
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000273/**
274 * xmlRelaxNGPartitions:
275 *
276 * A RelaxNGs partition associated to an interleave group
277 */
278typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition;
279typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr;
280struct _xmlRelaxNGPartition {
Daniel Veillard4c004142003-10-07 11:33:24 +0000281 int nbgroups; /* number of groups in the partitions */
282 xmlHashTablePtr triage; /* hash table used to direct nodes to the
283 * right group when possible */
284 int flags; /* determinist ? */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000285 xmlRelaxNGInterleaveGroupPtr *groups;
286};
287
288/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000289 * xmlRelaxNGValidState:
290 *
291 * A RelaxNGs validation state
292 */
293#define MAX_ATTR 20
294typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState;
295typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr;
296struct _xmlRelaxNGValidState {
Daniel Veillard4c004142003-10-07 11:33:24 +0000297 xmlNodePtr node; /* the current node */
298 xmlNodePtr seq; /* the sequence of children left to validate */
299 int nbAttrs; /* the number of attributes */
300 int maxAttrs; /* the size of attrs */
301 int nbAttrLeft; /* the number of attributes left to validate */
302 xmlChar *value; /* the value when operating on string */
303 xmlChar *endvalue; /* the end value when operating on string */
304 xmlAttrPtr *attrs; /* the array of attributes */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000305};
306
307/**
Daniel Veillardfd573f12003-03-16 17:52:32 +0000308 * xmlRelaxNGStates:
309 *
310 * A RelaxNGs container for validation state
311 */
312typedef struct _xmlRelaxNGStates xmlRelaxNGStates;
313typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr;
314struct _xmlRelaxNGStates {
Daniel Veillard4c004142003-10-07 11:33:24 +0000315 int nbState; /* the number of states */
316 int maxState; /* the size of the array */
Daniel Veillardfd573f12003-03-16 17:52:32 +0000317 xmlRelaxNGValidStatePtr *tabState;
318};
319
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000320#define ERROR_IS_DUP 1
Daniel Veillard4c004142003-10-07 11:33:24 +0000321
Daniel Veillardfd573f12003-03-16 17:52:32 +0000322/**
Daniel Veillard42f12e92003-03-07 18:32:59 +0000323 * xmlRelaxNGValidError:
324 *
325 * A RelaxNGs validation error
326 */
327typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError;
328typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr;
329struct _xmlRelaxNGValidError {
Daniel Veillard4c004142003-10-07 11:33:24 +0000330 xmlRelaxNGValidErr err; /* the error number */
331 int flags; /* flags */
332 xmlNodePtr node; /* the current node */
333 xmlNodePtr seq; /* the current child */
334 const xmlChar *arg1; /* first arg */
335 const xmlChar *arg2; /* second arg */
Daniel Veillard42f12e92003-03-07 18:32:59 +0000336};
337
338/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000339 * xmlRelaxNGValidCtxt:
340 *
341 * A RelaxNGs validation context
342 */
343
344struct _xmlRelaxNGValidCtxt {
Daniel Veillard4c004142003-10-07 11:33:24 +0000345 void *userData; /* user specific data block */
346 xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
347 xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */
Daniel Veillard659e71e2003-10-10 14:10:40 +0000348 xmlStructuredErrorFunc serror;
Daniel Veillard4c004142003-10-07 11:33:24 +0000349 int nbErrors; /* number of errors in validation */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000350
Daniel Veillard4c004142003-10-07 11:33:24 +0000351 xmlRelaxNGPtr schema; /* The schema in use */
352 xmlDocPtr doc; /* the document being validated */
353 int flags; /* validation flags */
354 int depth; /* validation depth */
355 int idref; /* requires idref checking */
356 int errNo; /* the first error found */
Daniel Veillard42f12e92003-03-07 18:32:59 +0000357
358 /*
359 * Errors accumulated in branches may have to be stacked to be
360 * provided back when it's sure they affect validation.
361 */
362 xmlRelaxNGValidErrorPtr err; /* Last error */
Daniel Veillard4c004142003-10-07 11:33:24 +0000363 int errNr; /* Depth of the error stack */
364 int errMax; /* Max depth of the error stack */
365 xmlRelaxNGValidErrorPtr errTab; /* stack of errors */
Daniel Veillard1564e6e2003-03-15 21:30:25 +0000366
Daniel Veillard4c004142003-10-07 11:33:24 +0000367 xmlRelaxNGValidStatePtr state; /* the current validation state */
368 xmlRelaxNGStatesPtr states; /* the accumulated state list */
Daniel Veillard798024a2003-03-19 10:36:09 +0000369
Daniel Veillard4c004142003-10-07 11:33:24 +0000370 xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */
371 int freeStatesNr;
372 int freeStatesMax;
373 xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */
Daniel Veillardf4e55762003-04-15 23:32:22 +0000374
375 /*
376 * This is used for "progressive" validation
377 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000378 xmlRegExecCtxtPtr elem; /* the current element regexp */
379 int elemNr; /* the number of element validated */
380 int elemMax; /* the max depth of elements */
381 xmlRegExecCtxtPtr *elemTab; /* the stack of regexp runtime */
382 int pstate; /* progressive state */
383 xmlNodePtr pnode; /* the current node */
384 xmlRelaxNGDefinePtr pdef; /* the non-streamable definition */
385 int perr; /* signal error in content model
386 * outside the regexp */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000387};
388
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000389/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000390 * xmlRelaxNGInclude:
391 *
392 * Structure associated to a RelaxNGs document element
393 */
394struct _xmlRelaxNGInclude {
Daniel Veillard4c004142003-10-07 11:33:24 +0000395 xmlRelaxNGIncludePtr next; /* keep a chain of includes */
396 xmlChar *href; /* the normalized href value */
397 xmlDocPtr doc; /* the associated XML document */
398 xmlRelaxNGDefinePtr content; /* the definitions */
399 xmlRelaxNGPtr schema; /* the schema */
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000400};
401
402/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000403 * xmlRelaxNGDocument:
404 *
405 * Structure associated to a RelaxNGs document element
406 */
407struct _xmlRelaxNGDocument {
Daniel Veillardc482e262003-02-26 14:48:48 +0000408 xmlRelaxNGDocumentPtr next; /* keep a chain of documents */
Daniel Veillard4c004142003-10-07 11:33:24 +0000409 xmlChar *href; /* the normalized href value */
410 xmlDocPtr doc; /* the associated XML document */
411 xmlRelaxNGDefinePtr content; /* the definitions */
412 xmlRelaxNGPtr schema; /* the schema */
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000413};
414
Daniel Veillard3ebc7d42003-02-24 17:17:58 +0000415
Daniel Veillard6eadf632003-01-23 18:29:16 +0000416/************************************************************************
Daniel Veillard4c004142003-10-07 11:33:24 +0000417 * *
418 * Some factorized error routines *
419 * *
420 ************************************************************************/
421
422/**
423 * xmlRngPErrMemory:
424 * @ctxt: an Relax-NG parser context
425 * @extra: extra informations
426 *
427 * Handle a redefinition of attribute error
428 */
429static void
430xmlRngPErrMemory(xmlRelaxNGParserCtxtPtr ctxt, const char *extra)
431{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000432 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +0000433 xmlGenericErrorFunc channel = NULL;
434 void *data = NULL;
435
436 if (ctxt != NULL) {
437 channel = ctxt->error;
438 data = ctxt->userData;
439 ctxt->nbErrors++;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000440 schannel = ctxt->serror;
Daniel Veillard4c004142003-10-07 11:33:24 +0000441 }
442 if (extra)
Daniel Veillard659e71e2003-10-10 14:10:40 +0000443 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000444 NULL, NULL, XML_FROM_RELAXNGP,
445 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
446 NULL, NULL, 0, 0,
447 "Memory allocation failed : %s\n", extra);
448 else
Daniel Veillard659e71e2003-10-10 14:10:40 +0000449 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000450 NULL, NULL, XML_FROM_RELAXNGP,
451 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
452 NULL, NULL, 0, 0, "Memory allocation failed\n");
453}
454
455/**
456 * xmlRngVErrMemory:
457 * @ctxt: a Relax-NG validation context
458 * @extra: extra informations
459 *
460 * Handle a redefinition of attribute error
461 */
462static void
463xmlRngVErrMemory(xmlRelaxNGValidCtxtPtr ctxt, const char *extra)
464{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000465 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +0000466 xmlGenericErrorFunc channel = NULL;
467 void *data = NULL;
468
469 if (ctxt != NULL) {
470 channel = ctxt->error;
471 data = ctxt->userData;
472 ctxt->nbErrors++;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000473 schannel = ctxt->serror;
Daniel Veillard4c004142003-10-07 11:33:24 +0000474 }
475 if (extra)
Daniel Veillard659e71e2003-10-10 14:10:40 +0000476 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000477 NULL, NULL, XML_FROM_RELAXNGV,
478 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
479 NULL, NULL, 0, 0,
480 "Memory allocation failed : %s\n", extra);
481 else
Daniel Veillard659e71e2003-10-10 14:10:40 +0000482 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000483 NULL, NULL, XML_FROM_RELAXNGV,
484 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
485 NULL, NULL, 0, 0, "Memory allocation failed\n");
486}
487
488/**
489 * xmlRngPErr:
490 * @ctxt: a Relax-NG parser context
491 * @node: the node raising the error
492 * @error: the error code
493 * @msg: message
494 * @str1: extra info
495 * @str2: extra info
496 *
497 * Handle a Relax NG Parsing error
498 */
499static void
500xmlRngPErr(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, int error,
501 const char *msg, const xmlChar * str1, const xmlChar * str2)
502{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000503 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +0000504 xmlGenericErrorFunc channel = NULL;
505 void *data = NULL;
506
507 if (ctxt != NULL) {
508 channel = ctxt->error;
509 data = ctxt->userData;
510 ctxt->nbErrors++;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000511 schannel = ctxt->serror;
Daniel Veillard4c004142003-10-07 11:33:24 +0000512 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000513 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000514 NULL, node, XML_FROM_RELAXNGP,
515 error, XML_ERR_ERROR, NULL, 0,
516 (const char *) str1, (const char *) str2, NULL, 0, 0,
517 msg, str1, str2);
518}
519
520/**
521 * xmlRngVErr:
522 * @ctxt: a Relax-NG validation context
523 * @node: the node raising the error
524 * @error: the error code
525 * @msg: message
526 * @str1: extra info
527 * @str2: extra info
528 *
529 * Handle a Relax NG Validation error
530 */
531static void
532xmlRngVErr(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node, int error,
533 const char *msg, const xmlChar * str1, const xmlChar * str2)
534{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000535 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +0000536 xmlGenericErrorFunc channel = NULL;
537 void *data = NULL;
538
539 if (ctxt != NULL) {
540 channel = ctxt->error;
541 data = ctxt->userData;
542 ctxt->nbErrors++;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000543 schannel = ctxt->serror;
Daniel Veillard4c004142003-10-07 11:33:24 +0000544 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000545 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000546 NULL, node, XML_FROM_RELAXNGV,
547 error, XML_ERR_ERROR, NULL, 0,
548 (const char *) str1, (const char *) str2, NULL, 0, 0,
549 msg, str1, str2);
550}
551
552/************************************************************************
Daniel Veillard6eadf632003-01-23 18:29:16 +0000553 * *
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000554 * Preliminary type checking interfaces *
555 * *
556 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +0000557
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000558/**
559 * xmlRelaxNGTypeHave:
560 * @data: data needed for the library
561 * @type: the type name
562 * @value: the value to check
563 *
564 * Function provided by a type library to check if a type is exported
565 *
566 * Returns 1 if yes, 0 if no and -1 in case of error.
567 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000568typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar * type);
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000569
570/**
571 * xmlRelaxNGTypeCheck:
572 * @data: data needed for the library
573 * @type: the type name
574 * @value: the value to check
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000575 * @result: place to store the result if needed
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000576 *
577 * Function provided by a type library to check if a value match a type
578 *
579 * Returns 1 if yes, 0 if no and -1 in case of error.
580 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000581typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar * type,
582 const xmlChar * value, void **result,
583 xmlNodePtr node);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000584
585/**
586 * xmlRelaxNGFacetCheck:
587 * @data: data needed for the library
588 * @type: the type name
589 * @facet: the facet name
590 * @val: the facet value
591 * @strval: the string value
592 * @value: the value to check
593 *
594 * Function provided by a type library to check a value facet
595 *
596 * Returns 1 if yes, 0 if no and -1 in case of error.
597 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000598typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar * type,
599 const xmlChar * facet,
600 const xmlChar * val,
601 const xmlChar * strval, void *value);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000602
603/**
604 * xmlRelaxNGTypeFree:
605 * @data: data needed for the library
606 * @result: the value to free
607 *
608 * Function provided by a type library to free a returned result
609 */
610typedef void (*xmlRelaxNGTypeFree) (void *data, void *result);
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000611
612/**
613 * xmlRelaxNGTypeCompare:
614 * @data: data needed for the library
615 * @type: the type name
616 * @value1: the first value
617 * @value2: the second value
618 *
619 * Function provided by a type library to compare two values accordingly
620 * to a type.
621 *
622 * Returns 1 if yes, 0 if no and -1 in case of error.
623 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000624typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar * type,
625 const xmlChar * value1,
626 xmlNodePtr ctxt1,
627 void *comp1,
628 const xmlChar * value2,
629 xmlNodePtr ctxt2);
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000630typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary;
631typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr;
632struct _xmlRelaxNGTypeLibrary {
Daniel Veillard4c004142003-10-07 11:33:24 +0000633 const xmlChar *namespace; /* the datatypeLibrary value */
634 void *data; /* data needed for the library */
635 xmlRelaxNGTypeHave have; /* the export function */
636 xmlRelaxNGTypeCheck check; /* the checking function */
637 xmlRelaxNGTypeCompare comp; /* the compare function */
638 xmlRelaxNGFacetCheck facet; /* the facet check function */
639 xmlRelaxNGTypeFree freef; /* the freeing function */
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000640};
641
642/************************************************************************
643 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +0000644 * Allocation functions *
645 * *
646 ************************************************************************/
Daniel Veillard6eadf632003-01-23 18:29:16 +0000647static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar);
648static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define);
Daniel Veillard4c004142003-10-07 11:33:24 +0000649static void xmlRelaxNGNormExtSpace(xmlChar * value);
Daniel Veillardc482e262003-02-26 14:48:48 +0000650static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema);
Daniel Veillard4c004142003-10-07 11:33:24 +0000651static int xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt
652 ATTRIBUTE_UNUSED,
653 xmlRelaxNGValidStatePtr state1,
654 xmlRelaxNGValidStatePtr state2);
Daniel Veillard798024a2003-03-19 10:36:09 +0000655static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +0000656 xmlRelaxNGValidStatePtr state);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000657
658/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000659 * xmlRelaxNGFreeDocument:
660 * @docu: a document structure
661 *
662 * Deallocate a RelaxNG document structure.
663 */
664static void
665xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu)
666{
667 if (docu == NULL)
668 return;
669
670 if (docu->href != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000671 xmlFree(docu->href);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000672 if (docu->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000673 xmlFreeDoc(docu->doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000674 if (docu->schema != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000675 xmlRelaxNGFreeInnerSchema(docu->schema);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000676 xmlFree(docu);
677}
678
679/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000680 * xmlRelaxNGFreeDocumentList:
681 * @docu: a list of document structure
682 *
683 * Deallocate a RelaxNG document structures.
684 */
685static void
686xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu)
687{
688 xmlRelaxNGDocumentPtr next;
Daniel Veillard4c004142003-10-07 11:33:24 +0000689
Daniel Veillardc482e262003-02-26 14:48:48 +0000690 while (docu != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000691 next = docu->next;
692 xmlRelaxNGFreeDocument(docu);
693 docu = next;
Daniel Veillardc482e262003-02-26 14:48:48 +0000694 }
695}
696
697/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000698 * xmlRelaxNGFreeInclude:
699 * @incl: a include structure
700 *
701 * Deallocate a RelaxNG include structure.
702 */
703static void
704xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl)
705{
706 if (incl == NULL)
707 return;
708
709 if (incl->href != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000710 xmlFree(incl->href);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000711 if (incl->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000712 xmlFreeDoc(incl->doc);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000713 if (incl->schema != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000714 xmlRelaxNGFree(incl->schema);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000715 xmlFree(incl);
716}
717
718/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000719 * xmlRelaxNGFreeIncludeList:
720 * @incl: a include structure list
721 *
722 * Deallocate a RelaxNG include structure.
723 */
724static void
725xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl)
726{
727 xmlRelaxNGIncludePtr next;
Daniel Veillard4c004142003-10-07 11:33:24 +0000728
Daniel Veillardc482e262003-02-26 14:48:48 +0000729 while (incl != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000730 next = incl->next;
731 xmlRelaxNGFreeInclude(incl);
732 incl = next;
Daniel Veillardc482e262003-02-26 14:48:48 +0000733 }
734}
735
736/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000737 * xmlRelaxNGNewRelaxNG:
738 * @ctxt: a Relax-NG validation context (optional)
739 *
740 * Allocate a new RelaxNG structure.
741 *
742 * Returns the newly allocated structure or NULL in case or error
743 */
744static xmlRelaxNGPtr
745xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt)
746{
747 xmlRelaxNGPtr ret;
748
749 ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG));
750 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000751 xmlRngPErrMemory(ctxt, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000752 return (NULL);
753 }
754 memset(ret, 0, sizeof(xmlRelaxNG));
755
756 return (ret);
757}
758
759/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000760 * xmlRelaxNGFreeInnerSchema:
761 * @schema: a schema structure
762 *
763 * Deallocate a RelaxNG schema structure.
764 */
765static void
766xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema)
767{
768 if (schema == NULL)
769 return;
770
771 if (schema->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000772 xmlFreeDoc(schema->doc);
Daniel Veillardc482e262003-02-26 14:48:48 +0000773 if (schema->defTab != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000774 int i;
Daniel Veillardc482e262003-02-26 14:48:48 +0000775
Daniel Veillard4c004142003-10-07 11:33:24 +0000776 for (i = 0; i < schema->defNr; i++)
777 xmlRelaxNGFreeDefine(schema->defTab[i]);
778 xmlFree(schema->defTab);
Daniel Veillardc482e262003-02-26 14:48:48 +0000779 }
780
781 xmlFree(schema);
782}
783
784/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000785 * xmlRelaxNGFree:
786 * @schema: a schema structure
787 *
788 * Deallocate a RelaxNG structure.
789 */
790void
791xmlRelaxNGFree(xmlRelaxNGPtr schema)
792{
793 if (schema == NULL)
794 return;
795
Daniel Veillard6eadf632003-01-23 18:29:16 +0000796 if (schema->topgrammar != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000797 xmlRelaxNGFreeGrammar(schema->topgrammar);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000798 if (schema->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000799 xmlFreeDoc(schema->doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000800 if (schema->documents != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000801 xmlRelaxNGFreeDocumentList(schema->documents);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000802 if (schema->includes != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000803 xmlRelaxNGFreeIncludeList(schema->includes);
Daniel Veillard419a7682003-02-03 23:22:49 +0000804 if (schema->defTab != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000805 int i;
Daniel Veillard419a7682003-02-03 23:22:49 +0000806
Daniel Veillard4c004142003-10-07 11:33:24 +0000807 for (i = 0; i < schema->defNr; i++)
808 xmlRelaxNGFreeDefine(schema->defTab[i]);
809 xmlFree(schema->defTab);
Daniel Veillard419a7682003-02-03 23:22:49 +0000810 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000811
812 xmlFree(schema);
813}
814
815/**
816 * xmlRelaxNGNewGrammar:
817 * @ctxt: a Relax-NG validation context (optional)
818 *
819 * Allocate a new RelaxNG grammar.
820 *
821 * Returns the newly allocated structure or NULL in case or error
822 */
823static xmlRelaxNGGrammarPtr
824xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt)
825{
826 xmlRelaxNGGrammarPtr ret;
827
828 ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar));
829 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000830 xmlRngPErrMemory(ctxt, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000831 return (NULL);
832 }
833 memset(ret, 0, sizeof(xmlRelaxNGGrammar));
834
835 return (ret);
836}
837
838/**
839 * xmlRelaxNGFreeGrammar:
840 * @grammar: a grammar structure
841 *
842 * Deallocate a RelaxNG grammar structure.
843 */
844static void
845xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar)
846{
847 if (grammar == NULL)
848 return;
849
Daniel Veillardc482e262003-02-26 14:48:48 +0000850 if (grammar->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000851 xmlRelaxNGFreeGrammar(grammar->children);
Daniel Veillardc482e262003-02-26 14:48:48 +0000852 }
Daniel Veillard419a7682003-02-03 23:22:49 +0000853 if (grammar->next != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000854 xmlRelaxNGFreeGrammar(grammar->next);
Daniel Veillard419a7682003-02-03 23:22:49 +0000855 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000856 if (grammar->refs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000857 xmlHashFree(grammar->refs, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000858 }
859 if (grammar->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000860 xmlHashFree(grammar->defs, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000861 }
862
863 xmlFree(grammar);
864}
865
866/**
867 * xmlRelaxNGNewDefine:
868 * @ctxt: a Relax-NG validation context
869 * @node: the node in the input document.
870 *
871 * Allocate a new RelaxNG define.
872 *
873 * Returns the newly allocated structure or NULL in case or error
874 */
875static xmlRelaxNGDefinePtr
Daniel Veillardfd573f12003-03-16 17:52:32 +0000876xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
Daniel Veillard6eadf632003-01-23 18:29:16 +0000877{
878 xmlRelaxNGDefinePtr ret;
879
Daniel Veillard419a7682003-02-03 23:22:49 +0000880 if (ctxt->defMax == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000881 ctxt->defMax = 16;
882 ctxt->defNr = 0;
883 ctxt->defTab = (xmlRelaxNGDefinePtr *)
884 xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr));
885 if (ctxt->defTab == NULL) {
886 xmlRngPErrMemory(ctxt, "allocating define\n");
887 return (NULL);
888 }
Daniel Veillard419a7682003-02-03 23:22:49 +0000889 } else if (ctxt->defMax <= ctxt->defNr) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000890 xmlRelaxNGDefinePtr *tmp;
891
892 ctxt->defMax *= 2;
893 tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab,
894 ctxt->defMax *
895 sizeof
896 (xmlRelaxNGDefinePtr));
897 if (tmp == NULL) {
898 xmlRngPErrMemory(ctxt, "allocating define\n");
899 return (NULL);
900 }
901 ctxt->defTab = tmp;
Daniel Veillard419a7682003-02-03 23:22:49 +0000902 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000903 ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine));
904 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000905 xmlRngPErrMemory(ctxt, "allocating define\n");
906 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000907 }
908 memset(ret, 0, sizeof(xmlRelaxNGDefine));
Daniel Veillard419a7682003-02-03 23:22:49 +0000909 ctxt->defTab[ctxt->defNr++] = ret;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000910 ret->node = node;
Daniel Veillardd4310742003-02-18 21:12:46 +0000911 ret->depth = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000912 return (ret);
913}
914
915/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000916 * xmlRelaxNGFreePartition:
917 * @partitions: a partition set structure
918 *
919 * Deallocate RelaxNG partition set structures.
920 */
921static void
Daniel Veillard4c004142003-10-07 11:33:24 +0000922xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions)
923{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000924 xmlRelaxNGInterleaveGroupPtr group;
925 int j;
926
927 if (partitions != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000928 if (partitions->groups != NULL) {
929 for (j = 0; j < partitions->nbgroups; j++) {
930 group = partitions->groups[j];
931 if (group != NULL) {
932 if (group->defs != NULL)
933 xmlFree(group->defs);
934 if (group->attrs != NULL)
935 xmlFree(group->attrs);
936 xmlFree(group);
937 }
938 }
939 xmlFree(partitions->groups);
940 }
941 if (partitions->triage != NULL) {
942 xmlHashFree(partitions->triage, NULL);
943 }
944 xmlFree(partitions);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000945 }
946}
Daniel Veillard4c004142003-10-07 11:33:24 +0000947
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000948/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000949 * xmlRelaxNGFreeDefine:
950 * @define: a define structure
951 *
952 * Deallocate a RelaxNG define structure.
953 */
954static void
955xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define)
956{
957 if (define == NULL)
958 return;
959
Daniel Veillard4c004142003-10-07 11:33:24 +0000960 if ((define->type == XML_RELAXNG_VALUE) && (define->attrs != NULL)) {
961 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000962
Daniel Veillard4c004142003-10-07 11:33:24 +0000963 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
964 if ((lib != NULL) && (lib->freef != NULL))
965 lib->freef(lib->data, (void *) define->attrs);
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000966 }
Daniel Veillard4c004142003-10-07 11:33:24 +0000967 if ((define->data != NULL) && (define->type == XML_RELAXNG_INTERLEAVE))
968 xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data);
969 if ((define->data != NULL) && (define->type == XML_RELAXNG_CHOICE))
970 xmlHashFree((xmlHashTablePtr) define->data, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000971 if (define->name != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000972 xmlFree(define->name);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000973 if (define->ns != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000974 xmlFree(define->ns);
Daniel Veillardedc91922003-01-26 00:52:04 +0000975 if (define->value != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000976 xmlFree(define->value);
Daniel Veillard52b48c72003-04-13 19:53:42 +0000977 if (define->contModel != NULL)
978 xmlRegFreeRegexp(define->contModel);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000979 xmlFree(define);
980}
981
982/**
Daniel Veillardfd573f12003-03-16 17:52:32 +0000983 * xmlRelaxNGNewStates:
984 * @ctxt: a Relax-NG validation context
985 * @size: the default size for the container
986 *
987 * Allocate a new RelaxNG validation state container
Daniel Veillardfd573f12003-03-16 17:52:32 +0000988 *
989 * Returns the newly allocated structure or NULL in case or error
990 */
991static xmlRelaxNGStatesPtr
992xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size)
993{
994 xmlRelaxNGStatesPtr ret;
995
Daniel Veillard798024a2003-03-19 10:36:09 +0000996 if ((ctxt != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +0000997 (ctxt->freeState != NULL) && (ctxt->freeStatesNr > 0)) {
998 ctxt->freeStatesNr--;
999 ret = ctxt->freeStates[ctxt->freeStatesNr];
1000 ret->nbState = 0;
1001 return (ret);
Daniel Veillard798024a2003-03-19 10:36:09 +00001002 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001003 if (size < 16)
1004 size = 16;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001005
1006 ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) +
Daniel Veillard4c004142003-10-07 11:33:24 +00001007 (size -
1008 1) *
1009 sizeof(xmlRelaxNGValidStatePtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001010 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001011 xmlRngVErrMemory(ctxt, "allocating states\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00001012 return (NULL);
1013 }
1014 ret->nbState = 0;
1015 ret->maxState = size;
Daniel Veillard4c004142003-10-07 11:33:24 +00001016 ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc((size) *
1017 sizeof
1018 (xmlRelaxNGValidStatePtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001019 if (ret->tabState == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001020 xmlRngVErrMemory(ctxt, "allocating states\n");
1021 xmlFree(ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001022 return (NULL);
1023 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001024 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001025}
1026
1027/**
Daniel Veillard798024a2003-03-19 10:36:09 +00001028 * xmlRelaxNGAddStateUniq:
1029 * @ctxt: a Relax-NG validation context
1030 * @states: the states container
1031 * @state: the validation state
1032 *
1033 * Add a RelaxNG validation state to the container without checking
1034 * for unicity.
1035 *
1036 * Return 1 in case of success and 0 if this is a duplicate and -1 on error
1037 */
1038static int
1039xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001040 xmlRelaxNGStatesPtr states,
1041 xmlRelaxNGValidStatePtr state)
Daniel Veillard798024a2003-03-19 10:36:09 +00001042{
1043 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001044 return (-1);
Daniel Veillard798024a2003-03-19 10:36:09 +00001045 }
1046 if (states->nbState >= states->maxState) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001047 xmlRelaxNGValidStatePtr *tmp;
1048 int size;
Daniel Veillard798024a2003-03-19 10:36:09 +00001049
Daniel Veillard4c004142003-10-07 11:33:24 +00001050 size = states->maxState * 2;
1051 tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
1052 (size) *
1053 sizeof
1054 (xmlRelaxNGValidStatePtr));
Daniel Veillard798024a2003-03-19 10:36:09 +00001055 if (tmp == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001056 xmlRngVErrMemory(ctxt, "adding states\n");
1057 return (-1);
1058 }
1059 states->tabState = tmp;
1060 states->maxState = size;
Daniel Veillard798024a2003-03-19 10:36:09 +00001061 }
1062 states->tabState[states->nbState++] = state;
Daniel Veillard4c004142003-10-07 11:33:24 +00001063 return (1);
Daniel Veillard798024a2003-03-19 10:36:09 +00001064}
1065
1066/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00001067 * xmlRelaxNGAddState:
1068 * @ctxt: a Relax-NG validation context
1069 * @states: the states container
1070 * @state: the validation state
1071 *
1072 * Add a RelaxNG validation state to the container
1073 *
1074 * Return 1 in case of success and 0 if this is a duplicate and -1 on error
1075 */
1076static int
Daniel Veillard4c004142003-10-07 11:33:24 +00001077xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt,
1078 xmlRelaxNGStatesPtr states,
1079 xmlRelaxNGValidStatePtr state)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001080{
1081 int i;
1082
1083 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001084 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001085 }
1086 if (states->nbState >= states->maxState) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001087 xmlRelaxNGValidStatePtr *tmp;
1088 int size;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001089
Daniel Veillard4c004142003-10-07 11:33:24 +00001090 size = states->maxState * 2;
1091 tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
1092 (size) *
1093 sizeof
1094 (xmlRelaxNGValidStatePtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001095 if (tmp == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001096 xmlRngVErrMemory(ctxt, "adding states\n");
1097 return (-1);
1098 }
1099 states->tabState = tmp;
1100 states->maxState = size;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001101 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001102 for (i = 0; i < states->nbState; i++) {
1103 if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) {
1104 xmlRelaxNGFreeValidState(ctxt, state);
1105 return (0);
1106 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00001107 }
1108 states->tabState[states->nbState++] = state;
Daniel Veillard4c004142003-10-07 11:33:24 +00001109 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001110}
1111
1112/**
1113 * xmlRelaxNGFreeStates:
1114 * @ctxt: a Relax-NG validation context
1115 * @states: teh container
1116 *
1117 * Free a RelaxNG validation state container
Daniel Veillardfd573f12003-03-16 17:52:32 +00001118 */
1119static void
Daniel Veillard798024a2003-03-19 10:36:09 +00001120xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001121 xmlRelaxNGStatesPtr states)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001122{
Daniel Veillard798024a2003-03-19 10:36:09 +00001123 if (states == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00001124 return;
Daniel Veillard798024a2003-03-19 10:36:09 +00001125 if ((ctxt != NULL) && (ctxt->freeStates == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001126 ctxt->freeStatesMax = 40;
1127 ctxt->freeStatesNr = 0;
1128 ctxt->freeStates = (xmlRelaxNGStatesPtr *)
1129 xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr));
1130 if (ctxt->freeStates == NULL) {
1131 xmlRngVErrMemory(ctxt, "storing states\n");
1132 }
1133 } else if ((ctxt != NULL)
1134 && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) {
1135 xmlRelaxNGStatesPtr *tmp;
Daniel Veillard798024a2003-03-19 10:36:09 +00001136
Daniel Veillard4c004142003-10-07 11:33:24 +00001137 tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates,
1138 2 * ctxt->freeStatesMax *
1139 sizeof
1140 (xmlRelaxNGStatesPtr));
1141 if (tmp == NULL) {
1142 xmlRngVErrMemory(ctxt, "storing states\n");
1143 xmlFree(states->tabState);
1144 xmlFree(states);
1145 return;
1146 }
1147 ctxt->freeStates = tmp;
1148 ctxt->freeStatesMax *= 2;
Daniel Veillard798024a2003-03-19 10:36:09 +00001149 }
1150 if ((ctxt == NULL) || (ctxt->freeState == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001151 xmlFree(states->tabState);
1152 xmlFree(states);
Daniel Veillard798024a2003-03-19 10:36:09 +00001153 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001154 ctxt->freeStates[ctxt->freeStatesNr++] = states;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001155 }
1156}
1157
1158/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00001159 * xmlRelaxNGNewValidState:
1160 * @ctxt: a Relax-NG validation context
1161 * @node: the current node or NULL for the document
1162 *
1163 * Allocate a new RelaxNG validation state
1164 *
1165 * Returns the newly allocated structure or NULL in case or error
1166 */
1167static xmlRelaxNGValidStatePtr
1168xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node)
1169{
1170 xmlRelaxNGValidStatePtr ret;
1171 xmlAttrPtr attr;
1172 xmlAttrPtr attrs[MAX_ATTR];
1173 int nbAttrs = 0;
1174 xmlNodePtr root = NULL;
1175
1176 if (node == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001177 root = xmlDocGetRootElement(ctxt->doc);
1178 if (root == NULL)
1179 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00001180 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001181 attr = node->properties;
1182 while (attr != NULL) {
1183 if (nbAttrs < MAX_ATTR)
1184 attrs[nbAttrs++] = attr;
1185 else
1186 nbAttrs++;
1187 attr = attr->next;
1188 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00001189 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001190 if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) {
1191 ctxt->freeState->nbState--;
1192 ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
Daniel Veillard798024a2003-03-19 10:36:09 +00001193 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001194 ret =
1195 (xmlRelaxNGValidStatePtr)
1196 xmlMalloc(sizeof(xmlRelaxNGValidState));
1197 if (ret == NULL) {
1198 xmlRngVErrMemory(ctxt, "allocating states\n");
1199 return (NULL);
1200 }
1201 memset(ret, 0, sizeof(xmlRelaxNGValidState));
Daniel Veillard6eadf632003-01-23 18:29:16 +00001202 }
Daniel Veillarde5b110b2003-02-04 14:43:39 +00001203 ret->value = NULL;
1204 ret->endvalue = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001205 if (node == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001206 ret->node = (xmlNodePtr) ctxt->doc;
1207 ret->seq = root;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001208 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001209 ret->node = node;
1210 ret->seq = node->children;
Daniel Veillard798024a2003-03-19 10:36:09 +00001211 }
1212 ret->nbAttrs = 0;
1213 if (nbAttrs > 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001214 if (ret->attrs == NULL) {
1215 if (nbAttrs < 4)
1216 ret->maxAttrs = 4;
1217 else
1218 ret->maxAttrs = nbAttrs;
1219 ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
1220 sizeof(xmlAttrPtr));
1221 if (ret->attrs == NULL) {
1222 xmlRngVErrMemory(ctxt, "allocating states\n");
1223 return (ret);
1224 }
1225 } else if (ret->maxAttrs < nbAttrs) {
1226 xmlAttrPtr *tmp;
Daniel Veillard798024a2003-03-19 10:36:09 +00001227
Daniel Veillard4c004142003-10-07 11:33:24 +00001228 tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs *
1229 sizeof(xmlAttrPtr));
1230 if (tmp == NULL) {
1231 xmlRngVErrMemory(ctxt, "allocating states\n");
1232 return (ret);
1233 }
1234 ret->attrs = tmp;
1235 ret->maxAttrs = nbAttrs;
1236 }
1237 ret->nbAttrs = nbAttrs;
1238 if (nbAttrs < MAX_ATTR) {
1239 memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs);
1240 } else {
1241 attr = node->properties;
1242 nbAttrs = 0;
1243 while (attr != NULL) {
1244 ret->attrs[nbAttrs++] = attr;
1245 attr = attr->next;
1246 }
1247 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00001248 }
Daniel Veillard1ed7f362003-02-03 10:57:45 +00001249 ret->nbAttrLeft = ret->nbAttrs;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001250 return (ret);
1251}
1252
1253/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00001254 * xmlRelaxNGCopyValidState:
1255 * @ctxt: a Relax-NG validation context
1256 * @state: a validation state
1257 *
1258 * Copy the validation state
1259 *
1260 * Returns the newly allocated structure or NULL in case or error
1261 */
1262static xmlRelaxNGValidStatePtr
1263xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001264 xmlRelaxNGValidStatePtr state)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001265{
1266 xmlRelaxNGValidStatePtr ret;
Daniel Veillard798024a2003-03-19 10:36:09 +00001267 unsigned int maxAttrs;
1268 xmlAttrPtr *attrs;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001269
1270 if (state == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00001271 return (NULL);
1272 if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) {
1273 ctxt->freeState->nbState--;
1274 ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
Daniel Veillard798024a2003-03-19 10:36:09 +00001275 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001276 ret =
1277 (xmlRelaxNGValidStatePtr)
1278 xmlMalloc(sizeof(xmlRelaxNGValidState));
1279 if (ret == NULL) {
1280 xmlRngVErrMemory(ctxt, "allocating states\n");
1281 return (NULL);
1282 }
1283 memset(ret, 0, sizeof(xmlRelaxNGValidState));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001284 }
Daniel Veillard798024a2003-03-19 10:36:09 +00001285 attrs = ret->attrs;
1286 maxAttrs = ret->maxAttrs;
1287 memcpy(ret, state, sizeof(xmlRelaxNGValidState));
1288 ret->attrs = attrs;
1289 ret->maxAttrs = maxAttrs;
1290 if (state->nbAttrs > 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001291 if (ret->attrs == NULL) {
1292 ret->maxAttrs = state->maxAttrs;
1293 ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
1294 sizeof(xmlAttrPtr));
1295 if (ret->attrs == NULL) {
1296 xmlRngVErrMemory(ctxt, "allocating states\n");
1297 ret->nbAttrs = 0;
1298 return (ret);
1299 }
1300 } else if (ret->maxAttrs < state->nbAttrs) {
1301 xmlAttrPtr *tmp;
Daniel Veillard798024a2003-03-19 10:36:09 +00001302
Daniel Veillard4c004142003-10-07 11:33:24 +00001303 tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs *
1304 sizeof(xmlAttrPtr));
1305 if (tmp == NULL) {
1306 xmlRngVErrMemory(ctxt, "allocating states\n");
1307 ret->nbAttrs = 0;
1308 return (ret);
1309 }
1310 ret->maxAttrs = state->maxAttrs;
1311 ret->attrs = tmp;
1312 }
1313 memcpy(ret->attrs, state->attrs,
1314 state->nbAttrs * sizeof(xmlAttrPtr));
Daniel Veillard798024a2003-03-19 10:36:09 +00001315 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001316 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001317}
1318
1319/**
1320 * xmlRelaxNGEqualValidState:
1321 * @ctxt: a Relax-NG validation context
1322 * @state1: a validation state
1323 * @state2: a validation state
1324 *
1325 * Compare the validation states for equality
1326 *
1327 * Returns 1 if equald, 0 otherwise
1328 */
1329static int
1330xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00001331 xmlRelaxNGValidStatePtr state1,
1332 xmlRelaxNGValidStatePtr state2)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001333{
1334 int i;
1335
1336 if ((state1 == NULL) || (state2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00001337 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001338 if (state1 == state2)
Daniel Veillard4c004142003-10-07 11:33:24 +00001339 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001340 if (state1->node != state2->node)
Daniel Veillard4c004142003-10-07 11:33:24 +00001341 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001342 if (state1->seq != state2->seq)
Daniel Veillard4c004142003-10-07 11:33:24 +00001343 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001344 if (state1->nbAttrLeft != state2->nbAttrLeft)
Daniel Veillard4c004142003-10-07 11:33:24 +00001345 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001346 if (state1->nbAttrs != state2->nbAttrs)
Daniel Veillard4c004142003-10-07 11:33:24 +00001347 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001348 if (state1->endvalue != state2->endvalue)
Daniel Veillard4c004142003-10-07 11:33:24 +00001349 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001350 if ((state1->value != state2->value) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00001351 (!xmlStrEqual(state1->value, state2->value)))
1352 return (0);
1353 for (i = 0; i < state1->nbAttrs; i++) {
1354 if (state1->attrs[i] != state2->attrs[i])
1355 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001356 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001357 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001358}
1359
1360/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00001361 * xmlRelaxNGFreeValidState:
1362 * @state: a validation state structure
1363 *
1364 * Deallocate a RelaxNG validation state structure.
1365 */
1366static void
Daniel Veillard798024a2003-03-19 10:36:09 +00001367xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001368 xmlRelaxNGValidStatePtr state)
Daniel Veillard6eadf632003-01-23 18:29:16 +00001369{
1370 if (state == NULL)
1371 return;
1372
Daniel Veillard798024a2003-03-19 10:36:09 +00001373 if ((ctxt != NULL) && (ctxt->freeState == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001374 ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40);
Daniel Veillard798024a2003-03-19 10:36:09 +00001375 }
1376 if ((ctxt == NULL) || (ctxt->freeState == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001377 if (state->attrs != NULL)
1378 xmlFree(state->attrs);
1379 xmlFree(state);
Daniel Veillard798024a2003-03-19 10:36:09 +00001380 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001381 xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state);
Daniel Veillard798024a2003-03-19 10:36:09 +00001382 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00001383}
1384
1385/************************************************************************
1386 * *
Daniel Veillard03c2f0a2004-01-25 19:54:59 +00001387 * Semi internal functions *
1388 * *
1389 ************************************************************************/
1390
1391/**
1392 * xmlRelaxParserSetFlag:
1393 * @ctxt: a RelaxNG parser context
1394 * @flags: a set of flags values
1395 *
1396 * Semi private function used to pass informations to a parser context
1397 * which are a combination of xmlRelaxNGParserFlag .
1398 *
1399 * Returns 0 if success and -1 in case of error
1400 */
1401int
1402xmlRelaxParserSetFlag(xmlRelaxNGParserCtxtPtr ctxt, int flags)
1403{
1404 if (ctxt == NULL) return(-1);
1405 if (flags & XML_RELAXNGP_FREE_DOC) {
1406 ctxt->crng |= XML_RELAXNGP_FREE_DOC;
1407 flags -= XML_RELAXNGP_FREE_DOC;
1408 }
1409 if (flags & XML_RELAXNGP_CRNG) {
1410 ctxt->crng |= XML_RELAXNGP_CRNG;
1411 flags -= XML_RELAXNGP_CRNG;
1412 }
1413 if (flags != 0) return(-1);
1414 return(0);
1415}
1416
1417/************************************************************************
1418 * *
1419 * Document functions *
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001420 * *
1421 ************************************************************************/
1422static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001423 xmlDocPtr doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001424
1425/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001426 * xmlRelaxNGIncludePush:
1427 * @ctxt: the parser context
1428 * @value: the element doc
1429 *
1430 * Pushes a new include on top of the include stack
1431 *
1432 * Returns 0 in case of error, the index in the stack otherwise
1433 */
1434static int
1435xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001436 xmlRelaxNGIncludePtr value)
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001437{
1438 if (ctxt->incTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001439 ctxt->incMax = 4;
1440 ctxt->incNr = 0;
1441 ctxt->incTab =
1442 (xmlRelaxNGIncludePtr *) xmlMalloc(ctxt->incMax *
1443 sizeof(ctxt->incTab[0]));
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001444 if (ctxt->incTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001445 xmlRngPErrMemory(ctxt, "allocating include\n");
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001446 return (0);
1447 }
1448 }
1449 if (ctxt->incNr >= ctxt->incMax) {
1450 ctxt->incMax *= 2;
1451 ctxt->incTab =
1452 (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00001453 ctxt->incMax *
1454 sizeof(ctxt->incTab[0]));
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001455 if (ctxt->incTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001456 xmlRngPErrMemory(ctxt, "allocating include\n");
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001457 return (0);
1458 }
1459 }
1460 ctxt->incTab[ctxt->incNr] = value;
1461 ctxt->inc = value;
1462 return (ctxt->incNr++);
1463}
1464
1465/**
1466 * xmlRelaxNGIncludePop:
1467 * @ctxt: the parser context
1468 *
1469 * Pops the top include from the include stack
1470 *
1471 * Returns the include just removed
1472 */
1473static xmlRelaxNGIncludePtr
1474xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt)
1475{
1476 xmlRelaxNGIncludePtr ret;
1477
1478 if (ctxt->incNr <= 0)
1479 return (0);
1480 ctxt->incNr--;
1481 if (ctxt->incNr > 0)
1482 ctxt->inc = ctxt->incTab[ctxt->incNr - 1];
1483 else
1484 ctxt->inc = NULL;
1485 ret = ctxt->incTab[ctxt->incNr];
1486 ctxt->incTab[ctxt->incNr] = 0;
1487 return (ret);
1488}
1489
1490/**
Daniel Veillard5add8682003-03-10 13:13:58 +00001491 * xmlRelaxNGRemoveRedefine:
1492 * @ctxt: the parser context
1493 * @URL: the normalized URL
1494 * @target: the included target
1495 * @name: the define name to eliminate
1496 *
1497 * Applies the elimination algorithm of 4.7
1498 *
1499 * Returns 0 in case of error, 1 in case of success.
1500 */
1501static int
1502xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001503 const xmlChar * URL ATTRIBUTE_UNUSED,
1504 xmlNodePtr target, const xmlChar * name)
1505{
Daniel Veillard5add8682003-03-10 13:13:58 +00001506 int found = 0;
1507 xmlNodePtr tmp, tmp2;
1508 xmlChar *name2;
1509
1510#ifdef DEBUG_INCLUDE
Daniel Veillard952379b2003-03-17 15:37:12 +00001511 if (name == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00001512 xmlGenericError(xmlGenericErrorContext,
1513 "Elimination of <include> start from %s\n", URL);
Daniel Veillard952379b2003-03-17 15:37:12 +00001514 else
Daniel Veillard4c004142003-10-07 11:33:24 +00001515 xmlGenericError(xmlGenericErrorContext,
1516 "Elimination of <include> define %s from %s\n",
1517 name, URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001518#endif
1519 tmp = target;
1520 while (tmp != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001521 tmp2 = tmp->next;
1522 if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) {
1523 found = 1;
1524 xmlUnlinkNode(tmp);
1525 xmlFreeNode(tmp);
1526 } else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) {
1527 name2 = xmlGetProp(tmp, BAD_CAST "name");
1528 xmlRelaxNGNormExtSpace(name2);
1529 if (name2 != NULL) {
1530 if (xmlStrEqual(name, name2)) {
1531 found = 1;
1532 xmlUnlinkNode(tmp);
1533 xmlFreeNode(tmp);
1534 }
1535 xmlFree(name2);
1536 }
1537 } else if (IS_RELAXNG(tmp, "include")) {
1538 xmlChar *href = NULL;
Daniel Veillard807daf82004-02-22 22:13:27 +00001539 xmlRelaxNGDocumentPtr inc = tmp->psvi;
Daniel Veillard5add8682003-03-10 13:13:58 +00001540
Daniel Veillard4c004142003-10-07 11:33:24 +00001541 if ((inc != NULL) && (inc->doc != NULL) &&
1542 (inc->doc->children != NULL)) {
Daniel Veillard5add8682003-03-10 13:13:58 +00001543
Daniel Veillard4c004142003-10-07 11:33:24 +00001544 if (xmlStrEqual
1545 (inc->doc->children->name, BAD_CAST "grammar")) {
Daniel Veillard5add8682003-03-10 13:13:58 +00001546#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001547 href = xmlGetProp(tmp, BAD_CAST "href");
Daniel Veillard5add8682003-03-10 13:13:58 +00001548#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00001549 if (xmlRelaxNGRemoveRedefine(ctxt, href,
1550 inc->doc->children->
1551 children, name) == 1) {
1552 found = 1;
1553 }
1554 if (href != NULL)
1555 xmlFree(href);
1556 }
1557 }
1558 }
1559 tmp = tmp2;
Daniel Veillard5add8682003-03-10 13:13:58 +00001560 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001561 return (found);
Daniel Veillard5add8682003-03-10 13:13:58 +00001562}
1563
1564/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001565 * xmlRelaxNGLoadInclude:
1566 * @ctxt: the parser context
1567 * @URL: the normalized URL
1568 * @node: the include node.
Daniel Veillard416589a2003-02-17 17:25:42 +00001569 * @ns: the namespace passed from the context.
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001570 *
1571 * First lookup if the document is already loaded into the parser context,
1572 * check against recursion. If not found the resource is loaded and
1573 * the content is preprocessed before being returned back to the caller.
1574 *
1575 * Returns the xmlRelaxNGIncludePtr or NULL in case of error
1576 */
1577static xmlRelaxNGIncludePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00001578xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * URL,
1579 xmlNodePtr node, const xmlChar * ns)
1580{
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001581 xmlRelaxNGIncludePtr ret = NULL;
1582 xmlDocPtr doc;
1583 int i;
Daniel Veillard5add8682003-03-10 13:13:58 +00001584 xmlNodePtr root, cur;
1585
1586#ifdef DEBUG_INCLUDE
1587 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00001588 "xmlRelaxNGLoadInclude(%s)\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001589#endif
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001590
1591 /*
1592 * check against recursion in the stack
1593 */
Daniel Veillard4c004142003-10-07 11:33:24 +00001594 for (i = 0; i < ctxt->incNr; i++) {
1595 if (xmlStrEqual(ctxt->incTab[i]->href, URL)) {
1596 xmlRngPErr(ctxt, NULL, XML_RNGP_INCLUDE_RECURSE,
1597 "Detected an Include recursion for %s\n", URL,
1598 NULL);
1599 return (NULL);
1600 }
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001601 }
1602
1603 /*
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001604 * load the document
1605 */
Daniel Veillard87247e82004-01-13 20:42:02 +00001606 doc = xmlReadFile((const char *) URL,NULL,0);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001607 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001608 xmlRngPErr(ctxt, node, XML_RNGP_PARSE_ERROR,
1609 "xmlRelaxNG: could not load %s\n", URL, NULL);
1610 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001611 }
Daniel Veillard5add8682003-03-10 13:13:58 +00001612#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001613 xmlGenericError(xmlGenericErrorContext, "Parsed %s Okay\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001614#endif
1615
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001616 /*
1617 * Allocate the document structures and register it first.
1618 */
1619 ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude));
1620 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001621 xmlRngPErrMemory(ctxt, "allocating include\n");
1622 xmlFreeDoc(doc);
1623 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001624 }
1625 memset(ret, 0, sizeof(xmlRelaxNGInclude));
1626 ret->doc = doc;
1627 ret->href = xmlStrdup(URL);
Daniel Veillardc482e262003-02-26 14:48:48 +00001628 ret->next = ctxt->includes;
1629 ctxt->includes = ret;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001630
1631 /*
Daniel Veillard416589a2003-02-17 17:25:42 +00001632 * transmit the ns if needed
1633 */
1634 if (ns != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001635 root = xmlDocGetRootElement(doc);
1636 if (root != NULL) {
1637 if (xmlHasProp(root, BAD_CAST "ns") == NULL) {
1638 xmlSetProp(root, BAD_CAST "ns", ns);
1639 }
1640 }
Daniel Veillard416589a2003-02-17 17:25:42 +00001641 }
1642
1643 /*
Daniel Veillardc482e262003-02-26 14:48:48 +00001644 * push it on the stack
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001645 */
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001646 xmlRelaxNGIncludePush(ctxt, ret);
1647
1648 /*
1649 * Some preprocessing of the document content, this include recursing
1650 * in the include stack.
1651 */
Daniel Veillard5add8682003-03-10 13:13:58 +00001652#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001653 xmlGenericError(xmlGenericErrorContext, "cleanup of %s\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001654#endif
1655
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001656 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
1657 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001658 ctxt->inc = NULL;
1659 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001660 }
1661
1662 /*
1663 * Pop up the include from the stack
1664 */
1665 xmlRelaxNGIncludePop(ctxt);
1666
Daniel Veillard5add8682003-03-10 13:13:58 +00001667#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001668 xmlGenericError(xmlGenericErrorContext, "Checking of %s\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001669#endif
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001670 /*
1671 * Check that the top element is a grammar
1672 */
1673 root = xmlDocGetRootElement(doc);
1674 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001675 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY,
1676 "xmlRelaxNG: included document is empty %s\n", URL,
1677 NULL);
1678 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001679 }
1680 if (!IS_RELAXNG(root, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001681 xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING,
1682 "xmlRelaxNG: included document %s root is not a grammar\n",
1683 URL, NULL);
1684 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001685 }
1686
1687 /*
1688 * Elimination of redefined rules in the include.
1689 */
1690 cur = node->children;
1691 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001692 if (IS_RELAXNG(cur, "start")) {
1693 int found = 0;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001694
Daniel Veillard4c004142003-10-07 11:33:24 +00001695 found =
1696 xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL);
1697 if (!found) {
1698 xmlRngPErr(ctxt, node, XML_RNGP_START_MISSING,
1699 "xmlRelaxNG: include %s has a start but not the included grammar\n",
1700 URL, NULL);
1701 }
1702 } else if (IS_RELAXNG(cur, "define")) {
1703 xmlChar *name;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001704
Daniel Veillard4c004142003-10-07 11:33:24 +00001705 name = xmlGetProp(cur, BAD_CAST "name");
1706 if (name == NULL) {
1707 xmlRngPErr(ctxt, node, XML_RNGP_NAME_MISSING,
1708 "xmlRelaxNG: include %s has define without name\n",
1709 URL, NULL);
1710 } else {
1711 int found;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001712
Daniel Veillard4c004142003-10-07 11:33:24 +00001713 xmlRelaxNGNormExtSpace(name);
1714 found = xmlRelaxNGRemoveRedefine(ctxt, URL,
1715 root->children, name);
1716 if (!found) {
1717 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_MISSING,
1718 "xmlRelaxNG: include %s has a define %s but not the included grammar\n",
1719 URL, name);
1720 }
1721 xmlFree(name);
1722 }
1723 }
1724 cur = cur->next;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001725 }
1726
1727
Daniel Veillard4c004142003-10-07 11:33:24 +00001728 return (ret);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001729}
1730
1731/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00001732 * xmlRelaxNGValidErrorPush:
1733 * @ctxt: the validation context
1734 * @err: the error code
1735 * @arg1: the first string argument
1736 * @arg2: the second string argument
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001737 * @dup: arg need to be duplicated
Daniel Veillard42f12e92003-03-07 18:32:59 +00001738 *
1739 * Pushes a new error on top of the error stack
1740 *
1741 * Returns 0 in case of error, the index in the stack otherwise
1742 */
1743static int
Daniel Veillard4c004142003-10-07 11:33:24 +00001744xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt,
1745 xmlRelaxNGValidErr err, const xmlChar * arg1,
1746 const xmlChar * arg2, int dup)
Daniel Veillard42f12e92003-03-07 18:32:59 +00001747{
1748 xmlRelaxNGValidErrorPtr cur;
Daniel Veillard4c004142003-10-07 11:33:24 +00001749
Daniel Veillarda507fbf2003-03-31 16:09:37 +00001750#ifdef DEBUG_ERROR
1751 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00001752 "Pushing error %d at %d on stack\n", err, ctxt->errNr);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00001753#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00001754 if (ctxt->errTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001755 ctxt->errMax = 8;
1756 ctxt->errNr = 0;
1757 ctxt->errTab =
1758 (xmlRelaxNGValidErrorPtr) xmlMalloc(ctxt->errMax *
1759 sizeof
1760 (xmlRelaxNGValidError));
Daniel Veillard42f12e92003-03-07 18:32:59 +00001761 if (ctxt->errTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001762 xmlRngVErrMemory(ctxt, "pushing error\n");
Daniel Veillard42f12e92003-03-07 18:32:59 +00001763 return (0);
1764 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001765 ctxt->err = NULL;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001766 }
1767 if (ctxt->errNr >= ctxt->errMax) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001768 ctxt->errMax *= 2;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001769 ctxt->errTab =
1770 (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00001771 ctxt->errMax *
1772 sizeof
1773 (xmlRelaxNGValidError));
Daniel Veillard42f12e92003-03-07 18:32:59 +00001774 if (ctxt->errTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001775 xmlRngVErrMemory(ctxt, "pushing error\n");
Daniel Veillard42f12e92003-03-07 18:32:59 +00001776 return (0);
1777 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001778 ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
Daniel Veillard42f12e92003-03-07 18:32:59 +00001779 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00001780 if ((ctxt->err != NULL) && (ctxt->state != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00001781 (ctxt->err->node == ctxt->state->node) && (ctxt->err->err == err))
1782 return (ctxt->errNr);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001783 cur = &ctxt->errTab[ctxt->errNr];
1784 cur->err = err;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001785 if (dup) {
1786 cur->arg1 = xmlStrdup(arg1);
1787 cur->arg2 = xmlStrdup(arg2);
Daniel Veillard4c004142003-10-07 11:33:24 +00001788 cur->flags = ERROR_IS_DUP;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001789 } else {
1790 cur->arg1 = arg1;
1791 cur->arg2 = arg2;
Daniel Veillard4c004142003-10-07 11:33:24 +00001792 cur->flags = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001793 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001794 if (ctxt->state != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001795 cur->node = ctxt->state->node;
1796 cur->seq = ctxt->state->seq;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001797 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001798 cur->node = NULL;
1799 cur->seq = NULL;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001800 }
1801 ctxt->err = cur;
1802 return (ctxt->errNr++);
1803}
1804
1805/**
1806 * xmlRelaxNGValidErrorPop:
1807 * @ctxt: the validation context
1808 *
1809 * Pops the top error from the error stack
Daniel Veillard42f12e92003-03-07 18:32:59 +00001810 */
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001811static void
Daniel Veillard42f12e92003-03-07 18:32:59 +00001812xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt)
1813{
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001814 xmlRelaxNGValidErrorPtr cur;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001815
Daniel Veillard580ced82003-03-21 21:22:48 +00001816 if (ctxt->errNr <= 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001817 ctxt->err = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001818 return;
Daniel Veillard580ced82003-03-21 21:22:48 +00001819 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001820 ctxt->errNr--;
1821 if (ctxt->errNr > 0)
1822 ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
1823 else
1824 ctxt->err = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001825 cur = &ctxt->errTab[ctxt->errNr];
1826 if (cur->flags & ERROR_IS_DUP) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001827 if (cur->arg1 != NULL)
1828 xmlFree((xmlChar *) cur->arg1);
1829 cur->arg1 = NULL;
1830 if (cur->arg2 != NULL)
1831 xmlFree((xmlChar *) cur->arg2);
1832 cur->arg2 = NULL;
1833 cur->flags = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001834 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001835}
1836
Daniel Veillard42f12e92003-03-07 18:32:59 +00001837/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001838 * xmlRelaxNGDocumentPush:
1839 * @ctxt: the parser context
1840 * @value: the element doc
1841 *
1842 * Pushes a new doc on top of the doc stack
1843 *
1844 * Returns 0 in case of error, the index in the stack otherwise
1845 */
1846static int
1847xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001848 xmlRelaxNGDocumentPtr value)
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001849{
1850 if (ctxt->docTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001851 ctxt->docMax = 4;
1852 ctxt->docNr = 0;
1853 ctxt->docTab =
1854 (xmlRelaxNGDocumentPtr *) xmlMalloc(ctxt->docMax *
1855 sizeof(ctxt->docTab[0]));
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001856 if (ctxt->docTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001857 xmlRngPErrMemory(ctxt, "adding document\n");
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001858 return (0);
1859 }
1860 }
1861 if (ctxt->docNr >= ctxt->docMax) {
1862 ctxt->docMax *= 2;
1863 ctxt->docTab =
1864 (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00001865 ctxt->docMax *
1866 sizeof(ctxt->docTab[0]));
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001867 if (ctxt->docTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001868 xmlRngPErrMemory(ctxt, "adding document\n");
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001869 return (0);
1870 }
1871 }
1872 ctxt->docTab[ctxt->docNr] = value;
1873 ctxt->doc = value;
1874 return (ctxt->docNr++);
1875}
1876
1877/**
1878 * xmlRelaxNGDocumentPop:
1879 * @ctxt: the parser context
1880 *
1881 * Pops the top doc from the doc stack
1882 *
1883 * Returns the doc just removed
1884 */
1885static xmlRelaxNGDocumentPtr
1886xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt)
1887{
1888 xmlRelaxNGDocumentPtr ret;
1889
1890 if (ctxt->docNr <= 0)
1891 return (0);
1892 ctxt->docNr--;
1893 if (ctxt->docNr > 0)
1894 ctxt->doc = ctxt->docTab[ctxt->docNr - 1];
1895 else
1896 ctxt->doc = NULL;
1897 ret = ctxt->docTab[ctxt->docNr];
1898 ctxt->docTab[ctxt->docNr] = 0;
1899 return (ret);
1900}
1901
1902/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001903 * xmlRelaxNGLoadExternalRef:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001904 * @ctxt: the parser context
1905 * @URL: the normalized URL
1906 * @ns: the inherited ns if any
1907 *
1908 * First lookup if the document is already loaded into the parser context,
1909 * check against recursion. If not found the resource is loaded and
1910 * the content is preprocessed before being returned back to the caller.
1911 *
1912 * Returns the xmlRelaxNGDocumentPtr or NULL in case of error
1913 */
1914static xmlRelaxNGDocumentPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00001915xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt,
1916 const xmlChar * URL, const xmlChar * ns)
1917{
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001918 xmlRelaxNGDocumentPtr ret = NULL;
1919 xmlDocPtr doc;
1920 xmlNodePtr root;
1921 int i;
1922
1923 /*
1924 * check against recursion in the stack
1925 */
Daniel Veillard4c004142003-10-07 11:33:24 +00001926 for (i = 0; i < ctxt->docNr; i++) {
1927 if (xmlStrEqual(ctxt->docTab[i]->href, URL)) {
1928 xmlRngPErr(ctxt, NULL, XML_RNGP_EXTERNALREF_RECURSE,
1929 "Detected an externalRef recursion for %s\n", URL,
1930 NULL);
1931 return (NULL);
1932 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001933 }
1934
1935 /*
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001936 * load the document
1937 */
Daniel Veillard87247e82004-01-13 20:42:02 +00001938 doc = xmlReadFile((const char *) URL,NULL,0);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001939 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001940 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
1941 "xmlRelaxNG: could not load %s\n", URL, NULL);
1942 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001943 }
1944
1945 /*
1946 * Allocate the document structures and register it first.
1947 */
1948 ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument));
1949 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001950 xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_ERR_NO_MEMORY,
1951 "xmlRelaxNG: allocate memory for doc %s\n", URL, NULL);
1952 xmlFreeDoc(doc);
1953 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001954 }
1955 memset(ret, 0, sizeof(xmlRelaxNGDocument));
1956 ret->doc = doc;
1957 ret->href = xmlStrdup(URL);
Daniel Veillardc482e262003-02-26 14:48:48 +00001958 ret->next = ctxt->documents;
1959 ctxt->documents = ret;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001960
1961 /*
1962 * transmit the ns if needed
1963 */
1964 if (ns != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001965 root = xmlDocGetRootElement(doc);
1966 if (root != NULL) {
1967 if (xmlHasProp(root, BAD_CAST "ns") == NULL) {
1968 xmlSetProp(root, BAD_CAST "ns", ns);
1969 }
1970 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001971 }
1972
1973 /*
1974 * push it on the stack and register it in the hash table
1975 */
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001976 xmlRelaxNGDocumentPush(ctxt, ret);
1977
1978 /*
1979 * Some preprocessing of the document content
1980 */
1981 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
1982 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001983 ctxt->doc = NULL;
1984 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001985 }
1986
1987 xmlRelaxNGDocumentPop(ctxt);
1988
Daniel Veillard4c004142003-10-07 11:33:24 +00001989 return (ret);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001990}
1991
1992/************************************************************************
1993 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00001994 * Error functions *
1995 * *
1996 ************************************************************************/
1997
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001998#define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0);
1999#define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0);
2000#define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0);
2001#define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1);
2002#define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002003
Daniel Veillard231d7912003-02-09 14:22:17 +00002004static const char *
Daniel Veillard4c004142003-10-07 11:33:24 +00002005xmlRelaxNGDefName(xmlRelaxNGDefinePtr def)
2006{
Daniel Veillard231d7912003-02-09 14:22:17 +00002007 if (def == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002008 return ("none");
2009 switch (def->type) {
2010 case XML_RELAXNG_EMPTY:
2011 return ("empty");
2012 case XML_RELAXNG_NOT_ALLOWED:
2013 return ("notAllowed");
2014 case XML_RELAXNG_EXCEPT:
2015 return ("except");
2016 case XML_RELAXNG_TEXT:
2017 return ("text");
2018 case XML_RELAXNG_ELEMENT:
2019 return ("element");
2020 case XML_RELAXNG_DATATYPE:
2021 return ("datatype");
2022 case XML_RELAXNG_VALUE:
2023 return ("value");
2024 case XML_RELAXNG_LIST:
2025 return ("list");
2026 case XML_RELAXNG_ATTRIBUTE:
2027 return ("attribute");
2028 case XML_RELAXNG_DEF:
2029 return ("def");
2030 case XML_RELAXNG_REF:
2031 return ("ref");
2032 case XML_RELAXNG_EXTERNALREF:
2033 return ("externalRef");
2034 case XML_RELAXNG_PARENTREF:
2035 return ("parentRef");
2036 case XML_RELAXNG_OPTIONAL:
2037 return ("optional");
2038 case XML_RELAXNG_ZEROORMORE:
2039 return ("zeroOrMore");
2040 case XML_RELAXNG_ONEORMORE:
2041 return ("oneOrMore");
2042 case XML_RELAXNG_CHOICE:
2043 return ("choice");
2044 case XML_RELAXNG_GROUP:
2045 return ("group");
2046 case XML_RELAXNG_INTERLEAVE:
2047 return ("interleave");
2048 case XML_RELAXNG_START:
2049 return ("start");
2050 case XML_RELAXNG_NOOP:
2051 return ("noop");
2052 case XML_RELAXNG_PARAM:
2053 return ("param");
Daniel Veillard231d7912003-02-09 14:22:17 +00002054 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002055 return ("unknown");
Daniel Veillard231d7912003-02-09 14:22:17 +00002056}
Daniel Veillardd2298792003-02-14 16:54:11 +00002057
Daniel Veillard6eadf632003-01-23 18:29:16 +00002058/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00002059 * xmlRelaxNGGetErrorString:
2060 * @err: the error code
2061 * @arg1: the first string argument
2062 * @arg2: the second string argument
Daniel Veillard6eadf632003-01-23 18:29:16 +00002063 *
Daniel Veillard42f12e92003-03-07 18:32:59 +00002064 * computes a formatted error string for the given error code and args
2065 *
2066 * Returns the error string, it must be deallocated by the caller
2067 */
2068static xmlChar *
Daniel Veillard4c004142003-10-07 11:33:24 +00002069xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar * arg1,
2070 const xmlChar * arg2)
2071{
Daniel Veillard42f12e92003-03-07 18:32:59 +00002072 char msg[1000];
2073
2074 if (arg1 == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002075 arg1 = BAD_CAST "";
Daniel Veillard42f12e92003-03-07 18:32:59 +00002076 if (arg2 == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002077 arg2 = BAD_CAST "";
Daniel Veillard42f12e92003-03-07 18:32:59 +00002078
2079 msg[0] = 0;
2080 switch (err) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002081 case XML_RELAXNG_OK:
2082 return (NULL);
2083 case XML_RELAXNG_ERR_MEMORY:
2084 return (xmlCharStrdup("out of memory\n"));
Daniel Veillard42f12e92003-03-07 18:32:59 +00002085 case XML_RELAXNG_ERR_TYPE:
Daniel Veillard4c004142003-10-07 11:33:24 +00002086 snprintf(msg, 1000, "failed to validate type %s\n", arg1);
2087 break;
2088 case XML_RELAXNG_ERR_TYPEVAL:
2089 snprintf(msg, 1000, "Type %s doesn't allow value '%s'\n", arg1,
2090 arg2);
2091 break;
2092 case XML_RELAXNG_ERR_DUPID:
2093 snprintf(msg, 1000, "ID %s redefined\n", arg1);
2094 break;
2095 case XML_RELAXNG_ERR_TYPECMP:
2096 snprintf(msg, 1000, "failed to compare type %s\n", arg1);
2097 break;
2098 case XML_RELAXNG_ERR_NOSTATE:
2099 return (xmlCharStrdup("Internal error: no state\n"));
2100 case XML_RELAXNG_ERR_NODEFINE:
2101 return (xmlCharStrdup("Internal error: no define\n"));
2102 case XML_RELAXNG_ERR_INTERNAL:
2103 snprintf(msg, 1000, "Internal error: %s\n", arg1);
2104 break;
2105 case XML_RELAXNG_ERR_LISTEXTRA:
2106 snprintf(msg, 1000, "Extra data in list: %s\n", arg1);
2107 break;
2108 case XML_RELAXNG_ERR_INTERNODATA:
2109 return (xmlCharStrdup
2110 ("Internal: interleave block has no data\n"));
2111 case XML_RELAXNG_ERR_INTERSEQ:
2112 return (xmlCharStrdup("Invalid sequence in interleave\n"));
2113 case XML_RELAXNG_ERR_INTEREXTRA:
2114 snprintf(msg, 1000, "Extra element %s in interleave\n", arg1);
2115 break;
2116 case XML_RELAXNG_ERR_ELEMNAME:
2117 snprintf(msg, 1000, "Expecting element %s, got %s\n", arg1,
2118 arg2);
2119 break;
2120 case XML_RELAXNG_ERR_ELEMNONS:
2121 snprintf(msg, 1000, "Expecting a namespace for element %s\n",
2122 arg1);
2123 break;
2124 case XML_RELAXNG_ERR_ELEMWRONGNS:
2125 snprintf(msg, 1000,
2126 "Element %s has wrong namespace: expecting %s\n", arg1,
2127 arg2);
2128 break;
2129 case XML_RELAXNG_ERR_ELEMWRONG:
2130 snprintf(msg, 1000, "Did not expect element %s there\n", arg1);
2131 break;
2132 case XML_RELAXNG_ERR_TEXTWRONG:
2133 snprintf(msg, 1000,
2134 "Did not expect text in element %s content\n", arg1);
2135 break;
2136 case XML_RELAXNG_ERR_ELEMEXTRANS:
2137 snprintf(msg, 1000, "Expecting no namespace for element %s\n",
2138 arg1);
2139 break;
2140 case XML_RELAXNG_ERR_ELEMNOTEMPTY:
2141 snprintf(msg, 1000, "Expecting element %s to be empty\n", arg1);
2142 break;
2143 case XML_RELAXNG_ERR_NOELEM:
2144 snprintf(msg, 1000, "Expecting an element %s, got nothing\n",
2145 arg1);
2146 break;
2147 case XML_RELAXNG_ERR_NOTELEM:
2148 return (xmlCharStrdup("Expecting an element got text\n"));
2149 case XML_RELAXNG_ERR_ATTRVALID:
2150 snprintf(msg, 1000, "Element %s failed to validate attributes\n",
2151 arg1);
2152 break;
2153 case XML_RELAXNG_ERR_CONTENTVALID:
2154 snprintf(msg, 1000, "Element %s failed to validate content\n",
2155 arg1);
2156 break;
2157 case XML_RELAXNG_ERR_EXTRACONTENT:
2158 snprintf(msg, 1000, "Element %s has extra content: %s\n",
2159 arg1, arg2);
2160 break;
2161 case XML_RELAXNG_ERR_INVALIDATTR:
2162 snprintf(msg, 1000, "Invalid attribute %s for element %s\n",
2163 arg1, arg2);
2164 break;
2165 case XML_RELAXNG_ERR_LACKDATA:
2166 snprintf(msg, 1000, "Datatype element %s contains no data\n",
2167 arg1);
2168 break;
2169 case XML_RELAXNG_ERR_DATAELEM:
2170 snprintf(msg, 1000, "Datatype element %s has child elements\n",
2171 arg1);
2172 break;
2173 case XML_RELAXNG_ERR_VALELEM:
2174 snprintf(msg, 1000, "Value element %s has child elements\n",
2175 arg1);
2176 break;
2177 case XML_RELAXNG_ERR_LISTELEM:
2178 snprintf(msg, 1000, "List element %s has child elements\n",
2179 arg1);
2180 break;
2181 case XML_RELAXNG_ERR_DATATYPE:
2182 snprintf(msg, 1000, "Error validating datatype %s\n", arg1);
2183 break;
2184 case XML_RELAXNG_ERR_VALUE:
2185 snprintf(msg, 1000, "Error validating value %s\n", arg1);
2186 break;
2187 case XML_RELAXNG_ERR_LIST:
2188 return (xmlCharStrdup("Error validating list\n"));
2189 case XML_RELAXNG_ERR_NOGRAMMAR:
2190 return (xmlCharStrdup("No top grammar defined\n"));
2191 case XML_RELAXNG_ERR_EXTRADATA:
2192 return (xmlCharStrdup("Extra data in the document\n"));
2193 default:
2194 return (xmlCharStrdup("Unknown error !\n"));
Daniel Veillard42f12e92003-03-07 18:32:59 +00002195 }
2196 if (msg[0] == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002197 snprintf(msg, 1000, "Unknown error code %d\n", err);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002198 }
Daniel Veillardadbb0e62003-05-10 20:02:45 +00002199 msg[1000 - 1] = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00002200 return (xmlStrdup((xmlChar *) msg));
Daniel Veillard42f12e92003-03-07 18:32:59 +00002201}
2202
2203/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00002204 * xmlRelaxNGShowValidError:
2205 * @ctxt: the validation context
2206 * @err: the error number
2207 * @node: the node
2208 * @child: the node child generating the problem.
2209 * @arg1: the first argument
2210 * @arg2: the second argument
2211 *
2212 * Show a validation error.
2213 */
2214static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002215xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt,
2216 xmlRelaxNGValidErr err, xmlNodePtr node,
2217 xmlNodePtr child, const xmlChar * arg1,
2218 const xmlChar * arg2)
Daniel Veillard42f12e92003-03-07 18:32:59 +00002219{
2220 xmlChar *msg;
2221
2222 if (ctxt->error == NULL)
2223 return;
2224
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002225#ifdef DEBUG_ERROR
Daniel Veillard4c004142003-10-07 11:33:24 +00002226 xmlGenericError(xmlGenericErrorContext, "Show error %d\n", err);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002227#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00002228 msg = xmlRelaxNGGetErrorString(err, arg1, arg2);
2229 if (msg == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002230 return;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002231
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002232 if (ctxt->errNo == XML_RELAXNG_OK)
Daniel Veillard4c004142003-10-07 11:33:24 +00002233 ctxt->errNo = err;
2234 xmlRngVErr(ctxt, (child == NULL ? node : child), err,
2235 (const char *) msg, arg1, arg2);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002236 xmlFree(msg);
2237}
2238
2239/**
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002240 * xmlRelaxNGPopErrors:
2241 * @ctxt: the validation context
2242 * @level: the error level in the stack
2243 *
2244 * pop and discard all errors until the given level is reached
2245 */
2246static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002247xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level)
2248{
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002249 int i;
2250 xmlRelaxNGValidErrorPtr err;
2251
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002252#ifdef DEBUG_ERROR
2253 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00002254 "Pop errors till level %d\n", level);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002255#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002256 for (i = level; i < ctxt->errNr; i++) {
2257 err = &ctxt->errTab[i];
2258 if (err->flags & ERROR_IS_DUP) {
2259 if (err->arg1 != NULL)
2260 xmlFree((xmlChar *) err->arg1);
2261 err->arg1 = NULL;
2262 if (err->arg2 != NULL)
2263 xmlFree((xmlChar *) err->arg2);
2264 err->arg2 = NULL;
2265 err->flags = 0;
2266 }
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002267 }
2268 ctxt->errNr = level;
Daniel Veillard580ced82003-03-21 21:22:48 +00002269 if (ctxt->errNr <= 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002270 ctxt->err = NULL;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002271}
Daniel Veillard4c004142003-10-07 11:33:24 +00002272
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002273/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00002274 * xmlRelaxNGDumpValidError:
2275 * @ctxt: the validation context
2276 *
2277 * Show all validation error over a given index.
2278 */
2279static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002280xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt)
2281{
Daniel Veillard5f1946a2003-03-31 16:38:16 +00002282 int i, j, k;
Daniel Veillard580ced82003-03-21 21:22:48 +00002283 xmlRelaxNGValidErrorPtr err, dup;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002284
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002285#ifdef DEBUG_ERROR
2286 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00002287 "Dumping error stack %d errors\n", ctxt->errNr);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002288#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002289 for (i = 0, k = 0; i < ctxt->errNr; i++) {
2290 err = &ctxt->errTab[i];
2291 if (k < MAX_ERROR) {
2292 for (j = 0; j < i; j++) {
2293 dup = &ctxt->errTab[j];
2294 if ((err->err == dup->err) && (err->node == dup->node) &&
2295 (xmlStrEqual(err->arg1, dup->arg1)) &&
2296 (xmlStrEqual(err->arg2, dup->arg2))) {
2297 goto skip;
2298 }
2299 }
2300 xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq,
2301 err->arg1, err->arg2);
2302 k++;
2303 }
2304 skip:
2305 if (err->flags & ERROR_IS_DUP) {
2306 if (err->arg1 != NULL)
2307 xmlFree((xmlChar *) err->arg1);
2308 err->arg1 = NULL;
2309 if (err->arg2 != NULL)
2310 xmlFree((xmlChar *) err->arg2);
2311 err->arg2 = NULL;
2312 err->flags = 0;
2313 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00002314 }
2315 ctxt->errNr = 0;
2316}
Daniel Veillard4c004142003-10-07 11:33:24 +00002317
Daniel Veillard42f12e92003-03-07 18:32:59 +00002318/**
2319 * xmlRelaxNGAddValidError:
2320 * @ctxt: the validation context
2321 * @err: the error number
2322 * @arg1: the first argument
2323 * @arg2: the second argument
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002324 * @dup: need to dup the args
Daniel Veillard42f12e92003-03-07 18:32:59 +00002325 *
2326 * Register a validation error, either generating it if it's sure
2327 * or stacking it for later handling if unsure.
2328 */
2329static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002330xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt,
2331 xmlRelaxNGValidErr err, const xmlChar * arg1,
2332 const xmlChar * arg2, int dup)
Daniel Veillard42f12e92003-03-07 18:32:59 +00002333{
2334 if ((ctxt == NULL) || (ctxt->error == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002335 return;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002336
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002337#ifdef DEBUG_ERROR
Daniel Veillard4c004142003-10-07 11:33:24 +00002338 xmlGenericError(xmlGenericErrorContext, "Adding error %d\n", err);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002339#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00002340 /*
2341 * generate the error directly
2342 */
2343 if (((ctxt->flags & 1) == 0) || (ctxt->flags & 2)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002344 xmlNodePtr node, seq;
2345
2346 /*
2347 * Flush first any stacked error which might be the
2348 * real cause of the problem.
2349 */
2350 if (ctxt->errNr != 0)
2351 xmlRelaxNGDumpValidError(ctxt);
2352 if (ctxt->state != NULL) {
2353 node = ctxt->state->node;
2354 seq = ctxt->state->seq;
2355 } else {
2356 node = seq = NULL;
2357 }
2358 xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002359 }
2360 /*
2361 * Stack the error for later processing if needed
2362 */
2363 else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002364 xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002365 }
2366}
2367
Daniel Veillard6eadf632003-01-23 18:29:16 +00002368
2369/************************************************************************
2370 * *
2371 * Type library hooks *
2372 * *
2373 ************************************************************************/
Daniel Veillardea3f3982003-01-26 19:45:18 +00002374static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00002375 const xmlChar * str);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002376
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002377/**
2378 * xmlRelaxNGSchemaTypeHave:
2379 * @data: data needed for the library
2380 * @type: the type name
2381 *
2382 * Check if the given type is provided by
2383 * the W3C XMLSchema Datatype library.
2384 *
2385 * Returns 1 if yes, 0 if no and -1 in case of error.
2386 */
2387static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002388xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar * type)
2389{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002390 xmlSchemaTypePtr typ;
2391
2392 if (type == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002393 return (-1);
2394 typ = xmlSchemaGetPredefinedType(type,
2395 BAD_CAST
2396 "http://www.w3.org/2001/XMLSchema");
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002397 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002398 return (0);
2399 return (1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002400}
2401
2402/**
2403 * xmlRelaxNGSchemaTypeCheck:
2404 * @data: data needed for the library
2405 * @type: the type name
2406 * @value: the value to check
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002407 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002408 *
2409 * Check if the given type and value are validated by
2410 * the W3C XMLSchema Datatype library.
2411 *
2412 * Returns 1 if yes, 0 if no and -1 in case of error.
2413 */
2414static int
2415xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002416 const xmlChar * type,
2417 const xmlChar * value,
2418 void **result, xmlNodePtr node)
2419{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002420 xmlSchemaTypePtr typ;
2421 int ret;
2422
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002423 if ((type == NULL) || (value == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002424 return (-1);
2425 typ = xmlSchemaGetPredefinedType(type,
2426 BAD_CAST
2427 "http://www.w3.org/2001/XMLSchema");
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002428 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002429 return (-1);
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002430 ret = xmlSchemaValPredefTypeNode(typ, value,
Daniel Veillard4c004142003-10-07 11:33:24 +00002431 (xmlSchemaValPtr *) result, node);
2432 if (ret == 2) /* special ID error code */
2433 return (2);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002434 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002435 return (1);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002436 if (ret > 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002437 return (0);
2438 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002439}
2440
2441/**
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002442 * xmlRelaxNGSchemaFacetCheck:
2443 * @data: data needed for the library
2444 * @type: the type name
2445 * @facet: the facet name
2446 * @val: the facet value
2447 * @strval: the string value
2448 * @value: the value to check
2449 *
2450 * Function provided by a type library to check a value facet
2451 *
2452 * Returns 1 if yes, 0 if no and -1 in case of error.
2453 */
2454static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002455xmlRelaxNGSchemaFacetCheck(void *data ATTRIBUTE_UNUSED,
2456 const xmlChar * type, const xmlChar * facetname,
2457 const xmlChar * val, const xmlChar * strval,
2458 void *value)
2459{
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002460 xmlSchemaFacetPtr facet;
2461 xmlSchemaTypePtr typ;
2462 int ret;
2463
2464 if ((type == NULL) || (strval == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002465 return (-1);
2466 typ = xmlSchemaGetPredefinedType(type,
2467 BAD_CAST
2468 "http://www.w3.org/2001/XMLSchema");
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002469 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002470 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002471
2472 facet = xmlSchemaNewFacet();
2473 if (facet == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002474 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002475
Daniel Veillard4c004142003-10-07 11:33:24 +00002476 if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002477 facet->type = XML_SCHEMA_FACET_MININCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002478 } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002479 facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002480 } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002481 facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002482 } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002483 facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002484 } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002485 facet->type = XML_SCHEMA_FACET_TOTALDIGITS;
Daniel Veillard4c004142003-10-07 11:33:24 +00002486 } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002487 facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS;
Daniel Veillard4c004142003-10-07 11:33:24 +00002488 } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002489 facet->type = XML_SCHEMA_FACET_PATTERN;
Daniel Veillard4c004142003-10-07 11:33:24 +00002490 } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002491 facet->type = XML_SCHEMA_FACET_ENUMERATION;
Daniel Veillard4c004142003-10-07 11:33:24 +00002492 } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002493 facet->type = XML_SCHEMA_FACET_WHITESPACE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002494 } else if (xmlStrEqual(facetname, BAD_CAST "length")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002495 facet->type = XML_SCHEMA_FACET_LENGTH;
Daniel Veillard4c004142003-10-07 11:33:24 +00002496 } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002497 facet->type = XML_SCHEMA_FACET_MAXLENGTH;
2498 } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) {
2499 facet->type = XML_SCHEMA_FACET_MINLENGTH;
2500 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002501 xmlSchemaFreeFacet(facet);
2502 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002503 }
2504 facet->value = xmlStrdup(val);
2505 ret = xmlSchemaCheckFacet(facet, typ, NULL, type);
2506 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002507 xmlSchemaFreeFacet(facet);
2508 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002509 }
2510 ret = xmlSchemaValidateFacet(typ, facet, strval, value);
2511 xmlSchemaFreeFacet(facet);
2512 if (ret != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002513 return (-1);
2514 return (0);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002515}
2516
2517/**
Daniel Veillard80b19092003-03-28 13:29:53 +00002518 * xmlRelaxNGSchemaFreeValue:
2519 * @data: data needed for the library
2520 * @value: the value to free
2521 *
2522 * Function provided by a type library to free a Schemas value
2523 *
2524 * Returns 1 if yes, 0 if no and -1 in case of error.
2525 */
2526static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002527xmlRelaxNGSchemaFreeValue(void *data ATTRIBUTE_UNUSED, void *value)
2528{
Daniel Veillard80b19092003-03-28 13:29:53 +00002529 xmlSchemaFreeValue(value);
2530}
2531
2532/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002533 * xmlRelaxNGSchemaTypeCompare:
2534 * @data: data needed for the library
2535 * @type: the type name
2536 * @value1: the first value
2537 * @value2: the second value
2538 *
Daniel Veillard80b19092003-03-28 13:29:53 +00002539 * Compare two values for equality accordingly a type from the W3C XMLSchema
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002540 * Datatype library.
2541 *
Daniel Veillard80b19092003-03-28 13:29:53 +00002542 * Returns 1 if equal, 0 if no and -1 in case of error.
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002543 */
2544static int
2545xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002546 const xmlChar * type,
2547 const xmlChar * value1,
2548 xmlNodePtr ctxt1,
2549 void *comp1,
2550 const xmlChar * value2, xmlNodePtr ctxt2)
2551{
Daniel Veillard80b19092003-03-28 13:29:53 +00002552 int ret;
2553 xmlSchemaTypePtr typ;
2554 xmlSchemaValPtr res1 = NULL, res2 = NULL;
2555
2556 if ((type == NULL) || (value1 == NULL) || (value2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002557 return (-1);
2558 typ = xmlSchemaGetPredefinedType(type,
2559 BAD_CAST
2560 "http://www.w3.org/2001/XMLSchema");
Daniel Veillard80b19092003-03-28 13:29:53 +00002561 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002562 return (-1);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002563 if (comp1 == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002564 ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1);
2565 if (ret != 0)
2566 return (-1);
2567 if (res1 == NULL)
2568 return (-1);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002569 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002570 res1 = (xmlSchemaValPtr) comp1;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002571 }
2572 ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2);
Daniel Veillard80b19092003-03-28 13:29:53 +00002573 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002574 xmlSchemaFreeValue(res1);
2575 return (-1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002576 }
2577 if (res1 == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002578 xmlSchemaFreeValue(res1);
2579 return (-1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002580 }
2581 ret = xmlSchemaCompareValues(res1, res2);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002582 if (res1 != (xmlSchemaValPtr) comp1)
Daniel Veillard4c004142003-10-07 11:33:24 +00002583 xmlSchemaFreeValue(res1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002584 xmlSchemaFreeValue(res2);
2585 if (ret == -2)
Daniel Veillard4c004142003-10-07 11:33:24 +00002586 return (-1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002587 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002588 return (1);
2589 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002590}
Daniel Veillard4c004142003-10-07 11:33:24 +00002591
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002592/**
2593 * xmlRelaxNGDefaultTypeHave:
2594 * @data: data needed for the library
2595 * @type: the type name
2596 *
2597 * Check if the given type is provided by
2598 * the default datatype library.
2599 *
2600 * Returns 1 if yes, 0 if no and -1 in case of error.
2601 */
2602static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002603xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED,
2604 const xmlChar * type)
2605{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002606 if (type == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002607 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002608 if (xmlStrEqual(type, BAD_CAST "string"))
Daniel Veillard4c004142003-10-07 11:33:24 +00002609 return (1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002610 if (xmlStrEqual(type, BAD_CAST "token"))
Daniel Veillard4c004142003-10-07 11:33:24 +00002611 return (1);
2612 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002613}
2614
2615/**
2616 * xmlRelaxNGDefaultTypeCheck:
2617 * @data: data needed for the library
2618 * @type: the type name
2619 * @value: the value to check
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002620 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002621 *
2622 * Check if the given type and value are validated by
2623 * the default datatype library.
2624 *
2625 * Returns 1 if yes, 0 if no and -1 in case of error.
2626 */
2627static int
2628xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002629 const xmlChar * type ATTRIBUTE_UNUSED,
2630 const xmlChar * value ATTRIBUTE_UNUSED,
2631 void **result ATTRIBUTE_UNUSED,
2632 xmlNodePtr node ATTRIBUTE_UNUSED)
2633{
Daniel Veillardd4310742003-02-18 21:12:46 +00002634 if (value == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002635 return (-1);
Daniel Veillardd4310742003-02-18 21:12:46 +00002636 if (xmlStrEqual(type, BAD_CAST "string"))
Daniel Veillard4c004142003-10-07 11:33:24 +00002637 return (1);
Daniel Veillardd4310742003-02-18 21:12:46 +00002638 if (xmlStrEqual(type, BAD_CAST "token")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002639 return (1);
Daniel Veillardd4310742003-02-18 21:12:46 +00002640 }
2641
Daniel Veillard4c004142003-10-07 11:33:24 +00002642 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002643}
2644
2645/**
2646 * xmlRelaxNGDefaultTypeCompare:
2647 * @data: data needed for the library
2648 * @type: the type name
2649 * @value1: the first value
2650 * @value2: the second value
2651 *
2652 * Compare two values accordingly a type from the default
2653 * datatype library.
2654 *
2655 * Returns 1 if yes, 0 if no and -1 in case of error.
2656 */
2657static int
2658xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002659 const xmlChar * type,
2660 const xmlChar * value1,
2661 xmlNodePtr ctxt1 ATTRIBUTE_UNUSED,
2662 void *comp1 ATTRIBUTE_UNUSED,
2663 const xmlChar * value2,
2664 xmlNodePtr ctxt2 ATTRIBUTE_UNUSED)
2665{
Daniel Veillardea3f3982003-01-26 19:45:18 +00002666 int ret = -1;
2667
2668 if (xmlStrEqual(type, BAD_CAST "string")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002669 ret = xmlStrEqual(value1, value2);
Daniel Veillardea3f3982003-01-26 19:45:18 +00002670 } else if (xmlStrEqual(type, BAD_CAST "token")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002671 if (!xmlStrEqual(value1, value2)) {
2672 xmlChar *nval, *nvalue;
Daniel Veillardea3f3982003-01-26 19:45:18 +00002673
Daniel Veillard4c004142003-10-07 11:33:24 +00002674 /*
2675 * TODO: trivial optimizations are possible by
2676 * computing at compile-time
2677 */
2678 nval = xmlRelaxNGNormalize(NULL, value1);
2679 nvalue = xmlRelaxNGNormalize(NULL, value2);
Daniel Veillardea3f3982003-01-26 19:45:18 +00002680
Daniel Veillard4c004142003-10-07 11:33:24 +00002681 if ((nval == NULL) || (nvalue == NULL))
2682 ret = -1;
2683 else if (xmlStrEqual(nval, nvalue))
2684 ret = 1;
2685 else
2686 ret = 0;
2687 if (nval != NULL)
2688 xmlFree(nval);
2689 if (nvalue != NULL)
2690 xmlFree(nvalue);
2691 } else
2692 ret = 1;
Daniel Veillardea3f3982003-01-26 19:45:18 +00002693 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002694 return (ret);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002695}
Daniel Veillard4c004142003-10-07 11:33:24 +00002696
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002697static int xmlRelaxNGTypeInitialized = 0;
2698static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL;
2699
2700/**
2701 * xmlRelaxNGFreeTypeLibrary:
2702 * @lib: the type library structure
2703 * @namespace: the URI bound to the library
2704 *
2705 * Free the structure associated to the type library
2706 */
Daniel Veillard6eadf632003-01-23 18:29:16 +00002707static void
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002708xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib,
Daniel Veillard4c004142003-10-07 11:33:24 +00002709 const xmlChar * namespace ATTRIBUTE_UNUSED)
2710{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002711 if (lib == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002712 return;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002713 if (lib->namespace != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002714 xmlFree((xmlChar *) lib->namespace);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002715 xmlFree(lib);
2716}
2717
2718/**
2719 * xmlRelaxNGRegisterTypeLibrary:
2720 * @namespace: the URI bound to the library
2721 * @data: data associated to the library
2722 * @have: the provide function
2723 * @check: the checking function
2724 * @comp: the comparison function
2725 *
2726 * Register a new type library
2727 *
2728 * Returns 0 in case of success and -1 in case of error.
2729 */
2730static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002731xmlRelaxNGRegisterTypeLibrary(const xmlChar * namespace, void *data,
2732 xmlRelaxNGTypeHave have,
2733 xmlRelaxNGTypeCheck check,
2734 xmlRelaxNGTypeCompare comp,
2735 xmlRelaxNGFacetCheck facet,
2736 xmlRelaxNGTypeFree freef)
2737{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002738 xmlRelaxNGTypeLibraryPtr lib;
2739 int ret;
2740
2741 if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00002742 (check == NULL) || (comp == NULL))
2743 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002744 if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002745 xmlGenericError(xmlGenericErrorContext,
2746 "Relax-NG types library '%s' already registered\n",
2747 namespace);
2748 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002749 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002750 lib =
2751 (xmlRelaxNGTypeLibraryPtr)
2752 xmlMalloc(sizeof(xmlRelaxNGTypeLibrary));
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002753 if (lib == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002754 xmlRngVErrMemory(NULL, "adding types library\n");
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002755 return (-1);
2756 }
2757 memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary));
2758 lib->namespace = xmlStrdup(namespace);
2759 lib->data = data;
2760 lib->have = have;
2761 lib->comp = comp;
2762 lib->check = check;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002763 lib->facet = facet;
2764 lib->freef = freef;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002765 ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib);
2766 if (ret < 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002767 xmlGenericError(xmlGenericErrorContext,
2768 "Relax-NG types library failed to register '%s'\n",
2769 namespace);
2770 xmlRelaxNGFreeTypeLibrary(lib, namespace);
2771 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002772 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002773 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002774}
2775
2776/**
2777 * xmlRelaxNGInitTypes:
2778 *
2779 * Initilize the default type libraries.
2780 *
2781 * Returns 0 in case of success and -1 in case of error.
2782 */
2783static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002784xmlRelaxNGInitTypes(void)
2785{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002786 if (xmlRelaxNGTypeInitialized != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002787 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002788 xmlRelaxNGRegisteredTypes = xmlHashCreate(10);
2789 if (xmlRelaxNGRegisteredTypes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002790 xmlGenericError(xmlGenericErrorContext,
2791 "Failed to allocate sh table for Relax-NG types\n");
2792 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002793 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002794 xmlRelaxNGRegisterTypeLibrary(BAD_CAST
2795 "http://www.w3.org/2001/XMLSchema-datatypes",
2796 NULL, xmlRelaxNGSchemaTypeHave,
2797 xmlRelaxNGSchemaTypeCheck,
2798 xmlRelaxNGSchemaTypeCompare,
2799 xmlRelaxNGSchemaFacetCheck,
2800 xmlRelaxNGSchemaFreeValue);
2801 xmlRelaxNGRegisterTypeLibrary(xmlRelaxNGNs, NULL,
2802 xmlRelaxNGDefaultTypeHave,
2803 xmlRelaxNGDefaultTypeCheck,
2804 xmlRelaxNGDefaultTypeCompare, NULL,
2805 NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002806 xmlRelaxNGTypeInitialized = 1;
Daniel Veillard4c004142003-10-07 11:33:24 +00002807 return (0);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002808}
2809
2810/**
2811 * xmlRelaxNGCleanupTypes:
2812 *
2813 * Cleanup the default Schemas type library associated to RelaxNG
2814 */
Daniel Veillard4c004142003-10-07 11:33:24 +00002815void
2816xmlRelaxNGCleanupTypes(void)
2817{
Daniel Veillarda84c0b32003-06-02 16:58:46 +00002818 xmlSchemaCleanupTypes();
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002819 if (xmlRelaxNGTypeInitialized == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002820 return;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002821 xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator)
Daniel Veillard4c004142003-10-07 11:33:24 +00002822 xmlRelaxNGFreeTypeLibrary);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002823 xmlRelaxNGTypeInitialized = 0;
Daniel Veillard6eadf632003-01-23 18:29:16 +00002824}
2825
2826/************************************************************************
2827 * *
Daniel Veillard952379b2003-03-17 15:37:12 +00002828 * Compiling element content into regexp *
2829 * *
2830 * Sometime the element content can be compiled into a pure regexp, *
2831 * This allows a faster execution and streamability at that level *
2832 * *
2833 ************************************************************************/
2834
Daniel Veillard52b48c72003-04-13 19:53:42 +00002835static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt,
2836 xmlRelaxNGDefinePtr def);
2837
Daniel Veillard952379b2003-03-17 15:37:12 +00002838/**
2839 * xmlRelaxNGIsCompileable:
2840 * @define: the definition to check
2841 *
2842 * Check if a definition is nullable.
2843 *
2844 * Returns 1 if yes, 0 if no and -1 in case of error
2845 */
2846static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002847xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def)
2848{
Daniel Veillard52b48c72003-04-13 19:53:42 +00002849 int ret = -1;
2850
Daniel Veillard952379b2003-03-17 15:37:12 +00002851 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002852 return (-1);
Daniel Veillard952379b2003-03-17 15:37:12 +00002853 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00002854 if ((def->type != XML_RELAXNG_ELEMENT) &&
2855 (def->dflags & IS_COMPILABLE))
Daniel Veillard4c004142003-10-07 11:33:24 +00002856 return (1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00002857 if ((def->type != XML_RELAXNG_ELEMENT) &&
2858 (def->dflags & IS_NOT_COMPILABLE))
Daniel Veillard4c004142003-10-07 11:33:24 +00002859 return (0);
2860 switch (def->type) {
Daniel Veillard952379b2003-03-17 15:37:12 +00002861 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00002862 ret = xmlRelaxNGIsCompileable(def->content);
2863 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002864 case XML_RELAXNG_TEXT:
Daniel Veillard952379b2003-03-17 15:37:12 +00002865 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00002866 ret = 1;
2867 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002868 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00002869 /*
2870 * Check if the element content is compileable
2871 */
2872 if (((def->dflags & IS_NOT_COMPILABLE) == 0) &&
2873 ((def->dflags & IS_COMPILABLE) == 0)) {
2874 xmlRelaxNGDefinePtr list;
2875
2876 list = def->content;
2877 while (list != NULL) {
2878 ret = xmlRelaxNGIsCompileable(list);
2879 if (ret != 1)
2880 break;
2881 list = list->next;
2882 }
2883 if (ret == 0)
2884 def->dflags |= IS_NOT_COMPILABLE;
2885 if (ret == 1)
2886 def->dflags |= IS_COMPILABLE;
Daniel Veillardd94849b2003-07-28 13:02:24 +00002887#ifdef DEBUG_COMPILE
Daniel Veillard4c004142003-10-07 11:33:24 +00002888 if (ret == 1) {
2889 xmlGenericError(xmlGenericErrorContext,
2890 "element content for %s is compilable\n",
2891 def->name);
2892 } else if (ret == 0) {
2893 xmlGenericError(xmlGenericErrorContext,
2894 "element content for %s is not compilable\n",
2895 def->name);
2896 } else {
2897 xmlGenericError(xmlGenericErrorContext,
2898 "Problem in RelaxNGIsCompileable for element %s\n",
2899 def->name);
2900 }
Daniel Veillardd94849b2003-07-28 13:02:24 +00002901#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002902 }
2903 /*
2904 * All elements return a compileable status unless they
2905 * are generic like anyName
2906 */
2907 if ((def->nameClass != NULL) || (def->name == NULL))
2908 ret = 0;
2909 else
2910 ret = 1;
2911 return (ret);
Daniel Veillard2134ab12003-07-23 19:56:29 +00002912 case XML_RELAXNG_REF:
2913 case XML_RELAXNG_EXTERNALREF:
2914 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00002915 if (def->depth == -20) {
2916 return (1);
2917 } else {
2918 xmlRelaxNGDefinePtr list;
Daniel Veillard2134ab12003-07-23 19:56:29 +00002919
Daniel Veillard4c004142003-10-07 11:33:24 +00002920 def->depth = -20;
2921 list = def->content;
2922 while (list != NULL) {
2923 ret = xmlRelaxNGIsCompileable(list);
2924 if (ret != 1)
2925 break;
2926 list = list->next;
2927 }
2928 }
2929 break;
Daniel Veillard2134ab12003-07-23 19:56:29 +00002930 case XML_RELAXNG_START:
Daniel Veillard952379b2003-03-17 15:37:12 +00002931 case XML_RELAXNG_OPTIONAL:
2932 case XML_RELAXNG_ZEROORMORE:
2933 case XML_RELAXNG_ONEORMORE:
2934 case XML_RELAXNG_CHOICE:
2935 case XML_RELAXNG_GROUP:
Daniel Veillard4c004142003-10-07 11:33:24 +00002936 case XML_RELAXNG_DEF:{
2937 xmlRelaxNGDefinePtr list;
Daniel Veillard952379b2003-03-17 15:37:12 +00002938
Daniel Veillard4c004142003-10-07 11:33:24 +00002939 list = def->content;
2940 while (list != NULL) {
2941 ret = xmlRelaxNGIsCompileable(list);
2942 if (ret != 1)
2943 break;
2944 list = list->next;
2945 }
2946 break;
2947 }
Daniel Veillard952379b2003-03-17 15:37:12 +00002948 case XML_RELAXNG_EXCEPT:
2949 case XML_RELAXNG_ATTRIBUTE:
2950 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard52b48c72003-04-13 19:53:42 +00002951 case XML_RELAXNG_DATATYPE:
2952 case XML_RELAXNG_LIST:
2953 case XML_RELAXNG_PARAM:
2954 case XML_RELAXNG_VALUE:
Daniel Veillard4c004142003-10-07 11:33:24 +00002955 ret = 0;
2956 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002957 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillard4c004142003-10-07 11:33:24 +00002958 ret = -1;
2959 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002960 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002961 if (ret == 0)
2962 def->dflags |= IS_NOT_COMPILABLE;
2963 if (ret == 1)
2964 def->dflags |= IS_COMPILABLE;
Daniel Veillardd94849b2003-07-28 13:02:24 +00002965#ifdef DEBUG_COMPILE
2966 if (ret == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002967 xmlGenericError(xmlGenericErrorContext,
2968 "RelaxNGIsCompileable %s : true\n",
2969 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00002970 } else if (ret == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002971 xmlGenericError(xmlGenericErrorContext,
2972 "RelaxNGIsCompileable %s : false\n",
2973 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00002974 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002975 xmlGenericError(xmlGenericErrorContext,
2976 "Problem in RelaxNGIsCompileable %s\n",
2977 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00002978 }
2979#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002980 return (ret);
Daniel Veillard52b48c72003-04-13 19:53:42 +00002981}
2982
2983/**
2984 * xmlRelaxNGCompile:
2985 * ctxt: the RelaxNG parser context
2986 * @define: the definition tree to compile
2987 *
2988 * Compile the set of definitions, it works recursively, till the
2989 * element boundaries, where it tries to compile the content if possible
2990 *
2991 * Returns 0 if success and -1 in case of error
2992 */
2993static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002994xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
2995{
Daniel Veillard52b48c72003-04-13 19:53:42 +00002996 int ret = 0;
2997 xmlRelaxNGDefinePtr list;
2998
Daniel Veillard4c004142003-10-07 11:33:24 +00002999 if ((ctxt == NULL) || (def == NULL))
3000 return (-1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003001
Daniel Veillard4c004142003-10-07 11:33:24 +00003002 switch (def->type) {
Daniel Veillard52b48c72003-04-13 19:53:42 +00003003 case XML_RELAXNG_START:
3004 if ((xmlRelaxNGIsCompileable(def) == 1) && (def->depth != -25)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003005 xmlAutomataPtr oldam = ctxt->am;
3006 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003007
3008 def->depth = -25;
3009
Daniel Veillard4c004142003-10-07 11:33:24 +00003010 list = def->content;
3011 ctxt->am = xmlNewAutomata();
3012 if (ctxt->am == NULL)
3013 return (-1);
3014 ctxt->state = xmlAutomataGetInitState(ctxt->am);
3015 while (list != NULL) {
3016 xmlRelaxNGCompile(ctxt, list);
3017 list = list->next;
3018 }
3019 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
3020 def->contModel = xmlAutomataCompile(ctxt->am);
3021 xmlRegexpIsDeterminist(def->contModel);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003022
Daniel Veillard4c004142003-10-07 11:33:24 +00003023 xmlFreeAutomata(ctxt->am);
3024 ctxt->state = oldstate;
3025 ctxt->am = oldam;
3026 }
3027 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003028 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003029 if ((ctxt->am != NULL) && (def->name != NULL)) {
3030 ctxt->state = xmlAutomataNewTransition2(ctxt->am,
3031 ctxt->state, NULL,
3032 def->name, def->ns,
3033 def);
3034 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003035 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003036 xmlAutomataPtr oldam = ctxt->am;
3037 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003038
3039 def->depth = -25;
3040
Daniel Veillard4c004142003-10-07 11:33:24 +00003041 list = def->content;
3042 ctxt->am = xmlNewAutomata();
3043 if (ctxt->am == NULL)
3044 return (-1);
3045 ctxt->state = xmlAutomataGetInitState(ctxt->am);
3046 while (list != NULL) {
3047 xmlRelaxNGCompile(ctxt, list);
3048 list = list->next;
3049 }
3050 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
3051 def->contModel = xmlAutomataCompile(ctxt->am);
3052 if (!xmlRegexpIsDeterminist(def->contModel)) {
3053 /*
3054 * we can only use the automata if it is determinist
3055 */
3056 xmlRegFreeRegexp(def->contModel);
3057 def->contModel = NULL;
3058 }
3059 xmlFreeAutomata(ctxt->am);
3060 ctxt->state = oldstate;
3061 ctxt->am = oldam;
3062 } else {
3063 xmlAutomataPtr oldam = ctxt->am;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003064
Daniel Veillard4c004142003-10-07 11:33:24 +00003065 /*
3066 * we can't build the content model for this element content
3067 * but it still might be possible to build it for some of its
3068 * children, recurse.
3069 */
3070 ret = xmlRelaxNGTryCompile(ctxt, def);
3071 ctxt->am = oldam;
3072 }
3073 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003074 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00003075 ret = xmlRelaxNGCompile(ctxt, def->content);
3076 break;
3077 case XML_RELAXNG_OPTIONAL:{
3078 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003079
Daniel Veillard4c004142003-10-07 11:33:24 +00003080 xmlRelaxNGCompile(ctxt, def->content);
3081 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
3082 break;
3083 }
3084 case XML_RELAXNG_ZEROORMORE:{
3085 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003086
Daniel Veillard4c004142003-10-07 11:33:24 +00003087 ctxt->state =
3088 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3089 oldstate = ctxt->state;
3090 list = def->content;
3091 while (list != NULL) {
3092 xmlRelaxNGCompile(ctxt, list);
3093 list = list->next;
3094 }
3095 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
3096 ctxt->state =
3097 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3098 break;
3099 }
3100 case XML_RELAXNG_ONEORMORE:{
3101 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003102
Daniel Veillard4c004142003-10-07 11:33:24 +00003103 list = def->content;
3104 while (list != NULL) {
3105 xmlRelaxNGCompile(ctxt, list);
3106 list = list->next;
3107 }
3108 oldstate = ctxt->state;
3109 list = def->content;
3110 while (list != NULL) {
3111 xmlRelaxNGCompile(ctxt, list);
3112 list = list->next;
3113 }
3114 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
3115 ctxt->state =
3116 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3117 break;
3118 }
3119 case XML_RELAXNG_CHOICE:{
3120 xmlAutomataStatePtr target = NULL;
3121 xmlAutomataStatePtr oldstate = ctxt->state;
3122
3123 list = def->content;
3124 while (list != NULL) {
3125 ctxt->state = oldstate;
3126 ret = xmlRelaxNGCompile(ctxt, list);
3127 if (ret != 0)
3128 break;
3129 if (target == NULL)
3130 target = ctxt->state;
3131 else {
3132 xmlAutomataNewEpsilon(ctxt->am, ctxt->state,
3133 target);
3134 }
3135 list = list->next;
3136 }
3137 ctxt->state = target;
3138
3139 break;
3140 }
Daniel Veillard2134ab12003-07-23 19:56:29 +00003141 case XML_RELAXNG_REF:
3142 case XML_RELAXNG_EXTERNALREF:
3143 case XML_RELAXNG_PARENTREF:
Daniel Veillard52b48c72003-04-13 19:53:42 +00003144 case XML_RELAXNG_GROUP:
3145 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00003146 list = def->content;
3147 while (list != NULL) {
3148 ret = xmlRelaxNGCompile(ctxt, list);
3149 if (ret != 0)
3150 break;
3151 list = list->next;
3152 }
3153 break;
3154 case XML_RELAXNG_TEXT:{
3155 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003156
Daniel Veillard4c004142003-10-07 11:33:24 +00003157 ctxt->state =
3158 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3159 oldstate = ctxt->state;
3160 xmlRelaxNGCompile(ctxt, def->content);
3161 xmlAutomataNewTransition(ctxt->am, ctxt->state,
3162 ctxt->state, BAD_CAST "#text",
3163 NULL);
3164 ctxt->state =
3165 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3166 break;
3167 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003168 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00003169 ctxt->state =
3170 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3171 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003172 case XML_RELAXNG_EXCEPT:
3173 case XML_RELAXNG_ATTRIBUTE:
3174 case XML_RELAXNG_INTERLEAVE:
3175 case XML_RELAXNG_NOT_ALLOWED:
3176 case XML_RELAXNG_DATATYPE:
3177 case XML_RELAXNG_LIST:
3178 case XML_RELAXNG_PARAM:
3179 case XML_RELAXNG_VALUE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003180 /* This should not happen and generate an internal error */
3181 fprintf(stderr, "RNG internal error trying to compile %s\n",
3182 xmlRelaxNGDefName(def));
3183 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003184 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003185 return (ret);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003186}
3187
3188/**
3189 * xmlRelaxNGTryCompile:
3190 * ctxt: the RelaxNG parser context
3191 * @define: the definition tree to compile
3192 *
3193 * Try to compile the set of definitions, it works recursively,
3194 * possibly ignoring parts which cannot be compiled.
3195 *
3196 * Returns 0 if success and -1 in case of error
3197 */
3198static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003199xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
3200{
Daniel Veillard52b48c72003-04-13 19:53:42 +00003201 int ret = 0;
3202 xmlRelaxNGDefinePtr list;
3203
Daniel Veillard4c004142003-10-07 11:33:24 +00003204 if ((ctxt == NULL) || (def == NULL))
3205 return (-1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003206
3207 if ((def->type == XML_RELAXNG_START) ||
3208 (def->type == XML_RELAXNG_ELEMENT)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003209 ret = xmlRelaxNGIsCompileable(def);
3210 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
3211 ctxt->am = NULL;
3212 ret = xmlRelaxNGCompile(ctxt, def);
Daniel Veillard2134ab12003-07-23 19:56:29 +00003213#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00003214 if (ret == 0) {
3215 if (def->type == XML_RELAXNG_START)
3216 xmlGenericError(xmlGenericErrorContext,
3217 "compiled the start\n");
3218 else
3219 xmlGenericError(xmlGenericErrorContext,
3220 "compiled element %s\n", def->name);
3221 } else {
3222 if (def->type == XML_RELAXNG_START)
3223 xmlGenericError(xmlGenericErrorContext,
3224 "failed to compile the start\n");
3225 else
3226 xmlGenericError(xmlGenericErrorContext,
3227 "failed to compile element %s\n",
3228 def->name);
3229 }
Daniel Veillard2134ab12003-07-23 19:56:29 +00003230#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00003231 return (ret);
3232 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003233 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003234 switch (def->type) {
Daniel Veillard52b48c72003-04-13 19:53:42 +00003235 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00003236 ret = xmlRelaxNGTryCompile(ctxt, def->content);
3237 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003238 case XML_RELAXNG_TEXT:
3239 case XML_RELAXNG_DATATYPE:
3240 case XML_RELAXNG_LIST:
3241 case XML_RELAXNG_PARAM:
3242 case XML_RELAXNG_VALUE:
3243 case XML_RELAXNG_EMPTY:
3244 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003245 ret = 0;
3246 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003247 case XML_RELAXNG_OPTIONAL:
3248 case XML_RELAXNG_ZEROORMORE:
3249 case XML_RELAXNG_ONEORMORE:
3250 case XML_RELAXNG_CHOICE:
3251 case XML_RELAXNG_GROUP:
3252 case XML_RELAXNG_DEF:
Daniel Veillard2134ab12003-07-23 19:56:29 +00003253 case XML_RELAXNG_START:
3254 case XML_RELAXNG_REF:
3255 case XML_RELAXNG_EXTERNALREF:
3256 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00003257 list = def->content;
3258 while (list != NULL) {
3259 ret = xmlRelaxNGTryCompile(ctxt, list);
3260 if (ret != 0)
3261 break;
3262 list = list->next;
3263 }
3264 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003265 case XML_RELAXNG_EXCEPT:
3266 case XML_RELAXNG_ATTRIBUTE:
3267 case XML_RELAXNG_INTERLEAVE:
3268 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillard4c004142003-10-07 11:33:24 +00003269 ret = 0;
3270 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003271 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003272 return (ret);
Daniel Veillard952379b2003-03-17 15:37:12 +00003273}
3274
3275/************************************************************************
3276 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00003277 * Parsing functions *
3278 * *
3279 ************************************************************************/
3280
Daniel Veillard4c004142003-10-07 11:33:24 +00003281static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr
3282 ctxt, xmlNodePtr node);
3283static xmlRelaxNGDefinePtr xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr
3284 ctxt, xmlNodePtr node);
3285static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr
3286 ctxt, xmlNodePtr nodes,
3287 int group);
3288static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr
3289 ctxt, xmlNodePtr node);
3290static xmlRelaxNGPtr xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt,
3291 xmlNodePtr node);
3292static int xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
3293 xmlNodePtr nodes);
3294static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr
3295 ctxt, xmlNodePtr node,
3296 xmlRelaxNGDefinePtr
3297 def);
3298static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr
3299 ctxt, xmlNodePtr nodes);
3300static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
3301 xmlRelaxNGDefinePtr define,
3302 xmlNodePtr elem);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003303
3304
Daniel Veillard249d7bb2003-03-19 21:02:29 +00003305#define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content))
Daniel Veillard6eadf632003-01-23 18:29:16 +00003306
3307/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00003308 * xmlRelaxNGIsNullable:
3309 * @define: the definition to verify
3310 *
3311 * Check if a definition is nullable.
3312 *
3313 * Returns 1 if yes, 0 if no and -1 in case of error
3314 */
3315static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003316xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define)
3317{
Daniel Veillardfd573f12003-03-16 17:52:32 +00003318 int ret;
Daniel Veillard4c004142003-10-07 11:33:24 +00003319
Daniel Veillardfd573f12003-03-16 17:52:32 +00003320 if (define == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003321 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003322
Daniel Veillarde063f482003-03-21 16:53:17 +00003323 if (define->dflags & IS_NULLABLE)
Daniel Veillard4c004142003-10-07 11:33:24 +00003324 return (1);
Daniel Veillarde063f482003-03-21 16:53:17 +00003325 if (define->dflags & IS_NOT_NULLABLE)
Daniel Veillard4c004142003-10-07 11:33:24 +00003326 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003327 switch (define->type) {
3328 case XML_RELAXNG_EMPTY:
3329 case XML_RELAXNG_TEXT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003330 ret = 1;
3331 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003332 case XML_RELAXNG_NOOP:
3333 case XML_RELAXNG_DEF:
3334 case XML_RELAXNG_REF:
3335 case XML_RELAXNG_EXTERNALREF:
3336 case XML_RELAXNG_PARENTREF:
3337 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003338 ret = xmlRelaxNGIsNullable(define->content);
3339 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003340 case XML_RELAXNG_EXCEPT:
3341 case XML_RELAXNG_NOT_ALLOWED:
3342 case XML_RELAXNG_ELEMENT:
3343 case XML_RELAXNG_DATATYPE:
3344 case XML_RELAXNG_PARAM:
3345 case XML_RELAXNG_VALUE:
3346 case XML_RELAXNG_LIST:
3347 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003348 ret = 0;
3349 break;
3350 case XML_RELAXNG_CHOICE:{
3351 xmlRelaxNGDefinePtr list = define->content;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003352
Daniel Veillard4c004142003-10-07 11:33:24 +00003353 while (list != NULL) {
3354 ret = xmlRelaxNGIsNullable(list);
3355 if (ret != 0)
3356 goto done;
3357 list = list->next;
3358 }
3359 ret = 0;
3360 break;
3361 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003362 case XML_RELAXNG_START:
3363 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003364 case XML_RELAXNG_GROUP:{
3365 xmlRelaxNGDefinePtr list = define->content;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003366
Daniel Veillard4c004142003-10-07 11:33:24 +00003367 while (list != NULL) {
3368 ret = xmlRelaxNGIsNullable(list);
3369 if (ret != 1)
3370 goto done;
3371 list = list->next;
3372 }
3373 return (1);
3374 }
3375 default:
3376 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003377 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003378 done:
Daniel Veillardfd573f12003-03-16 17:52:32 +00003379 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003380 define->dflags |= IS_NOT_NULLABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003381 if (ret == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00003382 define->dflags |= IS_NULLABLE;
3383 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003384}
3385
3386/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00003387 * xmlRelaxNGIsBlank:
3388 * @str: a string
3389 *
3390 * Check if a string is ignorable c.f. 4.2. Whitespace
3391 *
3392 * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
3393 */
3394static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003395xmlRelaxNGIsBlank(xmlChar * str)
3396{
Daniel Veillard6eadf632003-01-23 18:29:16 +00003397 if (str == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003398 return (1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003399 while (*str != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00003400 if (!(IS_BLANK_CH(*str)))
Daniel Veillard4c004142003-10-07 11:33:24 +00003401 return (0);
3402 str++;
Daniel Veillard6eadf632003-01-23 18:29:16 +00003403 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003404 return (1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003405}
3406
Daniel Veillard6eadf632003-01-23 18:29:16 +00003407/**
3408 * xmlRelaxNGGetDataTypeLibrary:
3409 * @ctxt: a Relax-NG parser context
3410 * @node: the current data or value element
3411 *
3412 * Applies algorithm from 4.3. datatypeLibrary attribute
3413 *
3414 * Returns the datatypeLibary value or NULL if not found
3415 */
3416static xmlChar *
3417xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00003418 xmlNodePtr node)
3419{
Daniel Veillard6eadf632003-01-23 18:29:16 +00003420 xmlChar *ret, *escape;
3421
Daniel Veillard6eadf632003-01-23 18:29:16 +00003422 if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003423 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3424 if (ret != NULL) {
3425 if (ret[0] == 0) {
3426 xmlFree(ret);
3427 return (NULL);
3428 }
3429 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3430 if (escape == NULL) {
3431 return (ret);
3432 }
3433 xmlFree(ret);
3434 return (escape);
3435 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00003436 }
3437 node = node->parent;
3438 while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003439 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3440 if (ret != NULL) {
3441 if (ret[0] == 0) {
3442 xmlFree(ret);
3443 return (NULL);
3444 }
3445 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3446 if (escape == NULL) {
3447 return (ret);
3448 }
3449 xmlFree(ret);
3450 return (escape);
3451 }
3452 node = node->parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00003453 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003454 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003455}
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003456
3457/**
Daniel Veillardedc91922003-01-26 00:52:04 +00003458 * xmlRelaxNGParseValue:
3459 * @ctxt: a Relax-NG parser context
3460 * @node: the data node.
3461 *
3462 * parse the content of a RelaxNG value node.
3463 *
3464 * Returns the definition pointer or NULL in case of error
3465 */
3466static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00003467xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
3468{
Daniel Veillardedc91922003-01-26 00:52:04 +00003469 xmlRelaxNGDefinePtr def = NULL;
Daniel Veillard5f1946a2003-03-31 16:38:16 +00003470 xmlRelaxNGTypeLibraryPtr lib = NULL;
Daniel Veillardedc91922003-01-26 00:52:04 +00003471 xmlChar *type;
3472 xmlChar *library;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003473 int success = 0;
Daniel Veillardedc91922003-01-26 00:52:04 +00003474
Daniel Veillardfd573f12003-03-16 17:52:32 +00003475 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillardedc91922003-01-26 00:52:04 +00003476 if (def == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003477 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003478 def->type = XML_RELAXNG_VALUE;
Daniel Veillardedc91922003-01-26 00:52:04 +00003479
3480 type = xmlGetProp(node, BAD_CAST "type");
3481 if (type != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003482 xmlRelaxNGNormExtSpace(type);
3483 if (xmlValidateNCName(type, 0)) {
3484 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE,
3485 "value type '%s' is not an NCName\n", type, NULL);
3486 }
3487 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3488 if (library == NULL)
3489 library =
3490 xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
Daniel Veillardedc91922003-01-26 00:52:04 +00003491
Daniel Veillard4c004142003-10-07 11:33:24 +00003492 def->name = type;
3493 def->ns = library;
Daniel Veillardedc91922003-01-26 00:52:04 +00003494
Daniel Veillard4c004142003-10-07 11:33:24 +00003495 lib = (xmlRelaxNGTypeLibraryPtr)
3496 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
3497 if (lib == NULL) {
3498 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
3499 "Use of unregistered type library '%s'\n", library,
3500 NULL);
3501 def->data = NULL;
3502 } else {
3503 def->data = lib;
3504 if (lib->have == NULL) {
3505 xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
3506 "Internal error with type library '%s': no 'have'\n",
3507 library, NULL);
3508 } else {
3509 success = lib->have(lib->data, def->name);
3510 if (success != 1) {
3511 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND,
3512 "Error type '%s' is not exported by type library '%s'\n",
3513 def->name, library);
3514 }
3515 }
3516 }
Daniel Veillardedc91922003-01-26 00:52:04 +00003517 }
3518 if (node->children == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003519 def->value = xmlStrdup(BAD_CAST "");
Daniel Veillard39eb88b2003-03-11 11:21:28 +00003520 } else if (((node->children->type != XML_TEXT_NODE) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00003521 (node->children->type != XML_CDATA_SECTION_NODE)) ||
3522 (node->children->next != NULL)) {
3523 xmlRngPErr(ctxt, node, XML_RNGP_TEXT_EXPECTED,
3524 "Expecting a single text value for <value>content\n",
3525 NULL, NULL);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003526 } else if (def != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003527 def->value = xmlNodeGetContent(node);
3528 if (def->value == NULL) {
3529 xmlRngPErr(ctxt, node, XML_RNGP_VALUE_NO_CONTENT,
3530 "Element <value> has no content\n", NULL, NULL);
3531 } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) {
3532 void *val = NULL;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003533
Daniel Veillard4c004142003-10-07 11:33:24 +00003534 success =
3535 lib->check(lib->data, def->name, def->value, &val, node);
3536 if (success != 1) {
3537 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_VALUE,
3538 "Value '%s' is not acceptable for type '%s'\n",
3539 def->value, def->name);
3540 } else {
3541 if (val != NULL)
3542 def->attrs = val;
3543 }
3544 }
Daniel Veillardedc91922003-01-26 00:52:04 +00003545 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003546 return (def);
Daniel Veillardedc91922003-01-26 00:52:04 +00003547}
3548
3549/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003550 * xmlRelaxNGParseData:
3551 * @ctxt: a Relax-NG parser context
3552 * @node: the data node.
3553 *
3554 * parse the content of a RelaxNG data node.
3555 *
3556 * Returns the definition pointer or NULL in case of error
3557 */
3558static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00003559xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
3560{
Daniel Veillard416589a2003-02-17 17:25:42 +00003561 xmlRelaxNGDefinePtr def = NULL, except, last = NULL;
Daniel Veillard8fe98712003-02-19 00:19:14 +00003562 xmlRelaxNGDefinePtr param, lastparam = NULL;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003563 xmlRelaxNGTypeLibraryPtr lib;
3564 xmlChar *type;
3565 xmlChar *library;
3566 xmlNodePtr content;
3567 int tmp;
3568
3569 type = xmlGetProp(node, BAD_CAST "type");
3570 if (type == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003571 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_MISSING, "data has no type\n", NULL,
3572 NULL);
3573 return (NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003574 }
Daniel Veillardd2298792003-02-14 16:54:11 +00003575 xmlRelaxNGNormExtSpace(type);
3576 if (xmlValidateNCName(type, 0)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003577 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE,
3578 "data type '%s' is not an NCName\n", type, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00003579 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003580 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3581 if (library == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003582 library =
3583 xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003584
Daniel Veillardfd573f12003-03-16 17:52:32 +00003585 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003586 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003587 xmlFree(type);
3588 return (NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003589 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003590 def->type = XML_RELAXNG_DATATYPE;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003591 def->name = type;
3592 def->ns = library;
3593
3594 lib = (xmlRelaxNGTypeLibraryPtr)
Daniel Veillard4c004142003-10-07 11:33:24 +00003595 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003596 if (lib == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003597 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
3598 "Use of unregistered type library '%s'\n", library,
3599 NULL);
3600 def->data = NULL;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003601 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003602 def->data = lib;
3603 if (lib->have == NULL) {
3604 xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
3605 "Internal error with type library '%s': no 'have'\n",
3606 library, NULL);
3607 } else {
3608 tmp = lib->have(lib->data, def->name);
3609 if (tmp != 1) {
3610 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND,
3611 "Error type '%s' is not exported by type library '%s'\n",
3612 def->name, library);
3613 } else
3614 if ((xmlStrEqual
3615 (library,
3616 BAD_CAST
3617 "http://www.w3.org/2001/XMLSchema-datatypes"))
3618 && ((xmlStrEqual(def->name, BAD_CAST "IDREF"))
3619 || (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) {
3620 ctxt->idref = 1;
3621 }
3622 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003623 }
3624 content = node->children;
Daniel Veillard416589a2003-02-17 17:25:42 +00003625
3626 /*
3627 * Handle optional params
3628 */
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003629 while (content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003630 if (!xmlStrEqual(content->name, BAD_CAST "param"))
3631 break;
3632 if (xmlStrEqual(library,
3633 BAD_CAST "http://relaxng.org/ns/structure/1.0")) {
3634 xmlRngPErr(ctxt, node, XML_RNGP_PARAM_FORBIDDEN,
3635 "Type library '%s' does not allow type parameters\n",
3636 library, NULL);
3637 content = content->next;
3638 while ((content != NULL) &&
3639 (xmlStrEqual(content->name, BAD_CAST "param")))
3640 content = content->next;
3641 } else {
3642 param = xmlRelaxNGNewDefine(ctxt, node);
3643 if (param != NULL) {
3644 param->type = XML_RELAXNG_PARAM;
3645 param->name = xmlGetProp(content, BAD_CAST "name");
3646 if (param->name == NULL) {
3647 xmlRngPErr(ctxt, node, XML_RNGP_PARAM_NAME_MISSING,
3648 "param has no name\n", NULL, NULL);
3649 }
3650 param->value = xmlNodeGetContent(content);
3651 if (lastparam == NULL) {
3652 def->attrs = lastparam = param;
3653 } else {
3654 lastparam->next = param;
3655 lastparam = param;
3656 }
3657 if (lib != NULL) {
3658 }
3659 }
3660 content = content->next;
3661 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003662 }
Daniel Veillard416589a2003-02-17 17:25:42 +00003663 /*
3664 * Handle optional except
3665 */
Daniel Veillard4c004142003-10-07 11:33:24 +00003666 if ((content != NULL)
3667 && (xmlStrEqual(content->name, BAD_CAST "except"))) {
3668 xmlNodePtr child;
3669 xmlRelaxNGDefinePtr tmp2, last2 = NULL;
Daniel Veillard416589a2003-02-17 17:25:42 +00003670
Daniel Veillard4c004142003-10-07 11:33:24 +00003671 except = xmlRelaxNGNewDefine(ctxt, node);
3672 if (except == NULL) {
3673 return (def);
3674 }
3675 except->type = XML_RELAXNG_EXCEPT;
3676 child = content->children;
3677 if (last == NULL) {
3678 def->content = except;
3679 } else {
3680 last->next = except;
3681 }
3682 if (child == NULL) {
3683 xmlRngPErr(ctxt, content, XML_RNGP_EXCEPT_NO_CONTENT,
3684 "except has no content\n", NULL, NULL);
3685 }
3686 while (child != NULL) {
3687 tmp2 = xmlRelaxNGParsePattern(ctxt, child);
3688 if (tmp2 != NULL) {
3689 if (last2 == NULL) {
3690 except->content = last2 = tmp2;
3691 } else {
3692 last2->next = tmp2;
3693 last2 = tmp2;
3694 }
3695 }
3696 child = child->next;
3697 }
3698 content = content->next;
Daniel Veillard416589a2003-02-17 17:25:42 +00003699 }
3700 /*
3701 * Check there is no unhandled data
3702 */
3703 if (content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003704 xmlRngPErr(ctxt, content, XML_RNGP_DATA_CONTENT,
3705 "Element data has unexpected content %s\n",
3706 content->name, NULL);
Daniel Veillard416589a2003-02-17 17:25:42 +00003707 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003708
Daniel Veillard4c004142003-10-07 11:33:24 +00003709 return (def);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003710}
3711
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003712static const xmlChar *invalidName = BAD_CAST "\1";
3713
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003714/**
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003715 * xmlRelaxNGCompareNameClasses:
3716 * @defs1: the first element/attribute defs
3717 * @defs2: the second element/attribute defs
3718 * @name: the restriction on the name
3719 * @ns: the restriction on the namespace
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003720 *
3721 * Compare the 2 lists of element definitions. The comparison is
3722 * that if both lists do not accept the same QNames, it returns 1
3723 * If the 2 lists can accept the same QName the comparison returns 0
3724 *
3725 * Returns 1 disttinct, 0 if equal
3726 */
3727static int
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003728xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1,
Daniel Veillard4c004142003-10-07 11:33:24 +00003729 xmlRelaxNGDefinePtr def2)
3730{
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003731 int ret = 1;
3732 xmlNode node;
3733 xmlNs ns;
3734 xmlRelaxNGValidCtxt ctxt;
Daniel Veillard4c004142003-10-07 11:33:24 +00003735
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003736 ctxt.flags = FLAGS_IGNORABLE;
3737
Daniel Veillard42f12e92003-03-07 18:32:59 +00003738 memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt));
3739
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003740 if ((def1->type == XML_RELAXNG_ELEMENT) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00003741 (def1->type == XML_RELAXNG_ATTRIBUTE)) {
3742 if (def2->type == XML_RELAXNG_TEXT)
3743 return (1);
3744 if (def1->name != NULL) {
3745 node.name = def1->name;
3746 } else {
3747 node.name = invalidName;
3748 }
3749 node.ns = &ns;
3750 if (def1->ns != NULL) {
3751 if (def1->ns[0] == 0) {
3752 node.ns = NULL;
3753 } else {
3754 ns.href = def1->ns;
3755 }
3756 } else {
3757 ns.href = invalidName;
3758 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003759 if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003760 if (def1->nameClass != NULL) {
3761 ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2);
3762 } else {
3763 ret = 0;
3764 }
3765 } else {
3766 ret = 1;
3767 }
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003768 } else if (def1->type == XML_RELAXNG_TEXT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003769 if (def2->type == XML_RELAXNG_TEXT)
3770 return (0);
3771 return (1);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003772 } else if (def1->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003773 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003774 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003775 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003776 }
3777 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003778 return (ret);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003779 if ((def2->type == XML_RELAXNG_ELEMENT) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00003780 (def2->type == XML_RELAXNG_ATTRIBUTE)) {
3781 if (def2->name != NULL) {
3782 node.name = def2->name;
3783 } else {
3784 node.name = invalidName;
3785 }
3786 node.ns = &ns;
3787 if (def2->ns != NULL) {
3788 if (def2->ns[0] == 0) {
3789 node.ns = NULL;
3790 } else {
3791 ns.href = def2->ns;
3792 }
3793 } else {
3794 ns.href = invalidName;
3795 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003796 if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003797 if (def2->nameClass != NULL) {
3798 ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1);
3799 } else {
3800 ret = 0;
3801 }
3802 } else {
3803 ret = 1;
3804 }
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003805 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003806 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003807 }
3808
Daniel Veillard4c004142003-10-07 11:33:24 +00003809 return (ret);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003810}
3811
3812/**
3813 * xmlRelaxNGCompareElemDefLists:
3814 * @ctxt: a Relax-NG parser context
3815 * @defs1: the first list of element/attribute defs
3816 * @defs2: the second list of element/attribute defs
3817 *
3818 * Compare the 2 lists of element or attribute definitions. The comparison
3819 * is that if both lists do not accept the same QNames, it returns 1
3820 * If the 2 lists can accept the same QName the comparison returns 0
3821 *
3822 * Returns 1 disttinct, 0 if equal
3823 */
3824static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003825xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt
3826 ATTRIBUTE_UNUSED, xmlRelaxNGDefinePtr * def1,
3827 xmlRelaxNGDefinePtr * def2)
3828{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003829 xmlRelaxNGDefinePtr *basedef2 = def2;
Daniel Veillard4c004142003-10-07 11:33:24 +00003830
Daniel Veillard154877e2003-01-30 12:17:05 +00003831 if ((def1 == NULL) || (def2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00003832 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003833 if ((*def1 == NULL) || (*def2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00003834 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003835 while (*def1 != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003836 while ((*def2) != NULL) {
3837 if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0)
3838 return (0);
3839 def2++;
3840 }
3841 def2 = basedef2;
3842 def1++;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003843 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003844 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003845}
3846
3847/**
Daniel Veillardce192eb2003-04-16 15:58:05 +00003848 * xmlRelaxNGGenerateAttributes:
3849 * @ctxt: a Relax-NG parser context
3850 * @def: the definition definition
3851 *
3852 * Check if the definition can only generate attributes
3853 *
3854 * Returns 1 if yes, 0 if no and -1 in case of error.
3855 */
3856static int
3857xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00003858 xmlRelaxNGDefinePtr def)
3859{
Daniel Veillardce192eb2003-04-16 15:58:05 +00003860 xmlRelaxNGDefinePtr parent, cur, tmp;
3861
3862 /*
3863 * Don't run that check in case of error. Infinite recursion
3864 * becomes possible.
3865 */
3866 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003867 return (-1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003868
3869 parent = NULL;
3870 cur = def;
3871 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003872 if ((cur->type == XML_RELAXNG_ELEMENT) ||
3873 (cur->type == XML_RELAXNG_TEXT) ||
3874 (cur->type == XML_RELAXNG_DATATYPE) ||
3875 (cur->type == XML_RELAXNG_PARAM) ||
3876 (cur->type == XML_RELAXNG_LIST) ||
3877 (cur->type == XML_RELAXNG_VALUE) ||
3878 (cur->type == XML_RELAXNG_EMPTY))
3879 return (0);
3880 if ((cur->type == XML_RELAXNG_CHOICE) ||
3881 (cur->type == XML_RELAXNG_INTERLEAVE) ||
3882 (cur->type == XML_RELAXNG_GROUP) ||
3883 (cur->type == XML_RELAXNG_ONEORMORE) ||
3884 (cur->type == XML_RELAXNG_ZEROORMORE) ||
3885 (cur->type == XML_RELAXNG_OPTIONAL) ||
3886 (cur->type == XML_RELAXNG_PARENTREF) ||
3887 (cur->type == XML_RELAXNG_EXTERNALREF) ||
3888 (cur->type == XML_RELAXNG_REF) ||
3889 (cur->type == XML_RELAXNG_DEF)) {
3890 if (cur->content != NULL) {
3891 parent = cur;
3892 cur = cur->content;
3893 tmp = cur;
3894 while (tmp != NULL) {
3895 tmp->parent = parent;
3896 tmp = tmp->next;
3897 }
3898 continue;
3899 }
3900 }
3901 if (cur == def)
3902 break;
3903 if (cur->next != NULL) {
3904 cur = cur->next;
3905 continue;
3906 }
3907 do {
3908 cur = cur->parent;
3909 if (cur == NULL)
3910 break;
3911 if (cur == def)
3912 return (1);
3913 if (cur->next != NULL) {
3914 cur = cur->next;
3915 break;
3916 }
3917 } while (cur != NULL);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003918 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003919 return (1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003920}
Daniel Veillard4c004142003-10-07 11:33:24 +00003921
Daniel Veillardce192eb2003-04-16 15:58:05 +00003922/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003923 * xmlRelaxNGGetElements:
3924 * @ctxt: a Relax-NG parser context
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003925 * @def: the definition definition
3926 * @eora: gather elements (0) or attributes (1)
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003927 *
3928 * Compute the list of top elements a definition can generate
3929 *
3930 * Returns a list of elements or NULL if none was found.
3931 */
3932static xmlRelaxNGDefinePtr *
3933xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00003934 xmlRelaxNGDefinePtr def, int eora)
3935{
Daniel Veillardfd573f12003-03-16 17:52:32 +00003936 xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003937 int len = 0;
3938 int max = 0;
3939
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003940 /*
3941 * Don't run that check in case of error. Infinite recursion
3942 * becomes possible.
3943 */
3944 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003945 return (NULL);
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003946
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003947 parent = NULL;
3948 cur = def;
3949 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003950 if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) ||
3951 (cur->type == XML_RELAXNG_TEXT))) ||
3952 ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) {
3953 if (ret == NULL) {
3954 max = 10;
3955 ret = (xmlRelaxNGDefinePtr *)
3956 xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr));
3957 if (ret == NULL) {
3958 xmlRngPErrMemory(ctxt, "getting element list\n");
3959 return (NULL);
3960 }
3961 } else if (max <= len) {
3962 max *= 2;
3963 ret =
3964 xmlRealloc(ret,
3965 (max + 1) * sizeof(xmlRelaxNGDefinePtr));
3966 if (ret == NULL) {
3967 xmlRngPErrMemory(ctxt, "getting element list\n");
3968 return (NULL);
3969 }
3970 }
3971 ret[len++] = cur;
3972 ret[len] = NULL;
3973 } else if ((cur->type == XML_RELAXNG_CHOICE) ||
3974 (cur->type == XML_RELAXNG_INTERLEAVE) ||
3975 (cur->type == XML_RELAXNG_GROUP) ||
3976 (cur->type == XML_RELAXNG_ONEORMORE) ||
3977 (cur->type == XML_RELAXNG_ZEROORMORE) ||
3978 (cur->type == XML_RELAXNG_OPTIONAL) ||
3979 (cur->type == XML_RELAXNG_PARENTREF) ||
3980 (cur->type == XML_RELAXNG_REF) ||
William M. Brack236c8c02004-03-20 11:32:36 +00003981 (cur->type == XML_RELAXNG_DEF) ||
3982 (cur->type == XML_RELAXNG_EXTERNALREF)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003983 /*
3984 * Don't go within elements or attributes or string values.
3985 * Just gather the element top list
3986 */
3987 if (cur->content != NULL) {
3988 parent = cur;
3989 cur = cur->content;
3990 tmp = cur;
3991 while (tmp != NULL) {
3992 tmp->parent = parent;
3993 tmp = tmp->next;
3994 }
3995 continue;
3996 }
3997 }
3998 if (cur == def)
3999 break;
4000 if (cur->next != NULL) {
4001 cur = cur->next;
4002 continue;
4003 }
4004 do {
4005 cur = cur->parent;
4006 if (cur == NULL)
4007 break;
4008 if (cur == def)
4009 return (ret);
4010 if (cur->next != NULL) {
4011 cur = cur->next;
4012 break;
4013 }
4014 } while (cur != NULL);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004015 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004016 return (ret);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004017}
Daniel Veillard4c004142003-10-07 11:33:24 +00004018
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004019/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00004020 * xmlRelaxNGCheckChoiceDeterminism:
4021 * @ctxt: a Relax-NG parser context
4022 * @def: the choice definition
4023 *
4024 * Also used to find indeterministic pattern in choice
4025 */
4026static void
4027xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00004028 xmlRelaxNGDefinePtr def)
4029{
Daniel Veillardfd573f12003-03-16 17:52:32 +00004030 xmlRelaxNGDefinePtr **list;
4031 xmlRelaxNGDefinePtr cur;
4032 int nbchild = 0, i, j, ret;
4033 int is_nullable = 0;
4034 int is_indeterminist = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004035 xmlHashTablePtr triage = NULL;
4036 int is_triable = 1;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004037
Daniel Veillard4c004142003-10-07 11:33:24 +00004038 if ((def == NULL) || (def->type != XML_RELAXNG_CHOICE))
4039 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004040
Daniel Veillarde063f482003-03-21 16:53:17 +00004041 if (def->dflags & IS_PROCESSED)
Daniel Veillard4c004142003-10-07 11:33:24 +00004042 return;
Daniel Veillarde063f482003-03-21 16:53:17 +00004043
Daniel Veillardfd573f12003-03-16 17:52:32 +00004044 /*
4045 * Don't run that check in case of error. Infinite recursion
4046 * becomes possible.
4047 */
4048 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004049 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004050
4051 is_nullable = xmlRelaxNGIsNullable(def);
4052
4053 cur = def->content;
4054 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004055 nbchild++;
4056 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004057 }
4058
4059 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
Daniel Veillard4c004142003-10-07 11:33:24 +00004060 sizeof(xmlRelaxNGDefinePtr
4061 *));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004062 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004063 xmlRngPErrMemory(ctxt, "building choice\n");
4064 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004065 }
4066 i = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004067 /*
4068 * a bit strong but safe
4069 */
4070 if (is_nullable == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004071 triage = xmlHashCreate(10);
Daniel Veillarde063f482003-03-21 16:53:17 +00004072 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004073 is_triable = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004074 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004075 cur = def->content;
4076 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004077 list[i] = xmlRelaxNGGetElements(ctxt, cur, 0);
4078 if ((list[i] == NULL) || (list[i][0] == NULL)) {
4079 is_triable = 0;
4080 } else if (is_triable == 1) {
4081 xmlRelaxNGDefinePtr *tmp;
4082 int res;
Daniel Veillarde063f482003-03-21 16:53:17 +00004083
Daniel Veillard4c004142003-10-07 11:33:24 +00004084 tmp = list[i];
4085 while ((*tmp != NULL) && (is_triable == 1)) {
4086 if ((*tmp)->type == XML_RELAXNG_TEXT) {
4087 res = xmlHashAddEntry2(triage,
4088 BAD_CAST "#text", NULL,
4089 (void *) cur);
4090 if (res != 0)
4091 is_triable = -1;
4092 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
4093 ((*tmp)->name != NULL)) {
4094 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4095 res = xmlHashAddEntry2(triage,
4096 (*tmp)->name, NULL,
4097 (void *) cur);
4098 else
4099 res = xmlHashAddEntry2(triage,
4100 (*tmp)->name, (*tmp)->ns,
4101 (void *) cur);
4102 if (res != 0)
4103 is_triable = -1;
4104 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
4105 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4106 res = xmlHashAddEntry2(triage,
4107 BAD_CAST "#any", NULL,
4108 (void *) cur);
4109 else
4110 res = xmlHashAddEntry2(triage,
4111 BAD_CAST "#any", (*tmp)->ns,
4112 (void *) cur);
4113 if (res != 0)
4114 is_triable = -1;
4115 } else {
4116 is_triable = -1;
4117 }
4118 tmp++;
4119 }
4120 }
4121 i++;
4122 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004123 }
4124
Daniel Veillard4c004142003-10-07 11:33:24 +00004125 for (i = 0; i < nbchild; i++) {
4126 if (list[i] == NULL)
4127 continue;
4128 for (j = 0; j < i; j++) {
4129 if (list[j] == NULL)
4130 continue;
4131 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4132 if (ret == 0) {
4133 is_indeterminist = 1;
4134 }
4135 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004136 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004137 for (i = 0; i < nbchild; i++) {
4138 if (list[i] != NULL)
4139 xmlFree(list[i]);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004140 }
4141
4142 xmlFree(list);
4143 if (is_indeterminist) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004144 def->dflags |= IS_INDETERMINIST;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004145 }
Daniel Veillarde063f482003-03-21 16:53:17 +00004146 if (is_triable == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004147 def->dflags |= IS_TRIABLE;
4148 def->data = triage;
Daniel Veillarde063f482003-03-21 16:53:17 +00004149 } else if (triage != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004150 xmlHashFree(triage, NULL);
Daniel Veillarde063f482003-03-21 16:53:17 +00004151 }
4152 def->dflags |= IS_PROCESSED;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004153}
4154
4155/**
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004156 * xmlRelaxNGCheckGroupAttrs:
4157 * @ctxt: a Relax-NG parser context
4158 * @def: the group definition
4159 *
4160 * Detects violations of rule 7.3
4161 */
4162static void
4163xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00004164 xmlRelaxNGDefinePtr def)
4165{
Daniel Veillardfd573f12003-03-16 17:52:32 +00004166 xmlRelaxNGDefinePtr **list;
4167 xmlRelaxNGDefinePtr cur;
4168 int nbchild = 0, i, j, ret;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004169
4170 if ((def == NULL) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00004171 ((def->type != XML_RELAXNG_GROUP) &&
4172 (def->type != XML_RELAXNG_ELEMENT)))
4173 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004174
Daniel Veillarde063f482003-03-21 16:53:17 +00004175 if (def->dflags & IS_PROCESSED)
Daniel Veillard4c004142003-10-07 11:33:24 +00004176 return;
Daniel Veillarde063f482003-03-21 16:53:17 +00004177
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004178 /*
4179 * Don't run that check in case of error. Infinite recursion
4180 * becomes possible.
4181 */
4182 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004183 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004184
Daniel Veillardfd573f12003-03-16 17:52:32 +00004185 cur = def->attrs;
4186 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004187 nbchild++;
4188 cur = cur->next;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004189 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004190 cur = def->content;
4191 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004192 nbchild++;
4193 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004194 }
4195
4196 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
Daniel Veillard4c004142003-10-07 11:33:24 +00004197 sizeof(xmlRelaxNGDefinePtr
4198 *));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004199 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004200 xmlRngPErrMemory(ctxt, "building group\n");
4201 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004202 }
4203 i = 0;
4204 cur = def->attrs;
4205 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004206 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4207 i++;
4208 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004209 }
4210 cur = def->content;
4211 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004212 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4213 i++;
4214 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004215 }
4216
Daniel Veillard4c004142003-10-07 11:33:24 +00004217 for (i = 0; i < nbchild; i++) {
4218 if (list[i] == NULL)
4219 continue;
4220 for (j = 0; j < i; j++) {
4221 if (list[j] == NULL)
4222 continue;
4223 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4224 if (ret == 0) {
4225 xmlRngPErr(ctxt, def->node, XML_RNGP_GROUP_ATTR_CONFLICT,
4226 "Attributes conflicts in group\n", NULL, NULL);
4227 }
4228 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004229 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004230 for (i = 0; i < nbchild; i++) {
4231 if (list[i] != NULL)
4232 xmlFree(list[i]);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004233 }
4234
4235 xmlFree(list);
Daniel Veillarde063f482003-03-21 16:53:17 +00004236 def->dflags |= IS_PROCESSED;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004237}
4238
4239/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00004240 * xmlRelaxNGComputeInterleaves:
4241 * @def: the interleave definition
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004242 * @ctxt: a Relax-NG parser context
Daniel Veillardfd573f12003-03-16 17:52:32 +00004243 * @name: the definition name
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004244 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00004245 * A lot of work for preprocessing interleave definitions
4246 * is potentially needed to get a decent execution speed at runtime
4247 * - trying to get a total order on the element nodes generated
4248 * by the interleaves, order the list of interleave definitions
4249 * following that order.
4250 * - if <text/> is used to handle mixed content, it is better to
4251 * flag this in the define and simplify the runtime checking
4252 * algorithm
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004253 */
4254static void
Daniel Veillardfd573f12003-03-16 17:52:32 +00004255xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def,
Daniel Veillard4c004142003-10-07 11:33:24 +00004256 xmlRelaxNGParserCtxtPtr ctxt,
4257 xmlChar * name ATTRIBUTE_UNUSED)
4258{
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004259 xmlRelaxNGDefinePtr cur, *tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004260
Daniel Veillardfd573f12003-03-16 17:52:32 +00004261 xmlRelaxNGPartitionPtr partitions = NULL;
4262 xmlRelaxNGInterleaveGroupPtr *groups = NULL;
4263 xmlRelaxNGInterleaveGroupPtr group;
Daniel Veillard4c004142003-10-07 11:33:24 +00004264 int i, j, ret, res;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004265 int nbgroups = 0;
4266 int nbchild = 0;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00004267 int is_mixed = 0;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004268 int is_determinist = 1;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004269
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004270 /*
4271 * Don't run that check in case of error. Infinite recursion
4272 * becomes possible.
4273 */
4274 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004275 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004276
Daniel Veillardfd573f12003-03-16 17:52:32 +00004277#ifdef DEBUG_INTERLEAVE
4278 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00004279 "xmlRelaxNGComputeInterleaves(%s)\n", name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004280#endif
4281 cur = def->content;
4282 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004283 nbchild++;
4284 cur = cur->next;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004285 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004286
Daniel Veillardfd573f12003-03-16 17:52:32 +00004287#ifdef DEBUG_INTERLEAVE
4288 xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild);
4289#endif
4290 groups = (xmlRelaxNGInterleaveGroupPtr *)
Daniel Veillard4c004142003-10-07 11:33:24 +00004291 xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004292 if (groups == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00004293 goto error;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004294 cur = def->content;
4295 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004296 groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr)
4297 xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup));
4298 if (groups[nbgroups] == NULL)
4299 goto error;
4300 if (cur->type == XML_RELAXNG_TEXT)
4301 is_mixed++;
4302 groups[nbgroups]->rule = cur;
4303 groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0);
4304 groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1);
4305 nbgroups++;
4306 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004307 }
4308#ifdef DEBUG_INTERLEAVE
4309 xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups);
4310#endif
4311
4312 /*
4313 * Let's check that all rules makes a partitions according to 7.4
4314 */
4315 partitions = (xmlRelaxNGPartitionPtr)
Daniel Veillard4c004142003-10-07 11:33:24 +00004316 xmlMalloc(sizeof(xmlRelaxNGPartition));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004317 if (partitions == NULL)
4318 goto error;
Daniel Veillard20863822003-03-22 17:51:47 +00004319 memset(partitions, 0, sizeof(xmlRelaxNGPartition));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004320 partitions->nbgroups = nbgroups;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004321 partitions->triage = xmlHashCreate(nbgroups);
Daniel Veillard4c004142003-10-07 11:33:24 +00004322 for (i = 0; i < nbgroups; i++) {
4323 group = groups[i];
4324 for (j = i + 1; j < nbgroups; j++) {
4325 if (groups[j] == NULL)
4326 continue;
4327
4328 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs,
4329 groups[j]->defs);
4330 if (ret == 0) {
4331 xmlRngPErr(ctxt, def->node, XML_RNGP_ELEM_TEXT_CONFLICT,
4332 "Element or text conflicts in interleave\n",
4333 NULL, NULL);
4334 }
4335 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs,
4336 groups[j]->attrs);
4337 if (ret == 0) {
4338 xmlRngPErr(ctxt, def->node, XML_RNGP_ATTR_CONFLICT,
4339 "Attributes conflicts in interleave\n", NULL,
4340 NULL);
4341 }
4342 }
4343 tmp = group->defs;
4344 if ((tmp != NULL) && (*tmp != NULL)) {
4345 while (*tmp != NULL) {
4346 if ((*tmp)->type == XML_RELAXNG_TEXT) {
4347 res = xmlHashAddEntry2(partitions->triage,
4348 BAD_CAST "#text", NULL,
4349 (void *) (long) (i + 1));
4350 if (res != 0)
4351 is_determinist = -1;
4352 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
4353 ((*tmp)->name != NULL)) {
4354 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4355 res = xmlHashAddEntry2(partitions->triage,
4356 (*tmp)->name, NULL,
4357 (void *) (long) (i + 1));
4358 else
4359 res = xmlHashAddEntry2(partitions->triage,
4360 (*tmp)->name, (*tmp)->ns,
4361 (void *) (long) (i + 1));
4362 if (res != 0)
4363 is_determinist = -1;
4364 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
4365 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4366 res = xmlHashAddEntry2(partitions->triage,
4367 BAD_CAST "#any", NULL,
4368 (void *) (long) (i + 1));
4369 else
4370 res = xmlHashAddEntry2(partitions->triage,
4371 BAD_CAST "#any", (*tmp)->ns,
4372 (void *) (long) (i + 1));
4373 if ((*tmp)->nameClass != NULL)
4374 is_determinist = 2;
4375 if (res != 0)
4376 is_determinist = -1;
4377 } else {
4378 is_determinist = -1;
4379 }
4380 tmp++;
4381 }
4382 } else {
4383 is_determinist = 0;
4384 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004385 }
4386 partitions->groups = groups;
4387
4388 /*
4389 * and save the partition list back in the def
4390 */
4391 def->data = partitions;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00004392 if (is_mixed != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004393 def->dflags |= IS_MIXED;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004394 if (is_determinist == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00004395 partitions->flags = IS_DETERMINIST;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004396 if (is_determinist == 2)
Daniel Veillard4c004142003-10-07 11:33:24 +00004397 partitions->flags = IS_DETERMINIST | IS_NEEDCHECK;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004398 return;
4399
Daniel Veillard4c004142003-10-07 11:33:24 +00004400 error:
4401 xmlRngPErrMemory(ctxt, "in interleave computation\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00004402 if (groups != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004403 for (i = 0; i < nbgroups; i++)
4404 if (groups[i] != NULL) {
4405 if (groups[i]->defs != NULL)
4406 xmlFree(groups[i]->defs);
4407 xmlFree(groups[i]);
4408 }
4409 xmlFree(groups);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004410 }
4411 xmlRelaxNGFreePartition(partitions);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004412}
4413
4414/**
4415 * xmlRelaxNGParseInterleave:
4416 * @ctxt: a Relax-NG parser context
4417 * @node: the data node.
4418 *
4419 * parse the content of a RelaxNG interleave node.
4420 *
4421 * Returns the definition pointer or NULL in case of error
4422 */
4423static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004424xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4425{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004426 xmlRelaxNGDefinePtr def = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004427 xmlRelaxNGDefinePtr last = NULL, cur;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004428 xmlNodePtr child;
4429
Daniel Veillardfd573f12003-03-16 17:52:32 +00004430 def = xmlRelaxNGNewDefine(ctxt, node);
4431 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004432 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004433 }
4434 def->type = XML_RELAXNG_INTERLEAVE;
4435
4436 if (ctxt->interleaves == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00004437 ctxt->interleaves = xmlHashCreate(10);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004438 if (ctxt->interleaves == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004439 xmlRngPErrMemory(ctxt, "create interleaves\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00004440 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004441 char name[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00004442
Daniel Veillard4c004142003-10-07 11:33:24 +00004443 snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++);
4444 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) {
4445 xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_ADD,
4446 "Failed to add %s to hash table\n",
4447 (const xmlChar *) name, NULL);
4448 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004449 }
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004450 child = node->children;
Daniel Veillardd2298792003-02-14 16:54:11 +00004451 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004452 xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_NO_CONTENT,
4453 "Element interleave is empty\n", NULL, NULL);
Daniel Veillard1564e6e2003-03-15 21:30:25 +00004454 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004455 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004456 if (IS_RELAXNG(child, "element")) {
4457 cur = xmlRelaxNGParseElement(ctxt, child);
4458 } else {
4459 cur = xmlRelaxNGParsePattern(ctxt, child);
4460 }
4461 if (cur != NULL) {
4462 cur->parent = def;
4463 if (last == NULL) {
4464 def->content = last = cur;
4465 } else {
4466 last->next = cur;
4467 last = cur;
4468 }
4469 }
4470 child = child->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004471 }
4472
Daniel Veillard4c004142003-10-07 11:33:24 +00004473 return (def);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004474}
Daniel Veillard6eadf632003-01-23 18:29:16 +00004475
4476/**
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004477 * xmlRelaxNGParseInclude:
4478 * @ctxt: a Relax-NG parser context
4479 * @node: the include node
4480 *
4481 * Integrate the content of an include node in the current grammar
4482 *
4483 * Returns 0 in case of success or -1 in case of error
4484 */
4485static int
Daniel Veillard4c004142003-10-07 11:33:24 +00004486xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4487{
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004488 xmlRelaxNGIncludePtr incl;
4489 xmlNodePtr root;
4490 int ret = 0, tmp;
4491
Daniel Veillard807daf82004-02-22 22:13:27 +00004492 incl = node->psvi;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004493 if (incl == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004494 xmlRngPErr(ctxt, node, XML_RNGP_INCLUDE_EMPTY,
4495 "Include node has no data\n", NULL, NULL);
4496 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004497 }
4498 root = xmlDocGetRootElement(incl->doc);
4499 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004500 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, "Include document is empty\n",
4501 NULL, NULL);
4502 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004503 }
4504 if (!xmlStrEqual(root->name, BAD_CAST "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004505 xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING,
4506 "Include document root is not a grammar\n", NULL, NULL);
4507 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004508 }
4509
4510 /*
4511 * Merge the definition from both the include and the internal list
4512 */
4513 if (root->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004514 tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children);
4515 if (tmp != 0)
4516 ret = -1;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004517 }
4518 if (node->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004519 tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children);
4520 if (tmp != 0)
4521 ret = -1;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004522 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004523 return (ret);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004524}
4525
4526/**
Daniel Veillard276be4a2003-01-24 01:03:34 +00004527 * xmlRelaxNGParseDefine:
4528 * @ctxt: a Relax-NG parser context
4529 * @node: the define node
4530 *
4531 * parse the content of a RelaxNG define element node.
4532 *
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004533 * Returns 0 in case of success or -1 in case of error
Daniel Veillard276be4a2003-01-24 01:03:34 +00004534 */
4535static int
Daniel Veillard4c004142003-10-07 11:33:24 +00004536xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4537{
Daniel Veillard276be4a2003-01-24 01:03:34 +00004538 xmlChar *name;
4539 int ret = 0, tmp;
4540 xmlRelaxNGDefinePtr def;
4541 const xmlChar *olddefine;
4542
4543 name = xmlGetProp(node, BAD_CAST "name");
4544 if (name == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004545 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_NAME_MISSING,
4546 "define has no name\n", NULL, NULL);
Daniel Veillard276be4a2003-01-24 01:03:34 +00004547 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004548 xmlRelaxNGNormExtSpace(name);
4549 if (xmlValidateNCName(name, 0)) {
4550 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_DEFINE_NAME,
4551 "define name '%s' is not an NCName\n", name, NULL);
4552 }
4553 def = xmlRelaxNGNewDefine(ctxt, node);
4554 if (def == NULL) {
4555 xmlFree(name);
4556 return (-1);
4557 }
4558 def->type = XML_RELAXNG_DEF;
4559 def->name = name;
4560 if (node->children == NULL) {
4561 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_EMPTY,
4562 "define has no children\n", NULL, NULL);
4563 } else {
4564 olddefine = ctxt->define;
4565 ctxt->define = name;
4566 def->content =
4567 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4568 ctxt->define = olddefine;
4569 }
4570 if (ctxt->grammar->defs == NULL)
4571 ctxt->grammar->defs = xmlHashCreate(10);
4572 if (ctxt->grammar->defs == NULL) {
4573 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
4574 "Could not create definition hash\n", NULL, NULL);
4575 ret = -1;
4576 } else {
4577 tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def);
4578 if (tmp < 0) {
4579 xmlRelaxNGDefinePtr prev;
Daniel Veillard154877e2003-01-30 12:17:05 +00004580
Daniel Veillard4c004142003-10-07 11:33:24 +00004581 prev = xmlHashLookup(ctxt->grammar->defs, name);
4582 if (prev == NULL) {
4583 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
4584 "Internal error on define aggregation of %s\n",
4585 name, NULL);
4586 ret = -1;
4587 } else {
4588 while (prev->nextHash != NULL)
4589 prev = prev->nextHash;
4590 prev->nextHash = def;
4591 }
4592 }
4593 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00004594 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004595 return (ret);
Daniel Veillard276be4a2003-01-24 01:03:34 +00004596}
4597
4598/**
Daniel Veillardfebcca42003-02-16 15:44:18 +00004599 * xmlRelaxNGProcessExternalRef:
4600 * @ctxt: the parser context
4601 * @node: the externlRef node
4602 *
4603 * Process and compile an externlRef node
4604 *
4605 * Returns the xmlRelaxNGDefinePtr or NULL in case of error
4606 */
4607static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004608xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4609{
Daniel Veillardfebcca42003-02-16 15:44:18 +00004610 xmlRelaxNGDocumentPtr docu;
4611 xmlNodePtr root, tmp;
4612 xmlChar *ns;
Daniel Veillard77648bb2003-02-20 15:03:22 +00004613 int newNs = 0, oldflags;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004614 xmlRelaxNGDefinePtr def;
4615
Daniel Veillard807daf82004-02-22 22:13:27 +00004616 docu = node->psvi;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004617 if (docu != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004618 def = xmlRelaxNGNewDefine(ctxt, node);
4619 if (def == NULL)
4620 return (NULL);
4621 def->type = XML_RELAXNG_EXTERNALREF;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004622
Daniel Veillard4c004142003-10-07 11:33:24 +00004623 if (docu->content == NULL) {
4624 /*
4625 * Then do the parsing for good
4626 */
4627 root = xmlDocGetRootElement(docu->doc);
4628 if (root == NULL) {
4629 xmlRngPErr(ctxt, node, XML_RNGP_EXTERNALREF_EMTPY,
4630 "xmlRelaxNGParse: %s is empty\n", ctxt->URL,
4631 NULL);
4632 return (NULL);
4633 }
4634 /*
4635 * ns transmission rules
4636 */
4637 ns = xmlGetProp(root, BAD_CAST "ns");
4638 if (ns == NULL) {
4639 tmp = node;
4640 while ((tmp != NULL) && (tmp->type == XML_ELEMENT_NODE)) {
4641 ns = xmlGetProp(tmp, BAD_CAST "ns");
4642 if (ns != NULL) {
4643 break;
4644 }
4645 tmp = tmp->parent;
4646 }
4647 if (ns != NULL) {
4648 xmlSetProp(root, BAD_CAST "ns", ns);
4649 newNs = 1;
4650 xmlFree(ns);
4651 }
4652 } else {
4653 xmlFree(ns);
4654 }
Daniel Veillardfebcca42003-02-16 15:44:18 +00004655
Daniel Veillard4c004142003-10-07 11:33:24 +00004656 /*
4657 * Parsing to get a precompiled schemas.
4658 */
4659 oldflags = ctxt->flags;
4660 ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF;
4661 docu->schema = xmlRelaxNGParseDocument(ctxt, root);
4662 ctxt->flags = oldflags;
4663 if ((docu->schema != NULL) &&
4664 (docu->schema->topgrammar != NULL)) {
4665 docu->content = docu->schema->topgrammar->start;
4666 }
4667
4668 /*
4669 * the externalRef may be reused in a different ns context
4670 */
4671 if (newNs == 1) {
4672 xmlUnsetProp(root, BAD_CAST "ns");
4673 }
4674 }
4675 def->content = docu->content;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004676 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004677 def = NULL;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004678 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004679 return (def);
Daniel Veillardfebcca42003-02-16 15:44:18 +00004680}
4681
4682/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00004683 * xmlRelaxNGParsePattern:
4684 * @ctxt: a Relax-NG parser context
4685 * @node: the pattern node.
4686 *
4687 * parse the content of a RelaxNG pattern node.
4688 *
Daniel Veillard276be4a2003-01-24 01:03:34 +00004689 * Returns the definition pointer or NULL in case of error or if no
4690 * pattern is generated.
Daniel Veillard6eadf632003-01-23 18:29:16 +00004691 */
4692static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004693xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4694{
Daniel Veillard6eadf632003-01-23 18:29:16 +00004695 xmlRelaxNGDefinePtr def = NULL;
4696
Daniel Veillardd2298792003-02-14 16:54:11 +00004697 if (node == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004698 return (NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00004699 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004700 if (IS_RELAXNG(node, "element")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004701 def = xmlRelaxNGParseElement(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004702 } else if (IS_RELAXNG(node, "attribute")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004703 def = xmlRelaxNGParseAttribute(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004704 } else if (IS_RELAXNG(node, "empty")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004705 def = xmlRelaxNGNewDefine(ctxt, node);
4706 if (def == NULL)
4707 return (NULL);
4708 def->type = XML_RELAXNG_EMPTY;
4709 if (node->children != NULL) {
4710 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_NOT_EMPTY,
4711 "empty: had a child node\n", NULL, NULL);
4712 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004713 } else if (IS_RELAXNG(node, "text")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004714 def = xmlRelaxNGNewDefine(ctxt, node);
4715 if (def == NULL)
4716 return (NULL);
4717 def->type = XML_RELAXNG_TEXT;
4718 if (node->children != NULL) {
4719 xmlRngPErr(ctxt, node, XML_RNGP_TEXT_HAS_CHILD,
4720 "text: had a child node\n", NULL, NULL);
4721 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004722 } else if (IS_RELAXNG(node, "zeroOrMore")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004723 def = xmlRelaxNGNewDefine(ctxt, node);
4724 if (def == NULL)
4725 return (NULL);
4726 def->type = XML_RELAXNG_ZEROORMORE;
4727 if (node->children == NULL) {
4728 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4729 "Element %s is empty\n", node->name, NULL);
4730 } else {
4731 def->content =
4732 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4733 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004734 } else if (IS_RELAXNG(node, "oneOrMore")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004735 def = xmlRelaxNGNewDefine(ctxt, node);
4736 if (def == NULL)
4737 return (NULL);
4738 def->type = XML_RELAXNG_ONEORMORE;
4739 if (node->children == NULL) {
4740 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4741 "Element %s is empty\n", node->name, NULL);
4742 } else {
4743 def->content =
4744 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4745 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004746 } else if (IS_RELAXNG(node, "optional")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004747 def = xmlRelaxNGNewDefine(ctxt, node);
4748 if (def == NULL)
4749 return (NULL);
4750 def->type = XML_RELAXNG_OPTIONAL;
4751 if (node->children == NULL) {
4752 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4753 "Element %s is empty\n", node->name, NULL);
4754 } else {
4755 def->content =
4756 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4757 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004758 } else if (IS_RELAXNG(node, "choice")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004759 def = xmlRelaxNGNewDefine(ctxt, node);
4760 if (def == NULL)
4761 return (NULL);
4762 def->type = XML_RELAXNG_CHOICE;
4763 if (node->children == NULL) {
4764 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4765 "Element %s is empty\n", node->name, NULL);
4766 } else {
4767 def->content =
4768 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4769 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004770 } else if (IS_RELAXNG(node, "group")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004771 def = xmlRelaxNGNewDefine(ctxt, node);
4772 if (def == NULL)
4773 return (NULL);
4774 def->type = XML_RELAXNG_GROUP;
4775 if (node->children == NULL) {
4776 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4777 "Element %s is empty\n", node->name, NULL);
4778 } else {
4779 def->content =
4780 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4781 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004782 } else if (IS_RELAXNG(node, "ref")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004783 def = xmlRelaxNGNewDefine(ctxt, node);
4784 if (def == NULL)
4785 return (NULL);
4786 def->type = XML_RELAXNG_REF;
4787 def->name = xmlGetProp(node, BAD_CAST "name");
4788 if (def->name == NULL) {
4789 xmlRngPErr(ctxt, node, XML_RNGP_REF_NO_NAME, "ref has no name\n",
4790 NULL, NULL);
4791 } else {
4792 xmlRelaxNGNormExtSpace(def->name);
4793 if (xmlValidateNCName(def->name, 0)) {
4794 xmlRngPErr(ctxt, node, XML_RNGP_REF_NAME_INVALID,
4795 "ref name '%s' is not an NCName\n", def->name,
4796 NULL);
4797 }
4798 }
4799 if (node->children != NULL) {
4800 xmlRngPErr(ctxt, node, XML_RNGP_REF_NOT_EMPTY, "ref is not empty\n",
4801 NULL, NULL);
4802 }
4803 if (ctxt->grammar->refs == NULL)
4804 ctxt->grammar->refs = xmlHashCreate(10);
4805 if (ctxt->grammar->refs == NULL) {
4806 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4807 "Could not create references hash\n", NULL, NULL);
4808 def = NULL;
4809 } else {
4810 int tmp;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004811
Daniel Veillard4c004142003-10-07 11:33:24 +00004812 tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def);
4813 if (tmp < 0) {
4814 xmlRelaxNGDefinePtr prev;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004815
Daniel Veillard4c004142003-10-07 11:33:24 +00004816 prev = (xmlRelaxNGDefinePtr)
4817 xmlHashLookup(ctxt->grammar->refs, def->name);
4818 if (prev == NULL) {
4819 if (def->name != NULL) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00004820 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4821 "Error refs definitions '%s'\n",
4822 def->name, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00004823 } else {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00004824 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4825 "Error refs definitions\n",
4826 NULL, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00004827 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004828 def = NULL;
4829 } else {
4830 def->nextHash = prev->nextHash;
4831 prev->nextHash = def;
4832 }
4833 }
4834 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00004835 } else if (IS_RELAXNG(node, "data")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004836 def = xmlRelaxNGParseData(ctxt, node);
Daniel Veillardedc91922003-01-26 00:52:04 +00004837 } else if (IS_RELAXNG(node, "value")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004838 def = xmlRelaxNGParseValue(ctxt, node);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00004839 } else if (IS_RELAXNG(node, "list")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004840 def = xmlRelaxNGNewDefine(ctxt, node);
4841 if (def == NULL)
4842 return (NULL);
4843 def->type = XML_RELAXNG_LIST;
4844 if (node->children == NULL) {
4845 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4846 "Element %s is empty\n", node->name, NULL);
4847 } else {
4848 def->content =
4849 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4850 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004851 } else if (IS_RELAXNG(node, "interleave")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004852 def = xmlRelaxNGParseInterleave(ctxt, node);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00004853 } else if (IS_RELAXNG(node, "externalRef")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004854 def = xmlRelaxNGProcessExternalRef(ctxt, node);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004855 } else if (IS_RELAXNG(node, "notAllowed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004856 def = xmlRelaxNGNewDefine(ctxt, node);
4857 if (def == NULL)
4858 return (NULL);
4859 def->type = XML_RELAXNG_NOT_ALLOWED;
4860 if (node->children != NULL) {
4861 xmlRngPErr(ctxt, node, XML_RNGP_NOTALLOWED_NOT_EMPTY,
4862 "xmlRelaxNGParse: notAllowed element is not empty\n",
4863 NULL, NULL);
4864 }
Daniel Veillard419a7682003-02-03 23:22:49 +00004865 } else if (IS_RELAXNG(node, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004866 xmlRelaxNGGrammarPtr grammar, old;
4867 xmlRelaxNGGrammarPtr oldparent;
Daniel Veillard419a7682003-02-03 23:22:49 +00004868
Daniel Veillardc482e262003-02-26 14:48:48 +00004869#ifdef DEBUG_GRAMMAR
Daniel Veillard4c004142003-10-07 11:33:24 +00004870 xmlGenericError(xmlGenericErrorContext,
4871 "Found <grammar> pattern\n");
Daniel Veillardc482e262003-02-26 14:48:48 +00004872#endif
4873
Daniel Veillard4c004142003-10-07 11:33:24 +00004874 oldparent = ctxt->parentgrammar;
4875 old = ctxt->grammar;
4876 ctxt->parentgrammar = old;
4877 grammar = xmlRelaxNGParseGrammar(ctxt, node->children);
4878 if (old != NULL) {
4879 ctxt->grammar = old;
4880 ctxt->parentgrammar = oldparent;
Daniel Veillardc482e262003-02-26 14:48:48 +00004881#if 0
Daniel Veillard4c004142003-10-07 11:33:24 +00004882 if (grammar != NULL) {
4883 grammar->next = old->next;
4884 old->next = grammar;
4885 }
Daniel Veillardc482e262003-02-26 14:48:48 +00004886#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00004887 }
4888 if (grammar != NULL)
4889 def = grammar->start;
4890 else
4891 def = NULL;
Daniel Veillard419a7682003-02-03 23:22:49 +00004892 } else if (IS_RELAXNG(node, "parentRef")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004893 if (ctxt->parentgrammar == NULL) {
4894 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_PARENT,
4895 "Use of parentRef without a parent grammar\n", NULL,
4896 NULL);
4897 return (NULL);
4898 }
4899 def = xmlRelaxNGNewDefine(ctxt, node);
4900 if (def == NULL)
4901 return (NULL);
4902 def->type = XML_RELAXNG_PARENTREF;
4903 def->name = xmlGetProp(node, BAD_CAST "name");
4904 if (def->name == NULL) {
4905 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_NAME,
4906 "parentRef has no name\n", NULL, NULL);
4907 } else {
4908 xmlRelaxNGNormExtSpace(def->name);
4909 if (xmlValidateNCName(def->name, 0)) {
4910 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NAME_INVALID,
4911 "parentRef name '%s' is not an NCName\n",
4912 def->name, NULL);
4913 }
4914 }
4915 if (node->children != NULL) {
4916 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NOT_EMPTY,
4917 "parentRef is not empty\n", NULL, NULL);
4918 }
4919 if (ctxt->parentgrammar->refs == NULL)
4920 ctxt->parentgrammar->refs = xmlHashCreate(10);
4921 if (ctxt->parentgrammar->refs == NULL) {
4922 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
4923 "Could not create references hash\n", NULL, NULL);
4924 def = NULL;
4925 } else if (def->name != NULL) {
4926 int tmp;
Daniel Veillard419a7682003-02-03 23:22:49 +00004927
Daniel Veillard4c004142003-10-07 11:33:24 +00004928 tmp =
4929 xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def);
4930 if (tmp < 0) {
4931 xmlRelaxNGDefinePtr prev;
Daniel Veillard419a7682003-02-03 23:22:49 +00004932
Daniel Veillard4c004142003-10-07 11:33:24 +00004933 prev = (xmlRelaxNGDefinePtr)
4934 xmlHashLookup(ctxt->parentgrammar->refs, def->name);
4935 if (prev == NULL) {
4936 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
4937 "Internal error parentRef definitions '%s'\n",
4938 def->name, NULL);
4939 def = NULL;
4940 } else {
4941 def->nextHash = prev->nextHash;
4942 prev->nextHash = def;
4943 }
4944 }
4945 }
Daniel Veillardd2298792003-02-14 16:54:11 +00004946 } else if (IS_RELAXNG(node, "mixed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004947 if (node->children == NULL) {
4948 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, "Mixed is empty\n",
4949 NULL, NULL);
4950 def = NULL;
4951 } else {
4952 def = xmlRelaxNGParseInterleave(ctxt, node);
4953 if (def != NULL) {
4954 xmlRelaxNGDefinePtr tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004955
Daniel Veillard4c004142003-10-07 11:33:24 +00004956 if ((def->content != NULL) && (def->content->next != NULL)) {
4957 tmp = xmlRelaxNGNewDefine(ctxt, node);
4958 if (tmp != NULL) {
4959 tmp->type = XML_RELAXNG_GROUP;
4960 tmp->content = def->content;
4961 def->content = tmp;
4962 }
4963 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004964
Daniel Veillard4c004142003-10-07 11:33:24 +00004965 tmp = xmlRelaxNGNewDefine(ctxt, node);
4966 if (tmp == NULL)
4967 return (def);
4968 tmp->type = XML_RELAXNG_TEXT;
4969 tmp->next = def->content;
4970 def->content = tmp;
4971 }
4972 }
Daniel Veillardd2298792003-02-14 16:54:11 +00004973 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004974 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_CONSTRUCT,
4975 "Unexpected node %s is not a pattern\n", node->name,
4976 NULL);
4977 def = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004978 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004979 return (def);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004980}
4981
4982/**
4983 * xmlRelaxNGParseAttribute:
4984 * @ctxt: a Relax-NG parser context
4985 * @node: the element node
4986 *
4987 * parse the content of a RelaxNG attribute node.
4988 *
4989 * Returns the definition pointer or NULL in case of error.
4990 */
4991static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004992xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4993{
Daniel Veillardd2298792003-02-14 16:54:11 +00004994 xmlRelaxNGDefinePtr ret, cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004995 xmlNodePtr child;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004996 int old_flags;
4997
Daniel Veillardfd573f12003-03-16 17:52:32 +00004998 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004999 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005000 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005001 ret->type = XML_RELAXNG_ATTRIBUTE;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005002 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005003 child = node->children;
5004 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005005 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_EMPTY,
5006 "xmlRelaxNGParseattribute: attribute has no children\n",
5007 NULL, NULL);
5008 return (ret);
5009 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005010 old_flags = ctxt->flags;
5011 ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005012 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
5013 if (cur != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005014 child = child->next;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005015
Daniel Veillardd2298792003-02-14 16:54:11 +00005016 if (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005017 cur = xmlRelaxNGParsePattern(ctxt, child);
5018 if (cur != NULL) {
5019 switch (cur->type) {
5020 case XML_RELAXNG_EMPTY:
5021 case XML_RELAXNG_NOT_ALLOWED:
5022 case XML_RELAXNG_TEXT:
5023 case XML_RELAXNG_ELEMENT:
5024 case XML_RELAXNG_DATATYPE:
5025 case XML_RELAXNG_VALUE:
5026 case XML_RELAXNG_LIST:
5027 case XML_RELAXNG_REF:
5028 case XML_RELAXNG_PARENTREF:
5029 case XML_RELAXNG_EXTERNALREF:
5030 case XML_RELAXNG_DEF:
5031 case XML_RELAXNG_ONEORMORE:
5032 case XML_RELAXNG_ZEROORMORE:
5033 case XML_RELAXNG_OPTIONAL:
5034 case XML_RELAXNG_CHOICE:
5035 case XML_RELAXNG_GROUP:
5036 case XML_RELAXNG_INTERLEAVE:
5037 case XML_RELAXNG_ATTRIBUTE:
5038 ret->content = cur;
5039 cur->parent = ret;
5040 break;
5041 case XML_RELAXNG_START:
5042 case XML_RELAXNG_PARAM:
5043 case XML_RELAXNG_EXCEPT:
5044 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CONTENT,
5045 "attribute has invalid content\n", NULL,
5046 NULL);
5047 break;
5048 case XML_RELAXNG_NOOP:
5049 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_NOOP,
5050 "RNG Internal error, noop found in attribute\n",
5051 NULL, NULL);
5052 break;
5053 }
5054 }
5055 child = child->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005056 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005057 if (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005058 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CHILDREN,
5059 "attribute has multiple children\n", NULL, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005060 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005061 ctxt->flags = old_flags;
Daniel Veillard4c004142003-10-07 11:33:24 +00005062 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005063}
5064
5065/**
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005066 * xmlRelaxNGParseExceptNameClass:
5067 * @ctxt: a Relax-NG parser context
5068 * @node: the except node
Daniel Veillard144fae12003-02-03 13:17:57 +00005069 * @attr: 1 if within an attribute, 0 if within an element
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005070 *
5071 * parse the content of a RelaxNG nameClass node.
5072 *
5073 * Returns the definition pointer or NULL in case of error.
5074 */
5075static xmlRelaxNGDefinePtr
Daniel Veillard144fae12003-02-03 13:17:57 +00005076xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00005077 xmlNodePtr node, int attr)
5078{
Daniel Veillard144fae12003-02-03 13:17:57 +00005079 xmlRelaxNGDefinePtr ret, cur, last = NULL;
5080 xmlNodePtr child;
5081
Daniel Veillardd2298792003-02-14 16:54:11 +00005082 if (!IS_RELAXNG(node, "except")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005083 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MISSING,
5084 "Expecting an except node\n", NULL, NULL);
5085 return (NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005086 }
5087 if (node->next != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005088 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MULTIPLE,
5089 "exceptNameClass allows only a single except node\n",
5090 NULL, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005091 }
Daniel Veillard144fae12003-02-03 13:17:57 +00005092 if (node->children == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005093 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_EMPTY, "except has no content\n",
5094 NULL, NULL);
5095 return (NULL);
Daniel Veillard144fae12003-02-03 13:17:57 +00005096 }
5097
Daniel Veillardfd573f12003-03-16 17:52:32 +00005098 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard144fae12003-02-03 13:17:57 +00005099 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005100 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005101 ret->type = XML_RELAXNG_EXCEPT;
Daniel Veillard144fae12003-02-03 13:17:57 +00005102 child = node->children;
5103 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005104 cur = xmlRelaxNGNewDefine(ctxt, child);
5105 if (cur == NULL)
5106 break;
5107 if (attr)
5108 cur->type = XML_RELAXNG_ATTRIBUTE;
5109 else
5110 cur->type = XML_RELAXNG_ELEMENT;
5111
Daniel Veillard419a7682003-02-03 23:22:49 +00005112 if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005113 if (last == NULL) {
5114 ret->content = cur;
5115 } else {
5116 last->next = cur;
5117 }
5118 last = cur;
5119 }
5120 child = child->next;
Daniel Veillard144fae12003-02-03 13:17:57 +00005121 }
5122
Daniel Veillard4c004142003-10-07 11:33:24 +00005123 return (ret);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005124}
5125
5126/**
5127 * xmlRelaxNGParseNameClass:
5128 * @ctxt: a Relax-NG parser context
5129 * @node: the nameClass node
5130 * @def: the current definition
5131 *
5132 * parse the content of a RelaxNG nameClass node.
5133 *
5134 * Returns the definition pointer or NULL in case of error.
5135 */
5136static xmlRelaxNGDefinePtr
5137xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node,
Daniel Veillard4c004142003-10-07 11:33:24 +00005138 xmlRelaxNGDefinePtr def)
5139{
Daniel Veillardfd573f12003-03-16 17:52:32 +00005140 xmlRelaxNGDefinePtr ret, tmp;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005141 xmlChar *val;
5142
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005143 ret = def;
Daniel Veillard4c004142003-10-07 11:33:24 +00005144 if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) ||
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005145 (IS_RELAXNG(node, "nsName"))) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005146 if ((def->type != XML_RELAXNG_ELEMENT) &&
5147 (def->type != XML_RELAXNG_ATTRIBUTE)) {
5148 ret = xmlRelaxNGNewDefine(ctxt, node);
5149 if (ret == NULL)
5150 return (NULL);
5151 ret->parent = def;
5152 if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE)
5153 ret->type = XML_RELAXNG_ATTRIBUTE;
5154 else
5155 ret->type = XML_RELAXNG_ELEMENT;
5156 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005157 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005158 if (IS_RELAXNG(node, "name")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005159 val = xmlNodeGetContent(node);
5160 xmlRelaxNGNormExtSpace(val);
5161 if (xmlValidateNCName(val, 0)) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005162 if (node->parent != NULL)
5163 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
5164 "Element %s name '%s' is not an NCName\n",
5165 node->parent->name, val);
5166 else
5167 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
5168 "name '%s' is not an NCName\n",
5169 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005170 }
5171 ret->name = val;
5172 val = xmlGetProp(node, BAD_CAST "ns");
5173 ret->ns = val;
5174 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5175 (val != NULL) &&
5176 (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005177 xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
Daniel Veillard4c004142003-10-07 11:33:24 +00005178 "Attribute with namespace '%s' is not allowed\n",
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005179 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005180 }
5181 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5182 (val != NULL) &&
5183 (val[0] == 0) && (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005184 xmlRngPErr(ctxt, node, XML_RNGP_XMLNS_NAME,
5185 "Attribute with QName 'xmlns' is not allowed\n",
5186 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005187 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005188 } else if (IS_RELAXNG(node, "anyName")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005189 ret->name = NULL;
5190 ret->ns = NULL;
5191 if (node->children != NULL) {
5192 ret->nameClass =
5193 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5194 (def->type ==
5195 XML_RELAXNG_ATTRIBUTE));
5196 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005197 } else if (IS_RELAXNG(node, "nsName")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005198 ret->name = NULL;
5199 ret->ns = xmlGetProp(node, BAD_CAST "ns");
5200 if (ret->ns == NULL) {
5201 xmlRngPErr(ctxt, node, XML_RNGP_NSNAME_NO_NS,
5202 "nsName has no ns attribute\n", NULL, NULL);
5203 }
5204 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5205 (ret->ns != NULL) &&
5206 (xmlStrEqual
5207 (ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
5208 xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
5209 "Attribute with namespace '%s' is not allowed\n",
5210 ret->ns, NULL);
5211 }
5212 if (node->children != NULL) {
5213 ret->nameClass =
5214 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5215 (def->type ==
5216 XML_RELAXNG_ATTRIBUTE));
5217 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005218 } else if (IS_RELAXNG(node, "choice")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005219 xmlNodePtr child;
5220 xmlRelaxNGDefinePtr last = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005221
Daniel Veillard4c004142003-10-07 11:33:24 +00005222 ret = xmlRelaxNGNewDefine(ctxt, node);
5223 if (ret == NULL)
5224 return (NULL);
5225 ret->parent = def;
5226 ret->type = XML_RELAXNG_CHOICE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005227
Daniel Veillard4c004142003-10-07 11:33:24 +00005228 if (node->children == NULL) {
5229 xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_EMPTY,
5230 "Element choice is empty\n", NULL, NULL);
5231 } else {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005232
Daniel Veillard4c004142003-10-07 11:33:24 +00005233 child = node->children;
5234 while (child != NULL) {
5235 tmp = xmlRelaxNGParseNameClass(ctxt, child, ret);
5236 if (tmp != NULL) {
5237 if (last == NULL) {
5238 last = ret->nameClass = tmp;
5239 } else {
5240 last->next = tmp;
5241 last = tmp;
5242 }
5243 }
5244 child = child->next;
5245 }
5246 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005247 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005248 xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_CONTENT,
5249 "expecting name, anyName, nsName or choice : got %s\n",
5250 node->name, NULL);
5251 return (NULL);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005252 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005253 if (ret != def) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005254 if (def->nameClass == NULL) {
5255 def->nameClass = ret;
5256 } else {
5257 tmp = def->nameClass;
5258 while (tmp->next != NULL) {
5259 tmp = tmp->next;
5260 }
5261 tmp->next = ret;
5262 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005263 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005264 return (ret);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005265}
5266
5267/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00005268 * xmlRelaxNGParseElement:
5269 * @ctxt: a Relax-NG parser context
5270 * @node: the element node
5271 *
5272 * parse the content of a RelaxNG element node.
5273 *
5274 * Returns the definition pointer or NULL in case of error.
5275 */
5276static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00005277xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
5278{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005279 xmlRelaxNGDefinePtr ret, cur, last;
5280 xmlNodePtr child;
Daniel Veillard276be4a2003-01-24 01:03:34 +00005281 const xmlChar *olddefine;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005282
Daniel Veillardfd573f12003-03-16 17:52:32 +00005283 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005284 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005285 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005286 ret->type = XML_RELAXNG_ELEMENT;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005287 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005288 child = node->children;
5289 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005290 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_EMPTY,
5291 "xmlRelaxNGParseElement: element has no children\n",
5292 NULL, NULL);
5293 return (ret);
5294 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005295 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
5296 if (cur != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005297 child = child->next;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005298
Daniel Veillard6eadf632003-01-23 18:29:16 +00005299 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005300 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NO_CONTENT,
5301 "xmlRelaxNGParseElement: element has no content\n",
5302 NULL, NULL);
5303 return (ret);
5304 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00005305 olddefine = ctxt->define;
5306 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005307 last = NULL;
5308 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005309 cur = xmlRelaxNGParsePattern(ctxt, child);
5310 if (cur != NULL) {
5311 cur->parent = ret;
5312 switch (cur->type) {
5313 case XML_RELAXNG_EMPTY:
5314 case XML_RELAXNG_NOT_ALLOWED:
5315 case XML_RELAXNG_TEXT:
5316 case XML_RELAXNG_ELEMENT:
5317 case XML_RELAXNG_DATATYPE:
5318 case XML_RELAXNG_VALUE:
5319 case XML_RELAXNG_LIST:
5320 case XML_RELAXNG_REF:
5321 case XML_RELAXNG_PARENTREF:
5322 case XML_RELAXNG_EXTERNALREF:
5323 case XML_RELAXNG_DEF:
5324 case XML_RELAXNG_ZEROORMORE:
5325 case XML_RELAXNG_ONEORMORE:
5326 case XML_RELAXNG_OPTIONAL:
5327 case XML_RELAXNG_CHOICE:
5328 case XML_RELAXNG_GROUP:
5329 case XML_RELAXNG_INTERLEAVE:
5330 if (last == NULL) {
5331 ret->content = last = cur;
5332 } else {
5333 if ((last->type == XML_RELAXNG_ELEMENT) &&
5334 (ret->content == last)) {
5335 ret->content = xmlRelaxNGNewDefine(ctxt, node);
5336 if (ret->content != NULL) {
5337 ret->content->type = XML_RELAXNG_GROUP;
5338 ret->content->content = last;
5339 } else {
5340 ret->content = last;
5341 }
5342 }
5343 last->next = cur;
5344 last = cur;
5345 }
5346 break;
5347 case XML_RELAXNG_ATTRIBUTE:
5348 cur->next = ret->attrs;
5349 ret->attrs = cur;
5350 break;
5351 case XML_RELAXNG_START:
5352 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5353 "RNG Internal error, start found in element\n",
5354 NULL, NULL);
5355 break;
5356 case XML_RELAXNG_PARAM:
5357 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5358 "RNG Internal error, param found in element\n",
5359 NULL, NULL);
5360 break;
5361 case XML_RELAXNG_EXCEPT:
5362 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5363 "RNG Internal error, except found in element\n",
5364 NULL, NULL);
5365 break;
5366 case XML_RELAXNG_NOOP:
5367 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5368 "RNG Internal error, noop found in element\n",
5369 NULL, NULL);
5370 break;
5371 }
5372 }
5373 child = child->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005374 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00005375 ctxt->define = olddefine;
Daniel Veillard4c004142003-10-07 11:33:24 +00005376 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005377}
5378
5379/**
5380 * xmlRelaxNGParsePatterns:
5381 * @ctxt: a Relax-NG parser context
5382 * @nodes: list of nodes
Daniel Veillard154877e2003-01-30 12:17:05 +00005383 * @group: use an implicit <group> for elements
Daniel Veillard6eadf632003-01-23 18:29:16 +00005384 *
5385 * parse the content of a RelaxNG start node.
5386 *
5387 * Returns the definition pointer or NULL in case of error.
5388 */
5389static xmlRelaxNGDefinePtr
Daniel Veillard154877e2003-01-30 12:17:05 +00005390xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes,
Daniel Veillard4c004142003-10-07 11:33:24 +00005391 int group)
5392{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005393 xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005394
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005395 parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005396 while (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005397 if (IS_RELAXNG(nodes, "element")) {
5398 cur = xmlRelaxNGParseElement(ctxt, nodes);
5399 if (def == NULL) {
5400 def = last = cur;
5401 } else {
5402 if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) &&
5403 (def == last)) {
5404 def = xmlRelaxNGNewDefine(ctxt, nodes);
5405 def->type = XML_RELAXNG_GROUP;
5406 def->content = last;
5407 }
5408 last->next = cur;
5409 last = cur;
5410 }
5411 cur->parent = parent;
5412 } else {
5413 cur = xmlRelaxNGParsePattern(ctxt, nodes);
5414 if (cur != NULL) {
5415 if (def == NULL) {
5416 def = last = cur;
5417 } else {
5418 last->next = cur;
5419 last = cur;
5420 }
5421 }
5422 }
5423 nodes = nodes->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005424 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005425 return (def);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005426}
5427
5428/**
5429 * xmlRelaxNGParseStart:
5430 * @ctxt: a Relax-NG parser context
5431 * @nodes: start children nodes
5432 *
5433 * parse the content of a RelaxNG start node.
5434 *
5435 * Returns 0 in case of success, -1 in case of error
5436 */
5437static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005438xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
5439{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005440 int ret = 0;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005441 xmlRelaxNGDefinePtr def = NULL, last;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005442
Daniel Veillardd2298792003-02-14 16:54:11 +00005443 if (nodes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005444 xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, "start has no children\n",
5445 NULL, NULL);
5446 return (-1);
Daniel Veillardd2298792003-02-14 16:54:11 +00005447 }
5448 if (IS_RELAXNG(nodes, "empty")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005449 def = xmlRelaxNGNewDefine(ctxt, nodes);
5450 if (def == NULL)
5451 return (-1);
5452 def->type = XML_RELAXNG_EMPTY;
5453 if (nodes->children != NULL) {
5454 xmlRngPErr(ctxt, nodes, XML_RNGP_EMPTY_CONTENT,
5455 "element empty is not empty\n", NULL, NULL);
5456 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005457 } else if (IS_RELAXNG(nodes, "notAllowed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005458 def = xmlRelaxNGNewDefine(ctxt, nodes);
5459 if (def == NULL)
5460 return (-1);
5461 def->type = XML_RELAXNG_NOT_ALLOWED;
5462 if (nodes->children != NULL) {
5463 xmlRngPErr(ctxt, nodes, XML_RNGP_NOTALLOWED_NOT_EMPTY,
5464 "element notAllowed is not empty\n", NULL, NULL);
5465 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005466 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005467 def = xmlRelaxNGParsePatterns(ctxt, nodes, 1);
Daniel Veillard2df2de22003-02-17 23:34:33 +00005468 }
5469 if (ctxt->grammar->start != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005470 last = ctxt->grammar->start;
5471 while (last->next != NULL)
5472 last = last->next;
5473 last->next = def;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005474 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005475 ctxt->grammar->start = def;
Daniel Veillardd2298792003-02-14 16:54:11 +00005476 }
5477 nodes = nodes->next;
5478 if (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005479 xmlRngPErr(ctxt, nodes, XML_RNGP_START_CONTENT,
5480 "start more than one children\n", NULL, NULL);
5481 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005482 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005483 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005484}
5485
5486/**
5487 * xmlRelaxNGParseGrammarContent:
5488 * @ctxt: a Relax-NG parser context
5489 * @nodes: grammar children nodes
5490 *
5491 * parse the content of a RelaxNG grammar node.
5492 *
5493 * Returns 0 in case of success, -1 in case of error
5494 */
5495static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005496xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
5497 xmlNodePtr nodes)
Daniel Veillard6eadf632003-01-23 18:29:16 +00005498{
Daniel Veillarde2a5a082003-02-02 14:35:17 +00005499 int ret = 0, tmp;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005500
5501 if (nodes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005502 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_EMPTY,
5503 "grammar has no children\n", NULL, NULL);
5504 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005505 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005506 while (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005507 if (IS_RELAXNG(nodes, "start")) {
5508 if (nodes->children == NULL) {
5509 xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY,
5510 "start has no children\n", NULL, NULL);
5511 } else {
5512 tmp = xmlRelaxNGParseStart(ctxt, nodes->children);
5513 if (tmp != 0)
5514 ret = -1;
5515 }
5516 } else if (IS_RELAXNG(nodes, "define")) {
5517 tmp = xmlRelaxNGParseDefine(ctxt, nodes);
5518 if (tmp != 0)
5519 ret = -1;
5520 } else if (IS_RELAXNG(nodes, "include")) {
5521 tmp = xmlRelaxNGParseInclude(ctxt, nodes);
5522 if (tmp != 0)
5523 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005524 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005525 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
5526 "grammar has unexpected child %s\n", nodes->name,
5527 NULL);
5528 ret = -1;
5529 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005530 nodes = nodes->next;
5531 }
5532 return (ret);
5533}
5534
5535/**
5536 * xmlRelaxNGCheckReference:
5537 * @ref: the ref
5538 * @ctxt: a Relax-NG parser context
5539 * @name: the name associated to the defines
5540 *
5541 * Applies the 4.17. combine attribute rule for all the define
5542 * element of a given grammar using the same name.
5543 */
5544static void
5545xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref,
Daniel Veillard4c004142003-10-07 11:33:24 +00005546 xmlRelaxNGParserCtxtPtr ctxt,
5547 const xmlChar * name)
5548{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005549 xmlRelaxNGGrammarPtr grammar;
Daniel Veillard276be4a2003-01-24 01:03:34 +00005550 xmlRelaxNGDefinePtr def, cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005551
5552 grammar = ctxt->grammar;
5553 if (grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005554 xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
5555 "Internal error: no grammar in CheckReference %s\n",
5556 name, NULL);
5557 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005558 }
5559 if (ref->content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005560 xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
5561 "Internal error: reference has content in CheckReference %s\n",
5562 name, NULL);
5563 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005564 }
5565 if (grammar->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005566 def = xmlHashLookup(grammar->defs, name);
5567 if (def != NULL) {
5568 cur = ref;
5569 while (cur != NULL) {
5570 cur->content = def;
5571 cur = cur->nextHash;
5572 }
5573 } else {
5574 xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
5575 "Reference %s has no matching definition\n", name,
5576 NULL);
5577 }
Daniel Veillardd4310742003-02-18 21:12:46 +00005578 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005579 xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
5580 "Reference %s has no matching definition\n", name,
5581 NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005582 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005583}
5584
5585/**
5586 * xmlRelaxNGCheckCombine:
5587 * @define: the define(s) list
5588 * @ctxt: a Relax-NG parser context
5589 * @name: the name associated to the defines
5590 *
5591 * Applies the 4.17. combine attribute rule for all the define
5592 * element of a given grammar using the same name.
5593 */
5594static void
5595xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define,
Daniel Veillard4c004142003-10-07 11:33:24 +00005596 xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * name)
5597{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005598 xmlChar *combine;
5599 int choiceOrInterleave = -1;
5600 int missing = 0;
5601 xmlRelaxNGDefinePtr cur, last, tmp, tmp2;
5602
5603 if (define->nextHash == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005604 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005605 cur = define;
5606 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005607 combine = xmlGetProp(cur->node, BAD_CAST "combine");
5608 if (combine != NULL) {
5609 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5610 if (choiceOrInterleave == -1)
5611 choiceOrInterleave = 1;
5612 else if (choiceOrInterleave == 0) {
5613 xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
5614 "Defines for %s use both 'choice' and 'interleave'\n",
5615 name, NULL);
5616 }
5617 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
5618 if (choiceOrInterleave == -1)
5619 choiceOrInterleave = 0;
5620 else if (choiceOrInterleave == 1) {
5621 xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
5622 "Defines for %s use both 'choice' and 'interleave'\n",
5623 name, NULL);
5624 }
5625 } else {
5626 xmlRngPErr(ctxt, define->node, XML_RNGP_UNKNOWN_COMBINE,
5627 "Defines for %s use unknown combine value '%s''\n",
5628 name, combine);
5629 }
5630 xmlFree(combine);
5631 } else {
5632 if (missing == 0)
5633 missing = 1;
5634 else {
5635 xmlRngPErr(ctxt, define->node, XML_RNGP_NEED_COMBINE,
5636 "Some defines for %s needs the combine attribute\n",
5637 name, NULL);
5638 }
5639 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005640
Daniel Veillard4c004142003-10-07 11:33:24 +00005641 cur = cur->nextHash;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005642 }
5643#ifdef DEBUG
5644 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00005645 "xmlRelaxNGCheckCombine(): merging %s defines: %d\n",
5646 name, choiceOrInterleave);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005647#endif
5648 if (choiceOrInterleave == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +00005649 choiceOrInterleave = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005650 cur = xmlRelaxNGNewDefine(ctxt, define->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005651 if (cur == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005652 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005653 if (choiceOrInterleave == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00005654 cur->type = XML_RELAXNG_INTERLEAVE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005655 else
Daniel Veillard4c004142003-10-07 11:33:24 +00005656 cur->type = XML_RELAXNG_CHOICE;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005657 tmp = define;
5658 last = NULL;
5659 while (tmp != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005660 if (tmp->content != NULL) {
5661 if (tmp->content->next != NULL) {
5662 /*
5663 * we need first to create a wrapper.
5664 */
5665 tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node);
5666 if (tmp2 == NULL)
5667 break;
5668 tmp2->type = XML_RELAXNG_GROUP;
5669 tmp2->content = tmp->content;
5670 } else {
5671 tmp2 = tmp->content;
5672 }
5673 if (last == NULL) {
5674 cur->content = tmp2;
5675 } else {
5676 last->next = tmp2;
5677 }
5678 last = tmp2;
5679 }
5680 tmp->content = cur;
5681 tmp = tmp->nextHash;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005682 }
5683 define->content = cur;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005684 if (choiceOrInterleave == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005685 if (ctxt->interleaves == NULL)
5686 ctxt->interleaves = xmlHashCreate(10);
5687 if (ctxt->interleaves == NULL) {
5688 xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5689 "Failed to create interleaves hash table\n", NULL,
5690 NULL);
5691 } else {
5692 char tmpname[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00005693
Daniel Veillard4c004142003-10-07 11:33:24 +00005694 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5695 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
5696 0) {
5697 xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5698 "Failed to add %s to hash table\n",
5699 (const xmlChar *) tmpname, NULL);
5700 }
5701 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005702 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005703}
5704
5705/**
5706 * xmlRelaxNGCombineStart:
5707 * @ctxt: a Relax-NG parser context
5708 * @grammar: the grammar
5709 *
5710 * Applies the 4.17. combine rule for all the start
5711 * element of a given grammar.
5712 */
5713static void
5714xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00005715 xmlRelaxNGGrammarPtr grammar)
5716{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005717 xmlRelaxNGDefinePtr starts;
5718 xmlChar *combine;
5719 int choiceOrInterleave = -1;
5720 int missing = 0;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005721 xmlRelaxNGDefinePtr cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005722
Daniel Veillard2df2de22003-02-17 23:34:33 +00005723 starts = grammar->start;
5724 if ((starts == NULL) || (starts->next == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00005725 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005726 cur = starts;
5727 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005728 if ((cur->node == NULL) || (cur->node->parent == NULL) ||
5729 (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) {
5730 combine = NULL;
5731 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_MISSING,
5732 "Internal error: start element not found\n", NULL,
5733 NULL);
5734 } else {
5735 combine = xmlGetProp(cur->node->parent, BAD_CAST "combine");
5736 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005737
Daniel Veillard4c004142003-10-07 11:33:24 +00005738 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, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
5744 "<start> use both 'choice' and 'interleave'\n",
5745 NULL, 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, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
5752 "<start> use both 'choice' and 'interleave'\n",
5753 NULL, NULL);
5754 }
5755 } else {
5756 xmlRngPErr(ctxt, cur->node, XML_RNGP_UNKNOWN_COMBINE,
5757 "<start> uses unknown combine value '%s''\n",
5758 combine, NULL);
5759 }
5760 xmlFree(combine);
5761 } else {
5762 if (missing == 0)
5763 missing = 1;
5764 else {
5765 xmlRngPErr(ctxt, cur->node, XML_RNGP_NEED_COMBINE,
5766 "Some <start> element miss the combine attribute\n",
5767 NULL, NULL);
5768 }
5769 }
5770
5771 cur = cur->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005772 }
5773#ifdef DEBUG
5774 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00005775 "xmlRelaxNGCombineStart(): merging <start>: %d\n",
5776 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, starts->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 Veillard2df2de22003-02-17 23:34:33 +00005787 cur->content = grammar->start;
5788 grammar->start = cur;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005789 if (choiceOrInterleave == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005790 if (ctxt->interleaves == NULL)
5791 ctxt->interleaves = xmlHashCreate(10);
5792 if (ctxt->interleaves == NULL) {
5793 xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5794 "Failed to create interleaves hash table\n", NULL,
5795 NULL);
5796 } else {
5797 char tmpname[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00005798
Daniel Veillard4c004142003-10-07 11:33:24 +00005799 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5800 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
5801 0) {
5802 xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5803 "Failed to add %s to hash table\n",
5804 (const xmlChar *) tmpname, NULL);
5805 }
5806 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005807 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005808}
5809
5810/**
Daniel Veillardd4310742003-02-18 21:12:46 +00005811 * xmlRelaxNGCheckCycles:
5812 * @ctxt: a Relax-NG parser context
5813 * @nodes: grammar children nodes
5814 * @depth: the counter
5815 *
5816 * Check for cycles.
5817 *
5818 * Returns 0 if check passed, and -1 in case of error
5819 */
5820static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005821xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt,
5822 xmlRelaxNGDefinePtr cur, int depth)
5823{
Daniel Veillardd4310742003-02-18 21:12:46 +00005824 int ret = 0;
5825
5826 while ((ret == 0) && (cur != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005827 if ((cur->type == XML_RELAXNG_REF) ||
5828 (cur->type == XML_RELAXNG_PARENTREF)) {
5829 if (cur->depth == -1) {
5830 cur->depth = depth;
5831 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5832 cur->depth = -2;
5833 } else if (depth == cur->depth) {
5834 xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_CYCLE,
5835 "Detected a cycle in %s references\n",
5836 cur->name, NULL);
5837 return (-1);
5838 }
5839 } else if (cur->type == XML_RELAXNG_ELEMENT) {
5840 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1);
5841 } else {
5842 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5843 }
5844 cur = cur->next;
Daniel Veillardd4310742003-02-18 21:12:46 +00005845 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005846 return (ret);
Daniel Veillardd4310742003-02-18 21:12:46 +00005847}
5848
5849/**
Daniel Veillard77648bb2003-02-20 15:03:22 +00005850 * xmlRelaxNGTryUnlink:
5851 * @ctxt: a Relax-NG parser context
5852 * @cur: the definition to unlink
5853 * @parent: the parent definition
5854 * @prev: the previous sibling definition
5855 *
5856 * Try to unlink a definition. If not possble make it a NOOP
5857 *
5858 * Returns the new prev definition
5859 */
5860static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00005861xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
5862 xmlRelaxNGDefinePtr cur,
5863 xmlRelaxNGDefinePtr parent, xmlRelaxNGDefinePtr prev)
5864{
Daniel Veillardfd573f12003-03-16 17:52:32 +00005865 if (prev != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005866 prev->next = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005867 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005868 if (parent != NULL) {
5869 if (parent->content == cur)
5870 parent->content = cur->next;
5871 else if (parent->attrs == cur)
5872 parent->attrs = cur->next;
5873 else if (parent->nameClass == cur)
5874 parent->nameClass = cur->next;
5875 } else {
5876 cur->type = XML_RELAXNG_NOOP;
5877 prev = cur;
5878 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005879 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005880 return (prev);
Daniel Veillard77648bb2003-02-20 15:03:22 +00005881}
5882
5883/**
Daniel Veillard4c5cf702003-02-21 15:40:34 +00005884 * xmlRelaxNGSimplify:
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005885 * @ctxt: a Relax-NG parser context
5886 * @nodes: grammar children nodes
5887 *
5888 * Check for simplification of empty and notAllowed
5889 */
5890static void
Daniel Veillard4c004142003-10-07 11:33:24 +00005891xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt,
5892 xmlRelaxNGDefinePtr cur, xmlRelaxNGDefinePtr parent)
5893{
Daniel Veillardfd573f12003-03-16 17:52:32 +00005894 xmlRelaxNGDefinePtr prev = NULL;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005895
Daniel Veillardfd573f12003-03-16 17:52:32 +00005896 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005897 if ((cur->type == XML_RELAXNG_REF) ||
5898 (cur->type == XML_RELAXNG_PARENTREF)) {
5899 if (cur->depth != -3) {
5900 cur->depth = -3;
5901 xmlRelaxNGSimplify(ctxt, cur->content, cur);
5902 }
5903 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
5904 cur->parent = parent;
5905 if ((parent != NULL) &&
5906 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
5907 (parent->type == XML_RELAXNG_LIST) ||
5908 (parent->type == XML_RELAXNG_GROUP) ||
5909 (parent->type == XML_RELAXNG_INTERLEAVE) ||
5910 (parent->type == XML_RELAXNG_ONEORMORE) ||
5911 (parent->type == XML_RELAXNG_ZEROORMORE))) {
5912 parent->type = XML_RELAXNG_NOT_ALLOWED;
5913 break;
5914 }
5915 if ((parent != NULL) && (parent->type == XML_RELAXNG_CHOICE)) {
5916 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5917 } else
5918 prev = cur;
5919 } else if (cur->type == XML_RELAXNG_EMPTY) {
5920 cur->parent = parent;
5921 if ((parent != NULL) &&
5922 ((parent->type == XML_RELAXNG_ONEORMORE) ||
5923 (parent->type == XML_RELAXNG_ZEROORMORE))) {
5924 parent->type = XML_RELAXNG_EMPTY;
5925 break;
5926 }
5927 if ((parent != NULL) &&
5928 ((parent->type == XML_RELAXNG_GROUP) ||
5929 (parent->type == XML_RELAXNG_INTERLEAVE))) {
5930 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5931 } else
5932 prev = cur;
5933 } else {
5934 cur->parent = parent;
5935 if (cur->content != NULL)
5936 xmlRelaxNGSimplify(ctxt, cur->content, cur);
5937 if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL))
5938 xmlRelaxNGSimplify(ctxt, cur->attrs, cur);
5939 if (cur->nameClass != NULL)
5940 xmlRelaxNGSimplify(ctxt, cur->nameClass, cur);
5941 /*
5942 * On Elements, try to move attribute only generating rules on
5943 * the attrs rules.
5944 */
5945 if (cur->type == XML_RELAXNG_ELEMENT) {
5946 int attronly;
5947 xmlRelaxNGDefinePtr tmp, pre;
Daniel Veillardce192eb2003-04-16 15:58:05 +00005948
Daniel Veillard4c004142003-10-07 11:33:24 +00005949 while (cur->content != NULL) {
5950 attronly =
5951 xmlRelaxNGGenerateAttributes(ctxt, cur->content);
5952 if (attronly == 1) {
5953 /*
5954 * migrate cur->content to attrs
5955 */
5956 tmp = cur->content;
5957 cur->content = tmp->next;
5958 tmp->next = cur->attrs;
5959 cur->attrs = tmp;
5960 } else {
5961 /*
5962 * cur->content can generate elements or text
5963 */
5964 break;
5965 }
5966 }
5967 pre = cur->content;
5968 while ((pre != NULL) && (pre->next != NULL)) {
5969 tmp = pre->next;
5970 attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp);
5971 if (attronly == 1) {
5972 /*
5973 * migrate tmp to attrs
5974 */
5975 pre->next = tmp->next;
5976 tmp->next = cur->attrs;
5977 cur->attrs = tmp;
5978 } else {
5979 pre = tmp;
5980 }
5981 }
5982 }
5983 /*
5984 * This may result in a simplification
5985 */
5986 if ((cur->type == XML_RELAXNG_GROUP) ||
5987 (cur->type == XML_RELAXNG_INTERLEAVE)) {
5988 if (cur->content == NULL)
5989 cur->type = XML_RELAXNG_EMPTY;
5990 else if (cur->content->next == NULL) {
5991 if ((parent == NULL) && (prev == NULL)) {
5992 cur->type = XML_RELAXNG_NOOP;
5993 } else if (prev == NULL) {
5994 parent->content = cur->content;
5995 cur->content->next = cur->next;
5996 cur = cur->content;
5997 } else {
5998 cur->content->next = cur->next;
5999 prev->next = cur->content;
6000 cur = cur->content;
6001 }
6002 }
6003 }
6004 /*
6005 * the current node may have been transformed back
6006 */
6007 if ((cur->type == XML_RELAXNG_EXCEPT) &&
6008 (cur->content != NULL) &&
6009 (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) {
6010 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6011 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
6012 if ((parent != NULL) &&
6013 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
6014 (parent->type == XML_RELAXNG_LIST) ||
6015 (parent->type == XML_RELAXNG_GROUP) ||
6016 (parent->type == XML_RELAXNG_INTERLEAVE) ||
6017 (parent->type == XML_RELAXNG_ONEORMORE) ||
6018 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6019 parent->type = XML_RELAXNG_NOT_ALLOWED;
6020 break;
6021 }
6022 if ((parent != NULL) &&
6023 (parent->type == XML_RELAXNG_CHOICE)) {
6024 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6025 } else
6026 prev = cur;
6027 } else if (cur->type == XML_RELAXNG_EMPTY) {
6028 if ((parent != NULL) &&
6029 ((parent->type == XML_RELAXNG_ONEORMORE) ||
6030 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6031 parent->type = XML_RELAXNG_EMPTY;
6032 break;
6033 }
6034 if ((parent != NULL) &&
6035 ((parent->type == XML_RELAXNG_GROUP) ||
6036 (parent->type == XML_RELAXNG_INTERLEAVE) ||
6037 (parent->type == XML_RELAXNG_CHOICE))) {
6038 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6039 } else
6040 prev = cur;
6041 } else {
6042 prev = cur;
6043 }
6044 }
6045 cur = cur->next;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006046 }
6047}
6048
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006049/**
6050 * xmlRelaxNGGroupContentType:
6051 * @ct1: the first content type
6052 * @ct2: the second content type
6053 *
6054 * Try to group 2 content types
6055 *
6056 * Returns the content type
6057 */
6058static xmlRelaxNGContentType
6059xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1,
Daniel Veillard4c004142003-10-07 11:33:24 +00006060 xmlRelaxNGContentType ct2)
6061{
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006062 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006063 (ct2 == XML_RELAXNG_CONTENT_ERROR))
6064 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006065 if (ct1 == XML_RELAXNG_CONTENT_EMPTY)
Daniel Veillard4c004142003-10-07 11:33:24 +00006066 return (ct2);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006067 if (ct2 == XML_RELAXNG_CONTENT_EMPTY)
Daniel Veillard4c004142003-10-07 11:33:24 +00006068 return (ct1);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006069 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00006070 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6071 return (XML_RELAXNG_CONTENT_COMPLEX);
6072 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006073}
6074
6075/**
6076 * xmlRelaxNGMaxContentType:
6077 * @ct1: the first content type
6078 * @ct2: the second content type
6079 *
6080 * Compute the max content-type
6081 *
6082 * Returns the content type
6083 */
6084static xmlRelaxNGContentType
6085xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1,
Daniel Veillard4c004142003-10-07 11:33:24 +00006086 xmlRelaxNGContentType ct2)
6087{
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006088 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006089 (ct2 == XML_RELAXNG_CONTENT_ERROR))
6090 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006091 if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006092 (ct2 == XML_RELAXNG_CONTENT_SIMPLE))
6093 return (XML_RELAXNG_CONTENT_SIMPLE);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006094 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006095 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6096 return (XML_RELAXNG_CONTENT_COMPLEX);
6097 return (XML_RELAXNG_CONTENT_EMPTY);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006098}
Daniel Veillard77648bb2003-02-20 15:03:22 +00006099
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006100/**
6101 * xmlRelaxNGCheckRules:
6102 * @ctxt: a Relax-NG parser context
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006103 * @cur: the current definition
Daniel Veillard77648bb2003-02-20 15:03:22 +00006104 * @flags: some accumulated flags
Daniel Veillardfd573f12003-03-16 17:52:32 +00006105 * @ptype: the parent type
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006106 *
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006107 * Check for rules in section 7.1 and 7.2
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006108 *
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006109 * Returns the content type of @cur
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006110 */
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006111static xmlRelaxNGContentType
Daniel Veillard4c004142003-10-07 11:33:24 +00006112xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt,
6113 xmlRelaxNGDefinePtr cur, int flags,
6114 xmlRelaxNGType ptype)
6115{
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006116 int nflags = flags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006117 xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006118
Daniel Veillardfd573f12003-03-16 17:52:32 +00006119 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006120 ret = XML_RELAXNG_CONTENT_EMPTY;
6121 if ((cur->type == XML_RELAXNG_REF) ||
6122 (cur->type == XML_RELAXNG_PARENTREF)) {
6123 if (flags & XML_RELAXNG_IN_LIST) {
6124 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_REF,
6125 "Found forbidden pattern list//ref\n", NULL,
6126 NULL);
6127 }
6128 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6129 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_REF,
6130 "Found forbidden pattern data/except//ref\n",
6131 NULL, NULL);
6132 }
6133 if (cur->depth > -4) {
6134 cur->depth = -4;
6135 ret = xmlRelaxNGCheckRules(ctxt, cur->content,
6136 flags, cur->type);
6137 cur->depth = ret - 15;
6138 } else if (cur->depth == -4) {
6139 ret = XML_RELAXNG_CONTENT_COMPLEX;
6140 } else {
6141 ret = (xmlRelaxNGContentType) (cur->depth + 15);
6142 }
6143 } else if (cur->type == XML_RELAXNG_ELEMENT) {
6144 /*
6145 * The 7.3 Attribute derivation rule for groups is plugged there
6146 */
6147 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6148 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6149 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ELEM,
6150 "Found forbidden pattern data/except//element(ref)\n",
6151 NULL, NULL);
6152 }
6153 if (flags & XML_RELAXNG_IN_LIST) {
6154 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ELEM,
6155 "Found forbidden pattern list//element(ref)\n",
6156 NULL, NULL);
6157 }
6158 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6159 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
6160 "Found forbidden pattern attribute//element(ref)\n",
6161 NULL, NULL);
6162 }
6163 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6164 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
6165 "Found forbidden pattern attribute//element(ref)\n",
6166 NULL, NULL);
6167 }
6168 /*
6169 * reset since in the simple form elements are only child
6170 * of grammar/define
6171 */
6172 nflags = 0;
6173 ret =
6174 xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type);
6175 if (ret != XML_RELAXNG_CONTENT_EMPTY) {
6176 xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_EMPTY,
6177 "Element %s attributes have a content type error\n",
6178 cur->name, NULL);
6179 }
6180 ret =
6181 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6182 cur->type);
6183 if (ret == XML_RELAXNG_CONTENT_ERROR) {
6184 xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_ERROR,
6185 "Element %s has a content type error\n",
6186 cur->name, NULL);
6187 } else {
6188 ret = XML_RELAXNG_CONTENT_COMPLEX;
6189 }
6190 } else if (cur->type == XML_RELAXNG_ATTRIBUTE) {
6191 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6192 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ATTR,
6193 "Found forbidden pattern attribute//attribute\n",
6194 NULL, NULL);
6195 }
6196 if (flags & XML_RELAXNG_IN_LIST) {
6197 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ATTR,
6198 "Found forbidden pattern list//attribute\n",
6199 NULL, NULL);
6200 }
6201 if (flags & XML_RELAXNG_IN_OOMGROUP) {
6202 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_GROUP_ATTR,
6203 "Found forbidden pattern oneOrMore//group//attribute\n",
6204 NULL, NULL);
6205 }
6206 if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) {
6207 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_INTERLEAVE_ATTR,
6208 "Found forbidden pattern oneOrMore//interleave//attribute\n",
6209 NULL, NULL);
6210 }
6211 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6212 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ATTR,
6213 "Found forbidden pattern data/except//attribute\n",
6214 NULL, NULL);
6215 }
6216 if (flags & XML_RELAXNG_IN_START) {
6217 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ATTR,
6218 "Found forbidden pattern start//attribute\n",
6219 NULL, NULL);
6220 }
6221 if ((!(flags & XML_RELAXNG_IN_ONEORMORE))
6222 && (cur->name == NULL)) {
6223 if (cur->ns == NULL) {
6224 xmlRngPErr(ctxt, cur->node, XML_RNGP_ANYNAME_ATTR_ANCESTOR,
6225 "Found anyName attribute without oneOrMore ancestor\n",
6226 NULL, NULL);
6227 } else {
6228 xmlRngPErr(ctxt, cur->node, XML_RNGP_NSNAME_ATTR_ANCESTOR,
6229 "Found nsName attribute without oneOrMore ancestor\n",
6230 NULL, NULL);
6231 }
6232 }
6233 nflags = flags | XML_RELAXNG_IN_ATTRIBUTE;
6234 xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
6235 ret = XML_RELAXNG_CONTENT_EMPTY;
6236 } else if ((cur->type == XML_RELAXNG_ONEORMORE) ||
6237 (cur->type == XML_RELAXNG_ZEROORMORE)) {
6238 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6239 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ONEMORE,
6240 "Found forbidden pattern data/except//oneOrMore\n",
6241 NULL, NULL);
6242 }
6243 if (flags & XML_RELAXNG_IN_START) {
6244 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ONEMORE,
6245 "Found forbidden pattern start//oneOrMore\n",
6246 NULL, NULL);
6247 }
6248 nflags = flags | XML_RELAXNG_IN_ONEORMORE;
6249 ret =
6250 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6251 cur->type);
6252 ret = xmlRelaxNGGroupContentType(ret, ret);
6253 } else if (cur->type == XML_RELAXNG_LIST) {
6254 if (flags & XML_RELAXNG_IN_LIST) {
6255 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_LIST,
6256 "Found forbidden pattern list//list\n", NULL,
6257 NULL);
6258 }
6259 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6260 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_LIST,
6261 "Found forbidden pattern data/except//list\n",
6262 NULL, NULL);
6263 }
6264 if (flags & XML_RELAXNG_IN_START) {
6265 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_LIST,
6266 "Found forbidden pattern start//list\n", NULL,
6267 NULL);
6268 }
6269 nflags = flags | XML_RELAXNG_IN_LIST;
6270 ret =
6271 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6272 cur->type);
6273 } else if (cur->type == XML_RELAXNG_GROUP) {
6274 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6275 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_GROUP,
6276 "Found forbidden pattern data/except//group\n",
6277 NULL, NULL);
6278 }
6279 if (flags & XML_RELAXNG_IN_START) {
6280 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_GROUP,
6281 "Found forbidden pattern start//group\n", NULL,
6282 NULL);
6283 }
6284 if (flags & XML_RELAXNG_IN_ONEORMORE)
6285 nflags = flags | XML_RELAXNG_IN_OOMGROUP;
6286 else
6287 nflags = flags;
6288 ret =
6289 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6290 cur->type);
6291 /*
6292 * The 7.3 Attribute derivation rule for groups is plugged there
6293 */
6294 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6295 } else if (cur->type == XML_RELAXNG_INTERLEAVE) {
6296 if (flags & XML_RELAXNG_IN_LIST) {
6297 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_INTERLEAVE,
6298 "Found forbidden pattern list//interleave\n",
6299 NULL, NULL);
6300 }
6301 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6302 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
6303 "Found forbidden pattern data/except//interleave\n",
6304 NULL, NULL);
6305 }
6306 if (flags & XML_RELAXNG_IN_START) {
6307 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
6308 "Found forbidden pattern start//interleave\n",
6309 NULL, NULL);
6310 }
6311 if (flags & XML_RELAXNG_IN_ONEORMORE)
6312 nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE;
6313 else
6314 nflags = flags;
6315 ret =
6316 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6317 cur->type);
6318 } else if (cur->type == XML_RELAXNG_EXCEPT) {
6319 if ((cur->parent != NULL) &&
6320 (cur->parent->type == XML_RELAXNG_DATATYPE))
6321 nflags = flags | XML_RELAXNG_IN_DATAEXCEPT;
6322 else
6323 nflags = flags;
6324 ret =
6325 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6326 cur->type);
6327 } else if (cur->type == XML_RELAXNG_DATATYPE) {
6328 if (flags & XML_RELAXNG_IN_START) {
6329 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_DATA,
6330 "Found forbidden pattern start//data\n", NULL,
6331 NULL);
6332 }
6333 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6334 ret = XML_RELAXNG_CONTENT_SIMPLE;
6335 } else if (cur->type == XML_RELAXNG_VALUE) {
6336 if (flags & XML_RELAXNG_IN_START) {
6337 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_VALUE,
6338 "Found forbidden pattern start//value\n", NULL,
6339 NULL);
6340 }
6341 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6342 ret = XML_RELAXNG_CONTENT_SIMPLE;
6343 } else if (cur->type == XML_RELAXNG_TEXT) {
6344 if (flags & XML_RELAXNG_IN_LIST) {
6345 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_TEXT,
6346 "Found forbidden pattern list//text\n", NULL,
6347 NULL);
6348 }
6349 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6350 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_TEXT,
6351 "Found forbidden pattern data/except//text\n",
6352 NULL, NULL);
6353 }
6354 if (flags & XML_RELAXNG_IN_START) {
6355 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_TEXT,
6356 "Found forbidden pattern start//text\n", NULL,
6357 NULL);
6358 }
6359 ret = XML_RELAXNG_CONTENT_COMPLEX;
6360 } else if (cur->type == XML_RELAXNG_EMPTY) {
6361 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6362 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_EMPTY,
6363 "Found forbidden pattern data/except//empty\n",
6364 NULL, NULL);
6365 }
6366 if (flags & XML_RELAXNG_IN_START) {
6367 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_EMPTY,
6368 "Found forbidden pattern start//empty\n", NULL,
6369 NULL);
6370 }
6371 ret = XML_RELAXNG_CONTENT_EMPTY;
6372 } else if (cur->type == XML_RELAXNG_CHOICE) {
6373 xmlRelaxNGCheckChoiceDeterminism(ctxt, cur);
6374 ret =
6375 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6376 } else {
6377 ret =
6378 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6379 }
6380 cur = cur->next;
6381 if (ptype == XML_RELAXNG_GROUP) {
6382 val = xmlRelaxNGGroupContentType(val, ret);
6383 } else if (ptype == XML_RELAXNG_INTERLEAVE) {
6384 tmp = xmlRelaxNGGroupContentType(val, ret);
6385 if (tmp != XML_RELAXNG_CONTENT_ERROR)
6386 tmp = xmlRelaxNGMaxContentType(val, ret);
6387 } else if (ptype == XML_RELAXNG_CHOICE) {
6388 val = xmlRelaxNGMaxContentType(val, ret);
6389 } else if (ptype == XML_RELAXNG_LIST) {
6390 val = XML_RELAXNG_CONTENT_SIMPLE;
6391 } else if (ptype == XML_RELAXNG_EXCEPT) {
6392 if (ret == XML_RELAXNG_CONTENT_ERROR)
6393 val = XML_RELAXNG_CONTENT_ERROR;
6394 else
6395 val = XML_RELAXNG_CONTENT_SIMPLE;
6396 } else {
6397 val = xmlRelaxNGGroupContentType(val, ret);
6398 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00006399
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006400 }
Daniel Veillard4c004142003-10-07 11:33:24 +00006401 return (val);
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006402}
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006403
6404/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00006405 * xmlRelaxNGParseGrammar:
6406 * @ctxt: a Relax-NG parser context
6407 * @nodes: grammar children nodes
6408 *
6409 * parse a Relax-NG <grammar> node
6410 *
6411 * Returns the internal xmlRelaxNGGrammarPtr built or
6412 * NULL in case of error
6413 */
6414static xmlRelaxNGGrammarPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006415xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
6416{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006417 xmlRelaxNGGrammarPtr ret, tmp, old;
6418
Daniel Veillardc482e262003-02-26 14:48:48 +00006419#ifdef DEBUG_GRAMMAR
6420 xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n");
6421#endif
6422
Daniel Veillard6eadf632003-01-23 18:29:16 +00006423 ret = xmlRelaxNGNewGrammar(ctxt);
6424 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006425 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006426
6427 /*
6428 * Link the new grammar in the tree
6429 */
6430 ret->parent = ctxt->grammar;
6431 if (ctxt->grammar != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006432 tmp = ctxt->grammar->children;
6433 if (tmp == NULL) {
6434 ctxt->grammar->children = ret;
6435 } else {
6436 while (tmp->next != NULL)
6437 tmp = tmp->next;
6438 tmp->next = ret;
6439 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006440 }
6441
6442 old = ctxt->grammar;
6443 ctxt->grammar = ret;
6444 xmlRelaxNGParseGrammarContent(ctxt, nodes);
6445 ctxt->grammar = ret;
Daniel Veillard2df2de22003-02-17 23:34:33 +00006446 if (ctxt->grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006447 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
6448 "Failed to parse <grammar> content\n", NULL, NULL);
Daniel Veillard2df2de22003-02-17 23:34:33 +00006449 } else if (ctxt->grammar->start == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006450 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_NO_START,
6451 "Element <grammar> has no <start>\n", NULL, NULL);
Daniel Veillard2df2de22003-02-17 23:34:33 +00006452 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006453
6454 /*
6455 * Apply 4.17 mergingd rules to defines and starts
6456 */
6457 xmlRelaxNGCombineStart(ctxt, ret);
6458 if (ret->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006459 xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine,
6460 ctxt);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006461 }
6462
6463 /*
6464 * link together defines and refs in this grammar
6465 */
6466 if (ret->refs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006467 xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference,
6468 ctxt);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006469 }
Daniel Veillard952379b2003-03-17 15:37:12 +00006470
Daniel Veillard6eadf632003-01-23 18:29:16 +00006471 ctxt->grammar = old;
Daniel Veillard4c004142003-10-07 11:33:24 +00006472 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006473}
6474
6475/**
6476 * xmlRelaxNGParseDocument:
6477 * @ctxt: a Relax-NG parser context
6478 * @node: the root node of the RelaxNG schema
6479 *
6480 * parse a Relax-NG definition resource and build an internal
6481 * xmlRelaxNG struture which can be used to validate instances.
6482 *
6483 * Returns the internal XML RelaxNG structure built or
6484 * NULL in case of error
6485 */
6486static xmlRelaxNGPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006487xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
6488{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006489 xmlRelaxNGPtr schema = NULL;
Daniel Veillard276be4a2003-01-24 01:03:34 +00006490 const xmlChar *olddefine;
Daniel Veillarde431a272003-01-29 23:02:33 +00006491 xmlRelaxNGGrammarPtr old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006492
6493 if ((ctxt == NULL) || (node == NULL))
6494 return (NULL);
6495
6496 schema = xmlRelaxNGNewRelaxNG(ctxt);
6497 if (schema == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006498 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006499
Daniel Veillard276be4a2003-01-24 01:03:34 +00006500 olddefine = ctxt->define;
6501 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006502 if (IS_RELAXNG(node, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006503 schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006504 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00006505 xmlRelaxNGGrammarPtr tmp, ret;
Daniel Veillardc482e262003-02-26 14:48:48 +00006506
Daniel Veillard4c004142003-10-07 11:33:24 +00006507 schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt);
6508 if (schema->topgrammar == NULL) {
6509 return (schema);
6510 }
6511 /*
6512 * Link the new grammar in the tree
6513 */
6514 ret->parent = ctxt->grammar;
6515 if (ctxt->grammar != NULL) {
6516 tmp = ctxt->grammar->children;
6517 if (tmp == NULL) {
6518 ctxt->grammar->children = ret;
6519 } else {
6520 while (tmp->next != NULL)
6521 tmp = tmp->next;
6522 tmp->next = ret;
6523 }
6524 }
6525 old = ctxt->grammar;
6526 ctxt->grammar = ret;
6527 xmlRelaxNGParseStart(ctxt, node);
6528 if (old != NULL)
6529 ctxt->grammar = old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006530 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00006531 ctxt->define = olddefine;
Daniel Veillardd4310742003-02-18 21:12:46 +00006532 if (schema->topgrammar->start != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006533 xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0);
6534 if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) {
6535 xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL);
6536 while ((schema->topgrammar->start != NULL) &&
6537 (schema->topgrammar->start->type == XML_RELAXNG_NOOP) &&
6538 (schema->topgrammar->start->next != NULL))
6539 schema->topgrammar->start =
6540 schema->topgrammar->start->content;
6541 xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start,
6542 XML_RELAXNG_IN_START, XML_RELAXNG_NOOP);
6543 }
Daniel Veillardd4310742003-02-18 21:12:46 +00006544 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006545#ifdef DEBUG
6546 if (schema == NULL)
6547 xmlGenericError(xmlGenericErrorContext,
6548 "xmlRelaxNGParseDocument() failed\n");
6549#endif
6550
6551 return (schema);
6552}
6553
6554/************************************************************************
6555 * *
6556 * Reading RelaxNGs *
6557 * *
6558 ************************************************************************/
6559
6560/**
6561 * xmlRelaxNGNewParserCtxt:
6562 * @URL: the location of the schema
6563 *
6564 * Create an XML RelaxNGs parse context for that file/resource expected
6565 * to contain an XML RelaxNGs file.
6566 *
6567 * Returns the parser context or NULL in case of error
6568 */
6569xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006570xmlRelaxNGNewParserCtxt(const char *URL)
6571{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006572 xmlRelaxNGParserCtxtPtr ret;
6573
6574 if (URL == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006575 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006576
Daniel Veillard4c004142003-10-07 11:33:24 +00006577 ret =
6578 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard6eadf632003-01-23 18:29:16 +00006579 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006580 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00006581 return (NULL);
6582 }
6583 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard4c004142003-10-07 11:33:24 +00006584 ret->URL = xmlStrdup((const xmlChar *) URL);
Daniel Veillard1703c5f2003-02-10 14:28:44 +00006585 ret->error = xmlGenericError;
6586 ret->userData = xmlGenericErrorContext;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006587 return (ret);
6588}
6589
6590/**
6591 * xmlRelaxNGNewMemParserCtxt:
6592 * @buffer: a pointer to a char array containing the schemas
6593 * @size: the size of the array
6594 *
6595 * Create an XML RelaxNGs parse context for that memory buffer expected
6596 * to contain an XML RelaxNGs file.
6597 *
6598 * Returns the parser context or NULL in case of error
6599 */
6600xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006601xmlRelaxNGNewMemParserCtxt(const char *buffer, int size)
6602{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006603 xmlRelaxNGParserCtxtPtr ret;
6604
6605 if ((buffer == NULL) || (size <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00006606 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006607
Daniel Veillard4c004142003-10-07 11:33:24 +00006608 ret =
6609 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard6eadf632003-01-23 18:29:16 +00006610 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006611 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00006612 return (NULL);
6613 }
6614 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6615 ret->buffer = buffer;
6616 ret->size = size;
Daniel Veillard1703c5f2003-02-10 14:28:44 +00006617 ret->error = xmlGenericError;
6618 ret->userData = xmlGenericErrorContext;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006619 return (ret);
6620}
6621
6622/**
Daniel Veillard33300b42003-04-17 09:09:19 +00006623 * xmlRelaxNGNewDocParserCtxt:
6624 * @doc: a preparsed document tree
6625 *
6626 * Create an XML RelaxNGs parser context for that document.
6627 * Note: since the process of compiling a RelaxNG schemas modifies the
6628 * document, the @doc parameter is duplicated internally.
6629 *
6630 * Returns the parser context or NULL in case of error
6631 */
6632xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006633xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc)
6634{
Daniel Veillard33300b42003-04-17 09:09:19 +00006635 xmlRelaxNGParserCtxtPtr ret;
6636 xmlDocPtr copy;
6637
6638 if (doc == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006639 return (NULL);
Daniel Veillard33300b42003-04-17 09:09:19 +00006640 copy = xmlCopyDoc(doc, 1);
6641 if (copy == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006642 return (NULL);
Daniel Veillard33300b42003-04-17 09:09:19 +00006643
Daniel Veillard4c004142003-10-07 11:33:24 +00006644 ret =
6645 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard33300b42003-04-17 09:09:19 +00006646 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006647 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard33300b42003-04-17 09:09:19 +00006648 return (NULL);
6649 }
6650 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6651 ret->document = copy;
6652 ret->userData = xmlGenericErrorContext;
6653 return (ret);
6654}
6655
6656/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00006657 * xmlRelaxNGFreeParserCtxt:
6658 * @ctxt: the schema parser context
6659 *
6660 * Free the resources associated to the schema parser context
6661 */
6662void
Daniel Veillard4c004142003-10-07 11:33:24 +00006663xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt)
6664{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006665 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006666 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006667 if (ctxt->URL != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006668 xmlFree(ctxt->URL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006669 if (ctxt->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006670 xmlRelaxNGFreeDocument(ctxt->doc);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00006671 if (ctxt->interleaves != NULL)
6672 xmlHashFree(ctxt->interleaves, NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006673 if (ctxt->documents != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006674 xmlRelaxNGFreeDocumentList(ctxt->documents);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006675 if (ctxt->includes != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006676 xmlRelaxNGFreeIncludeList(ctxt->includes);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006677 if (ctxt->docTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006678 xmlFree(ctxt->docTab);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00006679 if (ctxt->incTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006680 xmlFree(ctxt->incTab);
Daniel Veillard419a7682003-02-03 23:22:49 +00006681 if (ctxt->defTab != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006682 int i;
Daniel Veillard419a7682003-02-03 23:22:49 +00006683
Daniel Veillard4c004142003-10-07 11:33:24 +00006684 for (i = 0; i < ctxt->defNr; i++)
6685 xmlRelaxNGFreeDefine(ctxt->defTab[i]);
6686 xmlFree(ctxt->defTab);
Daniel Veillard419a7682003-02-03 23:22:49 +00006687 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006688 xmlFree(ctxt);
6689}
6690
Daniel Veillard6eadf632003-01-23 18:29:16 +00006691/**
Daniel Veillardd2298792003-02-14 16:54:11 +00006692 * xmlRelaxNGNormExtSpace:
6693 * @value: a value
6694 *
6695 * Removes the leading and ending spaces of the value
6696 * The string is modified "in situ"
6697 */
6698static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006699xmlRelaxNGNormExtSpace(xmlChar * value)
6700{
Daniel Veillardd2298792003-02-14 16:54:11 +00006701 xmlChar *start = value;
6702 xmlChar *cur = value;
Daniel Veillardd2298792003-02-14 16:54:11 +00006703
Daniel Veillard4c004142003-10-07 11:33:24 +00006704 if (value == NULL)
6705 return;
6706
William M. Brack76e95df2003-10-18 16:20:14 +00006707 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006708 cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +00006709 if (cur == start) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006710 do {
William M. Brack76e95df2003-10-18 16:20:14 +00006711 while ((*cur != 0) && (!IS_BLANK_CH(*cur)))
Daniel Veillard4c004142003-10-07 11:33:24 +00006712 cur++;
6713 if (*cur == 0)
6714 return;
6715 start = cur;
William M. Brack76e95df2003-10-18 16:20:14 +00006716 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006717 cur++;
6718 if (*cur == 0) {
6719 *start = 0;
6720 return;
6721 }
6722 } while (1);
Daniel Veillardd2298792003-02-14 16:54:11 +00006723 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00006724 do {
William M. Brack76e95df2003-10-18 16:20:14 +00006725 while ((*cur != 0) && (!IS_BLANK_CH(*cur)))
Daniel Veillard4c004142003-10-07 11:33:24 +00006726 *start++ = *cur++;
6727 if (*cur == 0) {
6728 *start = 0;
6729 return;
6730 }
6731 /* don't try to normalize the inner spaces */
William M. Brack76e95df2003-10-18 16:20:14 +00006732 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006733 cur++;
Daniel Veillard4c004142003-10-07 11:33:24 +00006734 if (*cur == 0) {
6735 *start = 0;
6736 return;
6737 }
Daniel Veillard4aede2e2003-10-17 12:43:59 +00006738 *start++ = *cur++;
Daniel Veillard4c004142003-10-07 11:33:24 +00006739 } while (1);
Daniel Veillardd2298792003-02-14 16:54:11 +00006740 }
6741}
6742
6743/**
6744 * xmlRelaxNGCheckAttributes:
6745 * @ctxt: a Relax-NG parser context
6746 * @node: a Relax-NG node
6747 *
6748 * Check all the attributes on the given node
6749 */
6750static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006751xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
6752{
Daniel Veillardd2298792003-02-14 16:54:11 +00006753 xmlAttrPtr cur, next;
6754
6755 cur = node->properties;
6756 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006757 next = cur->next;
6758 if ((cur->ns == NULL) ||
6759 (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
6760 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
6761 if ((!xmlStrEqual(node->name, BAD_CAST "element")) &&
6762 (!xmlStrEqual(node->name, BAD_CAST "attribute")) &&
6763 (!xmlStrEqual(node->name, BAD_CAST "ref")) &&
6764 (!xmlStrEqual(node->name, BAD_CAST "parentRef")) &&
6765 (!xmlStrEqual(node->name, BAD_CAST "param")) &&
6766 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6767 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6768 "Attribute %s is not allowed on %s\n",
6769 cur->name, node->name);
6770 }
6771 } else if (xmlStrEqual(cur->name, BAD_CAST "type")) {
6772 if ((!xmlStrEqual(node->name, BAD_CAST "value")) &&
6773 (!xmlStrEqual(node->name, BAD_CAST "data"))) {
6774 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6775 "Attribute %s is not allowed on %s\n",
6776 cur->name, node->name);
6777 }
6778 } else if (xmlStrEqual(cur->name, BAD_CAST "href")) {
6779 if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) &&
6780 (!xmlStrEqual(node->name, BAD_CAST "include"))) {
6781 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6782 "Attribute %s is not allowed on %s\n",
6783 cur->name, node->name);
6784 }
6785 } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) {
6786 if ((!xmlStrEqual(node->name, BAD_CAST "start")) &&
6787 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6788 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6789 "Attribute %s is not allowed on %s\n",
6790 cur->name, node->name);
6791 }
6792 } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) {
6793 xmlChar *val;
6794 xmlURIPtr uri;
Daniel Veillardd2298792003-02-14 16:54:11 +00006795
Daniel Veillard4c004142003-10-07 11:33:24 +00006796 val = xmlNodeListGetString(node->doc, cur->children, 1);
6797 if (val != NULL) {
6798 if (val[0] != 0) {
6799 uri = xmlParseURI((const char *) val);
6800 if (uri == NULL) {
6801 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_URI,
6802 "Attribute %s contains invalid URI %s\n",
6803 cur->name, val);
6804 } else {
6805 if (uri->scheme == NULL) {
6806 xmlRngPErr(ctxt, node, XML_RNGP_URI_NOT_ABSOLUTE,
6807 "Attribute %s URI %s is not absolute\n",
6808 cur->name, val);
6809 }
6810 if (uri->fragment != NULL) {
6811 xmlRngPErr(ctxt, node, XML_RNGP_URI_FRAGMENT,
6812 "Attribute %s URI %s has a fragment ID\n",
6813 cur->name, val);
6814 }
6815 xmlFreeURI(uri);
6816 }
6817 }
6818 xmlFree(val);
6819 }
6820 } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) {
6821 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_ATTRIBUTE,
6822 "Unknown attribute %s on %s\n", cur->name,
6823 node->name);
6824 }
6825 }
6826 cur = next;
Daniel Veillardd2298792003-02-14 16:54:11 +00006827 }
6828}
6829
6830/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00006831 * xmlRelaxNGCleanupTree:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006832 * @ctxt: a Relax-NG parser context
Daniel Veillardc5312d72003-02-21 17:14:10 +00006833 * @root: an xmlNodePtr subtree
Daniel Veillard6eadf632003-01-23 18:29:16 +00006834 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00006835 * Cleanup the subtree from unwanted nodes for parsing, resolve
6836 * Include and externalRef lookups.
Daniel Veillard6eadf632003-01-23 18:29:16 +00006837 */
Daniel Veillardc5312d72003-02-21 17:14:10 +00006838static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006839xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root)
6840{
Daniel Veillardc5312d72003-02-21 17:14:10 +00006841 xmlNodePtr cur, delete;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006842
Daniel Veillard6eadf632003-01-23 18:29:16 +00006843 delete = NULL;
6844 cur = root;
6845 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006846 if (delete != NULL) {
6847 xmlUnlinkNode(delete);
6848 xmlFreeNode(delete);
6849 delete = NULL;
6850 }
6851 if (cur->type == XML_ELEMENT_NODE) {
6852 /*
6853 * Simplification 4.1. Annotations
6854 */
6855 if ((cur->ns == NULL) ||
6856 (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
6857 if ((cur->parent != NULL) &&
6858 (cur->parent->type == XML_ELEMENT_NODE) &&
6859 ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) ||
6860 (xmlStrEqual(cur->parent->name, BAD_CAST "value")) ||
6861 (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) {
6862 xmlRngPErr(ctxt, cur, XML_RNGP_FOREIGN_ELEMENT,
6863 "element %s doesn't allow foreign elements\n",
6864 cur->parent->name, NULL);
6865 }
6866 delete = cur;
6867 goto skip_children;
6868 } else {
6869 xmlRelaxNGCleanupAttributes(ctxt, cur);
6870 if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) {
6871 xmlChar *href, *ns, *base, *URL;
6872 xmlRelaxNGDocumentPtr docu;
6873 xmlNodePtr tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006874
Daniel Veillard4c004142003-10-07 11:33:24 +00006875 ns = xmlGetProp(cur, BAD_CAST "ns");
6876 if (ns == NULL) {
6877 tmp = cur->parent;
6878 while ((tmp != NULL) &&
6879 (tmp->type == XML_ELEMENT_NODE)) {
6880 ns = xmlGetProp(tmp, BAD_CAST "ns");
6881 if (ns != NULL)
6882 break;
6883 tmp = tmp->parent;
6884 }
6885 }
6886 href = xmlGetProp(cur, BAD_CAST "href");
6887 if (href == NULL) {
6888 xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
6889 "xmlRelaxNGParse: externalRef has no href attribute\n",
6890 NULL, NULL);
6891 delete = cur;
6892 goto skip_children;
6893 }
6894 base = xmlNodeGetBase(cur->doc, cur);
6895 URL = xmlBuildURI(href, base);
6896 if (URL == NULL) {
6897 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
6898 "Failed to compute URL for externalRef %s\n",
6899 href, NULL);
6900 if (href != NULL)
6901 xmlFree(href);
6902 if (base != NULL)
6903 xmlFree(base);
6904 delete = cur;
6905 goto skip_children;
6906 }
6907 if (href != NULL)
6908 xmlFree(href);
6909 if (base != NULL)
6910 xmlFree(base);
6911 docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns);
6912 if (docu == NULL) {
6913 xmlRngPErr(ctxt, cur, XML_RNGP_EXTERNAL_REF_FAILURE,
6914 "Failed to load externalRef %s\n", URL,
6915 NULL);
6916 xmlFree(URL);
6917 delete = cur;
6918 goto skip_children;
6919 }
6920 if (ns != NULL)
6921 xmlFree(ns);
6922 xmlFree(URL);
Daniel Veillard807daf82004-02-22 22:13:27 +00006923 cur->psvi = docu;
Daniel Veillard4c004142003-10-07 11:33:24 +00006924 } else if (xmlStrEqual(cur->name, BAD_CAST "include")) {
6925 xmlChar *href, *ns, *base, *URL;
6926 xmlRelaxNGIncludePtr incl;
6927 xmlNodePtr tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006928
Daniel Veillard4c004142003-10-07 11:33:24 +00006929 href = xmlGetProp(cur, BAD_CAST "href");
6930 if (href == NULL) {
6931 xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
6932 "xmlRelaxNGParse: include has no href attribute\n",
6933 NULL, NULL);
6934 delete = cur;
6935 goto skip_children;
6936 }
6937 base = xmlNodeGetBase(cur->doc, cur);
6938 URL = xmlBuildURI(href, base);
6939 if (URL == NULL) {
6940 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
6941 "Failed to compute URL for include %s\n",
6942 href, NULL);
6943 if (href != NULL)
6944 xmlFree(href);
6945 if (base != NULL)
6946 xmlFree(base);
6947 delete = cur;
6948 goto skip_children;
6949 }
6950 if (href != NULL)
6951 xmlFree(href);
6952 if (base != NULL)
6953 xmlFree(base);
6954 ns = xmlGetProp(cur, BAD_CAST "ns");
6955 if (ns == NULL) {
6956 tmp = cur->parent;
6957 while ((tmp != NULL) &&
6958 (tmp->type == XML_ELEMENT_NODE)) {
6959 ns = xmlGetProp(tmp, BAD_CAST "ns");
6960 if (ns != NULL)
6961 break;
6962 tmp = tmp->parent;
6963 }
6964 }
6965 incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns);
6966 if (ns != NULL)
6967 xmlFree(ns);
6968 if (incl == NULL) {
6969 xmlRngPErr(ctxt, cur, XML_RNGP_INCLUDE_FAILURE,
6970 "Failed to load include %s\n", URL,
6971 NULL);
6972 xmlFree(URL);
6973 delete = cur;
6974 goto skip_children;
6975 }
6976 xmlFree(URL);
Daniel Veillard807daf82004-02-22 22:13:27 +00006977 cur->psvi = incl;
Daniel Veillard4c004142003-10-07 11:33:24 +00006978 } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) ||
6979 (xmlStrEqual(cur->name, BAD_CAST "attribute")))
6980 {
6981 xmlChar *name, *ns;
6982 xmlNodePtr text = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006983
Daniel Veillard4c004142003-10-07 11:33:24 +00006984 /*
6985 * Simplification 4.8. name attribute of element
6986 * and attribute elements
6987 */
6988 name = xmlGetProp(cur, BAD_CAST "name");
6989 if (name != NULL) {
6990 if (cur->children == NULL) {
6991 text =
6992 xmlNewChild(cur, cur->ns, BAD_CAST "name",
6993 name);
6994 } else {
6995 xmlNodePtr node;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006996
Daniel Veillard4c004142003-10-07 11:33:24 +00006997 node = xmlNewNode(cur->ns, BAD_CAST "name");
6998 if (node != NULL) {
6999 xmlAddPrevSibling(cur->children, node);
7000 text = xmlNewText(name);
7001 xmlAddChild(node, text);
7002 text = node;
7003 }
7004 }
7005 if (text == NULL) {
7006 xmlRngPErr(ctxt, cur, XML_RNGP_CREATE_FAILURE,
7007 "Failed to create a name %s element\n",
7008 name, NULL);
7009 }
7010 xmlUnsetProp(cur, BAD_CAST "name");
7011 xmlFree(name);
7012 ns = xmlGetProp(cur, BAD_CAST "ns");
7013 if (ns != NULL) {
7014 if (text != NULL) {
7015 xmlSetProp(text, BAD_CAST "ns", ns);
7016 /* xmlUnsetProp(cur, BAD_CAST "ns"); */
7017 }
7018 xmlFree(ns);
7019 } else if (xmlStrEqual(cur->name,
7020 BAD_CAST "attribute")) {
7021 xmlSetProp(text, BAD_CAST "ns", BAD_CAST "");
7022 }
7023 }
7024 } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) ||
7025 (xmlStrEqual(cur->name, BAD_CAST "nsName")) ||
7026 (xmlStrEqual(cur->name, BAD_CAST "value"))) {
7027 /*
7028 * Simplification 4.8. name attribute of element
7029 * and attribute elements
7030 */
7031 if (xmlHasProp(cur, BAD_CAST "ns") == NULL) {
7032 xmlNodePtr node;
7033 xmlChar *ns = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007034
Daniel Veillard4c004142003-10-07 11:33:24 +00007035 node = cur->parent;
7036 while ((node != NULL) &&
7037 (node->type == XML_ELEMENT_NODE)) {
7038 ns = xmlGetProp(node, BAD_CAST "ns");
7039 if (ns != NULL) {
7040 break;
7041 }
7042 node = node->parent;
7043 }
7044 if (ns == NULL) {
7045 xmlSetProp(cur, BAD_CAST "ns", BAD_CAST "");
7046 } else {
7047 xmlSetProp(cur, BAD_CAST "ns", ns);
7048 xmlFree(ns);
7049 }
7050 }
7051 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
7052 xmlChar *name, *local, *prefix;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007053
Daniel Veillard4c004142003-10-07 11:33:24 +00007054 /*
7055 * Simplification: 4.10. QNames
7056 */
7057 name = xmlNodeGetContent(cur);
7058 if (name != NULL) {
7059 local = xmlSplitQName2(name, &prefix);
7060 if (local != NULL) {
7061 xmlNsPtr ns;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007062
Daniel Veillard4c004142003-10-07 11:33:24 +00007063 ns = xmlSearchNs(cur->doc, cur, prefix);
7064 if (ns == NULL) {
7065 xmlRngPErr(ctxt, cur,
7066 XML_RNGP_PREFIX_UNDEFINED,
7067 "xmlRelaxNGParse: no namespace for prefix %s\n",
7068 prefix, NULL);
7069 } else {
7070 xmlSetProp(cur, BAD_CAST "ns",
7071 ns->href);
7072 xmlNodeSetContent(cur, local);
7073 }
7074 xmlFree(local);
7075 xmlFree(prefix);
7076 }
7077 xmlFree(name);
7078 }
7079 }
7080 /*
7081 * 4.16
7082 */
7083 if (xmlStrEqual(cur->name, BAD_CAST "nsName")) {
7084 if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7085 xmlRngPErr(ctxt, cur,
7086 XML_RNGP_PAT_NSNAME_EXCEPT_NSNAME,
7087 "Found nsName/except//nsName forbidden construct\n",
7088 NULL, NULL);
7089 }
7090 }
7091 } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) &&
7092 (cur != root)) {
7093 int oldflags = ctxt->flags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007094
Daniel Veillard4c004142003-10-07 11:33:24 +00007095 /*
7096 * 4.16
7097 */
7098 if ((cur->parent != NULL) &&
7099 (xmlStrEqual
7100 (cur->parent->name, BAD_CAST "anyName"))) {
7101 ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT;
7102 xmlRelaxNGCleanupTree(ctxt, cur);
7103 ctxt->flags = oldflags;
7104 goto skip_children;
7105 } else if ((cur->parent != NULL) &&
7106 (xmlStrEqual
7107 (cur->parent->name, BAD_CAST "nsName"))) {
7108 ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT;
7109 xmlRelaxNGCleanupTree(ctxt, cur);
7110 ctxt->flags = oldflags;
7111 goto skip_children;
7112 }
7113 } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) {
7114 /*
7115 * 4.16
7116 */
7117 if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) {
7118 xmlRngPErr(ctxt, cur,
7119 XML_RNGP_PAT_ANYNAME_EXCEPT_ANYNAME,
7120 "Found anyName/except//anyName forbidden construct\n",
7121 NULL, NULL);
7122 } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7123 xmlRngPErr(ctxt, cur,
7124 XML_RNGP_PAT_NSNAME_EXCEPT_ANYNAME,
7125 "Found nsName/except//anyName forbidden construct\n",
7126 NULL, NULL);
7127 }
7128 }
7129 /*
7130 * Thisd is not an else since "include" is transformed
7131 * into a div
7132 */
7133 if (xmlStrEqual(cur->name, BAD_CAST "div")) {
7134 xmlChar *ns;
7135 xmlNodePtr child, ins, tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007136
Daniel Veillard4c004142003-10-07 11:33:24 +00007137 /*
7138 * implements rule 4.11
7139 */
Daniel Veillard6eadf632003-01-23 18:29:16 +00007140
Daniel Veillard4c004142003-10-07 11:33:24 +00007141 ns = xmlGetProp(cur, BAD_CAST "ns");
7142
7143 child = cur->children;
7144 ins = cur;
7145 while (child != NULL) {
7146 if (ns != NULL) {
7147 if (!xmlHasProp(child, BAD_CAST "ns")) {
7148 xmlSetProp(child, BAD_CAST "ns", ns);
7149 }
7150 }
7151 tmp = child->next;
7152 xmlUnlinkNode(child);
7153 ins = xmlAddNextSibling(ins, child);
7154 child = tmp;
7155 }
7156 if (ns != NULL)
7157 xmlFree(ns);
7158 delete = cur;
7159 goto skip_children;
7160 }
7161 }
7162 }
7163 /*
7164 * Simplification 4.2 whitespaces
7165 */
7166 else if ((cur->type == XML_TEXT_NODE) ||
7167 (cur->type == XML_CDATA_SECTION_NODE)) {
7168 if (IS_BLANK_NODE(cur)) {
7169 if (cur->parent->type == XML_ELEMENT_NODE) {
7170 if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value"))
7171 &&
7172 (!xmlStrEqual
7173 (cur->parent->name, BAD_CAST "param")))
7174 delete = cur;
7175 } else {
7176 delete = cur;
7177 goto skip_children;
7178 }
7179 }
7180 } else {
7181 delete = cur;
7182 goto skip_children;
7183 }
7184
7185 /*
7186 * Skip to next node
7187 */
7188 if (cur->children != NULL) {
7189 if ((cur->children->type != XML_ENTITY_DECL) &&
7190 (cur->children->type != XML_ENTITY_REF_NODE) &&
7191 (cur->children->type != XML_ENTITY_NODE)) {
7192 cur = cur->children;
7193 continue;
7194 }
7195 }
7196 skip_children:
7197 if (cur->next != NULL) {
7198 cur = cur->next;
7199 continue;
7200 }
7201
7202 do {
7203 cur = cur->parent;
7204 if (cur == NULL)
7205 break;
7206 if (cur == root) {
7207 cur = NULL;
7208 break;
7209 }
7210 if (cur->next != NULL) {
7211 cur = cur->next;
7212 break;
7213 }
7214 } while (cur != NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007215 }
7216 if (delete != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007217 xmlUnlinkNode(delete);
7218 xmlFreeNode(delete);
7219 delete = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007220 }
Daniel Veillardc5312d72003-02-21 17:14:10 +00007221}
Daniel Veillard6eadf632003-01-23 18:29:16 +00007222
Daniel Veillardc5312d72003-02-21 17:14:10 +00007223/**
7224 * xmlRelaxNGCleanupDoc:
7225 * @ctxt: a Relax-NG parser context
7226 * @doc: an xmldocPtr document pointer
7227 *
7228 * Cleanup the document from unwanted nodes for parsing, resolve
7229 * Include and externalRef lookups.
7230 *
7231 * Returns the cleaned up document or NULL in case of error
7232 */
7233static xmlDocPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00007234xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc)
7235{
Daniel Veillardc5312d72003-02-21 17:14:10 +00007236 xmlNodePtr root;
7237
7238 /*
7239 * Extract the root
7240 */
7241 root = xmlDocGetRootElement(doc);
7242 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007243 xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
7244 ctxt->URL, NULL);
Daniel Veillardc5312d72003-02-21 17:14:10 +00007245 return (NULL);
7246 }
7247 xmlRelaxNGCleanupTree(ctxt, root);
Daniel Veillard4c004142003-10-07 11:33:24 +00007248 return (doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007249}
7250
7251/**
7252 * xmlRelaxNGParse:
7253 * @ctxt: a Relax-NG parser context
7254 *
7255 * parse a schema definition resource and build an internal
7256 * XML Shema struture which can be used to validate instances.
7257 * *WARNING* this interface is highly subject to change
7258 *
7259 * Returns the internal XML RelaxNG structure built from the resource or
7260 * NULL in case of error
7261 */
7262xmlRelaxNGPtr
7263xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt)
7264{
7265 xmlRelaxNGPtr ret = NULL;
7266 xmlDocPtr doc;
7267 xmlNodePtr root;
7268
7269 xmlRelaxNGInitTypes();
7270
7271 if (ctxt == NULL)
7272 return (NULL);
7273
7274 /*
7275 * First step is to parse the input document into an DOM/Infoset
7276 */
7277 if (ctxt->URL != NULL) {
Daniel Veillard87247e82004-01-13 20:42:02 +00007278 doc = xmlReadFile((const char *) ctxt->URL,NULL,0);
Daniel Veillard4c004142003-10-07 11:33:24 +00007279 if (doc == NULL) {
7280 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7281 "xmlRelaxNGParse: could not load %s\n", ctxt->URL,
7282 NULL);
7283 return (NULL);
7284 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007285 } else if (ctxt->buffer != NULL) {
Daniel Veillard87247e82004-01-13 20:42:02 +00007286 doc = xmlReadMemory(ctxt->buffer, ctxt->size,NULL,NULL,0);
Daniel Veillard4c004142003-10-07 11:33:24 +00007287 if (doc == NULL) {
7288 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7289 "xmlRelaxNGParse: could not parse schemas\n", NULL,
7290 NULL);
7291 return (NULL);
7292 }
7293 doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
7294 ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
Daniel Veillard33300b42003-04-17 09:09:19 +00007295 } else if (ctxt->document != NULL) {
7296 doc = ctxt->document;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007297 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007298 xmlRngPErr(ctxt, NULL, XML_RNGP_EMPTY,
7299 "xmlRelaxNGParse: nothing to parse\n", NULL, NULL);
7300 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007301 }
7302 ctxt->document = doc;
7303
7304 /*
7305 * Some preprocessing of the document content
7306 */
7307 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
7308 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007309 xmlFreeDoc(ctxt->document);
7310 ctxt->document = NULL;
7311 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007312 }
7313
Daniel Veillard6eadf632003-01-23 18:29:16 +00007314 /*
7315 * Then do the parsing for good
7316 */
7317 root = xmlDocGetRootElement(doc);
7318 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007319 xmlRngPErr(ctxt, (xmlNodePtr) doc,
7320 XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
7321 ctxt->URL, NULL);
7322 xmlFreeDoc(doc);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007323 return (NULL);
7324 }
7325 ret = xmlRelaxNGParseDocument(ctxt, root);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007326 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007327 xmlFreeDoc(doc);
7328 return (NULL);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007329 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00007330
7331 /*
Daniel Veillardfd573f12003-03-16 17:52:32 +00007332 * Check the ref/defines links
7333 */
7334 /*
7335 * try to preprocess interleaves
7336 */
7337 if (ctxt->interleaves != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007338 xmlHashScan(ctxt->interleaves,
7339 (xmlHashScanner) xmlRelaxNGComputeInterleaves, ctxt);
Daniel Veillardfd573f12003-03-16 17:52:32 +00007340 }
7341
7342 /*
Daniel Veillard6eadf632003-01-23 18:29:16 +00007343 * if there was a parsing error return NULL
7344 */
7345 if (ctxt->nbErrors > 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007346 xmlRelaxNGFree(ret);
7347 ctxt->document = NULL;
7348 xmlFreeDoc(doc);
7349 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007350 }
7351
7352 /*
Daniel Veillard52b48c72003-04-13 19:53:42 +00007353 * try to compile (parts of) the schemas
7354 */
Daniel Veillardce192eb2003-04-16 15:58:05 +00007355 if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) {
7356 if (ret->topgrammar->start->type != XML_RELAXNG_START) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007357 xmlRelaxNGDefinePtr def;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007358
Daniel Veillard4c004142003-10-07 11:33:24 +00007359 def = xmlRelaxNGNewDefine(ctxt, NULL);
7360 if (def != NULL) {
7361 def->type = XML_RELAXNG_START;
7362 def->content = ret->topgrammar->start;
7363 ret->topgrammar->start = def;
7364 }
7365 }
7366 xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007367 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00007368
7369 /*
Daniel Veillard6eadf632003-01-23 18:29:16 +00007370 * Transfer the pointer for cleanup at the schema level.
7371 */
7372 ret->doc = doc;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007373 ctxt->document = NULL;
7374 ret->documents = ctxt->documents;
7375 ctxt->documents = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00007376
Daniel Veillarde2a5a082003-02-02 14:35:17 +00007377 ret->includes = ctxt->includes;
7378 ctxt->includes = NULL;
Daniel Veillard419a7682003-02-03 23:22:49 +00007379 ret->defNr = ctxt->defNr;
7380 ret->defTab = ctxt->defTab;
7381 ctxt->defTab = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007382 if (ctxt->idref == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00007383 ret->idref = 1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007384
7385 return (ret);
7386}
Daniel Veillard4c004142003-10-07 11:33:24 +00007387
Daniel Veillard6eadf632003-01-23 18:29:16 +00007388/**
7389 * xmlRelaxNGSetParserErrors:
7390 * @ctxt: a Relax-NG validation context
7391 * @err: the error callback
7392 * @warn: the warning callback
7393 * @ctx: contextual data for the callbacks
7394 *
7395 * Set the callback functions used to handle errors for a validation context
7396 */
7397void
7398xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007399 xmlRelaxNGValidityErrorFunc err,
7400 xmlRelaxNGValidityWarningFunc warn, void *ctx)
7401{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007402 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007403 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007404 ctxt->error = err;
7405 ctxt->warning = warn;
7406 ctxt->userData = ctx;
7407}
Daniel Veillard409a8142003-07-18 15:16:57 +00007408
7409/**
7410 * xmlRelaxNGGetParserErrors:
7411 * @ctxt: a Relax-NG validation context
7412 * @err: the error callback result
7413 * @warn: the warning callback result
7414 * @ctx: contextual data for the callbacks result
7415 *
7416 * Get the callback information used to handle errors for a validation context
7417 *
7418 * Returns -1 in case of failure, 0 otherwise.
7419 */
7420int
7421xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007422 xmlRelaxNGValidityErrorFunc * err,
7423 xmlRelaxNGValidityWarningFunc * warn, void **ctx)
7424{
Daniel Veillard409a8142003-07-18 15:16:57 +00007425 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007426 return (-1);
7427 if (err != NULL)
7428 *err = ctxt->error;
7429 if (warn != NULL)
7430 *warn = ctxt->warning;
7431 if (ctx != NULL)
7432 *ctx = ctxt->userData;
7433 return (0);
Daniel Veillard409a8142003-07-18 15:16:57 +00007434}
7435
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007436#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard4c004142003-10-07 11:33:24 +00007437
Daniel Veillard6eadf632003-01-23 18:29:16 +00007438/************************************************************************
7439 * *
7440 * Dump back a compiled form *
7441 * *
7442 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007443static void xmlRelaxNGDumpDefine(FILE * output,
7444 xmlRelaxNGDefinePtr define);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007445
7446/**
7447 * xmlRelaxNGDumpDefines:
7448 * @output: the file output
7449 * @defines: a list of define structures
7450 *
7451 * Dump a RelaxNG structure back
7452 */
7453static void
Daniel Veillard4c004142003-10-07 11:33:24 +00007454xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines)
7455{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007456 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007457 xmlRelaxNGDumpDefine(output, defines);
7458 defines = defines->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007459 }
7460}
7461
7462/**
7463 * xmlRelaxNGDumpDefine:
7464 * @output: the file output
7465 * @define: a define structure
7466 *
7467 * Dump a RelaxNG structure back
7468 */
7469static void
Daniel Veillard4c004142003-10-07 11:33:24 +00007470xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define)
7471{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007472 if (define == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007473 return;
7474 switch (define->type) {
Daniel Veillard6eadf632003-01-23 18:29:16 +00007475 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00007476 fprintf(output, "<empty/>\n");
7477 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007478 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillard4c004142003-10-07 11:33:24 +00007479 fprintf(output, "<notAllowed/>\n");
7480 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007481 case XML_RELAXNG_TEXT:
Daniel Veillard4c004142003-10-07 11:33:24 +00007482 fprintf(output, "<text/>\n");
7483 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007484 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00007485 fprintf(output, "<element>\n");
7486 if (define->name != NULL) {
7487 fprintf(output, "<name");
7488 if (define->ns != NULL)
7489 fprintf(output, " ns=\"%s\"", define->ns);
7490 fprintf(output, ">%s</name>\n", define->name);
7491 }
7492 xmlRelaxNGDumpDefines(output, define->attrs);
7493 xmlRelaxNGDumpDefines(output, define->content);
7494 fprintf(output, "</element>\n");
7495 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007496 case XML_RELAXNG_LIST:
Daniel Veillard4c004142003-10-07 11:33:24 +00007497 fprintf(output, "<list>\n");
7498 xmlRelaxNGDumpDefines(output, define->content);
7499 fprintf(output, "</list>\n");
7500 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007501 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007502 fprintf(output, "<oneOrMore>\n");
7503 xmlRelaxNGDumpDefines(output, define->content);
7504 fprintf(output, "</oneOrMore>\n");
7505 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007506 case XML_RELAXNG_ZEROORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007507 fprintf(output, "<zeroOrMore>\n");
7508 xmlRelaxNGDumpDefines(output, define->content);
7509 fprintf(output, "</zeroOrMore>\n");
7510 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007511 case XML_RELAXNG_CHOICE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007512 fprintf(output, "<choice>\n");
7513 xmlRelaxNGDumpDefines(output, define->content);
7514 fprintf(output, "</choice>\n");
7515 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007516 case XML_RELAXNG_GROUP:
Daniel Veillard4c004142003-10-07 11:33:24 +00007517 fprintf(output, "<group>\n");
7518 xmlRelaxNGDumpDefines(output, define->content);
7519 fprintf(output, "</group>\n");
7520 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007521 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007522 fprintf(output, "<interleave>\n");
7523 xmlRelaxNGDumpDefines(output, define->content);
7524 fprintf(output, "</interleave>\n");
7525 break;
7526 case XML_RELAXNG_OPTIONAL:
7527 fprintf(output, "<optional>\n");
7528 xmlRelaxNGDumpDefines(output, define->content);
7529 fprintf(output, "</optional>\n");
7530 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007531 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007532 fprintf(output, "<attribute>\n");
7533 xmlRelaxNGDumpDefines(output, define->content);
7534 fprintf(output, "</attribute>\n");
7535 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007536 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007537 fprintf(output, "<define");
7538 if (define->name != NULL)
7539 fprintf(output, " name=\"%s\"", define->name);
7540 fprintf(output, ">\n");
7541 xmlRelaxNGDumpDefines(output, define->content);
7542 fprintf(output, "</define>\n");
7543 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007544 case XML_RELAXNG_REF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007545 fprintf(output, "<ref");
7546 if (define->name != NULL)
7547 fprintf(output, " name=\"%s\"", define->name);
7548 fprintf(output, ">\n");
7549 xmlRelaxNGDumpDefines(output, define->content);
7550 fprintf(output, "</ref>\n");
7551 break;
Daniel Veillard419a7682003-02-03 23:22:49 +00007552 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007553 fprintf(output, "<parentRef");
7554 if (define->name != NULL)
7555 fprintf(output, " name=\"%s\"", define->name);
7556 fprintf(output, ">\n");
7557 xmlRelaxNGDumpDefines(output, define->content);
7558 fprintf(output, "</parentRef>\n");
7559 break;
7560 case XML_RELAXNG_EXTERNALREF:
7561 fprintf(output, "<externalRef>");
7562 xmlRelaxNGDumpDefines(output, define->content);
7563 fprintf(output, "</externalRef>\n");
7564 break;
Daniel Veillarde431a272003-01-29 23:02:33 +00007565 case XML_RELAXNG_DATATYPE:
Daniel Veillard6eadf632003-01-23 18:29:16 +00007566 case XML_RELAXNG_VALUE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007567 TODO break;
7568 case XML_RELAXNG_START:
7569 case XML_RELAXNG_EXCEPT:
7570 case XML_RELAXNG_PARAM:
7571 TODO break;
7572 case XML_RELAXNG_NOOP:
7573 xmlRelaxNGDumpDefines(output, define->content);
7574 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007575 }
7576}
Daniel Veillard4c004142003-10-07 11:33:24 +00007577
Daniel Veillard6eadf632003-01-23 18:29:16 +00007578/**
7579 * xmlRelaxNGDumpGrammar:
7580 * @output: the file output
7581 * @grammar: a grammar structure
7582 * @top: is this a top grammar
7583 *
7584 * Dump a RelaxNG structure back
7585 */
7586static void
7587xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top)
7588{
7589 if (grammar == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007590 return;
7591
Daniel Veillard6eadf632003-01-23 18:29:16 +00007592 fprintf(output, "<grammar");
7593 if (top)
Daniel Veillard4c004142003-10-07 11:33:24 +00007594 fprintf(output, " xmlns=\"http://relaxng.org/ns/structure/1.0\"");
7595 switch (grammar->combine) {
7596 case XML_RELAXNG_COMBINE_UNDEFINED:
7597 break;
7598 case XML_RELAXNG_COMBINE_CHOICE:
7599 fprintf(output, " combine=\"choice\"");
7600 break;
7601 case XML_RELAXNG_COMBINE_INTERLEAVE:
7602 fprintf(output, " combine=\"interleave\"");
7603 break;
7604 default:
7605 fprintf(output, " <!-- invalid combine value -->");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007606 }
7607 fprintf(output, ">\n");
7608 if (grammar->start == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007609 fprintf(output, " <!-- grammar had no start -->");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007610 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007611 fprintf(output, "<start>\n");
7612 xmlRelaxNGDumpDefine(output, grammar->start);
7613 fprintf(output, "</start>\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007614 }
7615 /* TODO ? Dump the defines ? */
7616 fprintf(output, "</grammar>\n");
7617}
7618
7619/**
7620 * xmlRelaxNGDump:
7621 * @output: the file output
7622 * @schema: a schema structure
7623 *
7624 * Dump a RelaxNG structure back
7625 */
7626void
7627xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema)
7628{
7629 if (schema == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007630 fprintf(output, "RelaxNG empty or failed to compile\n");
7631 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007632 }
7633 fprintf(output, "RelaxNG: ");
7634 if (schema->doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007635 fprintf(output, "no document\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007636 } else if (schema->doc->URL != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007637 fprintf(output, "%s\n", schema->doc->URL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007638 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007639 fprintf(output, "\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007640 }
7641 if (schema->topgrammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007642 fprintf(output, "RelaxNG has no top grammar\n");
7643 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007644 }
7645 xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1);
7646}
7647
Daniel Veillardfebcca42003-02-16 15:44:18 +00007648/**
7649 * xmlRelaxNGDumpTree:
7650 * @output: the file output
7651 * @schema: a schema structure
7652 *
7653 * Dump the transformed RelaxNG tree.
7654 */
7655void
7656xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema)
7657{
7658 if (schema == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007659 fprintf(output, "RelaxNG empty or failed to compile\n");
7660 return;
Daniel Veillardfebcca42003-02-16 15:44:18 +00007661 }
7662 if (schema->doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007663 fprintf(output, "no document\n");
Daniel Veillardfebcca42003-02-16 15:44:18 +00007664 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007665 xmlDocDump(output, schema->doc);
Daniel Veillardfebcca42003-02-16 15:44:18 +00007666 }
7667}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007668#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardfebcca42003-02-16 15:44:18 +00007669
Daniel Veillard6eadf632003-01-23 18:29:16 +00007670/************************************************************************
7671 * *
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007672 * Validation of compiled content *
Daniel Veillard6eadf632003-01-23 18:29:16 +00007673 * *
7674 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007675static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
7676 xmlRelaxNGDefinePtr define);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007677
7678/**
7679 * xmlRelaxNGValidateCompiledCallback:
7680 * @exec: the regular expression instance
7681 * @token: the token which matched
7682 * @transdata: callback data, the define for the subelement if available
7683 @ @inputdata: callback data, the Relax NG validation context
7684 *
7685 * Handle the callback and if needed validate the element children.
7686 */
Daniel Veillard4c004142003-10-07 11:33:24 +00007687static void
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007688xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00007689 const xmlChar * token,
7690 void *transdata, void *inputdata)
7691{
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007692 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
7693 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
7694 int ret;
7695
7696#ifdef DEBUG_COMPILE
7697 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00007698 "Compiled callback for: '%s'\n", token);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007699#endif
7700 if (ctxt == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007701 fprintf(stderr, "callback on %s missing context\n", token);
7702 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7703 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7704 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007705 }
7706 if (define == NULL) {
7707 if (token[0] == '#')
Daniel Veillard4c004142003-10-07 11:33:24 +00007708 return;
7709 fprintf(stderr, "callback on %s missing define\n", token);
7710 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7711 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7712 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007713 }
7714 if ((ctxt == NULL) || (define == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007715 fprintf(stderr, "callback on %s missing info\n", token);
7716 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7717 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7718 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007719 } else if (define->type != XML_RELAXNG_ELEMENT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007720 fprintf(stderr, "callback on %s define is not element\n", token);
7721 if (ctxt->errNo == XML_RELAXNG_OK)
7722 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7723 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007724 }
7725 ret = xmlRelaxNGValidateDefinition(ctxt, define);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007726 if (ret != 0)
7727 ctxt->perr = ret;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007728}
7729
7730/**
7731 * xmlRelaxNGValidateCompiledContent:
7732 * @ctxt: the RelaxNG validation context
7733 * @regexp: the regular expression as compiled
7734 * @content: list of children to test against the regexp
7735 *
7736 * Validate the content model of an element or start using the regexp
7737 *
7738 * Returns 0 in case of success, -1 in case of error.
7739 */
7740static int
7741xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007742 xmlRegexpPtr regexp, xmlNodePtr content)
7743{
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007744 xmlRegExecCtxtPtr exec;
7745 xmlNodePtr cur;
Daniel Veillard62163602003-04-17 09:36:38 +00007746 int ret = 0;
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007747 int oldperr = ctxt->perr;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007748
7749 if ((ctxt == NULL) || (regexp == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00007750 return (-1);
7751 exec = xmlRegNewExecCtxt(regexp,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007752 xmlRelaxNGValidateCompiledCallback, ctxt);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007753 ctxt->perr = 0;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007754 cur = content;
7755 while (cur != NULL) {
7756 ctxt->state->seq = cur;
Daniel Veillard4c004142003-10-07 11:33:24 +00007757 switch (cur->type) {
7758 case XML_TEXT_NODE:
7759 case XML_CDATA_SECTION_NODE:
7760 if (xmlIsBlankNode(cur))
7761 break;
7762 ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt);
7763 if (ret < 0) {
7764 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG,
7765 cur->parent->name);
7766 }
7767 break;
7768 case XML_ELEMENT_NODE:
7769 if (cur->ns != NULL) {
7770 ret = xmlRegExecPushString2(exec, cur->name,
7771 cur->ns->href, ctxt);
7772 } else {
7773 ret = xmlRegExecPushString(exec, cur->name, ctxt);
7774 }
7775 if (ret < 0) {
7776 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name);
7777 }
7778 break;
7779 default:
7780 break;
7781 }
7782 if (ret < 0)
7783 break;
7784 /*
7785 * Switch to next element
7786 */
7787 cur = cur->next;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007788 }
7789 ret = xmlRegExecPushString(exec, NULL, NULL);
7790 if (ret == 1) {
7791 ret = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00007792 ctxt->state->seq = NULL;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007793 } else if (ret == 0) {
7794 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00007795 * TODO: get some of the names needed to exit the current state of exec
7796 */
7797 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
7798 ret = -1;
7799 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
7800 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007801 } else {
7802 ret = -1;
7803 }
7804 xmlRegFreeExecCtxt(exec);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007805 /*
7806 * There might be content model errors outside of the pure
7807 * regexp validation, e.g. for attribute values.
7808 */
7809 if ((ret == 0) && (ctxt->perr != 0)) {
7810 ret = ctxt->perr;
7811 }
7812 ctxt->perr = oldperr;
Daniel Veillard4c004142003-10-07 11:33:24 +00007813 return (ret);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007814}
7815
7816/************************************************************************
7817 * *
7818 * Progressive validation of when possible *
7819 * *
7820 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007821static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
7822 xmlRelaxNGDefinePtr defines);
7823static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt,
William M. Brack272693c2003-11-14 16:20:34 +00007824 int dolog);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00007825static void xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007826
7827/**
7828 * xmlRelaxNGElemPush:
7829 * @ctxt: the validation context
7830 * @exec: the regexp runtime for the new content model
7831 *
7832 * Push a new regexp for the current node content model on the stack
7833 *
7834 * Returns 0 in case of success and -1 in case of error.
7835 */
7836static int
Daniel Veillard4c004142003-10-07 11:33:24 +00007837xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec)
7838{
Daniel Veillardf4e55762003-04-15 23:32:22 +00007839 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007840 ctxt->elemMax = 10;
7841 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax *
7842 sizeof
7843 (xmlRegExecCtxtPtr));
Daniel Veillardf4e55762003-04-15 23:32:22 +00007844 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007845 xmlRngVErrMemory(ctxt, "validating\n");
7846 return (-1);
7847 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00007848 }
7849 if (ctxt->elemNr >= ctxt->elemMax) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007850 ctxt->elemMax *= 2;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007851 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00007852 ctxt->elemMax *
7853 sizeof
7854 (xmlRegExecCtxtPtr));
Daniel Veillardf4e55762003-04-15 23:32:22 +00007855 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007856 xmlRngVErrMemory(ctxt, "validating\n");
7857 return (-1);
7858 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00007859 }
7860 ctxt->elemTab[ctxt->elemNr++] = exec;
7861 ctxt->elem = exec;
Daniel Veillard4c004142003-10-07 11:33:24 +00007862 return (0);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007863}
7864
7865/**
7866 * xmlRelaxNGElemPop:
7867 * @ctxt: the validation context
7868 *
7869 * Pop the regexp of the current node content model from the stack
7870 *
7871 * Returns the exec or NULL if empty
7872 */
7873static xmlRegExecCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00007874xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt)
7875{
Daniel Veillardf4e55762003-04-15 23:32:22 +00007876 xmlRegExecCtxtPtr ret;
7877
Daniel Veillard4c004142003-10-07 11:33:24 +00007878 if (ctxt->elemNr <= 0)
7879 return (NULL);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007880 ctxt->elemNr--;
7881 ret = ctxt->elemTab[ctxt->elemNr];
7882 ctxt->elemTab[ctxt->elemNr] = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00007883 if (ctxt->elemNr > 0)
Daniel Veillardf4e55762003-04-15 23:32:22 +00007884 ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1];
7885 else
Daniel Veillard4c004142003-10-07 11:33:24 +00007886 ctxt->elem = NULL;
7887 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007888}
7889
7890/**
7891 * xmlRelaxNGValidateProgressiveCallback:
7892 * @exec: the regular expression instance
7893 * @token: the token which matched
7894 * @transdata: callback data, the define for the subelement if available
7895 @ @inputdata: callback data, the Relax NG validation context
7896 *
7897 * Handle the callback and if needed validate the element children.
7898 * some of the in/out informations are passed via the context in @inputdata.
7899 */
Daniel Veillard4c004142003-10-07 11:33:24 +00007900static void
7901xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec
7902 ATTRIBUTE_UNUSED,
7903 const xmlChar * token,
7904 void *transdata, void *inputdata)
7905{
Daniel Veillardf4e55762003-04-15 23:32:22 +00007906 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
7907 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
Daniel Veillardce192eb2003-04-16 15:58:05 +00007908 xmlRelaxNGValidStatePtr state, oldstate;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007909 xmlNodePtr node = ctxt->pnode;
Daniel Veillardc3ca5ba2003-05-09 22:26:28 +00007910 int ret = 0, oldflags;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007911
7912#ifdef DEBUG_PROGRESSIVE
7913 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00007914 "Progressive callback for: '%s'\n", token);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007915#endif
7916 if (ctxt == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007917 fprintf(stderr, "callback on %s missing context\n", token);
7918 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007919 }
7920 ctxt->pstate = 1;
7921 if (define == NULL) {
7922 if (token[0] == '#')
Daniel Veillard4c004142003-10-07 11:33:24 +00007923 return;
7924 fprintf(stderr, "callback on %s missing define\n", token);
7925 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7926 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7927 ctxt->pstate = -1;
7928 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007929 }
7930 if ((ctxt == NULL) || (define == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007931 fprintf(stderr, "callback on %s missing info\n", token);
7932 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7933 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7934 ctxt->pstate = -1;
7935 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007936 } else if (define->type != XML_RELAXNG_ELEMENT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007937 fprintf(stderr, "callback on %s define is not element\n", token);
7938 if (ctxt->errNo == XML_RELAXNG_OK)
7939 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7940 ctxt->pstate = -1;
7941 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007942 }
7943 if (node->type != XML_ELEMENT_NODE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007944 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
7945 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
7946 xmlRelaxNGDumpValidError(ctxt);
7947 ctxt->pstate = -1;
7948 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007949 }
7950 if (define->contModel == NULL) {
7951 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00007952 * this node cannot be validated in a streamable fashion
7953 */
Daniel Veillardf4e55762003-04-15 23:32:22 +00007954#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00007955 xmlGenericError(xmlGenericErrorContext,
7956 "Element '%s' validation is not streamable\n",
7957 token);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007958#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00007959 ctxt->pstate = 0;
7960 ctxt->pdef = define;
7961 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007962 }
7963 exec = xmlRegNewExecCtxt(define->contModel,
Daniel Veillard4c004142003-10-07 11:33:24 +00007964 xmlRelaxNGValidateProgressiveCallback, ctxt);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007965 if (exec == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007966 ctxt->pstate = -1;
7967 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007968 }
7969 xmlRelaxNGElemPush(ctxt, exec);
7970
7971 /*
7972 * Validate the attributes part of the content.
7973 */
7974 state = xmlRelaxNGNewValidState(ctxt, node);
7975 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007976 ctxt->pstate = -1;
7977 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007978 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00007979 oldstate = ctxt->state;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007980 ctxt->state = state;
7981 if (define->attrs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007982 ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
7983 if (ret != 0) {
7984 ctxt->pstate = -1;
7985 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
7986 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00007987 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00007988 if (ctxt->state != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007989 ctxt->state->seq = NULL;
7990 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
7991 if (ret != 0) {
7992 ctxt->pstate = -1;
7993 }
7994 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardce192eb2003-04-16 15:58:05 +00007995 } else if (ctxt->states != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007996 int tmp = -1, i;
Daniel Veillardce192eb2003-04-16 15:58:05 +00007997
7998 oldflags = ctxt->flags;
Daniel Veillardce192eb2003-04-16 15:58:05 +00007999
Daniel Veillard4c004142003-10-07 11:33:24 +00008000 for (i = 0; i < ctxt->states->nbState; i++) {
8001 state = ctxt->states->tabState[i];
8002 ctxt->state = state;
8003 ctxt->state->seq = NULL;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008004
Daniel Veillard4c004142003-10-07 11:33:24 +00008005 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
8006 tmp = 0;
8007 break;
8008 }
8009 }
8010 if (tmp != 0) {
8011 /*
8012 * validation error, log the message for the "best" one
8013 */
8014 ctxt->flags |= FLAGS_IGNORABLE;
8015 xmlRelaxNGLogBestError(ctxt);
8016 }
8017 for (i = 0; i < ctxt->states->nbState; i++) {
8018 xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[i]);
8019 }
8020 xmlRelaxNGFreeStates(ctxt, ctxt->states);
8021 ctxt->states = NULL;
8022 if ((ret == 0) && (tmp == -1))
8023 ctxt->pstate = -1;
8024 ctxt->flags = oldflags;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008025 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008026 if (ctxt->pstate == -1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008027 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
8028 xmlRelaxNGDumpValidError(ctxt);
8029 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008030 }
8031 ctxt->state = oldstate;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008032}
8033
8034/**
8035 * xmlRelaxNGValidatePushElement:
8036 * @ctxt: the validation context
8037 * @doc: a document instance
8038 * @elem: an element instance
8039 *
8040 * Push a new element start on the RelaxNG validation stack.
8041 *
8042 * returns 1 if no validation problem was found or 0 if validating the
8043 * element requires a full node, and -1 in case of error.
8044 */
8045int
Daniel Veillard33300b42003-04-17 09:09:19 +00008046xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt,
8047 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillardf4e55762003-04-15 23:32:22 +00008048 xmlNodePtr elem)
8049{
8050 int ret = 1;
8051
8052 if ((ctxt == NULL) || (elem == NULL))
8053 return (-1);
8054
8055#ifdef DEBUG_PROGRESSIVE
8056 xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name);
8057#endif
8058 if (ctxt->elem == 0) {
8059 xmlRelaxNGPtr schema;
8060 xmlRelaxNGGrammarPtr grammar;
8061 xmlRegExecCtxtPtr exec;
8062 xmlRelaxNGDefinePtr define;
8063
8064 schema = ctxt->schema;
8065 if (schema == NULL) {
8066 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8067 return (-1);
8068 }
8069 grammar = schema->topgrammar;
8070 if ((grammar == NULL) || (grammar->start == NULL)) {
8071 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8072 return (-1);
8073 }
8074 define = grammar->start;
8075 if (define->contModel == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008076 ctxt->pdef = define;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008077 return (0);
8078 }
8079 exec = xmlRegNewExecCtxt(define->contModel,
8080 xmlRelaxNGValidateProgressiveCallback,
8081 ctxt);
8082 if (exec == NULL) {
8083 return (-1);
8084 }
8085 xmlRelaxNGElemPush(ctxt, exec);
8086 }
8087 ctxt->pnode = elem;
8088 ctxt->pstate = 0;
8089 if (elem->ns != NULL) {
8090 ret =
8091 xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href,
8092 ctxt);
8093 } else {
8094 ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt);
8095 }
8096 if (ret < 0) {
8097 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name);
8098 } else {
8099 if (ctxt->pstate == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008100 ret = 0;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008101 else if (ctxt->pstate < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008102 ret = -1;
8103 else
8104 ret = 1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008105 }
8106#ifdef DEBUG_PROGRESSIVE
8107 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008108 xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n",
8109 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008110#endif
8111 return (ret);
8112}
8113
8114/**
8115 * xmlRelaxNGValidatePushCData:
8116 * @ctxt: the RelaxNG validation context
8117 * @data: some character data read
8118 * @len: the lenght of the data
8119 *
8120 * check the CData parsed for validation in the current stack
8121 *
8122 * returns 1 if no validation problem was found or -1 otherwise
8123 */
8124int
8125xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00008126 const xmlChar * data, int len ATTRIBUTE_UNUSED)
Daniel Veillardf4e55762003-04-15 23:32:22 +00008127{
8128 int ret = 1;
8129
8130 if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL))
8131 return (-1);
8132
8133#ifdef DEBUG_PROGRESSIVE
8134 xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len);
8135#endif
8136
8137 while (*data != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008138 if (!IS_BLANK_CH(*data))
Daniel Veillardf4e55762003-04-15 23:32:22 +00008139 break;
8140 data++;
8141 }
8142 if (*data == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008143 return (1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008144
8145 ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt);
8146 if (ret < 0) {
Daniel Veillard33300b42003-04-17 09:09:19 +00008147 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO ");
Daniel Veillardf4e55762003-04-15 23:32:22 +00008148#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00008149 xmlGenericError(xmlGenericErrorContext, "CDATA failed\n");
Daniel Veillardf4e55762003-04-15 23:32:22 +00008150#endif
8151
Daniel Veillard4c004142003-10-07 11:33:24 +00008152 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008153 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008154 return (1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008155}
8156
8157/**
8158 * xmlRelaxNGValidatePopElement:
8159 * @ctxt: the RelaxNG validation context
8160 * @doc: a document instance
8161 * @elem: an element instance
8162 *
8163 * Pop the element end from the RelaxNG validation stack.
8164 *
8165 * returns 1 if no validation problem was found or 0 otherwise
8166 */
8167int
8168xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt,
8169 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008170 xmlNodePtr elem)
8171{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008172 int ret;
8173 xmlRegExecCtxtPtr exec;
8174
Daniel Veillard4c004142003-10-07 11:33:24 +00008175 if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL))
8176 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008177#ifdef DEBUG_PROGRESSIVE
8178 xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name);
8179#endif
8180 /*
8181 * verify that we reached a terminal state of the content model.
8182 */
8183 exec = xmlRelaxNGElemPop(ctxt);
8184 ret = xmlRegExecPushString(exec, NULL, NULL);
8185 if (ret == 0) {
8186 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00008187 * TODO: get some of the names needed to exit the current state of exec
8188 */
8189 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
8190 ret = -1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008191 } else if (ret < 0) {
8192 ret = -1;
8193 } else {
8194 ret = 1;
8195 }
8196 xmlRegFreeExecCtxt(exec);
8197#ifdef DEBUG_PROGRESSIVE
8198 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008199 xmlGenericError(xmlGenericErrorContext, "PopElem %s failed\n",
8200 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008201#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008202 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008203}
8204
8205/**
8206 * xmlRelaxNGValidateFullElement:
8207 * @ctxt: the validation context
8208 * @doc: a document instance
8209 * @elem: an element instance
8210 *
8211 * Validate a full subtree when xmlRelaxNGValidatePushElement() returned
8212 * 0 and the content of the node has been expanded.
8213 *
8214 * returns 1 if no validation problem was found or -1 in case of error.
8215 */
8216int
Daniel Veillard33300b42003-04-17 09:09:19 +00008217xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt,
8218 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008219 xmlNodePtr elem)
8220{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008221 int ret;
8222 xmlRelaxNGValidStatePtr state;
8223
Daniel Veillard4c004142003-10-07 11:33:24 +00008224 if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL))
8225 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008226#ifdef DEBUG_PROGRESSIVE
8227 xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name);
8228#endif
8229 state = xmlRelaxNGNewValidState(ctxt, elem->parent);
8230 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008231 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008232 }
8233 state->seq = elem;
8234 ctxt->state = state;
8235 ctxt->errNo = XML_RELAXNG_OK;
8236 ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef);
Daniel Veillard4c004142003-10-07 11:33:24 +00008237 if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK))
8238 ret = -1;
8239 else
8240 ret = 1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008241 xmlRelaxNGFreeValidState(ctxt, state);
8242 ctxt->state = NULL;
8243#ifdef DEBUG_PROGRESSIVE
8244 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008245 xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n",
8246 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008247#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008248 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008249}
8250
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008251/************************************************************************
8252 * *
8253 * Generic interpreted validation implementation *
8254 * *
8255 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00008256static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8257 xmlRelaxNGDefinePtr define);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008258
8259/**
8260 * xmlRelaxNGSkipIgnored:
8261 * @ctxt: a schema validation context
8262 * @node: the top node.
8263 *
8264 * Skip ignorable nodes in that context
8265 *
8266 * Returns the new sibling or NULL in case of error.
8267 */
8268static xmlNodePtr
8269xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008270 xmlNodePtr node)
8271{
Daniel Veillard6eadf632003-01-23 18:29:16 +00008272 /*
8273 * TODO complete and handle entities
8274 */
8275 while ((node != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00008276 ((node->type == XML_COMMENT_NODE) ||
8277 (node->type == XML_PI_NODE) ||
William M. Brack7217c862004-03-15 02:43:56 +00008278 (node->type == XML_XINCLUDE_START) ||
8279 (node->type == XML_XINCLUDE_END) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00008280 (((node->type == XML_TEXT_NODE) ||
8281 (node->type == XML_CDATA_SECTION_NODE)) &&
8282 ((ctxt->flags & FLAGS_MIXED_CONTENT) ||
8283 (IS_BLANK_NODE(node)))))) {
8284 node = node->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008285 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008286 return (node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008287}
8288
8289/**
Daniel Veillardedc91922003-01-26 00:52:04 +00008290 * xmlRelaxNGNormalize:
8291 * @ctxt: a schema validation context
8292 * @str: the string to normalize
8293 *
8294 * Implements the normalizeWhiteSpace( s ) function from
8295 * section 6.2.9 of the spec
8296 *
8297 * Returns the new string or NULL in case of error.
8298 */
8299static xmlChar *
Daniel Veillard4c004142003-10-07 11:33:24 +00008300xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar * str)
8301{
Daniel Veillardedc91922003-01-26 00:52:04 +00008302 xmlChar *ret, *p;
8303 const xmlChar *tmp;
8304 int len;
Daniel Veillard4c004142003-10-07 11:33:24 +00008305
Daniel Veillardedc91922003-01-26 00:52:04 +00008306 if (str == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00008307 return (NULL);
Daniel Veillardedc91922003-01-26 00:52:04 +00008308 tmp = str;
Daniel Veillard4c004142003-10-07 11:33:24 +00008309 while (*tmp != 0)
8310 tmp++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008311 len = tmp - str;
8312
Daniel Veillard3c908dc2003-04-19 00:07:51 +00008313 ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar));
Daniel Veillardedc91922003-01-26 00:52:04 +00008314 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008315 xmlRngVErrMemory(ctxt, "validating\n");
8316 return (NULL);
Daniel Veillardedc91922003-01-26 00:52:04 +00008317 }
8318 p = ret;
William M. Brack76e95df2003-10-18 16:20:14 +00008319 while (IS_BLANK_CH(*str))
Daniel Veillard4c004142003-10-07 11:33:24 +00008320 str++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008321 while (*str != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008322 if (IS_BLANK_CH(*str)) {
8323 while (IS_BLANK_CH(*str))
Daniel Veillard4c004142003-10-07 11:33:24 +00008324 str++;
8325 if (*str == 0)
8326 break;
8327 *p++ = ' ';
8328 } else
8329 *p++ = *str++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008330 }
8331 *p = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00008332 return (ret);
Daniel Veillardedc91922003-01-26 00:52:04 +00008333}
8334
8335/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008336 * xmlRelaxNGValidateDatatype:
8337 * @ctxt: a Relax-NG validation context
8338 * @value: the string value
8339 * @type: the datatype definition
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008340 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008341 *
8342 * Validate the given value against the dataype
8343 *
8344 * Returns 0 if the validation succeeded or an error code.
8345 */
8346static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008347xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt,
8348 const xmlChar * value,
8349 xmlRelaxNGDefinePtr define, xmlNodePtr node)
8350{
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008351 int ret, tmp;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008352 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008353 void *result = NULL;
8354 xmlRelaxNGDefinePtr cur;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008355
8356 if ((define == NULL) || (define->data == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008357 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008358 }
8359 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008360 if (lib->check != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008361 if ((define->attrs != NULL) &&
8362 (define->attrs->type == XML_RELAXNG_PARAM)) {
8363 ret =
8364 lib->check(lib->data, define->name, value, &result, node);
8365 } else {
8366 ret = lib->check(lib->data, define->name, value, NULL, node);
8367 }
8368 } else
8369 ret = -1;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008370 if (ret < 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008371 VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name);
8372 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
8373 lib->freef(lib->data, result);
8374 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008375 } else if (ret == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008376 ret = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008377 } else if (ret == 2) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008378 VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008379 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00008380 VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value);
8381 ret = -1;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008382 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008383 cur = define->attrs;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008384 while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008385 if (lib->facet != NULL) {
8386 tmp = lib->facet(lib->data, define->name, cur->name,
8387 cur->value, value, result);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008388 if (tmp != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008389 ret = -1;
8390 }
8391 cur = cur->next;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008392 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008393 if ((ret == 0) && (define->content != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008394 const xmlChar *oldvalue, *oldendvalue;
Daniel Veillard416589a2003-02-17 17:25:42 +00008395
Daniel Veillard4c004142003-10-07 11:33:24 +00008396 oldvalue = ctxt->state->value;
8397 oldendvalue = ctxt->state->endvalue;
8398 ctxt->state->value = (xmlChar *) value;
8399 ctxt->state->endvalue = NULL;
8400 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8401 ctxt->state->value = (xmlChar *) oldvalue;
8402 ctxt->state->endvalue = (xmlChar *) oldendvalue;
Daniel Veillard416589a2003-02-17 17:25:42 +00008403 }
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008404 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00008405 lib->freef(lib->data, result);
8406 return (ret);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008407}
8408
8409/**
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008410 * xmlRelaxNGNextValue:
8411 * @ctxt: a Relax-NG validation context
8412 *
8413 * Skip to the next value when validating within a list
8414 *
8415 * Returns 0 if the operation succeeded or an error code.
8416 */
8417static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008418xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt)
8419{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008420 xmlChar *cur;
8421
8422 cur = ctxt->state->value;
8423 if ((cur == NULL) || (ctxt->state->endvalue == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008424 ctxt->state->value = NULL;
8425 ctxt->state->endvalue = NULL;
8426 return (0);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008427 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008428 while (*cur != 0)
8429 cur++;
8430 while ((cur != ctxt->state->endvalue) && (*cur == 0))
8431 cur++;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008432 if (cur == ctxt->state->endvalue)
Daniel Veillard4c004142003-10-07 11:33:24 +00008433 ctxt->state->value = NULL;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008434 else
Daniel Veillard4c004142003-10-07 11:33:24 +00008435 ctxt->state->value = cur;
8436 return (0);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008437}
8438
8439/**
8440 * xmlRelaxNGValidateValueList:
8441 * @ctxt: a Relax-NG validation context
8442 * @defines: the list of definitions to verify
8443 *
8444 * Validate the given set of definitions for the current value
8445 *
8446 * Returns 0 if the validation succeeded or an error code.
8447 */
8448static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008449xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt,
8450 xmlRelaxNGDefinePtr defines)
8451{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008452 int ret = 0;
8453
8454 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008455 ret = xmlRelaxNGValidateValue(ctxt, defines);
8456 if (ret != 0)
8457 break;
8458 defines = defines->next;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008459 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008460 return (ret);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008461}
8462
8463/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00008464 * xmlRelaxNGValidateValue:
8465 * @ctxt: a Relax-NG validation context
8466 * @define: the definition to verify
8467 *
8468 * Validate the given definition for the current value
8469 *
8470 * Returns 0 if the validation succeeded or an error code.
8471 */
8472static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008473xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8474 xmlRelaxNGDefinePtr define)
8475{
Daniel Veillardedc91922003-01-26 00:52:04 +00008476 int ret = 0, oldflags;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008477 xmlChar *value;
8478
8479 value = ctxt->state->value;
8480 switch (define->type) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008481 case XML_RELAXNG_EMPTY:{
8482 if ((value != NULL) && (value[0] != 0)) {
8483 int idx = 0;
Daniel Veillardd4310742003-02-18 21:12:46 +00008484
William M. Brack76e95df2003-10-18 16:20:14 +00008485 while (IS_BLANK_CH(value[idx]))
Daniel Veillard4c004142003-10-07 11:33:24 +00008486 idx++;
8487 if (value[idx] != 0)
8488 ret = -1;
8489 }
8490 break;
8491 }
8492 case XML_RELAXNG_TEXT:
8493 break;
8494 case XML_RELAXNG_VALUE:{
8495 if (!xmlStrEqual(value, define->value)) {
8496 if (define->name != NULL) {
8497 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillardedc91922003-01-26 00:52:04 +00008498
Daniel Veillard4c004142003-10-07 11:33:24 +00008499 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
8500 if ((lib != NULL) && (lib->comp != NULL)) {
8501 ret = lib->comp(lib->data, define->name,
8502 define->value, define->node,
8503 (void *) define->attrs,
8504 value, ctxt->state->node);
8505 } else
8506 ret = -1;
8507 if (ret < 0) {
8508 VALID_ERR2(XML_RELAXNG_ERR_TYPECMP,
8509 define->name);
8510 return (-1);
8511 } else if (ret == 1) {
8512 ret = 0;
8513 } else {
8514 ret = -1;
8515 }
8516 } else {
8517 xmlChar *nval, *nvalue;
Daniel Veillardedc91922003-01-26 00:52:04 +00008518
Daniel Veillard4c004142003-10-07 11:33:24 +00008519 /*
8520 * TODO: trivial optimizations are possible by
8521 * computing at compile-time
8522 */
8523 nval = xmlRelaxNGNormalize(ctxt, define->value);
8524 nvalue = xmlRelaxNGNormalize(ctxt, value);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008525
Daniel Veillard4c004142003-10-07 11:33:24 +00008526 if ((nval == NULL) || (nvalue == NULL) ||
8527 (!xmlStrEqual(nval, nvalue)))
8528 ret = -1;
8529 if (nval != NULL)
8530 xmlFree(nval);
8531 if (nvalue != NULL)
8532 xmlFree(nvalue);
8533 }
8534 }
8535 if (ret == 0)
8536 xmlRelaxNGNextValue(ctxt);
8537 break;
8538 }
8539 case XML_RELAXNG_DATATYPE:{
8540 ret = xmlRelaxNGValidateDatatype(ctxt, value, define,
8541 ctxt->state->seq);
8542 if (ret == 0)
8543 xmlRelaxNGNextValue(ctxt);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008544
Daniel Veillard4c004142003-10-07 11:33:24 +00008545 break;
8546 }
8547 case XML_RELAXNG_CHOICE:{
8548 xmlRelaxNGDefinePtr list = define->content;
8549 xmlChar *oldvalue;
8550
8551 oldflags = ctxt->flags;
8552 ctxt->flags |= FLAGS_IGNORABLE;
8553
8554 oldvalue = ctxt->state->value;
8555 while (list != NULL) {
8556 ret = xmlRelaxNGValidateValue(ctxt, list);
8557 if (ret == 0) {
8558 break;
8559 }
8560 ctxt->state->value = oldvalue;
8561 list = list->next;
8562 }
8563 ctxt->flags = oldflags;
8564 if (ret != 0) {
8565 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8566 xmlRelaxNGDumpValidError(ctxt);
8567 } else {
8568 if (ctxt->errNr > 0)
8569 xmlRelaxNGPopErrors(ctxt, 0);
8570 }
8571 if (ret == 0)
8572 xmlRelaxNGNextValue(ctxt);
8573 break;
8574 }
8575 case XML_RELAXNG_LIST:{
8576 xmlRelaxNGDefinePtr list = define->content;
8577 xmlChar *oldvalue, *oldend, *val, *cur;
8578
Daniel Veillard416589a2003-02-17 17:25:42 +00008579#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008580 int nb_values = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00008581#endif
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008582
Daniel Veillard4c004142003-10-07 11:33:24 +00008583 oldvalue = ctxt->state->value;
8584 oldend = ctxt->state->endvalue;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008585
Daniel Veillard4c004142003-10-07 11:33:24 +00008586 val = xmlStrdup(oldvalue);
8587 if (val == NULL) {
8588 val = xmlStrdup(BAD_CAST "");
8589 }
8590 if (val == NULL) {
8591 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
8592 return (-1);
8593 }
8594 cur = val;
8595 while (*cur != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008596 if (IS_BLANK_CH(*cur)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008597 *cur = 0;
8598 cur++;
Daniel Veillard416589a2003-02-17 17:25:42 +00008599#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008600 nb_values++;
Daniel Veillard416589a2003-02-17 17:25:42 +00008601#endif
William M. Brack76e95df2003-10-18 16:20:14 +00008602 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00008603 *cur++ = 0;
8604 } else
8605 cur++;
8606 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008607#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008608 xmlGenericError(xmlGenericErrorContext,
8609 "list value: '%s' found %d items\n",
8610 oldvalue, nb_values);
8611 nb_values = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008612#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008613 ctxt->state->endvalue = cur;
8614 cur = val;
8615 while ((*cur == 0) && (cur != ctxt->state->endvalue))
8616 cur++;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008617
Daniel Veillard4c004142003-10-07 11:33:24 +00008618 ctxt->state->value = cur;
8619
8620 while (list != NULL) {
8621 if (ctxt->state->value == ctxt->state->endvalue)
8622 ctxt->state->value = NULL;
8623 ret = xmlRelaxNGValidateValue(ctxt, list);
8624 if (ret != 0) {
8625#ifdef DEBUG_LIST
8626 xmlGenericError(xmlGenericErrorContext,
8627 "Failed to validate value: '%s' with %d rule\n",
8628 ctxt->state->value, nb_values);
8629#endif
8630 break;
8631 }
8632#ifdef DEBUG_LIST
8633 nb_values++;
8634#endif
8635 list = list->next;
8636 }
8637
8638 if ((ret == 0) && (ctxt->state->value != NULL) &&
8639 (ctxt->state->value != ctxt->state->endvalue)) {
8640 VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA,
8641 ctxt->state->value);
8642 ret = -1;
8643 }
8644 xmlFree(val);
8645 ctxt->state->value = oldvalue;
8646 ctxt->state->endvalue = oldend;
8647 break;
8648 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008649 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00008650 ret = xmlRelaxNGValidateValueList(ctxt, define->content);
8651 if (ret != 0) {
8652 break;
8653 }
8654 /* no break on purpose */
8655 case XML_RELAXNG_ZEROORMORE:{
8656 xmlChar *cur, *temp;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008657
Daniel Veillard4c004142003-10-07 11:33:24 +00008658 oldflags = ctxt->flags;
8659 ctxt->flags |= FLAGS_IGNORABLE;
8660 cur = ctxt->state->value;
8661 temp = NULL;
8662 while ((cur != NULL) && (cur != ctxt->state->endvalue) &&
8663 (temp != cur)) {
8664 temp = cur;
8665 ret =
8666 xmlRelaxNGValidateValueList(ctxt, define->content);
8667 if (ret != 0) {
8668 ctxt->state->value = temp;
8669 ret = 0;
8670 break;
8671 }
8672 cur = ctxt->state->value;
8673 }
8674 ctxt->flags = oldflags;
8675 if (ret != 0) {
8676 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8677 xmlRelaxNGDumpValidError(ctxt);
8678 } else {
8679 if (ctxt->errNr > 0)
8680 xmlRelaxNGPopErrors(ctxt, 0);
8681 }
8682 break;
8683 }
8684 case XML_RELAXNG_EXCEPT:{
8685 xmlRelaxNGDefinePtr list;
Daniel Veillard416589a2003-02-17 17:25:42 +00008686
Daniel Veillard4c004142003-10-07 11:33:24 +00008687 list = define->content;
8688 while (list != NULL) {
8689 ret = xmlRelaxNGValidateValue(ctxt, list);
8690 if (ret == 0) {
8691 ret = -1;
8692 break;
8693 } else
8694 ret = 0;
8695 list = list->next;
8696 }
8697 break;
8698 }
Daniel Veillard463a5472003-02-27 21:30:32 +00008699 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00008700 case XML_RELAXNG_GROUP:{
8701 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008702
Daniel Veillard4c004142003-10-07 11:33:24 +00008703 list = define->content;
8704 while (list != NULL) {
8705 ret = xmlRelaxNGValidateValue(ctxt, list);
8706 if (ret != 0) {
8707 ret = -1;
8708 break;
8709 } else
8710 ret = 0;
8711 list = list->next;
8712 }
8713 break;
8714 }
Daniel Veillard463a5472003-02-27 21:30:32 +00008715 case XML_RELAXNG_REF:
8716 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00008717 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8718 break;
8719 default:
8720 TODO ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008721 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008722 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008723}
8724
8725/**
8726 * xmlRelaxNGValidateValueContent:
8727 * @ctxt: a Relax-NG validation context
8728 * @defines: the list of definitions to verify
8729 *
8730 * Validate the given definitions for the current value
8731 *
8732 * Returns 0 if the validation succeeded or an error code.
8733 */
8734static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008735xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt,
8736 xmlRelaxNGDefinePtr defines)
8737{
Daniel Veillard6eadf632003-01-23 18:29:16 +00008738 int ret = 0;
8739
8740 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008741 ret = xmlRelaxNGValidateValue(ctxt, defines);
8742 if (ret != 0)
8743 break;
8744 defines = defines->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008745 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008746 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008747}
8748
8749/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00008750 * xmlRelaxNGAttributeMatch:
8751 * @ctxt: a Relax-NG validation context
8752 * @define: the definition to check
8753 * @prop: the attribute
8754 *
8755 * Check if the attribute matches the definition nameClass
8756 *
8757 * Returns 1 if the attribute matches, 0 if no, or -1 in case of error
8758 */
8759static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008760xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt,
8761 xmlRelaxNGDefinePtr define, xmlAttrPtr prop)
8762{
Daniel Veillardfd573f12003-03-16 17:52:32 +00008763 int ret;
8764
8765 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008766 if (!xmlStrEqual(define->name, prop->name))
8767 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008768 }
8769 if (define->ns != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008770 if (define->ns[0] == 0) {
8771 if (prop->ns != NULL)
8772 return (0);
8773 } else {
8774 if ((prop->ns == NULL) ||
8775 (!xmlStrEqual(define->ns, prop->ns->href)))
8776 return (0);
8777 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008778 }
8779 if (define->nameClass == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00008780 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008781 define = define->nameClass;
8782 if (define->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008783 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008784
Daniel Veillard4c004142003-10-07 11:33:24 +00008785 list = define->content;
8786 while (list != NULL) {
8787 ret = xmlRelaxNGAttributeMatch(ctxt, list, prop);
8788 if (ret == 1)
8789 return (0);
8790 if (ret < 0)
8791 return (ret);
8792 list = list->next;
8793 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008794 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00008795 TODO}
8796 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008797}
8798
8799/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00008800 * xmlRelaxNGValidateAttribute:
8801 * @ctxt: a Relax-NG validation context
8802 * @define: the definition to verify
8803 *
8804 * Validate the given attribute definition for that node
8805 *
8806 * Returns 0 if the validation succeeded or an error code.
8807 */
8808static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008809xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt,
8810 xmlRelaxNGDefinePtr define)
8811{
Daniel Veillard6eadf632003-01-23 18:29:16 +00008812 int ret = 0, i;
8813 xmlChar *value, *oldvalue;
8814 xmlAttrPtr prop = NULL, tmp;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008815 xmlNodePtr oldseq;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008816
Daniel Veillard1ed7f362003-02-03 10:57:45 +00008817 if (ctxt->state->nbAttrLeft <= 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008818 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008819 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008820 for (i = 0; i < ctxt->state->nbAttrs; i++) {
8821 tmp = ctxt->state->attrs[i];
8822 if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) {
8823 if ((((define->ns == NULL) || (define->ns[0] == 0)) &&
8824 (tmp->ns == NULL)) ||
8825 ((tmp->ns != NULL) &&
8826 (xmlStrEqual(define->ns, tmp->ns->href)))) {
8827 prop = tmp;
8828 break;
8829 }
8830 }
8831 }
8832 if (prop != NULL) {
8833 value = xmlNodeListGetString(prop->doc, prop->children, 1);
8834 oldvalue = ctxt->state->value;
8835 oldseq = ctxt->state->seq;
8836 ctxt->state->seq = (xmlNodePtr) prop;
8837 ctxt->state->value = value;
8838 ctxt->state->endvalue = NULL;
8839 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
8840 if (ctxt->state->value != NULL)
8841 value = ctxt->state->value;
8842 if (value != NULL)
8843 xmlFree(value);
8844 ctxt->state->value = oldvalue;
8845 ctxt->state->seq = oldseq;
8846 if (ret == 0) {
8847 /*
8848 * flag the attribute as processed
8849 */
8850 ctxt->state->attrs[i] = NULL;
8851 ctxt->state->nbAttrLeft--;
8852 }
8853 } else {
8854 ret = -1;
8855 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00008856#ifdef DEBUG
Daniel Veillard4c004142003-10-07 11:33:24 +00008857 xmlGenericError(xmlGenericErrorContext,
8858 "xmlRelaxNGValidateAttribute(%s): %d\n",
8859 define->name, ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008860#endif
8861 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00008862 for (i = 0; i < ctxt->state->nbAttrs; i++) {
8863 tmp = ctxt->state->attrs[i];
8864 if ((tmp != NULL) &&
8865 (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) {
8866 prop = tmp;
8867 break;
8868 }
8869 }
8870 if (prop != NULL) {
8871 value = xmlNodeListGetString(prop->doc, prop->children, 1);
8872 oldvalue = ctxt->state->value;
8873 oldseq = ctxt->state->seq;
8874 ctxt->state->seq = (xmlNodePtr) prop;
8875 ctxt->state->value = value;
8876 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
8877 if (ctxt->state->value != NULL)
8878 value = ctxt->state->value;
8879 if (value != NULL)
8880 xmlFree(value);
8881 ctxt->state->value = oldvalue;
8882 ctxt->state->seq = oldseq;
8883 if (ret == 0) {
8884 /*
8885 * flag the attribute as processed
8886 */
8887 ctxt->state->attrs[i] = NULL;
8888 ctxt->state->nbAttrLeft--;
8889 }
8890 } else {
8891 ret = -1;
8892 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00008893#ifdef DEBUG
Daniel Veillard4c004142003-10-07 11:33:24 +00008894 if (define->ns != NULL) {
8895 xmlGenericError(xmlGenericErrorContext,
8896 "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n",
8897 define->ns, ret);
8898 } else {
8899 xmlGenericError(xmlGenericErrorContext,
8900 "xmlRelaxNGValidateAttribute(anyName): %d\n",
8901 ret);
8902 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00008903#endif
Daniel Veillard6eadf632003-01-23 18:29:16 +00008904 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008905
8906 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008907}
8908
8909/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00008910 * xmlRelaxNGValidateAttributeList:
8911 * @ctxt: a Relax-NG validation context
8912 * @define: the list of definition to verify
8913 *
8914 * Validate the given node against the list of attribute definitions
8915 *
8916 * Returns 0 if the validation succeeded or an error code.
8917 */
8918static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008919xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
8920 xmlRelaxNGDefinePtr defines)
8921{
Daniel Veillardce192eb2003-04-16 15:58:05 +00008922 int ret = 0, res;
8923 int needmore = 0;
8924 xmlRelaxNGDefinePtr cur;
8925
8926 cur = defines;
8927 while (cur != NULL) {
8928 if (cur->type == XML_RELAXNG_ATTRIBUTE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008929 if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0)
8930 ret = -1;
8931 } else
8932 needmore = 1;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008933 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008934 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008935 if (!needmore)
Daniel Veillard4c004142003-10-07 11:33:24 +00008936 return (ret);
Daniel Veillardce192eb2003-04-16 15:58:05 +00008937 cur = defines;
8938 while (cur != NULL) {
8939 if (cur->type != XML_RELAXNG_ATTRIBUTE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008940 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
8941 res = xmlRelaxNGValidateDefinition(ctxt, cur);
8942 if (res < 0)
8943 ret = -1;
8944 } else {
8945 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
8946 return (-1);
8947 }
8948 if (res == -1) /* continues on -2 */
8949 break;
8950 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008951 cur = cur->next;
8952 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008953
8954 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008955}
8956
8957/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00008958 * xmlRelaxNGNodeMatchesList:
8959 * @node: the node
8960 * @list: a NULL terminated array of definitions
8961 *
8962 * Check if a node can be matched by one of the definitions
8963 *
8964 * Returns 1 if matches 0 otherwise
8965 */
8966static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008967xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr * list)
8968{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00008969 xmlRelaxNGDefinePtr cur;
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00008970 int i = 0, tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00008971
8972 if ((node == NULL) || (list == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00008973 return (0);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00008974
8975 cur = list[i++];
8976 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008977 if ((node->type == XML_ELEMENT_NODE) &&
8978 (cur->type == XML_RELAXNG_ELEMENT)) {
8979 tmp = xmlRelaxNGElementMatch(NULL, cur, node);
8980 if (tmp == 1)
8981 return (1);
8982 } else if (((node->type == XML_TEXT_NODE) ||
8983 (node->type == XML_CDATA_SECTION_NODE)) &&
8984 (cur->type == XML_RELAXNG_TEXT)) {
8985 return (1);
8986 }
8987 cur = list[i++];
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00008988 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008989 return (0);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00008990}
8991
8992/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00008993 * xmlRelaxNGValidateInterleave:
8994 * @ctxt: a Relax-NG validation context
8995 * @define: the definition to verify
8996 *
8997 * Validate an interleave definition for a node.
8998 *
8999 * Returns 0 if the validation succeeded or an error code.
9000 */
9001static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009002xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt,
9003 xmlRelaxNGDefinePtr define)
9004{
William M. Brack779af002003-08-01 15:55:39 +00009005 int ret = 0, i, nbgroups;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009006 int errNr = ctxt->errNr;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009007 int oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009008
9009 xmlRelaxNGValidStatePtr oldstate;
9010 xmlRelaxNGPartitionPtr partitions;
9011 xmlRelaxNGInterleaveGroupPtr group = NULL;
9012 xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem;
9013 xmlNodePtr *list = NULL, *lasts = NULL;
9014
9015 if (define->data != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009016 partitions = (xmlRelaxNGPartitionPtr) define->data;
9017 nbgroups = partitions->nbgroups;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009018 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009019 VALID_ERR(XML_RELAXNG_ERR_INTERNODATA);
9020 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009021 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009022 /*
9023 * Optimizations for MIXED
9024 */
9025 oldflags = ctxt->flags;
Daniel Veillarde063f482003-03-21 16:53:17 +00009026 if (define->dflags & IS_MIXED) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009027 ctxt->flags |= FLAGS_MIXED_CONTENT;
9028 if (nbgroups == 2) {
9029 /*
9030 * this is a pure <mixed> case
9031 */
9032 if (ctxt->state != NULL)
9033 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
9034 ctxt->state->seq);
9035 if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT)
9036 ret = xmlRelaxNGValidateDefinition(ctxt,
9037 partitions->groups[1]->
9038 rule);
9039 else
9040 ret = xmlRelaxNGValidateDefinition(ctxt,
9041 partitions->groups[0]->
9042 rule);
9043 if (ret == 0) {
9044 if (ctxt->state != NULL)
9045 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
9046 ctxt->state->
9047 seq);
9048 }
9049 ctxt->flags = oldflags;
9050 return (ret);
9051 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009052 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009053
9054 /*
9055 * Build arrays to store the first and last node of the chain
9056 * pertaining to each group
9057 */
9058 list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9059 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009060 xmlRngVErrMemory(ctxt, "validating\n");
9061 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009062 }
9063 memset(list, 0, nbgroups * sizeof(xmlNodePtr));
9064 lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9065 if (lasts == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009066 xmlRngVErrMemory(ctxt, "validating\n");
9067 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009068 }
9069 memset(lasts, 0, nbgroups * sizeof(xmlNodePtr));
9070
9071 /*
9072 * Walk the sequence of children finding the right group and
9073 * sorting them in sequences.
9074 */
9075 cur = ctxt->state->seq;
9076 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9077 start = cur;
9078 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009079 ctxt->state->seq = cur;
9080 if ((partitions->triage != NULL) &&
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009081 (partitions->flags & IS_DETERMINIST)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009082 void *tmp = NULL;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009083
Daniel Veillard4c004142003-10-07 11:33:24 +00009084 if ((cur->type == XML_TEXT_NODE) ||
9085 (cur->type == XML_CDATA_SECTION_NODE)) {
9086 tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text",
9087 NULL);
9088 } else if (cur->type == XML_ELEMENT_NODE) {
9089 if (cur->ns != NULL) {
9090 tmp = xmlHashLookup2(partitions->triage, cur->name,
9091 cur->ns->href);
9092 if (tmp == NULL)
9093 tmp = xmlHashLookup2(partitions->triage,
9094 BAD_CAST "#any",
9095 cur->ns->href);
9096 } else
9097 tmp =
9098 xmlHashLookup2(partitions->triage, cur->name,
9099 NULL);
9100 if (tmp == NULL)
9101 tmp =
9102 xmlHashLookup2(partitions->triage, BAD_CAST "#any",
9103 NULL);
9104 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009105
Daniel Veillard4c004142003-10-07 11:33:24 +00009106 if (tmp == NULL) {
9107 i = nbgroups;
9108 } else {
9109 i = ((long) tmp) - 1;
9110 if (partitions->flags & IS_NEEDCHECK) {
9111 group = partitions->groups[i];
9112 if (!xmlRelaxNGNodeMatchesList(cur, group->defs))
9113 i = nbgroups;
9114 }
9115 }
9116 } else {
9117 for (i = 0; i < nbgroups; i++) {
9118 group = partitions->groups[i];
9119 if (group == NULL)
9120 continue;
9121 if (xmlRelaxNGNodeMatchesList(cur, group->defs))
9122 break;
9123 }
9124 }
9125 /*
9126 * We break as soon as an element not matched is found
9127 */
9128 if (i >= nbgroups) {
9129 break;
9130 }
9131 if (lasts[i] != NULL) {
9132 lasts[i]->next = cur;
9133 lasts[i] = cur;
9134 } else {
9135 list[i] = cur;
9136 lasts[i] = cur;
9137 }
9138 if (cur->next != NULL)
9139 lastchg = cur->next;
9140 else
9141 lastchg = cur;
9142 cur = xmlRelaxNGSkipIgnored(ctxt, cur->next);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009143 }
9144 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009145 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9146 ret = -1;
9147 goto done;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009148 }
9149 lastelem = cur;
9150 oldstate = ctxt->state;
Daniel Veillard4c004142003-10-07 11:33:24 +00009151 for (i = 0; i < nbgroups; i++) {
9152 ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate);
9153 group = partitions->groups[i];
9154 if (lasts[i] != NULL) {
9155 last = lasts[i]->next;
9156 lasts[i]->next = NULL;
9157 }
9158 ctxt->state->seq = list[i];
9159 ret = xmlRelaxNGValidateDefinition(ctxt, group->rule);
9160 if (ret != 0)
9161 break;
9162 if (ctxt->state != NULL) {
9163 cur = ctxt->state->seq;
9164 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9165 xmlRelaxNGFreeValidState(ctxt, oldstate);
9166 oldstate = ctxt->state;
9167 ctxt->state = NULL;
9168 if (cur != NULL) {
9169 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9170 ret = -1;
9171 ctxt->state = oldstate;
9172 goto done;
9173 }
9174 } else if (ctxt->states != NULL) {
9175 int j;
9176 int found = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009177
Daniel Veillard4c004142003-10-07 11:33:24 +00009178 for (j = 0; j < ctxt->states->nbState; j++) {
9179 cur = ctxt->states->tabState[j]->seq;
9180 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9181 if (cur == NULL) {
9182 found = 1;
9183 break;
9184 }
9185 }
9186 if (ctxt->states->nbState > 0) {
9187 xmlRelaxNGFreeValidState(ctxt, oldstate);
9188 oldstate =
9189 ctxt->states->tabState[ctxt->states->nbState - 1];
9190 }
9191 for (j = 0; j < ctxt->states->nbState - 1; j++) {
9192 xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[j]);
9193 }
9194 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9195 ctxt->states = NULL;
9196 if (found == 0) {
9197 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9198 ret = -1;
9199 ctxt->state = oldstate;
9200 goto done;
9201 }
9202 } else {
9203 ret = -1;
9204 break;
9205 }
9206 if (lasts[i] != NULL) {
9207 lasts[i]->next = last;
9208 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009209 }
9210 if (ctxt->state != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00009211 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009212 ctxt->state = oldstate;
9213 ctxt->state->seq = lastelem;
9214 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009215 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9216 ret = -1;
9217 goto done;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009218 }
9219
Daniel Veillard4c004142003-10-07 11:33:24 +00009220 done:
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009221 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009222 /*
9223 * builds the next links chain from the prev one
9224 */
9225 cur = lastchg;
9226 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009227 if ((cur == start) || (cur->prev == NULL))
9228 break;
9229 cur->prev->next = cur;
9230 cur = cur->prev;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009231 }
9232 if (ret == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009233 if (ctxt->errNr > errNr)
9234 xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009235 }
9236
9237 xmlFree(list);
9238 xmlFree(lasts);
Daniel Veillard4c004142003-10-07 11:33:24 +00009239 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009240}
9241
9242/**
9243 * xmlRelaxNGValidateDefinitionList:
9244 * @ctxt: a Relax-NG validation context
9245 * @define: the list of definition to verify
9246 *
9247 * Validate the given node content against the (list) of definitions
9248 *
9249 * Returns 0 if the validation succeeded or an error code.
9250 */
9251static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009252xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt,
9253 xmlRelaxNGDefinePtr defines)
9254{
Daniel Veillardfd573f12003-03-16 17:52:32 +00009255 int ret = 0, res;
9256
9257
Daniel Veillard952379b2003-03-17 15:37:12 +00009258 if (defines == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009259 VALID_ERR2(XML_RELAXNG_ERR_INTERNAL,
9260 BAD_CAST "NULL definition list");
9261 return (-1);
Daniel Veillard952379b2003-03-17 15:37:12 +00009262 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009263 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009264 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
9265 res = xmlRelaxNGValidateDefinition(ctxt, defines);
9266 if (res < 0)
9267 ret = -1;
9268 } else {
9269 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
9270 return (-1);
9271 }
9272 if (res == -1) /* continues on -2 */
9273 break;
9274 defines = defines->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009275 }
9276
Daniel Veillard4c004142003-10-07 11:33:24 +00009277 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009278}
9279
9280/**
9281 * xmlRelaxNGElementMatch:
Daniel Veillard416589a2003-02-17 17:25:42 +00009282 * @ctxt: a Relax-NG validation context
9283 * @define: the definition to check
Daniel Veillardfd573f12003-03-16 17:52:32 +00009284 * @elem: the element
Daniel Veillard416589a2003-02-17 17:25:42 +00009285 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00009286 * Check if the element matches the definition nameClass
Daniel Veillard416589a2003-02-17 17:25:42 +00009287 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00009288 * Returns 1 if the element matches, 0 if no, or -1 in case of error
Daniel Veillard416589a2003-02-17 17:25:42 +00009289 */
9290static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009291xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
9292 xmlRelaxNGDefinePtr define, xmlNodePtr elem)
9293{
Daniel Veillard580ced82003-03-21 21:22:48 +00009294 int ret = 0, oldflags = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00009295
Daniel Veillardfd573f12003-03-16 17:52:32 +00009296 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009297 if (!xmlStrEqual(elem->name, define->name)) {
9298 VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name);
9299 return (0);
9300 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00009301 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009302 if ((define->ns != NULL) && (define->ns[0] != 0)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009303 if (elem->ns == NULL) {
9304 VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, elem->name);
9305 return (0);
9306 } else if (!xmlStrEqual(elem->ns->href, define->ns)) {
9307 VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS,
9308 elem->name, define->ns);
9309 return (0);
9310 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009311 } else if ((elem->ns != NULL) && (define->ns != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00009312 (define->name == NULL)) {
9313 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, elem->name);
9314 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009315 } else if ((elem->ns != NULL) && (define->name != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009316 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, define->name);
9317 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009318 }
9319
9320 if (define->nameClass == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00009321 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009322
9323 define = define->nameClass;
9324 if (define->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009325 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009326
Daniel Veillard4c004142003-10-07 11:33:24 +00009327 if (ctxt != NULL) {
9328 oldflags = ctxt->flags;
9329 ctxt->flags |= FLAGS_IGNORABLE;
9330 }
9331
9332 list = define->content;
9333 while (list != NULL) {
9334 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9335 if (ret == 1) {
9336 if (ctxt != NULL)
9337 ctxt->flags = oldflags;
9338 return (0);
9339 }
9340 if (ret < 0) {
9341 if (ctxt != NULL)
9342 ctxt->flags = oldflags;
9343 return (ret);
9344 }
9345 list = list->next;
9346 }
9347 ret = 1;
9348 if (ctxt != NULL) {
9349 ctxt->flags = oldflags;
9350 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009351 } else if (define->type == XML_RELAXNG_CHOICE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009352 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009353
Daniel Veillard4c004142003-10-07 11:33:24 +00009354 if (ctxt != NULL) {
9355 oldflags = ctxt->flags;
9356 ctxt->flags |= FLAGS_IGNORABLE;
9357 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009358
Daniel Veillard4c004142003-10-07 11:33:24 +00009359 list = define->nameClass;
9360 while (list != NULL) {
9361 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9362 if (ret == 1) {
9363 if (ctxt != NULL)
9364 ctxt->flags = oldflags;
9365 return (1);
9366 }
9367 if (ret < 0) {
9368 if (ctxt != NULL)
9369 ctxt->flags = oldflags;
9370 return (ret);
9371 }
9372 list = list->next;
9373 }
9374 if (ctxt != NULL) {
9375 if (ret != 0) {
9376 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9377 xmlRelaxNGDumpValidError(ctxt);
9378 } else {
9379 if (ctxt->errNr > 0)
9380 xmlRelaxNGPopErrors(ctxt, 0);
9381 }
9382 }
9383 ret = 0;
9384 if (ctxt != NULL) {
9385 ctxt->flags = oldflags;
9386 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009387 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009388 TODO ret = -1;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009389 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009390 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009391}
9392
9393/**
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009394 * xmlRelaxNGBestState:
9395 * @ctxt: a Relax-NG validation context
9396 *
9397 * Find the "best" state in the ctxt->states list of states to report
9398 * errors about. I.e. a state with no element left in the child list
9399 * or the one with the less attributes left.
9400 * This is called only if a falidation error was detected
9401 *
9402 * Returns the index of the "best" state or -1 in case of error
9403 */
9404static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009405xmlRelaxNGBestState(xmlRelaxNGValidCtxtPtr ctxt)
9406{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009407 xmlRelaxNGValidStatePtr state;
9408 int i, tmp;
9409 int best = -1;
9410 int value = 1000000;
9411
9412 if ((ctxt == NULL) || (ctxt->states == NULL) ||
9413 (ctxt->states->nbState <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00009414 return (-1);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009415
Daniel Veillard4c004142003-10-07 11:33:24 +00009416 for (i = 0; i < ctxt->states->nbState; i++) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009417 state = ctxt->states->tabState[i];
Daniel Veillard4c004142003-10-07 11:33:24 +00009418 if (state == NULL)
9419 continue;
9420 if (state->seq != NULL) {
9421 if ((best == -1) || (value > 100000)) {
9422 value = 100000;
9423 best = i;
9424 }
9425 } else {
9426 tmp = state->nbAttrLeft;
9427 if ((best == -1) || (value > tmp)) {
9428 value = tmp;
9429 best = i;
9430 }
9431 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009432 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009433 return (best);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009434}
9435
9436/**
9437 * xmlRelaxNGLogBestError:
9438 * @ctxt: a Relax-NG validation context
9439 *
9440 * Find the "best" state in the ctxt->states list of states to report
9441 * errors about and log it.
9442 */
9443static void
Daniel Veillard4c004142003-10-07 11:33:24 +00009444xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt)
9445{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009446 int best;
9447
9448 if ((ctxt == NULL) || (ctxt->states == NULL) ||
9449 (ctxt->states->nbState <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00009450 return;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009451
9452 best = xmlRelaxNGBestState(ctxt);
9453 if ((best >= 0) && (best < ctxt->states->nbState)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009454 ctxt->state = ctxt->states->tabState[best];
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009455
Daniel Veillard4c004142003-10-07 11:33:24 +00009456 xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009457 }
9458}
9459
9460/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009461 * xmlRelaxNGValidateElementEnd:
9462 * @ctxt: a Relax-NG validation context
William M. Brack272693c2003-11-14 16:20:34 +00009463 * @dolog: indicate that error logging should be done
Daniel Veillardfd573f12003-03-16 17:52:32 +00009464 *
9465 * Validate the end of the element, implements check that
9466 * there is nothing left not consumed in the element content
9467 * or in the attribute list.
9468 *
9469 * Returns 0 if the validation succeeded or an error code.
9470 */
9471static int
William M. Brack272693c2003-11-14 16:20:34 +00009472xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int dolog)
Daniel Veillard4c004142003-10-07 11:33:24 +00009473{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009474 int i;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009475 xmlRelaxNGValidStatePtr state;
9476
9477 state = ctxt->state;
9478 if (state->seq != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009479 state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq);
9480 if (state->seq != NULL) {
William M. Brack272693c2003-11-14 16:20:34 +00009481 if (dolog) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009482 VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT,
9483 state->node->name, state->seq->name);
9484 }
9485 return (-1);
9486 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009487 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009488 for (i = 0; i < state->nbAttrs; i++) {
9489 if (state->attrs[i] != NULL) {
William M. Brack272693c2003-11-14 16:20:34 +00009490 if (dolog) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009491 VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR,
9492 state->attrs[i]->name, state->node->name);
9493 }
9494 return (-1 - i);
9495 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009496 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009497 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009498}
9499
9500/**
9501 * xmlRelaxNGValidateState:
9502 * @ctxt: a Relax-NG validation context
9503 * @define: the definition to verify
9504 *
9505 * Validate the current state against the definition
9506 *
9507 * Returns 0 if the validation succeeded or an error code.
9508 */
9509static int
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009510xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt,
9511 xmlRelaxNGDefinePtr define)
9512{
Daniel Veillardfd573f12003-03-16 17:52:32 +00009513 xmlNodePtr node;
9514 int ret = 0, i, tmp, oldflags, errNr;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009515 xmlRelaxNGValidStatePtr oldstate = NULL, state;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009516
9517 if (define == NULL) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009518 VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
9519 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009520 }
9521
9522 if (ctxt->state != NULL) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009523 node = ctxt->state->seq;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009524 } else {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009525 node = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009526 }
9527#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009528 for (i = 0; i < ctxt->depth; i++)
9529 xmlGenericError(xmlGenericErrorContext, " ");
Daniel Veillardfd573f12003-03-16 17:52:32 +00009530 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009531 "Start validating %s ", xmlRelaxNGDefName(define));
Daniel Veillardfd573f12003-03-16 17:52:32 +00009532 if (define->name != NULL)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009533 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009534 if ((node != NULL) && (node->name != NULL))
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009535 xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009536 else
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009537 xmlGenericError(xmlGenericErrorContext, "\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00009538#endif
9539 ctxt->depth++;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009540 switch (define->type) {
9541 case XML_RELAXNG_EMPTY:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009542 node = xmlRelaxNGSkipIgnored(ctxt, node);
9543 ret = 0;
9544 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009545 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009546 ret = -1;
9547 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009548 case XML_RELAXNG_TEXT:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009549 while ((node != NULL) &&
9550 ((node->type == XML_TEXT_NODE) ||
9551 (node->type == XML_COMMENT_NODE) ||
9552 (node->type == XML_PI_NODE) ||
9553 (node->type == XML_CDATA_SECTION_NODE)))
9554 node = node->next;
9555 ctxt->state->seq = node;
9556 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009557 case XML_RELAXNG_ELEMENT:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009558 errNr = ctxt->errNr;
9559 node = xmlRelaxNGSkipIgnored(ctxt, node);
9560 if (node == NULL) {
9561 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name);
9562 ret = -1;
9563 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9564 xmlRelaxNGDumpValidError(ctxt);
9565 break;
9566 }
9567 if (node->type != XML_ELEMENT_NODE) {
9568 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
9569 ret = -1;
9570 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9571 xmlRelaxNGDumpValidError(ctxt);
9572 break;
9573 }
9574 /*
9575 * This node was already validated successfully against
9576 * this definition.
9577 */
Daniel Veillard807daf82004-02-22 22:13:27 +00009578 if (node->psvi == define) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009579 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
9580 if (ctxt->errNr > errNr)
9581 xmlRelaxNGPopErrors(ctxt, errNr);
9582 if (ctxt->errNr != 0) {
9583 while ((ctxt->err != NULL) &&
9584 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME)
9585 && (xmlStrEqual(ctxt->err->arg2, node->name)))
9586 ||
9587 ((ctxt->err->err ==
9588 XML_RELAXNG_ERR_ELEMEXTRANS)
9589 && (xmlStrEqual(ctxt->err->arg1, node->name)))
9590 || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM)
9591 || (ctxt->err->err ==
9592 XML_RELAXNG_ERR_NOTELEM)))
9593 xmlRelaxNGValidErrorPop(ctxt);
9594 }
9595 break;
9596 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009597
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009598 ret = xmlRelaxNGElementMatch(ctxt, define, node);
9599 if (ret <= 0) {
9600 ret = -1;
9601 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9602 xmlRelaxNGDumpValidError(ctxt);
9603 break;
9604 }
9605 ret = 0;
9606 if (ctxt->errNr != 0) {
9607 if (ctxt->errNr > errNr)
9608 xmlRelaxNGPopErrors(ctxt, errNr);
9609 while ((ctxt->err != NULL) &&
9610 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) &&
9611 (xmlStrEqual(ctxt->err->arg2, node->name))) ||
9612 ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) &&
9613 (xmlStrEqual(ctxt->err->arg1, node->name))) ||
9614 (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) ||
9615 (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM)))
9616 xmlRelaxNGValidErrorPop(ctxt);
9617 }
9618 errNr = ctxt->errNr;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009619
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009620 oldflags = ctxt->flags;
9621 if (ctxt->flags & FLAGS_MIXED_CONTENT) {
9622 ctxt->flags -= FLAGS_MIXED_CONTENT;
9623 }
9624 state = xmlRelaxNGNewValidState(ctxt, node);
9625 if (state == NULL) {
9626 ret = -1;
9627 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9628 xmlRelaxNGDumpValidError(ctxt);
9629 break;
9630 }
Daniel Veillard7fe1f3a2003-03-31 22:13:33 +00009631
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009632 oldstate = ctxt->state;
9633 ctxt->state = state;
9634 if (define->attrs != NULL) {
9635 tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
9636 if (tmp != 0) {
9637 ret = -1;
9638 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
9639 }
9640 }
9641 if (define->contModel != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009642 xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state;
9643 xmlRelaxNGStatesPtr tmpstates = ctxt->states;
9644 xmlNodePtr nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009645
Daniel Veillard4c004142003-10-07 11:33:24 +00009646 nstate = xmlRelaxNGNewValidState(ctxt, node);
9647 ctxt->state = nstate;
9648 ctxt->states = NULL;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009649
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009650 tmp = xmlRelaxNGValidateCompiledContent(ctxt,
9651 define->contModel,
9652 ctxt->state->seq);
Daniel Veillard4c004142003-10-07 11:33:24 +00009653 nseq = ctxt->state->seq;
9654 ctxt->state = tmpstate;
9655 ctxt->states = tmpstates;
9656 xmlRelaxNGFreeValidState(ctxt, nstate);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009657
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009658#ifdef DEBUG_COMPILE
Daniel Veillard4c004142003-10-07 11:33:24 +00009659 xmlGenericError(xmlGenericErrorContext,
9660 "Validating content of '%s' : %d\n",
9661 define->name, tmp);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009662#endif
Daniel Veillardce192eb2003-04-16 15:58:05 +00009663 if (tmp != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00009664 ret = -1;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009665
9666 if (ctxt->states != NULL) {
9667 tmp = -1;
9668
Daniel Veillardce192eb2003-04-16 15:58:05 +00009669 for (i = 0; i < ctxt->states->nbState; i++) {
9670 state = ctxt->states->tabState[i];
9671 ctxt->state = state;
Daniel Veillard4c004142003-10-07 11:33:24 +00009672 ctxt->state->seq = nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009673
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009674 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
Daniel Veillardce192eb2003-04-16 15:58:05 +00009675 tmp = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00009676 break;
9677 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009678 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009679 if (tmp != 0) {
9680 /*
9681 * validation error, log the message for the "best" one
9682 */
9683 ctxt->flags |= FLAGS_IGNORABLE;
9684 xmlRelaxNGLogBestError(ctxt);
9685 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009686 for (i = 0; i < ctxt->states->nbState; i++) {
9687 xmlRelaxNGFreeValidState(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00009688 ctxt->states->
9689 tabState[i]);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009690 }
9691 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9692 ctxt->flags = oldflags;
9693 ctxt->states = NULL;
9694 if ((ret == 0) && (tmp == -1))
9695 ret = -1;
9696 } else {
9697 state = ctxt->state;
Daniel Veillard4c004142003-10-07 11:33:24 +00009698 ctxt->state->seq = nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009699 if (ret == 0)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009700 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009701 xmlRelaxNGFreeValidState(ctxt, state);
9702 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009703 } else {
9704 if (define->content != NULL) {
9705 tmp = xmlRelaxNGValidateDefinitionList(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00009706 define->
9707 content);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009708 if (tmp != 0) {
9709 ret = -1;
9710 if (ctxt->state == NULL) {
9711 ctxt->state = oldstate;
9712 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
9713 node->name);
9714 ctxt->state = NULL;
9715 } else {
9716 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
9717 node->name);
9718 }
9719
9720 }
9721 }
9722 if (ctxt->states != NULL) {
9723 tmp = -1;
9724
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009725 for (i = 0; i < ctxt->states->nbState; i++) {
9726 state = ctxt->states->tabState[i];
9727 ctxt->state = state;
9728
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009729 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009730 tmp = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00009731 break;
9732 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009733 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009734 if (tmp != 0) {
9735 /*
9736 * validation error, log the message for the "best" one
9737 */
9738 ctxt->flags |= FLAGS_IGNORABLE;
9739 xmlRelaxNGLogBestError(ctxt);
9740 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009741 for (i = 0; i < ctxt->states->nbState; i++) {
9742 xmlRelaxNGFreeValidState(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00009743 ctxt->states->
9744 tabState[i]);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009745 }
9746 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9747 ctxt->flags = oldflags;
9748 ctxt->states = NULL;
9749 if ((ret == 0) && (tmp == -1))
9750 ret = -1;
9751 } else {
9752 state = ctxt->state;
9753 if (ret == 0)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009754 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009755 xmlRelaxNGFreeValidState(ctxt, state);
9756 }
9757 }
9758 if (ret == 0) {
Daniel Veillard807daf82004-02-22 22:13:27 +00009759 node->psvi = define;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009760 }
9761 ctxt->flags = oldflags;
9762 ctxt->state = oldstate;
9763 if (oldstate != NULL)
9764 oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
9765 if (ret != 0) {
9766 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
9767 xmlRelaxNGDumpValidError(ctxt);
9768 ret = 0;
9769 } else {
9770 ret = -2;
9771 }
9772 } else {
9773 if (ctxt->errNr > errNr)
9774 xmlRelaxNGPopErrors(ctxt, errNr);
9775 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009776
9777#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009778 xmlGenericError(xmlGenericErrorContext,
9779 "xmlRelaxNGValidateDefinition(): validated %s : %d",
9780 node->name, ret);
9781 if (oldstate == NULL)
9782 xmlGenericError(xmlGenericErrorContext, ": no state\n");
9783 else if (oldstate->seq == NULL)
9784 xmlGenericError(xmlGenericErrorContext, ": done\n");
9785 else if (oldstate->seq->type == XML_ELEMENT_NODE)
9786 xmlGenericError(xmlGenericErrorContext, ": next elem %s\n",
9787 oldstate->seq->name);
9788 else
9789 xmlGenericError(xmlGenericErrorContext, ": next %s %d\n",
9790 oldstate->seq->name, oldstate->seq->type);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009791#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009792 break;
9793 case XML_RELAXNG_OPTIONAL:{
9794 errNr = ctxt->errNr;
9795 oldflags = ctxt->flags;
9796 ctxt->flags |= FLAGS_IGNORABLE;
9797 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
9798 ret =
9799 xmlRelaxNGValidateDefinitionList(ctxt,
9800 define->content);
9801 if (ret != 0) {
9802 if (ctxt->state != NULL)
9803 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
9804 ctxt->state = oldstate;
9805 ctxt->flags = oldflags;
9806 ret = 0;
9807 if (ctxt->errNr > errNr)
9808 xmlRelaxNGPopErrors(ctxt, errNr);
9809 break;
9810 }
9811 if (ctxt->states != NULL) {
9812 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
9813 } else {
9814 ctxt->states = xmlRelaxNGNewStates(ctxt, 1);
9815 if (ctxt->states == NULL) {
9816 xmlRelaxNGFreeValidState(ctxt, oldstate);
9817 ctxt->flags = oldflags;
9818 ret = -1;
9819 if (ctxt->errNr > errNr)
9820 xmlRelaxNGPopErrors(ctxt, errNr);
9821 break;
9822 }
9823 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
9824 xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state);
9825 ctxt->state = NULL;
9826 }
9827 ctxt->flags = oldflags;
9828 ret = 0;
9829 if (ctxt->errNr > errNr)
9830 xmlRelaxNGPopErrors(ctxt, errNr);
9831 break;
9832 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009833 case XML_RELAXNG_ONEORMORE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009834 errNr = ctxt->errNr;
9835 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
9836 if (ret != 0) {
9837 break;
9838 }
9839 if (ctxt->errNr > errNr)
9840 xmlRelaxNGPopErrors(ctxt, errNr);
9841 /* no break on purpose */
9842 case XML_RELAXNG_ZEROORMORE:{
9843 int progress;
9844 xmlRelaxNGStatesPtr states = NULL, res = NULL;
9845 int base, j;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009846
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009847 errNr = ctxt->errNr;
9848 res = xmlRelaxNGNewStates(ctxt, 1);
9849 if (res == NULL) {
9850 ret = -1;
9851 break;
9852 }
9853 /*
9854 * All the input states are also exit states
9855 */
9856 if (ctxt->state != NULL) {
9857 xmlRelaxNGAddStates(ctxt, res,
9858 xmlRelaxNGCopyValidState(ctxt,
9859 ctxt->
9860 state));
9861 } else {
9862 for (j = 0; j < ctxt->states->nbState; j++) {
9863 xmlRelaxNGAddStates(ctxt, res,
9864 xmlRelaxNGCopyValidState(ctxt,
9865 ctxt->
9866 states->
9867 tabState
9868 [j]));
9869 }
9870 }
9871 oldflags = ctxt->flags;
9872 ctxt->flags |= FLAGS_IGNORABLE;
9873 do {
9874 progress = 0;
9875 base = res->nbState;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009876
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009877 if (ctxt->states != NULL) {
9878 states = ctxt->states;
9879 for (i = 0; i < states->nbState; i++) {
9880 ctxt->state = states->tabState[i];
9881 ctxt->states = NULL;
9882 ret = xmlRelaxNGValidateDefinitionList(ctxt,
9883 define->
9884 content);
9885 if (ret == 0) {
9886 if (ctxt->state != NULL) {
9887 tmp = xmlRelaxNGAddStates(ctxt, res,
9888 ctxt->state);
9889 ctxt->state = NULL;
9890 if (tmp == 1)
9891 progress = 1;
9892 } else if (ctxt->states != NULL) {
9893 for (j = 0; j < ctxt->states->nbState;
9894 j++) {
9895 tmp =
9896 xmlRelaxNGAddStates(ctxt, res,
9897 ctxt->
9898 states->
9899 tabState
9900 [j]);
9901 if (tmp == 1)
9902 progress = 1;
9903 }
9904 xmlRelaxNGFreeStates(ctxt,
9905 ctxt->states);
9906 ctxt->states = NULL;
9907 }
9908 } else {
9909 if (ctxt->state != NULL) {
9910 xmlRelaxNGFreeValidState(ctxt,
9911 ctxt->state);
9912 ctxt->state = NULL;
9913 }
9914 }
9915 }
9916 } else {
9917 ret = xmlRelaxNGValidateDefinitionList(ctxt,
9918 define->
9919 content);
9920 if (ret != 0) {
9921 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
9922 ctxt->state = NULL;
9923 } else {
9924 base = res->nbState;
9925 if (ctxt->state != NULL) {
9926 tmp = xmlRelaxNGAddStates(ctxt, res,
9927 ctxt->state);
9928 ctxt->state = NULL;
9929 if (tmp == 1)
9930 progress = 1;
9931 } else if (ctxt->states != NULL) {
9932 for (j = 0; j < ctxt->states->nbState; j++) {
9933 tmp = xmlRelaxNGAddStates(ctxt, res,
9934 ctxt->
9935 states->
9936 tabState[j]);
9937 if (tmp == 1)
9938 progress = 1;
9939 }
9940 if (states == NULL) {
9941 states = ctxt->states;
9942 } else {
9943 xmlRelaxNGFreeStates(ctxt,
9944 ctxt->states);
9945 }
9946 ctxt->states = NULL;
9947 }
9948 }
9949 }
9950 if (progress) {
9951 /*
9952 * Collect all the new nodes added at that step
9953 * and make them the new node set
9954 */
9955 if (res->nbState - base == 1) {
9956 ctxt->state = xmlRelaxNGCopyValidState(ctxt,
9957 res->
9958 tabState
9959 [base]);
9960 } else {
9961 if (states == NULL) {
9962 xmlRelaxNGNewStates(ctxt,
9963 res->nbState - base);
9964 }
9965 states->nbState = 0;
9966 for (i = base; i < res->nbState; i++)
9967 xmlRelaxNGAddStates(ctxt, states,
9968 xmlRelaxNGCopyValidState
9969 (ctxt,
9970 res->tabState[i]));
9971 ctxt->states = states;
9972 }
9973 }
9974 } while (progress == 1);
9975 if (states != NULL) {
9976 xmlRelaxNGFreeStates(ctxt, states);
9977 }
9978 ctxt->states = res;
9979 ctxt->flags = oldflags;
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00009980#if 0
9981 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00009982 * errors may have to be propagated back...
9983 */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009984 if (ctxt->errNr > errNr)
9985 xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00009986#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009987 ret = 0;
9988 break;
9989 }
9990 case XML_RELAXNG_CHOICE:{
9991 xmlRelaxNGDefinePtr list = NULL;
9992 xmlRelaxNGStatesPtr states = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009993
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009994 node = xmlRelaxNGSkipIgnored(ctxt, node);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009995
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009996 errNr = ctxt->errNr;
9997 if ((define->dflags & IS_TRIABLE)
9998 && (define->data != NULL)) {
9999 xmlHashTablePtr triage =
10000 (xmlHashTablePtr) define->data;
Daniel Veillarde063f482003-03-21 16:53:17 +000010001
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010002 /*
10003 * Something we can optimize cleanly there is only one
10004 * possble branch out !
10005 */
10006 if (node == NULL) {
10007 ret = -1;
10008 break;
10009 }
10010 if ((node->type == XML_TEXT_NODE) ||
10011 (node->type == XML_CDATA_SECTION_NODE)) {
10012 list =
10013 xmlHashLookup2(triage, BAD_CAST "#text", NULL);
10014 } else if (node->type == XML_ELEMENT_NODE) {
10015 if (node->ns != NULL) {
10016 list = xmlHashLookup2(triage, node->name,
10017 node->ns->href);
10018 if (list == NULL)
10019 list =
10020 xmlHashLookup2(triage, BAD_CAST "#any",
10021 node->ns->href);
10022 } else
10023 list =
10024 xmlHashLookup2(triage, node->name, NULL);
10025 if (list == NULL)
10026 list =
10027 xmlHashLookup2(triage, BAD_CAST "#any",
10028 NULL);
10029 }
10030 if (list == NULL) {
10031 ret = -1;
10032 break;
10033 }
10034 ret = xmlRelaxNGValidateDefinition(ctxt, list);
10035 if (ret == 0) {
10036 }
10037 break;
10038 }
Daniel Veillarde063f482003-03-21 16:53:17 +000010039
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010040 list = define->content;
10041 oldflags = ctxt->flags;
10042 ctxt->flags |= FLAGS_IGNORABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010043
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010044 while (list != NULL) {
10045 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
10046 ret = xmlRelaxNGValidateDefinition(ctxt, list);
10047 if (ret == 0) {
10048 if (states == NULL) {
10049 states = xmlRelaxNGNewStates(ctxt, 1);
10050 }
10051 if (ctxt->state != NULL) {
10052 xmlRelaxNGAddStates(ctxt, states, ctxt->state);
10053 } else if (ctxt->states != NULL) {
10054 for (i = 0; i < ctxt->states->nbState; i++) {
10055 xmlRelaxNGAddStates(ctxt, states,
10056 ctxt->states->
10057 tabState[i]);
10058 }
10059 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10060 ctxt->states = NULL;
10061 }
10062 } else {
10063 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10064 }
10065 ctxt->state = oldstate;
10066 list = list->next;
10067 }
10068 if (states != NULL) {
10069 xmlRelaxNGFreeValidState(ctxt, oldstate);
10070 ctxt->states = states;
10071 ctxt->state = NULL;
10072 ret = 0;
10073 } else {
10074 ctxt->states = NULL;
10075 }
10076 ctxt->flags = oldflags;
10077 if (ret != 0) {
10078 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
10079 xmlRelaxNGDumpValidError(ctxt);
10080 }
10081 } else {
10082 if (ctxt->errNr > errNr)
10083 xmlRelaxNGPopErrors(ctxt, errNr);
10084 }
10085 break;
10086 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010087 case XML_RELAXNG_DEF:
10088 case XML_RELAXNG_GROUP:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010089 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
10090 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010091 case XML_RELAXNG_INTERLEAVE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010092 ret = xmlRelaxNGValidateInterleave(ctxt, define);
10093 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010094 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010095 ret = xmlRelaxNGValidateAttribute(ctxt, define);
10096 break;
Daniel Veillardf4e55762003-04-15 23:32:22 +000010097 case XML_RELAXNG_START:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010098 case XML_RELAXNG_NOOP:
Daniel Veillardfd573f12003-03-16 17:52:32 +000010099 case XML_RELAXNG_REF:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010100 case XML_RELAXNG_EXTERNALREF:
Daniel Veillard952379b2003-03-17 15:37:12 +000010101 case XML_RELAXNG_PARENTREF:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010102 ret = xmlRelaxNGValidateDefinition(ctxt, define->content);
10103 break;
10104 case XML_RELAXNG_DATATYPE:{
10105 xmlNodePtr child;
10106 xmlChar *content = NULL;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010107
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010108 child = node;
10109 while (child != NULL) {
10110 if (child->type == XML_ELEMENT_NODE) {
10111 VALID_ERR2(XML_RELAXNG_ERR_DATAELEM,
10112 node->parent->name);
10113 ret = -1;
10114 break;
10115 } else if ((child->type == XML_TEXT_NODE) ||
10116 (child->type == XML_CDATA_SECTION_NODE)) {
10117 content = xmlStrcat(content, child->content);
10118 }
10119 /* TODO: handle entities ... */
10120 child = child->next;
10121 }
10122 if (ret == -1) {
10123 if (content != NULL)
10124 xmlFree(content);
10125 break;
10126 }
10127 if (content == NULL) {
10128 content = xmlStrdup(BAD_CAST "");
10129 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010130 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010131 ret = -1;
10132 break;
10133 }
10134 }
10135 ret = xmlRelaxNGValidateDatatype(ctxt, content, define,
10136 ctxt->state->seq);
10137 if (ret == -1) {
10138 VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name);
10139 } else if (ret == 0) {
10140 ctxt->state->seq = NULL;
10141 }
10142 if (content != NULL)
10143 xmlFree(content);
10144 break;
10145 }
10146 case XML_RELAXNG_VALUE:{
10147 xmlChar *content = NULL;
10148 xmlChar *oldvalue;
10149 xmlNodePtr child;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010150
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010151 child = node;
10152 while (child != NULL) {
10153 if (child->type == XML_ELEMENT_NODE) {
10154 VALID_ERR2(XML_RELAXNG_ERR_VALELEM,
10155 node->parent->name);
10156 ret = -1;
10157 break;
10158 } else if ((child->type == XML_TEXT_NODE) ||
10159 (child->type == XML_CDATA_SECTION_NODE)) {
10160 content = xmlStrcat(content, child->content);
10161 }
10162 /* TODO: handle entities ... */
10163 child = child->next;
10164 }
10165 if (ret == -1) {
10166 if (content != NULL)
10167 xmlFree(content);
10168 break;
10169 }
10170 if (content == NULL) {
10171 content = xmlStrdup(BAD_CAST "");
10172 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010173 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010174 ret = -1;
10175 break;
10176 }
10177 }
10178 oldvalue = ctxt->state->value;
10179 ctxt->state->value = content;
10180 ret = xmlRelaxNGValidateValue(ctxt, define);
10181 ctxt->state->value = oldvalue;
10182 if (ret == -1) {
10183 VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name);
10184 } else if (ret == 0) {
10185 ctxt->state->seq = NULL;
10186 }
10187 if (content != NULL)
10188 xmlFree(content);
10189 break;
10190 }
10191 case XML_RELAXNG_LIST:{
10192 xmlChar *content;
10193 xmlNodePtr child;
10194 xmlChar *oldvalue, *oldendvalue;
10195 int len;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010196
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010197 /*
10198 * Make sure it's only text nodes
10199 */
10200
10201 content = NULL;
10202 child = node;
10203 while (child != NULL) {
10204 if (child->type == XML_ELEMENT_NODE) {
10205 VALID_ERR2(XML_RELAXNG_ERR_LISTELEM,
10206 node->parent->name);
10207 ret = -1;
10208 break;
10209 } else if ((child->type == XML_TEXT_NODE) ||
10210 (child->type == XML_CDATA_SECTION_NODE)) {
10211 content = xmlStrcat(content, child->content);
10212 }
10213 /* TODO: handle entities ... */
10214 child = child->next;
10215 }
10216 if (ret == -1) {
10217 if (content != NULL)
10218 xmlFree(content);
10219 break;
10220 }
10221 if (content == NULL) {
10222 content = xmlStrdup(BAD_CAST "");
10223 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010224 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010225 ret = -1;
10226 break;
10227 }
10228 }
10229 len = xmlStrlen(content);
10230 oldvalue = ctxt->state->value;
10231 oldendvalue = ctxt->state->endvalue;
10232 ctxt->state->value = content;
10233 ctxt->state->endvalue = content + len;
10234 ret = xmlRelaxNGValidateValue(ctxt, define);
10235 ctxt->state->value = oldvalue;
10236 ctxt->state->endvalue = oldendvalue;
10237 if (ret == -1) {
10238 VALID_ERR(XML_RELAXNG_ERR_LIST);
10239 } else if ((ret == 0) && (node != NULL)) {
10240 ctxt->state->seq = node->next;
10241 }
10242 if (content != NULL)
10243 xmlFree(content);
10244 break;
10245 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010246 case XML_RELAXNG_EXCEPT:
10247 case XML_RELAXNG_PARAM:
10248 TODO ret = -1;
10249 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010250 }
10251 ctxt->depth--;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010252#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010253 for (i = 0; i < ctxt->depth; i++)
10254 xmlGenericError(xmlGenericErrorContext, " ");
Daniel Veillardfd573f12003-03-16 17:52:32 +000010255 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010256 "Validating %s ", xmlRelaxNGDefName(define));
Daniel Veillardfd573f12003-03-16 17:52:32 +000010257 if (define->name != NULL)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010258 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010259 if (ret == 0)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010260 xmlGenericError(xmlGenericErrorContext, "suceeded\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +000010261 else
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010262 xmlGenericError(xmlGenericErrorContext, "failed\n");
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010263#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010264 return (ret);
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010265}
10266
10267/**
Daniel Veillardfd573f12003-03-16 17:52:32 +000010268 * xmlRelaxNGValidateDefinition:
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010269 * @ctxt: a Relax-NG validation context
10270 * @define: the definition to verify
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010271 *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010272 * Validate the current node lists against the definition
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010273 *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010274 * Returns 0 if the validation succeeded or an error code.
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010275 */
10276static int
Daniel Veillard4c004142003-10-07 11:33:24 +000010277xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
10278 xmlRelaxNGDefinePtr define)
10279{
Daniel Veillardfd573f12003-03-16 17:52:32 +000010280 xmlRelaxNGStatesPtr states, res;
10281 int i, j, k, ret, oldflags;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010282
Daniel Veillardfd573f12003-03-16 17:52:32 +000010283 /*
10284 * We should NOT have both ctxt->state and ctxt->states
10285 */
10286 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010287 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10288 ctxt->state = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010289 }
10290
10291 if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010292 if (ctxt->states != NULL) {
10293 ctxt->state = ctxt->states->tabState[0];
10294 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10295 ctxt->states = NULL;
10296 }
10297 ret = xmlRelaxNGValidateState(ctxt, define);
10298 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10299 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10300 ctxt->state = NULL;
10301 }
10302 if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) {
10303 ctxt->state = ctxt->states->tabState[0];
10304 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10305 ctxt->states = NULL;
10306 }
10307 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010308 }
10309
10310 states = ctxt->states;
10311 ctxt->states = NULL;
10312 res = NULL;
10313 j = 0;
10314 oldflags = ctxt->flags;
10315 ctxt->flags |= FLAGS_IGNORABLE;
Daniel Veillard4c004142003-10-07 11:33:24 +000010316 for (i = 0; i < states->nbState; i++) {
10317 ctxt->state = states->tabState[i];
10318 ctxt->states = NULL;
10319 ret = xmlRelaxNGValidateState(ctxt, define);
10320 /*
10321 * We should NOT have both ctxt->state and ctxt->states
10322 */
10323 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10324 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10325 ctxt->state = NULL;
10326 }
10327 if (ret == 0) {
10328 if (ctxt->states == NULL) {
10329 if (res != NULL) {
10330 /* add the state to the container */
10331 xmlRelaxNGAddStates(ctxt, res, ctxt->state);
10332 ctxt->state = NULL;
10333 } else {
10334 /* add the state directly in states */
10335 states->tabState[j++] = ctxt->state;
10336 ctxt->state = NULL;
10337 }
10338 } else {
10339 if (res == NULL) {
10340 /* make it the new container and copy other results */
10341 res = ctxt->states;
10342 ctxt->states = NULL;
10343 for (k = 0; k < j; k++)
10344 xmlRelaxNGAddStates(ctxt, res,
10345 states->tabState[k]);
10346 } else {
10347 /* add all the new results to res and reff the container */
10348 for (k = 0; k < ctxt->states->nbState; k++)
10349 xmlRelaxNGAddStates(ctxt, res,
10350 ctxt->states->tabState[k]);
10351 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10352 ctxt->states = NULL;
10353 }
10354 }
10355 } else {
10356 if (ctxt->state != NULL) {
10357 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10358 ctxt->state = NULL;
10359 } else if (ctxt->states != NULL) {
10360 for (k = 0; k < ctxt->states->nbState; k++)
10361 xmlRelaxNGFreeValidState(ctxt,
10362 ctxt->states->tabState[k]);
10363 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10364 ctxt->states = NULL;
10365 }
10366 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010367 }
10368 ctxt->flags = oldflags;
10369 if (res != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010370 xmlRelaxNGFreeStates(ctxt, states);
10371 ctxt->states = res;
10372 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010373 } else if (j > 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010374 states->nbState = j;
10375 ctxt->states = states;
10376 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010377 } else if (j == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010378 ctxt->state = states->tabState[0];
10379 xmlRelaxNGFreeStates(ctxt, states);
10380 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010381 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +000010382 ret = -1;
10383 xmlRelaxNGFreeStates(ctxt, states);
10384 if (ctxt->states != NULL) {
10385 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10386 ctxt->states = NULL;
10387 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010388 }
10389 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010390 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10391 ctxt->state = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010392 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010393 return (ret);
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010394}
10395
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010396/**
Daniel Veillard6eadf632003-01-23 18:29:16 +000010397 * xmlRelaxNGValidateDocument:
10398 * @ctxt: a Relax-NG validation context
10399 * @doc: the document
10400 *
10401 * Validate the given document
10402 *
10403 * Returns 0 if the validation succeeded or an error code.
10404 */
10405static int
Daniel Veillard4c004142003-10-07 11:33:24 +000010406xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
10407{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010408 int ret;
10409 xmlRelaxNGPtr schema;
10410 xmlRelaxNGGrammarPtr grammar;
10411 xmlRelaxNGValidStatePtr state;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010412 xmlNodePtr node;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010413
10414 if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +000010415 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010416
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010417 ctxt->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010418 schema = ctxt->schema;
10419 grammar = schema->topgrammar;
10420 if (grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010421 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
10422 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010423 }
10424 state = xmlRelaxNGNewValidState(ctxt, NULL);
10425 ctxt->state = state;
10426 ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010427 if ((ctxt->state != NULL) && (state->seq != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010428 state = ctxt->state;
10429 node = state->seq;
10430 node = xmlRelaxNGSkipIgnored(ctxt, node);
10431 if (node != NULL) {
10432 if (ret != -1) {
10433 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10434 ret = -1;
10435 }
10436 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010437 } else if (ctxt->states != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010438 int i;
10439 int tmp = -1;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010440
Daniel Veillard4c004142003-10-07 11:33:24 +000010441 for (i = 0; i < ctxt->states->nbState; i++) {
10442 state = ctxt->states->tabState[i];
10443 node = state->seq;
10444 node = xmlRelaxNGSkipIgnored(ctxt, node);
10445 if (node == NULL)
10446 tmp = 0;
10447 xmlRelaxNGFreeValidState(ctxt, state);
10448 }
10449 if (tmp == -1) {
10450 if (ret != -1) {
10451 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10452 ret = -1;
10453 }
10454 }
Daniel Veillard6eadf632003-01-23 18:29:16 +000010455 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +000010456 if (ctxt->state != NULL) {
10457 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillard4c004142003-10-07 11:33:24 +000010458 ctxt->state = NULL;
Daniel Veillardbbb78b52003-03-21 01:24:45 +000010459 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010460 if (ret != 0)
10461 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillard580ced82003-03-21 21:22:48 +000010462#ifdef DEBUG
10463 else if (ctxt->errNr != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010464 ctxt->error(ctxt->userData,
10465 "%d Extra error messages left on stack !\n",
10466 ctxt->errNr);
10467 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillard580ced82003-03-21 21:22:48 +000010468 }
10469#endif
Daniel Veillardf54cd532004-02-25 11:52:31 +000010470#ifdef LIBXML_VALID_ENABLED
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010471 if (ctxt->idref == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010472 xmlValidCtxt vctxt;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010473
Daniel Veillard4c004142003-10-07 11:33:24 +000010474 memset(&vctxt, 0, sizeof(xmlValidCtxt));
10475 vctxt.valid = 1;
10476 vctxt.error = ctxt->error;
10477 vctxt.warning = ctxt->warning;
10478 vctxt.userData = ctxt->userData;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010479
Daniel Veillard4c004142003-10-07 11:33:24 +000010480 if (xmlValidateDocumentFinal(&vctxt, doc) != 1)
10481 ret = -1;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010482 }
Daniel Veillardf54cd532004-02-25 11:52:31 +000010483#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010484 if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK))
Daniel Veillard4c004142003-10-07 11:33:24 +000010485 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010486
Daniel Veillard4c004142003-10-07 11:33:24 +000010487 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010488}
10489
Daniel Veillardfd573f12003-03-16 17:52:32 +000010490/************************************************************************
10491 * *
10492 * Validation interfaces *
10493 * *
10494 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +000010495
Daniel Veillard6eadf632003-01-23 18:29:16 +000010496/**
10497 * xmlRelaxNGNewValidCtxt:
10498 * @schema: a precompiled XML RelaxNGs
10499 *
10500 * Create an XML RelaxNGs validation context based on the given schema
10501 *
10502 * Returns the validation context or NULL in case of error
10503 */
10504xmlRelaxNGValidCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +000010505xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema)
10506{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010507 xmlRelaxNGValidCtxtPtr ret;
10508
10509 ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt));
10510 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010511 xmlRngVErrMemory(NULL, "building context\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +000010512 return (NULL);
10513 }
10514 memset(ret, 0, sizeof(xmlRelaxNGValidCtxt));
10515 ret->schema = schema;
Daniel Veillard1703c5f2003-02-10 14:28:44 +000010516 ret->error = xmlGenericError;
10517 ret->userData = xmlGenericErrorContext;
Daniel Veillard42f12e92003-03-07 18:32:59 +000010518 ret->errNr = 0;
10519 ret->errMax = 0;
10520 ret->err = NULL;
10521 ret->errTab = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010522 ret->idref = schema->idref;
Daniel Veillard798024a2003-03-19 10:36:09 +000010523 ret->states = NULL;
10524 ret->freeState = NULL;
10525 ret->freeStates = NULL;
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010526 ret->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010527 return (ret);
10528}
10529
10530/**
10531 * xmlRelaxNGFreeValidCtxt:
10532 * @ctxt: the schema validation context
10533 *
10534 * Free the resources associated to the schema validation context
10535 */
10536void
Daniel Veillard4c004142003-10-07 11:33:24 +000010537xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt)
10538{
Daniel Veillard798024a2003-03-19 10:36:09 +000010539 int k;
10540
Daniel Veillard6eadf632003-01-23 18:29:16 +000010541 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010542 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010543 if (ctxt->states != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010544 xmlRelaxNGFreeStates(NULL, ctxt->states);
Daniel Veillard798024a2003-03-19 10:36:09 +000010545 if (ctxt->freeState != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010546 for (k = 0; k < ctxt->freeState->nbState; k++) {
10547 xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]);
10548 }
10549 xmlRelaxNGFreeStates(NULL, ctxt->freeState);
Daniel Veillard798024a2003-03-19 10:36:09 +000010550 }
Daniel Veillard798024a2003-03-19 10:36:09 +000010551 if (ctxt->freeStates != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010552 for (k = 0; k < ctxt->freeStatesNr; k++) {
10553 xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]);
10554 }
10555 xmlFree(ctxt->freeStates);
Daniel Veillard798024a2003-03-19 10:36:09 +000010556 }
Daniel Veillard42f12e92003-03-07 18:32:59 +000010557 if (ctxt->errTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010558 xmlFree(ctxt->errTab);
Daniel Veillardf4e55762003-04-15 23:32:22 +000010559 if (ctxt->elemTab != NULL) {
10560 xmlRegExecCtxtPtr exec;
10561
Daniel Veillard4c004142003-10-07 11:33:24 +000010562 exec = xmlRelaxNGElemPop(ctxt);
10563 while (exec != NULL) {
10564 xmlRegFreeExecCtxt(exec);
10565 exec = xmlRelaxNGElemPop(ctxt);
10566 }
10567 xmlFree(ctxt->elemTab);
Daniel Veillardf4e55762003-04-15 23:32:22 +000010568 }
Daniel Veillard6eadf632003-01-23 18:29:16 +000010569 xmlFree(ctxt);
10570}
10571
10572/**
10573 * xmlRelaxNGSetValidErrors:
10574 * @ctxt: a Relax-NG validation context
10575 * @err: the error function
10576 * @warn: the warning function
10577 * @ctx: the functions context
10578 *
10579 * Set the error and warning callback informations
10580 */
10581void
10582xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +000010583 xmlRelaxNGValidityErrorFunc err,
10584 xmlRelaxNGValidityWarningFunc warn, void *ctx)
10585{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010586 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010587 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010588 ctxt->error = err;
10589 ctxt->warning = warn;
10590 ctxt->userData = ctx;
10591}
10592
10593/**
Daniel Veillard409a8142003-07-18 15:16:57 +000010594 * xmlRelaxNGGetValidErrors:
10595 * @ctxt: a Relax-NG validation context
10596 * @err: the error function result
10597 * @warn: the warning function result
10598 * @ctx: the functions context result
10599 *
10600 * Get the error and warning callback informations
10601 *
10602 * Returns -1 in case of error and 0 otherwise
10603 */
10604int
10605xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +000010606 xmlRelaxNGValidityErrorFunc * err,
10607 xmlRelaxNGValidityWarningFunc * warn, void **ctx)
10608{
Daniel Veillard409a8142003-07-18 15:16:57 +000010609 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010610 return (-1);
10611 if (err != NULL)
10612 *err = ctxt->error;
10613 if (warn != NULL)
10614 *warn = ctxt->warning;
10615 if (ctx != NULL)
10616 *ctx = ctxt->userData;
10617 return (0);
Daniel Veillard409a8142003-07-18 15:16:57 +000010618}
10619
10620/**
Daniel Veillard6eadf632003-01-23 18:29:16 +000010621 * xmlRelaxNGValidateDoc:
10622 * @ctxt: a Relax-NG validation context
10623 * @doc: a parsed document tree
10624 *
10625 * Validate a document tree in memory.
10626 *
10627 * Returns 0 if the document is valid, a positive error code
10628 * number otherwise and -1 in case of internal or API error.
10629 */
10630int
Daniel Veillard4c004142003-10-07 11:33:24 +000010631xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
10632{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010633 int ret;
10634
10635 if ((ctxt == NULL) || (doc == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +000010636 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010637
10638 ctxt->doc = doc;
10639
10640 ret = xmlRelaxNGValidateDocument(ctxt, doc);
Daniel Veillard71531f32003-02-05 13:19:53 +000010641 /*
10642 * TODO: build error codes
10643 */
10644 if (ret == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +000010645 return (1);
10646 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010647}
10648
10649#endif /* LIBXML_SCHEMAS_ENABLED */